The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TreeKeyIdx Class Reference

#include <treekeyidx.h>

+ Inheritance diagram for TreeKeyIdx:
+ Collaboration diagram for TreeKeyIdx:

Classes

class  TreeNode
 

Public Member Functions

virtual int _compare (const TreeKeyIdx &ikey)
 
virtual void append ()
 
virtual void appendChild ()
 
virtual void assureKeyPath (const char *keyPath=0)
 
virtual void clearBounds () const
 
virtual SWKeyclone () const
 
virtual int compare (const SWKey &ikey)
 
virtual void copyFrom (const TreeKeyIdx &ikey)
 
virtual void copyFrom (const SWKey &ikey)
 
virtual void decrement (int steps=1)
 
virtual bool equals (const SWKey &ikey)
 
SWDEPRECATED char Error ()
 
virtual bool firstChild ()
 
const SWClassgetClass () const
 
virtual char getError () const
 
virtual long getIndex () const
 
virtual int getLevel ()
 
char * getLocale () const
 
virtual const char * getLocalName ()
 
unsigned long getOffset () const
 
virtual const char * getOSISRefRangeText () const
 
virtual const char * getRangeText () const
 
virtual const char * getShortRangeText () const
 
virtual const char * getShortText () const
 
virtual const char * getText () const
 
virtual const char * getUserData (int *size=0) const
 
virtual bool hasChildren ()
 
virtual void increment (int steps=1)
 
virtual void insertBefore ()
 
virtual bool isBoundSet () const
 
bool isPersist () const
 
virtual bool isTraversable () const
 
virtual bool nextSibling ()
 
virtual SWKeyoperator= (const TreeKeyIdx &ikey)
 
virtual bool parent ()
 
SWDEPRECATED char Persist () const
 
SWDEPRECATED char Persist (signed char ipersist)
 
virtual char popError ()
 
virtual void positionFrom (const SWKey &ikey)
 
virtual bool previousSibling ()
 
virtual void remove ()
 
virtual void root ()
 
virtual void save ()
 
virtual void setError (char err)
 
virtual void setIndex (long iindex)
 
void setLocale (const char *name)
 
virtual const char * setLocalName (const char *)
 
void setOffset (unsigned long offset)
 
void setPersist (bool ipersist)
 
virtual void setPosition (SW_POSITION p)
 
void setPositionChangeListener (PositionChangeListener *pcl)
 
virtual SWKEY_OPERATORS void setText (const char *ikey)
 
virtual void setUserData (const char *userData, int size=0)
 
 TreeKeyIdx (const TreeKeyIdx &ikey)
 
 TreeKeyIdx (const char *idxPath, int fileMode=-1)
 
virtual ~TreeKeyIdx ()
 

Static Public Member Functions

static signed char create (const char *path)
 

Public Attributes

class
TreeKey::PositionChangeListener
posChangeListener
 
SW_u64 userData
 

Protected Member Functions

SWLocalegetPrivateLocale () const
 
void positionChanged ()
 

Protected Attributes

bool boundSet
 
char error
 
char * keytext
 
char * localeName
 
const SWClassmyClass
 
bool persist
 
char * rangeText
 
SWBuf unsnappedKeyText
 

Private Member Functions

void getTreeNodeFromDatOffset (long ioffset, TreeNode *buf) const
 
char getTreeNodeFromIdxOffset (long ioffset, TreeNode *node) const
 
void init ()
 
void saveTreeNode (TreeNode *node)
 
void saveTreeNodeOffsets (TreeNode *node)
 

Private Attributes

class TreeKeyIdx::TreeNode currentNode
 
FileDescdatfd
 
FileDescidxfd
 
char * path
 

Detailed Description

Class TreeKeyIdx The TreeKey implementation used for tree-based modules in SWORD, such as GenBooks.

Definition at line 39 of file treekeyidx.h.

Constructor & Destructor Documentation

TreeKeyIdx::TreeKeyIdx ( const TreeKeyIdx ikey)

Definition at line 41 of file treekeyidx.cpp.

41  : currentNode() {
42  init();
43  path = 0;
44  idxfd = 0;
45  datfd = 0;
46  copyFrom(ikey);
47 }
virtual void copyFrom(const TreeKeyIdx &ikey)
Definition: treekeyidx.cpp:450
FileDesc * idxfd
Definition: treekeyidx.h:58
char * path
Definition: treekeyidx.h:56
void init()
Definition: treekeyidx.cpp:77
FileDesc * datfd
Definition: treekeyidx.h:59
class TreeKeyIdx::TreeNode currentNode
TreeKeyIdx::TreeKeyIdx ( const char *  idxPath,
int  fileMode = -1 
)

Definition at line 49 of file treekeyidx.cpp.

