The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gbfidx.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * gbfidx.cpp - This code reeks but works (at least for WEB).
4  * Good luck!
5  *
6  * $Id: gbfidx.cpp 3063 2014-03-04 13:04:11Z chrislit $
7  *
8  * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
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.
16  *
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.
21  *
22  */
23 
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 
30 #ifndef __GNUC__
31 #include <io.h>
32 #else
33 #include <unistd.h>
34 #endif
35 
36 #include <fcntl.h>
37 #include <versekey.h>
38 #include <filemgr.h>
39 
40 using namespace sword;
41 
42 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size);
43 char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size);
44 void openfiles(char *fname);
45 void checkparams(int argc, char **argv);
46 
47 
48 VerseKey key1, key2, key3;
49 int fp, vfp, cfp, bfp;
51 short chapsize;
52 char testmnt;
53 
54 
55 int main(int argc, char **argv)
56 {
57  long pos, offset;
58  int num1, num2, rangemax;
59  char startflag = 0;
60  short size;
61 
62  checkparams(argc, argv);
63 
64  openfiles(argv[1]);
65 
66  testmnt = key1.getTestament();
67  num1 = key1.getChapter();
68  num2 = key1.getVerse();
69  pos = 0;
70  write(bfp, &pos, 4); /* Book offset for testament intros */
71  pos = 4;
72  write(cfp, &pos, 4); /* Chapter offset for testament intro */
73 
74 
75 /* Right now just zero out intros until parsing correctly */
76  pos = 0;
77  size = 0;
78  write(vfp, &pos, 4); /* Module intro */
79  write(vfp, &size, 2);
80  write(vfp, &pos, 4); /* Testament intro */
81  write(vfp, &size, 2);
82 
83  while(!findbreak(fp, &offset, &num1, &num2, &rangemax, &size)) {
84  if (!startflag) {
85  startflag = 1;
86  }
87  else {
88  if (num2 < key2.getVerse()) { // new chapter
89  if (num1 <= key2.getChapter()) { // new book
90  key2.setVerse(1);
91  key2.setChapter(1);
92  key2.setBook(key2.getBook()+1);
93  }
94  printf("Found Chapter Break: %d ('%s')\n", num1, (const char *)key2);
95  chapoffset = offset;
96  chapsize = size;
97 // continue;
98  }
99  }
100  key2.setVerse(1);
101  key2.setChapter(num1);
102  key2.setVerse(num2);
103 
104  key3 = key2;
105 // key3 += (rangemax - key3.getVerse());
106 
107  writeidx(key1, key2, key3, offset, size);
108  }
109  close(vfp);
110  close(cfp);
111  close(bfp);
112  close(fp);
113  return 0;
114 }
115 
116 
117 /**************************************************************************
118  * ENT: key1 - current location of index
119  * key2 - minimum keyval for which this offset is valid
120  * key3 - maximum keyval for which this offset is valid
121  */
122 
123 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
124 {
125  long pos;
126  short tmp;
127 
128  for (; ((key1 <= key3) && (key1.popError() != KEYERR_OUTOFBOUNDS) && (key1.getTestament() == testmnt)); key1+=1) {
129  if (key1.getVerse() == 1) { // new chapter
130  if (key1.getChapter() == 1) { // new book
131  pos = lseek(cfp, 0, SEEK_CUR);
132  write(bfp, &pos, 4);
133  pos = lseek(vfp, 0, SEEK_CUR); /* Book intro (cps) */
134  write(cfp, &pos, 4);
135  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)*/
136  write(vfp, &chapsize, 2);
137  }
138  pos = lseek(vfp, 0, SEEK_CUR);
139  write(cfp, &pos, 4);
140  write(vfp, &chapoffset, 4); /* Chapter intro */
141  write(vfp, &chapsize, 2);
142  }
143  if (key1 >= key2) {
144  write(vfp, &offset, 4);
145  write(vfp, &size, 2);
146  }
147  else {
148  pos = 0;
149  tmp = 0;
150  write(vfp, &pos, 4);
151  write(vfp, &tmp, 2);
152  }
153  }
154 }
155 
156 
157 char startchap(char *buf)
158 {
159  if (buf[0] != '<')
160  return 0;
161  if (buf[1] != 'S')
162  return 0;
163  if (buf[2] != 'C')
164  return 0;
165 /*
166  if (!isdigit(buf[2]))
167  return 0;
168  for (loop = 3; loop < 7; loop++) {
169  if (buf[loop] == ' ')
170  break;
171  if ((!isdigit(buf[loop])) && (buf[loop] != ',') && (buf[loop] != '-'))
172  return 0;
173  }
174 */
175  return 1;
176 }
177 
178 
179 char startentry(char *buf)
180 {
181  if (buf[0] != '<')
182  return 0;
183  if (buf[1] != 'S')
184  return 0;
185  if (buf[2] != 'V')
186  return 0;
187 /*
188  if (!isdigit(buf[2]))
189  return 0;
190  for (loop = 3; loop < 7; loop++) {
191  if (buf[loop] == ' ')
192  break;
193  if ((!isdigit(buf[loop])) && (buf[loop] != ',') && (buf[loop] != '-'))
194  return 0;
195  }
196 */
197  return 1;
198 }
199 
200 
201 char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
202 {
203  char buf[7];
204  int loop;
205  long offset2;
206  int ch2, vs2, rm2;
207  bool flag;
208  long chapstart = 0;
209 
210  memset(buf, ' ', 7);
211 
212  while (1) {
213  if (startchap(buf)) {
214  chapstart = lseek(fp, 0, SEEK_CUR) - 7;
215  memset(buf, ' ', 3);
216  flag = false;
217  for (loop = 3; loop < 6; loop++) {
218  if (isdigit(buf[loop]))
219  flag = true;
220  else {
221  buf[loop] = 0;
222  break;
223  }
224  }
225  if (flag)
226  *num1 = atoi(buf);
227  else (*num1)++;
228  }
229  if (startentry(buf)) {
230  memset(buf, ' ', 3);
231  flag = false;
232  for (loop = 3; loop < 6; loop++) {
233  if (isdigit(buf[loop]))
234  flag = true;
235  else {
236  buf[loop] = 0;
237  break;
238  }
239  if (flag)
240  *num2 = atoi(buf);
241  else (*num2)++;
242  }
243  loop++;
244  if (size)
245  *offset = lseek(fp, 0, SEEK_CUR) - (7 - loop);
246  else *offset = (chapstart) ? chapstart : lseek(fp, 0, SEEK_CUR) - 7;
247  if (size) {
248  ch2 = *num1;
249  vs2 = *num2;
250  if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
251  *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
252  }
253  else {
254  if (vs2) {
255  *size = (offset2 - (*offset));
256  }
257  }
258  lseek(fp, *offset, SEEK_SET);
259  }
260  return 0;
261  }
262  memmove(buf, &buf[1], 6);
263  if (read(fp, &buf[6], 1) != 1)
264  return 1;
265  }
266 }
267 
268 
269 void openfiles(char *fname)
270 {
271  SWBuf buf;
272 
273  if ((fp = FileMgr::openFileReadOnly(fname)) < 0) {
274  fprintf(stderr, "Couldn't open file: %s\n", fname);
275  exit(1);
276  }
277 
278  buf.setFormatted("%s.vss", fname);
279  if ((vfp = FileMgr::createPathAndFile(buf.c_str())) < 0) {
280  fprintf(stderr, "Couldn't open file: %s\n", buf.c_str());
281  exit(1);
282  }
283 
284  buf.setFormatted("%s.cps", fname);
285  if ((cfp = FileMgr::createPathAndFile(buf.c_str())) < 0) {
286  fprintf(stderr, "Couldn't open file: %s\n", buf.c_str());
287  exit(1);
288  }
289 
290  buf.setFormatted("%s.bks", fname);
291  if ((bfp = FileMgr::createPathAndFile(buf.c_str())) < 0) {
292  fprintf(stderr, "Couldn't open file: %s\n", buf.c_str());
293  exit(1);
294  }
295 }
296 
297 
298 void checkparams(int argc, char **argv)
299 {
300  if (argc < 2) {
301  fprintf(stderr, "usage: %s <file to process> [nt - for new testmt file]\n", argv[0]);
302  exit(1);
303  }
304  if (argc == 3)
305  key1 = key2 = key3 = "Matthew 1:1";
306  else key1 = key2 = key3 = "Genesis 1:1";
307 }
void openfiles(char *fname)
Definition: gbfidx.cpp:269
long chapoffset
Definition: gbfidx.cpp:50
char testmnt
Definition: gbfidx.cpp:52
#define SEEK_CUR
Definition: zconf.h:245
int pos
Definition: regex.c:5534
char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
Definition: gbfidx.cpp:201
static int openFileReadOnly(const char *fName)
Definition: filemgr.cpp:474
#define SEEK_END
Definition: zconf.h:246
short chapsize
Definition: gbfidx.cpp:51
int main(int argc, char **argv)
Definition: addcomment.cpp:32
void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
Definition: gbfidx.cpp:123
void checkparams(int argc, char **argv)
Definition: gbfidx.cpp:298
int fp
Definition: gbfidx.cpp:49
int vfp
Definition: gbfidx.cpp:49
VerseKey key2
Definition: gbfidx.cpp:48
VerseKey key3
Definition: gbfidx.cpp:48
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
int bfp
Definition: gbfidx.cpp:49
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
static int createPathAndFile(const char *fName)
Definition: filemgr.cpp:479
int cfp
Definition: gbfidx.cpp:49
char startentry(char *buf)
Definition: gbfidx.cpp:179
char startchap(char *buf)
Definition: gbfidx.cpp:157
VerseKey key1
Definition: gbfidx.cpp:48