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 pos = 0;
00047 write(bfp, &pos, 4);
00048 pos = 4;
00049 write(cfp, &pos, 4);
00050
00051
00052
00053 pos = 0;
00054 size = 0;
00055 write(vfp, &pos, 4);
00056 write(vfp, &size, 2);
00057 write(vfp, &pos, 4);
00058 write(vfp, &size, 2);
00059
00060 while(!findbreak(fp, &offset, &num1, &num2, &rangemax, &size)) {
00061 if (num2) {
00062 key2.Verse(1);
00063 key2.Chapter(num1);
00064 key2.Verse(num2);
00065 }
00066 else {
00067 key2.Verse(1);
00068 if (!startflag) {
00069 startflag = 1;
00070 }
00071 else {
00072 if (num1 <= key2.Chapter()) {
00073 key2.Chapter(1);
00074 key2.Book(key2.Book()+1);
00075 }
00076 }
00077 key2.Chapter(num1);
00078 printf("Found Chapter Break: %d ('%s')\n", num1, (const char *)key2);
00079 chapoffset = offset;
00080 chapsize = size;
00081 continue;
00082 }
00083
00084 key3 = key2;
00085 key3 += (rangemax - key3.Verse());
00086
00087 writeidx(key1, key2, key3, offset, size);
00088 }
00089 close(vfp);
00090 close(cfp);
00091 close(bfp);
00092 close(fp);
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
00103 {
00104 long pos;
00105 short tmp;
00106
00107 for (; ((key1 <= key3) && (key1.Error() != KEYERR_OUTOFBOUNDS) && (key1.Testament() == testmnt)); key1+=1) {
00108 if (key1.Verse() == 1) {
00109 if (key1.Chapter() == 1) {
00110 pos = lseek(cfp, 0, SEEK_CUR);
00111 write(bfp, &pos, 4);
00112 pos = lseek(vfp, 0, SEEK_CUR);
00113 write(cfp, &pos, 4);
00114 write(vfp, &chapoffset, 4);
00115 write(vfp, &chapsize, 2);
00116 }
00117 pos = lseek(vfp, 0, SEEK_CUR);
00118 write(cfp, &pos, 4);
00119 write(vfp, &chapoffset, 4);
00120 write(vfp, &chapsize, 2);
00121 }
00122 if (key1 >= key2) {
00123 write(vfp, &offset, 4);
00124 write(vfp, &size, 2);
00125 }
00126 else {
00127 pos = 0;
00128 tmp = 0;
00129 write(vfp, &pos, 4);
00130 write(vfp, &tmp, 2);
00131 }
00132 }
00133 }
00134
00135
00136 char startentry(char *buf)
00137 {
00138 char loop;
00139
00140 if (buf[0] != 10)
00141 return 0;
00142 if (buf[1] != '#')
00143 return 0;
00144 if (!isdigit(buf[2]))
00145 return 0;
00146 for (loop = 3; loop < 7; loop++) {
00147 if (buf[loop] == ' ')
00148 break;
00149 if ((!isdigit(buf[loop])) && (buf[loop] != ',') && (buf[loop] != '-'))
00150 return 0;
00151 }
00152 return 1;
00153 }
00154
00155
00156 char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
00157 {
00158 char buf[7];
00159 char buf2[20];
00160 char ch;
00161 char loop;
00162 long offset2;
00163 int ch2, vs2, rm2;
00164
00165 memset(buf, ' ', 7);
00166
00167 while (1) {
00168 if (startentry(buf)) {
00169 memset(buf, ' ', 2);
00170 for (loop = 2; loop < 7; loop++) {
00171 if ((buf[loop] == '-') || (buf[loop] == ',') || (buf[loop] == ' ')) {
00172 buf[loop] = 0;
00173 *num2 = atoi(buf);
00174 break;
00175 }
00176 }
00177 for (ch = loop + 1; ch < 7; ch++) {
00178 if (buf[ch] == ' ') {
00179 break;
00180 }
00181 }
00182 buf[ch] = 0;
00183 *rangemax = atoi(&buf[loop+1]);
00184 if (!*rangemax)
00185 *rangemax = *num2;
00186 *offset = lseek(fp, 0, SEEK_CUR) - 5;
00187 if (size) {
00188 if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
00189 *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
00190 }
00191 else {
00192 if (vs2) {
00193 *size = (offset2 - (*offset)) - 3;
00194 }
00195 else {
00196 sprintf(buf2, "$-$-$- XX:%d", ch2);
00197 *size = (offset2 - (*offset)) - ((strlen(buf2) + 4));
00198 }
00199 }
00200 lseek(fp, *offset, SEEK_SET);
00201 }
00202 return 0;
00203 }
00204
00205 if (!strncmp(buf, "$-$-$-", 6)) {
00206 memset(buf2, 0, 7);
00207 loop = 0;
00208 while ((read(fp, &buf2[loop], 1) == 1) && (loop < 7)) {
00209 if ((buf2[loop] == 10) || (buf2[loop] == 13)) {
00210 buf2[loop] = 0;
00211 break;
00212 }
00213 loop++;
00214 }
00215 while (read(fp, &ch, 1) == 1) {
00216 if (ch == '*')
00217 break;
00218 }
00219
00220 *offset = lseek(fp, 0, SEEK_CUR) - 1;
00221 *num2 = 0;
00222 for (loop = strlen(buf2) - 1; loop; loop--) {
00223 if (buf2[loop] == ':')
00224 break;
00225 }
00226 *num1 = atoi(&buf2[loop+1]);
00227 printf("Chapter marker: %s\n", buf2);
00228 if (size) {
00229 if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
00230 *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
00231 }
00232 else {
00233 if (vs2) {
00234 *size = (offset2 - (*offset)) - 3;
00235 }
00236 else {
00237 sprintf(buf2, "$-$-$- XX:%d", ch2);
00238 *size = (offset2 - (*offset)) - ((strlen(buf2) + 4));
00239 }
00240 }
00241 lseek(fp, *offset, SEEK_SET);
00242 }
00243 return 0;
00244 }
00245
00246
00247 memmove(buf, &buf[1], 6);
00248 if (read(fp, &buf[6], 1) != 1)
00249 return 1;
00250 }
00251 }
00252
00253
00254 void openfiles(char *fname)
00255 {
00256 char buf[255];
00257
00258 if ((fp = open(fname, O_RDONLY)) == -1) {
00259 fprintf(stderr, "Couldn't open file: %s\n", fname);
00260 exit(1);
00261 }
00262
00263 sprintf(buf, "%s.vss", fname);
00264 if ((vfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00265 fprintf(stderr, "Couldn't open file: %s\n", buf);
00266 exit(1);
00267 }
00268
00269 sprintf(buf, "%s.cps", fname);
00270 if ((cfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00271 fprintf(stderr, "Couldn't open file: %s\n", buf);
00272 exit(1);
00273 }
00274
00275 sprintf(buf, "%s.bks", fname);
00276 if ((bfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00277 fprintf(stderr, "Couldn't open file: %s\n", buf);
00278 exit(1);
00279 }
00280 }
00281
00282
00283 void checkparams(int argc, char **argv)
00284 {
00285 if (argc < 2) {
00286 fprintf(stderr, "usage: %s <file to process> [nt - for new testmt file]\n", argv[0]);
00287 exit(1);
00288 }
00289 if (argc == 3)
00290 key1 = key2 = key3 = "Matthew 1:1";
00291 else key1 = key2 = key3 = "Genesis 1:1";
00292 }