summaryrefslogtreecommitdiff
path: root/utilities/imp2vs.cpp
diff options
context:
space:
mode:
authorRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:54:01 -0400
committerRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:54:01 -0400
commit71a39f4652cd51df814c930dd268f3c9ad2aee86 (patch)
tree5994350a603908c4e4d660bc9d72c4ec43dd648e /utilities/imp2vs.cpp
parent03134fa5f6f25d92724ce4c183f9bbe12a9e37dc (diff)
Imported Upstream version 1.6.0+dfsg
Diffstat (limited to 'utilities/imp2vs.cpp')
-rw-r--r--utilities/imp2vs.cpp373
1 files changed, 203 insertions, 170 deletions
diff --git a/utilities/imp2vs.cpp b/utilities/imp2vs.cpp
index 9ae3881..62d34a1 100644
--- a/utilities/imp2vs.cpp
+++ b/utilities/imp2vs.cpp
@@ -1,182 +1,215 @@
-#include <ctype.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <string>
-#include <fstream>
-using namespace std;
+/*
+ * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
+ * CrossWire Bible Society
+ * P. O. Box 2528
+ * Tempe, AZ 85280-2528
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ */
#include <iostream>
#include <rawtext.h>
- #include <rawtext4.h>
+#include <rawtext4.h>
#include <versekey.h>
-
+#include <swbuf.h>
+#include <filemgr.h>
+#include <stdio.h>
#ifndef NO_SWORD_NAMESPACE
-using sword::RawText;
-using sword::RawText4;
-using sword::VerseKey;
-using sword::SWText;
-using sword::ListKey;
+using namespace sword;
#endif
+using namespace std;
+
+void writeEntry(const SWBuf &key, const SWBuf &entry, SWModule *module);
+
+void usage(const char *progName, const char *error = 0) {
+ if (error) fprintf(stderr, "\n%s: %s\n", progName, error);
+ fprintf(stderr, "\n=== imp2vs (Revision $Rev: 2234 $) SWORD Bible/Commentary importer.\n");
+ fprintf(stderr, "\nusage: %s <imp_file> [options]\n", progName);
+ fprintf(stderr, " -a\t\t\t augment module if exists (default is to create new)\n");
+ fprintf(stderr, " -o <output_path>\t where to write data files.\n");
+ fprintf(stderr, " -4\t\t\t use 4 byte size entries (default is 2).\n");
+ fprintf(stderr, " -v <v11n>\t\t specify a versification scheme to use (default is KJV)\n");
+ fprintf(stderr, "\t\t\t\t Note: The following are valid values for v11n:\n");
+ VerseMgr *vmgr = VerseMgr::getSystemVerseMgr();
+ StringList av11n = vmgr->getVersificationSystems();
+ for (StringList::iterator loop = av11n.begin(); loop != av11n.end(); loop++) {
+ fprintf(stderr, "\t\t\t\t\t%s\n", (*loop).c_str());
+ }
+ fprintf(stderr, "\n");
+ fprintf(stderr, "'imp' format is a simple standard for importing data into SWORD modules.\n"
+ "Required is a plain text file containing $$$key lines followed by content.\n\n"
+ "$$$Gen.1.1\n"
+ "In the beginning God created\n"
+ "the heavens and the earth\n"
+ "$$$Gen.1.2\n"
+ "and the earth...\n\n"
+ "Key lines can contain ranges, for example, a commentary entry which discusses\n"
+ "John 1:1-4 might have a key, $$$Jn.1.1-4. Special keys for intro entries use\n"
+ "standard SWORD notation, e.g. $$$Rom.4.0 for intro of Romans chapter 4,\n"
+ "$$$Rev.0.0 for intro of the Book of Revelation of John. $$$[ Module Heading ]\n"
+ "for entire module intro. $$$[ Testament 2 Heading ] for NT intro.\n\n");
+ exit(-1);
+}
+
+
int main(int argc, char **argv) {
-
- const string helptext ="imp2vs 1.0 Bible/Commentary module creation tool for the SWORD Project\n usage:\n %s <filename> [output dir] \n";
-
- string keybuffer = "";
- string entbuffer = "";
- string linebuffer = "";
- string modname = "";
-
- if (argc > 2) {
- modname = argv[2];
- }
- else if (argc > 1) {
- modname = "./";
- }
- else {
- fprintf(stderr, helptext.c_str(), argv[0]);
- exit(-1);
- }
- int mode = 1;
- if (argc > 3) {
- switch (*argv[3]) {
- case '4':
- mode = 2;
- break;
- default:
- mode = 1;
- }
- }
-
-
-
- try {
-
- ifstream infile(argv[1]);
-
- SWText* mod;
- RawText * modRaw;
- RawText4 * modRaw4;
-
- if (mode == 1) {
- RawText::createModule(modname.c_str());
- modRaw= new RawText(modname.c_str());
- mod = modRaw;
- } else {
- RawText4::createModule(modname.c_str());
- modRaw4= new RawText4(modname.c_str());
- mod = modRaw4;
- }
-
- VerseKey* vkey = new VerseKey;
- vkey->Headings(1);
- vkey->AutoNormalize(0);
- vkey->Persist(1);
- mod->setKey(*vkey);
- char final; // 2 == pre-final line; 1 == final line; 0 == EOF
-
- getline(infile,linebuffer);
- final = (!infile.eof()) + 1;
-
- while (final) {
- if (final == 1 || (linebuffer.size() > 3 && linebuffer.substr(0,3) == "$$$")) {
- if (keybuffer.size() && entbuffer.size()) {
- std::cout << "from file: " << keybuffer << std::endl;
- *vkey = keybuffer.c_str();
- if (!vkey->Chapter()) {
- // bad hack: 0:0 is Book intro; (chapter):0 is Chapter intro; 0:2 is Module intro; 0:1 is Testament intro
- int backstep = vkey->Verse();
- if (backstep) {
- vkey->Verse(1);
- vkey->Chapter(1);
- switch (backstep) {
- case 2:
- vkey->Book(1);
- vkey->Testament(0);
- case 1:
- vkey->Book(0);
- vkey->Chapter(0);
- }
- vkey->Verse(0);
- }
-
- std::cout << "adding entry: " << *vkey << " length " << entbuffer.size() << "/" << (unsigned short)entbuffer.size() << std::endl;
- mod->setEntry(entbuffer.c_str(), entbuffer.size());
+
+
+ // handle options
+ if (argc < 2) usage(*argv);
+
+ const char *progName = argv[0];
+ const char *inFileName = argv[1];
+ SWBuf v11n = "KJV";
+ SWBuf outPath = "./";
+ bool fourByteSize = false;
+ bool append = false;
+
+ for (int i = 2; i < argc; i++) {
+ if (!strcmp(argv[i], "-a")) {
+ append = true;
+ }
+ if (!strcmp(argv[i], "-4")) {
+ fourByteSize = true;
+ }
+ else if (!strcmp(argv[i], "-o")) {
+ if (i+1 < argc) outPath = argv[++i];
+ else usage(progName, "-o requires <output_path>");
+ }
+ else if (!strcmp(argv[i], "-v")) {
+ if (i+1 < argc) v11n = argv[++i];
+ else usage(progName, "-v requires <v11n>");
+ }
+ else usage(progName, (((SWBuf)"Unknown argument: ")+ argv[i]).c_str());
+ }
+ // -----------------------------------------------------
+ const VerseMgr::System *v = VerseMgr::getSystemVerseMgr()->getVersificationSystem(v11n);
+ if (!v) std::cout << "Warning: Versification " << v11n << " not found. Using KJV versification...\n";
+
+
+ // setup module
+ if (!append) {
+ if (!fourByteSize)
+ RawText::createModule(outPath, v11n);
+ else RawText4::createModule(outPath, v11n);
+ }
+
+ SWModule *module = (!fourByteSize)
+ ? (SWModule *)new RawText(outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, v11n)
+ : (SWModule *)new RawText4(outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, v11n);
+ // -----------------------------------------------------
+
+
+ // setup module key to allow full range of possible values, and then some
+ VerseKey *vkey = (VerseKey *)module->CreateKey();
+ vkey->Headings(1);
+ vkey->AutoNormalize(0);
+ vkey->Persist(1);
+ module->setKey(*vkey);
+ // -----------------------------------------------------
+
+
+ // process input file
+ FileDesc *fd = FileMgr::getSystemFileMgr()->open(inFileName, FileMgr::RDONLY);
+
+ SWBuf lineBuffer;
+ SWBuf currentKey;
+ SWBuf currentEntry;
+
+ while (FileMgr::getLine(fd, lineBuffer)) {
+ if (lineBuffer.startsWith("$$$")) {
+ writeEntry(currentKey, currentEntry, module);
+ currentKey = lineBuffer;
+ currentKey << 3;
+ currentKey.trim();
+ currentEntry = "";
+ }
+ else {
+ currentEntry += lineBuffer;
+ }
}
- else {
- ListKey listkey = vkey->ParseVerseList(keybuffer.c_str(), "Gen1:1", true);
- int i;
- bool havefirst = false;
- VerseKey firstverse;
- firstverse.Headings(1);
- firstverse.AutoNormalize(0);
- for (i = 0; i < listkey.Count(); i++) {
- VerseKey *element = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(i));
- if (element) {
- *vkey = element->LowerBound();
- VerseKey finalkey = element->UpperBound();
- finalkey.Headings(1);
- finalkey.AutoNormalize(0);
- if (!havefirst) {
- havefirst = true;
- firstverse = *vkey;
-
- std::cout << "adding entry: " << *vkey << " length " << entbuffer.size() << "/" << (unsigned short)entbuffer.size() << std::endl;
- mod->setEntry(entbuffer.c_str(), entbuffer.size());
- (*vkey)++;
- }
- while (! (finalkey < (*vkey))) {
- std::cout << "linking entry: " << *vkey << " to " << firstverse << std::endl;
- *mod << &firstverse;
- (*vkey)++;
- }
- }
- else {
- if (havefirst) {
- *vkey = (*listkey.GetElement(i));
- std::cout << "linking entry: " << *vkey << " to " << firstverse << std::endl;
- *mod << &firstverse;
- }
- else {
- *vkey = (*listkey.GetElement(i));
- havefirst = true;
- firstverse = *vkey;
-
- std::cout << "adding entry: " << *vkey << " length " << entbuffer.size() << "/" << (unsigned short)entbuffer.size() << std::endl;
- mod->setEntry(entbuffer.c_str(), entbuffer.size());
- }
- }
- }
+ writeEntry(currentKey, currentEntry, module);
+
+ FileMgr::getSystemFileMgr()->close(fd);
+
+ delete vkey;
+
+ return 0;
+}
+
+
+
+int page = 0;
+
+
+void writeEntry(const SWBuf &key, const SWBuf &entry, SWModule *module)
+{
+ if (key.size() && entry.size()) {
+ std::cout << "from file: " << key << std::endl;
+ VerseKey *vkey = (VerseKey *)module->getKey();
+ VerseKey *linkMaster = (VerseKey *)module->CreateKey();
+
+ ListKey listKey = vkey->ParseVerseList(key.c_str(), "Gen1:1", true);
+
+ bool first = true;
+ for (listKey = TOP; !listKey.Error(); listKey++) {
+ *vkey = listKey;
+ if (first) {
+ *linkMaster = *vkey;
+ SWBuf text = module->getRawEntry();
+ text += entry;
+
+
+ //------------------------------------------------------------
+ // Tregelles Page marking special stuff
+ //------------------------------------------------------------
+/*
+ const char *pageMarker = "<seg type=\"page\" subtype=\"";
+ int newPage = page;
+ SWBuf pageData = strstr(text.c_str(), pageMarker);
+ if (pageData.length()) {
+ pageData << strlen(pageMarker);
+ const char *pn = pageData.stripPrefix('"');
+ if (pn) newPage = atoi(pn);
+ }
+ // add page stuff for treg
+ if (text.startsWith(pageMarker)) {
+ // don't add anything cuz we already start with one
+ }
+ else {
+ SWBuf pm = pageMarker;
+ pm.appendFormatted("%d\" />", page);
+ text = pm + text;
+ }
+
+ page = newPage; // when our line set a new page number
+
+*/
+ //------------------------------------------------------------
+
+
+
+
+ std::cout << "adding entry: " << *vkey << " length " << entry.size() << "/" << (unsigned short)text.size() << std::endl;
+ module->setEntry(text);
+ first = false;
+ }
+ else {
+ std::cout << "linking entry: " << *vkey << " to " << *linkMaster << std::endl;
+ module->linkEntry(linkMaster);
+ }
+ }
+
+ delete linkMaster;
}
- }
- if (linebuffer.size() > 3)
- keybuffer = linebuffer.substr(3,linebuffer.size()) ;
- entbuffer.resize(0);
- }
- else {
- entbuffer.append(linebuffer);
- }
- final--;
- if (final) {
- getline(infile,linebuffer);
- final = (!infile.eof()) + 1;
- }
- }
- }
- catch (const std::exception& e) {
- std::cerr << "Exception: imp2vs failed: " << e.what() << std::endl;
- std::cerr << "Line: " << linebuffer.size() << " " << linebuffer << std::endl;
- std::cerr << "Key: " << keybuffer.size() << " " << keybuffer << std::endl;
- std::cerr << "Ent: " << entbuffer.size() << " " << entbuffer << std::endl;
- return -2;
- }
- catch (...) {
- std::cerr << "Exception: imp2vs failed" << std::endl;
- return -3;
- }
-
- return 0;
}