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

thmlosis.cpp

00001 /******************************************************************************
00002  *
00003  * thmlstrongs -        SWFilter decendant to hide or show strongs number
00004  *                      in a ThML module.
00005  */
00006 
00007 
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <thmlosis.h>
00012 #include <swmodule.h>
00013 #include <versekey.h>
00014 #ifndef __GNUC__
00015 #else
00016 #include <unixstr.h>
00017 #endif
00018 
00019 
00020 ThMLOSIS::ThMLOSIS() {
00021 }
00022 
00023 
00024 ThMLOSIS::~ThMLOSIS() {
00025 }
00026 
00027 
00028 char ThMLOSIS::ProcessText(char *text, int maxlen, const SWKey *key, const SWModule *module) {
00029 
00030         char *to, *from, token[2048]; // cheese.  Fix.
00031         int tokpos = 0;
00032         bool intoken = false;
00033         int len;
00034         bool lastspace = false;
00035         int word = 1;
00036         char val[128];
00037         char buf[128];
00038         char wordstr[5];
00039         char *valto;
00040         char *ch;
00041         char *textStart, *textEnd;
00042         char *wordStart, *wordEnd;
00043         bool newText = false;
00044         bool newWord = false;
00045         string tmp;
00046         bool suspendTextPassThru = false;
00047         bool keepToken = false;
00048 
00049         len = strlen(text) + 1; // shift string to right of buffer
00050         if (len < maxlen) {
00051                 memmove(&text[maxlen - len], text, len);
00052                 from = &text[maxlen - len];
00053         }
00054         else    from = text;
00055         
00056         textStart = from;
00057         wordStart = text;
00058 
00059         // -------------------------------
00060 
00061         for (to = text; *from; from++) {
00062                 if (*from == '<') {
00063                         intoken = true;
00064                         tokpos = 0;
00065                         token[0] = 0;
00066                         token[1] = 0;
00067                         token[2] = 0;
00068                         textEnd = from-1;
00069                         wordEnd = to;
00070                         continue;
00071                 }
00072                 if (*from == '>') {     // process tokens
00073                         intoken = false;
00074                         keepToken = false;
00075                         suspendTextPassThru = false;
00076                         newWord = true;
00077 
00078 
00079                         while (wordStart < (text+maxlen)) {
00080 //                              if (strchr(" ,;.?!()'\"", *wordStart))
00081                                 if (strchr(";,: .?!()'\"", *wordStart))
00082                                         wordStart++;
00083                                 else break;
00084                         }
00085                         while (wordEnd > wordStart) {
00086                                 if (strchr(" ,;:.?!()'\"", *wordEnd))
00087                                         wordEnd--;
00088                                 else break;
00089                         }
00090 
00091                         // Scripture Reference
00092                         if (!strncmp(token, "scripRef", 8)) {
00093         //                      pushString(buf, "<reference work=\"Bible.KJV\" reference=\"");
00094                                 suspendTextPassThru = true;
00095                                 newText = true;
00096                         }
00097                         else    if (!strncmp(token, "/scripRef", 9)) {
00098                                 tmp = "";
00099                                 tmp.append(textStart, (int)(textEnd - textStart)+1);
00100                                 pushString(&to, convertToOSIS(tmp.c_str(), key));
00101                                 suspendTextPassThru = false;
00102                         }
00103 
00104                         // Footnote
00105                         if (!strcmp(token, "note")) {
00106         //                      pushString(buf, "<reference work=\"Bible.KJV\" reference=\"");
00107                                 suspendTextPassThru = true;
00108                                 newText = true;
00109                         }
00110                         else    if (!strcmp(token, "/note")) {
00111                                 tmp = "<note type=\"x-StudyNote\"><notePart type=\"x-MainText\">";
00112                                 tmp.append(textStart, (int)(textEnd - textStart)+1);
00113                                 tmp += "</notePart></note>";
00114                                 pushString(&to, tmp.c_str());
00115                                 suspendTextPassThru = false;
00116                         }
00117 
00118                         // Figure
00119                         else    if (!strncmp(token, "img ", 4)) {
00120                                 const char *src = strstr(token, "src");
00121                                 if (!src)               // assert we have a src attribute
00122                                         return false;
00123 
00124                                 pushString(&to, "<figure src=\"");
00125                                 const char *c;
00126                                 for (c = src;((*c) && (*c != '"')); c++);
00127 
00128                                 /* uncomment for SWORD absolute path logic
00129                                 if (*(c+1) == '/') {
00130                                         pushString(buf, "file:");
00131                                         pushString(buf, module->getConfigEntry("AbsoluteDataPath"));
00132                                         if (*((*buf)-1) == '/')
00133                                                 c++;            // skip '/'
00134                                 }
00135                                 end of uncomment for asolute path logic */
00136 
00137                                 for (c++;((*c) && (*c != '"')); c++)
00138                                         *to++ = *c;
00139 
00140                                 pushString(&to, "\" />");
00141                                 return true;
00142                         }
00143 
00144                         // Strongs numbers
00145                         else    if (!strnicmp(token, "sync type=\"Strongs\" ", 20)) {   // Strongs
00146                                 if (module->isProcessEntryAttributes()) {
00147                                         valto = val;
00148                                         for (unsigned int i = 27; token[i] != '\"' && i < 150; i++)
00149                                                 *valto++ = token[i];
00150                                         *valto = 0;
00151                                         if (atoi((!isdigit(*val))?val+1:val) < 5627) {
00152                                                 // normal strongs number
00153                                                 strstrip(val);
00154                                                 sprintf(buf, "<w lemma=\"x-Strong:%s\">", val);
00155                                                 memmove(wordStart+strlen(buf), wordStart, (to-wordStart)+1);
00156                                                 memcpy(wordStart, buf, strlen(buf));
00157                                                 to+=strlen(buf);
00158                                                 pushString(&to, "</w>");
00159                                                 module->getEntryAttributes()["Word"][wordstr]["Strongs"] = val;
00160 //                                              tmp = "";
00161 //                                              tmp.append(textStart, (int)(wordEnd - wordStart));
00162 //                                              module->getEntryAttributes()["Word"][wordstr]["Text"] = tmp;
00163                                         }
00164                                         else {
00165                                                 // verb morph
00166                                                 sprintf(wordstr, "%03d", word-1);
00167                                                 module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
00168                                         }
00169                                 }
00170                         }
00171 
00172                         // Morphology
00173                         else    if (!strncmp(token, "sync type=\"morph\"", 17)) {
00174                                 for (ch = token+17; *ch; ch++) {
00175                                         if (!strncmp(ch, "class=\"", 7)) {
00176                                                 valto = val;
00177                                                 for (unsigned int i = 7; ch[i] != '\"' && i < 127; i++)
00178                                                         *valto++ = ch[i];
00179                                                 *valto = 0;
00180                                                 sprintf(wordstr, "%03d", word-1);
00181                                                 strstrip(val);
00182                                                 module->getEntryAttributes()["Word"][wordstr]["MorphClass"] = val;
00183                                         }
00184                                         if (!strncmp(ch, "value=\"", 7)) {
00185                                                 valto = val;
00186                                                 for (unsigned int i = 7; ch[i] != '\"' && i < 127; i++)
00187                                                         *valto++ = ch[i];
00188                                                 *valto = 0;
00189                                                 sprintf(wordstr, "%03d", word-1);
00190                                                 strstrip(val);
00191                                                 module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
00192                                         }
00193                                 }
00194                                 if (!strncmp(wordStart, "<w ", 3)) {
00195 
00196                                         const char *cls = "Unknown", *morph;
00197 
00198                                         if (module->getEntryAttributes()["Word"][wordstr]["Morph"].size() > 0) {
00199                                                 if (module->getEntryAttributes()["Word"][wordstr]["MorphClass"].size() > 0)
00200                                                         cls = module->getEntryAttributes()["Word"][wordstr]["MorphClass"].c_str();
00201                                                 morph = module->getEntryAttributes()["Word"][wordstr]["Morph"].c_str();
00202                                         
00203                                                 sprintf(buf, "morph=\"x-%s:%s\" ", cls, morph);
00204                                                 memmove(wordStart+3+strlen(buf), wordStart+3, (to-wordStart)+1);
00205                                                 memcpy(wordStart+3, buf, strlen(buf));
00206                                                 to+=strlen(buf);
00207                                         }
00208                                 }
00209                         }
00210 
00211                         if (!keepToken) {       // if we don't want strongs
00212                                 if (strchr(" ,:;.?!()'\"", from[1])) {
00213                                         if (lastspace)
00214                                                 to--;
00215                                 }
00216                                 if (newText) {textStart = from+1; newText = false; }
00217 //                              if (newWord) {wordStart = to; newWord = false; }
00218                                 continue;
00219                         }
00220                         // if not a strongs token, keep token in text
00221                         *to++ = '<';
00222                         for (char *tok = token; *tok; tok++)
00223                                 *to++ = *tok;
00224                         *to++ = '>';
00225                         if (newText) {textStart = to; newWord = false; }
00226 //                      if (newWord) {wordStart = to; newWord = false; }
00227                         continue;
00228                 }
00229                 if (intoken) {
00230                         if ((tokpos < 2045) && ((*from != 10)&&(*from != 13))) {
00231                                 token[tokpos++] = *from;
00232                                 token[tokpos+2] = 0;
00233                         }
00234                 }
00235                 else    {
00236                         if (newWord && (*from != ' ')) {wordStart = to; newWord = false; memset(to, 0, 10); }
00237                         if (!suspendTextPassThru) {
00238                                 *to++ = *from;
00239                                 lastspace = (*from == ' ');
00240                         }
00241                 }
00242         }
00243 
00244         VerseKey *vkey = SWDYNAMIC_CAST(VerseKey, key);
00245         if (vkey) {
00246                 char ref[254];
00247                 if (vkey->Verse())
00248                         sprintf(ref, "<verseStart ref=\"%s\" />", vkey->getOSISRef());
00249                 else if (vkey->Chapter())
00250                         sprintf(ref, "<chapterStart ref=\"%s\" />", vkey->getOSISRef());
00251                 else if (vkey->Book())
00252                         sprintf(ref, "<bookStart ref=\"%s\" />", vkey->getOSISRef());
00253                 else *ref = 0;
00254                 if (*ref) {
00255                         memmove(text+strlen(ref), text, maxlen-strlen(ref)-1);
00256                         memcpy(text, ref, strlen(ref));
00257                         to+=strlen(ref);
00258                         if (vkey->Verse()) {
00259                                 VerseKey tmp;
00260                                 tmp = *vkey;
00261                                 tmp.AutoNormalize(0);
00262                                 tmp.Headings(1);
00263                                 sprintf(ref, "<verseEnd ref=\"%s\" />", vkey->getOSISRef());
00264                                 pushString(&to, ref);
00265                                 tmp = MAXVERSE;
00266                                 if (*vkey == tmp) {
00267                                         tmp.Verse(0);
00268                                         sprintf(ref, "<chapterEnd ref=\"%s\" />", tmp.getOSISRef());
00269                                         pushString(&to, ref);
00270                                         tmp = MAXCHAPTER;
00271                                         tmp = MAXVERSE;
00272                                         if (*vkey == tmp) {
00273                                                 tmp.Chapter(0);
00274                                                 tmp.Verse(0);
00275                                                 sprintf(ref, "<bookEnd ref=\"%s\" />", tmp.getOSISRef());
00276                                                 pushString(&to, ref);
00277                                         }
00278                                 }
00279                         }
00280 
00281                         else if (vkey->Chapter())
00282                                 sprintf(ref, "<chapterStart ref=\"%s\" />", vkey->getOSISRef());
00283                         else sprintf(ref, "<bookStart ref=\"%s\" />", vkey->getOSISRef());
00284                 }
00285         }
00286         *to++ = 0;
00287         *to = 0;
00288         return 0;
00289 }
00290 
00291 
00292 void ThMLOSIS::pushString(char **buf, const char *format, ...) {
00293   va_list argptr;
00294 
00295   va_start(argptr, format);
00296   *buf += vsprintf(*buf, format, argptr);
00297   va_end(argptr);
00298 
00299 //  *buf += strlen(*buf);
00300 }
00301 
00302 
00303 const char *ThMLOSIS::convertToOSIS(const char *inRef, const SWKey *key) {
00304         static string outRef;
00305 
00306         outRef = "";
00307 
00308         VerseKey defLanguage;
00309         ListKey verses = defLanguage.ParseVerseList(inRef, (*key), true);
00310         const char *startFrag = inRef;
00311         for (int i = 0; i < verses.Count(); i++) {
00312                 VerseKey *element = SWDYNAMIC_CAST(VerseKey, verses.GetElement(i));
00313                 char buf[5120];
00314                 char frag[800];
00315                 if (element) {
00316                         memmove(frag, startFrag, ((const char *)element->userData - startFrag) + 1);
00317                         frag[((const char *)element->userData - startFrag) + 1] = 0;
00318                         startFrag = (const char *)element->userData + 1;
00319                         sprintf(buf, "<reference refStart=\"KJV:%s\" refEnd=\"%s\">%s</reference>", element->LowerBound().getOSISRef(), element->UpperBound().getOSISRef(), frag);
00320                 }
00321                 else {
00322                         memmove(frag, startFrag, ((const char *)verses.GetElement(i)->userData - startFrag) + 1);
00323                         frag[((const char *)verses.GetElement(i)->userData - startFrag) + 1] = 0;
00324                         startFrag = (const char *)verses.GetElement(i)->userData + 1;
00325                         sprintf(buf, "<reference refStart=\"KJV:%s\">%s</reference>", VerseKey(*verses.GetElement(i)).getOSISRef(), frag);
00326                 }
00327                 outRef+=buf;
00328         }
00329         return outRef.c_str();
00330 }

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