The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
addgb.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * addgb.cpp - Utility to create/modify a GenBook module by adding a single
4  * entry
5  *
6  * $Id: addgb.cpp 3063 2014-03-04 13:04:11Z chrislit $
7  *
8  * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #ifdef _MSC_VER
25  #pragma warning( disable: 4251 )
26 #endif
27 
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 
34 #ifndef __GNUC__
35 #include <io.h>
36 #else
37 #include <unistd.h>
38 #endif
39 
40 #include <entriesblk.h>
41 #include <iostream>
42 #include <string>
43 #include <stdio.h>
44 #include <treekeyidx.h>
45 #include <rawgenbook.h>
46 
47 #ifndef NO_SWORD_NAMESPACE
48 using sword::TreeKeyIdx;
49 using sword::RawGenBook;
50 using sword::SWKey;
51 #endif
52 
53 
54 void printTree(TreeKeyIdx treeKey, TreeKeyIdx *target = 0, int level = 1) {
55  if (!target)
56  target = &treeKey;
57 
58  unsigned long currentOffset = target->getOffset();
59  std::cout << ((currentOffset == treeKey.getOffset()) ? "==>" : "");
60  for (int i = 0; i < level; i++) std::cout << "\t";
61  std::cout << treeKey.getLocalName() << "/\n";
62  if (treeKey.firstChild()) {
63  printTree(treeKey, target, level+1);
64  treeKey.parent();
65  }
66  if (treeKey.nextSibling())
67  printTree(treeKey, target, level);
68 
69 }
70 
71 
72 int main(int argc, char **argv) {
73 
74  const char * helptext ="addgb 1.0 General Book module creation tool for the SWORD Project\nUse -a to add a new leaf entry from standard input or a file\n usage:\n %s -a <filename> <key> [</path/to/file/with/entry>]\n";
75 
76  // const char * helptext ="addgb 1.0 General Book module creation tool for the SWORD Project\nUse -a to add a new leaf entry from standard input or a file, -d to delete an\nentry, -l to link two leaf entries.\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";
77 
78  char mode;
79  unsigned long entrysize;
80 
81  if (argc < 3) {
82  fprintf(stderr, helptext, argv[0]);
83  exit(-1);
84  }
85 
86  mode = argv[1][1];
87 
88  // Do some initialization stuff
89  TreeKeyIdx *treeKey = new TreeKeyIdx(argv[2]);
90  if (treeKey->popError()) {
91  treeKey->create(argv[2]);
92  delete treeKey;
93  treeKey = new TreeKeyIdx(argv[2]);
94  RawGenBook::createModule(argv[2]);
95  }
96  delete treeKey;
97  RawGenBook *book = new RawGenBook(argv[2]);
98 
99  if ((mode == 'a') && (argc == 4 || argc == 5)) {
100  char buffer[1048576]; //this is the max size of any entry
101 
102 
103  FILE *infile;
104  // case: add from text file
105  //Open our data file and read its contents into the buffer
106  if (argc == 5) infile = fopen(argv[4], "r");
107  // case: add from stdin
108  else infile = stdin;
109 
110  entrysize = fread(buffer, sizeof(char), sizeof(buffer), infile);
111  book->setKey(argv[3]);
112  book->setEntry(buffer, entrysize); // save text to module at current position
113  }
114 
115  /*
116  // let's pretend these don't exist for the time being
117 
118  // Link 2 verses
119  else if ((mode == 'l') && argc == 5) {
120  *key = argv[3];
121  mod.setKey(*key);
122 
123  SWKey tmpkey = argv[4];
124  mod << &(tmpkey);
125  }
126 
127  // Delete an entry
128  else if ((mode == 'd') && argc == 4) {
129  mod.setKey(argv[3]);
130  mod.deleteEntry();
131  }
132  */
133  // Bad arguments, print usage
134  else {
135  fprintf(stderr, helptext, argv[0]);
136  exit(-1);
137  }
138 
139  //DEBUG printTree(root, treeKey);
140 
141  delete treeKey;
142  delete book;
143  return 0;
144 }
void printTree(TreeKeyIdx treeKey, TreeKeyIdx *target=0, int level=1)
Definition: addgb.cpp:54
virtual const char * getLocalName()
Definition: treekeyidx.cpp:91
static char createModule(const char *ipath)
Definition: rawgenbook.cpp:191
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
static signed char create(const char *path)
Definition: treekeyidx.cpp:290
virtual bool nextSibling()
Definition: treekeyidx.cpp:168
virtual bool parent()
Definition: treekeyidx.cpp:148
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawgenbook.cpp:134
virtual char popError()
Definition: swkey.cpp:147
unsigned long getOffset() const
Definition: treekeyidx.cpp:410
virtual bool firstChild()
Definition: treekeyidx.cpp:158