File: htcfg.cc

package info (click to toggle)
ht 0.5.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,388 kB
  • ctags: 9,064
  • sloc: cpp: 51,336; ansic: 11,954; sh: 2,742; yacc: 1,142; lex: 396; makefile: 178
file content (330 lines) | stat: -rw-r--r-- 7,066 bytes parent folder | download
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *	HT Editor
 *	htcfg.cc
 *
 *	Copyright (C) 1999, 2000, 2001 Stefan Weyergraf (stefan@weyergraf.de)
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	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.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#include "cstream.h"
#include "htapp.h"
#include "htcfg.h"
#include "htctrl.h"
#include "htdebug.h"
#include "htendian.h"
#include "htstring.h"
#include "stream.h"
#include "store.h"
#include "htsys.h"
#include "tools.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>

/* VERSION 2 (for ht-0.4.4 and later) */

/* NOTE: as of Version 2 ALL integers in HT config-files are
   stored in big-endian format... (non-intel) */
   
#define object_stream_bin			0
#define object_stream_txt			1
#define object_stream_bin_compressed	2

int def_project_ostream_type=object_stream_bin_compressed;

int def_file_ostream_type=object_stream_bin_compressed;

ht_object_stream *create_object_stream(ht_stream *f, int object_stream_type)
{
	ht_object_stream *s;
	switch (object_stream_type) {
		case object_stream_bin: {
			s=new ht_object_stream_bin();
			((ht_object_stream_bin*)s)->init(f);
			break;
		}
		case object_stream_txt: {
			s=new ht_object_stream_txt();
			((ht_object_stream_txt*)s)->init(f);
			break;
		}
		case object_stream_bin_compressed: {
			ht_compressed_stream *cs=new ht_compressed_stream();
			cs->init(f, false);
			s=new ht_object_stream_bin();
			((ht_object_stream_bin*)s)->init(cs);
			s->set_stream_ownership(true);
			break;
		}
		default: {
			return NULL;
		}
	}
	return s;
}

struct config_header {
	char magic[4] HTPACKED;
	char version[4] HTPACKED;
	char stream_type[2] HTPACKED;
};

/*
 *	project configs
 */

char *projectconfig_file;

/**/

loadstore_result save_projectconfig()
{
	ht_file *f=new ht_file();
	f->init(projectconfig_file, FAM_WRITE | FAM_CREATE);
	if (f->get_error()) {
		f->done();
		delete f;
		return LS_ERROR_WRITE;
	}
	
/* write project config header */
	config_header h;

	memmove(h.magic, ht_projectconfig_magic, sizeof h.magic);

	char q[16];

	sprintf(q, "%04x", ht_projectconfig_fileversion);
	memmove(h.version, q, sizeof h.version);

	sprintf(q, "%02x", def_project_ostream_type);
	memmove(h.stream_type, q, sizeof h.stream_type);

	f->write(&h, sizeof h);
	
/* write object stream type */
	ht_object_stream *d=create_object_stream(f, def_project_ostream_type);
	   
	switch (def_project_ostream_type) {
		case object_stream_bin:
			break;
		case object_stream_txt:
			f->write((void*)"\n#\n#\tThis is a generated file!\n#\n", 33);
			break;
	}
/* write config */
	app->store(d);
	if (d->get_error()) return LS_ERROR_WRITE;
		
	d->done();
	delete d;

	f->done();
	delete f;
	
	return LS_OK;
}

