File: gbfidx.cpp

package info (click to toggle)
sword 1.7.3+dfsg-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,584 kB
  • ctags: 5,515
  • sloc: cpp: 41,736; sh: 11,924; ansic: 6,447; objc: 1,362; makefile: 1,239; cs: 897; java: 821; perl: 784; pascal: 681; tcl: 350; xml: 180; python: 79
file content (342 lines) | stat: -rw-r--r-- 7,935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*****************************************************************************
 *
 *  gbfidx.cpp -	This code reeks but works (at least for WEB).
 *			Good luck!
 *
 * $Id: gbfidx.cpp 2833 2013-06-29 06:40:28Z chrislit $
 *
 * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#ifndef __GNUC__
#include <io.h>
#else
#include <unistd.h>
#endif

#include <fcntl.h>
#include <versekey.h>
#include <filemgr.h>

using namespace sword;

void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size);
char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size);
void openfiles(char *fname);
void checkparams(int argc, char **argv);


VerseKey key1, key2, key3;
int fp, vfp, cfp, bfp;
long chapoffset;
short chapsize;
char testmnt;


int main(int argc, char **argv)
{
	long pos, offset;
	int num1, num2, rangemax, w;
	char startflag = 0;
	short size;

	checkparams(argc, argv);

	openfiles(argv[1]);

	testmnt = key1.getTestament();
	num1 = key1.getChapter();
	num2 = key1.getVerse();
	pos  = 0;
	w = write(bfp, &pos, 4);  /* Book    offset for testament intros */
	if (w != 4)
		perror("ERROR: write failed in main");
	pos = 4;
	w = write(cfp, &pos, 4);  /* Chapter offset for testament intro */
	if (w != 4)
		perror("ERROR: write failed in main");


/*	Right now just zero out intros until parsing correctly */
	pos = 0;
	size = 0;
	w = write(vfp, &pos, 4);  /* Module intro */
	if (w != 4)
		perror("ERROR: write failed in main");
	w = write(vfp, &size, 2);
	if (w != 2)
		perror("ERROR: write failed in main");
	w = write(vfp, &pos, 4);  /* Testament intro */
	if (w != 4)
		perror("ERROR: write failed in main");
	w = write(vfp, &size, 2);
	if (w != 2)
		perror("ERROR: write failed in main");

	while(!findbreak(fp, &offset, &num1, &num2, &rangemax, &size)) {
		if (!startflag) {
			startflag = 1;
		}
		else {
			if (num2 < key2.getVerse()) {            // new chapter
				if (num1 <= key2.getChapter()) { // new book
					key2.setVerse(1);
					key2.setChapter(1);
					key2.setBook(key2.getBook()+1);
				}
				printf("Found Chapter Break: %d ('%s')\n", num1, (const char *)key2);
				chapoffset = offset;
				chapsize = size;
//				continue;
			}
		}
		key2.setVerse(1);
		key2.setChapter(num1);
		key2.setVerse(num2);

		key3 = key2;
//		key3 += (rangemax - key3.getVerse());

		writeidx(key1, key2, key3, offset, size);
	}
	close(vfp);
	close(cfp);
	close(bfp);
	close(fp);
	return 0;
}


/**************************************************************************
 * ENT:	key1	- current location of index
 * 	key2	- minimum keyval for which this offset is valid
 * 	key3	- maximum keyval for which this offset is valid
 */

void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
{
	int w;
	long pos;
	short tmp;

	for (; ((key1 <= key3) && (key1.popError() != KEYERR_OUTOFBOUNDS) && (key1.getTestament() == testmnt)); key1+=1) {
		if (key1.getVerse() == 1) {	// new chapter
			if (key1.getChapter() == 1) {	// new book
				pos = lseek(cfp, 0, SEEK_CUR);
				w = write(bfp, &pos, 4);
				if (w != 2)
					perror("ERROR: write failed in writeidx");
				pos = lseek(vfp, 0, SEEK_CUR); /* Book intro (cps) */
				w = write(cfp, &pos, 4);
				if (w != 2)
					perror("ERROR: write failed in writeidx");
				w = 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)*/
				if (w != 2)
					perror("ERROR: write failed in writeidx");
				w = write(vfp, &chapsize, 2);
				if (w != 2)
					perror("ERROR: write failed in writeidx");
			}
			pos = lseek(vfp, 0, SEEK_CUR);
			w = write(cfp, &pos, 4);
			if (w != 4)
				perror("ERROR: write failed in writeidx");
			w = write(vfp, &chapoffset, 4);  /* Chapter intro */
			if (w != 4)
				perror("ERROR: write failed in writeidx");
			w = write(vfp, &chapsize, 2);
			if (w != 2)
				perror("ERROR: write failed in writeidx");
		}
		if (key1 >= key2) {
			w = write(vfp, &offset, 4);
			if (w != 4)
				perror("ERROR: write failed in writeidx");
			w = write(vfp, &size, 2);
			if (w != 2)
				perror("ERROR: write failed in writeidx");
		}
		else	{
			pos = 0;
			tmp = 0;
			w = write(vfp, &pos, 4);
			if (w != 4)
				perror("ERROR: write failed in writeidx");
			w = write(vfp, &tmp, 2);
			if (w != 2)
				perror("ERROR: write failed in writeidx");
		}
	}
}


