[svn-commits] eliel: branch group/data_api_gsoc2009 r202152 - /team/group/data_api_gsoc2009...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sat Jun 20 12:33:31 CDT 2009


Author: eliel
Date: Sat Jun 20 12:33:27 2009
New Revision: 202152

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=202152
Log:
Implement one of the needed recursive data generators.

Modified:
    team/group/data_api_gsoc2009/main/data.c

Modified: team/group/data_api_gsoc2009/main/data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/main/data.c?view=diff&rev=202152&r1=202151&r2=202152
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Sat Jun 20 12:33:27 2009
@@ -471,23 +471,15 @@
 	}
 }
 
-struct ast_data *ast_data_create(const char *name)
+static struct ast_data *data_result_create(const char *name)
 {
 	struct ast_data *res;
 	size_t namelen;
 
-	if (!name) {
-		ast_log(LOG_ERROR, "You need to pass a root node name in order to "
-			"create a data structure\n");
-		return NULL;
-	}
-
 	namelen = strlen(name) + 1;
 
 	res = ao2_alloc(sizeof(*res) + namelen, data_result_destructor);
 	if (!res) {
-		ast_log(LOG_ERROR, "Unable to allocate a node structure "
-			"named '%s'.\n", name);
 		return NULL;
 	}
 
@@ -495,6 +487,30 @@
 	res->type = AST_DATA_CONTAINER;
 
 	return res;
+}
+
+/*!
+ * \internal
+ * \brief Find a child node, based on its name.
+ * \param[in] root The starting point.
+ * \param[in] name The child name.
+ * \retval NULL if the node wasn't found.
+ * \retval non-NULL the node we were looking for.
+ */
+static struct ast_data *data_result_find_child(struct ast_data *root, const char *name)
+{
+	struct ast_data *found, *find_node;
+
+	find_node = data_result_create(name);
+	if (!find_node) {
+		return NULL;
+	}
+
+	found = ao2_find(root->children, find_node, OBJ_POINTER);
+
+	ao2_ref(find_node, -1);
+
+	return found;
 }
 
 /*!
@@ -509,7 +525,7 @@
 	const char *path)
 {
 	char *savepath;
-	struct ast_data *child, *findtmp, *res;
+	struct ast_data *child;
 
 	if (!path) {
 		return node;
@@ -522,25 +538,60 @@
 		return NULL;
 	}
 
-	findtmp = ast_data_create(next_node_name(&savepath));
-	if (!findtmp) {
-		ast_log(LOG_ERROR, "Unable to allocate the temporary node "
-			"used for searching\n");
-		return NULL;
-	}
-
-	child = ao2_find(node->children, findtmp, OBJ_POINTER);
-	ao2_ref(findtmp, -1);
-
+	child = data_result_find_child(node, next_node_name(&savepath));
 	if (!child) {
 		ast_log(LOG_ERROR, "Unable to find node '%s' inside '%s'\n",
 				path, node->name);
 		return NULL;
 	}
 
-	res = data_result_get_node(child, savepath);
-
-	return res;
+	return data_result_get_node(child, savepath);
+}
+
+/*!
+ * \brief Add a child to the specified root node.
+ * \param[in] root The root node pointer.
+ * \param[in] child The child to add to the root node.
+ */
+static void data_result_add_child(struct ast_data *root, struct ast_data *child)
+{
+	ao2_link(root->children, child);
+}
+
+static struct ast_data *data_result_generate(struct data_provider *root_provider,
+	const char *parent_node_name)
+{
+	struct ast_data *generated, *node;
+	struct ao2_iterator i;
+	struct data_provider *provider;
+
+	node = data_result_create(parent_node_name);
+	if (!node) {
+		ast_log(LOG_ERROR, "Unable to allocate '%s' node\n", parent_node_name);
+		return NULL;
+	}
+
+	/* if this is a terminal node, just run the callback function. */
+	if (root_provider->handler->get) {
+		generated = root_provider->handler->get();
+		if (generated) {
+			data_result_add_child(node, generated);
+		}
+
+		return node;
+	}
+
+	/* if this is not a terminal node, generate every child node. */
+	i = ao2_iterator_init(root_provider->children, 0);
+	while ((provider = ao2_iterator_next(&i))) {
+		generated = data_result_generate(provider, provider->name);
+		if (generated) {
+			data_result_add_child(node, generated);
+		}
+		ao2_ref(provider, -1);
+	}
+
+	return node;
 }
 
 enum ast_data_type ast_data_get_type(struct ast_data *node, const char *path)
@@ -584,6 +635,17 @@
 	/* TODO: implement this. */
 	/* adds a boolean node inside another node. */
 	return NULL;
+}
+
+struct ast_data *ast_data_create(const char *name)
+{
+	if (!name) {
+		ast_log(LOG_ERROR, "You must specify a root node name in order to create "
+			"a data result tree\n");
+		return NULL;
+	}
+
+	return data_result_create(name);
 }
 
 void ast_data_free(struct ast_data *root)




More information about the svn-commits mailing list