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

« back to all changes in this revision

Viewing changes to src/modules/common/rawverse.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
 *  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
 
5
 *                      class VerseKey
 
6
 */
 
7
 
 
8
 
 
9
#include <ctype.h>
 
10
#include <stdio.h>
 
11
#include <fcntl.h>
 
12
#include <errno.h>
 
13
 
 
14
#ifndef __GNUC__
 
15
#include <io.h>
 
16
#include <sys/stat.h>
 
17
#else
 
18
#include <unistd.h>
 
19
#endif
 
20
 
 
21
#include <utilfuns.h>
 
22
#include <rawverse.h>
 
23
#include <versekey.h>
 
24
#include <sysdata.h>
 
25
 
 
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.
 
29
 
 
30
SWORD_NAMESPACE_START
 
31
 
 
32
/******************************************************************************
 
33
 * RawVerse Statics
 
34
 */
 
35
 
 
36
int RawVerse::instance = 0;
 
37
const char *RawVerse::nl = "\r\n";
 
38
 
 
39
 
 
40
/******************************************************************************
 
41
 * RawVerse Constructor - Initializes data for instance of RawVerse
 
42
 *
 
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/')
 
46
 */
 
47
 
 
48
RawVerse::RawVerse(const char *ipath, int fileMode)
 
49
{
 
50
        char *buf;
 
51
 
 
52
        path = 0;
 
53
        stdstr(&path, ipath);
 
54
     buf = new char [ strlen(path) + 80 ];
 
55
        if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
 
56
                path[strlen(path)-1] = 0;
 
57
 
 
58
        if (fileMode == -1) { // try read/write if possible
 
59
                fileMode = O_RDWR;
 
60
        }
 
61
                
 
62
        sprintf(buf, "%s/ot.vss", path);
 
63
        idxfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
64
 
 
65
        sprintf(buf, "%s/nt.vss", path);
 
66
        idxfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
67
 
 
68
        sprintf(buf, "%s/ot", path);
 
69
        textfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
70
 
 
71
        sprintf(buf, "%s/nt", path);
 
72
        textfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
73
 
 
74
        delete [] buf;
 
75
        instance++;
 
76
}
 
77
 
 
78
 
 
79
/******************************************************************************
 
80
 * RawVerse Destructor - Cleans up instance of RawVerse
 
81
 */
 
82
 
 
83
RawVerse::~RawVerse()
 
84
{
 
85
        int loop1;
 
86
 
 
87
        if (path)
 
88
                delete [] path;
 
89
 
 
90
        --instance;
 
91
 
 
92
        for (loop1 = 0; loop1 < 2; loop1++) {
 
93
                FileMgr::systemFileMgr.close(idxfp[loop1]);
 
94
                FileMgr::systemFileMgr.close(textfp[loop1]);
 
95
        }
 
96
}
 
97
 
 
98
 
 
99
/******************************************************************************
 
100
 * RawVerse::findoffset - Finds the offset of the key verse from the indexes
 
101
 *
 
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
 
106
 */
 
107
 
 
108
void RawVerse::findOffset(char testmt, long idxoff, long *start, unsigned short *size) {
 
109
        idxoff *= 6;
 
110
        if (!testmt)
 
111
                testmt = ((idxfp[1]) ? 1:2);
 
112
                
 
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
 
117
 
 
118
                *start = swordtoarch32(*start);
 
119
                *size  = swordtoarch16(*size);
 
120
 
 
121
                if (len < 2) {
 
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
 
123
                }
 
124
        }
 
125
        else {
 
126
                *start = 0;
 
127
                *size = 0;
 
128
        }
 
129
}
 
130
 
 
131
 
 
132
/******************************************************************************
 
133
 * RawVerse::preptext   - Prepares the text before returning it to external
 
134
 *                              objects
 
135
 *
 
136
 * ENT: buf     - buffer where text is stored and where to store the prep'd
 
137
 *                      text.
 
138
 */
 
139
 
 
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]) {
 
146
                case 10:
 
147
                        if (!realdata)
 
148
                                continue;
 
149
                        space = (cr) ? 0 : 1;
 
150
                        cr = 0;
 
151
                        nlcnt++;
 
152
                        if (nlcnt > 1) {
 
153
//                              *to++ = nl;
 
154
                                rawBuf[to++] = 10;
 
155
//                              *to++ = nl[1];
 
156
//                              nlcnt = 0;
 
157
                        }
 
158
                        continue;
 
159
                case 13:
 
160
                        if (!realdata)
 
161
                                continue;
 
162
//                      *to++ = nl[0];
 
163
                        rawBuf[to++] = 10;
 
164
                        space = 0;
 
165
                        cr = 1;
 
166
                        continue;
 
167
                }
 
168
                realdata = 1;
 
169
                nlcnt = 0;
 
170
                if (space) {
 
171
                        space = 0;
 
172
                        if (rawBuf[from] != ' ') {
 
173
                                rawBuf[to++] = ' ';
 
174
                                from--;
 
175
                                continue;
 
176
                        }
 
177
                }
 
178
                rawBuf[to++] = rawBuf[from];
 
179
        }
 
180
        buf.setSize(to);
 
