Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

rwpidx.cpp

00001 /*****************************************************************************
00002  *
00003  *      This code wreaks but works (at least for RWP).  Good luck!
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 = 0, num2 = 0, 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);  /* Book    offset for testament intros */
00048         pos = 4;
00049         write(cfp, &pos, 4);  /* Chapter offset for testament intro */
00050 
00051 
00052 /*      Right now just zero out intros until parsing correctly */
00053         pos = 0;
00054         size = 0;
00055         write(vfp, &pos, 4);  /* Module intro */
00056         write(vfp, &size, 2);
00057         write(vfp, &pos, 4);  /* Testament intro */
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()) { // new book
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                 printf("Found verse Break: ('%s')\n", (const char *)key2);
00088                 writeidx(key1, key2, key3, offset, size);
00089         }
00090         close(vfp);
00091         close(cfp);
00092         close(bfp);
00093         close(fp);
00094 }
00095 
00096 
00097 /**************************************************************************
00098  * ENT: key1    - current location of index
00099  *      key2    - minimum keyval for which this offset is valid
00100  *      key3    - maximum keyval for which this offset is valid
00101  */
00102 
00103 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
00104 {
00105         long pos;
00106         short tmp;
00107 
00108         for (; ((key1 <= key3) && (key1.Error() != KEYERR_OUTOFBOUNDS) && (key1.Testament() == testmnt)); key1+=1) {
00109                 if (key1.Verse() == 1) {        // new chapter
00110                         if (key1.Chapter() == 1) {      // new book
00111                                 pos = lseek(cfp, 0, SEEK_CUR);
00112                                 write(bfp, &pos, 4);
00113                                 pos = lseek(vfp, 0, SEEK_CUR); /* Book intro (cps) */
00114                                 write(cfp, &pos, 4);
00115                                 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)*/
00116                                 write(vfp, &chapsize, 2);
00117                         }
00118                         pos = lseek(vfp, 0, SEEK_CUR);
00119                         write(cfp, &pos, 4);
00120                         write(vfp, &chapoffset, 4);  /* Chapter intro */
00121                         write(vfp, &chapsize, 2);
00122                 }
00123                 if (key1 >= key2) {
00124                         write(vfp, &offset, 4);
00125                         write(vfp, &size, 2);
00126                 }
00127                 else    {
00128                         pos = 0;
00129                         tmp = 0;
00130                         write(vfp, &pos, 4);
00131                         write(vfp, &tmp, 2);
00132                 }
00133         }
00134 }
00135 
00136 
00137 char startentry(char *buf)
00138 {
00139         char colon = 0;
00140 
00141         if (buf[0] != 10)
00142                 return 0;
00143         if (buf[1] != 10)
00144                 return 0;
00145         if (!isdigit(buf[2]))
00146                 return 0;
00147         if (!isdigit(buf[3])) {
00148                 if (buf[3]!= ':')
00149                         return 0;
00150                 else    colon++;
00151         }
00152         if (!isdigit(buf[4])) {
00153                 if (buf[4]!= ':')
00154                         return 0;
00155                 else    colon++;
00156         }
00157         if (colon != 1)
00158                 return 0;
00159         return 1;
00160 }
00161 
00162 
00163 char findbreak(int fp, long *offset, int *num1, int *num2, int *rangemax, short *size)
00164 {
00165         char buf[7];
00166         char buf2[20];
00167         char ch;
00168         char loop;
00169         long offset2;
00170         int ch2, vs2, rm2;
00171         
00172         memset(buf, ' ', 7);
00173 
00174         while (1) {
00175                 if (startentry(buf)) {
00176                         buf[0] = ' ';
00177                         buf[1] = ' ';
00178                         sscanf(buf, "%d:%d", num1, num2);
00179                         *rangemax = *num2;
00180                         *offset = lseek(fp, 0, SEEK_CUR) - 5;
00181                         if (size) {
00182                                 if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
00183                                         *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
00184                                 }
00185                                 else {
00186                                         if (vs2) {
00187                                                 *size = (offset2 - (*offset)) - 2;
00188                                         }
00189                                         else {
00190                                                 *size = (offset2 - (*offset)) - 6;
00191                                         }
00192                                 }
00193                                 lseek(fp, *offset, SEEK_SET);
00194                         }
00195                         return 0;
00196                 }
00197                                         
00198                 if (!strncmp(buf, "$-$-$-", 6)) {
00199                         *offset = lseek(fp, 0, SEEK_CUR) - 1;
00200                         *num2 = 0;
00201                         (*num1)++;
00202                         printf("Book marker: %s\n", buf2);
00203                         if (size) {
00204                                 if (findbreak(fp, &offset2, &ch2, &vs2, &rm2, 0)) {
00205                                         *size = (short) (lseek(fp, 0, SEEK_END) - (*offset));
00206                                 }
00207                                 else {
00208                                         if (vs2) {
00209                                                 *size = (offset2 - (*offset)) - 2;
00210                                         }
00211                                         else {
00212                                                 *size = (offset2 - (*offset)) - 6;
00213                                         }
00214                                 }
00215                                 lseek(fp, *offset, SEEK_SET);
00216                         }
00217                         return 0;
00218                 }
00219                                  
00220                                 
00221                 memmove(buf, &buf[1], 6);
00222                 if (read(fp, &buf[6], 1) != 1)
00223                         return 1;
00224         }
00225 }
00226 
00227 
00228 void openfiles(char *fname)
00229 {
00230         char buf[255];
00231 
00232         if ((fp = open(fname, O_RDONLY)) == -1) {
00233                 fprintf(stderr, "Couldn't open file: %s\n", fname);
00234                 exit(1);
00235         }
00236 
00237         sprintf(buf, "%s.vss", fname);
00238         if ((vfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00239                 fprintf(stderr, "Couldn't open file: %s\n", buf);
00240                 exit(1);
00241         }
00242 
00243         sprintf(buf, "%s.cps", fname);
00244         if ((cfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00245                 fprintf(stderr, "Couldn't open file: %s\n", buf);
00246                 exit(1);
00247         }
00248 
00249         sprintf(buf, "%s.bks", fname);
00250         if ((bfp = open(buf, O_CREAT|O_WRONLY)) == -1) {
00251                 fprintf(stderr, "Couldn't open file: %s\n", buf);
00252                 exit(1);
00253         }
00254 }
00255 
00256 
00257 void checkparams(int argc, char **argv)
00258 {
00259         if (argc < 2) {
00260                 fprintf(stderr, "usage: %s <file to process> [nt - for new testmt file]\n", argv[0]);
00261                 exit(1);
00262         }
00263         if (argc == 3)
00264                 key1 = key2 = key3 = "Matthew 1:1";
00265         else    key1 = key2 = key3 = "Genesis 1:1";
00266 }

Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by doxygen1.2.15