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

#include <rawstr4.h>

+ Inheritance diagram for RawStr4:
+ Collaboration diagram for RawStr4:

Public Member Functions

signed char findOffset (const char *key, SW_u32 *start, SW_u32 *size, long away=0, SW_u32 *idxoff=0) const
 
void getIDXBuf (long ioffset, char **buf) const
 
void getIDXBufDat (long ioffset, char **buf) const
 
 RawStr4 (const char *ipath, int fileMode=-1, bool caseSensitive=false)
 
void readText (SW_u32 start, SW_u32 *size, char **idxbuf, SWBuf &buf) const
 
virtual ~RawStr4 ()
 

Static Public Member Functions

static signed char createModule (const char *path)
 

Static Public Attributes

static const char nl = '\n'
 

Protected Member Functions

void doLinkEntry (const char *destkey, const char *srckey)
 
void doSetText (const char *key, const char *buf, long len=-1)
 

Protected Attributes

FileDescdatfd
 
FileDescidxfd
 

Static Protected Attributes

static const int IDXENTRYSIZE = 8
 

Private Attributes

bool caseSensitive
 
long lastoff
 
char * path
 

Static Private Attributes

static int instance = 0
 

Detailed Description

Definition at line 35 of file rawstr4.h.

Constructor & Destructor Documentation

RawStr4::RawStr4 ( const char *  ipath,
int  fileMode = -1,
bool  caseSensitive = false 
)

Definition at line 59 of file rawstr4.cpp.

