1
/******************************************************************************
2
* rawverse.cpp - code for class 'RawVerse4'- 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
8
* Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
9
* CrossWire Bible Society
11
* Tempe, AZ 85280-2528
13
* This program is free software; you can redistribute it and/or modify it
14
* under the terms of the GNU General Public License as published by the
15
* Free Software Foundation version 2.
17
* This program is distributed in the hope that it will be useful, but
18
* WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
* General Public License for more details.
32
#include <rawverse4.h>
41
/******************************************************************************
45
int RawVerse4::instance = 0;
46
const char *RawVerse4::nl = "\r\n";
49
/******************************************************************************
50
* RawVerse4 Constructor - Initializes data for instance of RawVerse4
52
* ENT: ipath - path of the directory where data and index files are located.
53
* be sure to include the trailing separator (e.g. '/' or '\')
54
* (e.g. 'modules/texts/rawtext/webster/')
57
RawVerse4::RawVerse4(const char *ipath, int fileMode)
64
if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
65
path[strlen(path)-1] = 0;
67
if (fileMode == -1) { // try read/write if possible
68
fileMode = FileMgr::RDWR;
71
buf.setFormatted("%s/ot.vss", path);
72
idxfp[0] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
74
buf.setFormatted("%s/nt.vss", path);
75
idxfp[1] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
77
buf.setFormatted("%s/ot", path);
78
textfp[0] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
80
buf.setFormatted("%s/nt", path);
81
textfp[1] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
87
/******************************************************************************
88
* RawVerse4 Destructor - Cleans up instance of RawVerse4
91
RawVerse4::~RawVerse4()
100
for (loop1 = 0; loop1 < 2; loop1++) {
101
FileMgr::getSystemFileMgr()->close(idxfp[loop1]);
102
FileMgr::getSystemFileMgr()->close(textfp[loop1]);
107
/******************************************************************************
108
* RawVerse4::findoffset - Finds the offset of the key verse from the indexes
110
* ENT: testmt - testament to find (0 - Bible/module introduction)
111
* idxoff - offset into .vss
112
* start - address to store the starting offset
113
* size - address to store the size of the entry
116
void RawVerse4::findOffset(char testmt, long idxoff, long *start, unsigned long *size) const {
119
testmt = ((idxfp[1]) ? 1:2);
121
if (idxfp[testmt-1]->getFd() >= 0) {
122
idxfp[testmt-1]->seek(idxoff, SEEK_SET);
125
idxfp[testmt-1]->read(&tmpStart, 4);
126
long len = idxfp[testmt-1]->read(&tmpSize, 4); // read size
128
*start = swordtoarch32(tmpStart);
129
*size = swordtoarch32(tmpSize);
132
*size = (unsigned long)((*start) ? (textfp[testmt-1]->seek(0, SEEK_END) - (long)*start) : 0); // if for some reason we get an error reading size, make size to end of file
142
/******************************************************************************
143
* RawVerse4::readtext - gets text at a given offset
145
* ENT: testmt - testament file to search in (0 - Old; 1 - New)
146
* start - starting offset where the text is located in the file
147
* size - size of text entry + 2 (null)(null)
148
* buf - buffer to store text
152
void RawVerse4::readText(char testmt, long start, unsigned long size, SWBuf &buf) {
155
buf.setSize(size + 1);
157
testmt = ((idxfp[1]) ? 1:2);
159
if (textfp[testmt-1]->getFd() >= 0) {
160
textfp[testmt-1]->seek(start, SEEK_SET);
161
textfp[testmt-1]->read(buf.getRawData(), (int)size);
167
/******************************************************************************
168
* RawVerse4::settext - Sets text for current offset
170
* ENT: testmt - testament to find (0 - Bible/module introduction)
171
* idxoff - offset into .vss
172
* buf - buffer to store
173
* len - length of buffer (0 - null terminated)
176
void RawVerse4::doSetText(char testmt, long idxoff, const char *buf, long len)
183
testmt = ((idxfp[1]) ? 1:2);
185
size = (len < 0) ? strlen(buf) : len;
187
start = textfp[testmt-1]->seek(0, SEEK_END);
188
idxfp[testmt-1]->seek(idxoff, SEEK_SET);
191
textfp[testmt-1]->seek(start, SEEK_SET);
192
textfp[testmt-1]->write(buf, (int)size);
194
// add a new line to make data file easier to read in an editor
195
textfp[testmt-1]->write(nl, 2);
201
start = archtosword32(start);
202
size = archtosword32(size);
204
idxfp[testmt-1]->write(&start, 4);
205
idxfp[testmt-1]->write(&size, 4);
209
/******************************************************************************
210
* RawVerse4::linkentry - links one entry to another
212
* ENT: testmt - testament to find (0 - Bible/module introduction)
213
* destidxoff - dest offset into .vss
214
* srcidxoff - source offset into .vss
217
void RawVerse4::doLinkEntry(char testmt, long destidxoff, long srcidxoff) {
225
testmt = ((idxfp[1]) ? 1:2);
228
idxfp[testmt-1]->seek(srcidxoff, SEEK_SET);
229
idxfp[testmt-1]->read(&start, 4);
230
idxfp[testmt-1]->read(&size, 4);
233
idxfp[testmt-1]->seek(destidxoff, SEEK_SET);
234
idxfp[testmt-1]->write(&start, 4);
235
idxfp[testmt-1]->write(&size, 4);
239
/******************************************************************************
240
* RawVerse4::CreateModule - Creates new module files
242
* ENT: path - directory to store module files
246
char RawVerse4::createModule(const char *ipath, const char *v11n)
249
char *buf = new char [ strlen (ipath) + 20 ];
252
stdstr(&path, ipath);
254
if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
255
path[strlen(path)-1] = 0;
257
sprintf(buf, "%s/ot", path);
258
FileMgr::removeFile(buf);
259
fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
261
FileMgr::getSystemFileMgr()->close(fd);
263
sprintf(buf, "%s/nt", path);
264
FileMgr::removeFile(buf);
265
fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
267
FileMgr::getSystemFileMgr()->close(fd);
269
sprintf(buf, "%s/ot.vss", path);
270
FileMgr::removeFile(buf);
271
fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
274
sprintf(buf, "%s/nt.vss", path);
275
FileMgr::removeFile(buf);
276
fd2 = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
280
vk.setVersificationSystem(v11n);
284
offset = archtosword32(offset);
285
size = archtosword32(size);
287
for (vk = TOP; !vk.Error(); vk++) {
288
if (vk.Testament() < 2) {
289
fd->write(&offset, 4);
293
fd2->write(&offset, 4);
294
fd2->write(&size, 4);
297
fd2->write(&offset, 4);
298
fd2->write(&size, 4);
300
FileMgr::getSystemFileMgr()->close(fd);
301
FileMgr::getSystemFileMgr()->close(fd2);