The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
versetreekey.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * versetreekey.cpp - code for class 'VerseTreeKey'- versekey using treekey
4  * for data retrieval
5  *
6  * $Id: versetreekey.cpp 3808 2020-10-02 13:23:34Z scribe $
7  *
8  * Copyright 2006-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 
25 #include <versetreekey.h>
26 #include <ctype.h>
27 #include <swlog.h>
28 
30 
31 static const char *classes[] = {"VerseTreeKey", "VerseKey", "SWKey", "SWObject", 0};
32 static const SWClass classdef(classes);
33 
34 
35 /******************************************************************************
36  * VerseTreeKey Constructor - initializes instance of VerseTreeKey
37  *
38  * ENT: ikey - base key (will take various forms of 'BOOK CH:VS'. See
39  * VerseTreeKey::parse for more detailed information)
40  */
41 
42 VerseTreeKey::VerseTreeKey(TreeKey *treeKey, const SWKey *ikey) : VerseKey(ikey)
43 {
44  init(treeKey);
45  if (ikey)
46  parse();
47 }
48 
49 
50 /******************************************************************************
51  * VerseTreeKey Constructor - initializes instance of VerseTreeKey
52  *
53  * ENT: ikey - text key (will take various forms of 'BOOK CH:VS'. See
54  * VerseTreeKey::parse for more detailed information)
55  */
56 
57 VerseTreeKey::VerseTreeKey(TreeKey *treeKey, const char *ikey) : VerseKey(ikey)
58 {
59  init(treeKey);
60  if (ikey)
61  parse();
62 }
63 
64 
66 {
67  init(k.treeKey);
68 }
69 
70 
71 VerseTreeKey::VerseTreeKey(TreeKey *treeKey, const char *min, const char *max) : VerseKey(min, max)
72 {
73  init(treeKey);
74 }
75 
76 
78 {
79  myClass = &classdef;
80  this->treeKey = (TreeKey *)treeKey->clone();
81  this->treeKey->setPositionChangeListener(this);
82  internalPosChange = false;
83 }
84 
85 
87 {
88  return new VerseTreeKey(*this);
89 }
90 
91 
92 int VerseTreeKey::getBookFromAbbrev(const char *iabbr) const
93 {
94  int bookno = VerseKey::getBookFromAbbrev(iabbr);
95  if (bookno < 0) {
96  SWLog::getSystemLog(); // TODO fix for odd android bug
97 /*
98  vector<struct sbook>::iterator it = find(books, iabbr);
99  if (it == books.end()) {
100  TreeKey *tkey = this->treeKey;
101  int saveError = tkey->popError();
102  long bookmark = tkey->getOffset();
103  SWBuf segment;
104  internalPosChange = true;
105  do {
106  segment = (SWBuf)tkey->getLocalName();
107  } while (tkey->parent());
108  segment << 1;
109  if (saveError) {
110  error = saveError;
111  }
112  tkey->setOffset(bookmark);
113  }
114  books.push_back(sbook(iabbr));
115 */
116  }
117  return bookno;
118 }
119 
120 /******************************************************************************
121  * VerseTreeKey Destructor - cleans up instance of VerseTreeKey
122  *
123  * ENT: ikey - text key
124  */
125 
127  delete treeKey;
128 }
129 
130 
131 void VerseTreeKey::decrement(int steps) {
132  int treeError = 0;
134  do {
135  treeKey->decrement();
136  treeError = treeKey->popError();
137  // iterate until 3 levels and no versekey parse errors
138  } while (!treeError && ((treeKey->getLevel() < 3) || error));
139  if (error && !treeError) {
140  int saveError = error;
141  increment();
142  error = saveError;
143  }
144  if (treeError) {
146  error = treeError;
147  }
148  if (_compare(getUpperBound()) > 0) {
151  }
152  if (_compare(getLowerBound()) < 0) {
155  }
156 }
157 
158 
159 void VerseTreeKey::increment(int steps) {
160  int treeError = 0;
162  do {
163  treeKey->increment();
164  treeError = treeKey->popError();
165  // iterate until 3 levels and no versekey parse errors
166  } while (!treeError && ((treeKey->getLevel() < 3) || error));
167  if (error && !treeError) {
168  int saveError = error;
169  decrement();
170  error = saveError;
171  }
172  if (treeError) {
174  error = treeError;
175  }
176  // bounds
177  if (_compare(getUpperBound()) > 0) {
180  }
181  if (_compare(getLowerBound()) < 0) {
184  }
185 }
186 
187 
189  if (!internalPosChange) {
191  int saveError = tkey->popError();
192  long bookmark = tkey->getOffset();
193  SWBuf seg[4];
194  internalPosChange = true;
195  int legs = 0;
196  do {
197  seg[legs] = tkey->getLocalName();
198  legs++;
199  } while (tkey->parent() && (legs < 4));
200 
201  legs--;
202 
203  if ((legs < 2) && (!seg[0].length() || seg[0] == "/")) { //"[ Module Heading ]";
204  testament = 0;
205  book = 0;
206  chapter = 0;
207  setVerse(0);
208  }
209  else if ((legs < 2)
210  && ((!strncmp(seg[0].c_str(), "[ Testament ", 12)) && //"[ Testament n Heading ]";
211  (isdigit(seg[0][12])) &&
212  (!strcmp(seg[0].c_str()+13, " Heading ]")))) {
213  testament = (seg[0][12]-48);
214  book = 0;
215  chapter = 0;
216  setVerse(0);
217  } //path = "[ Module Heading ]";
218  else {
219  setBookName(seg[--legs]);
220  chapter = (legs > 0) ? atoi(seg[--legs]) : 0;
221  setVerse((legs > 0) ? atoi(seg[--legs]) : 0);
222  }
223 
224 // setText(path);
225  if (saveError) {
226  error = saveError;
227  }
228  tkey->setOffset(bookmark);
229  tkey->setError(saveError);
230  internalPosChange = false;
231  }
232 }
233 
234 
236  internalPosChange = true;
237  SWBuf path;
238  if (!getTestament()) path = "/"; // "[ Module Heading ]";
239  else if (!getBook()) path.setFormatted("/[ Testament %d Heading ]", getTestament());
240  else path.setFormatted("/%s/%d/%d", getOSISBookName(), getChapter(), getVerse());
241  if (getSuffix()) path += getSuffix();
242  long bookmark = treeKey->getOffset();
243  treeKey->setText(path);
244 
245  // if our module has jacked inconsistencies, then let's put our tree back to where it was
246  if (treeKey->popError()) {
247  treeKey->setOffset(bookmark);
248  }
249 
250  internalPosChange = false;
251 }
252 
253 
255  syncVerseToTree();
256  return treeKey;
257 }
258 
260  syncVerseToTree();
261  return treeKey;
262 }
263 
264 // can autonormalize yet (ever?)
265 void VerseTreeKey::Normalize(char autocheck) {
266  error = 0;
267 }
268 
270  return treeKey->getOffset();
271 }
272 
273 
275 
276  if (isBoundSet()) {
277  return VerseKey::setPosition(p);
278  }
279 
280  switch (p) {
281  case POS_TOP:
282  popError();
283  treeKey->setPosition(p);
284  increment();
285  decrement();
286  popError();
287  break;
288  case POS_BOTTOM:
289  popError();
290  treeKey->setPosition(p);
291  decrement();
292  increment();
293  popError();
294  break;
295  case POS_MAXVERSE:
296  case POS_MAXCHAPTER:
298  break;
299  }
300 }
301 
302 
303 /******************************************************************************
304  * VerseTreeKey::copyFrom - Equates this VerseTreeKey to another VerseTreeKey
305 
306 void VerseTreeKey::copyFrom(const VerseTreeKey &ikey) {
307  VerseKey::copyFrom(ikey);
308 }
309  */
310 
virtual bool parent()=0
virtual bool isBoundSet() const
Definition: swkey.h:189
virtual void setPosition(SW_POSITION p)=0
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual unsigned long getOffset() const =0
#define POS_MAXVERSE
Definition: versekey.h:39
signed int chapter
Definition: versekey.h:97
VerseTreeKey(TreeKey *treeKey, const char *ikey=0)
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
virtual void positionChanged()
virtual void setPosition(SW_POSITION newpos)
#define POS_MAXCHAPTER
Definition: versekey.h:40
virtual void setPosition(SW_POSITION newpos)
Definition: versekey.cpp:1288
virtual void setOffset(unsigned long offset)=0
virtual SWKey * clone() const
signed char testament
Definition: versekey.h:95
static SWLog * getSystemLog()
Definition: swlog.cpp:53
virtual void setBookName(const char *bname)
Definition: versekey.cpp:1582
const SWClass * myClass
Definition: swobject.h:55
void setPositionChangeListener(PositionChangeListener *pcl)
Definition: treekey.h:64
virtual const char * getLocalName()=0
VerseKey & getLowerBound() const
Definition: versekey.cpp:1156
virtual int getLevel()
Definition: treekey.h:76
virtual void increment(int steps=1)=0
virtual void decrement(int steps=1)
virtual void positionFrom(const SWKey &ikey)
Definition: versekey.cpp:183
static SWORD_NAMESPACE_START const char * classes[]
long lastGoodOffset
Definition: versetreekey.h:50
static const SWClass classdef(classes)
virtual SWKey * clone() const
Definition: swkey.cpp:75
virtual ~VerseTreeKey()
virtual int _compare(const VerseKey &ikey)
Definition: versekey.cpp:1790
virtual char getSuffix() const
Definition: versekey.cpp:1630
void init()
Definition: swkey.cpp:68
virtual int getChapter() const
Definition: versekey.cpp:1522
virtual char parse(bool checkNormalize=true)
Definition: versekey.cpp:326
virtual int getBookFromAbbrev(const char *abbr) const
Definition: versekey.cpp:396
#define POS_TOP
Definition: swkey.h:65
virtual void setVerse(int iverse)
Definition: versekey.cpp:1622
virtual char getBook() const
Definition: versekey.cpp:1510
virtual int getVerse() const
Definition: versekey.cpp:1534
virtual TreeKey * getTreeKey()
TreeKey * treeKey
Definition: versetreekey.h:45
virtual void increment(int steps=1)
VerseKey & getUpperBound() const
Definition: versekey.cpp:1179
virtual void decrement(int steps=1)=0
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
virtual char popError()
Definition: swkey.cpp:147
virtual void setText(const char *ikey)=0
#define POS_BOTTOM
Definition: swkey.h:66
virtual long newIndex() const
virtual int getBookFromAbbrev(const char *abbr) const
void syncVerseToTree() const
signed char book
Definition: versekey.h:96
char error
Definition: swkey.h:106
virtual void setError(char err)
Definition: swkey.h:165
bool internalPosChange
Definition: versetreekey.h:109
#define SWORD_NAMESPACE_END
Definition: defs.h:40
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
Definition: swkey.h:77
virtual const char * getOSISBookName() const
Definition: versekey.cpp:1270
virtual void Normalize(char autocheck=0)
virtual char getTestament() const
Definition: versekey.cpp:1498