utilities/addvs.cpp File Reference

#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <io.h>
#include <swmgr.h>
#include <versekey.h>
#include <rawtext.h>
#include <iostream>
Include dependency graph for addvs.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 48 of file addvs.cpp.

00048                                 {
00049   
00050   const char * helptext = "addvs 1.1 Bible & Commentary module creation tool for the SWORD Project\nUse -a to add a new verse from standard input or a file, -d to delete a verse,\n-l to link two verses, -c to create a new module.\n  usage:\n   %s -a </path/to/module> <verse> [</path/to/file/with/verse>]\n   %s -d </path/to/module> <key>\n   %s -l </path/to/module> <first verse (already assigned)> <second verse>\n   %s -c </path/to/module>\n";
00051   long entrysize;
00052 
00053   if (argc < 3) {
00054     fprintf(stderr, helptext, argv[0], argv[0], argv[0], argv[0]);
00055     exit(-1);
00056   }
00057  if (!strcmp(argv[1], "-a") && (argc == 4 || argc == 5)) {  
00058     
00059     // Do some initialization stuff
00060     char buffer[65536];  //this is the max size of any entry
00061     RawText * mod = new RawText(argv[2]);   // open our datapath with our RawText driver.
00062     VerseKey *vkey = new VerseKey;
00063     vkey->setIntros(true);
00064     vkey->setAutoNormalize(false);
00065     vkey->setPersist(true);      // the magical setting
00066     *vkey = argv[3];   
00067     // Set our VerseKey
00068     mod->setKey(*vkey);
00069     if (!vkey->getChapter()) {
00070       // bad hack >>
00071       // 0:0 is Book intro
00072       // (chapter):0 is Chapter intro
00073       //
00074       // 0:2 is Module intro
00075       // 0:1 is Testament intro
00076       int backstep = vkey->getVerse();
00077       vkey->setVerse(0);
00078       *mod -= backstep;       
00079       // << bad hack
00080 
00081       FILE *infile;
00082       // case: add from text file
00083       //Open our data file and read its contents into the buffer
00084       if (argc == 5) infile = fopen(argv[4], "r");
00085       // case: add from stdin
00086       else infile = stdin;
00087       
00088       entrysize = fread(buffer, sizeof(char), sizeof(buffer), infile);
00089       
00090       mod->setEntry(buffer, entrysize); // save text to module at current position
00091     }
00092     else {
00093       ListKey listkey = vkey->parseVerseList(argv[3], "Gen1:1", true);
00094       int i;
00095       bool havefirst = false;
00096       VerseKey firstverse;
00097       for (i = 0; i < listkey.Count(); i++) {
00098     VerseKey *element = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(i));
00099     if (element) {
00100       mod->setKey(element->getLowerBound());
00101       VerseKey finalkey = element->getUpperBound();
00102       std::cout << mod->getKeyText() << "-" << (const char*)finalkey << std::endl;
00103       if (!havefirst) {
00104         havefirst = true;
00105         firstverse = *mod->getKey();
00106         FILE *infile;
00107         // case: add from text file
00108         //Open our data file and read its contents into the buffer
00109         if (argc == 5) infile = fopen(argv[4], "r");
00110         // case: add from stdin
00111         else infile = stdin;
00112         
00113         entrysize = fread(buffer, sizeof(char), sizeof(buffer), infile);
00114         
00115         mod->setEntry(buffer, entrysize);   // save text to module at current position
00116         std::cout << "f" << (const char*)firstverse << std::endl;
00117         (*mod)++;
00118       }
00119       while (*mod->getKey() <= finalkey) {
00120         std::cout << mod->getKeyText() << std::endl;
00121         *(SWModule*)mod << &firstverse;
00122         (*mod)++;
00123       }
00124     }
00125     else {
00126       if (havefirst) {
00127         mod->setKey(*listkey.getElement(i));
00128         *(SWModule*)mod << &firstverse;
00129         std::cout << mod->getKeyText() << std::endl;
00130       }
00131       else {
00132         mod->setKey(*listkey.getElement(i));
00133         havefirst = true;
00134         firstverse = *mod->getKey();
00135         FILE *infile;
00136         // case: add from text file
00137         //Open our data file and read its contents into the buffer
00138         if (argc == 5) infile = fopen(argv[4], "r");
00139         // case: add from stdin
00140         else infile = stdin;
00141         
00142         entrysize = fread(buffer, sizeof(char), sizeof(buffer), infile);
00143         
00144         mod->setEntry(buffer, entrysize);   // save text to module at current position
00145         std::cout << "f" << (const char*)firstverse << std::endl;
00146       }
00147     }
00148       }
00149     }
00150     delete vkey;
00151  }
00152  // Link 2 verses
00153  else if (!strcmp(argv[1], "-l") && argc == 5) {
00154    // Do some initialization stuff
00155    RawText *mod = new RawText(argv[2]); // open our datapath with our RawText driver.
00156    
00157    mod->setKey(argv[4]);    // set key from argument
00158    SWKey tmpkey = (SWKey) argv[3];
00159    *(SWModule*)mod << &(tmpkey);
00160    delete mod;
00161  }
00162  
00163  else if (!strcmp(argv[1], "-d") && argc == 4) {
00164    RawText mod(argv[2]);    // open our datapath with our RawText driver.
00165    VerseKey *vkey = new VerseKey;
00166    vkey->setIntros(true);
00167    vkey->setAutoNormalize(false);
00168    vkey->setPersist(true);      // the magical setting
00169    
00170    // Set our VerseKey
00171    mod.setKey(*vkey);
00172    *vkey = argv[3];
00173 
00174    if (!vkey->getChapter())
00175      {
00176        // bad hack >>
00177        // 0:0 is Book intro
00178        // (chapter):0 is Chapter intro
00179        //
00180        // 0:2 is Module intro
00181        // 0:1 is Testament intro
00182        int backstep = vkey->getVerse();
00183        vkey->setVerse(0);
00184        mod -= backstep;       
00185        // << bad hack
00186      }
00187    
00188    mod.deleteEntry();
00189    delete vkey;
00190  }
00191 
00192   // Make a new module
00193   else if (!strcmp(argv[1], "-c") && argc == 3) {
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(-2);
00199     }
00200   }   
00201   
00202   // Bad arguments, print usage
00203   else {
00204     fprintf(stderr, helptext, argv[0], argv[0], argv[0], argv[0]);
00205     exit(-1);
00206   }
00207 }


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