[svn-commits] eliel: branch group/data_api_gsoc2009 r202151 - in /team/group/data_api_gsoc2...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sat Jun 20 11:13:39 CDT 2009


Author: eliel
Date: Sat Jun 20 11:13:35 2009
New Revision: 202151

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=202151
Log:
- Rename the functions to follow a naming convention.
        data_provider_* for functions related to the provider tree.
	data_result_* for functions related to the ast_data returned tree.
- Implement two needed functions to manipulate the result tree.


Modified:
    team/group/data_api_gsoc2009/include/asterisk/data.h
    team/group/data_api_gsoc2009/main/data.c

Modified: team/group/data_api_gsoc2009/include/asterisk/data.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/include/asterisk/data.h?view=diff&rev=202151&r1=202150&r2=202151
==============================================================================
--- team/group/data_api_gsoc2009/include/asterisk/data.h (original)
+++ team/group/data_api_gsoc2009/include/asterisk/data.h Sat Jun 20 11:13:35 2009
@@ -217,6 +217,14 @@
 struct ast_data *ast_data_create(const char *name);
 
 /*!
+ * \brief Get a node type.
+ * \param[in] res A pointer to the ast_data result set.
+ * \param[in] path A path to the node to get the type.
+ * \return The type of the requested node type.
+ */
+enum ast_data_type ast_data_get_type(struct ast_data *res, const char *path);
+
+/*!
  * \brief Add a container child.
  * \TODO: Complete docs
  */

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=202151&r1=202150&r2=202151
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Sat Jun 20 11:13:35 2009
@@ -57,7 +57,7 @@
 };
 
 /*! \brief A data container node pointing to the registered handler. */
