summaryrefslogtreecommitdiff
path: root/src/modules/common/rawstr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/common/rawstr.cpp')
-rw-r--r--src/modules/common/rawstr.cpp168
1 files changed, 79 insertions, 89 deletions
diff --git a/src/modules/common/rawstr.cpp b/src/modules/common/rawstr.cpp
index 787946c..5c4ef0e 100644
--- a/src/modules/common/rawstr.cpp
+++ b/src/modules/common/rawstr.cpp
@@ -16,11 +16,13 @@
#include <unistd.h>
#endif
-#include <string.h>
#include <stdlib.h>
#include <utilfuns.h>
#include <rawstr.h>
#include <sysdata.h>
+
+SWORD_NAMESPACE_START
+
/******************************************************************************
* RawStr Statics
*/
@@ -93,7 +95,7 @@ RawStr::~RawStr()
* buf - address of pointer to allocate for storage of string
*/
-void RawStr::getidxbufdat(long ioffset, char **buf)
+void RawStr::getIDXBufDat(long ioffset, char **buf)
{
int size;
char ch;
@@ -103,14 +105,13 @@ void RawStr::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 +129,7 @@ void RawStr::getidxbufdat(long ioffset, char **buf)
* buf - address of pointer to allocate for storage of string
*/
-void RawStr::getidxbuf(long ioffset, char **buf)
+void RawStr::getIDXBuf(long ioffset, char **buf)
{
char *trybuf, *targetbuf;
long offset;
@@ -139,18 +140,7 @@ void RawStr::getidxbuf(long ioffset, char **buf)
offset = swordtoarch32(offset);
- getidxbufdat(offset, buf);
- for (trybuf = targetbuf = *buf; *trybuf; trybuf++, targetbuf++) {
-/*
- if (*trybuf == '-') { // ignore '-' because alphabetized silly in file
- targetbuf--;
- continue;
- }
-*/
- *targetbuf = SW_toupper(*trybuf);
- }
- *targetbuf = 0;
- trybuf = 0;
+ getIDXBufDat(offset, buf);
}
}
@@ -164,39 +154,30 @@ void RawStr::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 RawStr::findoffset(const char *ikey, long *start, unsigned short *size, long away, long *idxoff)
+signed char RawStr::findOffset(const char *ikey, long *start, unsigned short *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) - 6;
+ retval = (tailoff >= 0) ? 0 : -2; // if NOT new file
if (*ikey) {
headoff = 0;
- 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 / 6) - (headoff / 6))) / 2) * 6 : lastoff;
lastoff = -1;
- getidxbuf(tryoff, &trybuf);
+ getIDXBuf(tryoff, &trybuf);
if (!*trybuf && tryoff) { // In case of extra entry at end of idx (not first entry)
tryoff += (tryoff > (maxoff / 2))?-6:6;
@@ -288,11 +269,12 @@ signed char RawStr::findoffset(const char *ikey, long *start, unsigned short *si
* text.
*/
-void RawStr::preptext(char *buf) {
- char *to, *from, space = 0, cr = 0, realdata = 0, nlcnt = 0;
-
- for (to = from = buf; *from; from++) {
- switch (*from) {
+void RawStr::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;
@@ -301,14 +283,16 @@ void RawStr::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;
@@ -317,27 +301,27 @@ void RawStr::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;
}
}
/******************************************************************************
- * RawStr::gettext - gets text at a given offset
+ * RawStr::readtext - gets text at a given offset
*
* ENT:
* start - starting offset where the text is located in the file
@@ -346,40 +330,41 @@ void RawStr::preptext(char *buf) {
*
*/
-void RawStr::gettext(long istart, unsigned short isize, char *idxbuf, char *buf)
+void RawStr::readText(long istart, unsigned short *isize, char **idxbuf, SWBuf &buf)
{
- char *ch;
+ unsigned int ch;
char *idxbuflocal = 0;
- getidxbufdat(istart, &idxbuflocal);
+ getIDXBufDat(istart, &idxbuflocal);
long start = istart;
- unsigned short size = isize;
do {
- memset(buf, 0, size);
+ if (*idxbuf)
+ delete [] *idxbuf;
+ buf = "";
+ buf.setFillByte(0);
+ buf.setSize(++(*isize));
+
+ *idxbuf = new char [ (*isize) ];
+
lseek(datfd->getFd(), start, SEEK_SET);
- read(datfd->getFd(), buf, (int)(size - 2));
+ 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;
}
}
- size -= (unsigned short)(ch-buf);
- memmove(buf, ch, size);
- buf[size] = 0;
- buf[size+1] = 0;
-
+ 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 + 6, &start, &size);
- // TODO: FIX! THIS IS WRONG!!! buf is not reallocated for the appropriate size!
+ findOffset(buf.c_str() + 6, &start, isize);
}
else break;
}
@@ -387,9 +372,9 @@ void RawStr::gettext(long istart, unsigned short 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);
}
}
@@ -403,7 +388,7 @@ void RawStr::gettext(long istart, unsigned short isize, char *idxbuf, char *buf)
* len - length of buffer (0 - null terminated)
*/
-void RawStr::settext(const char *ikey, const char *buf, long len)
+void RawStr::doSetText(const char *ikey, const char *buf, long len)
{
long start, outstart;
@@ -420,19 +405,22 @@ void RawStr::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, 2);
+ toupperstr_utf8(key, strlen(key)*2);
+
+ len = (len < 0) ? strlen(buf) : len;
- getidxbufdat(start, &dbKey);
+ getIDXBufDat(start, &dbKey);
if (strcmp(key, dbKey) < 0) {
}
else if (strcmp(key, dbKey) > 0) {
- idxoff += 6;
- } else if ((!strcmp(key, dbKey)) && (len || strlen(buf) /*we're not deleting*/)) { // got absolute entry
+ if (errorStatus != -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);
@@ -448,14 +436,14 @@ void RawStr::settext(const char *ikey, const char *buf, long len)
memmove(tmpbuf, ch, size - (unsigned short)(ch-tmpbuf));
// resolve link
- if (!strncmp(tmpbuf, "@LINK", 5) && (len ? len : strlen(buf))) {
+ 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);
+ findOffset(tmpbuf + 6, &start, &size, 0, &idxoff);
}
else break;
}
@@ -472,11 +460,11 @@ void RawStr::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);
@@ -484,7 +472,7 @@ void RawStr::settext(const char *ikey, const char *buf, long len)
outsize = archtosword16(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, (int)size);
@@ -521,10 +509,10 @@ void RawStr::settext(const char *ikey, const char *buf, long len)
* srcidxoff - source offset into .vss
*/
-void RawStr::linkentry(const char *destkey, const char *srckey) {
+void RawStr::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;
}
@@ -563,3 +551,5 @@ signed char RawStr::createModule(const char *ipath)
return 0;
}
+
+SWORD_NAMESPACE_END