[asterisk-commits] eliel: branch group/data_api_gsoc2009 r210471 - /team/group/data_api_gsoc2009...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Aug 4 14:28:55 CDT 2009


Author: eliel
Date: Tue Aug  4 14:28:52 2009
New Revision: 210471

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=210471
Log:
Generate the filtering tree in another way we can now use:
/queues/queue/name|count|wrapuptime


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=210471&r1=210470&r2=210471
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Tue Aug  4 14:28:52 2009
@@ -863,7 +863,7 @@
  * \retval NULL on error.
  * \retval non-NULL A dynamically allocated search tree.
  */
-static struct ast_data_search *data_search_parse(const char *search_string)
+static struct ast_data_search *data_search_generate(const char *search_string)
 {
 	struct ast_str *name, *value, *comparison;
 	char *elements, *search_string_dup, *saveptr;
@@ -1438,13 +1438,34 @@
 {
 	struct data_filter_node *node;
 
+	while ((node = AST_RWLIST_REMOVE_HEAD(flist, next))) {
+		data_filter_list_destroy(&(node->children));
+		ast_free(node);
+	}
+}
+
+/*!
+ * \internal
+ * \brief
+ */
+static struct data_filter_node *data_filter_add_node(struct data_filter_list *flist, char *name)
+{
+	struct data_filter_node *node;
+
 	AST_LIST_TRAVERSE(flist, node, next) {
-		if (node->children.first) {
-			data_filter_list_destroy(&(node->children));
-		}
-
-		ast_free(node);
-	}
+		if (!strcasecmp(node->name, name)) {
+			return node;
+		}
+	}
+
+	node = ast_calloc(1, sizeof(*node) + strlen(name) + 1);
+	if (!node) {
+		return NULL;
+	}
+	strcpy(node->name, name);
+	AST_LIST_INSERT_TAIL(flist, node, next);
+
+	return node;
 }
 
 /*!
@@ -1453,49 +1474,41 @@
  * \param[in] Filter list to add the path onto.
  * \param[in] The path to add into the filter list.
  * \retval NULL on error.
- * \retval non-NULL XXX-document this.
- */
-static struct data_filter_list *data_filter_add_node(struct data_filter_list *flist, char *path)
-{
-	int found;
-	struct data_filter_list **list_head = &flist;
+ * \retval non-NULL A tree with the wanted nodes.
+ */
+static struct data_filter_list *data_filter_add_nodes(struct data_filter_list *flist, char *path)
+{
 	struct data_filter_node *node;
-	char *token, *saveptr;
-
-	for (token = strtok_r(path, "/", &saveptr); token; token = strtok_r(NULL, "/", &saveptr)) {
+	char *savepath, *saveptr, *token, *node_name;
+
+	if (!path) {
+		return NULL;
+	}
+
+	savepath = ast_strdupa(path);
+
+	node_name = next_node_name(&savepath);
+
+	if (!node_name) {
+		return NULL;
+	}
+
+	for (token = strtok_r(node_name, "|", &saveptr);
+			token; token = strtok_r(NULL, "|", &saveptr)) {
 		if (!flist) {
 			flist = ast_calloc(1, sizeof(*flist));
 			if (!flist) {
-				/* XXX: release already allocated memory. */
 				return NULL;
 			}
-		} else {
-			found = 0;
-
-			AST_LIST_TRAVERSE(flist, node, next) {
-				if (!strcasecmp(node->name, token)) {
-					found = 1;
-					break;
-				}
-			}
-
-			if (found) {
-				continue;
-			}
-		}
-
-		node = ast_calloc(1, sizeof(*node) + strlen(token) + 2);
+		}
+		node = data_filter_add_node(flist, token);
 		if (!node) {
-			/* XXX: release already allocated memory. */
-			return NULL;
-		}
-		*(node->name) = '/';
-		strcpy(node->name + 1, token);
-		AST_LIST_INSERT_TAIL(flist, node, next);
-		flist = &(node->children);
-	}
-
-	return *list_head;
+			continue;
+		}
+		data_filter_add_nodes(&(node->children), savepath);
+	}
+
+	return flist;
 }
 
 /*!
@@ -1506,70 +1519,20 @@
 static struct data_filter_list *data_filter_generate(const char *constfilter)
 {
 	struct data_filter_list *filter = NULL;
-	char *strfilter = ast_strdupa(constfilter), *token, *saveptr;
+	char *strfilter, *token, *saveptr;
+
+	if (!constfilter) {
+		return NULL;
+	}
+
+	strfilter = ast_strdupa(constfilter);
 
 	for (token = strtok_r(strfilter, ",", &saveptr); token; token = strtok_r(NULL, ",", &saveptr)) {
-		filter = data_filter_add_node(filter, token);
+		filter = data_filter_add_nodes(filter, token);
 	}
 
 	return filter;
 }
-
-/*!
- * \internal
- * \brief Get the name of the last node in the specified path.
- * \param[in] constpath A path of the form rootnode/first_child/second_child/last_leaf
- * \retval NULL On error.
- * \retval non-NULL The name of the last node in the path.
- */
-static inline char *data_filter_get_last_leaf(const char *constpath)
-{
-	char *path = ast_strdupa(constpath), *leaf = NULL, *token, *saveptr;
-
-	for (token = strtok_r(path, "/", &saveptr); token; token = strtok_r(NULL, "/", &saveptr)) {
-		leaf = token;
-	}
-
-	return leaf;
-}
-
-#if 0
-/*!
- * \internal
- * \XXX-document this.
- */
-static int data_filter_add_result_child(struct data_filter_list *flist,
-	const char *pathroot,
-	struct ast_data *node,
-	struct ast_data *generated)
-{
-	int found = 0;
-	struct data_filter_node *fnode, *fnode2;
-
-	if (!flist) {
-		data_result_add_child(node, generated);
-		return 1;
-	}
-
-	AST_LIST_TRAVERSE(flist, fnode, next) {
-		if (!strcasecmp(fnode->name, pathroot)) {
-			AST_LIST_TRAVERSE(&(fnode->children), fnode2, next) {
-				if (!strcasecmp(fnode->name, generated->name)) {
-					data_result_add_child(node, generated);
-					found = 1;
-					break;
-				}
-			}
-
-			if (found) {
-				break;
-			}
-		}
-	}
-
-	return found;
-}
-#endif
 
 /*!
  * \internal
@@ -1580,10 +1543,9 @@
  * \retval NULL on error.
  * \retval non-NULL The generated result tree.
  */
