[asterisk-commits] eliel: branch eliel/data_retrieval r189600 - in /team/eliel/data_retrieval: i...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 21 08:40:00 CDT 2009


Author: eliel
Date: Tue Apr 21 08:39:45 2009
New Revision: 189600

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=189600
Log:
Now instead of returning a DOM return the root node.
Implement the ast_data_iterator_(start,next,stop)


Modified:
    team/eliel/data_retrieval/include/asterisk/data.h
    team/eliel/data_retrieval/main/data.c

Modified: team/eliel/data_retrieval/include/asterisk/data.h
URL: http://svn.digium.com/svn-view/asterisk/team/eliel/data_retrieval/include/asterisk/data.h?view=diff&rev=189600&r1=189599&r2=189600
==============================================================================
--- team/eliel/data_retrieval/include/asterisk/data.h (original)
+++ team/eliel/data_retrieval/include/asterisk/data.h Tue Apr 21 08:39:45 2009
@@ -80,6 +80,8 @@
 struct ast_data;
 /*! \brief opaque definition of an ast_data_node result. */
 struct ast_data_node;
+/*! \brief opaque definition of an ast_data_iterator */
+struct ast_data_iterator;
 
 typedef struct ast_data_node *(*ast_data_cb)(void);
 
@@ -205,6 +207,31 @@
  */
 int ast_data_insert_bool(struct ast_data_node *handler, const char *path, int value);
 
+/*!
+ * \brief Initialize the iterator structure for a path.
+ * \param d
+ * \param path
+ * \retval NULL on error.
+ * \retval
+ */
+struct ast_data_iterator *ast_data_iterator_start(struct ast_data *d, const char *path);
+
+/*!
+ * \brief
+ * \param i
+ * \retval NULL on error.
+ * \retval
+ */
+struct ast_data *ast_data_iterator_next(struct ast_data_iterator *i);
+
+/*!
+ * \brief
+ * \param i
+ * \retval < 0 on error.
+ * \retval 0 on success.
+ */
+int ast_data_iterator_stop(struct ast_data_iterator *i);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/eliel/data_retrieval/main/data.c
URL: http://svn.digium.com/svn-view/asterisk/team/eliel/data_retrieval/main/data.c?view=diff&rev=189600&r1=189599&r2=189600
==============================================================================
--- team/eliel/data_retrieval/main/data.c (original)
+++ team/eliel/data_retrieval/main/data.c Tue Apr 21 08:39:45 2009
@@ -48,6 +48,14 @@
 	char name[0];
 };
 
+/*! \brief A data iterator. */
+struct ast_data_iterator {
+	/*! \brief Current data node. */
+	struct ast_xml_node *current;
+	/*! \brief The iterator string. */
+	char path[0];
+};
+
 /*! \brief The asterisk data maint content structure. */
 static struct ao2_container *root_data;
 
@@ -359,9 +367,8 @@
  * \retval NULL on error.
  * \retval An xml document with the generated result.
  */
