The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
addld.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * addld.cpp - Utility to build/modify an LD module by adding a single entry
4  *
5  * $Id: addld.cpp 3063 2014-03-04 13:04:11Z chrislit $
6  *
7  * Copyright 2000-2013 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 <ctype.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 
33 #ifndef __GNUC__
34 #include <io.h>
35 #else
36 #include <unistd.h>
37 #endif
38 
39 #include <swmgr.h>
40 #include <rawld.h>
41 #include <rawld4.h>
42 #include <zld.h>
43 #ifndef EXCLUDEZLIB
44 #include <zipcomprs.h>
45 #endif
46 
47 #ifndef NO_SWORD_NAMESPACE
48 using sword::SWMgr;
49 #ifndef EXCLUDEZLIB
50 using sword::ZipCompress;
51 #endif
52 using sword::RawLD4;
53 using sword::SWKey;
54 using sword::zLD;
55 using sword::RawLD;
56 #endif
57 
58 
59 int main(int argc, char **argv) {
60 
61  const char * helptext ="addld 1.0 Lexicon & Dictionary module creation tool for the SWORD Project\nUse -a to add a new LD entry from standard input or a file, -d to delete an\nentry, -l to link two LD entries, -c to create a new module.\n usage:\n %s -a <filename> <key> [</path/to/file/with/entry>]\n %s -d <filename> <key>\n %s -l <filename> <first key (already assigned)> <second key>\n %s -c <filename>\nTo use 4-byte LD instead of 2-byte, insert a 4 immediately after the '-'.\nTo use zLD instead of 2-byte, insert a z immediately after the '-'.\n";
62  long entrysize;
63 
64  bool fourbyte = false;
65  bool compress = false;
66  char mode;
67 
68  if (argc < 3) {
69  fprintf(stderr, helptext, argv[0], argv[0], argv[0], argv[0]);
70  exit(-1);
71  }
72 
73  if (argv[1][1] == '4') {
74  fourbyte = false;
75  mode = argv[1][2];
76  }
77  else if (argv[1][1] == 'z') {
78  compress = true;
79  mode = argv[1][2];
80  }
81  else {
82  mode = argv[1][1];
83  }
84 
85  if ((mode == 'a') && (argc == 4 || argc == 5)) {
86 
87  // Do some initialization stuff
88  if (fourbyte) {
89  char buffer[1048576]; //this is the max size of any entry
90  RawLD4 mod(argv[2]); // open our datapath with our RawText driver.
91  SWKey* key = mod.createKey();
92  key->setPersist(true); // the magical setting
93 
94  // Set our VerseKey
95  *key = argv[3];
96  mod.setKey(*key);
97  FILE *infile;
98  // case: add from text file
99  //Open our data file and read its contents into the buffer
100  if (argc == 5) infile = fopen(argv[4], "r");
101  // case: add from stdin
102  else infile = stdin;
103 
104  entrysize = fread(buffer, sizeof(char), sizeof(buffer), infile);
105  mod.setEntry(buffer, entrysize); // save text to module at current position
106  }
107  else if (compress) {
108 #ifndef EXCLUDEZLIB
109  char buffer[1048576]; //this is the max size of any entry
110  zLD mod(argv[2], 0, 0, 200, new ZipCompress()); // open our datapath with our RawText driver.
111  SWKey* key = mod.createKey();
112  key->setPersist(true); // the magical setting
113 
114  // Set our VerseKey
115  *key = argv[3];
116  mod.setKey(*key);
117  FILE *infile;
118  // case: add from text file
119  //Open our data file and read its contents into the buffer
120  if (argc == 5) infile = fopen(argv[4], "r");
121  // case: add from stdin
122  else infile = stdin;
123 
124  entrysize = fread(buffer, sizeof(char), sizeof(buffer), infile);
125  mod.setEntry(buffer, entrysize); // save text to module at current position
126 #else
127  fprintf(stderr, "error: %s: SWORD library not built with ZIP compression support.\n", argv[0]);
128  exit(-3);
129 #endif
130  }
131  else {
132  char buffer[65536]; //this is the max size of any entry
133  RawLD mod(argv[2]); // open our datapath with our RawText driver.
134  SWKey* key = mod.createKey();
135  key->setPersist(true); // the magical setting
136 
137  // Set our VerseKey
138  *key = argv[3];
139  mod.setKey(*key);
140  FILE *infile;
141  // case: add from text file
142  //Open our data file and read its contents into the buffer
143  if (argc == 5) infile = fopen(argv[4], "r");
144  // case: add from stdin
145  else infile = stdin;
146 
147  entrysize = fread(buffer, sizeof(char), sizeof(buffer), infile);
148  mod.setEntry(buffer, entrysize); // save text to module at current position
149  }
150 
151  }
152  // Link 2 verses
153  else if ((mode == 'l') && argc == 5) {
154  // Do some initialization stuff
155  if (fourbyte) {
156  RawLD4 mod(argv[2]); // open our datapath with our RawText driver.
157  SWKey* key = mod.createKey();
158  key->setPersist(true); // the magical setting
159 
160  *key = argv[3];
161  mod.setKey(*key);
162  SWKey tmpkey = argv[4];
163  mod << &(tmpkey);
164  }
165  else if (compress) {
166  zLD mod(argv[2]); // open our datapath with our RawText driver.
167  SWKey* key = mod.createKey();
168  key->setPersist(true); // the magical setting
169 
170  *key = argv[3];
171  mod.setKey(*key);
172 
173  SWKey tmpkey = argv[4];
174  mod << &(tmpkey);
175  }
176  else {
177  RawLD mod(argv[2]); // open our datapath with our RawText driver.
178  SWKey* key = mod.createKey();
179  key->setPersist(true); // the magical setting
180 
181  *key = argv[3];
182  mod.setKey(*key);
183 
184  SWKey tmpkey = argv[4];
185  mod << &(tmpkey);
186  }
187  }
188  else if ((mode == 'd') && argc == 4) {
189  if (fourbyte) {
190  RawLD4 mod(argv[2]); // open our datapath with our RawText driver.
191  mod.setKey(argv[3]);
192  mod.deleteEntry();
193  }
194  if (compress) {
195  zLD mod(argv[2]); // open our datapath with our RawText driver.
196  mod.setKey(argv[3]);
197  mod.deleteEntry();
198  }
199  else {
200  RawLD mod(argv[2]); // open our datapath with our RawText driver.
201  mod.setKey(argv[3]);
202  mod.deleteEntry();
203  }
204 
205  }
206  // Make a new module
207  else if ((mode == 'c') && argc == 3) {
208  // Try to initialize a default set of datafiles and indicies at our
209  // datapath location passed to us from the user.
210  if (fourbyte) {
211  if (RawLD4::createModule(argv[2])) {
212  fprintf(stderr, "error: %s: couldn't create module at path: %s \n", argv[0], argv[2]);
213  exit(-2);
214  }
215  }
216  if (compress) {
217  if (zLD::createModule(argv[2])) {
218  fprintf(stderr, "error: %s: couldn't create module at path: %s \n", argv[0], argv[2]);
219  exit(-2);
220  }
221  }
222  else {
223  if (RawLD::createModule(argv[2])) {
224  fprintf(stderr, "error: %s: couldn't create module at path: %s \n", argv[0], argv[2]);
225  exit(-2);
226  }
227  }
228  }
229 
230  // Bad arguments, print usage
231  else {
232  fprintf(stderr, helptext, argv[0], argv[0], argv[0], argv[0]);
233  exit(-1);
234  }
235 }
virtual void setEntry(const char *inbuf, long len=-1)
Definition: zld.cpp:146
Definition: rawld4.h:35
virtual void deleteEntry()
Definition: zld.cpp:176
Definition: zld.h:34
preg buffer
Definition: regex.c:8089
virtual char setKey(const SWKey *ikey)
Definition: swmodule.cpp:298
int main(int argc, char **argv)
Definition: addcomment.cpp:32
SWCompress * compress
Definition: step2vpl.cpp:140
Definition: rawld.h:35
static char createModule(const char *path)
Definition: rawld4.h:53
void setPersist(bool ipersist)
Definition: swkey.cpp:135
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawld.cpp:149
static char createModule(const char *path)
Definition: rawld.h:53
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawld4.cpp:147
virtual void deleteEntry()
Definition: rawld4.cpp:177
static char createModule(const char *path)
Definition: zld.h:49
Definition: swkey.h:77
virtual SWKey * createKey() const
Definition: swld.cpp:62
virtual void deleteEntry()
Definition: rawld.cpp:179