char startchap(char *buf)
{
	if (buf[0] != '<')
		return 0;
	if (buf[1] != 'S')
		return 0;
	if (buf[2] != 'C')
		return 0;
/*
	if (!isdigit(buf[2]))
		return 0;
	for (loop = 3; loop < 7; loop++) {
		if (buf[loop] == ' ')
			break;
		if ((!isdigit(buf[loop])) && (buf[loop] != ',') && (buf[loop] != '-'))
			return 0;
	}
*/
	return 1;
}


char startentry(char *buf)
{
	if (buf[0] != '<')
		return 0;
	if (buf[1] != 'S')
		return 0;
	if (buf[2] != 'V')
		return 0;
/*
	if (!isdigit(buf[2]))
		return 0;
	for (loop = 3; loop < 7; loop++) {
		if (buf[loop] == ' ')
			break;
		if ((!isdigit(buf[loop])) && (buf[loop] != ',') && (buf[loop] != '-'))
			return 0;
	}
*/
	return 1;
}


char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
{
	char buf[7];
	int loop;
	long offset2;
	int ch2, vs2, rm2;
	bool flag;
	long chapstart = 0; 
	
	memset(buf, ' ', 7);

	while (1) {
		if (startchap(buf)) {
			chapstart = lseek(fp, 0, SEEK_CUR) - 7;
			memset(buf, ' ', 3);
			flag = false;
			for (loop = 3; loop < 6; loop++) {
				if (isdigit(buf[loop]))
					flag = true;
				else {
					buf[loop] = 0;
					break;
				}
			}
			if (flag) 
				*num1 = atoi(buf);
			else	(*num1)++;
		}
		if (startentry(buf)) {
			memset(buf, ' ', 3);
			flag = false;
			for (loop = 3; loop < 6; loop++) {
				if (isdigit(buf[loop]))
					flag = true;
				else {
					buf[loop] = 0;
					break;
				}
			if (flag)
				*num2 = atoi(buf);
			else	(*num2)++;
			}
			loop++;
			if (size)
				*offset = lseek(fp, 0, SEEK_CUR) - (7 - loop);
			else	*offset = (chapstart) ? chapstart : lseek(fp, 0, SEEK_CUR) - 7;
			if (size) {
				ch2 = *num1;
				vs2 = *num2;
				if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
					*size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
				}
				else {
					if (vs2) {
						*size = (offset2 - (*offset));
					}
				}
				lseek(fp, *offset, SEEK_SET);
			}
			return 0;
		}
		memmove(buf, &buf[1], 6);
		if (read(fp, &buf[6], 1) != 1)
			return 1;
	}
}


void openfiles(char *fname)
{
	SWBuf buf;

	if ((fp = FileMgr::openFileReadOnly(fname)) < 0) {
		fprintf(stderr, "Couldn't open file: %s\n", fname);
		exit(1);
	}

	buf.setFormatted("%s.vss", fname);
	if ((vfp = FileMgr::createPathAndFile(buf.c_str())) < 0) {
		fprintf(stderr, "Couldn't open file: %s\n", buf.c_str());
		exit(1);
	}

	buf.setFormatted("%s.cps", fname);
	if ((cfp = FileMgr::createPathAndFile(buf.c_str())) < 0) {
		fprintf(stderr, "Couldn't open file: %s\n", buf.c_str());
		exit(1);
	}

	buf.setFormatted("%s.bks", fname);
	if ((bfp = FileMgr::createPathAndFile(buf.c_str())) < 0) {
		fprintf(stderr, "Couldn't open file: %s\n", buf.c_str());
		exit(1);
	}
}


void checkparams(int argc, char **argv)
{
	if (argc < 2) {
		fprintf(stderr, "usage: %s <file to process> [nt - for new testmt file]\n", argv[0]);
		exit(1);
	}
	if (argc == 3)
		key1 = key2 = key3 = "Matthew 1:1";
	else	key1 = key2 = key3 = "Genesis 1:1";
}