The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <bz2comprs.h>
#include <xzcomprs.h>
#include <localemgr.h>
#include <cipherfil.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 (SWModule *module, const SWBuf &key, const SWBuf &entry, bool replace)
 

Variables

int page = 0
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 96 of file imp2vs.cpp.

96  {
97 
98 
99  // handle options
100  if (argc < 2) usage(*argv);
101 
102  const char *progName = argv[0];
103  const char *inFileName = argv[1];
104  SWBuf v11n = "KJV";
105  SWBuf outPath = "./";
106  SWBuf locale = "en";
107 
108  bool fourByteSize = false;
109  bool append = false;
110  bool replace = false;
111  int iType = 4;
112  SWBuf cipherKey = "";
113  SWCompress *compressor = 0;
114  SWBuf compType = "";
115 
116  for (int i = 2; i < argc; i++) {
117  if (!strcmp(argv[i], "-a")) {
118  append = true;
119  }
120  else if (!strcmp(argv[i], "-r")) {
121  replace = true;
122  }
123  else if (!strcmp(argv[i], "-z")) {
124  if (fourByteSize) usage(*argv, "Cannot specify both -z and -4");
125  compType = "ZIP";
126  if (i+1 < argc && argv[i+1][0] != '-') {
127  switch (argv[++i][0]) {
128  case 'l': compType = "LZSS"; break;
129  case 'z': compType = "ZIP"; break;
130  case 'b': compType = "BZIP2"; break;
131  case 'x': compType = "XZ"; break;
132  }
133  }
134  }
135  else if (!strcmp(argv[i], "-Z")) {
136  if (compType.size()) usage(*argv, "Cannot specify both -z and -Z");
137  if (fourByteSize) usage(*argv, "Cannot specify both -Z and -4");
138  compType = "LZSS";
139  }
140  else if (!strcmp(argv[i], "-4")) {
141  fourByteSize = true;
142  }
143  else if (!strcmp(argv[i], "-b")) {
144  if (i+1 < argc) {
145  iType = atoi(argv[++i]);
146  if ((iType >= 2) && (iType <= 4)) continue;
147  }
148  usage(*argv, "-b requires one of <2|3|4>");
149  }
150  else if (!strcmp(argv[i], "-o")) {
151  if (i+1 < argc) outPath = argv[++i];
152  else usage(progName, "-o requires <output_path>");
153  }
154  else if (!strcmp(argv[i], "-v")) {
155  if (i+1 < argc) v11n = argv[++i];
156  else usage(progName, "-v requires <v11n>");
157  }
158  else if (!strcmp(argv[i], "-l")) {
159  if (i+1 < argc) locale = argv[++i];
160  else usage(progName, "-l requires <locale>");
161  }
162  else if (!strcmp(argv[i], "-c")) {
163  if (i+1 < argc) cipherKey = argv[++i];
164  else usage(*argv, "-c requires <cipher_key>");
165  }
166  else usage(progName, (((SWBuf)"Unknown argument: ")+ argv[i]).c_str());
167  }
168  // -----------------------------------------------------
170  if (!v) std::cout << "Warning: Versification " << v11n << " not found. Using KJV versification...\n";
171 
172  if (compType == "LZSS") {
173  compressor = new LZSSCompress();
174  }
175  else if (compType == "ZIP") {
176 #ifndef EXCLUDEZLIB
177  compressor = new ZipCompress();
178 #else
179  usage(*argv, "ERROR: SWORD library not compiled with ZIP compression support.\n\tBe sure libz is available when compiling SWORD library");
180 #endif
181  }
182  else if (compType == "BZIP2") {
183 #ifndef EXCLUDEBZIP2
184  compressor = new Bzip2Compress();
185 #else
186  usage(*argv, "ERROR: SWORD library not compiled with bzip2 compression support.\n\tBe sure libbz2 is available when compiling SWORD library");
187 #endif
188  }
189  else if (compType == "XZ") {
190 #ifndef EXCLUDEXZ
191  compressor = new XzCompress();
192 #else
193  usage(*argv, "ERROR: SWORD library not compiled with xz compression support.\n\tBe sure liblzma is available when compiling SWORD library");
194 #endif
195  }
196 
197 
198  // setup module
199  if (!append) {
200  if (compressor) {
201  if (zText::createModule(outPath, iType, v11n)) {
202  fprintf(stderr, "ERROR: %s: couldn't create module at path: %s \n", *argv, outPath.c_str());
203  exit(-1);
204  }
205  }
206  else {
207  if (!fourByteSize)
208  RawText::createModule(outPath, v11n);
209  else RawText4::createModule(outPath, v11n);
210  }
211  }
212 
213  SWModule *module = 0;
214  if (compressor) {
215  // Create a compressed text module allowing very large entries
216  // Taking defaults except for first, fourth, fifth and last argument
217  module = new zText(
218  outPath, // ipath
219  0, // iname
220  0, // idesc
221  iType, // iblockType
222  compressor, // icomp
223  0, // idisp
224  ENC_UNKNOWN, // enc
225  DIRECTION_LTR, // dir
226  FMT_UNKNOWN, // markup
227  0, // lang
228  v11n // versification
229  );
230  }
231  else {
232  module = (!fourByteSize)
233  ? (SWModule *)new RawText(outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, v11n)
234  : (SWModule *)new RawText4(outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, v11n);
235  }
236 
237  SWFilter *cipherFilter = 0;
238 
239  if (cipherKey.length()) {
240  fprintf(stderr, "Adding cipher filter with phrase: %s\n", cipherKey.c_str() );
241  cipherFilter = new CipherFilter(cipherKey.c_str());
242  module->addRawFilter(cipherFilter);
243  }
244  // -----------------------------------------------------
245 
246  // setup locale manager
247 
249 
250 
251  // setup module key to allow full range of possible values, and then some
252 
253  VerseKey *vkey = (VerseKey *)module->createKey();
254  vkey->setIntros(true);
255  vkey->setAutoNormalize(false);
256  vkey->setPersist(true);
257  module->setKey(*vkey);
258  // -----------------------------------------------------
259 
260 
261  // process input file
262  FileDesc *fd = FileMgr::getSystemFileMgr()->open(inFileName, FileMgr::RDONLY);
263 
264  SWBuf lineBuffer;
265  SWBuf keyBuffer;
266  SWBuf entBuffer;
267 
268  bool more = true;
269  do {
270  more = FileMgr::getLine(fd, lineBuffer)!=0;
271  if (lineBuffer.startsWith("$$$")) {
272  if ((keyBuffer.size()) && (entBuffer.size())) {
273  writeEntry(module, keyBuffer, entBuffer, replace);
274  }
275  keyBuffer = lineBuffer;
276  keyBuffer << 3;
277  keyBuffer.trim();
278  entBuffer.size(0);
279  }
280  else {
281  if (keyBuffer.size()) {
282  entBuffer += lineBuffer;
283  entBuffer += "\n";
284  }
285  }
286  } while (more);
287  if ((keyBuffer.size()) && (entBuffer.size())) {
288  writeEntry(module, keyBuffer, entBuffer, replace);
289  }
290 
291  delete module;
292  if (cipherFilter)
293  delete cipherFilter;
294  delete vkey;
295 
296  FileMgr::getSystemFileMgr()->close(fd);
297 
298  return 0;
299 }
virtual void setDefaultLocaleName(const char *name)
Definition: localemgr.cpp:251
static unsigned int RDONLY
Definition: filemgr.h:75
static char createModule(const char *path, const char *v11n="KJV")
Definition: rawtext.h:46
SWText * module
Definition: osis2mod.cpp:105
SWBuf v11n
Definition: osis2mod.cpp:107
static LocaleMgr * getSystemLocaleMgr()
Definition: localemgr.cpp:54
void writeEntry(SWModule *book, SWBuf keyBuffer, SWBuf entBuffer)
Definition: imp2gbs.cpp:131
static char createModule(const char *path, const char *v11n="KJV")
Definition: rawtext4.h:48
static char createModule(const char *path, int blockBound, const char *v11n="KJV")
Definition: ztext.h:62
void usage(const char *app)
Definition: imp2gbs.cpp:65
static char getLine(FileDesc *fDesc, SWBuf &line)
Definition: filemgr.cpp:527
SWBuf outPath
Definition: imp2gbs.cpp:55
static VersificationMgr * getSystemVersificationMgr()
const System * getVersificationSystem(const char *name) const
Definition: ztext.h:39
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
void usage ( const char *  progName,
const char *  error = 0 
)

