The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
thmlstrongs.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * thmlstrongs.cpp - SWFilter descendant to hide or show Strong's number
4  * in a ThML module
5  *
6  * $Id: thmlstrongs.cpp 3790 2020-09-11 15:26:02Z scribe $
7  *
8  * Copyright 2001-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 <thmlstrongs.h>
27 #include <swmodule.h>
28 #include <utilstr.h>
29 #include <ctype.h>
30 
31 
33 
34 namespace {
35 
36  static const char oName[] = "Strong's Numbers";
37  static const char oTip[] = "Toggles Strong's Numbers On and Off if they exist";
38 
39  static const StringList *oValues() {
40  static const SWBuf choices[3] = {"Off", "On", ""};
41  static const StringList oVals(&choices[0], &choices[2]);
42  return &oVals;
43  }
44 }
45 
46 
48 }
49 
50 
52 }
53 
54 
55 char ThMLStrongs::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
56  char token[2048]; // cheese. Fix.
57  const char *from;
58  int tokpos = 0;
59  bool intoken = false;
60  bool lastspace = false;
61  int word = 1;
62  char val[128];
63  char wordstr[11];
64  char *valto;
65  char *ch;
66  unsigned int textStart = 0, textEnd = 0;
67  SWBuf tmp;
68  bool newText = false;
69 
70  SWBuf orig = text;
71  from = orig.c_str();
72 
73  for (text = ""; *from; from++) {
74  if (*from == '<') {
75  intoken = true;
76  tokpos = 0;
77  token[0] = 0;
78  token[1] = 0;
79  token[2] = 0;
80  textEnd = (unsigned int)text.length();
81  continue;
82  }
83  if (*from == '>') { // process tokens
84  intoken = false;
85  if (!strnicmp(token, "sync type=\"Strongs\" ", 20)) { // Strongs
86  if (module->isProcessEntryAttributes()) {
87  valto = val;
88  for (unsigned int i = 27; token[i] != '\"' && i < 150; i++)
89  *valto++ = token[i];
90  *valto = 0;
91  if (atoi((!isdigit(*val))?val+1:val) < 5627) {
92  // normal strongs number
93  sprintf(wordstr, "%03d", word);
94  module->getEntryAttributes()["Word"][wordstr]["PartCount"] = "1";
95  module->getEntryAttributes()["Word"][wordstr]["Lemma"] = val;
96  module->getEntryAttributes()["Word"][wordstr]["LemmaClass"] = "strong";
97  module->getEntryAttributes()["Word"][wordstr]["Lemma.1"] = val;
98  module->getEntryAttributes()["Word"][wordstr]["LemmaClass.1"] = "strong";
99  tmp = "";
100  tmp.append(text.c_str()+textStart, (int)(textEnd - textStart));
101  module->getEntryAttributes()["Word"][wordstr]["Text"] = tmp;
102  newText = true;
103  }
104  else {
105 /*
106  // verb morph
107  sprintf(wordstr, "%03d", word);
108  module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
109  module->getEntryAttributes()["Word"][wordstr]["MorphClass"] = "OLBMorph";
110  module->getEntryAttributes()["Word"][wordstr]["Morph.1"] = val;
111  module->getEntryAttributes()["Word"][wordstr]["MorphClass.1"] = "OLBMorph";
112 */
113  word--; // for now, completely ignore this word attribute.
114  }
115  word++;
116  }
117 
118  if (!option) { // if we don't want strongs
119  if ((from[1] == ' ') || (from[1] == ',') || (from[1] == ';') || (from[1] == '.') || (from[1] == '?') || (from[1] == '!') || (from[1] == ')') || (from[1] == '\'') || (from[1] == '\"')) {
120  if (lastspace)
121  text--;
122  }
123  if (newText) {textStart = (unsigned int)text.length(); newText = false; }
124  continue;
125  }
126  }
127  if (module->isProcessEntryAttributes()) {
128  if (!strncmp(token, "sync type=\"morph\"", 17)) {
129  for (ch = token+17; *ch; ch++) {
130  if (!strncmp(ch, "class=\"", 7)) {
131  valto = val;
132  for (unsigned int i = 7; ch[i] != '\"' && i < 127; i++)
133  *valto++ = ch[i];
134  *valto = 0;
135  sprintf(wordstr, "%03d", word-1);
136  if ((!stricmp(val, "Robinsons")) || (!stricmp(val, "Robinson"))) {
137  strcpy(val, "robinson");
138  }
139  module->getEntryAttributes()["Word"][wordstr]["MorphClass"] = val;
140  module->getEntryAttributes()["Word"][wordstr]["MorphClass.1"] = val;
141  }
142  if (!strncmp(ch, "value=\"", 7)) {
143  valto = val;
144  for (unsigned int i = 7; ch[i] != '\"' && i < 127; i++)
145  *valto++ = ch[i];
146  *valto = 0;
147  sprintf(wordstr, "%03d", word-1);
148  module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
149  module->getEntryAttributes()["Word"][wordstr]["Morph.1"] = val;
150  }
151  }
152  newText = true;
153  }
154  }
155  // if not a strongs token, keep token in text
156  text += '<';
157  text += token;
158  text += '>';
159  if (newText) {textStart = (unsigned int)text.length(); newText = false; }
160  continue;
161  }
162  if (intoken) {
163  if (tokpos < 2045) {
164  token[tokpos++] = *from;
165  // TODO: why is this + 2 ?
166  token[tokpos+2] = 0;
167  }
168  }
169  else {
170  text += *from;
171  lastspace = (*from == ' ');
172  }
173  }
174  return 0;
175 }
176 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
SWText * module
Definition: osis2mod.cpp:105
static const StringList * oValues()
int stricmp(const char *s1, const char *s2)
Definition: utilstr.cpp:194
const char * c_str() const
Definition: swbuf.h:158
std::list< SWBuf > StringList
Definition: swmodule.cpp:91
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
static const char oName[]
virtual bool isProcessEntryAttributes() const
Definition: swmodule.h:832
virtual AttributeTypeList & getEntryAttributes() const
Definition: swmodule.h:817
static const char * choices[4]
static const char oTip[]
virtual ~ThMLStrongs()
Definition: thmlstrongs.cpp:51
int strnicmp(const char *s1, const char *s2, int len)
Definition: utilstr.cpp:180
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
Definition: thmlstrongs.cpp:55