1 |
/*
|
2 |
Copyright (C) 2007 Alexander Atanasov <aatanasov@gmail.com>
|
3 |
|
4 |
This program is free software; you can redistribute it and/or modify
|
5 |
it under the terms of the GNU General Public License as published by
|
6 |
the Free Software Foundation; version 2 of the License.
|
7 |
|
8 |
This program is distributed in the hope that it will be useful,
|
9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11 |
GNU General Public License for more details.
|
12 |
|
13 |
You should have received a copy of the GNU General Public License
|
14 |
along with this program; if not, write to the Free Software
|
15 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
16 |
MA 02110-1301 USA
|
17 |
|
18 |
Garmin and MapSource are registered trademarks or trademarks
|
19 |
of Garmin Ltd. or one of its subsidiaries.
|
20 |
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
File format is:
|
25 |
|
26 |
POINT
|
27 |
GROUP,0x0100 = town_label_1e5, Megapolis (10M +)
|
28 |
GROUP,0x0200 = town_label_5e4, Megapolis (5-10M)
|
29 |
...
|
30 |
GROUP,0x1e00-0x1e3f = district_label, District, Province, State Name
|
31 |
...
|
32 |
POLYLINE
|
33 |
GROUP,0x00 = street_1_land, Road
|
34 |
GROUP,0x01 = highway_land, Major HWY thick
|
35 |
GROUP,0x02 = street_4_land, Principal HWY-thick
|
36 |
GROUP,0x03 = street_2_land, Principal HWY-medium
|
37 |
....
|
38 |
POLYGONE
|
39 |
GROUP,0x01 = town_poly, City (>200k)
|
40 |
GROUP,0x02 = town_poly, City (<200k)
|
41 |
GROUP,0x03 = town_poly, Village
|
42 |
|
43 |
GROUP is
|
44 |
0 - good old garmin types in RGN1
|
45 |
1 - NT types in RGN2-4 5 is completely unknown yet
|
46 |
*/
|
47 |
|
48 |
#include <stdio.h>
|
49 |
#include <string.h>
|
50 |
#include <malloc.h>
|
51 |
#include <unistd.h>
|
52 |
#include "item.h"
|
53 |
#include "gar2navit.h"
|
54 |
|
55 |
#define dlog(x, y...) fprintf(stderr, ## y)
|
56 |
/*
|
57 |
static int add_def(struct gar2nav_conv *conv, int type, unsigned short minid,
|
58 |
unsigned short maxid, unsigned int routable, char *ntype,
|
59 |
char *descr)
|
60 |
*/
|
61 |
|
62 |
static void print_header(FILE *fp)
|
63 |
{
|
64 |
fprintf(fp, "// This is autogenerated file -- DO NOT EDIT\n");
|
65 |
fprintf(fp, "struct gar2nav_conv *g2n_default_conv(void)\n"
|
66 |
"{\n"
|
67 |
"\tstruct gar2nav_conv *conv;\n"
|
68 |
"\n"
|
69 |
"\tconv = calloc(1, sizeof(*conv));\n"
|
70 |
"\tif (!conv)\n"
|
71 |
"\t\treturn conv;\n");
|
72 |
}
|
73 |
|
74 |
static int load_types_file(char *file, char *out)
|
75 |
{
|
76 |
char buf[4096];
|
77 |
char descr[4096];
|
78 |
char ntype[4096];
|
79 |
FILE *fp;
|
80 |
unsigned int minid, maxid, group;
|
81 |
int rc;
|
82 |
int type = -1;
|
83 |
FILE *fpout = stdout;
|
84 |
|
85 |
fp = fopen(file, "r");
|
86 |
if (!fp)
|
87 |
return -1;
|
88 |
if (out) {
|
89 |
fpout = fopen(out, "w");
|
90 |
if (!fpout)
|
91 |
return -1;
|
92 |
}
|
93 |
print_header(fpout);
|
94 |
while (fgets(buf, sizeof(buf), fp)) {
|
95 |
if (*buf == '#' || *buf == '\n')
|
96 |
continue;
|
97 |
if (!strncasecmp(buf, "POINT", 5)) {
|
98 |
type = 1;
|
99 |
continue;
|
100 |
} else if (!strncasecmp(buf, "POI", 3)) {
|
101 |
type = 1;
|
102 |
continue;
|
103 |
} else if (!strncasecmp(buf, "POLYLINE", 8)) {
|
104 |
type = 2;
|
105 |
continue;
|
106 |
} else if (!strncasecmp(buf, "POLYGONE", 8)) {
|
107 |
type = 3;
|
108 |
continue;
|
109 |
}
|
110 |
|
111 |
rc = sscanf(buf, "%d, 0x%04X - 0x%04X = %[^\t , ] , %[^\n]",
|
112 |
&group, &minid, &maxid, ntype, descr);
|
113 |
if (rc != 5) {
|
114 |
maxid = 0;
|
115 |
rc = sscanf(buf, "%d, 0x%04X = %[^\t, ], %[^\n]",
|
116 |
&group,&minid, ntype, descr);
|
117 |
if (rc != 4) {
|
118 |
dlog(1, "Invalid line rc=%d:[%s]\n",rc, buf);
|
119 |
dlog(1, "minid=%04X ntype=[%s] des=[%s]\n",
|
120 |
minid, ntype, descr);
|
121 |
continue;
|
122 |
}
|
123 |
}
|
124 |
|
125 |
fprintf(fpout, "\tadd_def(conv, %d, %#.04x, %#.04x, %d, \"%s\", \"%s\");\n",
|
126 |
type, minid, maxid, group, ntype, descr);
|
127 |
}
|
128 |
fprintf(fpout, "\treturn conv;\n");
|
129 |
fprintf(fpout, "}\n");
|
130 |
fclose(fp);
|
131 |
if (out)
|
132 |
fclose(fpout);
|
133 |
return 1;
|
134 |
}
|
135 |
|
136 |
int main(int argc, char **argv)
|
137 |
{
|
138 |
if (argc!=3) {
|
139 |
fprintf(stderr, "Usage: %s garmintypes.txt outfile.c\n",
|
140 |
argv[0]);
|
141 |
return -1;
|
142 |
}
|
143 |
if (load_types_file(argv[1], argv[2]) < 0) {
|
144 |
unlink(argv[2]);
|
145 |
return -1;
|
146 |
}
|
147 |
return 0;
|
148 |
}
|