utilities/vpl2mod.cpp File Reference

#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <io.h>
#include <filemgr.h>
#include <swmgr.h>
#include <rawtext.h>
#include <iostream>
#include <string>
#include <versekey.h>
Include dependency graph for vpl2mod.cpp:

Go to the source code of this file.

Functions

void fixText (char *text)
bool isKJVRef (const char *buf)
int main (int argc, char **argv)
char * parseVReg (char *buf)
char readline (int fd, char **buf)

Function Documentation

void fixText ( char *  text  ) 

Definition at line 148 of file vpl2mod.cpp.

00148                          {
00149     char *to = text;
00150     while(*text) {
00151         *to++ = *text++;
00152         *to++ = *text++;
00153         if (!*text)
00154             break;
00155         if (*text != ' ')
00156             std::cerr << "problem\n";
00157         else    text++;
00158     }
00159     *to = 0;
00160 }

bool isKJVRef ( const char *  buf  ) 

Definition at line 131 of file vpl2mod.cpp.

00131                                {
00132     VerseKey vk, test;
00133     vk.setAutoNormalize(false);
00134     vk.setIntros(true); // turn on mod/testmnt/book/chap headings
00135     vk.setPersist(true);
00136     // lets do some tests on the verse --------------
00137     vk = buf;
00138     test = buf;
00139 
00140     if (vk.getTestament() && vk.getBook() && vk.getChapter() && vk.getVerse()) { // if we're not a heading
00141 //      std::cerr << (const char*)vk << " == "  << (const char*)test << std::endl;
00142         return (vk == test);
00143     }
00144     else return true;   // no check if we're a heading... Probably bad.
00145 }

int main ( int  argc,
char **  argv 
)

Definition at line 162 of file vpl2mod.cpp.

00162                                 {
00163 
00164     // Let's test our command line arguments
00165     if (argc < 2) {
00166 //      fprintf(stderr, "usage: %s <vpl_file> </path/to/mod> [0|1 - file includes prepended verse references]\n", argv[0]);
00167         fprintf(stderr, "usage: %s <source_vpl_file> </path/to/output/mod/> [0|1 - prepended verse refs] [0|1 - NT only]\n\n", argv[0]);
00168         fprintf(stderr, "\tWARNING: THIS IS CURRENTLY A KJV-VERSIFICATION-ONLY UTILITY\n");
00169         fprintf(stderr, "\tWith no verse refs, source file must contain exactly 31102 lines.\n");
00170         fprintf(stderr, "\tThis is KJV verse count plus headings for MODULE,\n");
00171         fprintf(stderr, "\tTESTAMENT, BOOK, CHAPTER. An example snippet follows:\n\n");
00172         fprintf(stderr, "\t\tMODULE HEADER\n");
00173         fprintf(stderr, "\t\tOLD TESTAMENT HEADER\n");
00174         fprintf(stderr, "\t\tGENESIS HEADER\n");
00175         fprintf(stderr, "\t\tCHAPTER 1 HEADER\n");
00176         fprintf(stderr, "\t\tIn the beginning...\n\n");
00177         fprintf(stderr, "\t... implying there must also be a CHAPTER2 HEADER,\n");
00178         fprintf(stderr, "\tEXODUS HEADER, NEW TESTAMENT HEADER, etc.  If there is no text for\n");
00179         fprintf(stderr, "\tthe header, a blank line must, at least, hold place.\n\n");
00180         fprintf(stderr, "\tWith verse refs, source file must simply contain any number of lines,\n");
00181         fprintf(stderr, "\tthat begin with the verse reference for which it is an entry.  e.g.:\n\n");
00182         fprintf(stderr, "\t\tgen 1:0 CHAPTER 1 HEADER\n");
00183         fprintf(stderr, "\t\tgen 1:1 In the beginning...\n\n");
00184         exit(-1);
00185     }
00186 
00187     // Let's see if we can open our input file
00188     int fd = FileMgr::openFileReadOnly(argv[1]);
00189     if (fd < 0) {
00190         fprintf(stderr, "error: %s: couldn't open input file: %s \n", argv[0], argv[1]);
00191         exit(-2);
00192     }
00193 
00194     // Try to initialize a default set of datafiles and indicies at our
00195     // datapath location passed to us from the user.
00196     if (RawText::createModule(argv[2])) {
00197         fprintf(stderr, "error: %s: couldn't create module at path: %s \n", argv[0], argv[2]);
00198         exit(-3);
00199     }
00200 
00201     // not used yet, but for future support of a vpl file with each line
00202     // prepended with verse reference, eg. "Gen 1:1 In the beginning..."
00203     bool vref = false;
00204     if (argc > 3)
00205         vref = (argv[3][0] == '0') ? false : true;
00206 
00207     // if 'nt' is the 4th arg, our vpl file only has the NT
00208     bool ntonly = false;
00209     if (argc > 4)
00210                 ntonly = (argv[4][0] == '0') ? false : true;
00211     
00212     // Do some initialization stuff
00213     char *buffer = 0;
00214     RawText mod(argv[2]);   // open our datapath with our RawText driver.
00215     VerseKey vk;
00216     vk.setAutoNormalize(false);
00217     vk.setIntros(true); // turn on mod/testmnt/book/chap headings
00218     vk.setPersist(true);
00219 
00220     mod.setKey(vk);
00221 
00222     // Loop through module from TOP to BOTTOM and set next line from
00223     // input file as text for this entry in the module
00224     mod = TOP;
00225     if (ntonly) vk = "Matthew 1:1";
00226       
00227     int successive = 0;  //part of hack below
00228     while ((!mod.popError()) && (!readline(fd, &buffer))) {
00229         if (*buffer == '|') // comments, ignore line
00230             continue;
00231         if (vref) {
00232             const char *verseText = parseVReg(buffer);
00233             if (!verseText) {   // if we didn't find a valid verse ref
00234                 std::cerr << "No valid verse ref found on line: " << buffer << "\n";
00235                 exit(-4);
00236             }
00237 
00238             vk = buffer;
00239             if (vk.popError()) {
00240                 std::cerr << "Error parsing key: " << buffer << "\n";
00241                 exit(-5);
00242             }
00243             string orig = mod.getRawEntry();
00244 
00245             if (!isKJVRef(buffer)) {
00246                 VerseKey origVK = vk;
00247                 /* This block is functioning improperly -- problem with AutoNormalize???
00248                 do {
00249                     vk--;
00250                 }
00251                 while (!vk.popError() && !isKJVRef(vk)); */
00252                 //hack to replace above:
00253                 successive++;
00254                 vk -= successive;
00255                 orig = mod.getRawEntry();
00256 
00257                 std::cerr << "Not a valid KJV ref: " << origVK << "\n";
00258                 std::cerr << "appending to ref: " << vk << "\n";
00259                 orig += " [ (";
00260                 orig += origVK;
00261                 orig += ") ";
00262                 orig += verseText;
00263                 orig += " ] ";
00264                 verseText = orig.c_str();
00265             }
00266             else {
00267               successive = 0;
00268             }
00269 
00270             if (orig.length() > 1)
00271                    std::cerr << "Warning, overwriting verse: " << vk << std::endl;
00272               
00273             // ------------- End verse tests -----------------
00274             mod << verseText;   // save text to module at current position
00275         }
00276         else {
00277             fixText(buffer);
00278             mod << buffer;  // save text to module at current position
00279             mod++;  // increment module position
00280         }
00281     }
00282 
00283     // clear up our buffer that readline might have allocated
00284     if (buffer)
00285         delete [] buffer;
00286 }

