Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /branches/destination-access/extras/find-fixme/fixme-dumper.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1654 - (show annotations) (download) (as text)
Thu May 14 17:32:16 2015 UTC (9 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 11179 byte(s)
Merge changes from trunk related to compiler warnings, portability and
pedantic compilation flags.

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