The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gbfidx.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <fcntl.h>
#include <versekey.h>
#include <filemgr.h>
+ Include dependency graph for gbfidx.cpp:

Go to the source code of this file.

Functions

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

Variables

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

Function Documentation

void checkparams ( int  argc,
char **  argv 
)

Definition at line 298 of file gbfidx.cpp.

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 }
VerseKey key2
Definition: gbfidx.cpp:48
VerseKey key3
Definition: gbfidx.cpp:48
VerseKey key1
Definition: gbfidx.cpp:48
char findbreak ( int  fp,
long *  offset,
int *  num1,
int *  num2,
int *  rangemax,
short *  size 
)

Definition at line 201 of file gbfidx.cpp.

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 }
#define SEEK_CUR
Definition: zconf.h:245
char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
Definition: gbfidx.cpp:201
#define SEEK_END
Definition: zconf.h:246
int fp
Definition: gbfidx.cpp:49
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
char startentry(char *buf)
Definition: gbfidx.cpp:179
char startchap(char *buf)
Definition: gbfidx.cpp:157
int main ( int  argc,
char **  argv 
)

Definition at line 55 of file gbfidx.cpp.

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 }
void openfiles(char *fname)
Definition: gbfidx.cpp:269
long chapoffset
Definition: gbfidx.cpp:50
char testmnt
Definition: gbfidx.cpp:52
int pos
Definition: regex.c:5534
char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
Definition: gbfidx.cpp:201
short chapsize
Definition: gbfidx.cpp:51
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
int bfp
Definition: gbfidx.cpp:49
int size
Definition: regex.c:5043
int cfp
Definition: gbfidx.cpp:49
VerseKey key1
Definition: gbfidx.cpp:48
void openfiles ( char *  fname)

Definition at line 269 of file gbfidx.cpp.

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 }
static int openFileReadOnly(const char *fName)
Definition: filemgr.cpp:474
int fp
Definition: gbfidx.cpp:49
int vfp
Definition: gbfidx.cpp:49
int bfp
Definition: gbfidx.cpp:49
static int createPathAndFile(const char *fName)
Definition: filemgr.cpp:479
int cfp
Definition: gbfidx.cpp:49
char startchap ( char *  buf)

Definition at line 157 of file gbfidx.cpp.

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 }
char startentry ( char *  buf)

Definition at line 179 of file gbfidx.cpp.

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 }
void writeidx ( VerseKey key1,
VerseKey key2,
VerseKey key3,
long  offset,
short  size 
)

Definition at line 123 of file gbfidx.cpp.

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 }
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
short chapsize
Definition: gbfidx.cpp:51
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
int size
Definition: regex.c:5043
int cfp
Definition: gbfidx.cpp:49
VerseKey key1
Definition: gbfidx.cpp:48

Variable Documentation

int bfp

Definition at line 49 of file gbfidx.cpp.

int cfp

Definition at line 49 of file gbfidx.cpp.

long chapoffset

Definition at line 50 of file gbfidx.cpp.

short chapsize

Definition at line 51 of file gbfidx.cpp.

int fp

Definition at line 49 of file gbfidx.cpp.

VerseKey key1

Definition at line 48 of file gbfidx.cpp.

VerseKey key2

Definition at line 48 of file gbfidx.cpp.

VerseKey key3

Definition at line 48 of file gbfidx.cpp.

char testmnt

Definition at line 52 of file gbfidx.cpp.

int vfp

Definition at line 49 of file gbfidx.cpp.