Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Greek2Greek.cpp

00001 //*****************************************************************************
00002 // Author      : William Dicks                                              ***
00003 // Date Created: 10 February 1998                                           ***
00004 // Purpose     : Implementation for Greek to b-Greek conversion and vice    ***
00005 //             : versa.                                                     ***
00006 // File Name   : Greek2Greek.cpp                                            ***
00007 //                                                                          ***
00008 // Author info : ---------------------------------------------------------- ***
00009 //     Address : 23 Tieroogpark                                             ***
00010 //             : Hoewe Str                                                  ***
00011 //             : Elarduspark X3                                             ***
00012 //             : 0181                                                       ***
00013 //             : South Africa                                               ***
00014 //     Home Tel: +27 (0)12 345 3166                                         ***
00015 //     Cell No : +27 (0)82 577 4424                                         ***
00016 //     e-mail  : wd@isis.co.za                                              ***
00017 // Church WWW  : http://www.hatfield.co.za                                  ***
00018 //                                                                          ***
00019 // Bugfix info : ---------------------------------------------------------- ***
00020 // Bug #1      : Greek Font character 197 converted to b-Greek "6"          ***
00021 // Date Fixed  : 23 February 1998                                           ***
00022 //*****************************************************************************
00023 
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <ctype.h>
00027 
00028 #include "Greek2Greek.h"
00029 #include "GreekChars.h"
00030 
00031 //*****************************************************************************
00032 // Used to convert a string created by using the Greek font supplied with the
00033 // Sword Project to a string that conforms to the b-Greek discussion list 
00034 // method of transliteration.
00035 //*****************************************************************************
00036 
00037 unsigned char Greek2bGreek(
00038          unsigned char *sResult, 
00039          unsigned char *sGreekText, 
00040          int nMaxResultBuflen)
00041 {
00042    char error;
00043 
00044    unsigned int NoOfChars = ParseGreek(sResult, sGreekText, nMaxResultBuflen);
00045 
00046    if (NoOfChars < strlen((char *)sGreekText))
00047       error = 1;
00048    else
00049       error = 0;
00050 
00051    return error;
00052 }
00053 
00054 //*****************************************************************************
00055 // Used to convert a string created by using the b-Greek method of 
00056 // transliteration to a string that can be converted to a Greek-font readable 
00057 // string.
00058 //*****************************************************************************
00059 
00060 unsigned char bGreek2Greek(
00061          unsigned char *sResult, 
00062          unsigned char *sGreekText, 
00063          int nMaxResultBuflen)
00064 {
00065    unsigned char error;
00066 
00067    unsigned int NoOfChars = ParsebGreek(sResult, sGreekText, nMaxResultBuflen);
00068 
00069    if (NoOfChars < strlen((char *)sGreekText))
00070       error = 1;
00071    else
00072       error = 0;
00073 
00074    return error;
00075 }
00076 
00077 //*****************************************************************************
00078 // Parse a Greek font created string and return the b-Greek equivalent
00079 //*****************************************************************************
00080 
00081 int ParseGreek(
00082          unsigned char *sResult, 
00083          unsigned char *sGreekText, 
00084          int nMaxResultBuflen)
00085 {
00086    int characters = 0;
00087    int index = 0;
00088    unsigned char tmp;
00089    bool iota;        // true = IOTA subscript; false = No IOTA
00090    bool breathing;   // true = add breathing; false = no breathing
00091    bool rough;       // true = rough breathing; false = smooth
00092 
00093    // While text is not equal to NULL pointer
00094 
00095    while (sGreekText[index] && characters < nMaxResultBuflen)
00096    {
00097       iota = breathing = rough = false;
00098       tmp = Font2char(sGreekText[index++], iota, breathing, rough);
00099 
00100       if (breathing)
00101       {
00102          if (rough)  // Rough breathing
00103          {
00104             sResult[characters++] = ROUGH;      // Add rough breathing "h"
00105             sResult[characters++] = tmp;        // Insert char
00106          }
00107          else
00108             sResult[characters++] = tmp;           // Insert char
00109       }
00110       else
00111       {
00112          if (iota)   // IOTA subscript
00113          {
00114             sResult[characters++] = tmp;        // Insert char
00115             sResult[characters++] = IOTA_SUB;   // Add Iota subscript
00116          }
00117          else
00118             sResult[characters++] = tmp;           // Insert char
00119       }
00120    }
00121    sResult[characters] = 0;                                     // Terminate the string
00122 
00123    return index;
00124 }
00125 
00126 //*****************************************************************************
00127 // Parse a b-Greek string and return the Greek font equivalent
00128 //*****************************************************************************
00129 int ParsebGreek(
00130          unsigned char *sResult, 
00131          unsigned char *sGreekText, 
00132          int nMaxResultBuflen)
00133 {
00134    int characters = 0;
00135    int index = 0;
00136    bool iota = false;        // true = IOTA subscript; false = No IOTA
00137    bool breathing = false;   // true = add breathing; false = no breathing
00138    bool rough = false;       // true = rough breathing; false = smooth
00139    bool fSigma = false;      // Final sigma flag
00140    bool nChar = true;        // New char flag
00141 
00142    // While text is not equal to NULL pointer
00143 
00144    while (*sGreekText || characters < nMaxResultBuflen)
00145    {
00146       if (nChar)
00147       {
00148          if (*sGreekText == (unsigned char)ROUGH)
00149          {
00150             rough = true;
00151             breathing = true;
00152          }
00153          else
00154          {
00155             rough = false;
00156             breathing = true;
00157          }
00158          
00159          nChar = false;
00160       }
00161       else if (isPunctSpace(*(sGreekText + 1)))
00162       {
00163          fSigma = true;
00164       }
00165       else if (*(sGreekText + 1) == (unsigned char)IOTA_SUB)
00166       {
00167          iota = true;
00168       }
00169       
00170       if (*sGreekText != (unsigned char)IOTA_SUB)
00171       {
00172          if (*sGreekText == (unsigned char)' ')
00173          {
00174             nChar = true;
00175          }
00176 
00177          if (breathing)
00178          {
00179             if (rough)
00180             {
00181                // When we read a rough breather we want to increment the pointer 
00182                // to the right character before char2Font is called.
00183 
00184                sResult[index++] = 
00185                   char2Font(*++sGreekText, fSigma, iota, breathing, rough);
00186 
00187                sGreekText++;
00188                characters++;
00189             }
00190             else
00191             {
00192                sResult[index++] = 
00193                   char2Font(*sGreekText++, fSigma, iota, breathing, rough);
00194                characters++;
00195             }
00196          }
00197          else
00198          {
00199             sResult[index++] = 
00200                char2Font(*sGreekText++, fSigma, iota, breathing, rough);
00201             characters++;
00202          }
00203       }
00204       else
00205       {
00206          sGreekText++;
00207          characters++;
00208       }
00209 
00210       fSigma = iota = breathing = rough = false;
00211    }
00212 
00213    sResult[index] = 0;                                  // Terminate the string
00214 
00215    return characters;
00216 }
00217 
00218 
00219 //*****************************************************************************
00220 // Convert a character to a GREEK font character
00221 //*****************************************************************************
00222 unsigned char char2Font(
00223                unsigned char letter,      // bGreek letter to convert to Font letter
00224                bool finalSigma,  // Is it a final SIGMA
00225                bool iota,        // true = IOTA subscript; false = No IOTA
00226                bool breathing,   // true = add breathing; false = no breathing
00227                bool rough)       // true = rough breathing; false = smooth
00228 {
00229    unsigned char charFont = 0;
00230 
00231    switch (letter)
00232    {
00233       case ALPHA:            // A
00234          if (breathing)
00235          {
00236             if (rough)
00237             {
00238                charFont = (unsigned char)gROUGH_ALPHA;
00239             }
00240             else
00241                charFont = (unsigned char)gNON_ROUGH_ALPHA;
00242          }
00243          else
00244          {
00245             if (iota)
00246             {
00247                charFont = (unsigned char)gIOTA_ALPHA;
00248             }
00249             else
00250                charFont = (unsigned char)gALPHA;
00251          }
00252 
00253       break;
00254 
00255       case BETA:             // B
00256          charFont = (unsigned char)gBETA;
00257 
00258       break;
00259 
00260       case CHI:              // C
00261          charFont = (unsigned char)gCHI;
00262 
00263       break;
00264 
00265       case DELTA:            // D
00266          charFont = (unsigned char)gDELTA;
00267 
00268       break;
00269 
00270       case EPSILON:          // E
00271          if (breathing)
00272          {
00273             if (rough)
00274             {
00275                charFont = (unsigned char)gROUGH_EPSILON;
00276             }
00277             else
00278                charFont = (unsigned char)gNON_ROUGH_EPSILON;
00279          }
00280          else
00281          {
00282             charFont = (unsigned char)gEPSILON;
00283          }
00284 
00285       break;
00286 
00287       case PHI:              // F
00288          charFont = (unsigned char)gPHI;
00289 
00290       break;
00291 
00292       case GAMMA:            // G
00293          charFont = (unsigned char)gGAMMA;
00294 
00295       break;
00296 
00297       case ETA:              // H
00298          if (breathing)
00299          {
00300             if (rough)
00301             {
00302                charFont = (unsigned char)gROUGH_ETA;
00303             }
00304             else
00305                charFont = (unsigned char)gNON_ROUGH_ETA;
00306          }
00307          else
00308          {
00309             if (iota)
00310             {
00311                charFont = (unsigned char)gIOTA_ETA;
00312             }
00313             else
00314                charFont = (unsigned char)gETA;
00315          }
00316 
00317       break;
00318 
00319       case IOTA:             // I
00320          if (breathing)
00321          {
00322             if (rough)
00323             {
00324                charFont = (unsigned char)gROUGH_IOTA;
00325             }
00326             else
00327                charFont = (unsigned char)gNON_ROUGH_IOTA;
00328          }
00329          else
00330          {
00331             charFont = (unsigned char)gIOTA;
00332          }
00333 
00334       break;
00335 
00336       case KAPPA:            // K
00337          charFont = (unsigned char)gKAPPA;
00338 
00339       break;
00340 
00341       case LAMBDA:           // L
00342          charFont = (unsigned char)gLAMBDA;
00343 
00344       break;
00345 
00346       case MU:               // M
00347          charFont = (unsigned char)gMU;
00348 
00349       break;
00350 
00351       case NU:               // N
00352          charFont = (unsigned char)gNU;
00353 
00354       break;
00355 
00356       case OMICRON:          // O
00357          if (breathing)
00358          {
00359             if (rough)
00360             {
00361                charFont = (unsigned char)gROUGH_OMICRON;
00362             }
00363             else
00364                charFont = (unsigned char)gNON_ROUGH_OMICRON;
00365          }
00366          else
00367          {
00368             charFont = (unsigned char)gOMICRON;
00369          }
00370 
00371       break;
00372 
00373       case PI:               // P
00374          charFont = (unsigned char)gPI;
00375 
00376       break;
00377 
00378       case THETA:            // Q
00379          charFont = (unsigned char)gTHETA;
00380 
00381       break;
00382 
00383       case RHO:              // R
00384          if (breathing)
00385          {
00386             if (rough)
00387             {
00388                charFont = (unsigned char)gROUGH_RHO;
00389             }
00390             else
00391                charFont = (unsigned char)gNON_ROUGH_RHO;
00392          }
00393          else
00394          {
00395             charFont = (unsigned char)gRHO;
00396          }
00397 
00398       break;
00399 
00400       case SIGMA:            // S
00401          if (finalSigma)
00402             charFont = (unsigned char)gSIGMA_END;
00403          else
00404             charFont = (unsigned char)gSIGMA;
00405 
00406       break;
00407 
00408       case TAU:              // T
00409          charFont = (unsigned char)gTAU;
00410 
00411       break;
00412 
00413       case UPSILON:          // U
00414          if (breathing)
00415          {
00416             if (rough)
00417             {
00418                charFont = (unsigned char)gROUGH_UPSILON;
00419             }
00420             else
00421                charFont = (unsigned char)gNON_ROUGH_UPSILON;
00422          }
00423          else
00424          {
00425             charFont = (unsigned char)gUPSILON;
00426          }
00427 
00428       break;
00429 
00430       case OMEGA:            // W
00431          if (breathing)
00432          {
00433             if (rough)
00434             {
00435                charFont = (unsigned char)gROUGH_OMEGA;
00436             }
00437             else
00438                charFont = (unsigned char)gNON_ROUGH_OMEGA;
00439          }
00440          else
00441          {
00442             if (iota)
00443             {
00444                charFont = (unsigned char)gIOTA_OMEGA;
00445             }
00446             else
00447                charFont = (unsigned char)gOMEGA;
00448          }
00449 
00450       break;
00451 
00452       case XI:               // X
00453          charFont = (unsigned char)gXI;
00454 
00455       break;
00456 
00457       case PSI:              // Y
00458          charFont = (unsigned char)gPSI;
00459 
00460       break;
00461 
00462       case ZETA:             // Z
00463          charFont = (unsigned char)gZETA;
00464 
00465       break;
00466 
00467       default:
00468          if (ispunct(letter) || isspace(letter))
00469          {
00470             charFont = getGreekPunct(letter);
00471          }
00472 
00473          if (isdigit(letter))
00474             charFont = letter;
00475 
00476       break;
00477    }
00478 
00479    return charFont;
00480 }
00481 
00482 
00483 //*****************************************************************************
00484 // Convert a GREEK font character to a character
00485 //*****************************************************************************
00486 unsigned char Font2char(
00487                unsigned char letter,       // bGreek letter to convert to Font letter
00488                bool &iota,        // true = IOTA subscript; false = No IOTA
00489                bool &breathing,   // true = add breathing; false = no breathing
00490                bool &rough)       // true = rough breathing; false = smooth
00491 {
00492    unsigned char character = 0;
00493 
00494    if (getSpecialChar(letter, letter))
00495    {
00496       switch (letter)
00497       {
00498          case gROUGH_ALPHA:         // hA
00499          case gIOTA_ALPHA:          // Ai
00500          case gNON_ROUGH_ALPHA:     // hA
00501             character = ALPHA;
00502 
00503             if (letter == gIOTA_ALPHA)
00504                iota = true;
00505             else
00506                iota = false;
00507 
00508             if (letter == gROUGH_ALPHA)
00509             {
00510                breathing = true;
00511                rough = true;
00512             }
00513             else
00514             {
00515                breathing = false;
00516                rough = false;
00517             }
00518 
00519             break;
00520 
00521          case gROUGH_EPSILON:       // hE
00522          case gNON_ROUGH_EPSILON:   // hE
00523             character = EPSILON;
00524             iota = false;
00525 
00526             if (letter == gROUGH_EPSILON)
00527             {
00528                breathing = true;
00529                rough = true;
00530             }
00531             else
00532             {
00533                breathing = false;
00534                rough = false;
00535             }
00536 
00537             break;
00538 
00539          case gROUGH_ETA:           // hH
00540          case gIOTA_ETA:            // Ei
00541          case gNON_ROUGH_ETA:       // hH
00542             character = ETA;
00543 
00544             if (letter == gIOTA_ETA)
00545                iota = true;
00546             else
00547                iota = false;
00548 
00549             if (letter == gROUGH_ETA)
00550             {
00551                breathing = true;
00552                rough = true;
00553             }
00554             else
00555             {
00556                breathing = false;
00557                rough = false;
00558             }
00559 
00560             break;
00561 
00562          case gROUGH_IOTA:          // hH
00563          case gNON_ROUGH_IOTA:      // hH
00564             character = IOTA;
00565             iota = false;
00566 
00567             if (letter == gROUGH_IOTA)
00568             {
00569                breathing = true;
00570                rough = true;
00571             }
00572             else
00573             {
00574                breathing = false;
00575                rough = false;
00576             }
00577 
00578             break;
00579 
00580          case gROUGH_OMICRON:       // hH
00581          case gNON_ROUGH_OMICRON:   // hH
00582             character = OMICRON;
00583             iota = false;
00584 
00585             if (letter == gROUGH_OMICRON)
00586             {
00587                breathing = true;
00588                rough = true;
00589             }
00590             else
00591             {
00592                breathing = false;
00593                rough = false;
00594             }
00595 
00596             break;
00597 
00598          case gROUGH_RHO:           // hR
00599          case gNON_ROUGH_RHO:       // hR
00600             character = RHO;
00601             iota = false;
00602 
00603             if (letter == gROUGH_RHO)
00604             {
00605                breathing = true;
00606                rough = true;
00607             }
00608             else
00609             {
00610                breathing = false;
00611                rough = false;
00612             }
00613 
00614             break;
00615 
00616          case gROUGH_UPSILON:       // hU
00617          case gNON_ROUGH_UPSILON:   // hU
00618             character = UPSILON;
00619             iota = false;
00620 
00621             if (letter == gROUGH_UPSILON)
00622             {
00623                breathing = true;
00624                rough = true;
00625             }
00626             else
00627             {
00628                breathing = false;
00629                rough = false;
00630             }
00631 
00632             break;
00633 
00634          case gROUGH_OMEGA:         // hW
00635          case gIOTA_OMEGA:          // Wi
00636          case gNON_ROUGH_OMEGA:     // hW
00637             character = OMEGA;
00638 
00639             if (letter == gIOTA_OMEGA)
00640                iota = true;
00641             else
00642                iota = false;
00643 
00644             if (letter == gROUGH_OMEGA)
00645             {
00646                breathing = true;
00647                rough = true;
00648             }
00649             else
00650             {
00651                breathing = false;
00652                rough = false;
00653             }
00654 
00655             break;
00656       }
00657    }  // if (letter > SPECIAL_GREEK)
00658    else
00659    {
00660       if (letter == gSIGMA_END)
00661       {
00662          character = SIGMA;
00663       }
00664       else if (ispunct(letter) || isspace(letter))
00665       {
00666          character = getbGreekPunct(letter);
00667       }
00668       else if (isdigit(letter))
00669       {
00670          character = letter;
00671       }
00672       else
00673       {
00674          character = letter - 32;
00675       }
00676    }
00677 
00678    return character;
00679 }
00680 
00681 //*****************************************************************************
00682 // Identify and return a bGreek letter from a special font char
00683 //*****************************************************************************
00684 bool getSpecialChar(unsigned char Font, unsigned char &letter)
00685 {
00686    bool Yes = false;
00687    letter = Font;
00688 
00689    if (Font >= 133 && Font <= 144)
00690    {
00691       letter = gIOTA;
00692       Font = gIOTA;
00693    }
00694 
00695    if (Font >= 154 && Font <= 159)
00696    {
00697       letter = gEPSILON;
00698       Font = gEPSILON;
00699    }
00700 
00701    if (Font >= 163 && Font <= 171)
00702    {
00703       letter = gALPHA;
00704       Font = gALPHA;
00705    }
00706 
00707    if (Font >= 172 && Font <= 182)
00708    {
00709       letter = gIOTA_ALPHA;
00710       Font = gIOTA_ALPHA;
00711       Yes = true;
00712    }
00713 
00714    if (Font >= 187 && Font <= 195)
00715    {
00716       letter = gETA;
00717       Font = gETA;
00718    }
00719  
00720    if (Font >= 197 && Font <= 207)
00721    {
00722       letter = gIOTA_ETA;
00723       Font = gIOTA_ETA;
00724       Yes = true;
00725    }
00726 
00727    if ((Font >= 210 && Font <= 215) || Font == 253)
00728    {
00729       letter = gOMICRON;
00730       Font = gOMICRON;
00731    }
00732 
00733    if (Font >= 218 && Font <= 229)
00734    {
00735       letter = gUPSILON;
00736       Font = gUPSILON;
00737    }
00738 
00739    if (Font >= 232 && Font <= 240)
00740    {
00741       letter = gOMEGA;
00742       Font = gOMEGA;
00743    }
00744 
00745    if (Font >= 241 && Font <= 251)
00746    {
00747       letter = gIOTA_OMEGA;
00748       Font = gIOTA_OMEGA;
00749       Yes = true;
00750    }
00751 
00752    Yes = SpecialGreek(Font);
00753 
00754    return Yes;
00755 }
00756 
00757 
00758 //*****************************************************************************
00759 // true if the font character is a special character; false it isn't
00760 //*****************************************************************************
00761 
00762 bool SpecialGreek(unsigned char Font)
00763 {
00764    bool res = false;
00765 
00766    switch (Font)
00767    {
00768       case gROUGH_ALPHA:
00769       case gROUGH_EPSILON:
00770       case gROUGH_ETA:
00771       case gROUGH_IOTA:
00772       case gROUGH_OMICRON:
00773       case gROUGH_RHO:
00774       case gROUGH_UPSILON:
00775       case gROUGH_OMEGA:
00776       case gIOTA_ALPHA:
00777       case gIOTA_ETA:
00778       case gIOTA_OMEGA:
00779       case gNON_ROUGH_ALPHA:
00780       case gNON_ROUGH_EPSILON:
00781       case gNON_ROUGH_ETA:
00782       case gNON_ROUGH_IOTA:
00783       case gNON_ROUGH_OMICRON:
00784       case gNON_ROUGH_RHO:
00785       case gNON_ROUGH_UPSILON:
00786       case gNON_ROUGH_OMEGA:
00787          res = true;
00788 
00789          break;
00790    }
00791 
00792    return res;
00793 }
00794 
00795 
00796 //*****************************************************************************
00797 // Return Greek font puntuation from bGreek punstuation
00798 //*****************************************************************************
00799 
00800 unsigned char getGreekPunct(unsigned char bGreek)
00801 {
00802    unsigned char Font;
00803 
00804    switch (bGreek)
00805    {
00806       case COMMA:
00807          Font = gCOMMA;
00808       break;
00809 
00810       case STOP:
00811          Font = gSTOP;
00812       break;
00813 
00814       case SEMI_COLON:
00815          Font = gSEMI_COLON;
00816       break;
00817 
00818       case QUESTION:
00819          Font = gQUESTION;
00820       break;
00821 
00822       default:
00823          Font = ' ';
00824       break;
00825    }
00826 
00827    return Font;
00828 }
00829 
00830 
00831 //*****************************************************************************
00832 // Return bGreek puntuation from Greek font punstuation
00833 //*****************************************************************************
00834 
00835 unsigned char getbGreekPunct(unsigned char Greek)
00836 {
00837    unsigned char character;
00838 
00839    switch (Greek)
00840    {
00841       case gCOMMA:
00842          character = COMMA;
00843       break;
00844 
00845       case gSTOP:
00846          character = STOP;
00847       break;
00848 
00849       case gSEMI_COLON:
00850          character = SEMI_COLON;
00851       break;
00852 
00853       case gQUESTION:
00854          character = QUESTION;
00855       break;
00856 
00857       default:
00858          character = ' ';
00859       break;   
00860    }
00861 
00862    return character;
00863 }
00864 
00865 
00866 //*****************************************************************************
00867 // Is the character punctuation or a space: true it is, false it isn't
00868 //*****************************************************************************
00869 
00870 bool isPunctSpace(unsigned char c)
00871 {
00872    return (ispunct(c) || isspace(c) || c == 0) ? true : false;
00873 }
00874 
00875 #ifdef __TEST
00876 
00877 int main()
00878 {
00879    unsigned char *sGreekText = (unsigned char *)
00880       "1„£kwboj qeoà kaˆ kur…ou „hsoà cristoà doàloj ta‹j dèdeka fula‹j ta‹j ™n tÍ diaspor´ ca…rein.";
00881    unsigned char *sResult = new unsigned char[100];
00882 
00883    char result = Greek2bGreek(
00884                   sResult, 
00885                   sGreekText, 
00886                   100);
00887 
00888    strset((char *)sResult, 0);
00889    strset((char *)sGreekText, 0);
00890 
00891    sGreekText = (unsigned char *)"18 EIS AFESIN TWN hAMARTWN hUMWN?";
00892    result = bGreek2Greek(
00893                   sResult, 
00894                   sGreekText, 
00895                   33);
00896 
00897    //delete[] sGreekText;
00898    delete[] sResult;
00899 }
00900 
00901 #endif // __TEST

Generated on Thu Jun 20 22:12:59 2002 for The Sword Project by doxygen1.2.15