summaryrefslogtreecommitdiffstats
path: root/paralleltag/modedit.cpp
blob: 25413da2937a78374d10f3d2898bb29148b4203e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <swmgr.h>
#include <markupfiltmgr.h>
#include <versekey.h>
#include <rawtext.h>
#include <iostream>

#ifndef __GNUC__
#include <io.h>
#else
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <regex.h> // GNU
#include <stdio.h>



#ifndef O_BINARY
#define O_BINARY 0
#endif

using namespace std;
using namespace sword;

SWMgr *mgr = 0;


void getModText(const char *modName, const char *key) {
	SWModule *mod = mgr->getModule(modName);
	//cout << "KEY:"<<key<<"<br>";
	if (mod) {
		mod->setKey(key);
		cout << mod->KeyText() << "|";
		cout << mod->getRawEntry();
	}
}


void exportRange(const char *modName, const char *key) {
	SWModule *mod = mgr->getModule(modName);
	//cout << "KEY:"<<key<<"<br>";
	if (mod) {
		VerseKey parser;
		ListKey scope = parser.ParseVerseList(key, parser, true);
		scope.Persist(1);
		mod->setKey(scope);
		RawText::createModule("./");
		RawText *outmod = new RawText("./");
		for (mod->setPosition(TOP); !mod->Error(); mod->increment()) {
			outmod->setKey(mod->KeyText());
			outmod->setEntry(mod->getRawEntry());
		}

	}
}


void writeModEntry(const char *modName, const char *key, const char *text) {
	SWModule *mod = mgr->getModule(modName);
	//cout << "KEY:"<<key<<"<br>";
	if (mod) {
		mod->setKey(key);
		mod->getRawEntry();
		mod->setEntry(text);
		(*mod)++;
		cout << mod->KeyText();
	}
}


void writeModEntryFromFile(const char *modName, const char *key, const char *fname) {
	SWModule *mod = mgr->getModule(modName);
	//cout << "KEY:"<<key<<"<br>";
	if (mod) {
		int fd = open(fname, O_RDONLY|O_BINARY);
		if (fd > -1) {
			long size = lseek(fd, 0, SEEK_END);
			lseek(fd, 0, SEEK_SET);
			char *buf = new char [ size + 1 ];
			read(fd, buf, size);
			buf[size] = 0;
			mod->setKey(key);
			mod->getRawEntry();
			mod->setEntry(buf);
			(*mod)++;
			cout << mod->KeyText();
			return;
		}
	}
	cout << key;
	return;
}


void getAttributes(const char *modName, const char *key) {
	SWModule *mod = mgr->getModule(modName);
	if (mod) {
		//cout << key << "<br>";
		mod->setKey(key);
		mod->RenderText();
		AttributeTypeList::iterator i1;
		AttributeList::iterator i2;
		AttributeValue::iterator i3;
		for (i1 = mod->getEntryAttributes().begin(); i1 != mod->getEntryAttributes().end(); i1++) {
			for (i2 = i1->second.begin(); i2 != i1->second.end(); i2++) {
				for (i3 = i2->second.begin(); i3 != i2->second.end(); i3++) {
					std::cout << i1->first << "|" << i2->first << "|" << i3->first << "|" << i3->second;
					std::cout.put(10);
				}
			}
		}
	}
}


void getContext(const char *modName, const char *strong) {
	SWModule *mod = mgr->getModule(modName);
	if (mod) {
		int MAX_ENTRIES = 7;
//	 	int SEARCH_TYPE = -3; //  - entryAttrib (eg. Word//Lemma/G1234/)	- slower
	 	int SEARCH_TYPE = -4; //  - lucene (be sure to run mkfastmod on your module)
		SWBuf searchTerm;
//		searchTerm.setFormatted("Word//Lemma/%s/", strong);	// entryAttribute format
		searchTerm.setFormatted("lemma:%s", strong);		// lucene format
		ListKey listKey = mod->search(searchTerm.c_str(), SEARCH_TYPE, REG_ICASE);
		for (listKey = TOP; !listKey.Error() && MAX_ENTRIES; listKey++) {
			std::cout << listKey.getShortText() << "|";
			mod->setKey(listKey);
			std::cout << mod->StripText() << "\n";
			MAX_ENTRIES--;
		}
	}
}


void getModHTML(const char *modName, const char *key) {
	SWModule *mod = mgr->getModule(modName);
	if (mod) {
		mod->setKey(key);
//		cout << "<html>\n";
//		cout << "<head>\n";
//		cout << "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n";
//		cout << "</head>\n";
//		cout << "<body>\n";
		cout << mod->KeyText() << "|";
		cout << mod->RenderText();
//		cout << "</body>\n";
//		cout << "</html>\n";
	}
}


int main(int argc, char **argv) {
	if (argc < 2) {
		fprintf(stderr, "usage: %s <read|html|write|fileWrite|attributes|export|context> <modname> <key> [text]\n\n", *argv);
		exit(1);
	}

	mgr = new SWMgr(new MarkupFilterMgr(FMT_HTML, ENC_UTF8));
	mgr->setGlobalOption("Textual Variants", "Secondary Reading");

//	mgr = new SWMgr("/home/sword/html/kjv2003", true, new MarkupFilterMgr(FMT_HTML, ENC_UTF8));
//SWMgr mgr("/home/sword/html/kjv2003");
	
	switch (argv[1][0]) {
	case 'r': getModText(argv[2], argv[3]);
			break;
	case 'h': getModHTML(argv[2], argv[3]);
			break;
	case 'a': getAttributes(argv[2], argv[3]);
			break;
	case 'e': exportRange(argv[2], argv[3]);
			break;
	case 'w': writeModEntry(argv[2], argv[3], argv[4]);
			break;
	case 'f': writeModEntryFromFile(argv[2], argv[3], argv[4]);
			break;
	case 'c': getContext(argv[2], argv[3]);
			break;
	}
	char buf[20];
//	cin >> buf;

	delete mgr;
	
	return 0;
}