]> mj.ucw.cz Git - leo.git/blobdiff - xml.c
More ucw-xml includes fixed
[leo.git] / xml.c
diff --git a/xml.c b/xml.c
index 98f2f8b003c87599e8164adc5164e2904cdab0f8..69e739d06915121f8f34e95a73368e62dd219912 100644 (file)
--- a/xml.c
+++ b/xml.c
@@ -1,12 +1,14 @@
 /*
  *     Hic Est Leo -- OSM XML Parser
  *
- *     (c) 2014 Martin Mares <mj@ucw.cz>
+ *     (c) 2014--2015 Martin Mares <mj@ucw.cz>
  */
 
+#undef LOCAL_DEBUG
+
 #include <ucw/lib.h>
 #include <ucw/fastbuf.h>
-#include <xml/xml.h>
+#include <ucw-xml/xml.h>
 
 #include <fcntl.h>
 #include <stdio.h>
@@ -147,83 +149,48 @@ static void h_error(struct xml_context *ctx)
   fprintf(stderr, "%s at %u: %s\n", (ctx->err_code < XML_ERR_ERROR) ? "warn" : "error", xml_row(ctx), ctx->err_msg);
 }
 
-#if 0
-
-static void h_stag(struct xml_context *ctx)
-{
-  printf("STAG %s\n", ctx->node->name);
-  if (!strcmp(ctx->node->name, "node"))
-    {
-      ctx->flags &= ~XML_REPORT_TAGS;
-      ctx->flags |= XML_ALLOC_ALL;
-    }
-}
-
-static void h_etag(struct xml_context *ctx)
+static void parse_element(struct xml_context *ctx, struct xml_node *e)
 {
-  printf("ETAG %s\n", ctx->node->name);
+  if (!strcmp(e->name, "node"))
+    parse_node(ctx, e);
+  else if (!strcmp(e->name, "way"))
+    parse_way(ctx, e);
+  else if (!strcmp(e->name, "relation"))
+    parse_relation(ctx, e);
 }
 
-#endif
-
 void osm_xml_parse(const char *name)
 {
-  msg(L_INFO, "Loading %s", name);
-
-  struct xml_context ctx;
-  xml_init(&ctx);
-  ctx.h_warn = ctx.h_error = ctx.h_fatal = h_error;
-  xml_push_fastbuf(&ctx, bopen_file(name, O_RDONLY, NULL));
-
-#if 0
-  ctx.flags |= XML_REPORT_TAGS;
-  ctx.h_stag = h_stag;
-  ctx.h_etag = h_etag;
-  xml_parse(&ctx);
-#endif
-
-#if 0
-  uns state;
-  while (state = xml_next(&ctx))
-    switch (state)
+  struct xml_context xml_ctx, *ctx = &xml_ctx;
+  xml_init(ctx);
+  ctx->h_warn = ctx->h_error = ctx->h_fatal = h_error;
+  xml_push_fastbuf(ctx, bopen_file(name, O_RDONLY, NULL));
+
+  uint state;
+  while (state = xml_next_state(ctx, XML_PULL_STAG))
+    if (state == XML_STATE_STAG)
       {
-      case XML_STATE_STAG:
-       printf("STAG %s\n", ctx.node->name);
-       if (!strcmp(ctx.node->name, "node"))
+       DBG("Level 1 tag <%s>", ctx->node->name);
+       if (!strcmp(ctx->node->name, "osm"))
          {
-           ctx.pull = XML_PULL_ETAG;
-           ctx.flags |= XML_ALLOC_CHARS | XML_ALLOC_TAGS;
-         }
-       break;
-      case XML_STATE_ETAG:
-       printf("ETAG %s\n", ctx.node->name);
-       if (!strcmp(ctx.node->name, "node"))
-         {
-           ctx.pull = XML_PULL_STAG | XML_PULL_ETAG;
-           ctx.flags &= ~(XML_ALLOC_CHARS | XML_ALLOC_TAGS);
+           DBG("Switched to level 2");
+           while ((state = xml_next_state(ctx, XML_PULL_STAG | XML_PULL_ETAG)) == XML_STATE_STAG)
+             {
+               ctx->flags |= XML_ALLOC_CHARS | XML_ALLOC_TAGS;
+               if (xml_skip_element(ctx) == XML_STATE_ETAG)
+                 {
+                   DBG("Level 2 tag <%s>", ctx->node->name);
+                   parse_element(ctx, ctx->node);
+                 }
+             }
+           DBG("Exited level 2 in state %u", state);
          }
-       break;
+       else
+         xml_skip_element(ctx);
       }
-#endif
 
-  ctx.flags |= XML_ALLOC_ALL;
-  xml_parse(&ctx);
-
-  if (ctx.err_code)
+  if (ctx->err_code)
     die("Fatal error in XML parser");
 
-  struct xml_node *root = ctx.dom;
-  ASSERT(root);
-  XML_NODE_FOR_EACH(e, root)
-    if (e->type == XML_NODE_ELEM)
-      {
-       if (!strcmp(e->name, "node"))
-         parse_node(&ctx, e);
-       else if (!strcmp(e->name, "way"))
-         parse_way(&ctx, e);
-       else if (!strcmp(e->name, "relation"))
-         parse_relation(&ctx, e);
-      }
-
-  xml_cleanup(&ctx);
+  xml_cleanup(ctx);
 }