From: martin-s Date: Sat, 9 Jul 2011 13:33:21 +0000 (+0000) Subject: Add:maptool:Split out parameter handling X-Git-Tag: navit-0.5.0.5194svn~589 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7e3250a5aa892bfc854a7f44df5b4307b5927b1e;p=profile%2Fivi%2Fnavit.git Add:maptool:Split out parameter handling git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@4606 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- diff --git a/navit/navit/maptool/maptool.c b/navit/navit/maptool/maptool.c index c9ae0d1..d325f5c 100644 --- a/navit/navit/maptool/maptool.c +++ b/navit/navit/maptool/maptool.c @@ -146,31 +146,213 @@ usage(FILE *f) exit(1); } -int main(int argc, char **argv) +struct maptool_params { + int zip64; + int keep_tmpfiles; + int process_nodes; + int process_ways; + int process_relations; + char *protobufdb; + char *protobufdb_operation; + char *md5file; + int start; + int end; + int output; + int o5m; + int compression_level; + int protobuf; + int dump_coordinates; + int input; + GList *map_handles; + FILE* input_file; + FILE* rule_file; + char *url; +}; + +static int +parse_option(struct maptool_params *p, char **argv, int argc, int *option_index) { - FILE *ways=NULL, *way2poi=NULL, *ways_split=NULL,*ways_split_index=NULL,*nodes=NULL,*turn_restrictions=NULL,*graph=NULL,*coastline=NULL,*tilesdir,*coords,*relations=NULL,*boundaries=NULL; - FILE *files[10]; - FILE *references[10]; + char *optarg_cp,*attr_name,*attr_value; + struct map *handle; + struct attr *attrs[10]; + int pos,c,i; -#if 0 - char *map=g_strdup(attrmap); + static struct option long_options[] = { + {"md5", 1, 0, '5'}, + {"64bit", 0, 0, '6'}, + {"attr-debug-level", 1, 0, 'a'}, + {"binfile", 0, 0, 'b'}, + {"compression-level", 1, 0, 'z'}, +#ifdef HAVE_POSTGRESQL + {"db", 1, 0, 'd'}, #endif - int zipnum,c,start=1,end=99,dump_coordinates=0; - int keep_tmpfiles=0; - int process_nodes=1, process_ways=1, process_relations=1; + {"dedupe-ways", 0, 0, 'w'}, + {"dump", 0, 0, 'D'}, + {"dump-coordinates", 0, 0, 'c'}, + {"end", 1, 0, 'e'}, + {"help", 0, 0, 'h'}, + {"keep-tmpfiles", 0, 0, 'k'}, + {"nodes-only", 0, 0, 'N'}, + {"map", 1, 0, 'm'}, + {"o5m", 0, 0, 'M'}, + {"plugin", 1, 0, 'p'}, + {"protobuf", 0, 0, 'P'}, + {"start", 1, 0, 's'}, + {"input-file", 1, 0, 'i'}, + {"rule-file", 1, 0, 'r'}, + {"ignore-unknown", 0, 0, 'n'}, + {"url", 1, 0, 'u'}, + {"ways-only", 0, 0, 'W'}, + {"slice-size", 1, 0, 'S'}, + {"unknown-country", 0, 0, 'U'}, + {0, 0, 0, 0} + }; + c = getopt_long (argc, argv, "5:6B:DMNO:PS:Wa:bc" +#ifdef HAVE_POSTGRESQL + "d:" +#endif + "e:hi:knm:p:r:s:wu:z:U", long_options, option_index); + if (c == -1) + return 1; + switch (c) { + case '5': + p->md5file=optarg; + break; + case '6': + p->zip64=1; + break; + case 'B': + p->protobufdb=optarg; + break; + case 'D': + p->output=1; + break; + case 'M': + p->o5m=1; + break; + case 'N': + p->process_ways=0; + break; + case 'R': + p->process_relations=0; + break; + case 'O': + p->protobufdb_operation=optarg; + p->output=1; + break; + case 'P': + p->protobuf=1; + break; + case 'S': + slice_size=atoll(optarg); + break; + case 'W': + p->process_nodes=0; + break; + case 'U': + unknown_country=1; + break; + case 'a': + attr_debug_level=atoi(optarg); + break; + case 'b': + p->input=1; + break; + case 'c': + p->dump_coordinates=1; + break; +#ifdef HAVE_POSTGRESQL + case 'd': + p->dbstr=optarg; + break; +#endif + case 'e': + p->end=atoi(optarg); + break; + case 'h': + return 2; + case 'm': + optarg_cp=g_strdup(optarg); + pos=0; + i=0; + attr_name=g_strdup(optarg); + attr_value=g_strdup(optarg); + while (i < 9 && attr_from_line(optarg_cp, NULL, &pos, attr_value, attr_name)) { + attrs[i]=attr_new_from_text(attr_name,attr_value); + if (attrs[i]) { + i++; + } else { + fprintf(stderr,"Failed to convert %s=%s to attribute\n",attr_name,attr_value); + } + attr_value=g_strdup(optarg); + } + attrs[i++]=NULL; + g_free(attr_value); + g_free(optarg_cp); + handle=map_new(NULL, attrs); + if (! handle) { + fprintf(stderr,"Failed to create map from attributes\n"); + exit(1); + } + p->map_handles=g_list_append(p->map_handles,handle); + break; + case 'n': + fprintf(stderr,"I will IGNORE unknown types\n"); + ignore_unkown=1; + break; + case 'k': + fprintf(stderr,"I will KEEP tmp files\n"); + p->keep_tmpfiles=1; + break; + case 'p': + add_plugin(optarg); + break; + case 's': + p->start=atoi(optarg); + break; + case 'w': + dedupe_ways_hash=g_hash_table_new(NULL, NULL); + break; + case 'i': + p->input_file = fopen( optarg, "r" ); + if (p->input_file == NULL ) + { + fprintf( stderr, "\nInput file (%s) not found\n", optarg ); + exit( -1 ); + } + break; + case 'r': + p->rule_file = fopen( optarg, "r" ); + if (p->rule_file == NULL ) + { + fprintf( stderr, "\nRule file (%s) not found\n", optarg ); + exit( -1 ); + } + break; + case 'u': + p->url=optarg; + break; #ifdef HAVE_ZLIB - int compression_level=9; -#else - int compression_level=0; + case 'z': + p->compression_level=atoi(optarg); + break; #endif - int zip64=0; - int output=0; - int input=0; - int protobuf=0; - int o5m=0; - int f,pos; - char *result,*optarg_cp,*attr_name,*attr_value; - char *protobufdb=NULL,*protobufdb_operation=NULL,*md5file=NULL; + case '?': + default: + return 0; + } + return 3; +} + +int main(int argc, char **argv) +{ + FILE *ways=NULL, *way2poi=NULL, *ways_split=NULL,*ways_split_index=NULL,*nodes=NULL,*turn_restrictions=NULL,*graph=NULL,*coastline=NULL,*tilesdir,*coords,*relations=NULL,*boundaries=NULL; + FILE *files[10]; + FILE *references[10]; + struct maptool_params p; + int zipnum; + int f; + char *result; #ifdef HAVE_POSTGRESQL char *dbstr=NULL; #endif @@ -179,9 +361,6 @@ int main(int argc, char **argv) FILE* rule_file = NULL; // external rule file - struct attr *attrs[10]; - GList *map_handles=NULL; - struct map *handle; struct maptool_osm osm; #if 0 char *suffixes[]={"m0l0", "m0l1","m0l2","m0l3","m0l4","m0l5","m0l6"}; @@ -197,210 +376,61 @@ int main(int argc, char **argv) main_init(argv[0]); struct zip_info *zip_info=NULL; int suffix_start=0; + int option_index = 0; char *timestamp=current_to_iso8601(); - char *url=NULL; + #ifndef HAVE_GLIB _g_slice_thread_init_nomessage(); #endif + memset(&p, 0, sizeof(p)); +#ifdef HAVE_ZLIB + p.compression_level=9; +#endif + p.start=1; + p.end=99; + p.input_file=stdin; + p.process_nodes=1; + p.process_ways=1; + p.process_relations=1; while (1) { -#if 0 - int this_option_optind = optind ? optind : 1; -#endif - int option_index = 0; - static struct option long_options[] = { - {"md5", 1, 0, '5'}, - {"64bit", 0, 0, '6'}, - {"attr-debug-level", 1, 0, 'a'}, - {"binfile", 0, 0, 'b'}, - {"compression-level", 1, 0, 'z'}, -#ifdef HAVE_POSTGRESQL - {"db", 1, 0, 'd'}, -#endif - {"dedupe-ways", 0, 0, 'w'}, - {"dump", 0, 0, 'D'}, - {"dump-coordinates", 0, 0, 'c'}, - {"end", 1, 0, 'e'}, - {"help", 0, 0, 'h'}, - {"keep-tmpfiles", 0, 0, 'k'}, - {"nodes-only", 0, 0, 'N'}, - {"map", 1, 0, 'm'}, - {"o5m", 0, 0, 'M'}, - {"plugin", 1, 0, 'p'}, - {"protobuf", 0, 0, 'P'}, - {"start", 1, 0, 's'}, - {"input-file", 1, 0, 'i'}, - {"rule-file", 1, 0, 'r'}, - {"ignore-unknown", 0, 0, 'n'}, - {"url", 1, 0, 'u'}, - {"ways-only", 0, 0, 'W'}, - {"slice-size", 1, 0, 'S'}, - {"unknown-country", 0, 0, 'U'}, - {0, 0, 0, 0} - }; - c = getopt_long (argc, argv, "5:6B:DMNO:PS:Wa:bc" -#ifdef HAVE_POSTGRESQL - "d:" -#endif - "e:hi:knm:p:r:s:wu:z:U", long_options, &option_index); - if (c == -1) - break; - switch (c) { - case '5': - md5file=optarg; - break; - case '6': - zip64=1; - break; - case 'B': - protobufdb=optarg; - break; - case 'D': - output=1; - break; - case 'M': - o5m=1; - break; - case 'N': - process_ways=0; - break; - case 'R': - process_relations=0; - break; - case 'O': - protobufdb_operation=optarg; - output=1; - break; - case 'P': - protobuf=1; - break; - case 'S': - slice_size=atoll(optarg); - break; - case 'W': - process_nodes=0; - break; - case 'U': - unknown_country=1; - break; - case 'a': - attr_debug_level=atoi(optarg); - break; - case 'b': - input=1; - break; - case 'c': - dump_coordinates=1; - break; -#ifdef HAVE_POSTGRESQL - case 'd': - dbstr=optarg; - break; -#endif - case 'e': - end=atoi(optarg); + int parse_result=parse_option(&p, argv, argc, &option_index); + if (!parse_result) { + usage(stderr); + exit(1); + } + if (parse_result == 1) break; - case 'h': + if (parse_result == 2) { usage(stdout); - break; - case 'm': - optarg_cp=g_strdup(optarg); - pos=0; - i=0; - attr_name=g_strdup(optarg); - attr_value=g_strdup(optarg); - while (i < 9 && attr_from_line(optarg_cp, NULL, &pos, attr_value, attr_name)) { - attrs[i]=attr_new_from_text(attr_name,attr_value); - if (attrs[i]) { - i++; - } else { - fprintf(stderr,"Failed to convert %s=%s to attribute\n",attr_name,attr_value); - } - attr_value=g_strdup(optarg); - } - attrs[i++]=NULL; - g_free(attr_value); - g_free(optarg_cp); - handle=map_new(NULL, attrs); - if (! handle) { - fprintf(stderr,"Failed to create map from attributes\n"); - exit(1); - } - map_handles=g_list_append(map_handles,handle); - break; - case 'n': - fprintf(stderr,"I will IGNORE unknown types\n"); - ignore_unkown=1; - break; - case 'k': - fprintf(stderr,"I will KEEP tmp files\n"); - keep_tmpfiles=1; - break; - case 'p': - add_plugin(optarg); - break; - case 's': - start=atoi(optarg); - break; - case 'w': - dedupe_ways_hash=g_hash_table_new(NULL, NULL); - break; - case 'i': - input_file = fopen( optarg, "r" ); - if ( input_file == NULL ) - { - fprintf( stderr, "\nInput file (%s) not found\n", optarg ); - exit( -1 ); - } - break; - case 'r': - rule_file = fopen( optarg, "r" ); - if ( rule_file == NULL ) - { - fprintf( stderr, "\nRule file (%s) not found\n", optarg ); - exit( -1 ); - } - break; - case 'u': - url=optarg; - break; -#ifdef HAVE_ZLIB - case 'z': - compression_level=atoi(optarg); - break; -#endif - case '?': - default: - fprintf(stderr,"c=%d\n", c); - usage(stderr); + exit(0); } - } - if (optind != argc-(output == 1 ? 0:1)) + if (optind != argc-(p.output == 1 ? 0:1)) usage(stderr); result=argv[optind]; // initialize plugins and OSM mappings maptool_init(rule_file); - if (protobufdb_operation) { - osm_protobufdb_load(input_file, protobufdb); + if (p.protobufdb_operation) { + osm_protobufdb_load(input_file, p.protobufdb); return 0; } // input from an OSM file - if (input == 0) { - if (start == 1) { + if (p.input == 0) { + if (p.start == 1) { unlink("coords.tmp"); - if (process_ways) + if (p.process_ways) ways=tempfile(suffix,"ways",1); - if (process_nodes) + if (p.process_nodes) nodes=tempfile(suffix,"nodes",1); - if (process_ways && process_nodes) { + if (p.process_ways && p.process_nodes) { turn_restrictions=tempfile(suffix,"turn_restrictions",1); if(doway2poi) way2poi=tempfile(suffix,"way2poi",1); } - if (process_relations) + if (p.process_relations) boundaries=tempfile(suffix,"boundaries",1); phase=1; fprintf(stderr,"PROGRESS: Phase 1: collecting data\n"); @@ -414,18 +444,18 @@ int main(int argc, char **argv) map_collect_data_osm_db(dbstr,&osm); else #endif - if (map_handles) { + if (p.map_handles) { GList *l; - phase1_map(map_handles,ways,nodes); - l=map_handles; + phase1_map(p.map_handles,ways,nodes); + l=p.map_handles; while (l) { map_destroy(l->data); l=g_list_next(l); } } - else if (protobuf) + else if (p.protobuf) map_collect_data_osm_protobuf(input_file,&osm); - else if (o5m) + else if (p.o5m) map_collect_data_osm_o5m(input_file,&osm); else map_collect_data_osm(input_file,&osm); @@ -447,7 +477,7 @@ int main(int argc, char **argv) fclose(way2poinew); tempfile_rename(suffix,"way2poi_resolved_new","way2poi_resolved"); way2poi=tempfile(suffix,"way2poi_resolved",0); - if (first && !keep_tmpfiles) + if (first && !p.keep_tmpfiles) tempfile_unlink(suffix,"way2poi"); } first=0; @@ -466,13 +496,13 @@ int main(int argc, char **argv) if (boundaries) fclose(boundaries); } - if (end == 1) + if (p.end == 1) exit(0); - if (start == 2) { + if (p.start == 2) { load_buffer("coords.tmp",&node_buffer,0, slice_size); } - if (start <= 2) { - if (process_ways) { + if (p.start <= 2) { + if (p.process_ways) { ways=tempfile(suffix,"ways",0); phase=2; fprintf(stderr,"PROGRESS: Phase 2: finding intersections\n"); @@ -496,7 +526,7 @@ int main(int argc, char **argv) ways=tempfile(suffix,"ways_to_resolve",0); } } - if(!keep_tmpfiles) + if(!p.keep_tmpfiles) tempfile_unlink(suffix,"ways"); tempfile_unlink(suffix,"ways_to_resolve"); } else @@ -506,7 +536,7 @@ int main(int argc, char **argv) node_buffer.base=NULL; node_buffer.malloced=0; node_buffer.size=0; - if (end == 2) + if (p.end == 2) exit(0); } else { ways_split=tempfile(suffix,"ways_split",0); @@ -524,10 +554,10 @@ int main(int argc, char **argv) fclose(coastline); } #endif - if (start <= 3) { + if (p.start <= 3) { fprintf(stderr,"PROGRESS: Phase 3: sorting countries, generating turn restrictions\n"); - sort_countries(keep_tmpfiles); - if (process_relations) { + sort_countries(p.keep_tmpfiles); + if (p.process_relations) { #if 0 boundaries=tempfile(suffix,"boundaries",0); ways_split=tempfile(suffix,"ways_split",0); @@ -548,32 +578,32 @@ int main(int argc, char **argv) fclose(coords); fclose(relations); fclose(turn_restrictions); - if(!keep_tmpfiles) + if(!p.keep_tmpfiles) tempfile_unlink(suffix,"turn_restrictions"); } } - if(!keep_tmpfiles) + if(!p.keep_tmpfiles) tempfile_unlink(suffix,"ways_split_index"); } - if (end == 3) + if (p.end == 3) exit(0); - if (output == 1) { + if (p.output == 1) { fprintf(stderr,"PROGRESS: Phase 4: dumping\n"); - if (process_nodes) { + if (p.process_nodes) { nodes=tempfile(suffix,"nodes",0); if (nodes) { dump(nodes); fclose(nodes); } } - if (process_ways) { + if (p.process_ways) { ways_split=tempfile(suffix,"ways_split",0); if (ways_split) { dump(ways_split); fclose(ways_split); } } - if (process_relations) { + if (p.process_relations) { relations=tempfile(suffix,"relations",0); fprintf(stderr,"Relations=%p\n",relations); if (relations) { @@ -585,11 +615,11 @@ int main(int argc, char **argv) } for (i = suffix_start ; i < suffix_count ; i++) { suffix=suffixes[i]; - if (start <= 4) { + if (p.start <= 4) { phase=3; if (i == suffix_start) { zip_info=zip_new(); - zip_set_zip64(zip_info, zip64); + zip_set_zip64(zip_info, p.zip64); zip_set_timestamp(zip_info, timestamp); } zipnum=zip_get_zipnum(zip_info); @@ -600,11 +630,11 @@ int main(int argc, char **argv) } else { for (f = 0 ; f < 3 ; f++) files[f]=NULL; - if (process_relations) + if (p.process_relations) files[0]=tempfile(suffix,"relations",0); - if (process_ways) + if (p.process_ways) files[1]=tempfile(suffix,"ways_split",0); - if (process_nodes) + if (p.process_nodes) files[2]=tempfile(suffix,"nodes",0); phase4(files,3,0,suffix,tilesdir,zip_info); @@ -616,29 +646,29 @@ int main(int argc, char **argv) fclose(tilesdir); zip_set_zipnum(zip_info,zipnum); } - if (end == 4) + if (p.end == 4) exit(0); if (zip_info) { zip_destroy(zip_info); zip_info=NULL; } - if (start <= 5) { + if (p.start <= 5) { phase=4; fprintf(stderr,"PROGRESS: Phase 5: assembling map %s\n",suffix); if (i == suffix_start) { char *zipdir=tempfile_name("zipdir",""); char *zipindex=tempfile_name("index",""); zip_info=zip_new(); - zip_set_zip64(zip_info, zip64); + zip_set_zip64(zip_info, p.zip64); zip_set_timestamp(zip_info, timestamp); zip_set_maxnamelen(zip_info, 14+strlen(suffixes[0])); - zip_set_compression_level(zip_info, compression_level); - if (md5file) + zip_set_compression_level(zip_info, p.compression_level); + if (p.md5file) zip_set_md5(zip_info, 1); zip_open(zip_info, result, zipdir, zipindex); - if (url) { + if (p.url) { map_information_attrs[1].type=attr_url; - map_information_attrs[1].u.str=url; + map_information_attrs[1].u.str=p.url; } index_init(zip_info, 1); } @@ -649,13 +679,13 @@ int main(int argc, char **argv) files[f]=NULL; references[f]=NULL; } - if (process_relations) + if (p.process_relations) files[0]=tempfile(suffix,"relations",0); - if (process_ways) { + if (p.process_ways) { files[1]=tempfile(suffix,"ways_split",0); references[1]=tempfile(suffix,"ways_split_ref",1); } - if (process_nodes) + if (p.process_nodes) files[2]=tempfile(suffix,"nodes",0); fprintf(stderr,"Slice %d\n",i); @@ -668,7 +698,7 @@ int main(int argc, char **argv) fclose(references[f]); } } - if(!keep_tmpfiles) { + if(!p.keep_tmpfiles) { tempfile_unlink(suffix,"relations"); tempfile_unlink(suffix,"nodes"); tempfile_unlink(suffix,"ways_split"); @@ -691,15 +721,15 @@ int main(int argc, char **argv) zip_write_index(zip_info); zip_write_directory(zip_info); zip_close(zip_info); - if (md5file && zip_get_md5(zip_info, md5_data)) { - FILE *md5=fopen(md5file,"w"); + if (p.md5file && zip_get_md5(zip_info, md5_data)) { + FILE *md5=fopen(p.md5file,"w"); int i; for (i = 0 ; i < 16 ; i++) fprintf(md5,"%02x",md5_data[i]); fprintf(md5,"\n"); fclose(md5); } - if (!keep_tmpfiles) { + if (!p.keep_tmpfiles) { remove_countryfiles(); tempfile_unlink("index",""); tempfile_unlink("zipdir","");