1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2011 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
#include <stdio.h>
|
20 |
#include <glib.h>
|
21 |
#include <string.h>
|
22 |
#include "coord.h"
|
23 |
#include "item.h"
|
24 |
#include "attr.h"
|
25 |
#include "maptool.h"
|
26 |
|
27 |
struct item_bin_sink *
|
28 |
item_bin_sink_new(void)
|
29 |
{
|
30 |
struct item_bin_sink *ret=g_new0(struct item_bin_sink, 1);
|
31 |
|
32 |
return ret;
|
33 |
}
|
34 |
|
35 |
struct item_bin_sink_func *
|
36 |
item_bin_sink_func_new(int(*func)(struct item_bin_sink_func *func, struct item_bin *ib, struct tile_data *tile_data))
|
37 |
{
|
38 |
struct item_bin_sink_func *ret=g_new0(struct item_bin_sink_func, 1);
|
39 |
ret->func = func;
|
40 |
return ret;
|
41 |
}
|
42 |
|
43 |
void item_bin_sink_func_destroy(struct item_bin_sink_func *func)
|
44 |
{
|
45 |
g_free(func);
|
46 |
}
|
47 |
|
48 |
void item_bin_sink_add_func(struct item_bin_sink *sink, struct item_bin_sink_func *func)
|
49 |
{
|
50 |
sink->sink_funcs = g_list_append(sink->sink_funcs, func);
|
51 |
}
|
52 |
|
53 |
void item_bin_sink_destroy(struct item_bin_sink *sink)
|
54 |
{
|
55 |
/* g_list_foreach(sink->sink_funcs, (GFunc)g_free, NULL); */
|
56 |
g_list_free(sink->sink_funcs);
|
57 |
g_free(sink);
|
58 |
}
|
59 |
|
60 |
int item_bin_write_to_sink(struct item_bin *ib, struct item_bin_sink *sink, struct tile_data *tile_data)
|
61 |
{
|
62 |
GList *list = sink->sink_funcs;
|
63 |
int ret = 0;
|
64 |
while (list)
|
65 |
{
|
66 |
struct item_bin_sink_func *func = list->data;
|
67 |
ret = func->func(func, ib, tile_data);
|
68 |
if (ret)
|
69 |
break;
|
70 |
list = g_list_next(list);
|
71 |
}
|
72 |
return ret;
|
73 |
}
|
74 |
|
75 |
struct item_bin_sink *
|
76 |
file_reader_new(FILE *in, int limit, int offset)
|
77 |
{
|
78 |
struct item_bin_sink *ret;
|
79 |
if (!in)
|
80 |
return NULL;
|
81 |
ret = item_bin_sink_new();
|
82 |
ret->priv_data[0] = in;
|
83 |
ret->priv_data[1] = (void *) (long) limit;
|
84 |
ret->priv_data[2] = (void *) (long) offset;
|
85 |
fseek(in, 0, SEEK_SET);
|
86 |
return ret;
|
87 |
}
|
88 |
|
89 |
int file_reader_finish(struct item_bin_sink *sink)
|
90 |
{
|
91 |
struct item_bin *ib;
|
92 |
int ret = 0;
|
93 |
FILE *in = sink->priv_data[0];
|
94 |
int limit = (int) (long) sink->priv_data[1];
|
95 |
int offset = (int) (long) sink->priv_data[2];
|
96 |
while ((ib = read_item(in, 0)))
|
97 |
{
|
98 |
if (offset > 0)
|
99 |
{
|
100 |
offset--;
|
101 |
}
|
102 |
else
|
103 |
{
|
104 |
ret = item_bin_write_to_sink(ib, sink, NULL);
|
105 |
if (ret || (limit != -1 && !--limit))
|
106 |
{
|
107 |
item_bin_sink_destroy(sink);
|
108 |
return ret;
|
109 |
}
|
110 |
}
|
111 |
}
|
112 |
item_bin_sink_destroy(sink);
|
113 |
return 0;
|
114 |
}
|
115 |
|
116 |
int file_writer_process(struct item_bin_sink_func *func, struct item_bin *ib, struct tile_data *tile_data)
|
117 |
{
|
118 |
FILE *out = func->priv_data[0];
|
119 |
item_bin_write(ib, out);
|
120 |
return 0;
|
121 |
}
|
122 |
|
123 |
struct item_bin_sink_func *
|
124 |
file_writer_new(FILE *out)
|
125 |
{
|
126 |
struct item_bin_sink_func *file_writer;
|
127 |
if (!out)
|
128 |
return NULL;
|
129 |
file_writer = item_bin_sink_func_new(file_writer_process);
|
130 |
file_writer->priv_data[0] = out;
|
131 |
return file_writer;
|
132 |
}
|
133 |
|
134 |
int file_writer_finish(struct item_bin_sink_func *file_writer)
|
135 |
{
|
136 |
item_bin_sink_func_destroy(file_writer);
|
137 |
return 0;
|
138 |
}
|
139 |
|
140 |
int tile_collector_process(struct item_bin_sink_func *tile_collector, struct item_bin *ib, struct tile_data *tile_data)
|
141 |
{
|
142 |
int *buffer, *buffer2;
|
143 |
int len = ib->len + 1;
|
144 |
GHashTable *hash = tile_collector->priv_data[0];
|
145 |
buffer = g_hash_table_lookup(hash, tile_data->buffer);
|
146 |
buffer2 = g_malloc((len + (buffer ? buffer[0] : 1)) * 4);
|
147 |
if (buffer)
|
148 |
{
|
149 |
memcpy(buffer2, buffer, buffer[0] * 4);
|
150 |
}
|
151 |
else
|
152 |
buffer2[0] = 1;
|
153 |
memcpy(buffer2 + buffer2[0], ib, len * 4);
|
154 |
buffer2[0] += len;
|
155 |
g_hash_table_insert(hash, g_strdup(tile_data->buffer), buffer2);
|
156 |
return 0;
|
157 |
}
|
158 |
|
159 |
struct item_bin_sink_func *
|
160 |
tile_collector_new(struct item_bin_sink *out)
|
161 |
{
|
162 |
struct item_bin_sink_func *tile_collector;
|
163 |
tile_collector = item_bin_sink_func_new(tile_collector_process);
|
164 |
tile_collector->priv_data[0] = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
165 |
tile_collector->priv_data[1] = out;
|
166 |
return tile_collector;
|
167 |
}
|
168 |
|