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

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Jul 6 14:40:20 CDT 2009


Author: eliel
Date: Mon Jul  6 14:40:14 2009
New Revision: 204981

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=204981
Log:
Start implementing the search mechanism.


Modified:
    team/group/data_api_gsoc2009/include/asterisk/data.h
    team/group/data_api_gsoc2009/main/data.c
    team/group/data_api_gsoc2009/tests/test_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=204981&r1=204980&r2=204981
==============================================================================
--- team/group/data_api_gsoc2009/include/asterisk/data.h (original)
+++ team/group/data_api_gsoc2009/include/asterisk/data.h Mon Jul  6 14:40:14 2009
@@ -96,6 +96,9 @@
 
 /*! \brief opaque definition of an ast_data_iterator handler. */
 struct ast_data_iterator;
+
+/*! \brief opaque definition of an ast_data_search structure. */
+struct ast_data_search;
 
 typedef struct ast_data *(*ast_data_get_cb)(void);
 typedef int *(*ast_data_put_cb)(void);
@@ -131,14 +134,11 @@
 	AST_DECLARE_STRING_FIELDS(
 		/*! \brief Path to the node to retrieve. */
 		AST_STRING_FIELD(path);
-		/*! \brief Search string, return the nodes that match this conditions.
-		 *         Setting it to NULL will match every node in the specified
-		 *         path. */
-		AST_STRING_FIELD(search);
 		/*! \brief Filter string, return the internal nodes specified here.
 		 *         Setting it to NULL will return every internal node. */
 		AST_STRING_FIELD(filter);
 	);
+	struct ast_data_search *search;
 };
 
 /*!
@@ -186,6 +186,18 @@
 #define ast_data_unregister(path) __ast_data_unregister(path, __FILE__)
 
 /*!
+ * \brief Generate a search structure.
+ * XXX
+ */
+struct ast_data_search *ast_data_search_create(const char *search_format, ...);
+
+/*!
+ * \brief Release a search structure.
+ * XXX
+ */
+void ast_data_search_release(struct ast_data_search *search_query);
+
+/*!
  * \brief Retrieve a subtree from the asterisk data API.
  * \param[in] query The query structure specifying what nodes to retrieve.
  * \retval NULL on error.

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=204981&r1=204980&r2=204981
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Mon Jul  6 14:40:14 2009
@@ -77,6 +77,44 @@
 	struct ao2_container *children;
 };
 
+/*! \brief Type of comparisons allow in the search string. */
+enum data_search_comparison {
+	DATA_CMP_UNKNOWN,
+	DATA_CMP_EQ,	/* =  */
+	DATA_CMP_NEQ,	/* != */
+	DATA_CMP_GT,	/* >  */
+	DATA_CMP_GE,	/* >= */
+	DATA_CMP_LT,	/* <  */
+	DATA_CMP_LE	/* <= */
+};
+
+/*! \brief The list of nodes with their search requirement. */
+struct ast_data_search {
+	/*! \brief The data type we are trying to
+	 * compare. */
+	enum ast_data_type value_type;
+
+	/* The value we are trying to compare. */
+	union {
+		int32_t sint;
+		uint32_t uint;
+		double dbl;
+		unsigned int boolean:1;
+		char *str;
+		struct in_addr ipaddr;
+		void *ptr;
+	} value;
+
+	/*! \brief The type of comparison. */
+	enum data_search_comparison cmp_type;
+
+	/*! \brief reference another node. */
+	struct ao2_container *children;
+
+	/*! \brief The name of the node we are trying to compare. */
+	char name[0];
+};
+
 /*! \brief A data container node pointing to the registered handler. */
 struct data_provider {
 	/*! \brief node content handler. */
@@ -519,6 +557,125 @@
 	}
 
 	return ret;
