summaryrefslogtreecommitdiffstats
path: root/intf/collate2imp.cpp
blob: 13a9fe381ed3382c72148e5b0ad0bd34d5b85464 (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
#include <filemgr.h>
#include <versekey.h>
#include <stdio.h>
#include <iostream>


using namespace sword;
using namespace std;

void usage(const char *app, const char *error = 0) {

	if (error) fprintf(stderr, "\n%s: %s\n", app, error);

	fprintf(stderr, "Convert Collate Plain Text collation to imp format\n");
	fprintf(stderr, "\nusage: %s <input_file>\n", app);
	fprintf(stderr, "\n");
	exit(-1);
}

int main(int argc, char **argv) {

	if (argc < 2) usage(*argv);

	SWBuf inFile = argv[1];
	SWBuf lineBuffer;
	SWBuf bookTag="<div type=\"book\" n=\"";
	SWBuf chapTag="<div type=\"chapter\" n=\"";
	SWBuf verseTag="<ab n=\"";

	VerseKey vk("Mat.1.1");
	vk.setAutoNormalize(false);

	bool inVerse = false;

	// Let's see if we can open our input file
	FileDesc *fd = FileMgr::getSystemFileMgr()->open(inFile, FileMgr::RDONLY);
	if (fd->getFd() < 0) {
		fprintf(stderr, "error: %s: couldn't open input file: %s \n", argv[0], inFile.c_str());
		exit(-2);
	}
	bool more = true;
	do {
		more = FileMgr::getLine(fd, lineBuffer)!=0;
		if (lineBuffer.startsWith(bookTag)) {
			lineBuffer << bookTag.length();
			SWBuf book = lineBuffer.stripPrefix('"');
			vk.setBook(atoi(book.c_str()));
		}
		else if (lineBuffer.startsWith(chapTag)) {
			lineBuffer << chapTag.length();
			SWBuf chapter = lineBuffer.stripPrefix('"');
			vk.setChapter(atoi(chapter.c_str()));
		}
		else if (lineBuffer.startsWith(verseTag)) {
			lineBuffer << verseTag.length();
			SWBuf verse = lineBuffer.stripPrefix('"');
			vk.setVerse(atoi(verse.c_str()));
			lineBuffer.stripPrefix('>');
			if (lineBuffer.indexOf("</ab>") > -1) {
				lineBuffer.setSize(lineBuffer.indexOf("</ab>"));
				inVerse = false;
			}
			else {
				inVerse = true;
			}
			lineBuffer.trim();
			if (lineBuffer.length() > 0 && lineBuffer != "&om;") {
				cout << "$$$" << vk.getOSISRef() << (vk.getVerse() ? "" : ".0") << "\n";
				cout << lineBuffer << endl;
			}
		}
		else if (inVerse) {
			if (lineBuffer.indexOf("</ab>") > -1) {
				lineBuffer.setSize(lineBuffer.indexOf("</ab>"));
				inVerse = false;
			}
			cout << lineBuffer.trim() << endl;
		}
	} while (more);
}