1 /******************************************************************************
2  *
3  *  gbfstrongs.cpp -	SWFilter descendant to hide or show Strong's number
4  *			in a GBF module
5  *
6  * $Id: gbfstrongs.cpp 3515 2017-11-01 11:38:09Z scribe $
7  *
8  * Copyright 1999-2013 CrossWire Bible Society (http://www.crosswire.org)
9  *	CrossWire Bible Society
10  *	P. O. Box 2528
11  *	Tempe, AZ  85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <gbfstrongs.h>
27 #include <swmodule.h>
28 #include <ctype.h>
29 
30 
31 SWORD_NAMESPACE_START
32 
33 namespace {
34 
35 	static const char oName[] = "Strong's Numbers";
36 	static const char oTip[]  = "Toggles Strong's Numbers On and Off if they exist";
37 
oValues()38 	static const StringList *oValues() {
39 		static const SWBuf choices[3] = {"Off", "On", ""};
40 		static const StringList oVals(&choices[0], &choices[2]);
41 		return &oVals;
42 	}
43 }
44 
45 
GBFStrongs()46 GBFStrongs::GBFStrongs() : SWOptionFilter(oName, oTip, oValues()) {
47 }
48 
49 
~GBFStrongs()50 GBFStrongs::~GBFStrongs() {
51 }
52 
53 
processText(SWBuf & text,const SWKey * key,const SWModule * module)54 char GBFStrongs::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
55 	char token[2048]; // cheese.  Fix.
56 	int tokpos = 0;
57 	bool intoken = false;
58 	bool lastspace = false;
59 	int word = 1;
60 	char val[128];
61 	char wordstr[11];
62 	char *valto;
63 	unsigned int textStart = 0, textEnd = 0;
64 	bool newText = false;
65 	SWBuf tmp;
66 	const char *from;
67 
68 	SWBuf orig = text;
69 	from = orig.c_str();
70 
71 	for (text = ""; *from; from++) {
72 		if (*from == '<') {
73 			intoken = true;
74 			tokpos = 0;
75 			token[0] = 0;
76 			token[1] = 0;
77 			token[2] = 0;
78 			textEnd = (unsigned int)text.size();
79 			continue;
80 		}
81 		if (*from == '>') {	// process tokens
82 			intoken = false;
83 			if (*token == 'W' && (token[1] == 'G' || token[1] == 'H')) {	// Strongs
84 				if (module->isProcessEntryAttributes()) {
85 					valto = val;
86 					for (unsigned int i = 1; ((token[i]) && (i < 150)); i++)
87 						*valto++ = token[i];
88 					*valto = 0;
89 					if (atoi((!isdigit(*val))?val+1:val) < 5627) {
90 						// normal strongs number
91 						sprintf(wordstr, "%03d", word++);
92 						module->getEntryAttributes()["Word"][wordstr]["PartsCount"] = "1";
93 						module->getEntryAttributes()["Word"][wordstr]["Lemma"] = val;
94 						module->getEntryAttributes()["Word"][wordstr]["LemmaClass"] = "strong";
95 						tmp = "";
96 						tmp.append(text.c_str()+textStart, (int)(textEnd - textStart));
97 						module->getEntryAttributes()["Word"][wordstr]["Text"] = tmp;
98 						newText = true;
99 					}
100 					else {
101 						// verb morph
102 						sprintf(wordstr, "%03d", word-1);
103 						module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
104 						module->getEntryAttributes()["Word"][wordstr]["MorphClass"] = "OLBMorph";
105 					}
106 				}
107 
108 				if (!option) {
109 					if ((from[1] == ' ') || (from[1] == ',') || (from[1] == ';') || (from[1] == '.') || (from[1] == '?') || (from[1] == '!') || (from[1] == ')') || (from[1] == '\'') || (from[1] == '\"')) {
110 						if (lastspace)
111 							text--;
112 					}
113 					if (newText) {textStart = (unsigned int)text.size(); newText = false; }
114 					continue;
115 				}
116 			}
117 			if (module->isProcessEntryAttributes()) {
118 				if ((*token == 'W') && (token[1] == 'T')) {	// Morph
119 					valto = val;
120 					for (unsigned int i = 2; ((token[i]) && (i < 150)); i++)
121 						*valto++ = token[i];
122 					*valto = 0;
123 					sprintf(wordstr, "%03d", word-1);
124 					module->getEntryAttributes()["Word"][wordstr]["MorphClass"] = "GBFMorph";
125 					module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
126 					newText = true;
127 				}
128 			}
129 			// if not a strongs token, keep token in text
130 			text += '<';
131 			text += token;
132 			text += '>';
133 			if (newText) {textStart = (unsigned int)text.size(); newText = false; }
134 			continue;
135 		}
136 		if (intoken) {
137 			if (tokpos < 2045) {
138 				token[tokpos++] = *from;
139 				// TODO: why is this + 2 ?
140 				token[tokpos+2] = 0;
141 			}
142 		}
143 		else {
144 			text += *from;
145 			lastspace = (*from == ' ');
146 		}
147 	}
148 	return 0;
149 }
150 
151 SWORD_NAMESPACE_END
152