~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/modules/common/zverse.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *  zverse.h   - code for class 'zVerse'- 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
 
5
 *                              class VerseKey for compressed modules
 
6
 */
 
7
 
 
8
 
 
9
#include <ctype.h>
 
10
#include <stdio.h>
 
11
#include <fcntl.h>
 
12
#include <errno.h>
 
13
#include <stdlib.h>
 
14
 
 
15
#ifndef __GNUC__
 
16
#include <io.h>
 
17
#else
 
18
#include <unistd.h>
 
19
#endif
 
20
 
 
21
#include <utilfuns.h>
 
22
#include <versekey.h>
 
23
#include <zverse.h>
 
24
#include <sysdata.h>
 
25
#include <swbuf.h>
 
26
 
 
27
 
 
28
#ifndef O_BINARY
 
29
#define O_BINARY 0
 
30
#endif
 
31
 
 
32
SWORD_NAMESPACE_START
 
33
 
 
34
/******************************************************************************
 
35
 * zVerse Statics
 
36
 */
 
37
 
 
38
int zVerse::instance = 0;
 
39
 
 
40
const char zVerse::uniqueIndexID[] = {'X', 'r', 'v', 'c', 'b'};
 
41
 
 
42
/******************************************************************************
 
43
 * zVerse Constructor - Initializes data for instance of zVerse
 
44
 *
 
45
 * ENT: ipath - path of the directory where data and index files are located.
 
46
 *              be sure to include the trailing separator (e.g. '/' or '\')
 
47
 *              (e.g. 'modules/texts/rawtext/webster/')
 
48
 *              fileMode - open mode for the files (O_RDONLY, etc.)
 
49
 *              blockType - verse, chapter, book, etc.
 
50
 */
 
51
 
 
52
zVerse::zVerse(const char *ipath, int fileMode, int blockType, SWCompress *icomp)
 
53
{
 
54
        char buf[127];
 
55
 
 
56
        nl = '\n';
 
57
        path = 0;
 
58
        cacheBufIdx = -1;
 
59
        cacheTestament = 0;
 
60
        cacheBuf = 0;
 
61
        dirtyCache = false;
 
62
        stdstr(&path, ipath);
 
63
 
 
64
        if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
 
65
                path[strlen(path)-1] = 0;
 
66
 
 
67
        compressor = (icomp) ? icomp : new SWCompress();
 
68
 
 
69
        if (fileMode == -1) { // try read/write if possible
 
70
                fileMode = O_RDWR;
 
71
        }
 
72
                
 
73
        sprintf(buf, "%s/ot.%czs", path, uniqueIndexID[blockType]);
 
74
        idxfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
75
 
 
76
        sprintf(buf, "%s/nt.%czs", path, uniqueIndexID[blockType]);
 
77
        idxfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
78
 
 
79
        sprintf(buf, "%s/ot.%czz", path, uniqueIndexID[blockType]);
 
80
        textfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
81
 
 
82
        sprintf(buf, "%s/nt.%czz", path, uniqueIndexID[blockType]);
 
83
        textfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
84
 
 
85
        sprintf(buf, "%s/ot.%czv", path, uniqueIndexID[blockType]);
 
86
        compfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
87
 
 
88
        sprintf(buf, "%s/nt.%czv", path, uniqueIndexID[blockType]);
 
89
        compfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
90
        
 
91
        instance++;
 
92
}
 
93
 
 
94
 
 
95
/******************************************************************************
 
96
 * zVerse Destructor - Cleans up instance of zVerse
 
97
 */
 
98
 
 
99
zVerse::~zVerse()
 
100
{
 
101
        int loop1;
 
102
 
 
103
        if (cacheBuf) {
 
104
                flushCache();
 
105
                free(cacheBuf);
 
106
        }
 
107
 
 
108
        if (path)
 
109
                delete [] path;
 
110
 
 
111
        if (compressor)
 
112
                delete compressor;
 
113
 
 
114
        --instance;
 
115
 
 
116
        for (loop1 = 0; loop1 < 2; loop1++) {
 
117
                FileMgr::systemFileMgr.close(idxfp[loop1]);
 
118
                FileMgr::systemFileMgr.close(textfp[loop1]);
 
119
                FileMgr::systemFileMgr.close(compfp[loop1]);
 
120
        }
 
121
}
 
