forked from sebastianbiallas/ht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htcfg.cc
309 lines (263 loc) · 7.33 KB
/
htcfg.cc
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
/*
* HT Editor
* htcfg.cc
*
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* 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 <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <memory>
#include "cstream.h"
#include "htcfg.h"
#include "htctrl.h"
#include "htdebug.h"
#include "endianess.h"
#include "htreg.h"
#include "strtools.h"
#include "stream.h"
#include "store.h"
#include "sys.h"
#include "tools.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
std::unique_ptr<ObjectStream> create_object_stream(Stream &f, int object_stream_type)
{
std::unique_ptr<ObjectStream> s;
switch (object_stream_type) {
case object_stream_bin:
s = std::make_unique<ObjectStreamBin>(&f, false);
break;
case object_stream_txt:
s = std::make_unique<ObjectStreamText>(&f, false);
break;
case object_stream_bin_compressed: {
CompressedStream *cs = new CompressedStream(&f, false);
s = std::make_unique<ObjectStreamBin>(cs, true);
break;
}
}
return s;
}
struct config_header {
char magic[4];
char version[4];
char stream_type[2];
} PACKED;
/*
* system configs
*/
char *systemconfig_file;
/**/
loadstore_result save_systemconfig(String &error_info)
{
try {
LocalFile f((String)systemconfig_file, IOAM_WRITE, FOM_CREATE);
/* write project config header */
config_header h;
memcpy(h.magic, ht_systemconfig_magic, sizeof h.magic);
char q[16];
int system_ostream_type = get_config_dword("misc/config format");
sprintf(q, "%04x", ht_systemconfig_fileversion);
memcpy(h.version, q, sizeof h.version);
sprintf(q, "%02x", system_ostream_type);
memcpy(h.stream_type, q, sizeof h.stream_type);
f.writex(&h, sizeof h);
/* write object stream type */
std::unique_ptr<ObjectStream> d = create_object_stream(f, system_ostream_type);
if (!d) {
return LS_ERROR_FORMAT;
}
switch (system_ostream_type) {
case object_stream_bin:
break;
case object_stream_txt:
f.writex((void*)"\n#\n#\tThis is a generated file!\n#\n", 33);
break;
}
/* write config */
app->store(*d);
} catch (const Exception &e) {
e.reason(error_info);
return LS_ERROR_WRITE;
}
return LS_OK;
}
bool load_systemconfig(loadstore_result *result, int *error_info)
{
uint8 object_stream_type = 128;
*error_info = 0;
std::unique_ptr<ObjectStream> d;
try {
LocalFile f((String)systemconfig_file, IOAM_READ, FOM_EXISTS);
/* read project config header */
config_header h;
if (f.read(&h, sizeof h) != sizeof h
|| memcmp(h.magic, ht_systemconfig_magic, sizeof h.magic) != 0) {
*result = LS_ERROR_MAGIC;
return false;
}
uint16 readver;
if (!hexw_ex(readver, (char*)h.version) || (readver != ht_systemconfig_fileversion)) {
*result = LS_ERROR_VERSION;
*error_info = readver;
return false;
}
/* object stream type */
if (!hexb_ex(object_stream_type, (char*)h.stream_type)) {
*result = LS_ERROR_FORMAT;
return false;
}
d = create_object_stream(f, object_stream_type);
if (!d) {
*result = LS_ERROR_FORMAT;
return false;
}
/* read config */
app->load(*d);
} catch (const ObjectNotRegisteredException &) {
*result = LS_ERROR_CORRUPTED;
if (object_stream_type==object_stream_txt && d) {
*error_info = ((ObjectStreamText*)d.get())->getErrorLine();
}
return false;
} catch (const IOException &e) {
if (e.mPosixErrno == ENOENT) {
*result = LS_ERROR_NOT_FOUND;
} else {
*result = LS_ERROR_READ;
if (object_stream_type == object_stream_txt && d) {
*error_info = ((ObjectStreamText*)d.get())->getErrorLine();
}
}
return false;
}
*result = LS_OK;
return true;
}
/**/
loadstore_result save_fileconfig(const char *fileconfig_file, const char *magic, uint version, store_fcfg_func store_func, void *context, String &error_info)
{
try {
LocalFile f((String)fileconfig_file, IOAM_WRITE, FOM_CREATE);
/* write file config header */
config_header h;
memcpy(h.magic, magic, sizeof h.magic);
char q[16];
int file_ostream_type = get_config_dword("misc/config format");
sprintf(q, "%04x", version);
memcpy(h.version, q, sizeof h.version);
sprintf(q, "%02x", file_ostream_type);
memcpy(h.stream_type, q, sizeof h.stream_type);
f.writex(&h, sizeof h);
/* object stream type */
auto d = create_object_stream(f, file_ostream_type);
switch (file_ostream_type) {
case object_stream_bin:
break;
case object_stream_txt:
f.writex((void*)"\n#\n#\tThis is a generated file!\n#\n", 33);
break;
}
/* write config */
store_func(*d, context);
} catch (const Exception &e) {
e.reason(error_info);
return LS_ERROR_WRITE;
}
return LS_OK;
}
loadstore_result load_fileconfig(const char *fileconfig_file, const char *magic, uint version, load_fcfg_func load_func, void *context, String &error_info)
{
uint8 object_stream_type = 128;
error_info.clear();
std::unique_ptr<ObjectStream> d;
try {
LocalFile f((String)fileconfig_file, IOAM_READ, FOM_EXISTS);
/* read file config header */
config_header h;
if (f.read(&h, sizeof h) != sizeof h
|| memcmp(h.magic, magic, sizeof h.magic) != 0) {
return LS_ERROR_MAGIC;
}
uint16 readver;
if (!hexw_ex(readver, (char*)h.version) || readver != version) {
error_info.assignFormat("%d", readver);
return LS_ERROR_VERSION;
}
if (!hexb_ex(object_stream_type, (char*)h.stream_type)) {
return LS_ERROR_FORMAT;
}
/* object stream type */
d = create_object_stream(f, object_stream_type);
if (!d) {
return LS_ERROR_FORMAT;
}
load_func(*d, context);
} catch (const ObjectNotRegisteredException &e) {
e.reason(error_info);
if (object_stream_type==object_stream_txt && d) {
String blub;
blub.assignFormat(" (in line %d)", ((ObjectStreamText*)d.get())->getErrorLine());
error_info += blub;
}
return LS_ERROR_CORRUPTED;
} catch (const IOException &e) {
e.reason(error_info);
if (e.mPosixErrno == ENOENT) {
return LS_ERROR_NOT_FOUND;
} else {
return LS_ERROR_READ;
}
} catch (const Exception &e) {
e.reason(error_info);
return LS_ERROR_CORRUPTED;
}
return LS_OK;
}
/*
* INIT
*/
bool ht_cfg_use_homedir = true;
bool init_cfg()
{
char *d = NULL;
if (ht_cfg_use_homedir) d = sys_get_home_dir();
if (!d || !ht_cfg_use_homedir) d = sys_dirname(appname);
if (!d) d = ht_strdup("");
#if !defined(WIN32) && !defined(__WIN32__) && !defined(DJGPP) && !defined(MSDOS)
const char *b = "/" SYSTEM_CONFIG_FILE_NAME;
#else
const char *b = "\\" SYSTEM_CONFIG_FILE_NAME;
#endif
systemconfig_file = ht_malloc(strlen(d)+strlen(b)+1);
strcpy(systemconfig_file, d);
strcat(systemconfig_file, b);
free(d);
return true;
}
/*
* DONE
*/
void done_cfg()
{
free(systemconfig_file);
}