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.cpp140
1 files changed, 51 insertions, 89 deletions
diff --git a/src/modules/common/rawstr.cpp b/src/modules/common/rawstr.cpp
index d2da1e9..6f17628 100644
--- a/src/modules/common/rawstr.cpp
+++ b/src/modules/common/rawstr.cpp
@@ -1,11 +1,26 @@
/******************************************************************************
* rawstr.cpp - code for class 'RawStr'- a module that reads raw text
- * files: ot and nt using indexs ??.bks ??.cps ??.vss
- * and provides lookup and parsing functions based on
- * class StrKey
+ * files: ot and nt using indexs ??.bks ??.cps ??.vss
+ * and provides lookup and parsing functions based on
+ * class StrKey
+ *
+ *
+ * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
+ * CrossWire Bible Society
+ * P. O. Box 2528
+ * Tempe, AZ 85280-2528
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
*/
-
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
@@ -27,6 +42,8 @@ SWORD_NAMESPACE_START
int RawStr::instance = 0;
char RawStr::nl = '\n';
+const int RawStr::IDXENTRYSIZE = 6;
+
/******************************************************************************
@@ -88,7 +105,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) const
{
int size;
char ch;
@@ -122,9 +139,9 @@ 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) const
{
- long offset;
+ __u32 offset;
if (idxfd > 0) {
idxfd->seek(ioffset, SEEK_SET);
@@ -149,17 +166,18 @@ void RawStr::getIDXBuf(long ioffset, char **buf)
* 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, __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) {
+ if (*ikey && retval != -2) {
headoff = 0;
stdstr(&key, ikey, 3);
@@ -203,6 +221,7 @@ signed char RawStr::findOffset(const char *ikey, long *start, unsigned short *si
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
}
}
@@ -216,17 +235,19 @@ signed char RawStr::findOffset(const char *ikey, long *start, unsigned short *si
idxfd->seek(tryoff, SEEK_SET);
- *start = *size = 0;
- idxfd->read(start, 4);
- idxfd->read(size, 2);
+ __u32 tmpStart;
+ __u16 tmpSize;
+ *start = *size = tmpStart = tmpSize = 0;
+ idxfd->read(&tmpStart, 4);
+ idxfd->read(&tmpSize, 2);
if (idxoff)
*idxoff = tryoff;
- *start = swordtoarch32(*start);
- *size = swordtoarch16(*size);
+ *start = swordtoarch32(tmpStart);
+ *size = swordtoarch16(tmpSize);
while (away) {
- long laststart = *start;
+ unsigned long laststart = *start;
unsigned short lastsize = *size;
long lasttry = tryoff;
tryoff += (away > 0) ? 6 : -6;
@@ -237,7 +258,8 @@ signed char RawStr::findOffset(const char *ikey, long *start, unsigned short *si
else if (idxfd->seek(tryoff, SEEK_SET) < 0)
bad = true;
if (bad) {
- retval = -1;
+ if(!awayFromSubstrCheck)
+ retval = -1;
*start = laststart;
*size = lastsize;
tryoff = lasttry;
@@ -245,15 +267,15 @@ signed char RawStr::findOffset(const char *ikey, long *start, unsigned short *si
*idxoff = tryoff;
break;
}
- idxfd->read(start, 4);
- idxfd->read(size, 2);
+ idxfd->read(&tmpStart, 4);
+ idxfd->read(&tmpSize, 2);
if (idxoff)
*idxoff = tryoff;
- *start = swordtoarch32(*start);
- *size = swordtoarch16(*size);
+ *start = swordtoarch32(tmpStart);
+ *size = swordtoarch16(tmpSize);
- if (((laststart != *start) || (lastsize != *size)) && (*start >= 0) && (*size))
+ if (((laststart != *start) || (lastsize != *size)) && (*size))
away += (away < 0) ? 1 : -1;
}
@@ -271,65 +293,6 @@ signed char RawStr::findOffset(const char *ikey, long *start, unsigned short *si
/******************************************************************************
- * RawStr::preptext - Prepares the text before returning it to external
- * objects
- *
- * ENT: buf - buffer where text is stored and where to store the prep'd
- * text.
- */
-
-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;
- space = (cr) ? 0 : 1;
- cr = 0;
- nlcnt++;
- if (nlcnt > 1) {
-// *to++ = nl;
- rawBuf[to++] = 10;
-// *to++ = nl[1];
-// nlcnt = 0;
- }
- continue;
- case 13:
- if (!realdata)
- continue;
-// *to++ = nl[0];
- rawBuf[to++] = 10;
- space = 0;
- cr = 1;
- continue;
- }
- realdata = 1;
- nlcnt = 0;
- if (space) {
- space = 0;
- if (rawBuf[from] != ' ') {
- rawBuf[to++] = ' ';
- from--;
- continue;
- }
- }
- rawBuf[to++] = rawBuf[from];
- }
- buf.setSize(to);
-
- while (to > 1) { // remove trailing excess
- to--;
- if ((rawBuf[to] == 10) || (rawBuf[to] == ' '))
- buf.setSize(to);
- else break;
- }
-}
-
-
-/******************************************************************************
* RawStr::readtext - gets text at a given offset
*
* ENT:
@@ -339,12 +302,12 @@ void RawStr::prepText(SWBuf &buf) {
*
*/
-void RawStr::readText(long istart, unsigned short *isize, char **idxbuf, SWBuf &buf)
+void RawStr::readText(__u32 istart, __u16 *isize, char **idxbuf, SWBuf &buf)
{
unsigned int ch;
char *idxbuflocal = 0;
getIDXBufDat(istart, &idxbuflocal);
- long start = istart;
+ __u32 start = istart;
do {
if (*idxbuf)
@@ -401,12 +364,12 @@ void RawStr::readText(long istart, unsigned short *isize, char **idxbuf, SWBuf &
void RawStr::doSetText(const char *ikey, const char *buf, long len)
{
- long start, outstart;
- long idxoff;
- long endoff;
- long shiftSize;
- unsigned short size;
- unsigned short outsize;
+ __u32 start, outstart;
+ __u32 idxoff;
+ __u32 endoff;
+ __s32 shiftSize;
+ __u16 size;
+ __u16 outsize;
static const char nl[] = {13, 10};
char *tmpbuf = 0;
char *key = 0;
@@ -526,7 +489,6 @@ void RawStr::doLinkEntry(const char *destkey, const char *srckey) {
delete [] text;
}
-
/******************************************************************************
* RawLD::CreateModule - Creates new module files
*