1 |
zoff99 |
8 |
/**
|
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
|
44 |
|
|
item_bin_sink_func_destroy(struct item_bin_sink_func *func)
|
45 |
|
|
{
|
46 |
|
|
g_free(func);
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
void
|
50 |
|
|
item_bin_sink_add_func(struct item_bin_sink *sink, struct item_bin_sink_func *func)
|
51 |
|
|
{
|
52 |
|
|
sink->sink_funcs=g_list_append(sink->sink_funcs, func);
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
void
|
56 |
|
|
item_bin_sink_destroy(struct item_bin_sink *sink)
|
57 |
|
|
{
|
58 |
|
|
/* g_list_foreach(sink->sink_funcs, (GFunc)g_free, NULL); */
|
59 |
|
|
g_list_free(sink->sink_funcs);
|
60 |
|
|
g_free(sink);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
int
|
64 |
|
|
item_bin_write_to_sink(struct item_bin *ib, struct item_bin_sink *sink, struct tile_data *tile_data)
|
65 |
|
|
{
|
66 |
|
|
GList *list=sink->sink_funcs;
|
67 |
|
|
int ret=0;
|
68 |
|
|
while (list) {
|
69 |
|
|
struct item_bin_sink_func *func=list->data;
|
70 |
|
|
ret=func->func(func, ib, tile_data);
|
71 |
|
|
if (ret)
|
72 |
|
|
break;
|
73 |
|
|
list=g_list_next(list);
|
74 |
|
|
}
|
75 |
|
|
return ret;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
struct item_bin_sink *
|
79 |
|
|
file_reader_new(FILE *in, int limit, int offset)
|
80 |
|
|
{
|
81 |
|
|
struct item_bin_sink *ret;
|
82 |
|
|
if (!in)
|
83 |
|
|
return NULL;
|
84 |
|
|
ret=item_bin_sink_new();
|
85 |
|
|
ret->priv_data[0]=in;
|
86 |
|
|
ret->priv_data[1]=(void *)(long)limit;
|
87 |
|
|
ret->priv_data[2]=(void *)(long)offset;
|
88 |
|
|
fseek(in, 0, SEEK_SET);
|
89 |
|
|
return ret;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
int
|
93 |
|
|
file_reader_finish(struct item_bin_sink *sink)
|
94 |
|
|
{
|
95 |
|
|
struct item_bin *ib;
|
96 |
|
|
int ret =0;
|
97 |
|
|
FILE *in=sink->priv_data[0];
|
98 |
|
|
int limit=(int)(long)sink->priv_data[1];
|
99 |
|
|
int offset=(int)(long)sink->priv_data[2];
|
100 |
|
|
while ((ib=read_item(in))) {
|
101 |
|
|
if (offset > 0) {
|
102 |
|
|
offset--;
|
103 |
|
|
} else {
|
104 |
|
|
ret=item_bin_write_to_sink(ib, sink, NULL);
|
105 |
|
|
if (ret || (limit != -1 && !--limit)) {
|
106 |
|
|
item_bin_sink_destroy(sink);
|
107 |
|
|
return ret;
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
item_bin_sink_destroy(sink);
|
112 |
|
|
return 0;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
int
|
116 |
|
|
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
|
135 |
|
|
file_writer_finish(struct item_bin_sink_func *file_writer)
|
136 |
|
|
{
|
137 |
|
|
item_bin_sink_func_destroy(file_writer);
|
138 |
|
|
return 0;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
int
|
143 |
|
|
tile_collector_process(struct item_bin_sink_func *tile_collector, struct item_bin *ib, struct tile_data *tile_data)
|
144 |
|
|
{
|
145 |
|
|
int *buffer,*buffer2;
|
146 |
|
|
int len=ib->len+1;
|
147 |
|
|
GHashTable *hash=tile_collector->priv_data[0];
|
148 |
|
|
buffer=g_hash_table_lookup(hash, tile_data->buffer);
|
149 |
|
|
buffer2=g_malloc((len+(buffer ? buffer[0] : 1))*4);
|
150 |
|
|
if (buffer) {
|
151 |
|
|
memcpy(buffer2, buffer, buffer[0]*4);
|
152 |
|
|
} else
|
153 |
|
|
buffer2[0]=1;
|
154 |
|
|
memcpy(buffer2+buffer2[0], ib, len*4);
|
155 |
|
|
buffer2[0]+=len;
|
156 |
|
|
g_hash_table_insert(hash, g_strdup(tile_data->buffer), buffer2);
|
157 |
|
|
return 0;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
struct item_bin_sink_func *
|
161 |
|
|
tile_collector_new(struct item_bin_sink *out)
|
162 |
|
|
{
|
163 |
|
|
struct item_bin_sink_func *tile_collector;
|
164 |
|
|
tile_collector=item_bin_sink_func_new(tile_collector_process);
|
165 |
|
|
tile_collector->priv_data[0]=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
166 |
|
|
tile_collector->priv_data[1]=out;
|
167 |
|
|
return tile_collector;
|
168 |
|
|
}
|
169 |
|
|
|