aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/comments/rawfiles/rawfiles.cpp
blob: c073a7376862b1d61cf971e1db34a5ede2c2aa29 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/******************************************************************************
 *  rawfiles.cpp - code for class 'RawFiles'- a module that produces HTML HREFs
 *			pointing to actual text desired.  Uses standard
 *			files:	ot and nt using indexs ??.bks ??.cps ??.vss
 */


#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>

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

#include <string.h>
#include <utilfuns.h>
#include <rawverse.h>
#include <rawfiles.h>
#include <filemgr.h>

#ifndef O_BINARY                // O_BINARY is needed in Borland C++ 4.53
#define O_BINARY 0              // If it hasn't been defined than we probably
#endif                          // don't need it.


 /******************************************************************************
 * RawFiles Constructor - Initializes data for instance of RawFiles
 *
 * ENT:	iname - Internal name for module
 *	idesc - Name to display to user for module
 *	idisp	 - Display object to use for displaying
 */

RawFiles::RawFiles(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang) : RawVerse(ipath, O_RDWR), SWCom(iname, idesc, idisp, enc, dir, mark, ilang)
{
}


/******************************************************************************
 * RawFiles Destructor - Cleans up instance of RawFiles
 */

RawFiles::~RawFiles()
{
}


/******************************************************************************
 * RawFiles::getRawEntry	- Returns the correct verse when char * cast
 *					is requested
 *
 * RET: string buffer with verse
 */

char *RawFiles::getRawEntry() {
	FileDesc *datafile;
	long  start = 0;
	unsigned short size = 0;
	char *tmpbuf;
	VerseKey *key = 0;

#ifndef _WIN32_WCE
	try {
#endif
		key = SWDYNAMIC_CAST(VerseKey, this->key);
#ifndef _WIN32_WCE
	}
	catch ( ... ) {}
#endif
	if (!key)
		key = new VerseKey(this->key);

	findoffset(key->Testament(), key->Index(), &start, &size);

	if (entrybuf)
		delete [] entrybuf;

	if (size) {
		tmpbuf   = new char [ (size + 2) + strlen(path) + 5 ];
		sprintf(tmpbuf,"%s/",path);
		readtext(key->Testament(), start, (size + 2), tmpbuf+strlen(tmpbuf));
		datafile = FileMgr::systemFileMgr.open(tmpbuf, O_RDONLY|O_BINARY);
		delete [] tmpbuf;
		if (datafile->getFd() > 0) {
			size = lseek(datafile->getFd(), 0, SEEK_END);
			entrybuf = new char [ size * FILTERPAD ];
			memset(entrybuf, 0, size * FILTERPAD);
			lseek(datafile->getFd(), 0, SEEK_SET);
			read(datafile->getFd(), entrybuf, size);
//			preptext(entrybuf);
		}
		else {
			entrybuf = new char [2];
			entrybuf[0] = 0;
			entrybuf[1] = 0;
		}
		FileMgr::systemFileMgr.close(datafile);
	}
	else {
		entrybuf = new char [2];
		entrybuf[0] = 0;
		entrybuf[1] = 0;
	}

	if (key != this->key)
		delete key;

	return entrybuf;
}


/******************************************************************************
 * RawFiles::setEntry(char *)- Update the modules current key entry with
 *				provided text
 */

void RawFiles::setEntry(const char *inbuf, long len) {
	FileDesc *datafile;
	long  start;
	unsigned short size;
	char *tmpbuf;
	VerseKey *key = 0;

	len = (len<0)?strlen(inbuf):len;
	try {
		key = SWDYNAMIC_CAST(VerseKey, this->key);
	}
	catch ( ... ) {}
	if (!key)
		key = new VerseKey(this->key);

	findoffset(key->Testament(), key->Index(), &start, &size);

	if (size) {
		tmpbuf   = new char [ (size + 3) + strlen(path) + 1 ];
		sprintf(tmpbuf, "%s/", path);
		readtext(key->Testament(), start, (size + 2), tmpbuf+strlen(tmpbuf));
	}
	else {
		tmpbuf   = new char [ 16 + strlen(path) + 1 ];
		sprintf(tmpbuf, "%s/%s", path, getnextfilename());
		settext(key->Testament(), key->Index(), tmpbuf+strlen(path)+1);
	}
	datafile = FileMgr::systemFileMgr.open(tmpbuf, O_CREAT|O_WRONLY|O_BINARY|O_TRUNC);
	delete [] tmpbuf;
	if (datafile->getFd() > 0) {
		write(datafile->getFd(), inbuf, len);
	}
	FileMgr::systemFileMgr.close(datafile);
	
	if (key != this->key)
		delete key;
}


/******************************************************************************
 * RawFiles::linkEntry(SWKey *)- Link the modules current key entry with
 *				another module entry
 *
 * RET: *this
 */

void RawFiles::linkEntry(const SWKey *inkey) {

	long  start;
	unsigned short size;
	char *tmpbuf;
	const VerseKey *key = 0;

	try {
		key = SWDYNAMIC_CAST(VerseKey, inkey);
	}
	catch ( ... ) {}
	if (!key)
		key = new VerseKey(this->key);

	findoffset(key->Testament(), key->Index(), &start, &size);

	if (size) {
		tmpbuf   = new char [ size + 2];
		readtext(key->Testament(), start, size + 2, tmpbuf);

		if (key != inkey)
			delete key;
		key = 0;

		try {
			key = SWDYNAMIC_CAST(VerseKey, inkey);
		}
		catch ( ... ) {}
		if (!key)
			key = new VerseKey(this->key);
		settext(key->Testament(), key->Index(), tmpbuf);
	}
	
	if (key != inkey)
		delete key;
}


/******************************************************************************
 * RawFiles::deleteEntry	- deletes this entry
 *
 * RET: *this
 */

void RawFiles::deleteEntry() {

	VerseKey *key = 0;

#ifndef _WIN32_WCE
	try {
#endif
		key = SWDYNAMIC_CAST(VerseKey, this->key);
#ifndef _WIN32_WCE
	}
	catch ( ... ) {}
#endif
	if (!key)
		key = new VerseKey(this->key);

	settext(key->Testament(), key->Index(), "");

	if (key != this->key)
		delete key;
}


/******************************************************************************
 * RawFiles::getnextfilename - generates a valid filename in which to store
 *				an entry
 *
 * RET: filename
 */

char *RawFiles::getnextfilename() {
	static char incfile[255];
	long number;
	FileDesc *datafile;

	sprintf(incfile, "%s/incfile", path);
	datafile = FileMgr::systemFileMgr.open(incfile, O_RDONLY|O_BINARY);
	if (read(datafile->getFd(), &number, 4) != 4)
		number = 0;
	number++;
	FileMgr::systemFileMgr.close(datafile);
	
	datafile = FileMgr::systemFileMgr.open(incfile, O_CREAT|O_WRONLY|O_BINARY|O_TRUNC);
	write(datafile->getFd(), &number, 4);
	FileMgr::systemFileMgr.close(datafile);
	sprintf(incfile, "%.7ld", number-1);
	return incfile;
}


char RawFiles::createModule (const char *path) {
	char *incfile = new char [ strlen (path) + 16 ];
    static long zero = 0;
	FileDesc *datafile;

	sprintf(incfile, "%s/incfile", path);
	datafile = FileMgr::systemFileMgr.open(incfile, O_CREAT|O_WRONLY|O_BINARY|O_TRUNC);
    delete [] incfile;
	write(datafile->getFd(), &zero, 4);
	FileMgr::systemFileMgr.close(datafile);

    return RawVerse::createModule (path);
}