-struct data_node_provider {
+struct data_provider {
 	/*! \brief node content handler. */
 	const struct ast_data_handler *handler;
 	/*! \brief children nodes. */
@@ -83,21 +83,21 @@
  * \brief Common string hash function.
  * \see ast_data_init
  */
-static int data_node_provider_hash(const void *obj, const int flags)
-{
-	const struct data_node_provider *node = obj;
+static int data_provider_hash(const void *obj, const int flags)
+{
+	const struct data_provider *node = obj;
 
 	return ast_str_hash(node->name);
 }
 
 /*!
  * \internal
- * \brief Compare two data_node_provider's.
+ * \brief Compare two data_provider's.
  * \see ast_data_init
  */
-static int data_node_provider_cmp(void *obj, void *arg, int flags)
-{
-	struct data_node_provider *node1 = obj, *node2 = arg;
+static int data_provider_cmp(void *obj, void *arg, int flags)
+{
+	struct data_provider *node1 = obj, *node2 = arg;
 
 	return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
 }
@@ -161,10 +161,10 @@
  * \retval NULL on error.
  * \retval The allocated data node structure.
  */
-static struct data_node_provider *data_node_provider_new(const char *name,
+static struct data_provider *data_provider_new(const char *name,
 	const struct ast_data_handler *handler, const char *registrar)
 {
-	struct data_node_provider *node;
+	struct data_provider *node;
 	size_t namelen;
 
 	namelen = strlen(name) + 1;
@@ -180,7 +180,7 @@
 
 	/* initialize the childrens container. */
 	if (!(node->children = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
-			data_node_provider_hash, data_node_provider_cmp))) {
+			data_provider_hash, data_provider_cmp))) {
 		ao2_ref(node, -1);
 		return NULL;
 	}
@@ -198,12 +198,12 @@
  * \retval NULL on error.
  * \retval A newly allocated child in parent.
  */
-static struct data_node_provider *data_node_provider_add_child(struct ao2_container *parent,
+static struct data_provider *data_provider_add_child(struct ao2_container *parent,
 	const char *name, const struct ast_data_handler *handler, const char *registrar)
 {
-	struct data_node_provider *child;
-
-	child = data_node_provider_new(name, handler, registrar);
+	struct data_provider *child;
+
+	child = data_provider_new(name, handler, registrar);
 	if (!child) {
 		return NULL;
 	}
@@ -223,13 +223,13 @@
  * \retval The node found.
  * \note Remember to decrement the ref count of the returned node after using it.
  */
-static struct data_node_provider *data_find_child(struct ao2_container *parent,
+static struct data_provider *data_provider_find(struct ao2_container *parent,
 	const char *name, const char *registrar)
 {
-	struct data_node_provider *find_node, *found;
+	struct data_provider *find_node, *found;
 
 	/* XXX avoid allocating a new data node for searching... */
-	find_node = data_node_provider_new(name, NULL, NULL);
+	find_node = data_provider_new(name, NULL, NULL);
 	if (!find_node) {
 		return NULL;
 	}
@@ -261,13 +261,13 @@
  * \param[in] registrar Who registered this node.
  * \retval <0 on error.
  * \retval 0 on success.
- * \see data_node_provider_create
- */
-static int data_node_provider_release(struct ao2_container *parent, const char *path,
+ * \see data_provider_create
+ */
+static int data_provider_release(struct ao2_container *parent, const char *path,
 	const char *registrar)
 {
 	char *node_name, *rpath;
-	struct data_node_provider *child;
+	struct data_provider *child;
 	int ret = 0;
 
 	rpath = strdupa(path);
@@ -280,14 +280,14 @@
 		return -1;
 	}
 
-	child = data_find_child(parent, node_name, registrar);
+	child = data_provider_find(parent, node_name, registrar);
 	if (!child) {
 		return -1;
 	}
 
 	/* if this is not a terminal node. */
 	if (!child->handler && rpath) {
-		ret = data_node_provider_release(child->children, rpath, registrar);
+		ret = data_provider_release(child->children, rpath, registrar);
 	}
 
 	/* if this node is empty, unlink it. */
@@ -307,17 +307,17 @@
  * \param[in] registrar
  * \see __ast_data_unregister
  */
-static void data_node_provider_release_all(struct ao2_container *parent,
+static void data_provider_release_all(struct ao2_container *parent,
 	const char *registrar)
 {
 	struct ao2_iterator i;
-	struct data_node_provider *node;
+	struct data_provider *node;
 
 	i = ao2_iterator_init(parent, 0);
 	while ((node = ao2_iterator_next(&i))) {
 		if (!node->handler) {
 			/* this is a non-terminal node, go inside it. */
-			data_node_provider_release_all(node->children, registrar);
+			data_provider_release_all(node->children, registrar);
 			if (!ao2_container_count(node->children)) {
 				/* if this node was left empty, unlink it. */
 				ao2_unlink(parent, node);
@@ -341,13 +341,13 @@
  * \param[in] registrar Who is trying to create this node provider.
  * \retval NULL on error.
  * \retval The created node.
- * \see data_node_provider_release
- */
-static struct data_node_provider *data_node_provider_create(struct ao2_container *parent,
+ * \see data_provider_release
+ */
+static struct data_provider *data_provider_create(struct ao2_container *parent,
 	const char *path, const char *registrar)
 {
 	char *rpath, *node_name;
-	struct data_node_provider *child, *ret = NULL;
+	struct data_provider *child, *ret = NULL;
 
 	rpath = strdupa(path);
 	if (!rpath) {
@@ -360,15 +360,15 @@
 		return NULL;
 	}
 
-	child = data_find_child(parent, node_name, registrar);
+	child = data_provider_find(parent, node_name, registrar);
 
 	if (!child) {
 		/* nodes without handler are non-terminal nodes. */
-		child = data_node_provider_add_child(parent, node_name, NULL, registrar);
+		child = data_provider_add_child(parent, node_name, NULL, registrar);
 	}
 
 	if (rpath) {
-		ret = data_node_provider_create(child->children, rpath, registrar);
+		ret = data_provider_create(child->children, rpath, registrar);
 		if (ret) {
 			ao2_ref(child, -1);
 		}
@@ -380,7 +380,7 @@
 int __ast_data_register(const char *path, const struct ast_data_handler *handler,
 	const char *registrar)
 {
-	struct data_node_provider *node;
+	struct data_provider *node;
 
 	if (!path) {
 		return -1;
@@ -389,7 +389,7 @@
 	/* create the node structure for the registered handler. */
 	data_write_lock();
 
-	node = data_node_provider_create(root_data, path, registrar);
+	node = data_provider_create(root_data, path, registrar);
 	if (!node) {
 		ast_log(LOG_ERROR, "Unable to create the specified path (%s) "
 			"for '%s'.\n", path, registrar);
@@ -442,9 +442,9 @@
 
 	data_write_lock();
 	if (path) {
-		ret = data_node_provider_release(root_data, path, registrar);
+		ret = data_provider_release(root_data, path, registrar);
 	} else {
-		data_node_provider_release_all(root_data, registrar);
+		data_provider_release_all(root_data, registrar);
 	}
 	data_unlock();
 
@@ -460,7 +460,7 @@
  * \internal
  * \brief Release the memory allocated by a call to ao2_alloc.
  */
-static void data_node_destructor(void *obj)
+static void data_result_destructor(void *obj)
 {
 	struct ast_data *node = obj;
 
@@ -484,14 +484,75 @@
 
 	namelen = strlen(name) + 1;
 
-	res = ao2_alloc(sizeof(*res) + namelen, data_node_destructor);
+	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;
 	}
 
+	/* set this node as a container. */
+	res->type = AST_DATA_CONTAINER;
+
 	return res;
+}
+
+/*!
+ * \internal
+ * \brief Get an internal node, from the result set.
+ * \param[in] node A node container.
+ * \param[in] path The path to the needed internal node.
+ * \retval NULL if the internal node is not found.
+ * \retval non-NULL the internal node with path 'path'.
+ */
+static struct ast_data *data_result_get_node(struct ast_data *node,
+	const char *path)
+{
+	char *savepath;
+	struct ast_data *child, *findtmp, *res;
+
+	if (!path) {
+		return node;
+	}
+
+	savepath = ast_strdupa(path);
+	if (!savepath) {
+		ast_log(LOG_ERROR, "Unable to allocate memory to store "
+			"the passed path\n");
+		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);
+
+	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;
+}
+
+enum ast_data_type ast_data_get_type(struct ast_data *node, const char *path)
+{
+	struct ast_data *internal;
+
+	internal = data_result_get_node(node, path);
+	if (!internal) {
+		return -1;
+	}
+
+	return internal->type;
 }
 
 struct ast_data *ast_data_add_node(struct ast_data *root, const char *childname)
@@ -580,7 +641,7 @@
 	ast_rwlock_init(&root_data_lock);
 
 	if (!(root_data = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
-		data_node_provider_hash, data_node_provider_cmp))) {
+		data_provider_hash, data_provider_cmp))) {
 		return -1;
 	}
 




More information about the svn-commits mailing list