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

« back to all changes in this revision

Viewing changes to src/modules/comments/rawcom/mhcidx.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
 *
 
3
 *      This code wreaks but works (at least for MHC).  Good luck!
 
4
 */
 
5
 
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <string.h>
 
9
#include <ctype.h>
 
10
 
 
11
#ifndef __GNUC__
 
12
#include <io.h>
 
13
#else
 
14
#include <unistd.h>
 
15
#endif
 
16
 
 
17
#include <fcntl.h>
 
18
#include <versekey.h>
 
19
 
 
20
 
 
21
void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size);
 
22
char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size);
 
23
void openfiles(char *fname);
 
24
void checkparams(int argc, char **argv);
 
25
 
 
26
 
 
27
VerseKey key1, key2, key3;
 
28
int fp, vfp, cfp, bfp;
 
29
long chapoffset;
 
30
short chapsize;
 
31
char testmnt;
 
32
 
 
33
 
 
34
main(int argc, char **argv)
 
35
{
 
36
        long pos, offset;
 
37
        int num1, num2, rangemax, curbook = 0, curchap = 0, curverse = 0;
 
38
        char buf[127], startflag = 0;
 
39
        short size, tmp;
 
40
 
 
41
        checkparams(argc, argv);
 
42
 
 
43
        openfiles(argv[1]);
 
44
 
 
45
        testmnt = key1.Testament();
 
46
        pos  = 0;
 
47
        write(bfp, &pos, 4);  /* Book    offset for testament intros */
 
48
        pos = 4;
 
49
        write(cfp, &pos, 4);  /* Chapter offset for testament intro */
 
50
 
 
51
 
 
52
/*      Right now just zero out intros until parsing correctly */
 
53
        pos = 0;
 
54
        size = 0;
 
55
        write(vfp, &pos, 4);  /* Module intro */
 
56
        write(vfp, &size, 2);
 
57
        write(vfp, &pos, 4);  /* Testament intro */
 
58
        write(vfp, &size, 2);
 
59
 
 
60
        while(!findbreak(fp, &offset, &num1, &num2, &rangemax, &size)) {
 
61
                if (num2) {                             
 
62
                        key2.Verse(1);
 
63
                        key2.Chapter(num1);
 
64
                        key2.Verse(num2);
 
65
                }
 
66
                else {
 
67
                        key2.Verse(1);
 
68
                        if (!startflag) {
 
69
                                startflag = 1;
 
70
                        }
 
71
                        else {
 
72
                                if (num1 <= key2.Chapter()) { // new book
 
73
                                        key2.Chapter(1);
 
74
                                        key2.Book(key2.Book()+1);
 
75
                                }
 
76
                        }
 
77
                        key2.Chapter(num1);
 
78
                        printf("Found Chapter Break: %d ('%s')\n", num1, (const char *)key2);
 
79
                        chapoffset = offset;
 
80
                        chapsize = size;
 
81
                        continue;
 
82
                }
 
83
 
 
84
                key3 = key2;
 
85
                key3 += (rangemax - key3.Verse());
 
86
 
 
87
                writeidx(key1, key2, key3, offset, size);
 
88
        }
 
89
        close(vfp);
 
90
        close(cfp);
 
91
        close(bfp);
 
92
        close(fp);
 
93
}
 
94
 
 
95
 
 
96
/**************************************************************************
 
97
 * ENT: key1    - current location of index
 
98
 *      key2    - minimum keyval for which this offset is valid
 
99
 *      key3    - maximum keyval for which this offset is valid
 
100
 */
 
101
 
 
102
void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
 
103
{
 
104
        long pos;
 
105
        short tmp;
 
106
 
 
107
        for (; ((key1 <= key3) && (key1.Error() != KEYERR_OUTOFBOUNDS) && (key1.Testament() == testmnt)); key1+=1) {
 
108
                if (key1.Verse() == 1) {        // new chapter
 
109
                        if (key1.Chapter() == 1) {      // new book
 
110
                                pos = lseek(cfp, 0, SEEK_CUR);
 
111
                                write(bfp, &pos, 4);
 
112
                                pos = lseek(vfp, 0, SEEK_CUR); /* Book intro (cps) */
 
113
                                write(cfp, &pos, 4);
 
114
                                write(vfp, &chapoffset, 4);  /* Book intro (vss)  set to same as chap for now(it should be chap 1 which usually contains the book into anyway)*/
 
115
                                write(vfp, &chapsize, 2);
 
116
                        }
 
117
                        pos = lseek(vfp, 0, SEEK_CUR);
 
118
                        write(cfp, &pos, 4);
 
119
                        write(vfp, &chapoffset, 4);  /* Chapter intro */
 
120
                        write(vfp, &chapsize, 2);
 
121
                }
 
122
                if (key1 >= key2) {
 
123
                        write(vfp, &offset, 4);
 
124
                        write(vfp, &size, 2);
 
125
                }
 
126
                else    {
 
127
                        pos = 0;
 
128
                        tmp = 0;
 
129
                        write(vfp, &pos, 4);
 
130
                        write(vfp, &tmp, 2);
 
131
                }
 
132
        }
 
133
}
 
134
 
 
135
 
 
136
char startentry(char *buf)
 
137
{
 
138
        char loop;
 
139
 
 
140
        if (buf[0] != 10)
 
141
                return 0;
 
142
        if (buf[1] != '#')
 
143
                return 0;
 
144
        if (!isdigit(buf[2]))
 
145
                return 0;
 
146
        for (loop = 3; loop < 7; loop++) {
 
147
                if (buf[loop] == ' ')
 
148
                        break;
 
149
                if ((!isdigit(buf[loop])) && (buf[loop] != ',') && (buf[loop] != '-'))
 
150
                        return 0;
 
151
        }
 
152
        return 1;
 
153
}
 
