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

#include <zstr.h>

+ Inheritance diagram for zStr:
+ Collaboration diagram for zStr:

Public Member Functions

signed char findKeyIndex (const char *ikey, long *idxoff, long away=0) const
 
void getText (long index, char **idxbuf, char **buf) const
 
void linkEntry (const char *destkey, const char *srckey)
 
virtual void rawZFilter (SWBuf &buf, char direction=0) const
 
void setText (const char *ikey, const char *buf, long len=-1)
 
 zStr (const char *ipath, int fileMode=-1, long blockCount=100, SWCompress *icomp=0, bool caseSensitive=false)
 
virtual ~zStr ()
 

Static Public Member Functions

static signed char createModule (const char *path)
 

Protected Member Functions

void flushCache () const
 
void getCompressedText (long block, long entry, char **buf) const
 
void getKeyFromDatOffset (long ioffset, char **buf) const
 
void getKeyFromIdxOffset (long ioffset, char **buf) const
 

Protected Attributes

FileDescdatfd
 
FileDescidxfd
 
FileDesczdtfd
 
FileDesczdxfd
 

Static Protected Attributes

static const int IDXENTRYSIZE = 8
 
static const int ZDXENTRYSIZE = 8
 

Private Attributes

long blockCount
 
EntriesBlockcacheBlock
 
long cacheBlockIndex
 
bool cacheDirty
 
bool caseSensitive
 
SWCompresscompressor
 
long lastoff
 
char * path
 

Static Private Attributes

static int instance = 0
 

Detailed Description

Definition at line 36 of file zstr.h.

Constructor & Destructor Documentation

zStr::zStr ( const char *  ipath,
int  fileMode = -1,
long  blockCount = 100,
SWCompress icomp = 0,
bool  caseSensitive = false 
)

Definition at line 59 of file zstr.cpp.

60 {
61  SWBuf buf;
62 
63  lastoff = -1;
64  path = 0;
65  stdstr(&path, ipath);
66 
67  compressor = (icomp) ? icomp : new SWCompress();
68  this->blockCount = blockCount;
69 
70  if (fileMode == -1) { // try read/write if possible
71  fileMode = FileMgr::RDWR;
72  }
73 
74  buf.setFormatted("%s.idx", path);
75  idxfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
76 
77  buf.setFormatted("%s.dat", path);
78  datfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
79 
80  buf.setFormatted("%s.zdx", path);
81  zdxfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
82 
83  buf.setFormatted("%s.zdt", path);
84  zdtfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
85 
86  if (!zdtfd || zdtfd->getFd() < 0) {
87 // couldn't find datafile but this might be fine if we're
88 // merely instantiating a remote InstallMgr SWMgr
89 SWLOGD("Couldn't open file: %s. errno: %d", buf.c_str(), errno);
90  }
91 
92  cacheBlock = 0;
93  cacheBlockIndex = -1;
94  cacheDirty = false;
95 
96  instance++;
97 }
FileDesc * datfd
Definition: zstr.h:51
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
SWCompress * compressor
Definition: zstr.h:47
static unsigned int RDWR
Definition: filemgr.h:76
FileDesc * idxfd
Definition: zstr.h:50
int getFd()
Definition: filemgr.h:231
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
long cacheBlockIndex
Definition: zstr.h:41
const char * c_str() const
Definition: swbuf.h:158
static int instance
Definition: zstr.h:39
bool cacheDirty
Definition: zstr.h:42
char * path
Definition: zstr.h:43
FileDesc * zdxfd
Definition: zstr.h:52
long lastoff
Definition: zstr.h:45
long blockCount
Definition: zstr.h:46
FileDesc * zdtfd
Definition: zstr.h:53
#define SWLOGD(...)
Definition: defs.h:187
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
EntriesBlock * cacheBlock
Definition: zstr.h:40
bool caseSensitive
Definition: zstr.h:44
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
zStr::~zStr ( )
virtual