char* parseVReg ( char *  buf  ) 

Definition at line 97 of file vpl2mod.cpp.

00097                            {
00098     char stage = 0;
00099 
00100     while (*buf) {
00101         switch (stage) {
00102         case 0:
00103             if (isalpha(*buf))
00104                 stage++;
00105             break;
00106         case 1:
00107             if (isdigit(*buf))
00108                 stage++;
00109             break;
00110         case 2:
00111             if (*buf == ':')
00112                 stage++;
00113             break;
00114         case 3:
00115             if (isdigit(*buf))
00116                 stage++;
00117             break;
00118        case 4:
00119             if (*buf == ' ') {
00120                 *buf = 0;
00121                 return ++buf;
00122             }
00123             break;
00124         }
00125         buf++;
00126     }
00127     return (stage == 4) ? buf : 0;  // if we got to stage 4 return after key buf, else return 0;
00128 }

char readline ( int  fd,
char **  buf 
)

Definition at line 53 of file vpl2mod.cpp.

00053                                   {
00054     char ch;
00055     if (*buf)
00056         delete [] *buf;
00057     *buf = 0;
00058     int len;
00059 
00060 
00061     long index = lseek(fd, 0, SEEK_CUR);
00062     // clean up any preceding white space
00063     while ((len = read(fd, &ch, 1)) == 1) {
00064         if ((ch != 13) && (ch != ' ') && (ch != '\t'))
00065             break;
00066         else index++;
00067     }
00068 
00069 
00070     while (ch != 10) {
00071         if ((len = read(fd, &ch, 1)) != 1)
00072             break;
00073     }
00074     
00075     int size = (lseek(fd, 0, SEEK_CUR) - index) - 1;
00076 
00077     *buf = new char [ size + 1 ];
00078 
00079     if (size > 0) {
00080         lseek(fd, index, SEEK_SET);
00081         read(fd, *buf, size);
00082         read(fd, &ch, 1);   //pop terminating char
00083         (*buf)[size] = 0;
00084 
00085         // clean up any trailing junk on buf
00086         for (char *it = *buf+(strlen(*buf)-1); it > *buf; it--) {
00087             if ((*it != 10) && (*it != 13) && (*it != ' ') && (*it != '\t'))
00088                 break;
00089             else *it = 0;
00090         }
00091     }
00092     else **buf = 0;
00093     return !len;
00094 }


Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1