Definition at line 57 of file imp2vs.cpp.

57  {
58  if (error) fprintf(stderr, "\n%s: %s\n", progName, error);
59  fprintf(stderr, "\n=== imp2vs (Revision $Rev: 3741 $) SWORD Bible/Commentary importer.\n");
60  fprintf(stderr, "\nusage: %s <imp_file> [options]\n", progName);
61  fprintf(stderr, " -a\t\t\t augment module if exists (default is to create new)\n");
62  fprintf(stderr, " -r\t\t\t replace existing entries (default is to append)\n");
63  fprintf(stderr, " -z <l|z|b|x>\t\t use compression (default: none):\n");
64  fprintf(stderr, "\t\t\t\t l - LZSS; z - ZIP; b - bzip2; x - xz\n");
65  fprintf(stderr, " -o <output_path>\t where to write data files.\n");
66  fprintf(stderr, " -4\t\t\t use 4 byte size entries (default is 2).\n");
67  fprintf(stderr, " -b <2|3|4>\t\t compression block size (default 4):\n");
68  fprintf(stderr, "\t\t\t\t 2 - verse; 3 - chapter; 4 - book\n");
69  fprintf(stderr, " -v <v11n>\t\t specify a versification scheme to use (default is KJV)\n");
70  fprintf(stderr, "\t\t\t\t Note: The following are valid values for v11n:\n");
72  StringList av11n = vmgr->getVersificationSystems();
73  for (StringList::iterator loop = av11n.begin(); loop != av11n.end(); loop++) {
74  fprintf(stderr, "\t\t\t\t\t%s\n", (*loop).c_str());
75  }
76  fprintf(stderr, " -l <locale>\t\t specify a locale scheme to use (default is en)\n");
77  fprintf(stderr, " -c <cipher_key>\t encipher module using supplied key\n");
78  fprintf(stderr, "\t\t\t\t (default no enciphering)\n");
79  fprintf(stderr, "\n");
80  fprintf(stderr, "'imp' format is a simple standard for importing data into SWORD modules.\n"
81  "Required is a plain text file containing $$$key lines followed by content.\n\n"
82  "$$$Gen.1.1\n"
83  "In the beginning God created\n"
84  "the heavens and the earth\n"
85  "$$$Gen.1.2\n"
86  "and the earth...\n\n"
87  "Key lines can contain ranges, for example, a commentary entry which discusses\n"
88  "John 1:1-4 might have a key, $$$Jn.1.1-4. Special keys for intro entries use\n"
89  "standard SWORD notation, e.g. $$$Rom.4.0 for intro of Romans chapter 4,\n"
90  "$$$Rev.0.0 for intro of the Book of Revelation of John. $$$[ Module Heading ]\n"
91  "for entire module intro. $$$[ Testament 2 Heading ] for NT intro.\n\n");
92  exit(-1);
93 }
std::list< SWBuf > StringList
Definition: swmodule.cpp:91
const StringList getVersificationSystems() const
static VersificationMgr * getSystemVersificationMgr()
void writeEntry ( SWModule module,
const SWBuf key,
const SWBuf entry,
bool  replace 
)

