void RawStr::getIDXBufDat(long ioffset, char **buf) const { int size; char ch; if (datfd > 0) { datfd->seek(ioffset, SEEK_SET); for (size = 0; datfd->read(&ch, 1) == 1; size++) { if ((ch == '\\') || (ch == 10) || (ch == 13)) break; } *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1); if (size) { datfd->seek(ioffset, SEEK_SET); datfd->read(*buf, size); } (*buf)[size] = 0; if (!caseSensitive) toupperstr_utf8(*buf, size*2); } else { *buf = (*buf) ? (char *)realloc(*buf, 1) : (char *)malloc(1); **buf = 0; } }
void RawStr::doSetText(const char *ikey, const char *buf, long len) { __u32 start, outstart; __u32 idxoff; __u32 endoff; __s32 shiftSize; __u16 size; __u16 outsize; char *tmpbuf = 0; char *key = 0; char *dbKey = 0; char *idxBytes = 0; char *outbuf = 0; char *ch = 0; char errorStatus = findOffset(ikey, &start, &size, 0, &idxoff); stdstr(&key, ikey, 2); if (!caseSensitive) toupperstr_utf8(key, strlen(key)*2); len = (len < 0) ? strlen(buf) : len; getIDXBufDat(start, &dbKey); if (strcmp(key, dbKey) < 0) { } else if (strcmp(key, dbKey) > 0) { if (errorStatus != (char)-2) // not a new file idxoff += 6; else idxoff = 0; } else if ((!strcmp(key, dbKey)) && (len>0 /*we're not deleting*/)) { // got absolute entry do { tmpbuf = new char [ size + 2 ]; memset(tmpbuf, 0, size + 2); datfd->seek(start, SEEK_SET); datfd->read(tmpbuf, (int)(size - 1)); for (ch = tmpbuf; *ch; ch++) { // skip over index string if (*ch == 10) { ch++; break; } } memmove(tmpbuf, ch, size - (unsigned short)(ch-tmpbuf)); // resolve link if (!strncmp(tmpbuf, "@LINK", 5) && (len)) { for (ch = tmpbuf; *ch; ch++) { // null before nl if (*ch == 10) { *ch = 0; break; } } findOffset(tmpbuf + 6, &start, &size, 0, &idxoff); } else break; } while (true); // while we're resolving links } endoff = idxfd->seek(0, SEEK_END); shiftSize = endoff - idxoff; if (shiftSize > 0) { idxBytes = new char [ shiftSize ]; idxfd->seek(idxoff, SEEK_SET); idxfd->read(idxBytes, shiftSize); } outbuf = new char [ len + strlen(key) + 5 ]; sprintf(outbuf, "%s%c%c", key, 13, 10); size = strlen(outbuf); memcpy(outbuf + size, buf, len); size = outsize = size + (len); start = outstart = datfd->seek(0, SEEK_END); outstart = archtosword32(start); outsize = archtosword16(size); idxfd->seek(idxoff, SEEK_SET); if (len > 0) { datfd->seek(start, SEEK_SET); datfd->write(outbuf, (int)size); // add a new line to make data file easier to read in an editor datfd->write(&nl, 1); idxfd->write(&outstart, 4); idxfd->write(&outsize, 2); if (idxBytes) { idxfd->write(idxBytes, shiftSize); delete [] idxBytes; } } else { // delete entry if (idxBytes) { idxfd->write(idxBytes+6, shiftSize-6); idxfd->seek(-1, SEEK_CUR); // last valid byte FileMgr::getSystemFileMgr()->trunc(idxfd); // truncate index delete [] idxBytes; } } delete [] key; delete [] outbuf; free(dbKey); }
signed char RawStr::findOffset(const char *ikey, __u32 *start, __u16 *size, long away, __u32 *idxoff) const { char *trybuf, *maxbuf, *key = 0, quitflag = 0; signed char retval = -1; long headoff, tailoff, tryoff = 0, maxoff = 0; int diff = 0; bool awayFromSubstrCheck = false; if (idxfd->getFd() >=0) { tailoff = maxoff = idxfd->seek(0, SEEK_END) - 6; retval = (tailoff >= 0) ? 0 : -2; // if NOT new file if (*ikey && retval != -2) { headoff = 0; stdstr(&key, ikey, 3); if (!caseSensitive) toupperstr_utf8(key, strlen(key)*3); int keylen = strlen(key); bool substr = false; trybuf = maxbuf = 0; getIDXBuf(maxoff, &maxbuf); while (headoff < tailoff) { tryoff = (lastoff == -1) ? headoff + ((((tailoff / 6) - (headoff / 6))) / 2) * 6 : lastoff; lastoff = -1; getIDXBuf(tryoff, &trybuf); if (!*trybuf && tryoff) { // In case of extra entry at end of idx (not first entry) tryoff += (tryoff > (maxoff / 2))?-6:6; retval = -1; break; } diff = strcmp(key, trybuf); if (!diff) break; if (!strncmp(trybuf, key, keylen)) substr = true; if (diff < 0) tailoff = (tryoff == headoff) ? headoff : tryoff; else headoff = tryoff; if (tailoff == headoff + 6) { if (quitflag++) headoff = tailoff; } } // didn't find exact match if (headoff >= tailoff) { tryoff = headoff; if (!substr && ((tryoff != maxoff)||(strncmp(key, maxbuf, keylen)<0))) { awayFromSubstrCheck = true; away--; // if our entry doesn't startwith our key, prefer the previous entry over the next } } if (trybuf) free(trybuf); delete [] key; if (maxbuf) free(maxbuf); } else tryoff = 0; idxfd->seek(tryoff, SEEK_SET); __u32 tmpStart; __u16 tmpSize; *start = *size = tmpStart = tmpSize = 0; idxfd->read(&tmpStart, 4); idxfd->read(&tmpSize, 2); if (idxoff) *idxoff = tryoff; *start = swordtoarch32(tmpStart); *size = swordtoarch16(tmpSize); while (away) { unsigned long laststart = *start; unsigned short lastsize = *size; long lasttry = tryoff; tryoff += (away > 0) ? 6 : -6; bool bad = false; if (((tryoff + (away*6)) < -6) || (tryoff + (away*6) > (maxoff+6))) bad = true; else if (idxfd->seek(tryoff, SEEK_SET) < 0) bad = true; if (bad) { if(!awayFromSubstrCheck) retval = -1; *start = laststart; *size = lastsize; tryoff = lasttry; if (idxoff) *idxoff = tryoff; break; } idxfd->read(&tmpStart, 4); idxfd->read(&tmpSize, 2); if (idxoff) *idxoff = tryoff; *start = swordtoarch32(tmpStart); *size = swordtoarch16(tmpSize); if (((laststart != *start) || (lastsize != *size)) && (*size)) away += (away < 0) ? 1 : -1; } lastoff = tryoff; } else { *start = 0; *size = 0; if (idxoff) *idxoff = 0; retval = -1; } return retval; }
void zStr::setText(const char *ikey, const char *buf, long len) { static const char nl[] = {13, 10}; __u32 start, outstart; __u32 size, outsize; __s32 endoff; long idxoff = 0; __s32 shiftSize; char *tmpbuf = 0; char *key = 0; char *dbKey = 0; char *idxBytes = 0; char *outbuf = 0; char *ch = 0; len = (len < 0) ? strlen(buf) : len; stdstr(&key, ikey, 3); if (!caseSensitive) toupperstr_utf8(key, strlen(key)*3); char notFound = findKeyIndex(ikey, &idxoff, 0); if (!notFound) { getKeyFromIdxOffset(idxoff, &dbKey); int diff = strcmp(key, dbKey); if (diff < 0) { } else if (diff > 0) { idxoff += IDXENTRYSIZE; } else if ((!diff) && (len > 0 /*we're not deleting*/)) { // got absolute entry do { idxfd->seek(idxoff, SEEK_SET); idxfd->read(&start, 4); idxfd->read(&size, 4); start = swordtoarch32(start); size = swordtoarch32(size); tmpbuf = new char [ size + 2 ]; memset(tmpbuf, 0, size + 2); datfd->seek(start, SEEK_SET); datfd->read(tmpbuf, size); for (ch = tmpbuf; *ch; ch++) { // skip over index string if (*ch == 10) { ch++; break; } } memmove(tmpbuf, ch, size - (unsigned long)(ch-tmpbuf)); // resolve link if (!strncmp(tmpbuf, "@LINK", 5) && (len)) { for (ch = tmpbuf; *ch; ch++) { // null before nl if (*ch == 10) { *ch = 0; break; } } findKeyIndex(tmpbuf + IDXENTRYSIZE, &idxoff); delete [] tmpbuf; } else break; } while (true); // while we're resolving links } } endoff = idxfd->seek(0, SEEK_END); shiftSize = endoff - idxoff; if (shiftSize > 0) { idxBytes = new char [ shiftSize ]; idxfd->seek(idxoff, SEEK_SET); idxfd->read(idxBytes, shiftSize); } outbuf = new char [ len + strlen(key) + 5 ]; sprintf(outbuf, "%s%c%c", key, 13, 10); size = strlen(outbuf); if (len > 0) { // NOT a link if (!cacheBlock) { flushCache(); cacheBlock = new EntriesBlock(); cacheBlockIndex = (zdxfd->seek(0, SEEK_END) / ZDXENTRYSIZE); } else if (cacheBlock->getCount() >= blockCount) { flushCache(); cacheBlock = new EntriesBlock(); cacheBlockIndex = (zdxfd->seek(0, SEEK_END) / ZDXENTRYSIZE); } __u32 entry = cacheBlock->addEntry(buf); cacheDirty = true; outstart = archtosword32(cacheBlockIndex); outsize = archtosword32(entry); memcpy (outbuf + size, &outstart, sizeof(__u32)); memcpy (outbuf + size + sizeof(__u32), &outsize, sizeof(__u32)); size += (sizeof(__u32) * 2); } else { // link memcpy(outbuf + size, buf, len); size += len; } start = datfd->seek(0, SEEK_END); outstart = archtosword32(start); outsize = archtosword32(size); idxfd->seek(idxoff, SEEK_SET); if (len > 0) { datfd->seek(start, SEEK_SET); datfd->write(outbuf, size); // add a new line to make data file easier to read in an editor datfd->write(&nl, 2); idxfd->write(&outstart, 4); idxfd->write(&outsize, 4); if (idxBytes) { idxfd->write(idxBytes, shiftSize); } } else { // delete entry if (idxBytes) { idxfd->write(idxBytes+IDXENTRYSIZE, shiftSize-IDXENTRYSIZE); idxfd->seek(-1, SEEK_CUR); // last valid byte FileMgr::getSystemFileMgr()->trunc(idxfd); // truncate index } } if (idxBytes) delete [] idxBytes; delete [] key; delete [] outbuf; free(dbKey); }
signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) const { char *maxbuf = 0, *trybuf = 0, *key = 0, quitflag = 0; signed char retval = 0; __s32 headoff, tailoff, tryoff = 0, maxoff = 0; __u32 start, size; int diff = 0; bool awayFromSubstrCheck = false; if (idxfd->getFd() >= 0) { tailoff = maxoff = idxfd->seek(0, SEEK_END) - IDXENTRYSIZE; if (*ikey) { headoff = 0; stdstr(&key, ikey, 3); if (!caseSensitive) toupperstr_utf8(key, strlen(key)*3); int keylen = strlen(key); bool substr = false; getKeyFromIdxOffset(maxoff, &maxbuf); while (headoff < tailoff) { tryoff = (lastoff == -1) ? headoff + (((((tailoff / IDXENTRYSIZE) - (headoff / IDXENTRYSIZE))) / 2) * IDXENTRYSIZE) : lastoff; lastoff = -1; getKeyFromIdxOffset(tryoff, &trybuf); if (!*trybuf && tryoff) { // In case of extra entry at end of idx (not first entry) tryoff += (tryoff > (maxoff / 2))?-IDXENTRYSIZE:IDXENTRYSIZE; retval = -1; break; } diff = strcmp(key, trybuf); if (!diff) break; if (!strncmp(trybuf, key, keylen)) substr = true; if (diff < 0) tailoff = (tryoff == headoff) ? headoff : tryoff; else headoff = tryoff; if (tailoff == headoff + IDXENTRYSIZE) { if (quitflag++) headoff = tailoff; } } // didn't find exact match if (headoff >= tailoff) { tryoff = headoff; if (!substr && ((tryoff != maxoff)||(strncmp(key, maxbuf, keylen)<0))) { awayFromSubstrCheck = true; away--; // if our entry doesn't startwith our key, prefer the previous entry over the next } } if (trybuf) free(trybuf); delete [] key; if (maxbuf) free(maxbuf); } else { tryoff = 0; } idxfd->seek(tryoff, SEEK_SET); start = size = 0; retval = (idxfd->read(&start, 4) == 4) ? retval : -1; retval = (idxfd->read(&size, 4) == 4) ? retval : -1; start = swordtoarch32(start); size = swordtoarch32(size); if (idxoff) *idxoff = tryoff; while (away) { __u32 laststart = start; __u32 lastsize = size; __s32 lasttry = tryoff; tryoff += (away > 0) ? IDXENTRYSIZE : -IDXENTRYSIZE; bool bad = false; if (((long)(tryoff + (away*IDXENTRYSIZE)) < -IDXENTRYSIZE) || (tryoff + (away*IDXENTRYSIZE) > (maxoff+IDXENTRYSIZE))) bad = true; else if (idxfd->seek(tryoff, SEEK_SET) < 0) bad = true; if (bad) { if(!awayFromSubstrCheck) retval = -1; start = laststart; size = lastsize; tryoff = lasttry; if (idxoff) *idxoff = tryoff; break; } idxfd->read(&start, 4); idxfd->read(&size, 4); start = swordtoarch32(start); size = swordtoarch32(size); if (idxoff) *idxoff = tryoff; if (((laststart != start) || (lastsize != size)) && (start) && (size)) away += (away < 0) ? 1 : -1; } lastoff = tryoff; } else { if (idxoff) *idxoff = 0; retval = -1; } return retval; }