The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
genbookutil.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * genbookutil.cpp -
4  *
5  * $Id: genbookutil.cpp 3063 2014-03-04 13:04:11Z chrislit $
6  *
7  * Copyright 2002-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 <entriesblk.h>
28 #include <iostream>
29 #include <stdio.h>
30 #include <treekeyidx.h>
31 #include <rawgenbook.h>
32 
33 #ifndef NO_SWORD_NAMESPACE
34 using namespace sword;
35 #endif
36 
37 void printTree(TreeKeyIdx treeKey, TreeKeyIdx *target = 0, int level = 1) {
38  if (!target)
39  target = &treeKey;
40 
41  unsigned long currentOffset = target->getOffset();
42  std::cout << ((currentOffset == treeKey.getOffset()) ? "==>" : "");
43  for (int i = 0; i < level; i++) std::cout << "\t";
44  std::cout << treeKey.getLocalName() << "/\n";
45  if (treeKey.firstChild()) {
46  printTree(treeKey, target, level+1);
47  treeKey.parent();
48  }
49  if (treeKey.nextSibling())
50  printTree(treeKey, target, level);
51 
52 }
53 
54 
55 void printLocalName(TreeKeyIdx *treeKey) {
56  std::cout << "locaName: " << treeKey->getLocalName() << "\n";
57 }
58 
59 
60 void setLocalName(TreeKeyIdx *treeKey) {
61  char buf[1023];
62  std::cout << "Enter New Node Name: ";
63  fgets(buf, 1000, stdin);
64  SWBuf name = buf;
65  treeKey->setLocalName(name.trim());
66  treeKey->save();
67 }
68 
69 
70 void gotoPath(TreeKeyIdx *treeKey) {
71  char buf[1023];
72  std::cout << "Enter Path: ";
73  fgets(buf, 1000, stdin);
74  SWBuf path = buf;
75  (*treeKey) = path.trim();
76 }
77 
78 
79 void assurePath(TreeKeyIdx *treeKey) {
80  char buf[1023];
81  std::cout << "Enter Path: ";
82  fgets(buf, 1000, stdin);
83  SWBuf path = buf;
84  treeKey->assureKeyPath(path.trim());
85 }
86 
87 
88 void viewEntryText(RawGenBook *book) {
89  std::cout << "\n";
90  std::cout << book->renderText();
91  std::cout << "\n";
92 }
93 
94 
95 void setEntryText(RawGenBook *book) {
96  SWBuf body;
97  TreeKeyIdx *treeKey = (TreeKeyIdx *)(SWKey *)(*book);
98  if (treeKey->getOffset()) {
99  char buf[1023];
100  std::cout << "Enter New Entry Text ('.' on a line by itself to end): \n";
101  do {
102  fgets(buf, 1000, stdin);
103  SWBuf text = buf;
104  text.trim();
105  if ((text[0] == '.') && (text[1] == 0))
106  break;
107  body += text;
108  body += "\n";
109  } while (true);
110 
111  (*book) << body.c_str();
112  }
113  else std::cout << "Can't add entry text to root node\n";
114 }
115 
116 
117 void appendSibbling(TreeKeyIdx *treeKey) {
118  if (treeKey->getOffset()) {
119  char buf[1023];
120  std::cout << "Enter New Sibbling Name: ";
121  fgets(buf, 1000, stdin);
122  SWBuf name = buf;
123  treeKey->append();
124  treeKey->setLocalName(name.trim());
125  treeKey->save();
126  }
127  else std::cout << "Can't add sibling to root node\n";
128 }
129 
130 
131 void appendChild(TreeKeyIdx *treeKey) {
132  char buf[1023];
133  std::cout << "Enter New Child Name: ";
134  fgets(buf, 1000, stdin);
135  SWBuf name = buf;
136  treeKey->appendChild();
137  treeKey->setLocalName(name.trim());
138  treeKey->save();
139 }
140 
141 
142 void deleteNode(TreeKeyIdx *treeKey) {
143  std::cout << "Removing entry [" << treeKey->getText() << "]\n";
144  treeKey->remove();
145 }
146 
147 
148 void removeEntry(EntriesBlock *eb, int index) {
149  if (index < eb->getCount()) {
150  std::cout << "Removing entry [" << index << "]\n";
151  eb->removeEntry(index);
152  }
153  else std::cout << "Invalid entry number\n\n";
154 }
155 
156 
157 int main(int argc, char **argv) {
158 
159  if (argc != 2) {
160  fprintf(stderr, "usage: %s <tree/key/data/path>\n", *argv);
161  exit(-1);
162  }
163 
164  TreeKeyIdx *treeKey = new TreeKeyIdx(argv[1]);
165 
166  if (treeKey->popError()) {
167  RawGenBook::createModule(argv[1]);
168  }
169  delete treeKey;
170 
171  RawGenBook *book = new RawGenBook(argv[1]);
172  TreeKeyIdx root = *((TreeKeyIdx *)((SWKey *)(*book)));
173  treeKey = (TreeKeyIdx *)(SWKey *)(*book);
174 
175  SWBuf input;
176  char line[1024];
177 
178  do {
179  std::cout << "[" << treeKey->getText() << "] > ";
180  fgets(line, 1000, stdin);
181  input = line;
182  input.trim();
183  if (input.length() > 0) {
184  switch (input[0]) {
185  case 'n': printLocalName(treeKey); break;
186  case 's': setLocalName(treeKey); break;
187  case 'g': gotoPath(treeKey); break;
188  case 'G': assurePath(treeKey); break;
189  case 'p': root.root(); printTree(root, treeKey); break;
190  case 'a': appendSibbling(treeKey); break;
191  case 'c': appendChild(treeKey); break;
192  case 'd': deleteNode(treeKey); break;
193  case 'j': treeKey->nextSibling(); break;
194  case 'k': treeKey->previousSibling(); break;
195  case 'h': treeKey->parent(); break;
196  case 'l': treeKey->firstChild(); break;
197  case 'r': treeKey->root(); break;
198  case 't': setEntryText(book); break;
199  case 'v': viewEntryText(book); break;
200  case 'q': break;
201  case '?':
202  default:
203  std::cout << "\n p - print tree\n";
204  std::cout << " n - get local name\n";
205  std::cout << " s - set local name\n";
206  std::cout << " j - next sibbling\n";
207  std::cout << " k - previous sibbling\n";
208  std::cout << " h - parent\n";
209  std::cout << " l - first child\n";
210  std::cout << " r - root\n";
211  std::cout << " g - goto path\n";
212  std::cout << " G goto path; create if it doesn't exist\n";
213  std::cout << " a - append sibbling\n";
214  std::cout << " c - append child\n";
215  std::cout << " d - delete node\n";
216  std::cout << " v - view entry text\n";
217  std::cout << " t - set entry text\n";
218  std::cout << " q - quit\n\n";
219  break;
220  }
221  }
222  }
223  while (input.compare("q"));
224 
225  delete treeKey;
226 
227  return 0;
228 }
void appendSibbling(TreeKeyIdx *treeKey)
void removeEntry(EntriesBlock *eb, int index)
void printTree(TreeKeyIdx treeKey, TreeKeyIdx *target=0, int level=1)
Definition: addgb.cpp:54
void deleteNode(TreeKeyIdx *treeKey)
static char createModule(const char *ipath)
Definition: rawgenbook.cpp:191
void setLocalName(TreeKeyIdx *treeKey)
Definition: genbookutil.cpp:60
void viewEntryText(RawGenBook *book)
Definition: genbookutil.cpp:88
void printLocalName(TreeKeyIdx *treeKey)
Definition: genbookutil.cpp:55
int main(int argc, char **argv)
Definition: addcomment.cpp:32
void removeEntry(int entryIndex)
Definition: entriesblk.cpp:166
void assurePath(TreeKeyIdx *treeKey)
Definition: genbookutil.cpp:79
void gotoPath(TreeKeyIdx *treeKey)
Definition: genbookutil.cpp:70
void appendChild(TreeKeyIdx *treeKey)
void setEntryText(RawGenBook *book)
Definition: genbookutil.cpp:95