[asterisk-commits] bbryant: branch group/data_api_gsoc2009 r212025 - in /team/group/data_api_gso...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Aug 13 05:48:09 CDT 2009


Author: bbryant
Date: Thu Aug 13 05:48:03 2009
New Revision: 212025

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=212025
Log:
This adds the ability for the filters to have "wildcard" asterisks for globbing.

i.e. filters can now contain expressions like "a*int", "*a", "a*foo*bar*", "*int", etc.

Modified:
    team/group/data_api_gsoc2009/main/data.c
    team/group/data_api_gsoc2009/tests/test_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=212025&r1=212024&r2=212025
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Thu Aug 13 05:48:03 2009
@@ -122,10 +122,16 @@
 	char name[0];
 };
 
+struct data_filter;
+
 /*! \brief The filter node. */
 struct data_filter {
 	/*! \brief node childrens. */
 	struct ao2_container *children;
+	/*! \brief glob list */
+	AST_LIST_HEAD_NOLOCK(glob_list_t, data_filter) glob_list;
+	/*! \brief glob list entry */
+	AST_LIST_ENTRY(data_filter) list;
 	/*! \brief node name. */
 	char name[0];
 };
@@ -1377,7 +1383,11 @@
  */
 static void data_filter_destructor(void *obj)
 {
-	struct data_filter *filter = obj;
+	struct data_filter *filter = obj, *globres;
+
+	AST_LIST_TRAVERSE(&(filter->glob_list), globres, list) {
+		ao2_ref(globres, -1);
+	}
 
 	ao2_ref(filter->children, -1);
 }
@@ -1390,7 +1400,8 @@
  */
 static struct data_filter *data_filter_alloc(const char *name)
 {
-	struct data_filter *res;
+	char *globname, *token;
+	struct data_filter *res, *globfilter;
 	size_t name_len = strlen(name) + 1;
 
 	res = ao2_alloc(sizeof(*res) + name_len, data_filter_destructor);
@@ -1407,6 +1418,15 @@
 	}
 
 	strcpy(res->name, name);
+
+	if (strchr(res->name, '*')) {
+		globname = ast_strdupa(res->name);
+		
+		while ((token = strsep(&globname, "*"))) {
+			globfilter = data_filter_alloc(token);
+			AST_LIST_INSERT_TAIL(&(res->glob_list), globfilter, list);
+		}
+	}
 
 	return res;
 }
@@ -1433,7 +1453,10 @@
 static struct data_filter *data_filter_find(struct ao2_container *parent,
 	const char *name)
 {
-	struct data_filter *find_node, *found;
+	int i, olend, orend, globfound;
+	size_t name_len = strlen(name), glob_len;
+	struct ao2_iterator iter;
+	struct data_filter *find_node, *found, *globres;
 
 	find_node = data_filter_alloc(name);
 	if (!find_node) {
@@ -1445,7 +1468,58 @@
 	/* free the created node used for searching. */
 	ao2_ref(find_node, -1);
 
-	return found;
+	if (found) {
+		return found;
+	}
+
+	iter = ao2_iterator_init(parent, 0);
+	while ((found = ao2_iterator_next(&iter))) {
+		if (!AST_LIST_EMPTY(&(found->glob_list))) {
+			i = 0;
+			globfound = 1;
+
+			olend = ast_strlen_zero(AST_LIST_FIRST(&(found->glob_list))->name);
+			orend = ast_strlen_zero(AST_LIST_LAST(&(found->glob_list))->name);
+
+			AST_LIST_TRAVERSE(&(found->glob_list), globres, list) {
+				if (!*globres->name) {
+					continue;
+				}
+
+				glob_len = strlen(globres->name);
+
+				if (!i && !olend) {
+					if (strncasecmp(name, globres->name, glob_len)) {
+						globfound = 0;
+						break;
+					}
+
+					i += glob_len;
+					continue;
+				}
+
+				for (globfound = 0; name_len - i >= glob_len; ++i) {
+					if (!strncasecmp(name + i, globres->name, glob_len)) {
+						globfound = 1;
+						i += glob_len;
+						break;
+					}
+				}
+
+				if (!globfound) {
+					break;
+				}
+			}
+
+			if (globfound && (i == name_len || orend)) {
+				return found;
+			}
+		}
+
+		ao2_ref(found, -1);
+	}
+
+	return NULL;
 }
 
 /*!

Modified: team/group/data_api_gsoc2009/tests/test_data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/tests/test_data.c?view=diff&rev=212025&r1=212024&r2=212025
==============================================================================
--- team/group/data_api_gsoc2009/tests/test_data.c (original)
+++ team/group/data_api_gsoc2009/tests/test_data.c Thu Aug 13 05:48:03 2009
@@ -196,7 +196,8 @@
 	struct ast_data_query query = {
 		.version = AST_DATA_QUERY_VERSION,
 		.path = "test/node1/node11/node111",
-		.search = "node111/test_structure/a_int=10"
+		.search = "node111/test_structure/a_int=10",
+		.filter = "node111/test_structure/a*int"
 	};
 
 	switch (cmd) {
@@ -210,7 +211,9 @@
 	}
 
 	/* get the tree and check for errors. */
-	ast_cli(a->fd, "path = %s\nsearch = %s\n", query.path, query.search);
+	ast_cli(a->fd, "path = %s\n", query.path);
+	ast_cli(a->fd, "search = %s\n", query.search);
+	ast_cli(a->fd, "filter = %s\n", query.filter);
 	ast_cli(a->fd, "Getting tree... ");
 
 	res = ast_data_get(&query);




More information about the asterisk-commits mailing list