Dummy IRI parsing. Needed to fix segfault bug, when freeing person's URI
[cascardo/atompub.git] / atom / person.c
index f0f653e..899fb8f 100644 (file)
@@ -20,6 +20,7 @@
 #include <atompub/atom.h>
 
 #include <glib.h>
+#include <libxml/tree.h>
 
 struct _atom_person
 {
@@ -76,7 +77,7 @@ atom_person_uri_set (AtomPerson *person, IRI *uri)
 {
   if (person->uri)
     iri_delete (person->uri);
-  person->uri = uri;
+  person->uri = iri_copy (uri);
 }
 
 char *
@@ -92,3 +93,37 @@ atom_person_email_set (AtomPerson *person, char *email)
     g_free (person->email);
   person->email = g_strdup (email);
 }
+
+xmlNodePtr
+atom_person_to_xmlnode (AtomPerson *person, char *elname)
+{
+  xmlNodePtr node;
+  node = xmlNewNode (NULL, elname);
+  xmlNewTextChild (node, NULL, "name", person->name);
+  if (person->uri)
+    xmlNewTextChild (node, NULL, "uri", iri_get_string (person->uri));
+  if (person->email)
+    xmlNewTextChild (node, NULL, "email", person->email);
+  return node;
+}
+
+AtomPerson *
+atom_person_new_from_xmlnode (xmlNodePtr node)
+{
+  xmlNodePtr child;
+  AtomPerson *person;
+  person = g_slice_new0 (AtomPerson);
+  for (child = node->xmlChildrenNode; child != NULL; child = child->next)
+    {
+      char *content = xmlNodeGetContent (child->xmlChildrenNode);
+      if (!xmlStrcmp (child->name, "name"))
+       person->name = content;
+      else if (!xmlStrcmp (child->name, "uri"))
+       person->uri = iri_new_from_string (content);
+      else if (!xmlStrcmp (child->name, "email"))
+       person->email = content;
+      else
+       xmlFree (content);
+    }
+  return person;
+}