The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
addld.cpp File Reference
#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <io.h>
#include <swmgr.h>
#include <rawld.h>
#include <rawld4.h>
#include <zld.h>
#include <zipcomprs.h>
+ Include dependency graph for addld.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 59 of file addld.cpp.

59  {
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 }
Definition: rawld4.h:35
Definition: zld.h:34
preg buffer
Definition: regex.c:8089
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
static char createModule(const char *path)
Definition: rawld.h:53
static char createModule(const char *path)
Definition: zld.h:49
Definition: swkey.h:77