122
 
 
123
 
 
124
/******************************************************************************
 
125
 * zVerse::findoffset   - Finds the offset of the key verse from the indexes
 
126
 *
 
127
 *
 
128
 *
 
129
 * ENT: testmt  - testament to find (0 - Bible/module introduction)
 
130
 *      book    - book      to find (0 - testament    introduction)
 
131
 *      chapter - chapter   to find (0 - book         introduction)
 
132
 *      verse   - verse     to find (0 - chapter      introduction)
 
133
 *      start   - address to store the starting offset
 
134
 *      size    - address to store the size of the entry
 
135
 */
 
136
 
 
137
void zVerse::findOffset(char testmt, long idxoff, long *start, unsigned short *size)
 
138
{
 
139
        // set start to offset in
 
140
        // set size to
 
141
        // set
 
142
        unsigned long ulBuffNum=0;                // buffer number
 
143
        unsigned long ulVerseStart=0;          // verse offset within buffer
 
144
        unsigned short usVerseSize=0;          // verse size
 
145
        unsigned long ulCompOffset=0;          // compressed buffer start
 
146
        unsigned long ulCompSize=0;                  // buffer size compressed
 
147
        unsigned long ulUnCompSize=0;             // buffer size uncompressed
 
148
        char *pcCompText=NULL;                                   // compressed text
 
149
 
 
150
        *start = *size = 0;
 
151
        //printf ("Finding offset %ld\n", idxoff);
 
152
        idxoff *= 10;
 
153
        if (!testmt) {
 
154
                testmt = ((idxfp[0]) ? 1:2);
 
155
        }
 
156
        
 
157
        // assert we have and valid file descriptor
 
158
        if (compfp[testmt-1]->getFd() < 1)
 
159
                return;
 
160
                
 
161
        long newOffset = lseek(compfp[testmt-1]->getFd(), idxoff, SEEK_SET);
 
162
        if (newOffset == idxoff) {
 
163
                if (read(compfp[testmt-1]->getFd(), &ulBuffNum, 4) != 4) {
 
164
                        printf ("Error reading ulBuffNum\n");
 
165
                        return;
 
166
                }
 
167
        }
 
168
        else return;
 
169
 
 
170
        ulBuffNum = swordtoarch32(ulBuffNum);
 
171
 
 
172
        if (read(compfp[testmt-1]->getFd(), &ulVerseStart, 4) < 2)
 
173
        {
 
174
                printf ("Error reading ulVerseStart\n");
 
175
                return;
 
176
        }
 
177
        if (read(compfp[testmt-1]->getFd(), &usVerseSize, 2) < 2)
 
178
        {
 
179
                printf ("Error reading usVerseSize\n");
 
180
                return;
 
181
        }
 
182
 
 
183
        *start = swordtoarch32(ulVerseStart);
 
184
        *size = swordtoarch16(usVerseSize);
 
185
 
 
186
        if (*size) {
 
187
                if (((long) ulBuffNum == cacheBufIdx) && (testmt == cacheTestament) && (cacheBuf)) {
 
188
                        // have the text buffered
 
189
                        return;
 
190
                }
 
191
 
 
192
                //printf ("Got buffer number{%ld} versestart{%ld} versesize{%d}\n", ulBuffNum, ulVerseStart, usVerseSize);
 
193
 
 
194
 
 
195
                if (lseek(idxfp[testmt-1]->getFd(), ulBuffNum*12, SEEK_SET)!=(long) ulBuffNum*12)
 
196
                {
 
197
                        printf ("Error seeking compressed file index\n");
 
198
                        return;
 
199
                }
 
200
                if (read(idxfp[testmt-1]->getFd(), &ulCompOffset, 4)<4)
 
201
                {
 
202
                        printf ("Error reading ulCompOffset\n");
 
203
                        return;
 
204
                }
 
205
                if (read(idxfp[testmt-1]->getFd(), &ulCompSize, 4)<4)
 
206
                {
 
207
                        printf ("Error reading ulCompSize\n");
 
208
                        return;
 
209
                }
 
210
                if (read(idxfp[testmt-1]->getFd(), &ulUnCompSize, 4)<4)
 
211
                {
 
212
                        printf ("Error reading ulUnCompSize\n");
 
213
                        return;
 
214
                }
 
215
 
 
216
                ulCompOffset  = swordtoarch32(ulCompOffset);
 
217
                ulCompSize  = swordtoarch32(ulCompSize);
 
218
                ulUnCompSize  = swordtoarch32(ulUnCompSize);
 
219
 
 
220
                if (lseek(textfp[testmt-1]->getFd(), ulCompOffset, SEEK_SET)!=(long)ulCompOffset)
 
221
                {
 
222
                        printf ("Error: could not seek to right place in compressed text\n");
 
223
                        return;
 
224
                }
 
225
                SWBuf pcCompText;
 
226
                pcCompText.setSize(ulCompSize+5);
 
227
 
 
228
                if (read(textfp[testmt-1]->getFd(), pcCompText.getRawData(), ulCompSize)<(long)ulCompSize) {
 
229
                        printf ("Error reading compressed text\n");
 
230
                        return;
 
231
                }
 
232
                pcCompText.setSize(ulCompSize);
 
233
                rawZFilter(pcCompText, 0); // 0 = decipher
 
234
                
 
235
                compressor->zBuf(&ulCompSize, pcCompText.getRawData());
 
236
 
 
237
                if (cacheBuf) {
 
238
                        flushCache();
 
239
                        free(cacheBuf);
 
240
                }
 
241
                
 
242
                unsigned long len = 0;
 
243
                compressor->Buf(0, &len);
 
244
                cacheBuf = (char *)calloc(len + 1, 1);
 
245
                memcpy(cacheBuf, compressor->Buf(), len);
 
246
 
 
247
                cacheTestament = testmt;
 
248
                cacheBufIdx = ulBuffNum;
 
249
        }
 
250
}
 
