24 #pragma warning( disable: 4251 )
41 #ifndef NO_SWORD_NAMESPACE
42 using sword::TreeKeyIdx;
43 using sword::RawGenBook;
52 #define HELPTEXT "xml2gbs 1.0 OSIS/ThML/TEI General Book module creation tool for the SWORD Project\n usage:\n xml2gbs [-l] [-i] [-fT|-fO|-fE] <filename> [modname]\n -l uses long div names in ThML files\n -i exports to IMP format instead of creating a module\n -fO, -fT, and -fE will set the importer to expect OSIS, ThML, or TEI format respectively\n (otherwise it attempts to autodetect)\n"
58 std::ifstream infile(filename);
61 if (!infile.is_open()) {
63 std::cerr << std::endl << std::endl <<
"Could not open file \"" << filename <<
"\"" << std::endl;
66 while (std::getline(infile, entbuffer) && format ==
F_AUTODETECT) {
67 if (strstr(entbuffer.c_str(),
"<osis")) {
70 else if (strstr(entbuffer.c_str(),
"<ThML")) {
73 else if (strstr(entbuffer.c_str(),
"<TEI")) {
83 int processXML(
const char* filename,
char* modname,
bool longnames,
bool exportfile,
unsigned char format) {
89 printf (
"%s :%s :%d :%d :%d\n\n", filename, modname, longnames, exportfile, format);
92 std::ifstream infile(filename);
93 if (!infile.is_open()) {
95 std::cerr << std::endl << std::endl <<
"Could not open file \"" << filename <<
"\"" << std::endl;
98 std::ofstream outfile;
100 strcat (modname,
".imp");
101 outfile.open(modname);
115 unsigned long entrysize = 0;
116 unsigned long keysize = 0;
133 while ((c = infile.get()) != EOF) {
137 while ((c = infile.get()) !=
'>')
142 if (keybuffer.length()) {
143 if (((format ==
F_OSIS) && ((!strncmp(keybuffer.c_str(),
"/div>", 5)) || (!strncmp(keybuffer.c_str(),
"/verse>", 7)) || (!strncmp(keybuffer.c_str(),
"/chapter>", 9)))) ||
144 ((format ==
F_THML) && ((!strncmp(keybuffer.c_str(),
"/div", 4)) && (keybuffer[4] >
'0' && keybuffer[4] <
'7')))) {
148 for (i = 0; i < level; i++) {
151 keybuffer2 += divs[i];
152 keysize += divs[i].length();
153 std::cout << keybuffer2 << std::endl;
157 std::cout << keybuffer2 << std::endl;
159 outfile <<
"$$$" << keybuffer2 << std::endl << entbuffer << std::endl;
162 book->
setKey(keybuffer2.c_str());
163 book->
setEntry(entbuffer.c_str(), entrysize);
173 else if (((format ==
F_OSIS) && !((!strncmp(keybuffer.c_str(),
"div>", 4) || !strncmp(keybuffer.c_str(),
"div ", 4)) || (!strncmp(keybuffer.c_str(),
"verse>", 6) || !strncmp(keybuffer.c_str(),
"verse ", 6)) || (!strncmp(keybuffer.c_str(),
"chapter>", 8) || !strncmp(keybuffer.c_str(),
"chapter ", 8)))) ||
174 ((format ==
F_THML) && !((!strncmp(keybuffer.c_str(),
"div", 3)) && (keybuffer[3] >
'0' && keybuffer[3] <
'7')))) {
177 entrysize += keybuffer.length();
178 entbuffer += keybuffer;
185 for (i = 0; i < level; i++) {
188 keybuffer2 += divs[i];
189 keysize += divs[i].length();
190 std::cout << keybuffer2 << std::endl;
194 std::cout << keybuffer2 << std::endl;
196 outfile <<
"$$$" << keybuffer2 << std::endl << entbuffer << std::endl;
199 book->
setKey(keybuffer2.c_str());
200 book->
setEntry(entbuffer.c_str(), entrysize);
209 keysize = keybuffer.length()-1;
215 if (format ==
F_OSIS && longnames ==
false) {
216 strtmp = (
char*)strstr(keybuffer.c_str(),
"osisID=\"");
219 for (;*strtmp !=
'\"'; strtmp++) {
223 else if (*strtmp ==
'.') {
226 else if (*strtmp != 13) {
234 strtmp = (
char*)strstr(keybuffer.c_str(),
"type=\"");
237 for (;*strtmp !=
'\"'; strtmp++) {
241 else if (*strtmp != 13) {
247 strtmp = (
char*)strstr(keybuffer.c_str(),
"n=\"");
250 for (;*strtmp !=
'\"'; strtmp++) {
254 else if (*strtmp != 13) {
261 strtmp = (
char*)strstr(keybuffer.c_str(),
"title=\"");
264 for (;*strtmp !=
'\"'; strtmp++) {
268 else if (*strtmp != 13) {
274 else if (format ==
F_THML) {
275 strtmp = (
char*)strstr(keybuffer.c_str(),
"title=\"");
278 for (;*strtmp !=
'\"'; strtmp++) {
282 else if (*strtmp != 13) {
290 if (keybuffer.length() && n.length())
294 if (longnames && keybuffer.length())
296 if (longnames || !keybuffer.length())
299 divs[level-1] = keybuffer;
319 int main(
int argc,
char **argv) {
327 bool longnames =
false;
328 bool exportfile =
false;
332 for (i = 1; i < argc; i++) {
333 if (argv[i][0] ==
'-') {
334 switch (argv[i][1]) {
342 if (argv[i][2] ==
'O') {
345 else if (argv[i][2] ==
'T') {
354 else if (*filename == 0) {
355 strncpy (filename, argv[i], 200);
357 else if (*modname == 0) {
358 strncpy (modname, argv[i], 200);
363 strncpy (filename, argv[1], 200);
372 for (i = 0; (i < 256) && (filename[i]) && (filename[i] !=
'.'); i++) {
373 modname[i] = filename[i];
381 fprintf(stderr,
"\n\nCould not detect file format for file \"%s\", please specify.\n", filename);
385 int retCode =
processXML (filename, modname, longnames, exportfile, format);
static char createModule(const char *ipath)
virtual char setKey(const SWKey *ikey)
static signed char create(const char *path)
unsigned char detectFormat(char *filename)
virtual void setEntry(const char *inbuf, long len=-1)
int processXML(const char *filename, char *modname, bool longnames, bool exportfile, unsigned char format)