[gnome-maps/wip/mlundblad/osm-oauth-external: 1/2] WIP: maps-osm: Add function to parse OSM user details




commit 9b73d9d816148a0751a7f79292575da7552f6388
Author: Marcus Lundblad <ml dfupdate se>
Date:   Sun Apr 24 23:26:08 2022 +0200

    WIP: maps-osm: Add function to parse OSM user details

 lib/maps-osm.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 lib/maps-osm.h |  2 ++
 2 files changed, 48 insertions(+), 1 deletion(-)
---
diff --git a/lib/maps-osm.c b/lib/maps-osm.c
index 307a9361..1164001c 100644
--- a/lib/maps-osm.c
+++ b/lib/maps-osm.c
@@ -169,7 +169,8 @@ get_sub_node (xmlDoc *doc)
   xmlXPathObject * xpath_obj;
 
   xpath_ctx = xmlXPathNewContext (doc);
-  xpath_obj = xmlXPathEvalExpression ((xmlChar *) "/osm/node|/osm/way|/osm/relation",
+  xpath_obj = xmlXPathEvalExpression ((xmlChar *)
+                                      "/osm/node|/osm/way|/osm/relation|/osm/user",
                                       xpath_ctx);
 
   if (xpath_obj && xpath_obj->nodesetval && xpath_obj->nodesetval->nodeNr > 0)
@@ -469,3 +470,47 @@ maps_osm_parse (const char *content, guint length, GError **error)
 
   return object;
 }
+
+/**
+ * maps_osm_parse_user_details:
+ * @content: XML data
+ * @error: Error handle
+ * Returns: (transfer full) Logged in user name
+ */
+char *
+maps_osm_parse_user_details (const char *content, GError **error)
+{
+  xmlDocPtr doc;
+  xmlNodePtr sub_node;
+  char *ret;
+
+  doc = read_xml_doc (content, strlen (content), error);
+  sub_node = get_sub_node (doc);
+
+  if (!sub_node)
+    {
+      xmlFreeDoc (doc);
+      *error = g_error_new_literal (MAPS_OSM_ERROR, 0,
+                                   _("Could not find OSM element"));
+      return NULL;
+    }
+
+  if (g_str_equal (sub_node->name, "user"))
+    {
+      g_autoptr (GHashTable) attributes;
+
+      attributes = parse_attributes (sub_node);
+      ret = g_strdup (g_hash_table_lookup (attributes, "display_name"));
+    }
+  else
+    {
+      *error = g_error_new_literal (MAPS_OSM_ERROR, 0,
+                                    _("Could not find user element"));
+      ret = NULL;
+    }
+
+    xmlFreeDoc (doc);
+    xmlFreeNode (sub_node);
+
+    return ret;
+}
diff --git a/lib/maps-osm.h b/lib/maps-osm.h
index faca7404..b3202479 100644
--- a/lib/maps-osm.h
+++ b/lib/maps-osm.h
@@ -32,4 +32,6 @@ void maps_osm_finalize (void);
 MapsOSMObject *maps_osm_parse (const char *content, guint length,
                                GError **error);
 
+
+char *maps_osm_parse_user_details(const char *content, GError **error);
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]