Routino SVN Repository Browser

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

ViewVC logotype

Contents of /branches/MSVC/extras/find-fixme/fixme-dumper.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1663 - (show annotations) (download) (as text)
Tue May 19 18:09:18 2015 UTC (9 years, 9 months ago) by amb
File MIME type: text/x-csrc
File size: 11157 byte(s)
Remove <sys/time.h> where not needed at all or when compiling with
Microsoft C compiler (in which case add a replacement gettimeofday
function) [inspired by patches from Oliver Eichler].

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