49  : currentNode() {
50  SWBuf buf;
51 
52  init();
53  path = 0;
54  stdstr(&path, idxPath);
55 
56  if (fileMode == -1) { // try read/write if possible
57  fileMode = FileMgr::RDWR;
58  }
59 
60  buf.setFormatted("%s.idx", path);
61  idxfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
62  buf.setFormatted("%s.dat", path);
63  datfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
64 
65  if (!datfd || datfd->getFd() < 0) {
66 // couldn't find datafile but this might be fine if we're
67 // merely instantiating a remote InstallMgr SWMgr
68 SWLOGD("Couldn't open file: %s. errno: %d", buf.c_str(), errno);
69  error = errno;
70  }
71  else {
72  root();
73  }
74 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
virtual void root()
Definition: treekeyidx.cpp:131
static unsigned int RDWR
Definition: filemgr.h:76
FileDesc * idxfd
Definition: treekeyidx.h:58
int getFd()
Definition: filemgr.h:231
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
char * path
Definition: treekeyidx.h:56
void init()
Definition: treekeyidx.cpp:77
const char * c_str() const
Definition: swbuf.h:158
FileDesc * datfd
Definition: treekeyidx.h:59
class TreeKeyIdx::TreeNode currentNode
char error
Definition: swkey.h:106
#define SWLOGD(...)
Definition: defs.h:187
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
TreeKeyIdx::~TreeKeyIdx ( )
virtual

Definition at line 82 of file treekeyidx.cpp.

82  {
83  if (path)
84  delete [] path;
85 
88 }
FileDesc * idxfd
Definition: treekeyidx.h:58
char * path
Definition: treekeyidx.h:56
void close(FileDesc *file)
Definition: filemgr.cpp:196
FileDesc * datfd
Definition: treekeyidx.h:59
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101

Member Function Documentation

int TreeKeyIdx::_compare ( const TreeKeyIdx ikey)
virtual

Definition at line 570 of file treekeyidx.cpp.

570  {
571  return (int)(getOffset() - ikey.getOffset());
572 }
unsigned long getOffset() const
Definition: treekeyidx.cpp:410
void TreeKeyIdx::append ( )
virtual

Implements TreeKey.

Definition at line 203 of file treekeyidx.cpp.

203  {
204  TreeNode lastSib;
205  if (currentNode.offset) {
207  while (lastSib.next > -1) {
208  getTreeNodeFromIdxOffset(lastSib.next, &lastSib);
209  }
210  SW_u32 idxOffset = (SW_u32)idxfd->seek(0, SEEK_END);
211  lastSib.next = idxOffset;
212  saveTreeNodeOffsets(&lastSib);
214  currentNode.clear();
215  currentNode.offset = idxOffset;
217  positionChanged();
218  }
219 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define SEEK_END
Definition: zconf.h:246
FileDesc * idxfd
Definition: treekeyidx.h:58
virtual bool parent()
Definition: treekeyidx.cpp:148
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
unsigned int SW_u32
Definition: sysdata.h:41
void saveTreeNodeOffsets(TreeNode *node)
Definition: treekeyidx.cpp:421
void TreeKeyIdx::appendChild ( )
virtual

Implements TreeKey.

Definition at line 222 of file treekeyidx.cpp.

222  {
223  if (firstChild()) {
224  append();
225  }
226  else {
227  SW_u32 idxOffset = (SW_u32)idxfd->seek(0, SEEK_END);
228  currentNode.firstChild = idxOffset;
231  currentNode.clear();
232  currentNode.offset = idxOffset;
234  }
235  positionChanged();
236 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define SEEK_END
Definition: zconf.h:246
FileDesc * idxfd
Definition: treekeyidx.h:58
virtual bool parent()
Definition: treekeyidx.cpp:148
virtual void append()
Definition: treekeyidx.cpp:203
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
unsigned int SW_u32
Definition: sysdata.h:41
void saveTreeNodeOffsets(TreeNode *node)
Definition: treekeyidx.cpp:421
virtual bool firstChild()
Definition: treekeyidx.cpp:158
void TreeKey::assureKeyPath ( const char *  keyPath = 0)
virtualinherited

Set the key to this path. If the path doesn't exist, then nodes are created as necessary

Parameters
keyPathpath to set/create; if unsupplied, then use any unsnapped setText value.

Definition at line 41 of file treekey.cpp.

41  {
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 }
Definition: swbuf.h:47
virtual const char * setLocalName(const char *)=0
virtual void append()=0
virtual const char * getLocalName()=0
SWBuf unsnappedKeyText
Definition: treekey.h:47
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
virtual bool nextSibling()=0
virtual void appendChild()=0
virtual void save()
Definition: treekey.h:135
virtual bool firstChild()=0
virtual void SWKey::clearBounds ( ) const
inlinevirtualinherited

Reimplemented in VerseKey.

Definition at line 190 of file swkey.h.

190 { boundSet = false; }
bool boundSet
Definition: swkey.h:104
SWKey * TreeKeyIdx::clone ( ) const
virtual

Returns a new exact clone of this SWKey object. This allocates a new SWKey which must be deleted by the caller

Returns
new clone of this key

Reimplemented from SWKey.

Definition at line 668 of file treekeyidx.cpp.

669 {
670  return new TreeKeyIdx(*this);
671 }
TreeKeyIdx(const TreeKeyIdx &ikey)
Definition: treekeyidx.cpp:41
int TreeKeyIdx::compare ( const SWKey ikey)
virtual

Compares this key object to another SWKey object

Parameters
ikeykey to compare with this one
Returns
>0 if this key is greater than compare key; <0 if this key is smaller than compare key; 0 if the keys are the same

Implements TreeKey.

Definition at line 575 of file treekeyidx.cpp.

575  {
576  const TreeKeyIdx *treeKey = SWDYNAMIC_CAST(const TreeKeyIdx, (&ikey));
577  if (treeKey)
578  return _compare(*treeKey);
579  return SWKey::compare(ikey);
580 }
virtual int _compare(const TreeKeyIdx &ikey)
Definition: treekeyidx.cpp:570
virtual int compare(const SWKey &ikey)
Definition: swkey.cpp:218
#define SWDYNAMIC_CAST(className, object)
Definition: defs.h:47
void TreeKeyIdx::copyFrom ( const TreeKeyIdx ikey)
virtual

Definition at line 450 of file treekeyidx.cpp.

450  {
451  unsnappedKeyText = "";
452 
453  SWKey::copyFrom(ikey);
454 
461 
462  if (currentNode.userData)
463  delete [] currentNode.userData;
464  if (currentNode.dsize) {
465  currentNode.userData = new char [ currentNode.dsize ];
467  }
468  else currentNode.userData = 0;
469 
470  bool newFiles = true;
471 
472  if (path && ikey.path)
473  newFiles = strcmp(path, ikey.path);
474 
475  if (newFiles) {
476  stdstr(&path, ikey.path);
477 
478  if (idxfd) {
481  }
482  idxfd = FileMgr::getSystemFileMgr()->open(ikey.idxfd->path, ikey.idxfd->mode, ikey.idxfd->perms);
483  datfd = FileMgr::getSystemFileMgr()->open(ikey.datfd->path, ikey.datfd->mode, ikey.datfd->perms);
484  }
485  positionChanged();
486 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
int perms
Definition: filemgr.h:251
int mode
Definition: filemgr.h:248
SWBuf unsnappedKeyText
Definition: treekey.h:47
FileDesc * idxfd
Definition: treekeyidx.h:58
char * path
Definition: filemgr.h:245
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
char * path
Definition: treekeyidx.h:56
void close(FileDesc *file)
Definition: filemgr.cpp:196
virtual void copyFrom(const SWKey &ikey)
Definition: swkey.cpp:173
FileDesc * datfd
Definition: treekeyidx.h:59
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
void TreeKeyIdx::copyFrom ( const SWKey ikey)
virtual

Copies as much info (position, range, etc.) as possible from another SWKey object

Parameters
ikeyother SWKey object from which to copy

Reimplemented from SWKey.

Definition at line 550 of file treekeyidx.cpp.

550  {
551  unsnappedKeyText = ikey;
552  SWKey::copyFrom(ikey);
553  positionChanged();
554 }
SWBuf unsnappedKeyText
Definition: treekey.h:47
virtual void copyFrom(const SWKey &ikey)
Definition: swkey.cpp:173
void positionChanged()
Definition: treekey.h:51
signed char TreeKeyIdx::create ( const char *  path)
static

Definition at line 290 of file treekeyidx.cpp.

290  {
291  char *path = 0;
292  char *buf = new char [ strlen (ipath) + 20 ];
293  FileDesc *fd, *fd2;
294 
295  stdstr(&path, ipath);
296 
297  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
298  path[strlen(path)-1] = 0;
299 
300  sprintf(buf, "%s.dat", path);
301  FileMgr::removeFile(buf);
303  fd->getFd();
305 
306  sprintf(buf, "%s.idx", path);
307  FileMgr::removeFile(buf);
309  fd2->getFd();
311  TreeKeyIdx newTree(path);
313  stdstr(&(root.name), "");
314  newTree.saveTreeNode(&root);
315 
316  delete [] path;
317 
318  return 0;
319 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
virtual void root()
Definition: treekeyidx.cpp:131
int getFd()
Definition: filemgr.h:231
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
char * path
Definition: treekeyidx.h:56
void close(FileDesc *file)
Definition: filemgr.cpp:196
static int removeFile(const char *fName)
Definition: filemgr.cpp:517
static unsigned int CREAT
Definition: filemgr.h:72
static unsigned int IWRITE
Definition: filemgr.h:79
static unsigned int WRONLY
Definition: filemgr.h:77
static unsigned int IREAD
Definition: filemgr.h:78
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
void TreeKeyIdx::decrement ( int  steps = 1)
virtual

Decrements key a number of entry positions This is only valid if isTraversable is true

Parameters
stepsNumber of entries to jump backward

Implements TreeKey.

Definition at line 583 of file treekeyidx.cpp.

583  {
585  positionChanged();
586 }
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
virtual bool SWKey::equals ( const SWKey ikey)
inlinevirtualinherited

test equality of this SWKey object's position with another SWKey

Parameters
ikeykey to compare with this one
Returns
true if the key positions are equal

Definition at line 204 of file swkey.h.

204 { return !compare(ikey); }
virtual int compare(const SWKey &ikey)
Definition: swkey.cpp:218
SWDEPRECATED char SWKey::Error ( )
inlineinherited
Deprecated:
Use popError and getError instead

Definition at line 169 of file swkey.h.

169 { return popError(); }
virtual char popError()
Definition: swkey.cpp:147
bool TreeKeyIdx::firstChild ( )
virtual

Go to the first child of the current node

Returns
success or failure

Implements TreeKey.

Definition at line 158 of file treekeyidx.cpp.

158  {
159  if (currentNode.firstChild > -1) {
161  positionChanged();
162  return true;
163  }
164  return false;
165 }
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
const SWClass* SWObject::getClass ( ) const
inlineinherited

Use this to get the class definition and inheritance order.

Returns
The class definition of this object

Definition at line 63 of file swobject.h.

63  {
64  return myClass;
65  }
const SWClass * myClass
Definition: swobject.h:55
virtual char SWKey::getError ( ) const
inlinevirtualinherited

Definition at line 164 of file swkey.h.

164 { return error; }
char error
Definition: swkey.h:106
virtual long TreeKey::getIndex ( ) const
inlinevirtualinherited

Use this function to get an index position within a module.

Reimplemented from SWKey.

Definition at line 127 of file treekey.h.

127 { return getOffset(); }
virtual unsigned long getOffset() const =0
int TreeKeyIdx::getLevel ( )
virtual

Reimplemented from TreeKey.

Definition at line 136 of file treekeyidx.cpp.

136  {
137  TreeNode iterator;
138  iterator.parent = currentNode.parent;
139  int level = 0;
140  while (iterator.parent > -1) {
141  level++;
142  getTreeNodeFromIdxOffset(iterator.parent, &iterator);
143  }
144  return level;
145 }
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
char* SWKey::getLocale ( ) const
inlineinherited

Definition at line 224 of file swkey.h.

224 { return localeName; }
char * localeName
Definition: swkey.h:108
const char * TreeKeyIdx::getLocalName ( )
virtual

Implements TreeKey.

Definition at line 91 of file treekeyidx.cpp.

91  {
92  unsnappedKeyText = "";
93  return currentNode.name;
94 }
SWBuf unsnappedKeyText
Definition: treekey.h:47
class TreeKeyIdx::TreeNode currentNode
unsigned long TreeKeyIdx::getOffset ( ) const
virtual

Implements TreeKey.

Definition at line 410 of file treekeyidx.cpp.

410  {
411  unsnappedKeyText = "";
412  return currentNode.offset;
413 }
SWBuf unsnappedKeyText
Definition: treekey.h:47
class TreeKeyIdx::TreeNode currentNode
const char * SWKey::getOSISRefRangeText ( ) const
virtualinherited

Reimplemented in VerseKey, and ListKey.

Definition at line 203 of file swkey.cpp.

203  {
204  return getRangeText();
205 }
virtual const char * getRangeText() const
Definition: swkey.cpp:193
SWLocale * SWKey::getPrivateLocale ( ) const
protectedinherited

Definition at line 111 of file swkey.cpp.

111  {
112  if (!locale) {
113  if ((!localeCache.name) || (strcmp(localeCache.name, localeName))) {
115  // this line is the entire bit of work we're trying to avoid with the cache
116  // worth it? compare time examples/cmdline/search KJV "God love world" to
117  // same with this method reduced to:
118  // if (!local) local = ... (call below); return locale;
120  }
122  }
123  return locale;
124 }
SWLocale * locale
Definition: swkey.h:82
char * localeName
Definition: swkey.h:108
char * name
Definition: swkey.h:81
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
virtual SWLocale * getLocale(const char *name)
Definition: localemgr.cpp:210
static LocaleMgr * getSystemLocaleMgr()
Definition: localemgr.cpp:54
SWLocale * locale
Definition: swkey.h:94
static LocaleCache localeCache
Definition: swkey.h:92
const char * SWKey::getRangeText ( ) const
virtualinherited

Reimplemented in VerseKey, and ListKey.

Definition at line 193 of file swkey.cpp.

193  {
195  return rangeText;
196 }
char * keytext
Definition: swkey.h:102
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
char * rangeText
Definition: swkey.h:103
virtual const char* SWKey::getShortRangeText ( ) const
inlinevirtualinherited

Reimplemented in VerseKey, and ListKey.

Definition at line 187 of file swkey.h.

187 { return getRangeText(); }
virtual const char * getRangeText() const
Definition: swkey.cpp:193
virtual const char* SWKey::getShortText ( ) const
inlinevirtualinherited

Reimplemented in VerseKey, and ListKey.

Definition at line 185 of file swkey.h.

185 { return getText(); }
virtual const char * getText() const
Definition: swkey.cpp:184
const char * TreeKeyIdx::getText ( ) const
virtual

returns string representation of this key

Implements TreeKey.

Definition at line 615 of file treekeyidx.cpp.

615  {
616  TreeNode parent;
617  static SWBuf fullPath;
618  fullPath = currentNode.name;
619  parent.parent = currentNode.parent;
620  while (parent.parent > -1) {
621  getTreeNodeFromIdxOffset(parent.parent, &parent);
622  fullPath = ((SWBuf)parent.name) + (SWBuf) "/" + fullPath;
623  }
624  // we've snapped; clear our unsnapped text holder
625  unsnappedKeyText = "";
626  return fullPath.c_str();
627 }
Definition: swbuf.h:47
SWBuf unsnappedKeyText
Definition: treekey.h:47
const char * c_str() const
Definition: swbuf.h:158
virtual bool parent()
Definition: treekeyidx.cpp:148
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void TreeKeyIdx::getTreeNodeFromDatOffset ( long  ioffset,
TreeNode buf 
) const
private

Definition at line 331 of file treekeyidx.cpp.

331  {
332  unsnappedKeyText = "";
333  char ch;
334  SW_s32 tmp;
335  SW_u16 tmp2;
336 
337  if (datfd && datfd->getFd() >= 0) {
338 
339  datfd->seek(ioffset, SEEK_SET);
340 
341  datfd->read(&tmp, 4);
342  node->parent = swordtoarch32(tmp);
343 
344  datfd->read(&tmp, 4);
345  node->next = swordtoarch32(tmp);
346 
347  datfd->read(&tmp, 4);
348  node->firstChild = swordtoarch32(tmp);
349 
350  SWBuf name;
351  do {
352  datfd->read(&ch, 1);
353  name += ch;
354  } while (ch);
355 
356  stdstr(&(node->name), name.c_str());
357 
358  datfd->read(&tmp2, 2);
359  node->dsize = swordtoarch16(tmp2);
360 
361  if (node->dsize) {
362  if (node->userData)
363  delete [] node->userData;
364  node->userData = new char [node->dsize];
365  datfd->read(node->userData, node->dsize);
366  }
367  }
368 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
Definition: swbuf.h:47
signed int SW_s32
Definition: sysdata.h:40
SWBuf unsnappedKeyText
Definition: treekey.h:47
int getFd()
Definition: filemgr.h:231
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
const char * c_str() const
Definition: swbuf.h:158
#define swordtoarch32(x)
Definition: sysdata.h:94
unsigned short SW_u16
Definition: sysdata.h:38
FileDesc * datfd
Definition: treekeyidx.h:59
#define SEEK_SET
Definition: zconf.h:244
#define swordtoarch16(x)
Definition: sysdata.h:93
long read(void *buf, long count)
Definition: filemgr.cpp:148
char TreeKeyIdx::getTreeNodeFromIdxOffset ( long  ioffset,
TreeNode node 
) const
private

Definition at line 380 of file treekeyidx.cpp.

380  {
381  unsnappedKeyText = "";
382  SW_u32 offset;
383  char error = KEYERR_OUTOFBOUNDS;
384 
385  if (ioffset < 0) {
386  ioffset = 0;
387  error = 77; // out of bounds but still position to 0;
388  }
389 
390  node->offset = (SW_s32)ioffset;
391  if (idxfd && idxfd->getFd() >= 0) {
392  idxfd->seek(ioffset, SEEK_SET);
393  if (idxfd->read(&offset, 4) == 4) {
394  offset = swordtoarch32(offset);
395  error = (error == 77) ? KEYERR_OUTOFBOUNDS : 0;
396  getTreeNodeFromDatOffset(offset, node);
397  }
398  else {
399  idxfd->seek(-4, SEEK_END);
400  if (idxfd->read(&offset, 4) == 4) {
401  offset = swordtoarch32(offset);
402  getTreeNodeFromDatOffset(offset, node);
403  }
404  }
405  }
406  return error;
407 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define SEEK_END
Definition: zconf.h:246
signed int SW_s32
Definition: sysdata.h:40
SWBuf unsnappedKeyText
Definition: treekey.h:47
FileDesc * idxfd
Definition: treekeyidx.h:58
int getFd()
Definition: filemgr.h:231
void getTreeNodeFromDatOffset(long ioffset, TreeNode *buf) const
Definition: treekeyidx.cpp:331
#define swordtoarch32(x)
Definition: sysdata.h:94
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
#define SEEK_SET
Definition: zconf.h:244
unsigned int SW_u32
Definition: sysdata.h:41
char error
Definition: swkey.h:106
long read(void *buf, long count)
Definition: filemgr.cpp:148
const char * TreeKeyIdx::getUserData ( int *  size = 0) const
virtual

Implements TreeKey.

Definition at line 97 of file treekeyidx.cpp.

97  {
98  unsnappedKeyText = "";
99  if (size)
100  *size = (int)currentNode.dsize;
101  return currentNode.userData;
102 }
SWBuf unsnappedKeyText
Definition: treekey.h:47
int size
Definition: regex.c:5043
class TreeKeyIdx::TreeNode currentNode
bool TreeKeyIdx::hasChildren ( )
virtual

Does the current node have children?

Returns
whether or not it does

Implements TreeKey.

Definition at line 198 of file treekeyidx.cpp.

198  {
199  return (currentNode.firstChild > -1);
200 }
class TreeKeyIdx::TreeNode currentNode
void TreeKeyIdx::increment ( int  steps = 1)
virtual

Increments key a number of entry positions This is only valid if isTraversable is true

Parameters
stepsNumber of entries to jump forward

Implements TreeKey.

Definition at line 588 of file treekeyidx.cpp.

588  {
590  if (error) {
591  SWLog::getSystemLog(); // strange fix for android // ->logError("error: %d", error);
592  }
593  positionChanged();
594 
595 /*
596  // assert positive
597  if (steps < 0) {
598  decrement(steps * -1);
599  return;
600  }
601 
602  while (steps > 0) {
603  if (!firstChild()) {
604  if (!nextSibbling() {
605  error = KEYERR_OUTOFBOUNDS;
606  return;
607  }
608  }
609  steps--;
610  }
611 */
612 }
static SWLog * getSystemLog()
Definition: swlog.cpp:53
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
void TreeKeyIdx::init ( )
private

Definition at line 77 of file treekeyidx.cpp.

77  {
78  myClass = &classdef;
79 }
const SWClass * myClass
Definition: swobject.h:55
static const SWClass classdef(classes)
void TreeKeyIdx::insertBefore ( )
virtual

Implements TreeKey.

Definition at line 239 of file treekeyidx.cpp.

239  {
240 }
virtual bool SWKey::isBoundSet ( ) const
inlinevirtualinherited

Definition at line 189 of file swkey.h.

189 { return boundSet; }
bool boundSet
Definition: swkey.h:104
bool SWKey::isPersist ( ) const
inherited

Gets whether this key should persist in any module to which it is set otherwise just a copy will be used in the module.

Returns
1 - persists in module; 0 - a copy is attempted

Definition at line 99 of file swkey.cpp.

100 {
101  return persist;
102 }
bool persist
Definition: swkey.h:105
virtual bool TreeKeyIdx::isTraversable ( ) const
inlinevirtual

Whether or not this key can be ++ – incremented

Reimplemented from TreeKey.

Definition at line 119 of file treekeyidx.h.

119 { return true; }
bool TreeKeyIdx::nextSibling ( )
virtual

Go to the next sibling of the current node

Returns
success or failure

Implements TreeKey.

Definition at line 168 of file treekeyidx.cpp.

168  {
169  if (currentNode.next > -1) {
171  positionChanged();
172  return true;
173  }
174  return false;
175 }
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
virtual SWKey& TreeKeyIdx::operator= ( const TreeKeyIdx ikey)
inlinevirtual

Definition at line 109 of file treekeyidx.h.

109 { copyFrom(ikey); return *this; }
virtual void copyFrom(const TreeKeyIdx &ikey)
Definition: treekeyidx.cpp:450
bool TreeKeyIdx::parent ( )
virtual

Go to the parent of the current node

Returns
success or failure

Implements TreeKey.

Definition at line 148 of file treekeyidx.cpp.

148  {
149  if (currentNode.parent > -1) {
151  positionChanged();
152  return true;
153  }
154  return false;
155 }
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
SWDEPRECATED char SWKey::Persist ( ) const
inlineinherited
Deprecated:
Use isPersist

Definition at line 148 of file swkey.h.

148 { return isPersist(); }
bool isPersist() const
Definition: swkey.cpp:99
SWDEPRECATED char SWKey::Persist ( signed char  ipersist)
inlineinherited
Deprecated:
Use setPersist and isPersist instead.

Definition at line 158 of file swkey.h.

158 { setPersist(ipersist!=0); return isPersist(); }
void setPersist(bool ipersist)
Definition: swkey.cpp:135
bool isPersist() const
Definition: swkey.cpp:99
char SWKey::popError ( )
virtualinherited

Gets and clears error status

Returns
error status

Definition at line 147 of file swkey.cpp.

148 {
149  char retval = error;
150 
151  error = 0;
152  return retval;
153 }
char error
Definition: swkey.h:106
void TreeKey::positionChanged ( )
inlineprotectedinherited

Definition at line 51 of file treekey.h.

virtual void positionChanged()=0
class TreeKey::PositionChangeListener * posChangeListener
virtual void SWKey::positionFrom ( const SWKey ikey)
inlinevirtualinherited

Reimplemented in VerseKey.

Definition at line 180 of file swkey.h.

180 { copyFrom(ikey); }
virtual void copyFrom(const SWKey &ikey)
Definition: swkey.cpp:173
bool TreeKeyIdx::previousSibling ( )
virtual

Go to the previous sibling of the current node

Returns
success or failure

Implements TreeKey.

Definition at line 178 of file treekeyidx.cpp.

178  {
179  TreeNode iterator;
180  SW_s32 target = currentNode.offset;
181  if (currentNode.parent > -1) {
183  getTreeNodeFromIdxOffset(iterator.firstChild, &iterator);
184  if (iterator.offset != target) {
185  while ((iterator.next != target) && (iterator.next > -1))
186  getTreeNodeFromIdxOffset(iterator.next, &iterator);
187  if (iterator.next > -1) {
188  error = getTreeNodeFromIdxOffset(iterator.offset, &currentNode);
189  positionChanged();
190  return true;
191  }
192  }
193  }
194  return false;
195 }
signed int SW_s32
Definition: sysdata.h:40
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
void TreeKeyIdx::remove ( )
virtual

Implements TreeKey.

Definition at line 243 of file treekeyidx.cpp.

243  {
244  TreeNode node;
245  bool done = false;
246  if (currentNode.offset) {
248  if (node.parent > -1) {
249  TreeNode parent;
250  getTreeNodeFromIdxOffset(node.parent, &parent);
251  if (parent.firstChild == node.offset) {
252  parent.firstChild = node.next;
253  saveTreeNodeOffsets(&parent);
254  getTreeNodeFromIdxOffset(parent.offset, &currentNode);
255  done = true;
256  }
257  }
258  if (!done) {
259  TreeNode iterator;
260  SW_s32 target = currentNode.offset;
261  if (currentNode.parent > -1) {
263  getTreeNodeFromIdxOffset(iterator.firstChild, &iterator);
264  if (iterator.offset != target) {
265  while ((iterator.next != target) && (iterator.next > -1)) {
266  getTreeNodeFromIdxOffset(iterator.next, &iterator);
267  }
268  if (iterator.next > -1) {
269  TreeNode prev;
270  getTreeNodeFromIdxOffset(iterator.offset, &prev);
271  prev.next = node.next;
272  saveTreeNodeOffsets(&prev);
273  getTreeNodeFromIdxOffset(prev.offset, &currentNode);
274  }
275  }
276  }
277  }
278  positionChanged();
279  }
280 }
signed int SW_s32
Definition: sysdata.h:40
virtual bool parent()
Definition: treekeyidx.cpp:148
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
void saveTreeNodeOffsets(TreeNode *node)
Definition: treekeyidx.cpp:421
void TreeKeyIdx::root ( )
virtual

Go to the root node

Implements TreeKey.

Definition at line 131 of file treekeyidx.cpp.

131  {
133  positionChanged();
134 }
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
void TreeKeyIdx::save ( )
virtual

Reimplemented from TreeKey.

Definition at line 126 of file treekeyidx.cpp.

126  {
128 }
class TreeKeyIdx::TreeNode currentNode
void saveTreeNode(TreeNode *node)
Definition: treekeyidx.cpp:489
void TreeKeyIdx::saveTreeNode ( TreeNode node)
private

Definition at line 489 of file treekeyidx.cpp.

489  {
490  long datOffset = 0;
491  SW_s32 tmp;
492  if (idxfd && idxfd->getFd() >= 0) {
493 
494  idxfd->seek(node->offset, SEEK_SET);
495  datOffset = datfd->seek(0, SEEK_END);
496  tmp = (SW_s32)archtosword32(datOffset);
497  idxfd->write(&tmp, 4);
498 
499  saveTreeNodeOffsets(node);
500 
501  datfd->write(node->name, strlen(node->name));
502  char null = 0;
503  datfd->write(&null, 1);
504 
505  SW_u16 tmp2 = archtosword16(node->dsize);
506  datfd->write(&tmp2, 2);
507 
508  if (node->dsize) {
509  datfd->write(node->userData, node->dsize);
510  }
511  }
512 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define archtosword32(x)
Definition: sysdata.h:97
#define SEEK_END
Definition: zconf.h:246
signed int SW_s32
Definition: sysdata.h:40
FileDesc * idxfd
Definition: treekeyidx.h:58
int getFd()
Definition: filemgr.h:231
long write(const void *buf, long count)
Definition: filemgr.cpp:153
unsigned short SW_u16
Definition: sysdata.h:38
FileDesc * datfd
Definition: treekeyidx.h:59
#define SEEK_SET
Definition: zconf.h:244
void saveTreeNodeOffsets(TreeNode *node)
Definition: treekeyidx.cpp:421
#define archtosword16(x)
Definition: sysdata.h:96
void TreeKeyIdx::saveTreeNodeOffsets ( TreeNode node)
private

Definition at line 421 of file treekeyidx.cpp.

421  {
422  unsnappedKeyText = "";
423  long datOffset = 0;
424  SW_s32 tmp;
425 
426  if (idxfd && idxfd->getFd() >= 0) {
427  idxfd->seek(node->offset, SEEK_SET);
428  if (idxfd->read(&tmp, 4) != 4) {
429  datOffset = datfd->seek(0, SEEK_END);
430  tmp = (SW_s32)archtosword32(datOffset);
431  idxfd->write(&tmp, 4);
432  }
433  else {
434  datOffset = swordtoarch32(tmp);
435  datfd->seek(datOffset, SEEK_SET);
436  }
437 
438  tmp = (SW_s32)archtosword32(node->parent);
439  datfd->write(&tmp, 4);
440 
441  tmp = (SW_s32)archtosword32(node->next);
442  datfd->write(&tmp, 4);
443 
444  tmp = (SW_s32)archtosword32(node->firstChild);
445  datfd->write(&tmp, 4);
446  }
447 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define archtosword32(x)
Definition: sysdata.h:97
#define SEEK_END
Definition: zconf.h:246
signed int SW_s32
Definition: sysdata.h:40
SWBuf unsnappedKeyText
Definition: treekey.h:47
FileDesc * idxfd
Definition: treekeyidx.h:58
int getFd()
Definition: filemgr.h:231
long write(const void *buf, long count)
Definition: filemgr.cpp:153
#define swordtoarch32(x)
Definition: sysdata.h:94
FileDesc * datfd
Definition: treekeyidx.h:59
#define SEEK_SET
Definition: zconf.h:244
long read(void *buf, long count)
Definition: filemgr.cpp:148
virtual void SWKey::setError ( char  err)
inlinevirtualinherited

Definition at line 165 of file swkey.h.

165 { error = err; }
char error
Definition: swkey.h:106
virtual void TreeKey::setIndex ( long  iindex)
inlinevirtualinherited

See documentation for Index()

Reimplemented from SWKey.

Definition at line 128 of file treekey.h.

128 { setOffset(iindex); }
virtual void setOffset(unsigned long offset)=0
void SWKey::setLocale ( const char *  name)
inlineinherited

Definition at line 225 of file swkey.h.

225 { stdstr(&localeName, name); locale = 0; } // this will force an on demand lookup of our locale
char * localeName
Definition: swkey.h:108
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
SWLocale * locale
Definition: swkey.h:94
const char * TreeKeyIdx::setLocalName ( const char *  newName)
virtual

Implements TreeKey.

Definition at line 119 of file treekeyidx.cpp.

119  {
120  unsnappedKeyText = "";
121  stdstr(&(currentNode.name), newName);
122  return currentNode.name;
123 }
SWBuf unsnappedKeyText
Definition: treekey.h:47
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
class TreeKeyIdx::TreeNode currentNode
void TreeKeyIdx::setOffset ( unsigned long  offset)
virtual

Implements TreeKey.

Definition at line 415 of file treekeyidx.cpp.

415  {
417  positionChanged();
418 }
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
void SWKey::setPersist ( bool  ipersist)
inherited

Sets whether this key should persist in any module to which it is set otherwise just a copy will be used in the module.

Parameters
ipersistvalue which to set persist;

Definition at line 135 of file swkey.cpp.

136 {
137  persist = ipersist;
138 }
bool persist
Definition: swkey.h:105
void TreeKeyIdx::setPosition ( SW_POSITION  p)
virtual

Implements TreeKey.

Definition at line 556 of file treekeyidx.cpp.

556  {
557  switch (p) {
558  case POS_TOP:
559  root();
560  break;
561  case POS_BOTTOM:
563  break;
564  }
565  positionChanged();
566  popError(); // clear error from normalize
567 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
virtual void root()
Definition: treekeyidx.cpp:131
#define SEEK_END
Definition: zconf.h:246
FileDesc * idxfd
Definition: treekeyidx.h:58
#define POS_TOP
Definition: swkey.h:65
char getTreeNodeFromIdxOffset(long ioffset, TreeNode *node) const
Definition: treekeyidx.cpp:380
virtual char popError()
Definition: swkey.cpp:147
#define POS_BOTTOM
Definition: swkey.h:66
class TreeKeyIdx::TreeNode currentNode
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
void TreeKey::setPositionChangeListener ( PositionChangeListener pcl)
inlineinherited

Definition at line 64 of file treekey.h.

class TreeKey::PositionChangeListener * posChangeListener
void setTreeKey(TreeKey *tk)
Definition: treekey.h:61
void TreeKeyIdx::setText ( const char *  ikey)
virtual

Sets this SWKey with a character string

Parameters
ikeystring used to set this key

Implements TreeKey.

Definition at line 515 of file treekeyidx.cpp.

515  {
516  char *buf = 0;
517  stdstr(&buf, ikey);
518  SWBuf leaf = strtok(buf, "/");
519  leaf.trim();
520  root();
521  while ((leaf.size()) && (!popError())) {
522  bool ok, inChild = false;
524  for (ok = firstChild(); ok; ok = nextSibling()) {
525  inChild = true;
526  if (leaf == getLocalName()) {
527  error = 0;
528  break;
529  }
530  }
531  leaf = strtok(0, "/");
532  leaf.trim();
533  if (!ok) {
534  if (inChild) { // if we didn't find a matching child node, default to first child
535  parent();
536  firstChild();
537  }
539  }
540  }
541  if (leaf.size())
543  delete [] buf;
544  unsnappedKeyText = ikey;
545  positionChanged();
546 }
Definition: swbuf.h:47
virtual void root()
Definition: treekeyidx.cpp:131
virtual const char * getLocalName()
Definition: treekeyidx.cpp:91
SWBuf unsnappedKeyText
Definition: treekey.h:47
virtual bool nextSibling()
Definition: treekeyidx.cpp:168
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 bool parent()
Definition: treekeyidx.cpp:148
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
unsigned long size() const
Definition: swbuf.h:185
virtual char popError()
Definition: swkey.cpp:147
void positionChanged()
Definition: treekey.h:51
char error
Definition: swkey.h:106
virtual bool firstChild()
Definition: treekeyidx.cpp:158
void TreeKeyIdx::setUserData ( const char *  userData,
int  size = 0 
)
virtual

Implements TreeKey.

Definition at line 105 of file treekeyidx.cpp.

105  {
106  // this makes sure any unsnapped path exists
107  assureKeyPath();
108  if (currentNode.userData)
109  delete currentNode.userData;
110 
111  if (!size)
112  size = (int)strlen(userData) + 1;
113 
114  currentNode.userData = new char [ size ];
115  memcpy(currentNode.userData, userData, size);
117 }
virtual void assureKeyPath(const char *keyPath=0)
Definition: treekey.cpp:41
int size
Definition: regex.c:5043
class TreeKeyIdx::TreeNode currentNode
SW_u64 userData
Definition: swkey.h:115

Member Data Documentation

bool SWKey::boundSet
mutableprotectedinherited

Definition at line 104 of file swkey.h.

class TreeKeyIdx::TreeNode TreeKeyIdx::currentNode
private
FileDesc* TreeKeyIdx::datfd
private

Definition at line 59 of file treekeyidx.h.

char SWKey::error
mutableprotectedinherited

Definition at line 106 of file swkey.h.

FileDesc* TreeKeyIdx::idxfd
private

Definition at line 58 of file treekeyidx.h.

char* SWKey::keytext
protectedinherited

Definition at line 102 of file swkey.h.

char* SWKey::localeName
protectedinherited

Definition at line 108 of file swkey.h.

const SWClass* SWObject::myClass
protectedinherited

Definition at line 55 of file swobject.h.

char* TreeKeyIdx::path
private

Definition at line 56 of file treekeyidx.h.

bool SWKey::persist
protectedinherited

Definition at line 105 of file swkey.h.

class TreeKey::PositionChangeListener * TreeKey::posChangeListener
inherited
char* SWKey::rangeText
mutableprotectedinherited

Definition at line 103 of file swkey.h.

SWBuf TreeKey::unsnappedKeyText
mutableprotectedinherited

Definition at line 47 of file treekey.h.

SW_u64 SWKey::userData
inherited

Definition at line 115 of file swkey.h.


The documentation for this class was generated from the following files: