utilities/imp2vs.cpp File Reference

#include <stdio.h>
#include <iostream>
#include <swbuf.h>
#include <filemgr.h>
#include <versekey.h>
#include <rawtext.h>
#include <rawtext4.h>
#include <ztext.h>
#include <lzsscomprs.h>
#include <zipcomprs.h>
#include <localemgr.h>
Include dependency graph for imp2vs.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
void usage (const char *progName, const char *error=0)
void writeEntry (const SWBuf &key, const SWBuf &entry, SWModule *module)

Variables

int page = 0

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 79 of file imp2vs.cpp.

00079                                 {
00080 
00081 
00082     // handle options
00083     if (argc < 2) usage(*argv);
00084 
00085     const char *progName   = argv[0];
00086     const char *inFileName = argv[1];
00087     SWBuf v11n             = "KJV";
00088     SWBuf outPath          = "./";
00089     SWBuf locale           = "en";
00090     
00091     bool fourByteSize      = false;
00092     bool append            = false;
00093     int iType              = 4;
00094     SWCompress *compressor = 0;
00095     SWBuf compType         = "";
00096 
00097     for (int i = 2; i < argc; i++) {
00098         if (!strcmp(argv[i], "-a")) {
00099             append = true;
00100         }
00101         else if (!strcmp(argv[i], "-z")) {
00102             if (compType.size()) usage(*argv, "Cannot specify both -z and -Z");
00103             if (fourByteSize) usage(*argv, "Cannot specify both -z and -4");
00104             compType = "ZIP";
00105         }
00106         else if (!strcmp(argv[i], "-Z")) {
00107             if (compType.size()) usage(*argv, "Cannot specify both -z and -Z");
00108             if (fourByteSize) usage(*argv, "Cannot specify both -Z and -4");
00109             compType = "LZSS";
00110         }
00111         else if (!strcmp(argv[i], "-4")) {
00112             fourByteSize = true;
00113         }
00114         else if (!strcmp(argv[i], "-b")) {
00115             if (i+1 < argc) {
00116                 iType = atoi(argv[++i]);
00117                 if ((iType >= 2) && (iType <= 4)) continue;
00118             }
00119             usage(*argv, "-b requires one of <2|3|4>");
00120         }
00121         else if (!strcmp(argv[i], "-o")) {
00122             if (i+1 < argc) outPath = argv[++i];
00123             else usage(progName, "-o requires <output_path>");
00124         }
00125         else if (!strcmp(argv[i], "-v")) {
00126             if (i+1 < argc) v11n = argv[++i];
00127             else usage(progName, "-v requires <v11n>");
00128         }
00129         else if (!strcmp(argv[i], "-l")) {
00130             if (i+1 < argc) locale = argv[++i];
00131             else usage(progName, "-l requires <locale>");
00132         }
00133         else usage(progName, (((SWBuf)"Unknown argument: ")+ argv[i]).c_str());
00134     }
00135     // -----------------------------------------------------
00136     const VersificationMgr::System *v = VersificationMgr::getSystemVersificationMgr()->getVersificationSystem(v11n);
00137     if (!v) std::cout << "Warning: Versification " << v11n << " not found. Using KJV versification...\n";
00138 
00139     if (compType == "ZIP") {
00140 #ifndef EXCLUDEZLIB
00141         compressor = new ZipCompress();
00142 #else
00143         usage(*argv, "ERROR: SWORD library not compiled with ZIP compression support.\n\tBe sure libzip is available when compiling SWORD library");
00144 #endif
00145     }
00146     else if (compType == "LZSS") {
00147         compressor = new LZSSCompress();
00148     }
00149 
00150     // setup module
00151     if (!append) {
00152         if (compressor) {
00153             if (zText::createModule(outPath, iType, v11n)) {
00154                 fprintf(stderr, "ERROR: %s: couldn't create module at path: %s \n", *argv, outPath.c_str());
00155                 exit(-1);
00156             }
00157         }
00158         else {
00159             if (!fourByteSize)
00160                 RawText::createModule(outPath, v11n);
00161             else    RawText4::createModule(outPath, v11n);
00162         }
00163     }
00164 
00165     SWModule *module = 0;
00166     if (compressor) {
00167         // Create a compressed text module allowing very large entries
00168         // Taking defaults except for first, fourth, fifth and last argument
00169         module = new zText(
00170                 outPath,        // ipath
00171                 0,      // iname
00172                 0,      // idesc
00173                 iType,      // iblockType
00174                 compressor, // icomp
00175                 0,      // idisp
00176                 ENC_UNKNOWN,    // enc
00177                 DIRECTION_LTR,  // dir
00178                 FMT_UNKNOWN,    // markup
00179                 0,      // lang
00180                 v11n        // versification
00181                );
00182     }
00183     else {
00184         module = (!fourByteSize)
00185             ? (SWModule *)new RawText(outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, v11n)
00186             : (SWModule *)new RawText4(outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, v11n);
00187     }
00188     // -----------------------------------------------------
00189     
00190     // setup locale manager
00191     
00192     LocaleMgr::getSystemLocaleMgr()->setDefaultLocaleName(locale);
00193             
00194 
00195     // setup module key to allow full range of possible values, and then some
00196     
00197     VerseKey *vkey = (VerseKey *)module->createKey();
00198     vkey->setIntros(true);
00199     vkey->setAutoNormalize(false);
00200     vkey->setPersist(true);
00201     module->setKey(*vkey);
00202     // -----------------------------------------------------
00203 
00204 
00205     // process input file
00206     FileDesc *fd = FileMgr::getSystemFileMgr()->open(inFileName, FileMgr::RDONLY);
00207 
00208     SWBuf lineBuffer;
00209     SWBuf currentKey;
00210     SWBuf currentEntry;
00211 
00212     while (FileMgr::getLine(fd, lineBuffer)) {
00213         if (lineBuffer.startsWith("$$$")) {
00214             writeEntry(currentKey, currentEntry, module);
00215             currentKey = lineBuffer;
00216             currentKey << 3;
00217             currentKey.trim();
00218             currentEntry = "";
00219         }
00220         else {
00221             currentEntry += lineBuffer;
00222         }
00223     }
00224     writeEntry(currentKey, currentEntry, module);
00225 
00226     FileMgr::getSystemFileMgr()->close(fd);
00227 
00228     delete module;
00229     delete vkey;
00230 
00231     return 0;
00232 }

void usage ( const char *  progName,
const char *  error = 0 
)

Definition at line 43 of file imp2vs.cpp.

00043                                                         {
00044     if (error) fprintf(stderr, "\n%s: %s\n", progName, error);
00045     fprintf(stderr, "\n=== imp2vs (Revision $Rev: 2234 $) SWORD Bible/Commentary importer.\n");
00046     fprintf(stderr, "\nusage: %s <imp_file> [options]\n", progName);
00047     fprintf(stderr, "  -a\t\t\t augment module if exists (default is to create new)\n");
00048     fprintf(stderr, "  -z\t\t\t use ZIP compression (default no compression)\n");
00049     fprintf(stderr, "  -Z\t\t\t use LZSS compression (default no compression)\n");
00050     fprintf(stderr, "  -o <output_path>\t where to write data files.\n");
00051     fprintf(stderr, "  -4\t\t\t use 4 byte size entries (default is 2).\n");
00052     fprintf(stderr, "  -b <2|3|4>\t\t compression block size (default 4):\n");
00053     fprintf(stderr, "\t\t\t\t 2 - verse; 3 - chapter; 4 - book\n");
00054     fprintf(stderr, "  -v <v11n>\t\t specify a versification scheme to use (default is KJV)\n");
00055     fprintf(stderr, "\t\t\t\t Note: The following are valid values for v11n:\n");
00056     VersificationMgr *vmgr = VersificationMgr::getSystemVersificationMgr();
00057     StringList av11n = vmgr->getVersificationSystems();
00058     for (StringList::iterator loop = av11n.begin(); loop != av11n.end(); loop++) {
00059         fprintf(stderr, "\t\t\t\t\t%s\n", (*loop).c_str());
00060         }
00061     fprintf(stderr, "  -l <locale>\t\t specify a locale scheme to use (default is en)\n");
00062     fprintf(stderr, "\n");
00063     fprintf(stderr, "'imp' format is a simple standard for importing data into SWORD modules.\n"
00064         "Required is a plain text file containing $$$key lines followed by content.\n\n"
00065         "$$$Gen.1.1\n"
00066         "In the beginning God created\n"
00067         "the heavens and the earth\n"
00068         "$$$Gen.1.2\n"
00069         "and the earth...\n\n"
00070         "Key lines can contain ranges, for example, a commentary entry which discusses\n"
00071         "John 1:1-4 might have a key, $$$Jn.1.1-4.  Special keys for intro entries use\n"
00072         "standard SWORD notation, e.g. $$$Rom.4.0 for intro of Romans chapter 4,\n"
00073         "$$$Rev.0.0 for intro of the Book of Revelation of John.  $$$[ Module Heading ]\n"
00074         "for entire module intro.  $$$[ Testament 2 Heading ] for NT intro.\n\n");
00075     exit(-1);
00076 }

void writeEntry ( const SWBuf &  key,
const SWBuf &  entry,
SWModule *  module 
)

Definition at line 239 of file imp2vs.cpp.

00240 {
00241     if (key.size() && entry.size()) {
00242         std::cout << "from file: " << key << std::endl;
00243         VerseKey *vkey = (VerseKey *)module->getKey();
00244         VerseKey *linkMaster = (VerseKey *)module->createKey();
00245 
00246         ListKey listKey = vkey->parseVerseList(key.c_str(), "Gen1:1", true);
00247 
00248         bool first = true;
00249         for (listKey = TOP; !listKey.popError(); listKey++) {
00250             *vkey = listKey;
00251             if (first) {
00252                 *linkMaster = *vkey;
00253                 SWBuf text = module->getRawEntry();
00254                 text += entry;
00255 
00256 
00257                 //------------------------------------------------------------
00258                 //  Tregelles Page marking special stuff
00259                 //------------------------------------------------------------
00260 /*
00261                 const char *pageMarker = "<seg type=\"page\" subtype=\"";
00262                 int newPage = page;
00263                 SWBuf pageData = strstr(text.c_str(), pageMarker);
00264                 if (pageData.length()) {
00265                     pageData << strlen(pageMarker);
00266                     const char *pn = pageData.stripPrefix('"');
00267                     if (pn) newPage = atoi(pn);
00268                 }
00269                 // add page stuff for treg
00270                 if (text.startsWith(pageMarker)) {
00271                     // don't add anything cuz we already start with one
00272                 }
00273                 else {
00274                     SWBuf pm = pageMarker;
00275                     pm.appendFormatted("%d\" />", page);
00276                     text = pm + text;
00277                 }
00278 
00279                 page = newPage; // when our line set a new page number
00280 
00281 */
00282                 //------------------------------------------------------------
00283 
00284 
00285 
00286 
00287                 std::cout << "adding entry: " << *vkey << " length " << entry.size() << "/" << (unsigned short)text.size() << std::endl;
00288                 module->setEntry(text);
00289                 first = false;
00290             }
00291             else {
00292                 std::cout << "linking entry: " << *vkey << " to " << *linkMaster << std::endl;
00293                 module->linkEntry(linkMaster);
00294             }
00295         }
00296 
00297         delete linkMaster;
00298     }
00299 }


Variable Documentation

int page = 0

Definition at line 236 of file imp2vs.cpp.


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