+}
+
+/*!
+ * \internal
+ * \brief Is a char used to specify a comparison?
+ * \param[in] a Character to evaluate.
+ * \retval 1 It is a char used to specify a comparison.
+ * \retval 0 It is NOT a char used to specify a comparison.
+ */
+static int data_search_comparison_char(char a)
+{
+	switch (a) {
+	case '!':
+	case '=':
+	case '<':
+	case '>':
+		return 1;
+	}
+
+	return 0;
+}
+
+/*!
+ * \internal
+ * \brief Get the type of comparison.
+ */
+#if 0
+static enum data_search_comparison data_search_comparison_type(const char *comparison_string)
+{
+	if (!strcmp(comparison_string, "=")) {
+		return DATA_CMP_EQ;
+	} else if (!strcmp(comparison_string, "!=")) {
+		return DATA_CMP_NEQ;
+	} else if (!strcmp(comparison_string, "<")) {
+		return DATA_CMP_LT;
+	} else if (!strcmp(comparison_string, ">")) {
+		return DATA_CMP_GT;
+	} else if (!strcmp(comparison_string, "<=")) {
+		return DATA_CMP_LE;
+	} else if (!strcmp(comparison_string, ">=")) {
+		return DATA_CMP_GE;
+	}
+
+	return DATA_CMP_UNKNOWN;
+}
+#endif
+
+struct ast_data_search *ast_data_search_create(const char *search_format, ...)
+{
+	va_list values;
+	struct ast_str *name, *format, *comparison;
+	char *elements, *search_format_dup;
+	int i;
+
+	if (!search_format) {
+		ast_log(LOG_ERROR, "You must pass a valid search string.\n");
+		return NULL;
+	}
+
+	name = ast_str_alloca(64);
+	format = ast_str_alloca(64);
+	comparison = ast_str_alloca(64);
+
+	va_start(values, search_format);
+
+	search_format_dup = ast_strdupa(search_format);
+
+	elements = strtok(search_format_dup, ",");
+
+	while (elements) {
+		/* Parse the name */
+		ast_str_reset(name);
+		for (i = 0; !data_search_comparison_char(elements[i]) &&
+			elements[i]; i++) {
+			ast_str_append(&name, 0, "%c", elements[i]);
+		}
+
+		/* check if the syntax is ok. */
+		if (!data_search_comparison_char(elements[i])) {
+			/* if this is the end of the string, then this is
+			 * an error! */
+			ast_log(LOG_ERROR, "Invalid search string!\n");
+			break;
+		}
+
+		/* parse the comparison string. */
+		ast_str_reset(comparison);
+		for (; elements[i] != '%' && elements[i]; i++) {
+			ast_str_append(&comparison, 0, "%c", elements[i]);
+		}
+
+		/* check if the syntax is ok. */
+		if (elements[i] != '%') {
+			ast_log(LOG_ERROR, "Invalid search string!\n");
+			break;
+		}
+
+		/* parse the format string. */
+		ast_str_reset(format);
+		for (; elements[i]; i++) {
+			ast_str_append(&format, 0, "%c", elements[i]);
+		}
+
+		ast_log(LOG_ERROR, "NAME = %s\n", ast_str_buffer(name));
+		ast_log(LOG_ERROR, "FORMAT = %s\n", ast_str_buffer(format));
+		ast_log(LOG_ERROR, "CMP = %s\n", ast_str_buffer(comparison));
+
+		/* add this node to the tree. */
+
+		elements = strtok(NULL, ",");
+	}
+	va_end(values);
+
+	return NULL;
+}
+
+void ast_data_search_release(struct ast_data_search *search)
+{
+
 }
 
 /*!

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=204981&r1=204980&r2=204981
==============================================================================
--- team/group/data_api_gsoc2009/tests/test_data.c (original)
+++ team/group/data_api_gsoc2009/tests/test_data.c Mon Jul  6 14:40:14 2009
@@ -93,6 +93,7 @@
 	struct ast_data_query query_null = {
 		.version = AST_DATA_QUERY_VERSION,
 		.path = "test/null",
+		.search = ast_data_search_create("test/prueba=%d,test/otraprueba=%d", cmd, cmd)
 	};
 	struct ast_data_query query_full = {
 		.version = AST_DATA_QUERY_VERSION,




More information about the svn-commits mailing list