181
 
 
182
        while (to > 1) {                        // remove trailing excess
 
183
                to--;
 
184
                if ((rawBuf[to] == 10) || (rawBuf[to] == ' '))
 
185
                        buf.setSize(to);
 
186
                else break;
 
187
        }
 
188
}
 
189
 
 
190
 
 
191
/******************************************************************************
 
192
 * RawVerse::readtext   - gets text at a given offset
 
193
 *
 
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
 
198
 *
 
199
 */
 
200
 
 
201
void RawVerse::readText(char testmt, long start, unsigned short size, SWBuf &buf) {
 
202
        buf = "";
 
203
        buf.setFillByte(0);
 
204
        buf.setSize(size + 1);
 
205
        if (!testmt)
 
206
                testmt = ((idxfp[1]) ? 1:2);
 
207
        if (size) {
 
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); 
 
211
                }
 
212
        }
 
213
}
 
214
 
 
215
 
 
216
/******************************************************************************
 
217
 * RawVerse::settext    - Sets text for current offset
 
218
 *
 
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)
 
223
 */
 
224
 
 
225
void RawVerse::doSetText(char testmt, long idxoff, const char *buf, long len)
 
226
{
 
227
        long start, outstart;
 
228
        unsigned short size;
 
229
        unsigned short outsize;
 
230
 
 
231
        idxoff *= 6;
 
232
        if (!testmt)
 
233
                testmt = ((idxfp[1]) ? 1:2);
 
234
 
 
235
        size = outsize = (len < 0) ? strlen(buf) : len;
 
236
 
 
237
        start = outstart = lseek(textfp[testmt-1]->getFd(), 0, SEEK_END);
 
238
        lseek(idxfp[testmt-1]->getFd(), idxoff, SEEK_SET);
 
239
 
 
240
        if (size) {
 
241
                lseek(textfp[testmt-1]->getFd(), start, SEEK_SET);
 
242
                write(textfp[testmt-1]->getFd(), buf, (int)size);
 
243
 
 
244
                // add a new line to make data file easier to read in an editor
 
245
                write(textfp[testmt-1]->getFd(), nl, 2);
 
246
        }
 
247
        else {
 
248
                start = 0;
 
249
        }
 
250
 
 
251
        outstart = archtosword32(start);
 
252
        outsize  = archtosword16(size);
 
253
 
 
254
        write(idxfp[testmt-1]->getFd(), &outstart, 4);
 
255
        write(idxfp[testmt-1]->getFd(), &outsize, 2);
 
256
 
 
257
 
 
258
}
 
259
 
 
260
 
 
261
/******************************************************************************
 
262
 * RawVerse::linkentry  - links one entry to another
 
263
 *
 
264
 * ENT: testmt  - testament to find (0 - Bible/module introduction)
 
265
 *      destidxoff      - dest offset into .vss
 
266
 *      srcidxoff               - source offset into .vss
 
267
 */
 
268
 
 
269
void RawVerse::doLinkEntry(char testmt, long destidxoff, long srcidxoff) {
 
270
        long start;
 
271
        unsigned short size;
 
272
 
 
273
        destidxoff *= 6;
 
274
        srcidxoff  *= 6;
 
275
 
 
276
        if (!testmt)
 
277
                testmt = ((idxfp[1]) ? 1:2);
 
278
 
 
279
        // get source
 
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);
 
283
 
 
284
        // write dest
 
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);
 
288
}
 
289
 
 
290
 
 
291
/******************************************************************************
 
292
 * RawVerse::CreateModule       - Creates new module files
 
293
 *
 
294
 * ENT: path    - directory to store module files
 
295
 * RET: error status
 
296
 */
 
297
 
 
298
char RawVerse::createModule(const char *ipath)
 
299
{
 
300
        char *path = 0;
 
301
        char *buf = new char [ strlen (ipath) + 20 ];
 
302
        FileDesc *fd, *fd2;
 
303
 
 
304
        stdstr(&path, ipath);
 
305
 
 
306
        if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
 
307
                path[strlen(path)-1] = 0;
 
308
 
 
309
        sprintf(buf, "%s/ot", path);
 
310
        unlink(buf);
 
311
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
312
        fd->getFd();
 
313
        FileMgr::systemFileMgr.close(fd);
 
314
 
 
315
        sprintf(buf, "%s/nt", path);
 
316
        unlink(buf);
 
317
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
318
        fd->getFd();
 
319
        FileMgr::systemFileMgr.close(fd);
 
320
 
 
321
        sprintf(buf, "%s/ot.vss", path);
 
322
        unlink(buf);
 
323
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
324
        fd->getFd();
 
325
 
 
326
        sprintf(buf, "%s/nt.vss", path);
 
327
        unlink(buf);
 
328
        fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
329
        fd2->getFd();
 
330
 
 
331
        VerseKey vk;
 
332
        vk.Headings(1);
 
333
        long offset = 0;
 
334
        short size = 0;
 
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);
 
338
        }
 
339
 
 
340
        FileMgr::systemFileMgr.close(fd);
 
341
        FileMgr::systemFileMgr.close(fd2);
 
342
 
 
343
        delete [] path;
 
344
        delete [] buf;
 
345
/*
 
346
        RawVerse rv(path);
 
347
        VerseKey mykey("Rev 22:21");
 
348
*/
 
349
        
 
350
        return 0;
 
351
}
 
352
 
 
353
SWORD_NAMESPACE_END