251
 
 
252
 
 
253
/******************************************************************************
 
254
 * zVerse::zreadtext    - gets text at a given offset
 
255
 *
 
256
 * ENT: testmt  - testament file to search in (0 - Old; 1 - New)
 
257
 *      start   - starting offset where the text is located in the file
 
258
 *      size    - size of text entry + 1 (null)
 
259
 *      buf     - buffer to store text
 
260
 *
 
261
 */
 
262
 
 
263
void zVerse::zReadText(char testmt, long start, unsigned short size, SWBuf &inBuf) {
 
264
        inBuf = "";
 
265
        inBuf.setFillByte(0);
 
266
        inBuf.setSize(size+1);
 
267
        if (size > 0) {
 
268
                if (cacheBuf)
 
269
                        strncpy(inBuf.getRawData(), &(cacheBuf[start]), size);
 
270
        }
 
271
        inBuf.setSize(strlen(inBuf.c_str()));
 
272
}
 
273
 
 
274
 
 
275
/******************************************************************************
 
276
 * zVerse::settext      - Sets text for current offset
 
277
 *
 
278
 * ENT: testmt  - testament to find (0 - Bible/module introduction)
 
279
 *      idxoff  - offset into .vss
 
280
 *      buf     - buffer to store
 
281
 *      len     - length of buffer (0 - null terminated)
 
282
 */
 
283
 
 
284
void zVerse::doSetText(char testmt, long idxoff, const char *buf, long len) {
 
285
 
 
286
        len = (len < 0) ? strlen(buf) : len;
 
287
        if (!testmt) 
 
288
                testmt = ((idxfp[0]) ? 1:2);
 
289
        if ((!dirtyCache) || (cacheBufIdx < 0)) {
 
290
                cacheBufIdx = lseek(idxfp[testmt-1]->getFd(), 0, SEEK_END) / 12;
 
291
                cacheTestament = testmt;
 
292
                if (cacheBuf)
 
293
                        free(cacheBuf);
 
294
                cacheBuf = (char *)calloc(len + 1, 1);
 
295
        }
 
296
        else cacheBuf = (char *)((cacheBuf)?realloc(cacheBuf, strlen(cacheBuf)+(len + 1)):calloc((len + 1), 1));
 
297
 
 
298
        dirtyCache = true;
 
299
 
 
300
        unsigned long start, outstart;
 
301
        unsigned long outBufIdx = cacheBufIdx;
 
302
        unsigned short size;
 
303
        unsigned short outsize;
 
304
 
 
305
        idxoff *= 10;
 
306
        size = outsize = len;
 
307
 
 
308
        start = strlen(cacheBuf);
 
309
 
 
310
        if (!size)
 
311
                start = outBufIdx = 0;
 
312
 
 
313
        outBufIdx = archtosword32(outBufIdx);
 
314
        outstart  = archtosword32(start);
 
315
        outsize   = archtosword16(size);
 
316
 
 
317
        lseek(compfp[testmt-1]->getFd(), idxoff, SEEK_SET);
 
318
        write(compfp[testmt-1]->getFd(), &outBufIdx, 4);
 
319
        write(compfp[testmt-1]->getFd(), &outstart, 4);
 
320
        write(compfp[testmt-1]->getFd(), &outsize, 2);
 
321
        strcat(cacheBuf, buf);
 
322
}
 
