summaryrefslogtreecommitdiff
path: root/utilities/treeidxutil.cpp
diff options
context:
space:
mode:
authorRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:59 -0400
committerRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:59 -0400
commit03134fa5f6f25d92724ce4c183f9bbe12a9e37dc (patch)
tree847326a4de82f0241ac87cbbc427a1b92a696a02 /utilities/treeidxutil.cpp
parentd7469385b05b9510338407fa123e9ad090f80af6 (diff)
Imported Upstream version 1.5.11
Diffstat (limited to 'utilities/treeidxutil.cpp')
-rw-r--r--utilities/treeidxutil.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/utilities/treeidxutil.cpp b/utilities/treeidxutil.cpp
new file mode 100644
index 0000000..7aa6a68
--- /dev/null
+++ b/utilities/treeidxutil.cpp
@@ -0,0 +1,145 @@
+#include <entriesblk.h>
+#include <iostream>
+#include <string>
+#include <stdio.h>
+#include <treekeyidx.h>
+
+#ifndef NO_SWORD_NAMESPACE
+using namespace sword;
+#endif
+
+void printTree(TreeKeyIdx treeKey, TreeKeyIdx *target = 0, int level = 1) {
+ if (!target)
+ target = &treeKey;
+
+ unsigned long currentOffset = target->getOffset();
+ std::cout << ((currentOffset == treeKey.getOffset()) ? "==>" : "");
+ for (int i = 0; i < level; i++) std::cout << "\t";
+ std::cout << treeKey.getLocalName() << std::endl;
+ if (treeKey.firstChild()) {
+ printTree(treeKey, target, level+1);
+ treeKey.parent();
+ }
+ if (treeKey.nextSibling())
+ printTree(treeKey, target, level);
+
+}
+
+
+void printLocalName(TreeKeyIdx *treeKey) {
+ std::cout << "locaName: " << treeKey->getLocalName() << std::endl;
+}
+
+
+void setLocalName(TreeKeyIdx *treeKey) {
+ char buf[1023];
+ std::cout << "Enter New Node Name: ";
+ fgets(buf, 1000, stdin);
+ treeKey->setLocalName(buf);
+ treeKey->save();
+}
+
+
+void assurePath(TreeKeyIdx *treeKey) {
+ char buf[1023];
+ std::cout << "Enter path: ";
+ fgets(buf, 1000, stdin);
+ treeKey->assureKeyPath(buf);
+}
+
+
+void appendSibbling(TreeKeyIdx *treeKey) {
+ if (treeKey->getOffset()) {
+ char buf[1023];
+ std::cout << "Enter New Sibbling Name: ";
+ fgets(buf, 1000, stdin);
+ treeKey->append();
+ treeKey->setLocalName(buf);
+ treeKey->save();
+ }
+ else std::cout << "Can't add sibling to root node\n";
+}
+
+
+void appendChild(TreeKeyIdx *treeKey) {
+ char buf[1023];
+ std::cout << "Enter New Child Name: ";
+ fgets(buf, 1000, stdin);
+ treeKey->appendChild();
+ treeKey->setLocalName(buf);
+ treeKey->save();
+}
+
+
+void removeEntry(EntriesBlock *eb, int index) {
+ if (index < eb->getCount()) {
+ std::cout << "Removing entry [" << index << "]\n";
+ eb->removeEntry(index);
+ }
+ else std::cout << "Invalid entry number\n\n";
+}
+
+
+int main(int argc, char **argv) {
+
+ if (argc != 2) {
+ fprintf(stderr, "usage: %s <tree/key/data/path>\n", *argv);
+ exit(-1);
+ }
+
+ TreeKeyIdx *treeKey = new TreeKeyIdx(argv[1]);
+
+ if (treeKey->Error()) {
+ treeKey->create(argv[1]);
+ delete treeKey;
+ treeKey = new TreeKeyIdx(argv[1]);
+ }
+ TreeKeyIdx root = *treeKey;
+
+ std::string input;
+ char line[1024];
+
+ do {
+ std::cout << "[" << treeKey->getText() << "] > ";
+ fgets(line, 1000, stdin);
+ input = line;
+ if (input.length() > 0) {
+ switch (input[0]) {
+ case 'n': printLocalName(treeKey); break;
+ case 's': setLocalName(treeKey); break;
+ case 'p': root.root(); printTree(root, treeKey); break;
+ case 'a': appendSibbling(treeKey); break;
+ case 'c': appendChild(treeKey); break;
+ case 'j': treeKey->nextSibling(); break;
+ case 'g': assurePath(treeKey); break;
+ case 'k': treeKey->previousSibling(); break;
+ case 'h': treeKey->parent(); break;
+ case 'l': treeKey->firstChild(); break;
+ case 'r': treeKey->root(); break;
+ case 'q': break;
+ case '?':
+ default:
+ std::cout << "\n p - print tree\n";
+ std::cout << " n - get local name\n";
+ std::cout << " s - set local name\n";
+ std::cout << " j - next sibbling\n";
+ std::cout << " k - previous sibbling\n";
+ std::cout << " h - parent\n";
+ std::cout << " l - first child\n";
+ std::cout << " r - root\n";
+ std::cout << " a - append sibbling\n";
+ std::cout << " c - append child\n";
+ std::cout << " u - get user data\n";
+ std::cout << " d - set user data\n";
+ std::cout << " g - goto path; create if it doesn't exist\n";
+ std::cout << " q - quit\n\n";
+ break;
+ }
+ }
+ }
+ while (input.compare("q"));
+
+ delete treeKey;
+
+ return 0;
+}