Definition at line 104 of file zstr.cpp.

104  {
105 
106  flushCache();
107 
108  if (path)
109  delete [] path;
110 
111  --instance;
112 
117 
118 
119  if (compressor)
120  delete compressor;
121 
122 }
FileDesc * datfd
Definition: zstr.h:51
SWCompress * compressor
Definition: zstr.h:47
FileDesc * idxfd
Definition: zstr.h:50
void flushCache() const
Definition: zstr.cpp:590
void close(FileDesc *file)
Definition: filemgr.cpp:196
static int instance
Definition: zstr.h:39
char * path
Definition: zstr.h:43
FileDesc * zdxfd
Definition: zstr.h:52
FileDesc * zdtfd
Definition: zstr.h:53
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101

Member Function Documentation

signed char zStr::createModule ( const char *  path)
static

Definition at line 663 of file zstr.cpp.

663  {
664  char *path = 0;
665  char *buf = new char [ strlen (ipath) + 20 ];
666  FileDesc *fd, *fd2;
667 
668  stdstr(&path, ipath);
669 
670  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
671  path[strlen(path)-1] = 0;
672 
673  sprintf(buf, "%s.dat", path);
674  FileMgr::removeFile(buf);
676  fd->getFd();
678 
679  sprintf(buf, "%s.idx", path);
680  FileMgr::removeFile(buf);
682  fd2->getFd();
684 
685  sprintf(buf, "%s.zdt", path);
686  FileMgr::removeFile(buf);
688  fd2->getFd();
690 
691  sprintf(buf, "%s.zdx", path);
692  FileMgr::removeFile(buf);
694  fd2->getFd();
696 
697  delete [] path;
698 
699  return 0;
700 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
int getFd()
Definition: filemgr.h:231
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
void close(FileDesc *file)
Definition: filemgr.cpp:196
static int removeFile(const char *fName)
Definition: filemgr.cpp:517
char * path
Definition: zstr.h:43
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
signed char zStr::findKeyIndex ( const char *  ikey,
long *  idxoff,
long  away = 0 
) const

Definition at line 193 of file zstr.cpp.

194 {
195  char *maxbuf = 0, *trybuf = 0, *key = 0, quitflag = 0;
196  signed char retval = 0;
197  SW_s32 headoff, tailoff, tryoff = 0, maxoff = 0;
198  SW_u32 start, size;
199  int diff = 0;
200  bool awayFromSubstrCheck = false;
201 
202  if (idxfd->getFd() >= 0) {
203  tailoff = maxoff = (SW_s32)idxfd->seek(0, SEEK_END) - IDXENTRYSIZE;
204  if (*ikey) {
205  headoff = 0;
206  stdstr(&key, ikey, 3);
207  if (!caseSensitive) toupperstr_utf8(key, (unsigned int)(strlen(key)*3));
208 
209  int keylen = (int)strlen(key);
210  bool substr = false;
211 
212  getKeyFromIdxOffset(maxoff, &maxbuf);
213 
214  while (headoff < tailoff) {
215  tryoff = ((SW_s32)lastoff == -1) ? headoff + (((((tailoff / IDXENTRYSIZE) - (headoff / IDXENTRYSIZE))) / 2) * IDXENTRYSIZE) : (SW_s32)lastoff;
216  lastoff = -1;
217 
218  getKeyFromIdxOffset(tryoff, &trybuf);
219 
220  if (!*trybuf && tryoff) { // In case of extra entry at end of idx (not first entry)
221  tryoff += (tryoff > (maxoff / 2))?-IDXENTRYSIZE:IDXENTRYSIZE;
222  retval = -1;
223  break;
224  }
225 
226  diff = strcmp(key, trybuf);
227 
228  if (!diff)
229  break;
230 
231  if (!strncmp(trybuf, key, keylen)) substr = true;
232 
233  if (diff < 0)
234  tailoff = (tryoff == headoff) ? headoff : tryoff;
235  else headoff = tryoff;
236 
237  if (tailoff == headoff + IDXENTRYSIZE) {
238  if (quitflag++)
239  headoff = tailoff;
240  }
241  }
242 
243  // didn't find exact match
244  if (headoff >= tailoff) {
245  tryoff = headoff;
246  if (!substr && ((tryoff != maxoff)||(strncmp(key, maxbuf, keylen)<0))) {
247  awayFromSubstrCheck = true;
248  away--; // if our entry doesn't startwith our key, prefer the previous entry over the next
249  }
250  }
251  if (trybuf)
252  free(trybuf);
253  delete [] key;
254  if (maxbuf)
255  free(maxbuf);
256  }
257  else { tryoff = 0; }
258 
259  idxfd->seek(tryoff, SEEK_SET);
260 
261  start = size = 0;
262  retval = (idxfd->read(&start, 4) == 4) ? retval : -1;
263  retval = (idxfd->read(&size, 4) == 4) ? retval : -1;
264  start = swordtoarch32(start);
265  size = swordtoarch32(size);
266 
267  if (idxoff)
268  *idxoff = tryoff;
269 
270  while (away) {
271  SW_u32 laststart = start;
272  SW_u32 lastsize = size;
273  SW_s32 lasttry = tryoff;
274  tryoff += (away > 0) ? IDXENTRYSIZE : -IDXENTRYSIZE;
275 
276  bool bad = false;
277  if (((long)(tryoff + (away*IDXENTRYSIZE)) < -IDXENTRYSIZE) || (tryoff + (away*IDXENTRYSIZE) > (maxoff+IDXENTRYSIZE)))
278  bad = true;
279  else if (idxfd->seek(tryoff, SEEK_SET) < 0)
280  bad = true;
281  if (bad) {
282  if(!awayFromSubstrCheck)
283  retval = -1;
284  start = laststart;
285  size = lastsize;
286  tryoff = lasttry;
287  if (idxoff)
288  *idxoff = tryoff;
289  break;
290  }
291  idxfd->read(&start, 4);
292  idxfd->read(&size, 4);
293  start = swordtoarch32(start);
294  size = swordtoarch32(size);
295 
296  if (idxoff)
297  *idxoff = tryoff;
298 
299 
300  if (((laststart != start) || (lastsize != size)) && size)
301  away += (away < 0) ? 1 : -1;
302  }
303 
304  lastoff = tryoff;
305  }
306  else {
307  if (idxoff)
308  *idxoff = 0;
309  retval = -1;
310  }
311  return retval;
312 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
void getKeyFromIdxOffset(long ioffset, char **buf) const
Definition: zstr.cpp:168
#define SEEK_END
Definition: zconf.h:246
signed int SW_s32
Definition: sysdata.h:40
FileDesc * idxfd
Definition: zstr.h:50
int getFd()
Definition: filemgr.h:231
char * toupperstr_utf8(char *t, unsigned int max=0)
Definition: stringmgr.h:118
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
free(preg->fastmap)
#define swordtoarch32(x)
Definition: sysdata.h:94
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
static const int IDXENTRYSIZE
Definition: zstr.h:54
long lastoff
Definition: zstr.h:45
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
bool caseSensitive
Definition: zstr.h:44
void zStr::flushCache ( ) const
protected

Definition at line 590 of file zstr.cpp.

590  {
591 
592  static const char nl[] = {13, 10};
593 
594  if (cacheBlock) {
595  if (cacheDirty) {
596  SW_u32 start = 0;
597  unsigned long size = 0;
598  SW_u32 outstart = 0, outsize = 0;
599 
600  const char *rawBuf = cacheBlock->getRawData(&size);
601  compressor->setUncompressedBuf(rawBuf, &size);
603 
604  SWBuf buf;
605  buf.setSize(size + 5);
606  memcpy(buf.getRawData(), compressor->getCompressedBuf(&size), size); // 1 = encipher
607  buf.setSize(size);
608  rawZFilter(buf, 1); // 1 = encipher
609 
610  long zdxSize = zdxfd->seek(0, SEEK_END);
611  unsigned long zdtSize = zdtfd->seek(0, SEEK_END);
612 
613  if ((cacheBlockIndex * ZDXENTRYSIZE) > (zdxSize - ZDXENTRYSIZE)) { // New Block
614  start = (SW_u32)zdtSize;
615  }
616  else {
617  zdxfd->seek(cacheBlockIndex * ZDXENTRYSIZE, SEEK_SET);
618  zdxfd->read(&start, 4);
619  zdxfd->read(&outsize, 4);
620  start = swordtoarch32(start);
621  outsize = swordtoarch32(outsize);
622  if (start + outsize >= zdtSize) { // last entry, just overwrite
623  // start is already set
624  }
625  else if (size < outsize) { // middle entry, but smaller, that's fine and let's preserve bigger size
626  size = outsize;
627  }
628  else { // middle and bigger-- we have serious problems, for now let's put it at the end = lots of wasted space
629  start = (SW_u32)zdtSize;
630  }
631  }
632 
633 
634 
635  outstart = archtosword32(start);
636  outsize = archtosword32((SW_u32)size);
637 
638  zdxfd->seek(cacheBlockIndex * ZDXENTRYSIZE, SEEK_SET);
639  zdtfd->seek(start, SEEK_SET);
640  zdtfd->write(buf, size);
641 
642  // add a new line to make data file easier to read in an editor
643  zdtfd->write(&nl, 2);
644 
645  zdxfd->write(&outstart, 4);
646  zdxfd->write(&outsize, 4);
647  }
648  delete cacheBlock;
649  cacheBlock = 0;
650  }
651  cacheBlockIndex = -1;
652  cacheDirty = false;
653 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
Definition: swbuf.h:47
#define archtosword32(x)
Definition: sysdata.h:97
SWCompress * compressor
Definition: zstr.h:47
#define SEEK_END
Definition: zconf.h:246
virtual char * getCompressedBuf(unsigned long *len=0)
Definition: swcomprs.cpp:111
long write(const void *buf, long count)
Definition: filemgr.cpp:153
virtual void setUncompressedBuf(const char *buf=0, unsigned long *len=0)
Definition: swcomprs.cpp:75
char * getRawData()
Definition: swbuf.h:379
long cacheBlockIndex
Definition: zstr.h:41
const char * getRawData(unsigned long *size)
Definition: entriesblk.cpp:99
#define swordtoarch32(x)
Definition: sysdata.h:94
bool cacheDirty
Definition: zstr.h:42
static const int ZDXENTRYSIZE
Definition: zstr.h:55
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
FileDesc * zdxfd
Definition: zstr.h:52
virtual void rawZFilter(SWBuf &buf, char direction=0) const
Definition: zstr.h:69
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
FileDesc * zdtfd
Definition: zstr.h:53
EntriesBlock * cacheBlock
Definition: zstr.h:40
void setSize(unsigned long len)
Definition: swbuf.h:255
void zStr::getCompressedText ( long  block,
long  entry,
char **  buf 
) const
protected

Definition at line 392 of file zstr.cpp.

392  {
393 
394  SW_u32 size = 0;
395 
396  if (cacheBlockIndex != block) {
397  SW_u32 start = 0;
398 
399  zdxfd->seek(block * ZDXENTRYSIZE, SEEK_SET);
400  zdxfd->read(&start, 4);
401  zdxfd->read(&size, 4);
402  start = swordtoarch32(start);
403  size = swordtoarch32(size);
404 
405  SWBuf buf;
406  buf.setSize(size + 5);
407  zdtfd->seek(start, SEEK_SET);
408  zdtfd->read(buf.getRawData(), size);
409 
410  flushCache();
411 
412  unsigned long len = size;
413  buf.setSize(size);
414  rawZFilter(buf, 0); // 0 = decipher
415 
416  compressor->setCompressedBuf(&len, buf.getRawData());
417  char *rawBuf = compressor->getUncompressedBuf(&len);
418  cacheBlock = new EntriesBlock(rawBuf, len);
419  cacheBlockIndex = block;
420  }
421  size = (SW_u32)cacheBlock->getEntrySize(entry);
422  *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
423  strcpy(*buf, cacheBlock->getEntry(entry));
424 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
Definition: swbuf.h:47
virtual void setCompressedBuf(unsigned long *len, char *buf=0)
Definition: swcomprs.cpp:101
SWCompress * compressor
Definition: zstr.h:47
void flushCache() const
Definition: zstr.cpp:590
const char * getEntry(int entryIndex)
Definition: entriesblk.cpp:147
virtual char * getUncompressedBuf(unsigned long *len=0)
Definition: swcomprs.cpp:90
char * malloc()
char * getRawData()
Definition: swbuf.h:379
long cacheBlockIndex
Definition: zstr.h:41
char * realloc()
#define swordtoarch32(x)
Definition: sysdata.h:94
static const int ZDXENTRYSIZE
Definition: zstr.h:55
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
FileDesc * zdxfd
Definition: zstr.h:52
virtual void rawZFilter(SWBuf &buf, char direction=0) const
Definition: zstr.h:69
unsigned int SW_u32
Definition: sysdata.h:41
unsigned long getEntrySize(int entryIndex)
Definition: entriesblk.cpp:157
long read(void *buf, long count)
Definition: filemgr.cpp:148
FileDesc * zdtfd
Definition: zstr.h:53
EntriesBlock * cacheBlock
Definition: zstr.h:40
void setSize(unsigned long len)
Definition: swbuf.h:255
void zStr::getKeyFromDatOffset ( long  ioffset,
char **  buf 
) const
protected

Definition at line 134 of file zstr.cpp.

135 {
136  int size;
137  char ch;
138  if (datfd && datfd->getFd() >= 0) {
139  datfd->seek(ioffset, SEEK_SET);
140  for (size = 0; datfd->read(&ch, 1) == 1; size++) {
141  if ((ch == '\\') || (ch == 10) || (ch == 13))
142  break;
143  }
144  *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
145  if (size) {
146  datfd->seek(ioffset, SEEK_SET);
147  datfd->read(*buf, size);
148  }
149  (*buf)[size] = 0;
150  if (!caseSensitive) toupperstr_utf8(*buf, size*2);
151  }
152  else {
153  *buf = (*buf) ? (char *)realloc(*buf, 1) : (char *)malloc(1);
154  **buf = 0;
155  }
156 }
FileDesc * datfd
Definition: zstr.h:51
long seek(long offset, int whence)
Definition: filemgr.cpp:143
int getFd()
Definition: filemgr.h:231
char * toupperstr_utf8(char *t, unsigned int max=0)
Definition: stringmgr.h:118
char * malloc()
char * realloc()
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
long read(void *buf, long count)
Definition: filemgr.cpp:148
bool caseSensitive
Definition: zstr.h:44
void zStr::getKeyFromIdxOffset ( long  ioffset,
char **  buf 
) const
protected

Definition at line 168 of file zstr.cpp.

169 {
170  SW_u32 offset;
171 
172  if (idxfd && idxfd->getFd() >= 0) {
173  idxfd->seek(ioffset, SEEK_SET);
174  idxfd->read(&offset, 4);
175  offset = swordtoarch32(offset);
176  getKeyFromDatOffset(offset, buf);
177  }
178 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
FileDesc * idxfd
Definition: zstr.h:50
int getFd()
Definition: filemgr.h:231
#define swordtoarch32(x)
Definition: sysdata.h:94
void getKeyFromDatOffset(long ioffset, char **buf) const
Definition: zstr.cpp:134
#define SEEK_SET
Definition: zconf.h:244
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
void zStr::getText ( long  index,
char **  idxbuf,
char **  buf 
) const

Definition at line 327 of file zstr.cpp.

327  {
328  char *ch;
329  char *idxbuflocal = 0;
330  getKeyFromIdxOffset(offset, &idxbuflocal);
331  SW_u32 start;
332  SW_u32 size;
333 
334  do {
335  idxfd->seek(offset, SEEK_SET);
336  idxfd->read(&start, 4);
337  idxfd->read(&size, 4);
338  start = swordtoarch32(start);
339  size = swordtoarch32(size);
340 
341  *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
342  *idxbuf = (*idxbuf) ? (char *)realloc(*idxbuf, size*2 + 1) : (char *)malloc(size*2 + 1);
343  memset(*buf, 0, size + 1);
344  memset(*idxbuf, 0, size + 1);
345  datfd->seek(start, SEEK_SET);
346  datfd->read(*buf, (int)(size));
347 
348  for (ch = *buf; *ch; ch++) { // skip over index string
349  if (*ch == 10) {
350  ch++;
351  break;
352  }
353  }
354  memmove(*buf, ch, size - (unsigned long)(ch-*buf));
355 
356  // resolve link
357  if (!strncmp(*buf, "@LINK", 5)) {
358  for (ch = *buf; *ch; ch++) { // null before nl
359  if (*ch == 10) {
360  *ch = 0;
361  break;
362  }
363  }
364  findKeyIndex(*buf + 6, &offset);
365  }
366  else break;
367  }
368  while (true); // while we're resolving links
369 
370  if (idxbuflocal) {
371  SW_u32 localsize = (SW_u32)strlen(idxbuflocal);
372  localsize = (localsize < (size - 1)) ? localsize : (size - 1);
373  strncpy(*idxbuf, idxbuflocal, localsize);
374  (*idxbuf)[localsize] = 0;
375  free(idxbuflocal);
376  }
377  SW_u32 block = 0;
378  SW_u32 entry = 0;
379  memmove(&block, *buf, sizeof(SW_u32));
380  memmove(&entry, *buf + sizeof(SW_u32), sizeof(SW_u32));
381  block = swordtoarch32(block);
382  entry = swordtoarch32(entry);
383  getCompressedText(block, entry, buf);
384 }
FileDesc * datfd
Definition: zstr.h:51
long seek(long offset, int whence)
Definition: filemgr.cpp:143
void getKeyFromIdxOffset(long ioffset, char **buf) const
Definition: zstr.cpp:168
FileDesc * idxfd
Definition: zstr.h:50
signed char findKeyIndex(const char *ikey, long *idxoff, long away=0) const
Definition: zstr.cpp:193
char * malloc()
void getCompressedText(long block, long entry, char **buf) const
Definition: zstr.cpp:392
free(preg->fastmap)
char * realloc()
#define swordtoarch32(x)
Definition: sysdata.h:94
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
void zStr::linkEntry ( const char *  destkey,
const char *  srckey 
)

Definition at line 582 of file zstr.cpp.

582  {
583  char *text = new char [ strlen(destkey) + 7 ];
584  sprintf(text, "@LINK %s", destkey);
585  setText(srckey, text);
586  delete [] text;
587 }
void setText(const char *ikey, const char *buf, long len=-1)
Definition: zstr.cpp:435
virtual void zStr::rawZFilter ( SWBuf buf,
char  direction = 0 
) const
inlinevirtual

Reimplemented in zLD.

Definition at line 69 of file zstr.h.

69 { (void) buf; (void) direction; }
void zStr::setText ( const char *  ikey,
const char *  buf,
long  len = -1 
)

Definition at line 435 of file zstr.cpp.

435  {
436 
437  static const char nl[] = {13, 10};
438 
439  SW_u32 start, outstart;
440  SW_u32 size, outsize;
441  SW_s32 endoff;
442  long idxoff = 0;
443  SW_s32 shiftSize;
444  char *tmpbuf = 0;
445  char *key = 0;
446  char *dbKey = 0;
447  char *idxBytes = 0;
448  char *outbuf = 0;
449  char *ch = 0;
450 
451  len = (len < 0) ? strlen(buf) : len;
452  stdstr(&key, ikey, 3);
453  if (!caseSensitive) toupperstr_utf8(key, (unsigned int)(strlen(key)*3));
454 
455  char notFound = findKeyIndex(ikey, &idxoff, 0);
456  if (!notFound) {
457  getKeyFromIdxOffset(idxoff, &dbKey);
458  int diff = strcmp(key, dbKey);
459  if (diff < 0) {
460  }
461  else if (diff > 0) {
462  idxoff += IDXENTRYSIZE;
463  }
464  else if ((!diff) && (len > 0 /*we're not deleting*/)) { // got absolute entry
465  do {
466  idxfd->seek(idxoff, SEEK_SET);
467  idxfd->read(&start, 4);
468  idxfd->read(&size, 4);
469  start = swordtoarch32(start);
470  size = swordtoarch32(size);
471 
472  tmpbuf = new char [ size + 2 ];
473  memset(tmpbuf, 0, size + 2);
474  datfd->seek(start, SEEK_SET);
475  datfd->read(tmpbuf, size);
476 
477  for (ch = tmpbuf; *ch; ch++) { // skip over index string
478  if (*ch == 10) {
479  ch++;
480  break;
481  }
482  }
483  memmove(tmpbuf, ch, size - (unsigned long)(ch-tmpbuf));
484 
485  // resolve link
486  if (!strncmp(tmpbuf, "@LINK", 5) && (len)) {
487  for (ch = tmpbuf; *ch; ch++) { // null before nl
488  if (*ch == 10) {
489  *ch = 0;
490  break;
491  }
492  }
493  findKeyIndex(tmpbuf + IDXENTRYSIZE, &idxoff);
494  delete [] tmpbuf;
495  }
496  else break;
497  }
498  while (true); // while we're resolving links
499  }
500  }
501 
502  endoff = (SW_s32)idxfd->seek(0, SEEK_END);
503 
504  shiftSize = endoff - (SW_s32)idxoff;
505 
506  if (shiftSize > 0) {
507  idxBytes = new char [ shiftSize ];
508  idxfd->seek(idxoff, SEEK_SET);
509  idxfd->read(idxBytes, shiftSize);
510  }
511 
512  outbuf = new char [ len + strlen(key) + 5 ];
513  sprintf(outbuf, "%s%c%c", key, 13, 10);
514  size = (SW_u32)strlen(outbuf);
515  if (len > 0) { // NOT a link
516  if (!cacheBlock) {
517  flushCache();
518  cacheBlock = new EntriesBlock();
520  }
521  else if (cacheBlock->getCount() >= blockCount) {
522  flushCache();
523  cacheBlock = new EntriesBlock();
525  }
526  SW_u32 entry = cacheBlock->addEntry(buf);
527  cacheDirty = true;
528  outstart = (SW_u32)archtosword32(cacheBlockIndex);
529  outsize = archtosword32(entry);
530  memcpy (outbuf + size, &outstart, sizeof(SW_u32));
531  memcpy (outbuf + size + sizeof(SW_u32), &outsize, sizeof(SW_u32));
532  size += (sizeof(SW_u32) * 2);
533  }
534  else { // link
535  memcpy(outbuf + size, buf, len);
536  size += len;
537  }
538 
539  start = (SW_u32)datfd->seek(0, SEEK_END);
540 
541  outstart = archtosword32(start);
542  outsize = archtosword32(size);
543 
544  idxfd->seek(idxoff, SEEK_SET);
545  if (len > 0) {
546  datfd->seek(start, SEEK_SET);
547  datfd->write(outbuf, size);
548 
549  // add a new line to make data file easier to read in an editor
550  datfd->write(&nl, 2);
551 
552  idxfd->write(&outstart, 4);
553  idxfd->write(&outsize, 4);
554  if (idxBytes) {
555  idxfd->write(idxBytes, shiftSize);
556  }
557  }
558  else { // delete entry
559  if (idxBytes) {
560  idxfd->write(idxBytes+IDXENTRYSIZE, shiftSize-IDXENTRYSIZE);
561  idxfd->seek(-1, SEEK_CUR); // last valid byte
562  FileMgr::getSystemFileMgr()->trunc(idxfd); // truncate index
563  }
564  }
565 
566  if (idxBytes)
567  delete [] idxBytes;
568  delete [] key;
569  delete [] outbuf;
570  free(dbKey);
571 }
FileDesc * datfd
Definition: zstr.h:51
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define SEEK_CUR
Definition: zconf.h:245
#define archtosword32(x)
Definition: sysdata.h:97
void getKeyFromIdxOffset(long ioffset, char **buf) const
Definition: zstr.cpp:168
#define SEEK_END
Definition: zconf.h:246
signed int SW_s32
Definition: sysdata.h:40
FileDesc * idxfd
Definition: zstr.h:50
void flushCache() const
Definition: zstr.cpp:590
int addEntry(const char *entry)
Definition: entriesblk.cpp:113
long write(const void *buf, long count)
Definition: filemgr.cpp:153
signed char findKeyIndex(const char *ikey, long *idxoff, long away=0) const
Definition: zstr.cpp:193
signed char trunc(FileDesc *file)
Definition: filemgr.cpp:256
char * toupperstr_utf8(char *t, unsigned int max=0)
Definition: stringmgr.h:118
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
long cacheBlockIndex
Definition: zstr.h:41
free(preg->fastmap)
#define swordtoarch32(x)
Definition: sysdata.h:94
bool cacheDirty
Definition: zstr.h:42
static const int ZDXENTRYSIZE
Definition: zstr.h:55
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
static const int IDXENTRYSIZE
Definition: zstr.h:54
FileDesc * zdxfd
Definition: zstr.h:52
long blockCount
Definition: zstr.h:46
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
EntriesBlock * cacheBlock
Definition: zstr.h:40
bool caseSensitive
Definition: zstr.h:44
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
int getCount()
Definition: entriesblk.cpp:61

Member Data Documentation

long zStr::blockCount
private

Definition at line 46 of file zstr.h.

EntriesBlock* zStr::cacheBlock
mutableprivate

Definition at line 40 of file zstr.h.

long zStr::cacheBlockIndex
mutableprivate

Definition at line 41 of file zstr.h.

bool zStr::cacheDirty
mutableprivate

Definition at line 42 of file zstr.h.

bool zStr::caseSensitive
private

Definition at line 44 of file zstr.h.

SWCompress* zStr::compressor
private

Definition at line 47 of file zstr.h.

FileDesc* zStr::datfd
protected

Definition at line 51 of file zstr.h.

const int zStr::IDXENTRYSIZE = 8
staticprotected

Definition at line 54 of file zstr.h.

FileDesc* zStr::idxfd
protected

Definition at line 50 of file zstr.h.

SWORD_NAMESPACE_START int zStr::instance = 0
staticprivate

Definition at line 39 of file zstr.h.

long zStr::lastoff
mutableprivate

Definition at line 45 of file zstr.h.

char* zStr::path
private

Definition at line 43 of file zstr.h.

FileDesc* zStr::zdtfd
protected

Definition at line 53 of file zstr.h.

const int zStr::ZDXENTRYSIZE = 8
staticprotected

Definition at line 55 of file zstr.h.

FileDesc* zStr::zdxfd
protected

Definition at line 52 of file zstr.h.


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