Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/extras/find-fixme/fixme-dumper.c
Parent Directory
|
Revision Log
Revision 1950 -
(hide annotations)
(download)
(as text)
Sat Jul 28 16:45:09 2018 UTC (6 years, 7 months ago) by amb
File MIME type: text/x-csrc
File size: 11685 byte(s)
Sat Jul 28 16:45:09 2018 UTC (6 years, 7 months ago) by amb
File MIME type: text/x-csrc
File size: 11685 byte(s)
Fix two warnings found by gcc-8.
1 | amb | 1367 | /*************************************** |
2 | Fixme file dumper. | ||
3 | |||
4 | Part of the Routino routing software. | ||
5 | ******************/ /****************** | ||
6 | amb | 1950 | This file Copyright 2013-2015, 2018 Andrew M. Bishop |
7 | amb | 1367 | |
8 | This program is free software: you can redistribute it and/or modify | ||
9 | it under the terms of the GNU Affero General Public License as published by | ||
10 | the Free Software Foundation, either version 3 of the License, or | ||
11 | (at your option) any later version. | ||
12 | |||
13 | This program is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | GNU Affero General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Affero General Public License | ||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | ***************************************/ | ||
21 | |||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | #include <sys/stat.h> | ||
27 | #include <time.h> | ||
28 | #include <math.h> | ||
29 | |||
30 | amb | 1797 | #include "version.h" |
31 | |||
32 | amb | 1367 | #include "types.h" |
33 | #include "errorlog.h" | ||
34 | |||
35 | #include "files.h" | ||
36 | #include "xmlparse.h" | ||
37 | |||
38 | |||
39 | /* Local functions */ | ||
40 | |||
41 | static void OutputErrorLog(ErrorLogs *errorlogs,double latmin,double latmax,double lonmin,double lonmax); | ||
42 | |||
43 | static void print_errorlog_visualiser(ErrorLogs *errorlogs,index_t item); | ||
44 | |||
45 | amb | 1378 | static char *RFC822Date(time_t t); |
46 | |||
47 | amb | 1367 | static void print_usage(int detail,const char *argerr,const char *err); |
48 | |||
49 | |||
50 | /*++++++++++++++++++++++++++++++++++++++ | ||
51 | The main program for the fixme dumper. | ||
52 | ++++++++++++++++++++++++++++++++++++++*/ | ||
53 | |||
54 | int main(int argc,char** argv) | ||
55 | { | ||
56 | ErrorLogs*OSMErrorLogs; | ||
57 | int arg; | ||
58 | char *dirname=NULL,*prefix=NULL; | ||
59 | char *errorlogs_filename; | ||
60 | amb | 1378 | int option_statistics=0; |
61 | amb | 1367 | int option_visualiser=0,coordcount=0; |
62 | double latmin=0,latmax=0,lonmin=0,lonmax=0; | ||
63 | char *option_data=NULL; | ||
64 | int option_dump_visualiser=0; | ||
65 | |||
66 | /* Parse the command line arguments */ | ||
67 | |||
68 | for(arg=1;arg<argc;arg++) | ||
69 | { | ||
70 | amb | 1797 | if(!strcmp(argv[arg],"--version")) |
71 | print_usage(-1,NULL,NULL); | ||
72 | else if(!strcmp(argv[arg],"--help")) | ||
73 | amb | 1367 | print_usage(1,NULL,NULL); |
74 | else if(!strncmp(argv[arg],"--dir=",6)) | ||
75 | dirname=&argv[arg][6]; | ||
76 | amb | 1378 | else if(!strcmp(argv[arg],"--statistics")) |
77 | option_statistics=1; | ||
78 | amb | 1367 | else if(!strcmp(argv[arg],"--visualiser")) |
79 | option_visualiser=1; | ||
80 | else if(!strcmp(argv[arg],"--dump-visualiser")) | ||
81 | option_dump_visualiser=1; | ||
82 | else if(!strncmp(argv[arg],"--latmin",8) && argv[arg][8]=='=') | ||
83 | {latmin=degrees_to_radians(atof(&argv[arg][9]));coordcount++;} | ||
84 | else if(!strncmp(argv[arg],"--latmax",8) && argv[arg][8]=='=') | ||
85 | {latmax=degrees_to_radians(atof(&argv[arg][9]));coordcount++;} | ||
86 | else if(!strncmp(argv[arg],"--lonmin",8) && argv[arg][8]=='=') | ||
87 | {lonmin=degrees_to_radians(atof(&argv[arg][9]));coordcount++;} | ||
88 | else if(!strncmp(argv[arg],"--lonmax",8) && argv[arg][8]=='=') | ||
89 | {lonmax=degrees_to_radians(atof(&argv[arg][9]));coordcount++;} | ||
90 | else if(!strncmp(argv[arg],"--data",6) && argv[arg][6]=='=') | ||
91 | option_data=&argv[arg][7]; | ||
92 | else if(!strncmp(argv[arg],"--fixme=",8)) | ||
93 | ; | ||
94 | else | ||
95 | print_usage(0,argv[arg],NULL); | ||
96 | } | ||
97 | |||
98 | amb | 1378 | if((option_statistics + option_visualiser + option_dump_visualiser)!=1) |
99 | print_usage(0,NULL,"Must choose --visualiser, --statistics or --dump-visualiser."); | ||
100 | amb | 1367 | |
101 | /* Load in the data - Note: No error checking because Load*List() will call exit() in case of an error. */ | ||
102 | |||
103 | OSMErrorLogs=LoadErrorLogs(errorlogs_filename=FileName(dirname,prefix,"fixme.mem")); | ||
104 | |||
105 | /* Write out the visualiser data */ | ||
106 | |||
107 | if(option_visualiser) | ||
108 | { | ||
109 | if(coordcount!=4) | ||
110 | print_usage(0,NULL,"The --visualiser option must have --latmin, --latmax, --lonmin, --lonmax.\n"); | ||
111 | |||
112 | if(!option_data) | ||
113 | print_usage(0,NULL,"The --visualiser option must have --data.\n"); | ||
114 | |||
115 | if(!strcmp(option_data,"fixmes")) | ||
116 | OutputErrorLog(OSMErrorLogs,latmin,latmax,lonmin,lonmax); | ||
117 | else | ||
118 | print_usage(0,option_data,NULL); | ||
119 | } | ||
120 | |||
121 | amb | 1378 | /* Print out statistics */ |
122 | |||
123 | if(option_statistics) | ||
124 | { | ||
125 | struct stat buf; | ||
126 | |||
127 | /* Examine the files */ | ||
128 | |||
129 | printf("Files\n"); | ||
130 | printf("-----\n"); | ||
131 | printf("\n"); | ||
132 | |||
133 | stat(errorlogs_filename,&buf); | ||
134 | |||
135 | printf("'%s%sfixme.mem' - %9"PRIu64" Bytes\n",prefix?prefix:"",prefix?"-":"",(uint64_t)buf.st_size); | ||
136 | printf("%s\n",RFC822Date(buf.st_mtime)); | ||
137 | printf("\n"); | ||
138 | |||
139 | printf("\n"); | ||
140 | printf("Error Logs\n"); | ||
141 | printf("----------\n"); | ||
142 | printf("\n"); | ||
143 | |||
144 | printf("Number(total) =%9"Pindex_t"\n",OSMErrorLogs->file.number); | ||
145 | printf("Number(geographical) =%9"Pindex_t"\n",OSMErrorLogs->file.number_geo); | ||
146 | printf("Number(non-geographical)=%9"Pindex_t"\n",OSMErrorLogs->file.number_nongeo); | ||
147 | |||
148 | printf("\n"); | ||
149 | stat(errorlogs_filename,&buf); | ||
150 | #if !SLIM | ||
151 | amb | 1650 | printf("Total strings=%9zu Bytes\n",(size_t)buf.st_size-(OSMErrorLogs->strings-(char*)OSMErrorLogs->data)); |
152 | amb | 1378 | #else |
153 | amb | 1650 | printf("Total strings=%9zu Bytes\n",(size_t)buf.st_size-(size_t)OSMErrorLogs->stringsoffset); |
154 | amb | 1378 | #endif |
155 | } | ||
156 | |||
157 | amb | 1367 | /* Print out internal data (in HTML format for the visualiser) */ |
158 | |||
159 | if(option_dump_visualiser) | ||
160 | { | ||
161 | index_t item; | ||
162 | |||
163 | if(!option_data) | ||
164 | print_usage(0,NULL,"The --dump-visualiser option must have --data.\n"); | ||
165 | |||
166 | for(arg=1;arg<argc;arg++) | ||
167 | if(!strncmp(argv[arg],"--data=fixme",12)) | ||
168 | { | ||
169 | item=atoi(&argv[arg][12]); | ||
170 | |||
171 | if(item<OSMErrorLogs->file.number) | ||
172 | print_errorlog_visualiser(OSMErrorLogs,item); | ||
173 | else | ||
174 | printf("Invalid fixme number; minimum=0, maximum=%"Pindex_t".\n",OSMErrorLogs->file.number-1); | ||
175 | } | ||
176 | } | ||
177 | |||
178 | amb | 1599 | exit(EXIT_SUCCESS); |
179 | amb | 1367 | } |
180 | |||
181 | |||
182 | /*++++++++++++++++++++++++++++++++++++++ | ||
183 | Output the data for error logs within the region. | ||
184 | |||
185 | ErrorLogs *errorlogs The set of error logs to use. | ||
186 | |||
187 | double latmin The minimum latitude. | ||
188 | |||
189 | double latmax The maximum latitude. | ||
190 | |||
191 | double lonmin The minimum longitude. | ||
192 | |||
193 | double lonmax The maximum longitude. | ||
194 | ++++++++++++++++++++++++++++++++++++++*/ | ||
195 | |||
196 | static void OutputErrorLog(ErrorLogs *errorlogs,double latmin,double latmax,double lonmin,double lonmax) | ||
197 | { | ||
198 | ll_bin_t latminbin=latlong_to_bin(radians_to_latlong(latmin))-errorlogs->file.latzero; | ||
199 | ll_bin_t latmaxbin=latlong_to_bin(radians_to_latlong(latmax))-errorlogs->file.latzero; | ||
200 | ll_bin_t lonminbin=latlong_to_bin(radians_to_latlong(lonmin))-errorlogs->file.lonzero; | ||
201 | ll_bin_t lonmaxbin=latlong_to_bin(radians_to_latlong(lonmax))-errorlogs->file.lonzero; | ||
202 | ll_bin_t latb,lonb; | ||
203 | index_t i,index1,index2; | ||
204 | |||
205 | /* Loop through all of the error logs. */ | ||
206 | |||
207 | for(latb=latminbin;latb<=latmaxbin;latb++) | ||
208 | for(lonb=lonminbin;lonb<=lonmaxbin;lonb++) | ||
209 | { | ||
210 | ll_bin2_t llbin=lonb*errorlogs->file.latbins+latb; | ||
211 | |||
212 | if(llbin<0 || llbin>(errorlogs->file.latbins*errorlogs->file.lonbins)) | ||
213 | continue; | ||
214 | |||
215 | index1=LookupErrorLogOffset(errorlogs,llbin); | ||
216 | index2=LookupErrorLogOffset(errorlogs,llbin+1); | ||
217 | |||
218 | if(index2>errorlogs->file.number_geo) | ||
219 | index2=errorlogs->file.number_geo; | ||
220 | |||
221 | for(i=index1;i<index2;i++) | ||
222 | { | ||
223 | ErrorLog *errorlogp=LookupErrorLog(errorlogs,i,1); | ||
224 | |||
225 | double lat=latlong_to_radians(bin_to_latlong(errorlogs->file.latzero+latb)+off_to_latlong(errorlogp->latoffset)); | ||
226 | double lon=latlong_to_radians(bin_to_latlong(errorlogs->file.lonzero+lonb)+off_to_latlong(errorlogp->lonoffset)); | ||
227 | |||
228 | if(lat>latmin && lat<latmax && lon>lonmin && lon<lonmax) | ||
229 | printf("fixme%"Pindex_t" %.6f %.6f\n",i,radians_to_degrees(lat),radians_to_degrees(lon)); | ||
230 | } | ||
231 | } | ||
232 | } | ||
233 | |||
234 | |||
235 | /*++++++++++++++++++++++++++++++++++++++ | ||
236 | Print out an error log entry from the database (in visualiser format). | ||
237 | |||
238 | ErrorLogs *errorlogs The set of error logs to use. | ||
239 | |||
240 | index_t item The error log index to print. | ||
241 | ++++++++++++++++++++++++++++++++++++++*/ | ||
242 | |||
243 | static void print_errorlog_visualiser(ErrorLogs *errorlogs,index_t item) | ||
244 | { | ||
245 | char *string=LookupErrorLogString(errorlogs,item); | ||
246 | |||
247 | printf("%s\n",ParseXML_Encode_Safe_XML(string)); | ||
248 | } | ||
249 | |||
250 | |||
251 | amb | 1378 | /*+ Conversion from time_t to date string (day of week). +*/ |
252 | static const char* const weekdays[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; | ||
253 | |||
254 | /*+ Conversion from time_t to date string (month of year). +*/ | ||
255 | static const char* const months[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; | ||
256 | |||
257 | |||
258 | amb | 1367 | /*++++++++++++++++++++++++++++++++++++++ |
259 | amb | 1378 | Convert the time into an RFC 822 compliant date. |
260 | |||
261 | char *RFC822Date Returns a pointer to a fixed string containing the date. | ||
262 | |||
263 | time_t t The time. | ||
264 | ++++++++++++++++++++++++++++++++++++++*/ | ||
265 | |||
266 | static char *RFC822Date(time_t t) | ||
267 | { | ||
268 | amb | 1950 | static char value[80]; /* static allocation of return value */ |
269 | amb | 1378 | char weekday[4]; |
270 | char month[4]; | ||
271 | struct tm *tim; | ||
272 | |||
273 | tim=gmtime(&t); | ||
274 | |||
275 | strcpy(weekday,weekdays[tim->tm_wday]); | ||
276 | strcpy(month,months[tim->tm_mon]); | ||
277 | |||
278 | /* Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 */ | ||
279 | |||
280 | sprintf(value,"%3s, %02d %3s %4d %02d:%02d:%02d %s", | ||
281 | weekday, | ||
282 | tim->tm_mday, | ||
283 | month, | ||
284 | tim->tm_year+1900, | ||
285 | tim->tm_hour, | ||
286 | tim->tm_min, | ||
287 | tim->tm_sec, | ||
288 | "GMT" | ||
289 | ); | ||
290 | |||
291 | return(value); | ||
292 | } | ||
293 | |||
294 | |||
295 | /*++++++++++++++++++++++++++++++++++++++ | ||
296 | amb | 1367 | Print out the usage information. |
297 | |||
298 | amb | 1797 | int detail The level of detail to use: -1 = just version number, 0 = low detail, 1 = full details. |
299 | amb | 1367 | |
300 | const char *argerr The argument that gave the error (if there is one). | ||
301 | |||
302 | const char *err Other error message (if there is one). | ||
303 | ++++++++++++++++++++++++++++++++++++++*/ | ||
304 | |||
305 | static void print_usage(int detail,const char *argerr,const char *err) | ||
306 | { | ||
307 | amb | 1797 | if(detail<0) |
308 | { | ||
309 | amb | 1367 | fprintf(stderr, |
310 | amb | 1797 | "Routino version " ROUTINO_VERSION " " ROUTINO_URL ".\n" |
311 | ); | ||
312 | } | ||
313 | amb | 1367 | |
314 | amb | 1797 | if(detail>=0) |
315 | { | ||
316 | amb | 1367 | fprintf(stderr, |
317 | amb | 1797 | "Usage: fixme-dumper [--version]\n" |
318 | " [--help]\n" | ||
319 | " [--dir=<dirname>]\n" | ||
320 | " [--statistics]\n" | ||
321 | " [--visualiser --latmin=<latmin> --latmax=<latmax>\n" | ||
322 | " --lonmin=<lonmin> --lonmax=<lonmax>\n" | ||
323 | " --data=<data-type>]\n" | ||
324 | " [--dump--visualiser [--data=fixme<number>]]\n"); | ||
325 | amb | 1367 | |
326 | amb | 1797 | if(argerr) |
327 | fprintf(stderr, | ||
328 | "\n" | ||
329 | "Error with command line parameter: %s\n",argerr); | ||
330 | |||
331 | if(err) | ||
332 | fprintf(stderr, | ||
333 | "\n" | ||
334 | "Error: %s\n",err); | ||
335 | } | ||
336 | |||
337 | if(detail==1) | ||
338 | amb | 1367 | fprintf(stderr, |
339 | "\n" | ||
340 | amb | 1797 | "--version Print the version of Routino.\n" |
341 | "\n" | ||
342 | amb | 1367 | "--help Prints this information.\n" |
343 | "\n" | ||
344 | amb | 1378 | "--dir=<dirname> The directory containing the fixme database.\n" |
345 | amb | 1367 | "\n" |
346 | amb | 1378 | "--statistics Print statistics about the fixme database.\n" |
347 | "\n" | ||
348 | "--visualiser Extract selected data from the fixme database:\n" | ||
349 | amb | 1367 | " --latmin=<latmin> * the minimum latitude (degrees N).\n" |
350 | " --latmax=<latmax> * the maximum latitude (degrees N).\n" | ||
351 | " --lonmin=<lonmin> * the minimum longitude (degrees E).\n" | ||
352 | " --lonmax=<lonmax> * the maximum longitude (degrees E).\n" | ||
353 | " --data=<data-type> * the type of data to select.\n" | ||
354 | "\n" | ||
355 | " <data-type> can be selected from:\n" | ||
356 | " fixmes = fixme tags extracted from the data.\n" | ||
357 | "\n" | ||
358 | "--dump-visualiser Dump selected contents of the database in HTML.\n" | ||
359 | " --data=fixme<number> * the fixme with the selected index.\n"); | ||
360 | |||
361 | exit(!detail); | ||
362 | } |