diff options
Diffstat (limited to 'src/modules/common/rawstr4.cpp')
-rw-r--r-- | src/modules/common/rawstr4.cpp | 169 |
1 files changed, 84 insertions, 85 deletions
diff --git a/src/modules/common/rawstr4.cpp b/src/modules/common/rawstr4.cpp index d5926ff..e278675 100644 --- a/src/modules/common/rawstr4.cpp +++ b/src/modules/common/rawstr4.cpp @@ -15,12 +15,13 @@ #include <unistd.h> #endif -#include <string.h> #include <stdlib.h> #include <utilfuns.h> #include <rawstr4.h> #include <sysdata.h> +SWORD_NAMESPACE_START + /****************************************************************************** * RawStr Statics */ @@ -93,8 +94,7 @@ RawStr4::~RawStr4() * buf - address of pointer to allocate for storage of string */ -void RawStr4::getidxbufdat(long ioffset, char **buf) -{ +void RawStr4::getIDXBufDat(long ioffset, char **buf) { int size; char ch; if (datfd > 0) { @@ -103,14 +103,13 @@ void RawStr4::getidxbufdat(long ioffset, char **buf) if ((ch == '\\') || (ch == 10) || (ch == 13)) break; } - *buf = (*buf) ? (char *)realloc(*buf, size + 1) : (char *)malloc(size + 1); + *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1); if (size) { lseek(datfd->getFd(), ioffset, SEEK_SET); read(datfd->getFd(), *buf, size); } (*buf)[size] = 0; - for (size--; size > 0; size--) - (*buf)[size] = SW_toupper((*buf)[size]); + toupperstr_utf8(*buf, size*2); } else { *buf = (*buf) ? (char *)realloc(*buf, 1) : (char *)malloc(1); @@ -128,7 +127,7 @@ void RawStr4::getidxbufdat(long ioffset, char **buf) * buf - address of pointer to allocate for storage of string */ -void RawStr4::getidxbuf(long ioffset, char **buf) +void RawStr4::getIDXBuf(long ioffset, char **buf) { char *trybuf, *targetbuf; long offset; @@ -139,18 +138,16 @@ void RawStr4::getidxbuf(long ioffset, char **buf) offset = swordtoarch32(offset); - getidxbufdat(offset, buf); + getIDXBufDat(offset, buf); + +/* What the heck is this supposed to do?????? for (trybuf = targetbuf = *buf; *trybuf; trybuf++, targetbuf++) { -/* - if (*trybuf == '-') { // ignore '-' because alphabetized silly in file - targetbuf--; - continue; - } -*/ - *targetbuf = SW_toupper(*trybuf); + *targetbuf = *trybuf; } *targetbuf = 0; trybuf = 0; + toupperstr_utf8(targetbuf); +*/ } } @@ -164,39 +161,30 @@ void RawStr4::getidxbuf(long ioffset, char **buf) * away - number of entries before of after to jump * (default = 0) * - * RET: error status + * RET: error status -1 general error; -2 new file */ -signed char RawStr4::findoffset(const char *ikey, long *start, unsigned long *size, long away, long *idxoff) +signed char RawStr4::findOffset(const char *ikey, long *start, unsigned long *size, long away, long *idxoff) { - char *trybuf, *targetbuf, *key, quitflag = 0; - signed char retval = 0; + char *trybuf, *targetbuf, *key = 0, quitflag = 0; + signed char retval = -1; long headoff, tailoff, tryoff = 0, maxoff = 0; if (idxfd->getFd() >=0) { + tailoff = maxoff = lseek(idxfd->getFd(), 0, SEEK_END) - 8; + retval = (tailoff >= 0) ? 0 : -2; // if NOT new file if (*ikey) { headoff = 0; - tailoff = maxoff = lseek(idxfd->getFd(), 0, SEEK_END) - 8; - key = new char [ strlen(ikey) + 1 ]; - strcpy(key, ikey); + stdstr(&key, ikey, 3); + toupperstr_utf8(key, strlen(key)*3); - for (trybuf = targetbuf = key; *trybuf; trybuf++, targetbuf++) { - /* - if (*trybuf == '-') { // ignore '-' because alphabetized silly in file - targetbuf--; - continue; - } - */ - *targetbuf = SW_toupper(*trybuf); - } - *targetbuf = 0; trybuf = 0; while (headoff < tailoff) { tryoff = (lastoff == -1) ? headoff + ((((tailoff / 8) - (headoff / 8))) / 2) * 8 : lastoff; lastoff = -1; - getidxbuf(tryoff, &trybuf); + getIDXBuf(tryoff, &trybuf); if (!*trybuf) { // In case of extra entry at end of idx tryoff += (tryoff > (maxoff / 2))?-8:8; @@ -288,12 +276,12 @@ signed char RawStr4::findoffset(const char *ikey, long *start, unsigned long *si * text. */ -void RawStr4::preptext(char *buf) -{ - char *to, *from, space = 0, cr = 0, realdata = 0, nlcnt = 0; - - for (to = from = buf; *from; from++) { - switch (*from) { +void RawStr4::prepText(SWBuf &buf) { + unsigned int to, from; + char space = 0, cr = 0, realdata = 0, nlcnt = 0; + char *rawBuf = buf.getRawData(); + for (to = from = 0; rawBuf[from]; from++) { + switch (rawBuf[from]) { case 10: if (!realdata) continue; @@ -302,14 +290,16 @@ void RawStr4::preptext(char *buf) nlcnt++; if (nlcnt > 1) { // *to++ = nl; - *to++ = nl; + rawBuf[to++] = 10; +// *to++ = nl[1]; // nlcnt = 0; } continue; case 13: if (!realdata) continue; - *to++ = nl; +// *to++ = nl[0]; + rawBuf[to++] = 10; space = 0; cr = 1; continue; @@ -318,27 +308,27 @@ void RawStr4::preptext(char *buf) nlcnt = 0; if (space) { space = 0; - if (*from != ' ') { - *to++ = ' '; + if (rawBuf[from] != ' ') { + rawBuf[to++] = ' '; from--; continue; } } - *to++ = *from; + rawBuf[to++] = rawBuf[from]; } - *to = 0; + buf.setSize(to); - while (to > (buf+1)) { // remove trailing excess + while (to > 1) { // remove trailing excess to--; - if ((*to == 10) || (*to == ' ')) - *to = 0; + if ((rawBuf[to] == 10) || (rawBuf[to] == ' ')) + buf.setSize(to); else break; } } /****************************************************************************** - * RawStr4::gettext - gets text at a given offset + * RawStr4::readtext - gets text at a given offset * * ENT: * start - starting offset where the text is located in the file @@ -347,36 +337,41 @@ void RawStr4::preptext(char *buf) * */ -void RawStr4::gettext(long istart, unsigned long isize, char *idxbuf, char *buf) +void RawStr4::readText(long istart, unsigned long *isize, char **idxbuf, SWBuf &buf) { - char *ch; + unsigned int ch; char *idxbuflocal = 0; - getidxbufdat(istart, &idxbuflocal); + getIDXBufDat(istart, &idxbuflocal); long start = istart; - unsigned long size = isize; do { - memset(buf, 0, size); + if (*idxbuf) + delete [] *idxbuf; + *idxbuf = new char [ (*isize) ]; + + buf = ""; + buf.setFillByte(0); + buf.setSize(*isize); lseek(datfd->getFd(), start, SEEK_SET); - read(datfd->getFd(), buf, (int)(size - 1)); + read(datfd->getFd(), buf.getRawData(), (int)((*isize) - 1)); - for (ch = buf; *ch; ch++) { // skip over index string - if (*ch == 10) { + for (ch = 0; buf[ch]; ch++) { // skip over index string + if (buf[ch] == 10) { ch++; break; } } - memmove(buf, ch, size - (unsigned long)(ch-buf)); - + buf = SWBuf(buf.c_str()+ch); // resolve link - if (!strncmp(buf, "@LINK", 5)) { - for (ch = buf; *ch; ch++) { // null before nl - if (*ch == 10) { - *ch = 0; + if (!strncmp(buf.c_str(), "@LINK", 5)) { + for (ch = 0; buf[ch]; ch++) { // null before nl + if (buf[ch] == 10) { + buf[ch] = 0; break; } } - findoffset(buf + 8, &start, &size); + findOffset(buf.c_str() + 6, &start, isize); + } else break; } @@ -384,9 +379,9 @@ void RawStr4::gettext(long istart, unsigned long isize, char *idxbuf, char *buf) if (idxbuflocal) { int localsize = strlen(idxbuflocal); - localsize = (localsize < (size - 1)) ? localsize : (size - 1); - strncpy(idxbuf, idxbuflocal, localsize); - idxbuf[localsize] = 0; + localsize = (localsize < (*isize - 1)) ? localsize : (*isize - 1); + strncpy(*idxbuf, idxbuflocal, localsize); + (*idxbuf)[localsize] = 0; free(idxbuflocal); } } @@ -400,8 +395,7 @@ void RawStr4::gettext(long istart, unsigned long isize, char *idxbuf, char *buf) * len - length of buffer (0 - null terminated) */ -void RawStr4::settext(const char *ikey, const char *buf, long len) -{ +void RawStr4::doSetText(const char *ikey, const char *buf, long len) { long start, outstart; long idxoff; @@ -417,19 +411,21 @@ void RawStr4::settext(const char *ikey, const char *buf, long len) char *outbuf = 0; char *ch = 0; - findoffset(ikey, &start, &size, 0, &idxoff); - stdstr(&key, ikey); - for (ch = key; *ch; ch++) - *ch = SW_toupper(*ch); - ch = 0; + char errorStatus = findOffset(ikey, &start, &size, 0, &idxoff); + stdstr(&key, ikey, 3); + toupperstr_utf8(key, strlen(key)*3); - getidxbufdat(start, &dbKey); + len = (len < 0) ? strlen(buf) : len; + getIDXBufDat(start, &dbKey); if (strcmp(key, dbKey) < 0) { } else if (strcmp(key, dbKey) > 0) { - idxoff += 8; - } else if ((!strcmp(key, dbKey)) && (len || strlen(buf) /*we're not deleting*/)) { // got absolute entry + if (errorStatus != -2) // not a new file + idxoff += 8; + 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); @@ -445,14 +441,15 @@ void RawStr4::settext(const char *ikey, const char *buf, long len) memmove(tmpbuf, ch, size - (unsigned long)(ch-tmpbuf)); // resolve link - if (!strncmp(tmpbuf, "@LINK", 5) && (len ? len : strlen(buf))) { + if (!strncmp(tmpbuf, "@LINK", 5) && (len > 0)) { for (ch = tmpbuf; *ch; ch++) { // null before nl if (*ch == 10) { *ch = 0; break; } } - findoffset(tmpbuf + 8, &start, &size, 0, &idxoff); + findOffset(tmpbuf + 8, &start, &size, 0, &idxoff); + ++size; } else break; } @@ -469,11 +466,11 @@ void RawStr4::settext(const char *ikey, const char *buf, long len) read(idxfd->getFd(), idxBytes, shiftSize); } - outbuf = new char [ (len ? len : strlen(buf)) + strlen(key) + 5 ]; + outbuf = new char [ len + strlen(key) + 5 ]; sprintf(outbuf, "%s%c%c", key, 13, 10); size = strlen(outbuf); - memcpy (outbuf + size, buf, len ? len : strlen(buf)); - size = outsize = size + (len ? len : strlen(buf)); + memcpy(outbuf + size, buf, len); + size = outsize = size + len; start = outstart = lseek(datfd->getFd(), 0, SEEK_END); @@ -481,7 +478,7 @@ void RawStr4::settext(const char *ikey, const char *buf, long len) outsize = archtosword32(size); lseek(idxfd->getFd(), idxoff, SEEK_SET); - if (len ? len : strlen(buf)) { + if (len>0) { lseek(datfd->getFd(), start, SEEK_SET); write(datfd->getFd(), outbuf, (long)size); @@ -518,10 +515,10 @@ void RawStr4::settext(const char *ikey, const char *buf, long len) * srcidxoff - source offset into .vss */ -void RawStr4::linkentry(const char *destkey, const char *srckey) { +void RawStr4::doLinkEntry(const char *destkey, const char *srckey) { char *text = new char [ strlen(destkey) + 7 ]; sprintf(text, "@LINK %s", destkey); - settext(srckey, text); + doSetText(srckey, text); delete [] text; } @@ -560,3 +557,5 @@ signed char RawStr4::createModule(const char *ipath) return 0; } + +SWORD_NAMESPACE_END |