Definition at line 306 of file imp2vs.cpp.

307 {
308  if (key.size() && entry.size()) {
309  std::cout << "from file: " << key << std::endl;
310  VerseKey *vkey = (VerseKey *)module->getKey();
311  VerseKey *linkMaster = (VerseKey *)module->createKey();
312 
313  ListKey listKey = vkey->parseVerseList(key.c_str(), "Gen1:1", true);
314 
315  bool first = true;
316  for (listKey = TOP; !listKey.popError(); listKey++) {
317  *vkey = listKey;
318  if (first) {
319  *linkMaster = *vkey;
320  SWBuf text = (replace) ? "" : module->getRawEntry();
321  if (text.length()) text += " ";
322  text += entry;
323 
324 
325  //------------------------------------------------------------
326  // Tregelles Page marking special stuff
327  //------------------------------------------------------------
328 /*
329  const char *pageMarker = "<seg type=\"page\" subtype=\"";
330  int newPage = page;
331  SWBuf pageData = strstr(text.c_str(), pageMarker);
332  if (pageData.length()) {
333  pageData << strlen(pageMarker);
334  const char *pn = pageData.stripPrefix('"');
335  if (pn) newPage = atoi(pn);
336  }
337  // add page stuff for treg
338  if (text.startsWith(pageMarker)) {
339  // don't add anything cuz we already start with one
340  }
341  else {
342  SWBuf pm = pageMarker;
343  pm.appendFormatted("%d\" />", page);
344  text = pm + text;
345  }
346 
347  page = newPage; // when our line set a new page number
348 
349 */
350  //------------------------------------------------------------
351 
352 
353 
354 
355  std::cout << "adding entry: " << *vkey << " length " << entry.size() << "/" << (unsigned short)text.size() << std::endl;
356  module->setEntry(text);
357  first = false;
358  }
359  else {
360  std::cout << "linking entry: " << *vkey << " to " << *linkMaster << std::endl;
361  module->linkEntry(linkMaster);
362  }
363  }
364 
365  delete linkMaster;
366  }
367 }
#define TOP
Definition: swkey.h:68
virtual SWKey * createKey() const
Definition: swtext.cpp:67
virtual void setEntry(const char *inbuf, long len=-1)
Definition: swmodule.cpp:1680
virtual SWKey * getKey() const
Definition: swmodule.cpp:284
SWText * module
Definition: osis2mod.cpp:105
virtual void linkEntry(const SWKey *sourceKey)
Definition: swmodule.cpp:1683
const char * getRawEntry() const
Definition: swmodule.h:500

Variable Documentation

int page = 0

Definition at line 303 of file imp2vs.cpp.