The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
imp2ld.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * imp2ld.cpp - Utility to import LD modules in IMP format
4  *
5  * $Id: imp2ld.cpp 3223 2014-05-01 05:56:07Z scribe $
6  *
7  * Copyright 2002-2014 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #ifdef _MSC_VER
24  #pragma warning( disable: 4251 )
25 #endif
26 
27 #include <string>
28 #include <vector>
29 #include <fstream>
30 #include <iostream>
31 #include <rawld.h>
32 #include <rawld4.h>
33 #include <zld.h>
34 #include <lzsscomprs.h>
35 #ifndef EXCLUDEZLIB
36 #include <zipcomprs.h>
37 #endif
38 #ifndef EXCLUDEBZIP2
39 #include <bz2comprs.h>
40 #endif
41 #ifndef EXCLUDEXZ
42 #include <xzcomprs.h>
43 #endif
44 #include <stdio.h>
45 
46 using std::string;
47 
48 #ifndef NO_SWORD_NAMESPACE
49 using namespace sword;
50 #endif
51 
52 void usage(const char *progName, const char *error = 0) {
53  if (error) fprintf(stderr, "\n%s: %s\n", progName, error);
54  fprintf(stderr, "\n=== imp2ld (Revision $Rev: 3223 $) SWORD lexicon importer.\n");
55  fprintf(stderr, "\nusage: %s <imp_file> [options]\n", progName);
56  fprintf(stderr, " -a\t\t\t augment module if exists (default is to create new)\n");
57  fprintf(stderr, " -z <l|z|b|x>\t\t use compression (default: none)\n");
58  fprintf(stderr, "\t\t\t\t l - LZSS; z - ZIP; b - bzip2; x - xz\n");
59  fprintf(stderr, " -o <output_path>\t\t where to write data files.\n");
60  fprintf(stderr, " -4\t\t\t use 4 byte size entries (default: 2).\n");
61  fprintf(stderr, " -b <entry_count>\t\t compression block size (default 30 entries)\n");
62  fprintf(stderr, " -s\t\t\t case sensitive keys (default is not case sensitive)\n");
63  fprintf(stderr, " -P\t\t\t disable key Strong's number padding (by default keys will be padded).");
64  fprintf(stderr, "\n");
65  fprintf(stderr, "'imp' format is a simple standard for importing data into SWORD modules.\n"
66  "Required is a plain text file containing $$$key lines followed by content.\n\n"
67  "$$$Abraham\n"
68  "Abraham was the father of Isaac...\n"
69  "He was called by God to leave his country and journey to the land of Canaan...\n"
70  "$$$Isaac\n"
71  "Isaac was the son of Abraham and Sarah...\n\n");
72  exit(-1);
73 }
74 
75 int main(int argc, char **argv) {
76 
77  std::vector<string> linkbuffer;
78  signed long i = 0;
79  string keybuffer;
80  string entbuffer;
81  string linebuffer;
82  char links = 0;
83  string modname;
84  SWBuf outPath = "";
85  bool append = false;
86  long blockCount = 30;
87  bool caseSensitive = false;
88  SWCompress *compressor = 0;
89  SWBuf compType = "";
90  bool fourByteSize = false;
91  bool strongsPadding = true;
92 
93  if (argc < 2) usage(*argv);
94 
95  const char *progName = argv[0];
96  const char *inFileName = argv[1];
97 
98  for (int i = 2; i < argc; i++) {
99  if (!strcmp(argv[i], "-a")) {
100  append = true;
101  }
102  else if (!strcmp(argv[i], "-z")) {
103  if (fourByteSize) usage(*argv, "Cannot specify both -z and -4");
104  compType = "ZIP";
105  if (i+1 < argc && argv[i+1][0] != '-') {
106  switch (argv[++i][0]) {
107  case 'l': compType = "LZSS"; break;
108  case 'z': compType = "ZIP"; break;
109  case 'b': compType = "BZIP2"; break;
110  case 'x': compType = "XZ"; break;
111  }
112  }
113  }
114  else if (!strcmp(argv[i], "-Z")) {
115  if (compType.size()) usage(*argv, "Cannot specify both -z and -Z");
116  if (fourByteSize) usage(*argv, "Cannot specify both -Z and -4");
117  compType = "LZSS";
118  }
119  else if (!strcmp(argv[i], "-4")) {
120  fourByteSize = true;
121  }
122  else if (!strcmp(argv[i], "-P")) {
123  strongsPadding = false;
124  }
125  else if (!strcmp(argv[i], "-b")) {
126  if (i+1 < argc) {
127  blockCount = atoi(argv[++i]);
128  if (blockCount > 0) continue;
129  }
130  usage(*argv, "-b requires in entry count integer > 0");
131  }
132  else if (!strcmp(argv[i], "-o")) {
133  if (i+1 < argc) outPath = argv[++i];
134  else usage(progName, "-o requires <output_path>");
135  }
136  else if (!strcmp(argv[i], "-s")) {
137  caseSensitive = true;
138  }
139  else usage(progName, (((SWBuf)"Unknown argument: ")+ argv[i]).c_str());
140  }
141 
142 
143 
144  if (outPath.size() < 1) {
145  for (i = 0; (i < 16) && (inFileName[i]) && (inFileName[i] != '.'); i++) {
146  outPath += inFileName[i];
147  }
148  }
149 
150  std::ifstream infile(inFileName);
151  if (!infile.is_open()) {
152  fprintf(stderr, "\nERROR: %s: could not open file for reading: %s\n\n", *argv, inFileName);
153  exit(-2);
154  }
155 
156  SWModule *mod = 0;
157  SWKey *key, *linkKey;
158 
159  if (compType == "LZSS") {
160  compressor = new LZSSCompress();
161  }
162  else if (compType == "ZIP") {
163 #ifndef EXCLUDEZLIB
164  compressor = new ZipCompress();
165 #else
166  usage(*argv, "ERROR: SWORD library not compiled with ZIP compression support.\n\tBe sure libz is available when compiling SWORD library");
167 #endif
168  }
169  else if (compType == "BZIP2") {
170 #ifndef EXCLUDEBZIP2
171  compressor = new Bzip2Compress();
172 #else
173  usage(*argv, "ERROR: SWORD library not compiled with bzip2 compression support.\n\tBe sure libbz2 is available when compiling SWORD library");
174 #endif
175  }
176  else if (compType == "XZ") {
177 #ifndef EXCLUDEXZ
178  compressor = new XzCompress();
179 #else
180  usage(*argv, "ERROR: SWORD library not compiled with xz compression support.\n\tBe sure liblzma is available when compiling SWORD library");
181 #endif
182  }
183 
184 
185  // setup module
186  if (!append) {
187  if (compressor) {
188  if (zLD::createModule(outPath)) {
189  fprintf(stderr, "ERROR: %s: couldn't create module at path: %s \n", *argv, outPath.c_str());
190  exit(-1);
191  }
192  }
193  else {
194  if (!fourByteSize)
195  RawLD::createModule(outPath);
196  else RawLD4::createModule(outPath);
197  }
198  }
199 
200  if (compressor) {
201  // Create a compressed text module allowing very large entries
202  // Taking defaults except for first, fourth, fifth and last argument
203  mod = new zLD(outPath, 0, 0, blockCount, compressor, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, caseSensitive, strongsPadding);
204  }
205  else {
206  mod = (!fourByteSize)
207  ? (SWModule *)new RawLD (outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, caseSensitive, strongsPadding)
208  : (SWModule *)new RawLD4(outPath, 0, 0, 0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0, caseSensitive, strongsPadding);
209  }
210 
211 
212 
213 
214  key = mod->createKey();
215  linkKey = mod->createKey();
216  key->setPersist(true);
217  mod->setKey(key);
218 
219  while (!infile.eof()) {
220  std::getline(infile, linebuffer);
221  if (linebuffer.size() > 3 && linebuffer.substr(0,3) == "$$$") {
222  if (keybuffer.size() && entbuffer.size()) {
223  std::cout << keybuffer << std::endl;
224  *key = keybuffer.c_str();
225 
226  mod->setEntry(entbuffer.c_str(), entbuffer.size());
227  for (i = 0; i < links; i++) {
228  std::cout << "Linking: " << linkbuffer[i] << std::endl;
229  *linkKey = linkbuffer[i].c_str();
230  mod->linkEntry(linkKey);
231  }
232  }
233  if (linebuffer.size() > 3)
234  keybuffer = linebuffer.substr(3,linebuffer.size());
235 
236  entbuffer.resize(0);
237  linkbuffer.clear();
238  links = 0;
239  }
240  else if (linebuffer.size() > 3 && linebuffer.substr(0,3) == "%%%") {
241  linkbuffer.push_back(linebuffer.substr(3,linebuffer.size()));
242  links++;
243  }
244  else {
245  entbuffer += linebuffer;
246  }
247  }
248 
249  //handle final entry
250  if (keybuffer.size() && entbuffer.size()) {
251  std::cout << keybuffer << std::endl;
252  *key = keybuffer.c_str();
253 
254  mod->setEntry(entbuffer.c_str(), entbuffer.size());
255  for (i = 0; i < links; i++) {
256  std::cout << "Linking: " << linkbuffer[i] << std::endl;
257  *linkKey = linkbuffer[i].c_str();
258  mod->linkEntry(linkKey);
259  }
260  }
261 
262  infile.close();
263 
264  delete linkKey;
265  delete mod;
266  delete key;
267 
268  return 0;
269 }
int main(int argc, char **argv)
Definition: addcomment.cpp:32
static char createModule(const char *path)
Definition: rawld4.h:53
static char createModule(const char *path)
Definition: rawld.h:53
void usage(const char *app)
Definition: imp2gbs.cpp:65
static char createModule(const char *path)
Definition: zld.h:49
SWBuf outPath
Definition: imp2gbs.cpp:55
const char * string
Definition: regex.c:5014