-static struct ast_data *__data_result_generate_node(const struct ast_data_query *query,
+static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
 	const struct data_provider *root_provider,
 	const char *parent_node_name,
-	struct data_filter_list *flist,
 	const struct ast_data_search *search)
 {
 	struct ast_data *generated, *node;
@@ -1595,10 +1557,6 @@
 	if (!node) {
 		ast_log(LOG_ERROR, "Unable to allocate '%s' node\n", parent_node_name);
 		return NULL;
-	}
-
-	if (!flist && query->filter) {
-		flist = data_filter_generate(query->filter);
 	}
 
 	if (root_provider->module) {
@@ -1622,7 +1580,7 @@
 		if (search) {
 			search_child = data_search_find(search->children, provider->name);
 		}
-		generated = __data_result_generate_node(query, provider, provider->name, flist, search_child);
+		generated = data_result_generate_node(query, provider, provider->name, search_child);
 		if (search_child) {
 			ao2_ref(search_child, -1);
 		}
@@ -1635,30 +1593,7 @@
 		ao2_ref(provider, -1);
 	}
 
-	/* Clean up the filter if we had one */
-	if (flist) {
-		data_filter_list_destroy(flist);
-	}
-
 	return node;
-}
-
-/*!
- * \internal
- * \brief Generate all the tree from a specified provider.
- * \param[in] query The query executed.
- * \param[in] root_provider The provider specified in the path of the query.
- * \param[in] parent_node_name The root node name.
- * \param[in] search
- * \retval NULL on error.
- * \retval non-NULL The generated result tree.
- */
-static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
-	const struct data_provider *root_provider,
-	const char *parent_node_name,
-	const struct ast_data_search *search)
-{
-	return __data_result_generate_node(query, root_provider, parent_node_name, NULL, search);
 }
 
 /*!
@@ -1674,8 +1609,9 @@
 {
 	char *node_name, *tmp_path;
 	struct data_provider *provider_child, *tmp_provider_child;
-	struct ast_data *result;
+	struct ast_data *result, *result_filtered;
 	struct ast_data_search *search = NULL, *search_child = NULL;
+	struct data_filter_list *flist = NULL;
 
 	if (!search_path) {
 		/* generate all the trees?. */
@@ -1710,24 +1646,39 @@
 		return NULL;
 	}
 
+	/* generate the search tree. */
 	if (query->search) {
-		search = data_search_parse(query->search);
+		search = data_search_generate(query->search);
 		search_child = data_search_find(search->children, provider_child->name);
 	}
 
+	/* generate the filter tree. */
+	if (query->filter) {
+		flist = data_filter_generate(query->filter);
+	}
+
 	result = data_result_generate_node(query, provider_child, provider_child->name, search_child);
 
+	/* release the requested provider. */
 	ao2_ref(provider_child, -1);
 
+	/* release the generated search tree. */
 	if (search_child) {
 		ao2_ref(search_child, -1);
 	}
-
 	if (search) {
 		data_search_release(search);
 	}
 
-	return result;
+	result_filtered = result;
+
+	/* release the generated filter tree. */
+	if (flist) {
+		data_filter_list_destroy(flist);
+		ast_free(flist);
+	}
+
+	return result_filtered;
 }
 
 struct ast_data *ast_data_get(const struct ast_data_query *query)
@@ -2042,6 +1993,8 @@
 	struct ast_data *internal = tree;
 	char *path, *ptr = NULL;
 
+	/* tree is the node we want to use to iterate? or we are going
+	 * to iterate thow an internal node? */
 	if (elements) {
 		path = ast_strdupa(elements);
 
@@ -2064,6 +2017,7 @@
 
 	iterator->pattern = (ptr ? strrchr(elements, '/') + 1 : elements);
 
+	/* is the last node a regular expression?, compile it! */
 	if (!regcomp(&(iterator->regex_pattern), iterator->pattern,
 			REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
 		iterator->is_pattern = 1;
@@ -2076,10 +2030,12 @@
 
 void ast_data_iterator_end(struct ast_data_iterator *iterator)
 {
+	/* decrement the reference counter. */
 	if (iterator->last) {
 		ao2_ref(iterator->last, -1);
 	}
 
+	/* release the generated pattern. */
 	if (iterator->is_pattern) {
 		regfree(&(iterator->regex_pattern));
 	}




More information about the asterisk-commits mailing list