323
 
 
324
 
 
325
void zVerse::flushCache() {
 
326
        if (dirtyCache) {
 
327
                unsigned long idxoff;
 
328
                unsigned long start, outstart;
 
329
                unsigned long size, outsize;
 
330
                unsigned long zsize, outzsize;
 
331
 
 
332
                idxoff = cacheBufIdx * 12;
 
333
                if (cacheBuf) {
 
334
                        size = outsize = zsize = outzsize = strlen(cacheBuf);
 
335
                        if (size) {
 
336
        //                      if (compressor) {
 
337
        //                              delete compressor;
 
338
        //                              compressor = new LZSSCompress();
 
339
        //                      }
 
340
                                compressor->Buf(cacheBuf);
 
341
                                compressor->zBuf(&zsize);
 
342
                                outzsize = zsize;
 
343
 
 
344
                                SWBuf buf;
 
345
                                buf.setSize(zsize + 5);
 
346
                                memcpy(buf.getRawData(), compressor->zBuf(&zsize), zsize);
 
347
                                buf.setSize(zsize);
 
348
                                rawZFilter(buf, 1); // 1 = encipher
 
349
 
 
350
                                start = outstart = lseek(textfp[cacheTestament-1]->getFd(), 0, SEEK_END);
 
351
 
 
352
                                outstart  = archtosword32(start);
 
353
                                outsize   = archtosword32(size);
 
354
                                outzsize  = archtosword32(zsize);
 
355
 
 
356
                                write(textfp[cacheTestament-1]->getFd(), buf, zsize);
 
357
 
 
358
                                lseek(idxfp[cacheTestament-1]->getFd(), idxoff, SEEK_SET);
 
359
                                write(idxfp[cacheTestament-1]->getFd(), &outstart, 4);
 
360
                                write(idxfp[cacheTestament-1]->getFd(), &outzsize, 4);
 
361
                                write(idxfp[cacheTestament-1]->getFd(), &outsize, 4);
 
362
                        }
 
363
                        free(cacheBuf);
 
364
                        cacheBuf = 0;
 
365
                }
 
366
                dirtyCache = false;
 
367
        }
 
368
}
 
369
 
 
370
/******************************************************************************
 
371
 * RawVerse::linkentry  - links one entry to another
 
372
 *
 
373
 * ENT: testmt  - testament to find (0 - Bible/module introduction)
 
374
 *      destidxoff      - dest offset into .vss
 
375
 *      srcidxoff               - source offset into .vss
 
376
 */
 
377
 
 
378
void zVerse::doLinkEntry(char testmt, long destidxoff, long srcidxoff) {
 
379
        long bufidx;
 
380
        long start;
 
381
        unsigned short size;
 
382
 
 
383
        destidxoff *= 10;
 
384
        srcidxoff  *= 10;
 
385
 
 
386
        if (!testmt)
 
387
                testmt = ((idxfp[1]) ? 1:2);
 
388
 
 
389
        // get source
 
390
        lseek(compfp[testmt-1]->getFd(), srcidxoff, SEEK_SET);
 
391
        read(compfp[testmt-1]->getFd(), &bufidx, 4);
 
392
        read(compfp[testmt-1]->getFd(), &start, 4);
 
393
        read(compfp[testmt-1]->getFd(), &size, 2);
 
394
 
 
395
        // write dest
 
396
        lseek(compfp[testmt-1]->getFd(), destidxoff, SEEK_SET);
 
397
        write(compfp[testmt-1]->getFd(), &bufidx, 4);
 
398
        write(compfp[testmt-1]->getFd(), &start, 4);
 
399
        write(compfp[testmt-1]->getFd(), &size, 2);
 
400
}
 
401
 
 
402
 
 
403
/******************************************************************************
 
404
 * RawVerse::CreateModule       - Creates new module files
 
405
 *
 
406
 * ENT: path    - directory to store module files
 
407
 * RET: error status
 
408
 */
 
409
 
 
410
char zVerse::createModule(const char *ipath, int blockBound)
 
