]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Add switch_xml_parse_str_dynamic and switch_xml_parse_str_dup
authorMathieu Rene <mrene@avgs.ca>
Thu, 7 May 2009 21:40:21 +0000 (21:40 +0000)
committerMathieu Rene <mrene@avgs.ca>
Thu, 7 May 2009 21:40:21 +0000 (21:40 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@13255 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_xml.h
src/switch_xml.c

index 77f34a666123a9cd499e590c87c0a95f9c0553d4..dded9480be497ded0f042d44a15b8a22bff25ced 100644 (file)
@@ -98,6 +98,21 @@ struct switch_xml {
        uint32_t flags;
 };
 
+/*! 
+ * \brief Parses a string into a switch_xml_t, ensuring the memory will be freed with switch_xml_free
+ * \param s The string to parse
+ * \param dup true if you want the string to be strdup()'d automatically
+ * \return the switch_xml_t or NULL if an error occured
+ */ 
+SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str_dynamic(char *s, switch_bool_t dup);
+
+/*! 
+ * \brief Parses a string into a switch_xml_t 
+ * \param s The string to parse
+ * \return the switch_xml_t or NULL if an error occured
+ */
+#define switch_xml_parse_str_dup(x)  switch_xml_parse_str_dynamic(x, SWITCH_TRUE)
+
 ///\brief Given a string of xml data and its length, parses it and creates an switch_xml
 ///\ structure. For efficiency, modifies the data by adding null terminators
 ///\ and decoding ampersand sequences. If you don't want this, copy the data and
index b3d830009df4c361a5098b19266eafbac208a2f2..6bd0b2d2548d70236cf1b7b1461627a14cb71d0b 100644 (file)
@@ -902,6 +902,19 @@ static void switch_xml_free_attr(char **attr)
        free(attr);
 }
 
+SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str_dynamic(char *s, switch_bool_t dup) 
+{
+       switch_xml_root_t root;
+       char *data = dup ? strdup(s) : s;
+       
+       if ((root = (switch_xml_root_t) switch_xml_parse_str(data, strlen(data)))) {
+               root->dynamic = 1; /* Make sure we free the memory is switch_xml_free() */
+               return &root->xml;
+       } else {
+               return NULL;
+       }
+}
+       
 /* parse the given xml string and return an switch_xml structure */
 SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str(char *s, switch_size_t len)
 {
@@ -1552,14 +1565,9 @@ SWITCH_DECLARE(switch_status_t) switch_xml_locate(const char *section,
 
                if ((conf = switch_xml_find_child(xml, "section", "name", section)) && (tag = switch_xml_find_child(conf, tag_name, key_name, key_value))) {
                        if (clone) {
-                               char *x;
-                               switch_xml_root_t xmlroot = NULL;
-                               x = switch_xml_toxml(tag, SWITCH_FALSE);
+                               char *x = switch_xml_toxml(tag, SWITCH_FALSE);
                                switch_assert(x);
-                               xmlroot = (switch_xml_root_t)switch_xml_parse_str(x, strlen(x));
-                               xmlroot->dynamic = 1;   /* free the memory in switch_xml_free */
-                               *root = (switch_xml_t)xmlroot;
-                               *node = *root;
+                               *node = *root = switch_xml_parse_str_dynamic(x, SWITCH_FALSE); /* x will be free()'d in switch_xml_free() */
                                switch_xml_free(xml);
                        } else {
                                *node = tag;