00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <ctype.h>
00010
00011 #ifndef __GNUC__
00012 #include <io.h>
00013 #else
00014 #include <unistd.h>
00015 #endif
00016
00017 #include <fcntl.h>
00018 #include <versekey.h>
00019
00020
00021 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size);
00022 char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size);
00023 void openfiles(char *fname);
00024 void checkparams(int argc, char **argv);
00025
00026
00027 VerseKey key1, key2, key3;
00028 int fp, vfp, cfp, bfp;
00029 long chapoffset;
00030 short chapsize;
00031 char testmnt;
00032
00033
00034 main(int argc, char **argv)
00035 {
00036 long pos, offset;
00037 int num1, num2, rangemax, curbook = 0, curchap = 0, curverse = 0;
00038 char buf[127], startflag = 0;
00039 short size, tmp;
00040
00041 checkparams(argc, argv);
00042
00043 openfiles(argv[1]);
00044
00045 testmnt = key1.Testament();
00046 num1 = key1.Chapter();
00047 num2 = key1.Verse();
00048 pos = 0;
00049 write(bfp, &pos, 4);
00050 pos = 4;
00051 write(cfp, &pos, 4);
00052
00053
00054
00055 pos = 0;
00056 size = 0;
00057 write(vfp, &pos, 4);
00058 write(vfp, &size, 2);
00059 write(vfp, &pos, 4);
00060 write(vfp, &size, 2);
00061
00062 while(!findbreak(fp, &offset, &num1, &num2, &rangemax, &size)) {
00063 if (!startflag) {
00064 startflag = 1;
00065 }
00066 else {
00067 if (num2 < key2.Verse()) {
00068 if (num1 <= key2.Chapter()) {
00069 key2.Verse(1);
00070 key2.Chapter(1);
00071 key2.Book(key2.Book()+1);
00072 }
00073 printf("Found Chapter Break: %d ('%s')\n", num1, (const char *)key2);
00074 chapoffset = offset;
00075 chapsize = size;
00076
00077 }
00078 }
00079 key2.Verse(1);
00080 key2.Chapter(num1);
00081 key2.Verse(num2);
00082
00083 key3 = key2;
00084
00085
00086 writeidx(key1, key2, key3, offset, size);
00087 }
00088 close(vfp);
00089 close(cfp);
00090 close(bfp);
00091 close(fp);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
00102 {
00103 long pos;
00104 short tmp;
00105
00106 for (; ((key1 <= key3) && (key1.Error() != KEYERR_OUTOFBOUNDS) && (key1.Testament() == testmnt)); key1+=1) {
00107 if (key1.Verse() == 1) {
00108 if (key1.Chapter() == 1) {
00109 pos = lseek(cfp, 0, SEEK_CUR);
00110 write(bfp, &pos, 4);
00111 pos = lseek(vfp, 0, SEEK_CUR);
00112 write(cfp, &pos, 4);
00113 write(vfp, &chapoffset, 4);
00114 write(vfp, &chapsize, 2);
00115 }
00116 pos = lseek(vfp, 0, SEEK_CUR);
00117 write(cfp, &pos, 4);
00118 write(vfp, &chapoffset, 4);
00119 write(vfp, &chapsize, 2);
00120 }
00121 if (key1 >= key2) {
00122 write(vfp, &offset, 4);
00123 write(vfp, &size, 2);
00124 }
00125 else {
00126 pos = 0;
00127 tmp = 0;
00128 write(vfp, &pos, 4);
00129 write(vfp, &tmp, 2);
00130 }
00131 }
00132 }
00133
00134
00135 char startchap(char *buf)
00136 {
00137 char loop;
00138
00139 if (buf[0] != '<')
00140 return 0;
00141 if (buf[1] != 'S')
00142 return 0;
00143 if (buf[2] != 'C')
00144 return 0;
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 return 1;
00156 }
00157
00158
00159 char startentry(char *buf)
00160 {
00161 char loop;
00162
00163 if (buf[0] != '<')
00164 return 0;
00165 if (buf[1] != 'S')
00166 return 0;
00167 if (buf[2] != 'V')
00168 return 0;
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 return 1;
00180 }
00181
00182
00183 char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
00184 {
00185 char buf[7];
00186 char buf2[20];
00187 char ch;
00188 char loop;
00189 long offset2;
00190 int ch2, vs2, rm2;
00191 bool flag;
00192 long chapstart = 0;
00193
00194 memset(buf, ' ', 7);
00195
00196 while (1) {
00197 if (startchap(buf)) {
00198 chapstart = lseek(fp, 0, SEEK_CUR) - 7;
00199 memset(buf, ' ', 3);
00200 flag = false;
00201 for (loop = 3; loop < 6; loop++) {
00202 if (isdigit(buf[loop]))
00203 flag = true;
00204 else {
00205 buf[loop] = 0;
00206 break;
00207 }
00208 }
00209 if (flag)
00210 *num1 = atoi(buf);
00211 else (*num1)++;
00212 }
00213 if (startentry(buf)) {
00214 memset(buf, ' ', 3);
00215 flag = false;
00216 for (loop = 3; loop < 6; loop++) {
00217 if (isdigit(buf[loop]))
00218 flag = true;
00219 else {
00220 buf[loop] = 0;
00221 break;
00222 }
00223 if (flag)
00224 *num2 = atoi(buf);
00225 else (*num2)++;
00226 }
00227 loop++;
00228 if (size)
00229 *offset = lseek(fp, 0, SEEK_CUR) - (7 - loop);
00230 else *offset = (chapstart) ? chapstart : lseek(fp, 0, SEEK_CUR) - 7;
00231 if (size) {
00232 ch2 = *num1;
00233 vs2 = *num2;
00234 if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
00235 *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
00236 }
00237 else {
00238 if (vs2) {
00239 *size = (offset2 - (*offset));
00240 }
00241 }
00242 lseek(fp, *offset, SEEK_SET);
00243 }
00244 return 0;
00245 }
00246 memmove(buf, &buf[1], 6);
00247 if (read(fp, &buf[6], 1) != 1)
00248 return 1;
00249 }
00250 }
00251
00252
00253 void openfiles(char *fname)
00254 {
00255 #ifndef O_BINARY // O_BINARY is needed in Borland C++ 4.53
00256 #define O_BINARY 0 // If it hasn't been defined than we probably
00257 #endif // don't need it.
00258 char buf[255];
00259
00260 if ((fp = open(fname, O_RDONLY|O_BINARY)) == -1) {
00261 fprintf(stderr, "Couldn't open file: %s\n", fname);
00262 exit(1);
00263 }
00264
00265 sprintf(buf, "%s.vss", fname);
00266 if ((vfp = open(buf, O_CREAT|O_WRONLY|O_BINARY)) == -1) {
00267 fprintf(stderr, "Couldn't open file: %s\n", buf);
00268 exit(1);
00269 }
00270
00271 sprintf(buf, "%s.cps", fname);
00272 if ((cfp = open(buf, O_CREAT|O_WRONLY|O_BINARY)) == -1) {
00273 fprintf(stderr, "Couldn't open file: %s\n", buf);
00274 exit(1);
00275 }
00276
00277 sprintf(buf, "%s.bks", fname);
00278 if ((bfp = open(buf, O_CREAT|O_WRONLY|O_BINARY)) == -1) {
00279 fprintf(stderr, "Couldn't open file: %s\n", buf);
00280 exit(1);
00281 }
00282 }
00283
00284
00285 void checkparams(int argc, char **argv)
00286 {
00287 if (argc < 2) {
00288 fprintf(stderr, "usage: %s <file to process> [nt - for new testmt file]\n", argv[0]);
00289 exit(1);
00290 }
00291 if (argc == 3)
00292 key1 = key2 = key3 = "Matthew 1:1";
00293 else key1 = key2 = key3 = "Genesis 1:1";
00294 }