1
/******************************************************************************
2
* rawverse.cpp - code for class 'RawVerse'- a module that reads raw text
3
* files: ot and nt using indexs ??.bks ??.cps ??.vss
4
* and provides lookup and parsing functions based on
26
#ifndef O_BINARY // O_BINARY is needed in Borland C++ 4.53
27
#define O_BINARY 0 // If it hasn't been defined than we probably
28
#endif // don't need it.
32
/******************************************************************************
36
int RawVerse::instance = 0;
37
const char *RawVerse::nl = "\r\n";
40
/******************************************************************************
41
* RawVerse Constructor - Initializes data for instance of RawVerse
43
* ENT: ipath - path of the directory where data and index files are located.
44
* be sure to include the trailing separator (e.g. '/' or '\')
45
* (e.g. 'modules/texts/rawtext/webster/')
48
RawVerse::RawVerse(const char *ipath, int fileMode)
54
buf = new char [ strlen(path) + 80 ];
55
if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
56
path[strlen(path)-1] = 0;
58
if (fileMode == -1) { // try read/write if possible
62
sprintf(buf, "%s/ot.vss", path);
63
idxfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
65
sprintf(buf, "%s/nt.vss", path);
66
idxfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
68
sprintf(buf, "%s/ot", path);
69
textfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
71
sprintf(buf, "%s/nt", path);
72
textfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
79
/******************************************************************************
80
* RawVerse Destructor - Cleans up instance of RawVerse
92
for (loop1 = 0; loop1 < 2; loop1++) {
93
FileMgr::systemFileMgr.close(idxfp[loop1]);
94
FileMgr::systemFileMgr.close(textfp[loop1]);
99
/******************************************************************************
100
* RawVerse::findoffset - Finds the offset of the key verse from the indexes
102
* ENT: testmt - testament to find (0 - Bible/module introduction)
103
* idxoff - offset into .vss
104
* start - address to store the starting offset
105
* size - address to store the size of the entry
108
void RawVerse::findOffset(char testmt, long idxoff, long *start, unsigned short *size) {
111
testmt = ((idxfp[1]) ? 1:2);
113
if (idxfp[testmt-1]->getFd() >= 0) {
114
lseek(idxfp[testmt-1]->getFd(), idxoff, SEEK_SET);
115
read(idxfp[testmt-1]->getFd(), start, 4);
116
long len = read(idxfp[testmt-1]->getFd(), size, 2); // read size
118
*start = swordtoarch32(*start);
119
*size = swordtoarch16(*size);
122
*size = (unsigned short)((*start) ? (lseek(textfp[testmt-1]->getFd(), 0, SEEK_END) - (long)*start) : 0); // if for some reason we get an error reading size, make size to end of file
132
/******************************************************************************
133
* RawVerse::preptext - Prepares the text before returning it to external
136
* ENT: buf - buffer where text is stored and where to store the prep'd
140
void RawVerse::prepText(SWBuf &buf) {
141
unsigned int to, from;
142
char space = 0, cr = 0, realdata = 0, nlcnt = 0;
143
char *rawBuf = buf.getRawData();
144
for (to = from = 0; rawBuf[from]; from++) {
145
switch (rawBuf[from]) {
149
space = (cr) ? 0 : 1;
172
if (rawBuf[from] != ' ') {
178
rawBuf[to++] = rawBuf[from];
182
while (to > 1) { // remove trailing excess
184
if ((rawBuf[to] == 10) || (rawBuf[to] == ' '))
191
/******************************************************************************
192
* RawVerse::readtext - gets text at a given offset
194
* ENT: testmt - testament file to search in (0 - Old; 1 - New)
195
* start - starting offset where the text is located in the file
196
* size - size of text entry + 2 (null)(null)
197
* buf - buffer to store text
201
void RawVerse::readText(char testmt, long start, unsigned short size, SWBuf &buf) {
204
buf.setSize(size + 1);
206
testmt = ((idxfp[1]) ? 1:2);
208
if (textfp[testmt-1]->getFd() >= 0) {
209
lseek(textfp[testmt-1]->getFd(), start, SEEK_SET);
210
read(textfp[testmt-1]->getFd(), buf.getRawData(), (int)size);
216
/******************************************************************************
217
* RawVerse::settext - Sets text for current offset
219
* ENT: testmt - testament to find (0 - Bible/module introduction)
220
* idxoff - offset into .vss
221
* buf - buffer to store
222
* len - length of buffer (0 - null terminated)
225
void RawVerse::doSetText(char testmt, long idxoff, const char *buf, long len)
227
long start, outstart;
229
unsigned short outsize;
233
testmt = ((idxfp[1]) ? 1:2);
235
size = outsize = (len < 0) ? strlen(buf) : len;
237
start = outstart = lseek(textfp[testmt-1]->getFd(), 0, SEEK_END);
238
lseek(idxfp[testmt-1]->getFd(), idxoff, SEEK_SET);
241
lseek(textfp[testmt-1]->getFd(), start, SEEK_SET);
242
write(textfp[testmt-1]->getFd(), buf, (int)size);
244
// add a new line to make data file easier to read in an editor
245
write(textfp[testmt-1]->getFd(), nl, 2);
251
outstart = archtosword32(start);
252
outsize = archtosword16(size);
254
write(idxfp[testmt-1]->getFd(), &outstart, 4);
255
write(idxfp[testmt-1]->getFd(), &outsize, 2);
261
/******************************************************************************
262
* RawVerse::linkentry - links one entry to another
264
* ENT: testmt - testament to find (0 - Bible/module introduction)
265
* destidxoff - dest offset into .vss
266
* srcidxoff - source offset into .vss
269
void RawVerse::doLinkEntry(char testmt, long destidxoff, long srcidxoff) {
277
testmt = ((idxfp[1]) ? 1:2);
280
lseek(idxfp[testmt-1]->getFd(), srcidxoff, SEEK_SET);
281
read(idxfp[testmt-1]->getFd(), &start, 4);
282
read(idxfp[testmt-1]->getFd(), &size, 2);
285
lseek(idxfp[testmt-1]->getFd(), destidxoff, SEEK_SET);
286
write(idxfp[testmt-1]->getFd(), &start, 4);
287
write(idxfp[testmt-1]->getFd(), &size, 2);
291
/******************************************************************************
292
* RawVerse::CreateModule - Creates new module files
294
* ENT: path - directory to store module files
298
char RawVerse::createModule(const char *ipath)
301
char *buf = new char [ strlen (ipath) + 20 ];
304
stdstr(&path, ipath);
306
if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
307
path[strlen(path)-1] = 0;
309
sprintf(buf, "%s/ot", path);
311
fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
313
FileMgr::systemFileMgr.close(fd);
315
sprintf(buf, "%s/nt", path);
317
fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
319
FileMgr::systemFileMgr.close(fd);
321
sprintf(buf, "%s/ot.vss", path);
323
fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
326
sprintf(buf, "%s/nt.vss", path);
328
fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
335
for (vk = TOP; !vk.Error(); vk++) {
336
write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &offset, 4);
337
write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &size, 2);
340
FileMgr::systemFileMgr.close(fd);
341
FileMgr::systemFileMgr.close(fd2);
347
VerseKey mykey("Rev 22:21");