411
{
 
412
        char *path = 0;
 
413
        char *buf = new char [ strlen (ipath) + 20 ];
 
414
        FileDesc *fd, *fd2;
 
415
 
 
416
        stdstr(&path, ipath);
 
417
 
 
418
        if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
 
419
                path[strlen(path)-1] = 0;
 
420
 
 
421
        sprintf(buf, "%s/ot.%czs", path, uniqueIndexID[blockBound]);
 
422
        unlink(buf);
 
423
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
424
        fd->getFd();
 
425
        FileMgr::systemFileMgr.close(fd);
 
426
 
 
427
        sprintf(buf, "%s/nt.%czs", path, uniqueIndexID[blockBound]);
 
428
        unlink(buf);
 
429
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
430
        fd->getFd();
 
431
        FileMgr::systemFileMgr.close(fd);
 
432
 
 
433
        sprintf(buf, "%s/ot.%czz", path, uniqueIndexID[blockBound]);
 
434
        unlink(buf);
 
435
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
436
        fd->getFd();
 
437
        FileMgr::systemFileMgr.close(fd);
 
438
 
 
439
        sprintf(buf, "%s/nt.%czz", path, uniqueIndexID[blockBound]);
 
440
        unlink(buf);
 
441
        fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
442
        fd2->getFd();
 
443
        FileMgr::systemFileMgr.close(fd);
 
444
 
 
445
        sprintf(buf, "%s/ot.%czv", path, uniqueIndexID[blockBound]);
 
446
        unlink(buf);
 
447
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
448
        fd->getFd();
 
449
 
 
450
        sprintf(buf, "%s/nt.%czv", path, uniqueIndexID[blockBound]);
 
451
        unlink(buf);
 
452
        fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
453
        fd2->getFd();
 
454
 
 
455
        VerseKey vk;
 
456
        vk.Headings(1);
 
457
        long offset = 0;
 
458
        short size = 0;
 
459
        for (vk = TOP; !vk.Error(); vk++) {
 
460
                write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &offset, 4);  //compBufIdxOffset
 
461
                write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &offset, 4);
 
462
                write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &size, 2);
 
463
        }
 
464
 
 
465
        FileMgr::systemFileMgr.close(fd);
 
466
        FileMgr::systemFileMgr.close(fd2);
 
467
 
 
468
        delete [] path;
 
469
        delete [] buf;
 
470
/*
 
471
        RawVerse rv(path);
 
472
        VerseKey mykey("Rev 22:21");
 
473
*/
 
474
        
 
475
        return 0;
 
476
}
 
477
 
 
478
 
 
479
/******************************************************************************
 
480
 * zVerse::preptext     - Prepares the text before returning it to external
 
481
 *                              objects
 
482
 *
 
483
 * ENT: buf     - buffer where text is stored and where to store the prep'd
 
484
 *                      text.
 
485
 */
 
486
 
 
487
void zVerse::prepText(SWBuf &buf) {
 
488
        unsigned int to, from; 
 
489
        char space = 0, cr = 0, realdata = 0, nlcnt = 0;
 
490
        char *rawBuf = buf.getRawData();
 
491
        for (to = from = 0; rawBuf[from]; from++) {
 
492
                switch (rawBuf[from]) {
 
493
                case 10:
 
494
                        if (!realdata)
 
495
                                continue;
 
496
                        space = (cr) ? 0 : 1;
 
497
                        cr = 0;
 
498
                        nlcnt++;
 
499
                        if (nlcnt > 1) {
 
500
//                              *to++ = nl;
 
501
                                rawBuf[to++] = 10;
 
502
//                              *to++ = nl[1];
 
503
//                              nlcnt = 0;
 
504
                        }
 
505
                        continue;
 
506
                case 13:
 
507
                        if (!realdata)
 
508
                                continue;
 
509
//                      *to++ = nl[0];
 
510
                        rawBuf[to++] = 10;
 
511
                        space = 0;
 
512
                        cr = 1;
 
513
                        continue;
 
514
                }
 
515
                realdata = 1;
 
516
                nlcnt = 0;
 
517
                if (space) {
 
518
                        space = 0;
 
519
                        if (rawBuf[from] != ' ') {
 
520
                                rawBuf[to++] = ' ';
 
521
                                from--;
 
522
                                continue;
 
523
                        }
 
524
                }
 
525
                rawBuf[to++] = rawBuf[from];
 
526
        }
 
527
        buf.setSize(to);
 
528
 
 
529
        while (to > 1) {                        // remove trailing excess
 
530
                to--;
 
531
                if ((rawBuf[to] == 10) || (rawBuf[to] == ' '))
 
532
                        buf.setSize(to);
 
533
                else break;
 
534
        }
 
535
}
 
536
 
 
537
 
 
538
SWORD_NAMESPACE_END