154
 
 
155
 
 
156
char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
 
157
{
 
158
        char buf[7];
 
159
        char buf2[20];
 
160
        char ch;
 
161
        char loop;
 
162
        long offset2;
 
163
        int ch2, vs2, rm2;
 
164
        
 
165
        memset(buf, ' ', 7);
 
166
 
 
167
        while (1) {
 
168
                if (startentry(buf)) {
 
169
                        memset(buf, ' ', 2);
 
170
                        for (loop = 2; loop < 7; loop++) {
 
171
                                if ((buf[loop] == '-') || (buf[loop] == ',') || (buf[loop] == ' ')) {
 
172
                                        buf[loop] = 0;
 
173
                                        *num2 = atoi(buf);
 
174
                                        break;
 
175
                                }
 
176
                        }
 
177
                        for (ch = loop + 1; ch < 7; ch++) {
 
178
                                if (buf[ch] == ' ') {
 
179
                                        break;
 
180
                                }
 
181
                        }
 
182
                        buf[ch] = 0;
 
183
                        *rangemax = atoi(&buf[loop+1]);
 
184
                        if (!*rangemax)
 
185
                                *rangemax = *num2;
 
186
                        *offset = lseek(fp, 0, SEEK_CUR) - 5;
 
187
                        if (size) {
 
188
                                if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
 
189
                                        *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
 
190
                                }
 
191
                                else {
 
192
                                        if (vs2) {
 
193
                                                *size = (offset2 - (*offset)) - 3;
 
194
                                        }
 
195
                                        else {
 
196
                                                sprintf(buf2, "$-$-$- XX:%d", ch2);
 
197
                                                *size = (offset2 - (*offset)) - ((strlen(buf2) + 4));
 
198
                                        }
 
199
                                }
 
200
                                lseek(fp, *offset, SEEK_SET);
 
201
                        }
 
202
                        return 0;
 
203
                }
 
204
                                        
 
205
                if (!strncmp(buf, "$-$-$-", 6)) {
 
206
                        memset(buf2, 0, 7);
 
207
                        loop = 0;
 
208
                        while ((read(fp, &buf2[loop], 1) == 1) && (loop < 7)) {
 
209
                                if ((buf2[loop] == 10) || (buf2[loop] == 13)) {
 
210
                                        buf2[loop] = 0;
 
211
                                        break;
 
212
                                }
 
213
                                loop++;
 
214
                        }
 
215
                        while (read(fp, &ch, 1) == 1) {
 
216
                                if (ch == '*')
 
217
                                        break;
 
218
                        }
 
219
                        
 
220
                        *offset = lseek(fp, 0, SEEK_CUR) - 1;
 
221
                        *num2 = 0;
 
222
                        for (loop = strlen(buf2) - 1; loop; loop--) {
 
223
                                if (buf2[loop] == ':')
 
224
                                        break;
 
225
                        }
 
226
                        *num1 = atoi(&buf2[loop+1]);
 
227
                        printf("Chapter marker: %s\n", buf2);
 
228
                        if (size) {
 
229
                                if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
 
230
                                        *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
 
231
                                }
 
232
                                else {
 
233
                                        if (vs2) {
 
234
                                                *size = (offset2 - (*offset)) - 3;
 
235
                                        }
 
236
                                        else {
 
237
                                                sprintf(buf2, "$-$-$- XX:%d", ch2);
 
238
                                                *size = (offset2 - (*offset)) - ((strlen(buf2) + 4));
 
239
                                        }
 
240
                                }
 
241
                                lseek(fp, *offset, SEEK_SET);
 
242
                        }
 
243
                        return 0;
 
244
                }
 
245
                                 
 
246
                                
 
247
                memmove(buf, &buf[1], 6);
 
248
                if (read(fp, &buf[6], 1) != 1)
 
249
                        return 1;
 
250
        }
 
251
}
 
252
 
 
253
 
 
254
void openfiles(char *fname)
 
255
{
 
256
        char buf[255];
 
257
 
 
258
        if ((fp = open(fname, O_RDONLY)) == -1) {
 
259
                fprintf(stderr, "Couldn't open file: %s\n", fname);
 
260
                exit(1);
 
261
        }
 
262
 
 
263
        sprintf(buf, "%s.vss", fname);
 
264
        if ((vfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
 
265
                fprintf(stderr, "Couldn't open file: %s\n", buf);
 
266
                exit(1);
 
267
        }
 
268
 
 
269
        sprintf(buf, "%s.cps", fname);
 
270
        if ((cfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
 
271
                fprintf(stderr, "Couldn't open file: %s\n", buf);
 
272
                exit(1);
 
273
        }
 
274
 
 
275
        sprintf(buf, "%s.bks", fname);
 
276
        if ((bfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
 
277
                fprintf(stderr, "Couldn't open file: %s\n", buf);
 
278
                exit(1);
 
279
        }
 
280
}
 
281
 
 
282
 
 
283
void checkparams(int argc, char **argv)
 
284
{
 
285
        if (argc < 2) {
 
286
                fprintf(stderr, "usage: %s <file to process> [nt - for new testmt file]\n", argv[0]);
 
287
                exit(1);
 
288
        }
 
289
        if (argc == 3)
 
290
                key1 = key2 = key3 = "Matthew 1:1";
 
291
        else    key1 = key2 = key3 = "Genesis 1:1";
 
292
}