From: Kaj-Michael Lang Date: Tue, 2 Oct 2007 12:22:55 +0000 (+0300) Subject: Implement reading of bzip2 compressed planet files. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48272be1aac50872957409a364ef605b1dbe664a;p=mapper Implement reading of bzip2 compressed planet files. - Other small fixes --- diff --git a/src/osm.c b/src/osm.c index 97301ba..ff404da 100644 --- a/src/osm.c +++ b/src/osm.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "osm.h" #include "latlon.h" @@ -251,7 +252,7 @@ gboolean use_bbox; gint wsegcnt; -node *osm_find_node(guint32 nid); +inline node *osm_find_node(guint32 nid); void osm_free_way_data(way *w); void print_way(way *w); gboolean db_open(void); @@ -584,18 +585,6 @@ return TRUE; /********************************************************************/ -void dump_array(const gchar **p) -{ -char **d; - -d=p; -while (*d!=NULL) { - g_printf("[%s]", *d); - d++; -} -g_print("\n"); -} - gchar *get_attr_key_value(const gchar **p, gchar *key) { char **d; @@ -626,7 +615,7 @@ else return ERROR; /********************************************************************/ static void -print_node (node *n) +node_print (node *n) { g_assert(n); if (n->data) { @@ -638,8 +627,22 @@ if (n->data) { g_printf("N: %d [%f:%f]\n", n->id, n->lat, n->lon); } +} + +#ifdef DEBUG +static void +dump_array(const gchar **p) +{ +char **d; +d=p; +while (*d!=NULL) { + g_printf("[%s]", *d); + d++; +} +g_print("\n"); } +#endif static inline gboolean osm_node_check_box(gdouble nlat, gdouble nlon) @@ -663,6 +666,8 @@ noded_cnt++; void osm_free_node_data(node *n) { +g_assert(n); +g_assert(n->data); if (n->data->name) g_free(n->data->name); g_slice_free(node_data, n->data); @@ -688,6 +693,7 @@ return n; static void print_segment(segment *s) { +g_assert(s); g_printf("Segment %d %d-%d:\n", s->id, s->from, s->to); } @@ -700,6 +706,7 @@ print_segment(s); void print_way(way *w) { +g_assert(w); g_printf("Way #%d(%d/%d): %s [%s:%s:%s]\n", w->id, g_slist_length(w->nodes), @@ -1293,46 +1300,55 @@ XML_ParserFree(xp); storage_free(); } +gboolean +osm_planet_parse_buffer(gchar *buffer, size_t r) +{ +if (XML_Parse(xp, buffer, r, r>0 ? 0:1) == XML_STATUS_ERROR) { + g_printerr("Parse error at line %d:\n%s\n", + XML_GetCurrentLineNumber(xp), + XML_ErrorString(XML_GetErrorCode(xp))); + return FALSE; +} +return TRUE; +} + gboolean osm_planet_parse_file(gchar *pfile) { -int f, r; +FILE *f; +BZFILE *b; +int bzerror; +int r; gchar buffer[FILE_BUFFER]; gboolean res=TRUE; -f=open(pfile, O_RDONLY); -if (f==-1) return FALSE; +f=fopen(pfile, "r"); +if (!f) + return FALSE; + +b=BZ2_bzReadOpen(&bzerror, f, 0, 0, NULL, 0); +if (bzerror != BZ_OK) { + BZ2_bzReadClose(&bzerror, b); + return FALSE; +} do { - r=read(f, buffer, FILE_BUFFER); - if (XML_Parse(xp, buffer, r, r>0 ? 0:1) == XML_STATUS_ERROR) { - g_printerr("Parse error at line %d:\n%s\n", - XML_GetCurrentLineNumber(xp), - XML_ErrorString(XML_GetErrorCode(xp))); + r=BZ2_bzRead(&bzerror, b, buffer, FILE_BUFFER); + if ((bzerror!=BZ_STREAM_END) && (bzerror!=BZ_OK)) { res=FALSE; - goto end; + break; } -} while (r>0); - -end:; + if (!osm_planet_parse_buffer(buffer, r)) { + res=FALSE; + break; + } +} while (bzerror==BZ_OK); -close(f); +BZ2_bzReadClose(&bzerror, b); +fclose(f); return res; } -gboolean -osm_planet_parse_buffer(gchar *buffer, size_t r) -{ -if (XML_Parse(xp, buffer, r, r>0 ? 0:1) == XML_STATUS_ERROR) { - g_printerr("Parse error at line %d:\n%s\n", - XML_GetCurrentLineNumber(xp), - XML_ErrorString(XML_GetErrorCode(xp))); - return FALSE; -} - -return TRUE; -} - /************************************************************************/ static gint @@ -1360,14 +1376,13 @@ g_printf("WayD size: %d\n", sizeof(way_data)); int main (int argc, char **argv) { if (argc<2) { - return print_fail("No planet XML file given", 1); + return print_fail("Give bzip2 compressed planet XML file as argument", 1); } else { g_printf("Using file: %s\n", argv[1]); } if (argc==6) { use_bbox=TRUE; - bbox.lat_min=atof(argv[2]); bbox.lon_min=atof(argv[3]); bbox.lat_max=atof(argv[4]);