This repository was archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathroadmap_city.c
460 lines (360 loc) · 10.9 KB
/
roadmap_city.c
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* roadmap_city.c - city list and search tools
*
* LICENSE:
*
* Copyright 2008 Israel Disatnik
* Copyright 2008 Ehud Shabtai
*
* This file is part of RoadMap.
*
* RoadMap 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; either version 2 of the License, or
* (at your option) any later version.
*
* RoadMap 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 RoadMap; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "roadmap.h"
#include "roadmap_list.h"
#include "roadmap_hash.h"
#include "roadmap_dictionary.h"
#include "roadmap_file.h"
#include "roadmap_path.h"
#include "roadmap_string.h"
#include "roadmap_city.h"
typedef struct {
RoadMapList list;
char *name;
} RoadMapCityData;
struct RoadMapCityItem {
RoadMapListItem current;
RoadMapCityId id;
int cell_id;
};
#define CITY_LIST_SIZE 4096
static RoadMapHash *RoadMapCityHash = NULL;
static int RoadMapCityCount = 0;
static int RoadMapCityChanged = 0;
void roadmap_city_init (void) {
roadmap_city_free ();
RoadMapCityCount = 0;
RoadMapCityHash = roadmap_hash_new ("city_list", CITY_LIST_SIZE);
RoadMapCityChanged = 1;
}
void roadmap_city_free (void) {
int i;
RoadMapListItem *ptr;
RoadMapListItem *tmp;
if (RoadMapCityHash) {
for (i = 0; i < RoadMapCityCount; i++) {
RoadMapCityData *city = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, i);
if (city) {
ROADMAP_LIST_FOR_EACH (&city->list, ptr, tmp) {
roadmap_list_remove (ptr);
free (ptr);
}
free (city->name);
free (city);
}
}
roadmap_hash_free (RoadMapCityHash);
RoadMapCityHash = NULL;
}
RoadMapCityCount = 0;
}
static int roadmap_city_hash_key (const char *name) {
int key = 0;
while (*name) {
key = key * 256 + *name++;
}
return key;
}
int roadmap_city_add (const char *name, int square_id, int city_id) {
int index = roadmap_city_find (name);
RoadMapCityEntry ptr;
RoadMapCityData *data;
if (index == -1) {
if (RoadMapCityCount && (RoadMapCityCount % CITY_LIST_SIZE == 0)) {
roadmap_hash_resize (RoadMapCityHash, RoadMapCityCount + CITY_LIST_SIZE);
}
index = RoadMapCityCount++;
data = malloc (sizeof (RoadMapCityData));
data->name = strdup (name);
ROADMAP_LIST_INIT (&data->list);
roadmap_hash_add (RoadMapCityHash, roadmap_city_hash_key (name), index);
roadmap_hash_set_value (RoadMapCityHash, index, data);
RoadMapCityChanged++;
} else {
RoadMapListItem *item;
RoadMapListItem *tmp;
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index);
ROADMAP_LIST_FOR_EACH (&data->list, item, tmp) {
ptr = (RoadMapCityEntry)item;
if (ptr->id.square_id == square_id) {
if (ptr->id.city_id != city_id) {
ptr->id.city_id = city_id;
RoadMapCityChanged++;
}
return index;
}
}
}
ptr = malloc (sizeof (struct RoadMapCityItem));
ptr->id.square_id = square_id;
ptr->id.city_id = city_id;
ptr->cell_id = index;
roadmap_list_append (&data->list, &ptr->current);
RoadMapCityChanged++;
return index;
}
void roadmap_city_remove (const char *name, int square_id) {
int index = roadmap_city_find (name);
RoadMapListItem *ptr;
RoadMapListItem *tmp;
RoadMapCityData *data;
if (index == -1) {
return;
}
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index);
ROADMAP_LIST_FOR_EACH (&data->list, ptr, tmp) {
if (((RoadMapCityEntry)ptr)->id.square_id == square_id) {
roadmap_list_remove (ptr);
free (ptr);
RoadMapCityChanged++;
break;
}
}
}
int roadmap_city_find (const char *name) {
int key;
int index;
RoadMapCityData *data;
if (!name) return -1;
if (!*name) return -1;
key = roadmap_city_hash_key (name);
index = roadmap_hash_get_first (RoadMapCityHash, key);
while (index != -1) {
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index);
if (!strcmp (data->name, name)) break;
index = roadmap_hash_get_next (RoadMapCityHash, index);
}
return index;
}
const RoadMapCityId *roadmap_city_first (int index, RoadMapCityEntry *entry) {
int cell_id = index;
RoadMapCityData *data;
if (index == -1) cell_id = 0;
if (cell_id < 0 || cell_id >= RoadMapCityCount) {
return NULL;
}
while (index == -1 && cell_id < RoadMapCityCount) {
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, cell_id);
if (!ROADMAP_LIST_EMPTY (&data->list)) break;
cell_id++;
}
if (cell_id >= RoadMapCityCount) {
return NULL;
}
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, cell_id);
if (ROADMAP_LIST_EMPTY (&data->list)) return NULL;
*entry = (RoadMapCityEntry) ROADMAP_LIST_FIRST (&data->list);
if (!*entry ||
((RoadMapListItem *)(*entry) == &data->list)) {
return NULL;
}
return &(*entry)->id;
}
const RoadMapCityId *roadmap_city_next (int index, RoadMapCityEntry *entry) {
int cell_id;
RoadMapCityData *data;
if (!*entry) {
return NULL;
}
cell_id = (*entry)->cell_id;
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, cell_id);
*entry = (RoadMapCityEntry)ROADMAP_LIST_NEXT (&(*entry)->current);
if ((RoadMapListItem *)(*entry) == &data->list) {
if (index >= 0) return NULL;
while (index == -1 && ++cell_id < RoadMapCityCount) {
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, cell_id);
if (!ROADMAP_LIST_EMPTY (&data->list)) break;
}
if (cell_id == RoadMapCityCount) return NULL;
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, cell_id);
*entry = (RoadMapCityEntry)ROADMAP_LIST_FIRST (&data->list);
}
return &(*entry)->id;
}
int roadmap_city_count (void) {
return RoadMapCityCount;
}
const char *roadmap_city_name (int index) {
return ((RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index))->name;
}
void roadmap_city_list_all (FILE *f) {
int index;
RoadMapCityEntry entry;
const RoadMapCityId *id;
RoadMapCityData *data;
fprintf (f, "====CITY LIST====\n");
for (index = 0; index < RoadMapCityCount; index++) {
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index);
if (data->name) {
fprintf (f, "City name is %s:\n", data->name);
id = roadmap_city_first (index, &entry);
while (id) {
fprintf (f, " Sq = %d ID = %d\n", id->square_id, id->city_id);
id = roadmap_city_next (index, &entry);
}
}
}
}
void roadmap_city_unload (int square) {
int index;
RoadMapListItem *ptr;
RoadMapListItem *tmp;
RoadMapCityData *data;
for (index = 0; index < RoadMapCityCount; index++) {
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index);
if (data->name) {
ROADMAP_LIST_FOR_EACH (&data->list, ptr, tmp) {
if (((RoadMapCityEntry)ptr)->id.square_id == square) {
roadmap_list_remove (ptr);
free (ptr);
RoadMapCityChanged++;
}
}
}
}
}
int roadmap_city_search (const char *str, RoadMapDictionaryCB cb, void *context) {
int index;
int count = 0;
RoadMapCityData *data;
for (index = 0; index < RoadMapCityCount; index++) {
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index);
if (data->name &&
(!str || roadmap_string_is_sub_ignore_case (data->name, str))) {
count++;
if (cb) {
if (!cb (index, data->name, context)) {
break;
}
}
}
}
return count;
}
static int roadmap_city_write_int (RoadMapFile file, int val, int switch_endian) {
if (switch_endian) {
unsigned char *b = (unsigned char *) &val;
val = (unsigned int) (b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3]);
}
return roadmap_file_write (file, &val, sizeof (int));
}
int roadmap_city_write_file (const char *path, const char *name, int switch_endian) {
RoadMapFile file;
int index;
RoadMapCityEntry entry;
const RoadMapCityId *id;
RoadMapCityData *data;
int length;
int count;
char *full_name;
if (!RoadMapCityChanged) return 0;
if (path) {
full_name = roadmap_path_join (path, name);
} else {
full_name = roadmap_path_join (roadmap_db_map_path(), name);
}
file = roadmap_file_open (full_name, "w");
roadmap_path_free (full_name);
if (!ROADMAP_FILE_IS_VALID (file)) return -1;
roadmap_file_write (file, &RoadMapCityCount, sizeof (int));
for (index = 0; index < RoadMapCityCount; index++) {
data = (RoadMapCityData *)roadmap_hash_get_value (RoadMapCityHash, index);
if (data->name) {
length = strlen (data->name);
roadmap_city_write_int (file, length, switch_endian);
roadmap_file_write (file, data->name, length);
count = 0;
id = roadmap_city_first (index, &entry);
while (id) {
count++;
id = roadmap_city_next (index, &entry);
}
roadmap_city_write_int (file, count, switch_endian);
id = roadmap_city_first (index, &entry);
while (id) {
roadmap_city_write_int (file, id->square_id, switch_endian);
roadmap_city_write_int (file, id->city_id, switch_endian);
id = roadmap_city_next (index, &entry);
}
} else {
length = 0;
count = 0;
roadmap_file_write (file, &length, sizeof (int));
roadmap_file_write (file, &count, sizeof (int));
}
}
roadmap_file_close (file);
RoadMapCityChanged = 0;
return 0;
}
int roadmap_city_read_file (const char *name) {
const char *map_path;
char *full_path;
RoadMapFile file;
int num_entries;
int length;
int count;
RoadMapCityId id;
char *city = NULL;
int rc = -1;
/* make sure index is clean */
roadmap_city_init ();
/* find the file in maps folders */
map_path = roadmap_path_first ("maps");
file = ROADMAP_INVALID_FILE;
while (map_path && !ROADMAP_FILE_IS_VALID (file)) {
full_path = roadmap_path_join (map_path, name);
file = roadmap_file_open (full_path, "r");
roadmap_path_free (full_path);
map_path = roadmap_path_next ("maps", map_path);
}
if (!ROADMAP_FILE_IS_VALID (file)) {
roadmap_log (ROADMAP_INFO, "failed to open city index file %s", name);
goto exit;
}
if (!roadmap_file_read (file, &num_entries, sizeof (int))) goto exit;
while (num_entries-- > 0) {
if (!roadmap_file_read (file, &length, sizeof (int))) goto exit;
city = (char *)malloc (length + 1);
if (roadmap_file_read (file, city, length) != length) goto exit;
city[length] = '\0';
if (!roadmap_file_read (file, &count, sizeof (int))) goto exit;
while (count-- > 0) {
if (!roadmap_file_read (file, &id, sizeof (RoadMapCityId))) goto exit;
roadmap_city_add (city, id.square_id, id.city_id);
}
free (city);
city = NULL;
}
RoadMapCityChanged = 0;
rc = 0;
exit:
if (city) free (city);
if (ROADMAP_FILE_IS_VALID (file)) roadmap_file_close (file);
return rc;
}