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 writeidx(key1, key2, key3, offset, size);
00064 key2++;
00065 key3 = key2;
00066 }
00067 close(vfp);
00068 close(cfp);
00069 close(bfp);
00070 close(fp);
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
00081 {
00082 long pos;
00083 short tmp;
00084
00085 for (; ((key1 <= key3) && (key1.Error() != KEYERR_OUTOFBOUNDS) && (key1.Testament() == testmnt)); key1+=1) {
00086 if (key1.Verse() == 1) {
00087 if (key1.Chapter() == 1) {
00088 pos = lseek(cfp, 0, SEEK_CUR);
00089 write(bfp, &pos, 4);
00090 pos = lseek(vfp, 0, SEEK_CUR);
00091 write(cfp, &pos, 4);
00092 write(vfp, &chapoffset, 4);
00093 write(vfp, &chapsize, 2);
00094 }
00095 pos = lseek(vfp, 0, SEEK_CUR);
00096 write(cfp, &pos, 4);
00097 write(vfp, &chapoffset, 4);
00098 write(vfp, &chapsize, 2);
00099 }
00100 if (key1 >= key2) {
00101 write(vfp, &offset, 4);
00102 write(vfp, &size, 2);
00103 }
00104 else {
00105 pos = 0;
00106 tmp = 0;
00107 write(vfp, &pos, 4);
00108 write(vfp, &tmp, 2);
00109 }
00110 }
00111 }
00112
00113
00114 char startchap(char *buf)
00115 {
00116 char loop;
00117
00118 if (buf[0] != '<')
00119 return 0;
00120 if (buf[1] != 'S')
00121 return 0;
00122 if (buf[2] != 'C')
00123 return 0;
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 return 1;
00135 }
00136
00137
00138 char startentry(char *buf)
00139 {
00140 char loop;
00141
00142 if (buf[0] != '<')
00143 return 0;
00144 if (buf[1] != 'S')
00145 return 0;
00146 if (buf[2] != 'V')
00147 return 0;
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 return 1;
00159 }
00160
00161
00162 char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
00163 {
00164 char buf[7];
00165 char buf2[20];
00166 char ch;
00167 char loop;
00168 long offset2;
00169 int ch2, vs2, rm2;
00170 bool flag;
00171 long chapstart = 0;
00172
00173 memset(buf, ' ', 7);
00174
00175 while (1) {
00176 if (startentry(buf)) {
00177 if (size)
00178 *offset = lseek(fp, 0, SEEK_CUR) - 3;
00179 else *offset = lseek(fp, 0, SEEK_CUR) - 7;
00180 if (size) {
00181 ch2 = *num1;
00182 vs2 = *num2;
00183 if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
00184 *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
00185 }
00186 else {
00187 *size = (offset2 - (*offset));
00188 }
00189 lseek(fp, *offset, SEEK_SET);
00190 }
00191 return 0;
00192 }
00193 memmove(buf, &buf[1], 6);
00194 if (read(fp, &buf[6], 1) != 1)
00195 return 1;
00196 }
00197 }
00198
00199
00200 void openfiles(char *fname)
00201 {
00202 char buf[255];
00203
00204 if ((fp = open(fname, O_RDONLY)) == -1) {
00205 fprintf(stderr, "Couldn't open file: %s\n", fname);
00206 exit(1);
00207 }
00208
00209 sprintf(buf, "%s.vss", fname);
00210 if ((vfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00211 fprintf(stderr, "Couldn't open file: %s\n", buf);
00212 exit(1);
00213 }
00214
00215 sprintf(buf, "%s.cps", fname);
00216 if ((cfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00217 fprintf(stderr, "Couldn't open file: %s\n", buf);
00218 exit(1);
00219 }
00220
00221 sprintf(buf, "%s.bks", fname);
00222 if ((bfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00223 fprintf(stderr, "Couldn't open file: %s\n", buf);
00224 exit(1);
00225 }
00226 }
00227
00228
00229 void checkparams(int argc, char **argv)
00230 {
00231 if (argc < 2) {
00232 fprintf(stderr, "usage: %s <file to process> [nt - for new testmt file]\n", argv[0]);
00233 exit(1);
00234 }
00235 if (argc == 3)
00236 key1 = key2 = key3 = "Matthew 1:1";
00237 else key1 = key2 = key3 = "Genesis 1:1";
00238 }