summaryrefslogtreecommitdiff
path: root/src/modules/common/zstr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/common/zstr.cpp')
-rw-r--r--src/modules/common/zstr.cpp124
1 files changed, 44 insertions, 80 deletions
diff --git a/src/modules/common/zstr.cpp b/src/modules/common/zstr.cpp
index 2539ff0..5b4da64 100644
--- a/src/modules/common/zstr.cpp
+++ b/src/modules/common/zstr.cpp
@@ -2,8 +2,25 @@
* zstr.cpp - code for class 'zStr'- a module that reads compressed text
* files 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>
@@ -111,7 +128,8 @@ zStr::~zStr() {
* buf - address of pointer to allocate for storage of string
*/
-void zStr::getKeyFromDatOffset(long ioffset, char **buf) {
+void zStr::getKeyFromDatOffset(long ioffset, char **buf) const
+{
int size;
char ch;
if (datfd > 0) {
@@ -144,12 +162,13 @@ void zStr::getKeyFromDatOffset(long ioffset, char **buf) {
* buf - address of pointer to allocate for storage of string
*/
-void zStr::getKeyFromIdxOffset(long ioffset, char **buf) {
+void zStr::getKeyFromIdxOffset(long ioffset, char **buf) const
+{
__u32 offset;
if (idxfd > 0) {
idxfd->seek(ioffset, SEEK_SET);
- idxfd->read(&offset, sizeof(__u32));
+ idxfd->read(&offset, 4);
offset = swordtoarch32(offset);
getKeyFromDatOffset(offset, buf);
}
@@ -168,12 +187,14 @@ void zStr::getKeyFromIdxOffset(long ioffset, char **buf) {
* RET: error status
*/
-signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
+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;
@@ -220,6 +241,7 @@ signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
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
}
}
@@ -234,8 +256,8 @@ signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
idxfd->seek(tryoff, SEEK_SET);
start = size = 0;
- retval = (idxfd->read(&start, sizeof(__u32))==sizeof(__u32)) ? retval : -1;
- retval = (idxfd->read(&size, sizeof(__u32))==sizeof(__u32)) ? retval : -1;
+ retval = (idxfd->read(&start, 4) == 4) ? retval : -1;
+ retval = (idxfd->read(&size, 4) == 4) ? retval : -1;
start = swordtoarch32(start);
size = swordtoarch32(size);
@@ -254,7 +276,8 @@ signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
else if (idxfd->seek(tryoff, SEEK_SET) < 0)
bad = true;
if (bad) {
- retval = -1;
+ if(!awayFromSubstrCheck)
+ retval = -1;
start = laststart;
size = lastsize;
tryoff = lasttry;
@@ -262,8 +285,8 @@ signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
*idxoff = tryoff;
break;
}
- idxfd->read(&start, sizeof(__u32));
- idxfd->read(&size, sizeof(__u32));
+ idxfd->read(&start, 4);
+ idxfd->read(&size, 4);
start = swordtoarch32(start);
size = swordtoarch32(size);
@@ -287,65 +310,6 @@ signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
/******************************************************************************
- * zStr::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 zStr::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;
- }
-}
-
-
-/******************************************************************************
* zStr::getText - gets text at a given offset
*
* ENT:
@@ -366,8 +330,8 @@ void zStr::getText(long offset, char **idxbuf, char **buf) {
do {
idxfd->seek(offset, SEEK_SET);
- idxfd->read(&start, sizeof(__u32));
- idxfd->read(&size, sizeof(__u32));
+ idxfd->read(&start, 4);
+ idxfd->read(&size, 4);
start = swordtoarch32(start);
size = swordtoarch32(size);
@@ -430,8 +394,8 @@ void zStr::getCompressedText(long block, long entry, char **buf) {
__u32 start = 0;
zdxfd->seek(block * ZDXENTRYSIZE, SEEK_SET);
- zdxfd->read(&start, sizeof(__u32));
- zdxfd->read(&size, sizeof(__u32));
+ zdxfd->read(&start, 4);
+ zdxfd->read(&size, 4);
start = swordtoarch32(start);
size = swordtoarch32(size);
@@ -496,8 +460,8 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
else if ((!diff) && (len > 0 /*we're not deleting*/)) { // got absolute entry
do {
idxfd->seek(idxoff, SEEK_SET);
- idxfd->read(&start, sizeof(__u32));
- idxfd->read(&size, sizeof(__u32));
+ idxfd->read(&start, 4);
+ idxfd->read(&size, 4);
start = swordtoarch32(start);
size = swordtoarch32(size);
@@ -581,8 +545,8 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
// add a new line to make data file easier to read in an editor
datfd->write(&nl, 2);
- idxfd->write(&outstart, sizeof(__u32));
- idxfd->write(&outsize, sizeof(__u32));
+ idxfd->write(&outstart, 4);
+ idxfd->write(&outsize, 4);
if (idxBytes) {
idxfd->write(idxBytes, shiftSize);
}
@@ -644,8 +608,8 @@ void zStr::flushCache() {
}
else {
zdxfd->seek(cacheBlockIndex * ZDXENTRYSIZE, SEEK_SET);
- zdxfd->read(&start, sizeof(__u32));
- zdxfd->read(&outsize, sizeof(__u32));
+ zdxfd->read(&start, 4);
+ zdxfd->read(&outsize, 4);
start = swordtoarch32(start);
outsize = swordtoarch32(outsize);
if (start + outsize >= zdtSize) { // last entry, just overwrite
@@ -671,8 +635,8 @@ void zStr::flushCache() {
// add a new line to make data file easier to read in an editor
zdtfd->write(&nl, 2);
- zdxfd->write(&outstart, sizeof(__u32));
- zdxfd->write(&outsize, sizeof(__u32));
+ zdxfd->write(&outstart, 4);
+ zdxfd->write(&outsize, 4);
}
delete cacheBlock;
cacheBlock = 0;