00001
00002
00003
00004
00005
00006
00007
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <gbfosis.h>
00012 #include <swmodule.h>
00013 #include <versekey.h>
00014 #ifndef __GNUC__
00015 #else
00016 #include <unixstr.h>
00017 #endif
00018
00019
00020 GBFOSIS::GBFOSIS() {
00021 }
00022
00023
00024 GBFOSIS::~GBFOSIS() {
00025 }
00026
00027
00028 char GBFOSIS::ProcessText(char *text, int maxlen, const SWKey *key, const SWModule *module) {
00029
00030 char *to, *from, token[2048];
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;
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 == '>') {
00073 intoken = false;
00074 keepToken = false;
00075 suspendTextPassThru = false;
00076 newWord = true;
00077
00078
00079 while (wordStart < (text+maxlen)) {
00080
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
00092 if (!strncmp(token, "scripRef", 8)) {
00093
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
00105 if (!strcmp(token, "RF")) {
00106
00107 suspendTextPassThru = true;
00108 newText = true;
00109 }
00110 else if (!strcmp(token, "Rf")) {
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
00119 else if (!strncmp(token, "img ", 4)) {
00120 const char *src = strstr(token, "src");
00121 if (!src)
00122 return false;
00123
00124 pushString(&to, "<figure src=\"");
00125 const char *c;
00126 for (c = src;((*c) && (*c != '"')); c++);
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 for (c++;((*c) && (*c != '"')); c++)
00138 *to++ = *c;
00139
00140 pushString(&to, "\" />");
00141 return true;
00142 }
00143
00144
00145 else if (*token == 'W' && (token[1] == 'G' || token[1] == 'H')) {
00146 if (module->isProcessEntryAttributes()) {
00147 valto = val;
00148 for (unsigned int i = 1; ((token[i]) && (i < 150)); i++)
00149 *valto++ = token[i];
00150 *valto = 0;
00151
00152 strstrip(val);
00153 if (!strncmp(wordStart, "<w ", 3)) {
00154 sprintf(buf, "lemma=\"x-Strong:%s\" ", val);
00155 memmove(wordStart+3+strlen(buf), wordStart+3, (to-wordStart)+1);
00156 memcpy(wordStart+3, buf, strlen(buf));
00157 to+=strlen(buf);
00158 }
00159 else {
00160 sprintf(buf, "<w lemma=\"x-Strong:%s\">", val);
00161 memmove(wordStart+strlen(buf), wordStart, (to-wordStart)+1);
00162 memcpy(wordStart, buf, strlen(buf));
00163 to+=strlen(buf);
00164 pushString(&to, "</w>");
00165 module->getEntryAttributes()["Word"][wordstr]["Strongs"] = val;
00166 }
00167 }
00168 }
00169
00170
00171 else if (*token == 'W' && token[1] == 'T' && (token[2] == 'G' || token[2] == 'H')) {
00172 valto = val;
00173 for (unsigned int i = 1; ((token[i]) && (i < 150)); i++)
00174 *valto++ = token[i];
00175 *valto = 0;
00176 strstrip(val);
00177 if (!strncmp(wordStart, "<w ", 3)) {
00178 sprintf(buf, "morph=\"x-%s:%s\" ", "StrongsMorph", val);
00179 memmove(wordStart+3+strlen(buf), wordStart+3, (to-wordStart)+1);
00180 memcpy(wordStart+3, buf, strlen(buf));
00181 to+=strlen(buf);
00182 }
00183 else {
00184 sprintf(buf, "<w morph=\"x-%s:%s\">", "StrongsMorph", val);
00185 memmove(wordStart+strlen(buf), wordStart, (to-wordStart)+1);
00186 memcpy(wordStart, buf, strlen(buf));
00187 to+=strlen(buf);
00188 pushString(&to, "</w>");
00189 }
00190 }
00191
00192 if (!keepToken) {
00193 if (from[1] && strchr(" ,;.:?!()'\"", from[1])) {
00194 if (lastspace)
00195 to--;
00196 }
00197 if (newText) {textStart = from+1; newText = false; }
00198
00199 continue;
00200 }
00201
00202 *to++ = '<';
00203 for (char *tok = token; *tok; tok++)
00204 *to++ = *tok;
00205 *to++ = '>';
00206 if (newText) {textStart = to; newWord = false; }
00207
00208 continue;
00209 }
00210 if (intoken) {
00211 if ((tokpos < 2045) && ((*from != 10)&&(*from != 13))) {
00212 token[tokpos++] = *from;
00213 token[tokpos+2] = 0;
00214 }
00215 }
00216 else {
00217 if (newWord && (*from != ' ')) {wordStart = to; newWord = false; memset(to, 0, 10); }
00218 if (!suspendTextPassThru) {
00219 *to++ = *from;
00220 lastspace = (*from == ' ');
00221 }
00222 }
00223 }
00224
00225 VerseKey *vkey = SWDYNAMIC_CAST(VerseKey, key);
00226 if (vkey) {
00227 char ref[254];
00228 if (vkey->Verse())
00229 sprintf(ref, "<verseStart ref=\"%s\" />", vkey->getOSISRef());
00230 else if (vkey->Chapter())
00231 sprintf(ref, "<chapterStart ref=\"%s\" />", vkey->getOSISRef());
00232 else if (vkey->Book())
00233 sprintf(ref, "<bookStart ref=\"%s\" />", vkey->getOSISRef());
00234 else *ref = 0;
00235 if (*ref) {
00236 memmove(text+strlen(ref), text, maxlen-strlen(ref)-1);
00237 memcpy(text, ref, strlen(ref));
00238 to+=strlen(ref);
00239 if (vkey->Verse()) {
00240 VerseKey tmp;
00241 tmp = *vkey;
00242 tmp.AutoNormalize(0);
00243 tmp.Headings(1);
00244 sprintf(ref, "<verseEnd ref=\"%s\" />", vkey->getOSISRef());
00245 pushString(&to, ref);
00246 tmp = MAXVERSE;
00247 if (*vkey == tmp) {
00248 tmp.Verse(0);
00249 sprintf(ref, "<chapterEnd ref=\"%s\" />", tmp.getOSISRef());
00250 pushString(&to, ref);
00251 tmp = MAXCHAPTER;
00252 tmp = MAXVERSE;
00253 if (*vkey == tmp) {
00254 tmp.Chapter(0);
00255 tmp.Verse(0);
00256 sprintf(ref, "<bookEnd ref=\"%s\" />", tmp.getOSISRef());
00257 pushString(&to, ref);
00258 }
00259 }
00260 }
00261
00262 else if (vkey->Chapter())
00263 sprintf(ref, "<chapterStart ref=\"%s\" />", vkey->getOSISRef());
00264 else sprintf(ref, "<bookStart ref=\"%s\" />", vkey->getOSISRef());
00265 }
00266 }
00267 *to++ = 0;
00268 *to = 0;
00269 return 0;
00270 }
00271
00272
00273 void GBFOSIS::pushString(char **buf, const char *format, ...) {
00274 va_list argptr;
00275
00276 va_start(argptr, format);
00277 *buf += vsprintf(*buf, format, argptr);
00278 va_end(argptr);
00279
00280
00281 }
00282
00283
00284 const char *GBFOSIS::convertToOSIS(const char *inRef, const SWKey *key) {
00285 static string outRef;
00286
00287 outRef = "";
00288
00289 VerseKey defLanguage;
00290 ListKey verses = defLanguage.ParseVerseList(inRef, (*key), true);
00291 const char *startFrag = inRef;
00292 for (int i = 0; i < verses.Count(); i++) {
00293 VerseKey *element = SWDYNAMIC_CAST(VerseKey, verses.GetElement(i));
00294 char buf[5120];
00295 char frag[800];
00296 if (element) {
00297 memmove(frag, startFrag, ((const char *)element->userData - startFrag) + 1);
00298 frag[((const char *)element->userData - startFrag) + 1] = 0;
00299 startFrag = (const char *)element->userData + 1;
00300 sprintf(buf, "<reference refStart=\"KJV:%s\" refEnd=\"%s\">%s</reference>", element->LowerBound().getOSISRef(), element->UpperBound().getOSISRef(), frag);
00301 }
00302 else {
00303 memmove(frag, startFrag, ((const char *)verses.GetElement(i)->userData - startFrag) + 1);
00304 frag[((const char *)verses.GetElement(i)->userData - startFrag) + 1] = 0;
00305 startFrag = (const char *)verses.GetElement(i)->userData + 1;
00306 sprintf(buf, "<reference refStart=\"KJV:%s\">%s</reference>", VerseKey(*verses.GetElement(i)).getOSISRef(), frag);
00307 }
00308 outRef+=buf;
00309 }
00310 return outRef.c_str();
00311 }