-static struct ast_xml_doc *data_xml_retrieve(const char *path)
-{
-	struct ast_xml_doc *doc;
+static struct ast_xml_node *data_xml_retrieve(const char *path)
+{
 	struct ast_xml_node *root_node;
 	struct data_node *child = NULL;
 	char *rpath, *node_name;
@@ -397,22 +404,13 @@
 		return NULL;
 	}
 
-	doc = ast_xml_new();
-	if (!doc) {
-		ast_log(LOG_ERROR, "Unable to create internal data representation structure.\n");
-		ast_xml_free_node(root_node);
-		return NULL;
-	}
-
-	ast_xml_set_root(doc, root_node);
-
-	return doc;
+	return root_node;
 }
 
 /*!
  * \internal
  * \brief Create xml nodes based on the path.
- * \param[in] doc The xml document.
+ * \param[in] root The xml root node.
  * \param[in] path The path of nodes to create.
  * \retval NULL on error.
  * \retval A pointer to the last allocated node.
@@ -488,7 +486,7 @@
 		return NULL;
 	}
 
-	node = ast_xml_get_root((struct ast_xml_doc *) handler);
+	node = (struct ast_xml_node *) handler;
 
 	rpath = ast_strdupa(path);
 	if (!rpath || !node) {
@@ -509,6 +507,9 @@
 	node_name = next_node_name(&rpath);
 	while (node_name && node) {
 		node = ast_xml_find_element(ast_xml_node_get_children(node), node_name, NULL, NULL);
+		if (!node) {
+			ast_log(LOG_ERROR, "Unknown node children: %s\n", node_name);
+		}
 		node_name = next_node_name(&rpath);
 	}
 
@@ -675,26 +676,26 @@
 
 struct ast_data *ast_data_get(const char *path)
 {
-	struct ast_xml_doc *doc;
+	struct ast_xml_node *res_node;
 
 	data_read_lock();
-	doc = data_xml_retrieve(path);
+	res_node = data_xml_retrieve(path);
 	data_unlock();
 
-	if (!doc) {
+	if (!res_node) {
 		ast_log(LOG_ERROR, "Unable to get data from %s\n", path);
 		return NULL;
 	}
 
-	return (struct ast_data *) doc;
+	return (struct ast_data *) res_node;
 }
 
 void ast_data_free(struct ast_data *retrieved_data)
 {
-	struct ast_xml_doc *dom = (struct ast_xml_doc *) retrieved_data;
-
-	if (dom) {
-		ast_xml_close(dom);
+	struct ast_xml_node *root_node = (struct ast_xml_node *) retrieved_data;
+
+	if (root_node) {
+		ast_xml_free_node(root_node);
 	}
 }
 
@@ -793,22 +794,74 @@
 	return 0;
 }
 
+struct ast_data_iterator *ast_data_iterator_start(struct ast_data *data_handler, const char *path)
+{
+	struct ast_xml_node *node;
+	struct ast_data_iterator *iterator;
+
+	node = data_xml_node_get(data_handler, path);
+	if (!node) {
+		return NULL;
+	}
+
+	iterator = ast_calloc(1, sizeof(*iterator) + strlen(path) + 1);
+	if (!iterator) {
+		return NULL;
+	}
+
+	iterator->current = ast_xml_node_get_children(node);
+
+	strcpy(iterator->path, path);
+
+	return iterator;
+}
+
+struct ast_data *ast_data_iterator_next(struct ast_data_iterator *i)
+{
+	struct ast_xml_node *next;
+
+	if (!i) {
+		return NULL;
+	}
+
+	next = i->current;
+
+	if (i->current) {
+		i->current = ast_xml_node_get_next(i->current);
+	}
+
+	return (struct ast_data *) next;
+}
+
+int ast_data_iterator_stop(struct ast_data_iterator *i)
+{
+	if (!i) {
+		return -1;
+	}
+
+	ast_free(i);
+
+	i = NULL;
+
+	return 0;
+}
+
 static struct ast_data_node *test_data_provider(void)
 {
 	struct ast_data_node *res;
 
 	res = ast_data_result("eliel");
 
-	ast_data_insert_long(res, "eliel/canaan/sardanons", 26);
-	ast_data_insert_str(res, "eliel/pruebastr", "test string");
+	ast_data_insert_long(res, "eliel/test1", 26);
+	ast_data_insert_str(res, "eliel/test2", "test string");
 
 	return res;
 }
 
 int ast_data_init(void)
 {
-	FILE *out;
-	struct ast_data *res;
+	struct ast_data *res, *d;
+	struct ast_data_iterator *i;
 
 	ast_rwlock_init(&root_data_lock);
 
@@ -821,13 +874,15 @@
 	ast_data_register("asterisk/node1/node11/node111", test_data_provider);
 	ast_data_register("asterisk/node2/node21/node211", test_data_provider);
 
-	res = ast_data_get("asterisk/node1");
-
-	out = fopen("/tmp/xmltest", "w+");
-	ast_xml_doc_dump_file(out, (struct ast_xml_doc *)res);
-	fclose(out);
-
-	ast_data_free(res);
+	res = ast_data_get("asterisk");
+
+	i = ast_data_iterator_start(res, "/asterisk/node1/node11/node111/eliel");
+	while ((d = ast_data_iterator_next(i))) {
+		ast_log(LOG_ERROR, "Node type = %d\n", ast_data_get_type(d, "/"));
+	}
+	ast_data_iterator_stop(i);
+
+	/* end of tests. */
 
 	return 0;
 }




More information about the asterisk-commits mailing list