The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
treekey.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * treekey.cpp - code for class 'versekey'- a standard Biblical
4  * verse key
5  *
6  * $Id: treekey.cpp 3808 2020-10-02 13:23:34Z scribe $
7  *
8  * Copyright 2002-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 <treekey.h>
26 #include <utilstr.h>
27 #include <string.h>
28 
30 
31 static const char *classes[] = {"TreeKey", "SWKey", "SWObject", 0};
32 static const SWClass classdef(classes);
33 
34 void TreeKey::init() {
35  myClass = &classdef;
36  unsnappedKeyText = "";
38 }
39 
40 
41 void TreeKey::assureKeyPath(const char *keyBuffer) {
42 
43  if (!keyBuffer) {
44  keyBuffer = unsnappedKeyText;
45  //assert we have something to do before setting root
46  if (!*keyBuffer)
47  return;
48  }
49 
50  char *keybuf = 0;
51  stdstr(&keybuf, keyBuffer);
52 
53  root();
54 
55  // TODO: change to NOT use strtok. strtok is dangerous.
56  SWBuf tok = strtok(keybuf, "/");
57  tok.trim();
58  while (tok.size()) {
59  bool foundkey = false;
60  if (hasChildren()) {
61  firstChild();
62  if (tok == getLocalName()) {
63  foundkey = true;
64  }
65  else {
66  while (nextSibling()) {
67  if (getLocalName()) {
68  if (tok == getLocalName()) {
69  foundkey = true;
70  break;
71  }
72  }
73  }
74  }
75  if (!foundkey) {
76  append();
77  setLocalName(tok);
78  save();
79  }
80  }
81  else {
82  appendChild();
83  setLocalName(tok);
84  save();
85  }
86 
87 #ifdef DEBUG
88 // std::cout << getLocalName() << " : " << tok << std::endl;
89 #endif
90 
91  tok = strtok(0, "/");
92  tok.trim();
93 
94  }
95  delete [] keybuf;
96 }
97 
98 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
static SWORD_NAMESPACE_START const char * classes[]
Definition: treekey.cpp:31
Definition: swbuf.h:47
virtual const char * setLocalName(const char *)=0
virtual void assureKeyPath(const char *keyPath=0)
Definition: treekey.cpp:41
const SWClass * myClass
Definition: swobject.h:55
virtual void append()=0
virtual const char * getLocalName()=0
SWBuf unsnappedKeyText
Definition: treekey.h:47
static const SWClass classdef(classes)
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
SWBuf & trim()
Definition: swbuf.h:443
virtual void root()=0
virtual bool hasChildren()=0
unsigned long size() const
Definition: swbuf.h:185
class TreeKey::PositionChangeListener * posChangeListener
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual bool nextSibling()=0
virtual void appendChild()=0
virtual void save()
Definition: treekey.h:135
void init()
Definition: treekey.cpp:34
virtual bool firstChild()=0