60 {
61  SWBuf buf;
62 
63  lastoff = -1;
64  path = 0;
65  stdstr(&path, ipath);
66 
67  if (fileMode == -1) { // try read/write if possible
68  fileMode = FileMgr::RDWR;
69  }
70 
71  buf.setFormatted("%s.idx", path);
72  idxfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
73 
74  buf.setFormatted("%s.dat", path);
75  datfd = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
76 
77  if (!datfd || datfd->getFd() < 0) {
78 // couldn't find datafile but this might be fine if we're
79 // merely instantiating a remote InstallMgr SWMgr
80 SWLOGD("Couldn't open file: %s. errno: %d", buf.c_str(), errno);
81  }
82 
83  instance++;
84 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
static unsigned int RDWR
Definition: filemgr.h:76
bool caseSensitive
Definition: rawstr4.h:40
int getFd()
Definition: filemgr.h:231
char * path
Definition: rawstr4.h:39
long lastoff
Definition: rawstr4.h:41
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
FileDesc * datfd
Definition: rawstr4.h:47
FileDesc * idxfd
Definition: rawstr4.h:46
static int instance
Definition: rawstr4.h:38
#define SWLOGD(...)
Definition: defs.h:187
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
RawStr4::~RawStr4 ( )
virtual

Definition at line 91 of file rawstr4.cpp.

92 {
93  if (path)
94  delete [] path;
95 
96  --instance;
97 
100 }
char * path
Definition: rawstr4.h:39
void close(FileDesc *file)
Definition: filemgr.cpp:196
FileDesc * datfd
Definition: rawstr4.h:47
FileDesc * idxfd
Definition: rawstr4.h:46
static int instance
Definition: rawstr4.h:38
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101

Member Function Documentation

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

Definition at line 511 of file rawstr4.cpp.

512 {
513  char *path = 0;
514  char *buf = new char [ strlen (ipath) + 20 ];
515  FileDesc *fd, *fd2;
516 
517  stdstr(&path, ipath);
518 
519  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
520  path[strlen(path)-1] = 0;
521 
522  sprintf(buf, "%s.dat", path);
523  FileMgr::removeFile(buf);
525  fd->getFd();
527 
528  sprintf(buf, "%s.idx", path);
529  FileMgr::removeFile(buf);
531  fd2->getFd();
533 
534  delete [] path;
535 
536  return 0;
537 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
int getFd()
Definition: filemgr.h:231
char * path
Definition: rawstr4.h:39
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
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 RawStr4::doLinkEntry ( const char *  destkey,
const char *  srckey 
)
protected

Definition at line 496 of file rawstr4.cpp.

496  {
497  char *text = new char [ strlen(destkey) + 7 ];
498  sprintf(text, "@LINK %s", destkey);
499  doSetText(srckey, text);
500  delete [] text;
501 }
void doSetText(const char *key, const char *buf, long len=-1)
Definition: rawstr4.cpp:377
void RawStr4::doSetText ( const char *  key,
const char *  buf,
long  len = -1 
)
protected

Definition at line 377 of file rawstr4.cpp.

377  {
378 
379  SW_u32 start, outstart;
380  SW_u32 idxoff;
381  SW_u32 endoff;
382  SW_s32 shiftSize;
383  SW_u32 size;
384  SW_u32 outsize;
385  char *tmpbuf = 0;
386  char *key = 0;
387  char *dbKey = 0;
388  char *idxBytes = 0;
389  char *outbuf = 0;
390  char *ch = 0;
391 
392  char errorStatus = findOffset(ikey, &start, &size, 0, &idxoff);
393  stdstr(&key, ikey, 3);
394  if (!caseSensitive) toupperstr_utf8(key, (unsigned int)(strlen(key)*3));
395 
396  len = (len < 0) ? strlen(buf) : len;
397  getIDXBufDat(start, &dbKey);
398 
399  if (strcmp(key, dbKey) < 0) {
400  }
401  else if (strcmp(key, dbKey) > 0) {
402  if (errorStatus != (char)-2) // not a new file
403  idxoff += 8;
404  else idxoff = 0;
405  }
406  else if ((!strcmp(key, dbKey)) && (len>0/*we're not deleting*/)) { // got absolute entry
407  do {
408  tmpbuf = new char [ size + 2 ];
409  memset(tmpbuf, 0, size + 2);
410  datfd->seek(start, SEEK_SET);
411  datfd->read(tmpbuf, (int)(size - 1));
412 
413  for (ch = tmpbuf; *ch; ch++) { // skip over index string
414  if (*ch == 10) {
415  ch++;
416  break;
417  }
418  }
419  memmove(tmpbuf, ch, size - (unsigned long)(ch-tmpbuf));
420 
421  // resolve link
422  if (!strncmp(tmpbuf, "@LINK", 5) && (len > 0)) {
423  for (ch = tmpbuf; *ch; ch++) { // null before nl
424  if (*ch == 10) {
425  *ch = 0;
426  break;
427  }
428  }
429  findOffset(tmpbuf + 8, &start, &size, 0, &idxoff);
430  ++size;
431  }
432  else break;
433  }
434  while (true); // while we're resolving links
435  }
436 
437  endoff = (SW_u32)idxfd->seek(0, SEEK_END);
438 
439  shiftSize = endoff - idxoff;
440 
441  if (shiftSize > 0) {
442  idxBytes = new char [ shiftSize ];
443  idxfd->seek(idxoff, SEEK_SET);
444  idxfd->read(idxBytes, shiftSize);
445  }
446 
447  outbuf = new char [ len + strlen(key) + 5 ];
448  sprintf(outbuf, "%s%c%c", key, 13, 10);
449  size = strlen(outbuf);
450  memcpy(outbuf + size, buf, len);
451  size = outsize = size + (SW_u32)len;
452 
453  start = outstart = (SW_u32)datfd->seek(0, SEEK_END);
454 
455  outstart = archtosword32(start);
456  outsize = archtosword32(size);
457 
458  idxfd->seek(idxoff, SEEK_SET);
459  if (len>0) {
460  datfd->seek(start, SEEK_SET);
461  datfd->write(outbuf, (long)size);
462 
463  // add a new line to make data file easier to read in an editor
464  datfd->write(&nl, 1);
465 
466  idxfd->write(&outstart, 4);
467  idxfd->write(&outsize, 4);
468  if (idxBytes) {
469  idxfd->write(idxBytes, shiftSize);
470  delete [] idxBytes;
471  }
472  }
473  else { // delete entry
474  if (idxBytes) {
475  idxfd->write(idxBytes+8, shiftSize-8);
476  idxfd->seek(-1, SEEK_CUR); // last valid byte
477  FileMgr::getSystemFileMgr()->trunc(idxfd); // truncate index
478  delete [] idxBytes;
479  }
480  }
481 
482  delete [] key;
483  delete [] outbuf;
484  free(dbKey);
485 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define SEEK_CUR
Definition: zconf.h:245
#define archtosword32(x)
Definition: sysdata.h:97
#define SEEK_END
Definition: zconf.h:246
static const char nl
Definition: rawstr4.h:52
signed int SW_s32
Definition: sysdata.h:40
signed char findOffset(const char *key, SW_u32 *start, SW_u32 *size, long away=0, SW_u32 *idxoff=0) const
Definition: rawstr4.cpp:182
bool caseSensitive
Definition: rawstr4.h:40
long write(const void *buf, long count)
Definition: filemgr.cpp:153
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
free(preg->fastmap)
FileDesc * datfd
Definition: rawstr4.h:47
FileDesc * idxfd
Definition: rawstr4.h:46
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
void getIDXBufDat(long ioffset, char **buf) const
Definition: rawstr4.cpp:112
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
signed char RawStr4::findOffset ( const char *  key,
SW_u32 start,
SW_u32 size,
long  away = 0,
SW_u32 idxoff = 0 
) const

Definition at line 182 of file rawstr4.cpp.

183 {
184  char *trybuf, *maxbuf, *key = 0, quitflag = 0;
185  signed char retval = -1;
186  long headoff, tailoff, tryoff = 0, maxoff = 0;
187  int diff = 0;
188  bool awayFromSubstrCheck = false;
189 
190  if (idxfd->getFd() >=0) {
191  tailoff = maxoff = idxfd->seek(0, SEEK_END) - 8;
192 
193  retval = (tailoff >= 0) ? 0 : -2; // if NOT new file
194  if (*ikey && retval != -2) {
195  headoff = 0;
196 
197  stdstr(&key, ikey, 3);
198  if (!caseSensitive) toupperstr_utf8(key, (unsigned int)(strlen(key)*3));
199 
200  int keylen = (int)strlen(key);
201  bool substr = false;
202 
203  trybuf = maxbuf = 0;
204  getIDXBuf(maxoff, &maxbuf);
205 
206  while (headoff < tailoff) {
207  tryoff = (lastoff == -1) ? headoff + ((((tailoff / 8) - (headoff / 8))) / 2) * 8 : lastoff;
208  lastoff = -1;
209  getIDXBuf(tryoff, &trybuf);
210 
211  if (!*trybuf && tryoff) { // In case of extra entry at end of idx (not first entry)
212  tryoff += (tryoff > (maxoff / 2))?-8:8;
213  retval = -1;
214  break;
215  }
216 
217  diff = strcmp(key, trybuf);
218 
219  if (!diff)
220  break;
221 
222  if (!strncmp(trybuf, key, keylen)) substr = true;
223 
224  if (diff < 0)
225  tailoff = (tryoff == headoff) ? headoff : tryoff;
226  else headoff = tryoff;
227 
228  if (tailoff == headoff + 8) {
229  if (quitflag++)
230  headoff = tailoff;
231  }
232  }
233 
234  // didn't find exact match
235  if (headoff >= tailoff) {
236  tryoff = headoff;
237  if (!substr && ((tryoff != maxoff)||(strncmp(key, maxbuf, keylen)<0))) {
238  awayFromSubstrCheck = true;
239  away--; // if our entry doesn't startwith our key, prefer the previous entry over the next
240  }
241  }
242  if (trybuf)
243  free(trybuf);
244  delete [] key;
245  if (maxbuf)
246  free(maxbuf);
247  }
248  else tryoff = 0;
249 
250  idxfd->seek(tryoff, SEEK_SET);
251 
252  SW_u32 tmpStart, tmpSize;
253  *start = *size = tmpStart = tmpSize = 0;
254  idxfd->read(&tmpStart, 4);
255  idxfd->read(&tmpSize, 4);
256  if (idxoff)
257  *idxoff = (SW_u32)tryoff;
258 
259  *start = swordtoarch32(tmpStart);
260  *size = swordtoarch32(tmpSize);
261 
262  while (away) {
263  unsigned long laststart = *start;
264  unsigned long lastsize = *size;
265  long lasttry = tryoff;
266  tryoff += (away > 0) ? 8 : -8;
267 
268  bool bad = false;
269  if (((tryoff + (away*8)) < -8) || (tryoff + (away*8) > (maxoff+8)))
270  bad = true;
271  else if (idxfd->seek(tryoff, SEEK_SET) < 0)
272  bad = true;
273  if (bad) {
274  if(!awayFromSubstrCheck)
275  retval = -1;
276  *start = (SW_u32)laststart;
277  *size = (SW_u32)lastsize;
278  tryoff = lasttry;
279  if (idxoff)
280  *idxoff = (SW_u32)tryoff;
281  break;
282  }
283  idxfd->read(&tmpStart, 4);
284  idxfd->read(&tmpSize, 4);
285  if (idxoff)
286  *idxoff = (SW_u32)tryoff;
287 
288  *start = swordtoarch32(tmpStart);
289  *size = swordtoarch32(tmpSize);
290 
291  if (((laststart != *start) || (lastsize != *size)) && (*size))
292  away += (away < 0) ? 1 : -1;
293  }
294 
295  lastoff = tryoff;
296  }
297  else {
298  *start = 0;
299  *size = 0;
300  if (idxoff)
301  *idxoff = 0;
302  retval = -1;
303  }
304  return retval;
305 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define SEEK_END
Definition: zconf.h:246
bool caseSensitive
Definition: rawstr4.h:40
int getFd()
Definition: filemgr.h:231
long lastoff
Definition: rawstr4.h:41
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
FileDesc * idxfd
Definition: rawstr4.h:46
#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 getIDXBuf(long ioffset, char **buf) const
Definition: rawstr4.cpp:146
void RawStr4::getIDXBuf ( long  ioffset,
char **  buf 
) const

Definition at line 146 of file rawstr4.cpp.

147 {
148  SW_u32 offset;
149 
150  if ((unsigned long)idxfd > 0) {
151  idxfd->seek(ioffset, SEEK_SET);
152 
153  idxfd->read(&offset, 4);
154  offset = swordtoarch32(offset);
155 
156  getIDXBufDat(offset, buf);
157 
158 /* What the heck is this supposed to do??????
159  for (trybuf = targetbuf = *buf; *trybuf; trybuf++, targetbuf++) {
160  *targetbuf = *trybuf;
161  }
162  *targetbuf = 0;
163  trybuf = 0;
164  if (!caseSensitive) toupperstr_utf8(targetbuf);
165 */
166  }
167 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
#define swordtoarch32(x)
Definition: sysdata.h:94
FileDesc * idxfd
Definition: rawstr4.h:46
#define SEEK_SET
Definition: zconf.h:244
void getIDXBufDat(long ioffset, char **buf) const
Definition: rawstr4.cpp:112
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
void RawStr4::getIDXBufDat ( long  ioffset,
char **  buf 
) const

Definition at line 112 of file rawstr4.cpp.

113 {
114  int size;
115  char ch;
116  if ((unsigned long)datfd > 0) {
117  datfd->seek(ioffset, SEEK_SET);
118  for (size = 0; datfd->read(&ch, 1) == 1; size++) {
119  if ((ch == '\\') || (ch == 10) || (ch == 13))
120  break;
121  }
122  *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
123  if (size) {
124  datfd->seek(ioffset, SEEK_SET);
125  datfd->read(*buf, size);
126  }
127  (*buf)[size] = 0;
128  if (!caseSensitive) toupperstr_utf8(*buf, size*2);
129  }
130  else {
131  *buf = (*buf) ? (char *)realloc(*buf, 1) : (char *)malloc(1);
132  **buf = 0;
133  }
134 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
bool caseSensitive
Definition: rawstr4.h:40
char * toupperstr_utf8(char *t, unsigned int max=0)
Definition: stringmgr.h:118
char * malloc()
char * realloc()
FileDesc * datfd
Definition: rawstr4.h:47
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
long read(void *buf, long count)
Definition: filemgr.cpp:148
void RawStr4::readText ( SW_u32  start,
SW_u32 size,
char **  idxbuf,
SWBuf buf 
) const

Definition at line 318 of file rawstr4.cpp.

319 {
320  unsigned int ch;
321  char *idxbuflocal = 0;
322  getIDXBufDat(istart, &idxbuflocal);
323  SW_u32 start = istart;
324 
325  do {
326  if (*idxbuf)
327  delete [] *idxbuf;
328 
329  buf = "";
330  buf.setFillByte(0);
331  buf.setSize(++(*isize));
332 
333  *idxbuf = new char [ (*isize) ];
334 
335  datfd->seek(start, SEEK_SET);
336  datfd->read(buf.getRawData(), (int)((*isize) - 1));
337 
338  for (ch = 0; buf[ch]; ch++) { // skip over index string
339  if (buf[ch] == 10) {
340  ch++;
341  break;
342  }
343  }
344  buf = SWBuf(buf.c_str()+ch);
345  // resolve link
346  if (!strncmp(buf.c_str(), "@LINK", 5)) {
347  for (ch = 0; buf[ch]; ch++) { // null before nl
348  if (buf[ch] == 10) {
349  buf[ch] = 0;
350  break;
351  }
352  }
353  findOffset(buf.c_str() + 6, &start, isize);
354  }
355  else break;
356  }
357  while (true); // while we're resolving links
358 
359  if (idxbuflocal) {
360  unsigned int localsize = (unsigned int)strlen(idxbuflocal);
361  localsize = (localsize < (*isize - 1)) ? localsize : (*isize - 1);
362  strncpy(*idxbuf, idxbuflocal, localsize);
363  (*idxbuf)[localsize] = 0;
364  free(idxbuflocal);
365  }
366 }
void setFillByte(char ch)
Definition: swbuf.h:146
long seek(long offset, int whence)
Definition: filemgr.cpp:143
Definition: swbuf.h:47
signed char findOffset(const char *key, SW_u32 *start, SW_u32 *size, long away=0, SW_u32 *idxoff=0) const
Definition: rawstr4.cpp:182
char * getRawData()
Definition: swbuf.h:379
free(preg->fastmap)
const char * c_str() const
Definition: swbuf.h:158
FileDesc * datfd
Definition: rawstr4.h:47
#define SEEK_SET
Definition: zconf.h:244
void getIDXBufDat(long ioffset, char **buf) const
Definition: rawstr4.cpp:112
unsigned int SW_u32
Definition: sysdata.h:41
long read(void *buf, long count)
Definition: filemgr.cpp:148
void setSize(unsigned long len)
Definition: swbuf.h:255

Member Data Documentation

bool RawStr4::caseSensitive
private

Definition at line 40 of file rawstr4.h.

FileDesc* RawStr4::datfd
protected

Definition at line 47 of file rawstr4.h.

const int RawStr4::IDXENTRYSIZE = 8
staticprotected

Definition at line 44 of file rawstr4.h.

FileDesc* RawStr4::idxfd
protected

Definition at line 46 of file rawstr4.h.

SWORD_NAMESPACE_START int RawStr4::instance = 0
staticprivate

Definition at line 38 of file rawstr4.h.

long RawStr4::lastoff
mutableprivate

Definition at line 41 of file rawstr4.h.

const char RawStr4::nl = '\n'
static

Definition at line 52 of file rawstr4.h.

char* RawStr4::path
private

Definition at line 39 of file rawstr4.h.


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