30 #include <sys/types.h>
59 RawStr4::RawStr4(
const char *ipath,
int fileMode,
bool caseSensitive) : caseSensitive(caseSensitive)
80 SWLOGD(
"Couldn't open file: %s. errno: %d", buf.
c_str(), errno);
116 if ((
unsigned long)
datfd > 0) {
118 for (size = 0;
datfd->
read(&ch, 1) == 1; size++) {
119 if ((ch ==
'\\') || (ch == 10) || (ch == 13))
122 *buf = (*buf) ? (
char *)
realloc(*buf, size*2 + 1) : (
char *)
malloc(size*2 + 1);
131 *buf = (*buf) ? (
char *)
realloc(*buf, 1) : (
char *)
malloc(1);
150 if ((
unsigned long)
idxfd > 0) {
184 char *trybuf, *maxbuf, *key = 0, quitflag = 0;
185 signed char retval = -1;
186 long headoff, tailoff, tryoff = 0, maxoff = 0;
188 bool awayFromSubstrCheck =
false;
193 retval = (tailoff >= 0) ? 0 : -2;
194 if (*ikey && retval != -2) {
200 int keylen = (int)strlen(key);
206 while (headoff < tailoff) {
207 tryoff = (
lastoff == -1) ? headoff + ((((tailoff / 8) - (headoff / 8))) / 2) * 8 :
lastoff;
211 if (!*trybuf && tryoff) {
212 tryoff += (tryoff > (maxoff / 2))?-8:8;
217 diff = strcmp(key, trybuf);
222 if (!strncmp(trybuf, key, keylen)) substr =
true;
225 tailoff = (tryoff == headoff) ? headoff : tryoff;
226 else headoff = tryoff;
228 if (tailoff == headoff + 8) {
235 if (headoff >= tailoff) {
237 if (!substr && ((tryoff != maxoff)||(strncmp(key, maxbuf, keylen)<0))) {
238 awayFromSubstrCheck =
true;
253 *start = *size = tmpStart = tmpSize = 0;
263 unsigned long laststart = *start;
264 unsigned long lastsize = *
size;
265 long lasttry = tryoff;
266 tryoff += (away > 0) ? 8 : -8;
269 if (((tryoff + (away*8)) < -8) || (tryoff + (away*8) > (maxoff+8)))
274 if(!awayFromSubstrCheck)
276 *start = (
SW_u32)laststart;
291 if (((laststart != *start) || (lastsize != *size)) && (*size))
292 away += (away < 0) ? 1 : -1;
321 char *idxbuflocal = 0;
333 *idxbuf =
new char [ (*isize) ];
338 for (ch = 0; buf[ch]; ch++) {
346 if (!strncmp(buf.
c_str(),
"@LINK", 5)) {
347 for (ch = 0; buf[ch]; ch++) {
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;
392 char errorStatus =
findOffset(ikey, &start, &size, 0, &idxoff);
396 len = (len < 0) ? strlen(buf) : len;
399 if (strcmp(key, dbKey) < 0) {
401 else if (strcmp(key, dbKey) > 0) {
402 if (errorStatus != (
char)-2)
406 else if ((!strcmp(key, dbKey)) && (len>0)) {
408 tmpbuf =
new char [ size + 2 ];
409 memset(tmpbuf, 0, size + 2);
413 for (ch = tmpbuf; *ch; ch++) {
419 memmove(tmpbuf, ch, size - (
unsigned long)(ch-tmpbuf));
422 if (!strncmp(tmpbuf,
"@LINK", 5) && (len > 0)) {
423 for (ch = tmpbuf; *ch; ch++) {
429 findOffset(tmpbuf + 8, &start, &size, 0, &idxoff);
439 shiftSize = endoff - idxoff;
442 idxBytes =
new char [ shiftSize ];
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;
497 char *text =
new char [ strlen(destkey) + 7 ];
498 sprintf(text,
"@LINK %s", destkey);
514 char *buf =
new char [ strlen (ipath) + 20 ];
519 if ((path[strlen(path)-1] ==
'/') || (path[strlen(path)-1] ==
'\\'))
520 path[strlen(path)-1] = 0;
522 sprintf(buf,
"%s.dat", path);
528 sprintf(buf,
"%s.idx", path);
void setFillByte(char ch)
#define SWORD_NAMESPACE_START
long seek(long offset, int whence)
static const int IDXENTRYSIZE
FileDesc * open(const char *path, int mode, bool tryDowngrade)
signed char findOffset(const char *key, SW_u32 *start, SW_u32 *size, long away=0, SW_u32 *idxoff=0) const
RawStr4(const char *ipath, int fileMode=-1, bool caseSensitive=false)
long write(const void *buf, long count)
signed char trunc(FileDesc *file)
char * toupperstr_utf8(char *t, unsigned int max=0)
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
void close(FileDesc *file)
const char * c_str() const
static int removeFile(const char *fName)
void readText(SW_u32 start, SW_u32 *size, char **idxbuf, SWBuf &buf) const
void doSetText(const char *key, const char *buf, long len=-1)
void getIDXBufDat(long ioffset, char **buf) const
void doLinkEntry(const char *destkey, const char *srckey)
static unsigned int CREAT
static signed char createModule(const char *path)
static unsigned int IWRITE
static unsigned int WRONLY
long read(void *buf, long count)
static unsigned int IREAD
#define SWORD_NAMESPACE_END
SWBuf & setFormatted(const char *format,...)
void setSize(unsigned long len)
void getIDXBuf(long ioffset, char **buf) const
static FileMgr * getSystemFileMgr()