summaryrefslogtreecommitdiff
path: root/utilities/mod2zmod.cpp
blob: 03fd8c51f5f456196454e375dd90699e357c225d (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/******************************************************************************
 *
 *  mod2zmod.cpp -	Compression on variable granularity
 *
 * $Id: mod2zmod.cpp 2833 2013-06-29 06:40:28Z chrislit $
 *
 * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */

#ifdef _MSC_VER
	#pragma warning( disable: 4251 )
#endif

#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <string>

#ifndef __GNUC__
#include <io.h>
#else
#include <unistd.h>
#endif

#include <swbuf.h>
#include <ztext.h>
#include <zld.h>
#include <zcom.h>
#include <swtext.h>
#include <swmgr.h>
#include <lzsscomprs.h>
#include <zipcomprs.h>
#include <versekey.h>
#include <stdio.h>
#include <cipherfil.h>

#ifndef NO_SWORD_NAMESPACE
using namespace sword;
#endif

using std::cerr;
using std::string;
using std::cout;

void errorOutHelp(char *appName) {
	cerr << appName << " - a tool to create compressed Sword modules\n";
	cerr << "version 0.1\n\n";
	cerr << "usage: "<< appName << " <modname> <datapath> [blockType [compressType [cipherKey]]]\n\n";
	cerr << "datapath: the directory in which to write the zModule\n";
	cerr << "blockType  : (default 4)\n\t2 - verses\n\t3 - chapters\n\t4 - books\n";
	cerr << "compressType: (default 1):\n\t1 - LZSS\n\t2 - Zip\n";
	cerr << "\n\n";
	exit(-1);
}


int main(int argc, char **argv)
{
	int iType = 4;
	int compType = 1;
	string cipherKey = "";
	SWCompress *compressor = 0;
	SWModule *inModule     = 0;
	SWModule *outModule    = 0;
	

	if ((argc < 3) || (argc > 6)) {
		errorOutHelp(argv[0]);
	}

	if (argc > 3) {
		iType = atoi(argv[3]);
		if (argc > 4) {
			compType = atoi(argv[4]);
			if (argc > 5) {
				cipherKey = argv[5];
			}
		}
	}

	if ((iType < 2) || (compType < 1) || (compType > 2) || (!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help")) || (!strcmp(argv[1], "/?")) || (!strcmp(argv[1], "-?")) || (!strcmp(argv[1], "-help"))) {
		errorOutHelp(argv[0]);
	}

	SWMgr mgr;

	ModMap::iterator it = mgr.Modules.find(argv[1]);
	if (it == mgr.Modules.end()) {
		fprintf(stderr, "error: %s: couldn't find module: %s\n", argv[0], argv[1]);
		exit(-2);
	}

	inModule = it->second;

	// Try to initialize a default set of datafiles and indicies at our
	// datapath location passed to us from the user.
	
#define BIBLE 1
#define LEX 2
#define COM 3

	int modType = 0;
	if (!strcmp(inModule->getType(), "Biblical Texts")) modType = BIBLE;
	if (!strcmp(inModule->getType(), "Lexicons / Dictionaries")) modType = LEX;
	if (!strcmp(inModule->getType(), "Commentaries")) modType = COM;

	switch (compType) {	// these are deleted by zText
	case 1: compressor = new LZSSCompress(); break;
	case 2: compressor = new ZipCompress(); break;
	}

	int result = 0;
	switch (modType) {
	case BIBLE:
	case COM: {
		SWKey *k = inModule->getKey();
		VerseKey *vk = SWDYNAMIC_CAST(VerseKey, k);
		result = zText::createModule(argv[2], iType, vk->getVersificationSystem());
		break;
	}
	case LEX:
		result = zLD::createModule(argv[2]);
		break;
	}

	if (result) {
		fprintf(stderr, "error: %s: couldn't create module at path: %s\n", argv[0], argv[2]);
		exit(-3);
	}

	switch (modType) {
	case BIBLE:
	case COM: {
		SWKey *k = inModule->getKey();
		VerseKey *vk = SWDYNAMIC_CAST(VerseKey, k);
		outModule = new zText(argv[2], 0, 0, iType, compressor,
			0, ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, 0,
			vk->getVersificationSystem());	// open our datapath with our RawText driver.
		((VerseKey *)(SWKey *)(*inModule))->setIntros(true);
		break;
	}
	case LEX:
		outModule = new zLD(argv[2], 0, 0, iType, compressor);		// open our datapath with our RawText driver.
		break;
	}

	SWFilter *cipherFilter = 0;
	if (!cipherKey.empty()) {
		cipherFilter = new CipherFilter(cipherKey.c_str());
		outModule->addRawFilter(cipherFilter);
	}

	string lastBuffer = "Something that would never be first module entry";
	SWKey bufferKey;
	SWKey lastBufferKey;
	SWKey *outModuleKey = outModule->createKey();
	VerseKey *vkey = SWDYNAMIC_CAST(VerseKey, outModuleKey);
	outModuleKey->setPersist(true);
	if (vkey) {
		vkey->setIntros(true);
		vkey->setAutoNormalize(false);
	}
	outModule->setKey(*outModuleKey);

	inModule->setSkipConsecutiveLinks(false);
	(*inModule) = TOP;
	while (!inModule->popError()) {
		bufferKey = *(SWKey *)(*inModule);
		// pseudo-check for link.  Will get most common links.
		if ((lastBuffer == inModule->getRawEntry()) &&(lastBuffer.length() > 0)) {
			*outModuleKey = bufferKey;
			outModule->linkEntry(&lastBufferKey);	// link to last key
		cout << "Adding [" << bufferKey << "] link to: [" << lastBufferKey << "]\n";
		}
		else {
			lastBuffer = inModule->getRawEntry();
			lastBufferKey = inModule->getKeyText();
			if (lastBuffer.length() > 0) {
				cout << "Adding [" << bufferKey << "] new text.\n";
				*outModuleKey = bufferKey;
//				outModule->getRawEntry();	// snap
//				outModule->setKey(bufferKey);
				(*outModule) << lastBuffer.c_str();	// save new text;
			}
			else {
                    cout << "Skipping [" << bufferKey << "] no entry in Module.\n";
			}
		}
		(*inModule)++;
	}
	delete outModule;
	delete outModuleKey;
	if (cipherFilter)
		delete cipherFilter;
}