bool load_projectconfig(loadstore_result *result, int *error_info)
{
	ht_file *f=new ht_file();
	f->init(projectconfig_file, FAM_READ);
	switch (f->get_error()) {
		case 0:break;
		case STERR_SYSTEM | ENOENT:
			f->done();
			delete f;
			*result = LS_ERROR_NOT_FOUND;
			return false;
		default:
			f->done();
			delete f;
			*result = LS_ERROR_READ;
			return false;
	}
/* read project config header */
	config_header h;

	f->read(&h, sizeof h);
	
	if (memcmp(h.magic, ht_projectconfig_magic, sizeof h.magic)!=0) {
		*result = LS_ERROR_MAGIC;
		return false;
	}

	if (hexw((char*)h.version)!=ht_projectconfig_fileversion) {
		*result = LS_ERROR_VERSION;
		*error_info = hexw((char*)h.version);
		return false;
	}

	dword object_stream_type=hexb((char*)h.stream_type);

/* object stream type */
	ht_object_stream *d=create_object_stream(f, object_stream_type);
	if (!d) {
		*result = LS_ERROR_FORMAT;
		return false;
	}
	
/* read config */
	if (app->load(d)!=0) {
		*result = LS_ERROR_CORRUPTED;
		*error_info = 0;
		if (d->get_error()) {
			if (object_stream_type==object_stream_txt)
				*error_info = ((ht_object_stream_txt*)d)->get_error_line();
		}
		return false;
	}
	
	d->done();
	delete d;

	f->done();
	delete f;	

	*result = LS_OK;
	return true;
}

/**/

loadstore_result save_fileconfig(char *fileconfig_file, store_fcfg_func store_func, void *context)
{
	ht_file *f=new ht_file();
	f->init(fileconfig_file, FAM_WRITE | FAM_CREATE);
	if (f->get_error()) {
		f->done();
		delete f;
		return LS_ERROR_WRITE;
	}
	
/* write file config header */
	config_header h;

	memmove(h.magic, ht_fileconfig_magic, sizeof h.magic);

	char q[16];

	sprintf(q, "%04x", ht_fileconfig_fileversion);
	memmove(h.version, q, sizeof h.version);

	sprintf(q, "%02x", def_file_ostream_type);
	memmove(h.stream_type, q, sizeof h.stream_type);

	f->write(&h, sizeof h);

/* object stream type */
	ht_object_stream *d=create_object_stream(f, def_file_ostream_type);
	   
/* write config */
	store_func(d, context);

	d->done();
	delete d;

	f->done();
	delete f;
		
	return LS_OK;
}

loadstore_result load_fileconfig(char *fileconfig_file, load_fcfg_func load_func, void *context, int *error_info)
{
	ht_file *f=new ht_file();
	f->init(fileconfig_file, FAM_READ);
	switch (f->get_error()) {
		case 0:break;
		case STERR_SYSTEM | ENOENT:
			f->done();
			delete f;
			return LS_ERROR_NOT_FOUND;
		default:
			f->done();
			delete f;
			return LS_ERROR_READ;
	}
/* read file config header */
	config_header h;

	f->read(&h, sizeof h);
	
	if (memcmp(h.magic, ht_fileconfig_magic, sizeof h.magic)!=0) return LS_ERROR_MAGIC;

	if (hexw((char*)h.version)!=ht_fileconfig_fileversion) {
		*error_info = hexw((char*)h.version);
		return LS_ERROR_VERSION;
	}
	
	dword object_stream_type=hexb((char*)h.stream_type);

/* object stream type */
	ht_object_stream *d=create_object_stream(f, object_stream_type);
	if (!d) return LS_ERROR_FORMAT;
	   
/* read config */
	if (load_func(d, context)) {
		*error_info = 0;
		if (d->get_error()) {
			if (object_stream_type==object_stream_txt)
				*error_info = ((ht_object_stream_txt*)d)->get_error_line();
		}
		return LS_ERROR_CORRUPTED;
	}
	
	d->done();
	delete d;

	f->done();
	delete f;
	
	return LS_OK;
}

/*
 *	INIT
 */

bool init_cfg()
{
#if defined(WIN32) || defined(__WIN32__) || defined(MSDOS) || defined(DJGPP)
	char d[1024];	/* FIXME: !!!! */
	sys_dirname(this_app, d);
	char *b="/"PROJECT_CONFIG_FILE_NAME;
	projectconfig_file=(char*)malloc(strlen(d)+strlen(b)+1);
	strcpy(projectconfig_file, d);
	strcat(projectconfig_file, b);
#else
	char *home=getenv("HOME");
	char *b="/"PROJECT_CONFIG_FILE_NAME;
	if (!home) home="";
	projectconfig_file=(char*)malloc(strlen(home)+strlen(b)+1);
	strcpy(projectconfig_file, home);
	strcat(projectconfig_file, b);
#endif
	return true;
}

/*
 *	DONE
 */

void done_cfg()
{
	free(projectconfig_file);
}