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

#include <rawstr.h>

+ Inheritance diagram for RawStr:
+ Collaboration diagram for RawStr:

Public Member Functions

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

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 = 6
 

Private Attributes

bool caseSensitive
 
long lastoff
 
char * path
 

Static Private Attributes

static int instance = 0
 

Detailed Description

Definition at line 35 of file rawstr.h.

Constructor & Destructor Documentation

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

Definition at line 59 of file rawstr.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 }
long lastoff
Definition: rawstr.h:41
static int instance
Definition: rawstr.h:38
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
char * path
Definition: rawstr.h:39
static unsigned int RDWR
Definition: filemgr.h:76
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
FileDesc * idxfd
Definition: rawstr.h:45
FileDesc * datfd
Definition: rawstr.h:46
#define SWLOGD(...)
Definition: defs.h:187
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
bool caseSensitive
Definition: rawstr.h:40
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
RawStr::~RawStr ( )
virtual

Definition at line 91 of file rawstr.cpp.

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

Member Function Documentation

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

Definition at line 502 of file rawstr.cpp.

503 {
504  char *path = 0;
505  char *buf = new char [ strlen (ipath) + 20 ];
506  FileDesc *fd, *fd2;
507 
508  stdstr(&path, ipath);
509 
510  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
511  path[strlen(path)-1] = 0;
512 
513  sprintf(buf, "%s.dat", path);
514  FileMgr::removeFile(buf);
516  fd->getFd();
518 
519  sprintf(buf, "%s.idx", path);
520  FileMgr::removeFile(buf);
522  fd2->getFd();
524 
525  delete [] path;
526 
527  return 0;
528 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
char * path
Definition: rawstr.h:39
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
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 RawStr::doLinkEntry ( const char *  destkey,
const char *  srckey 
)
protected

Definition at line 488 of file rawstr.cpp.

488  {
489  char *text = new char [ strlen(destkey) + 7 ];
490  sprintf(text, "@LINK %s", destkey);
491  doSetText(srckey, text);
492  delete [] text;
493 }
void doSetText(const char *key, const char *buf, long len=-1)
Definition: rawstr.cpp:368
void RawStr::doSetText ( const char *  key,
const char *  buf,
long  len = -1 
)
protected

Definition at line 368 of file rawstr.cpp.

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

Definition at line 173 of file rawstr.cpp.

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

Definition at line 146 of file rawstr.cpp.

147 {
148  SW_u32 offset;
149 
150  if (idxfd && idxfd->getFd() >= 0) {
151  idxfd->seek(ioffset, SEEK_SET);
152  idxfd->read(&offset, 4);
153 
154  offset = swordtoarch32(offset);
155 
156  getIDXBufDat(offset, buf);
157  }
158 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
int getFd()
Definition: filemgr.h:231
FileDesc * idxfd
Definition: rawstr.h:45
#define swordtoarch32(x)
Definition: sysdata.h:94
#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 getIDXBufDat(long ioffset, char **buf) const
Definition: rawstr.cpp:112
void RawStr::getIDXBufDat ( long  ioffset,
char **  buf 
) const

Definition at line 112 of file rawstr.cpp.

113 {
114  int size;
115  char ch;
116  if (datfd && datfd->getFd() >= 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
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
FileDesc * datfd
Definition: rawstr.h:46
long read(void *buf, long count)
Definition: filemgr.cpp:148
bool caseSensitive
Definition: rawstr.h:40
void RawStr::readText ( SW_u32  start,
SW_u16 size,
char **  idxbuf,
SWBuf buf 
) const

Definition at line 309 of file rawstr.cpp.

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

Member Data Documentation

bool RawStr::caseSensitive
private

Definition at line 40 of file rawstr.h.

FileDesc* RawStr::datfd
protected

Definition at line 46 of file rawstr.h.

const int RawStr::IDXENTRYSIZE = 6
staticprotected

Definition at line 49 of file rawstr.h.

FileDesc* RawStr::idxfd
protected

Definition at line 45 of file rawstr.h.

SWORD_NAMESPACE_START int RawStr::instance = 0
staticprivate

Definition at line 38 of file rawstr.h.

long RawStr::lastoff
mutableprivate

Definition at line 41 of file rawstr.h.

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

Definition at line 52 of file rawstr.h.

char* RawStr::path
private

Definition at line 39 of file rawstr.h.


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