forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tabfile.cc
302 lines (248 loc) · 4.77 KB
/
tabfile.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
/*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic licence.
* (see licence.txt)
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "../simdebug.h"
#include "koord.h"
#include "tabfile.h"
bool tabfile_t::open(const char *filename)
{
close();
file = fopen(filename, "r");
return file != NULL;
}
void tabfile_t::close()
{
if(file) {
fclose(file);
file = NULL;
}
}
const char *tabfileobj_t::get(const char *key) const
{
const char *result = objinfo.get(key);
return result ? result : "";
}
/**
* Get the string value for a key - key must be lowercase
* @return def if key isn't found, value otherwise
* @author Hj. Malthaner
*/
const char *tabfileobj_t::get_string(const char *key, const char * def)
{
const char *result = objinfo.get(key);
return result ? result : def;
}
bool tabfileobj_t::put(const char *key, const char *value)
{
if(objinfo.get(key)) {
return false;
}
objinfo.put(strdup(key), strdup(value));
return true;
}
void tabfileobj_t::clear()
{
FOR(stringhashtable_tpl<char const*>, const& i, objinfo) {
free(const_cast<char*>(i.key));
free(const_cast<char*>(i.value));
}
objinfo.clear();
}
const koord &tabfileobj_t::get_koord(const char *key, koord def)
{
static koord ret;
const char *value = get(key);
const char *tmp;
ret = def;
if(!value || !*value) {
return ret;
}
// 2. Wert bestimmen
for(tmp = value; *tmp != ','; tmp++) {
if(!*tmp) {
return ret;
}
}
ret.x = atoi(value);
ret.y = atoi(tmp + 1);
return ret;
}
int tabfileobj_t::get_int(const char *key, int def)
{
const char *value = get(key);
if(!value || !*value) {
return def;
}
else {
// skip spaces/tabs
while ( *value>0 && *value<=32 ) {
value ++;
}
// this inputs also hex correct
return strtol( value, NULL, 0 );
}
}
sint64 atosint64(const char* a)
{
return (sint64)(atof(a)+0.5);
}
sint64 tabfileobj_t::get_int64(const char *key, sint64 def)
{
const char *value = get(key);
if(!value || !*value) {
return def;
}
else {
return atosint64(value);
}
}
int *tabfileobj_t::get_ints(const char *key)
{
const char *value = get(key);
const char *tmp;
int count = 1;
int *result;
if(!value || !*value) {
result = new int[1];
result[0] = 0;
return result;
}
// Anzahl bestimmen
for(tmp = value; *tmp; tmp++) {
if(*tmp == ',') {
count++;
}
}
// Ergebnisvektor erstellen und füllen
result = new int[count + 1];
result[0] = count;
count = 1;
result[count++] = strtol( value, NULL, 0 );
for(tmp = value; *tmp; tmp++) {
if(*tmp == ',') {
// skip spaces/tabs
do {
tmp ++;
} while ( *tmp>0 && *tmp<=32 );
// this inputs also hex correct
result[count++] = strtol( tmp, NULL, 0 );
}
}
return result;
}
sint64 *tabfileobj_t::get_sint64s(const char *key)
{
const char *value = get(key);
const char *tmp;
int count = 1;
sint64 *result;
if(!value || !*value) {
result = new sint64[1];
result[0] = 0;
return result;
}
// Anzahl bestimmen
for(tmp = value; *tmp; tmp++) {
if(*tmp == ',') {
count++;
}
}
// Ergebnisvektor erstellen und füllen
result = new sint64[count + 1];
result[0] = count;
count = 1;
result[count++] = atosint64(value);
for(tmp = value; *tmp; tmp++) {
if(*tmp == ',') {
result[count++] = atosint64(tmp + 1);
}
}
return result;
}
bool tabfile_t::read(tabfileobj_t &objinfo)
{
bool lines = false;
char line[4096];
objinfo.clear();
do {
while(read_line(line, sizeof(line)) && *line != '-') {
char *delim = strchr(line, '=');
if(delim) {
*delim++ = '\0';
format_key(line);
objinfo.put(line, delim);
lines = true;
}
}
} while(!lines && !feof(file)); // skip empty objects
return lines;
}
bool tabfile_t::read_line(char *s, int size)
{
char *r;
size_t l;
do {
r = fgets(s, size, file);
} while(r != NULL && (*s == '#' || *s == ' ') );
if(r) {
l = strlen(r);
while( l && (r[l-1] == '\n' || r[l-1] == '\r') ) {
r[--l] = '\0';
}
}
return r != NULL;
}
void tabfile_t::format_key(char *key)
{
char *s = key + strlen(key);
char *t;
// trim right
while(s > key && s[-1] == ' ') {
*--s = '\0';
}
// make lowercase
for(s = key; *s; s++) {
*s = tolower(*s);
}
// skip spaces inside []
for(s = t = key; *s; s++) {
if(*s == '[') {
*t++ = *s++;
while(*s && *s != ']') {
if(*s == ' ') {
s++;
}
else {
*t++ = *s++;
}
}
s--;
}
else {
*t++ = *s;
}
}
*t = '\0';
}
void tabfile_t::format_value(char *value)
{
size_t len = strlen(value);
// trim right
while(len && value[len - 1] == ' ') {
value[--len] = '\0';
}
// trim left
if(*value == ' ') {
char *from;
for(from = value; *from == ' '; from++) {}
while(*value) {
*value++ = *from++;
}
}
}