[asterisk-commits] eliel: branch group/appdocsxml r136951 - /team/group/appdocsxml/main/pbx.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Aug 9 10:37:31 CDT 2008


Author: eliel
Date: Sat Aug  9 10:37:30 2008
New Revision: 136951

URL: http://svn.digium.com/view/asterisk?view=rev&rev=136951
Log:
Added the needed functions to parse <variablelist> elements inside any other element.
Notice that we need more work on output formatting.

Modified:
    team/group/appdocsxml/main/pbx.c

Modified: team/group/appdocsxml/main/pbx.c
URL: http://svn.digium.com/view/asterisk/team/group/appdocsxml/main/pbx.c?view=diff&rev=136951&r1=136950&r2=136951
==============================================================================
--- team/group/appdocsxml/main/pbx.c (original)
+++ team/group/appdocsxml/main/pbx.c Sat Aug  9 10:37:30 2008
@@ -2840,31 +2840,165 @@
 	return NULL;
 }
 
-/* \brief Return the string within a node formatted with <para> elements. 
- * \param node Parent node where content resides.
- * \retval NULL on error
- * \retval Node content on success.
+/*! \brief Parse a <para> element.
+ *  \param node The <para> element pointer.
+ *  \param tabs Added this string before the content of the <para> element.
+ *  \param posttabs Added this string after the content of the <para> element.
+ *  \param buffer This must be NULL or an ast_malloc'ed buffer. It will be used
+ *         to store the result (if already has something it will be appended to the current
+ *         string).
+ *  \retval 1 on succes.
+ *  \retval 0 on error.
+ */
+static int ast_xml_doc_parse_para(ast_xml_node *node, const char *tabs, const char *posttabs, int *len, char **buffer)
+{
+	ast_xml_text *tmptext;
+
+	if (!node || !node->AST_XML_CHILD) {
+		return 0;
+	}
+
+	if (strcasecmp((char *)node->AST_XML_NAME, "para")) {
+		return 0;
+	}
+
+	/* Get the text inside the <para> element and append it to buffer. */
+	tmptext = ast_xml_get_text(node);
+	if (tmptext) {
+		*buffer = ast_realloc(*buffer, *len + strlen(tmptext) + strlen(tabs) + strlen(posttabs) + 1);
+		*len += sprintf(*buffer + *len, "%s%s%s", tabs, tmptext, posttabs);
+		ast_xml_free_text(tmptext);
+	}
+
+	return 1;
+}
+
+/*! \brief Parse a <variable> node inside a <variablelist> node. 
+ *  \param node The variable node to parse.
+ *  \param tabs A string to be appended at the begining of the output that will be stored
+ *         in buffer.
+ *  \param len The current length of the buffer (if output is appended it will be modified).
+ *  \param buffer This must be NULL or an ast_malloc'ed buffer. It will be used
+ *  	   to store the result (if already has something it will be appended to the current
+ *  	   string).
+ */
+static void ast_xml_doc_parse_variable(ast_xml_node *node, const char *tabs, int *len, char **buffer)
+{
+	ast_xml_node *tmp;
+	ast_xml_attr *valname;
+	ast_xml_text *tmptext;
+
+	if (!node || !node->AST_XML_CHILD) {
+		return;
+	}	
+
+	tmp = node->AST_XML_CHILD;
+	while (tmp) {
+		/* XXX: increment by one tab the tabs? */
+		if (ast_xml_doc_parse_para(tmp, tabs, "\n", len, buffer)) {
+			tmp = tmp->AST_XML_NEXT;
+			continue;
+		}
+
+		if (!strcasecmp((char *)tmp->AST_XML_NAME, "value")) {
+			/* We do this first just to know if there is any comment inside <value>
+			and know how to print the name of the value. */
+			tmptext = ast_xml_get_text(tmp);
+
+			/* Parse each <value name='valuename'>desciption</value> */
+			valname = ast_xml_get_attribute(tmp, "name");
+			if (valname) {
+				*buffer = ast_realloc(*buffer, *len + strlen(valname) + strlen(tabs) + 3);
+				*len += sprintf(*buffer + *len, "\n%s%s%c", tabs, valname, (tmptext ? ':' : ' '));
+				ast_xml_free_attr(valname);
+			}
+			/* Check inside this node for any explanation about its meaning. */
+			if (tmptext) {
+				*buffer = ast_realloc(*buffer, *len + strlen(tmptext) + strlen(tabs) + 3);
+				*len += sprintf(*buffer + *len, "%s\t%s\n", tabs, tmptext);
+				ast_xml_free_text(tmptext);
+			}
+		}
+		tmp = tmp->AST_XML_NEXT;
+	}
+	
+}
+
+/*! \brief Parse a <variablelist> node and put all the output inside 'buffer'.
+ *  \param node The variablelist node pointer.
+ *  \param tabs A string to be appended at the begining of the output that will be stored
+ *         in buffer.
+ *  \param buffer This must be NULL, or a ast_malloc'ed buffer. It will be used
+ *  	   to store the result (if already has something it will be appended to the current
+ *  	   string).
+ *  \param len The current length of the buffer (if output is appended it will be modified). 
+ *  \retval 1 If a <variablelist> element is parsed.
+ *  \retval 0 On error.
+ */
+static int ast_xml_doc_parse_variablelist(ast_xml_node *node, const char *tabs, int *len, char **buffer)
+{
+	ast_xml_node *tmp;
+	ast_xml_attr *varname;
+
+	if (!node || !node->AST_XML_CHILD) {
+		return 0;
+	}
+
+	if (strcasecmp((char *)node->AST_XML_NAME, "variablelist")) {
+		return 0;	
+	}
+
+	tmp = node->AST_XML_CHILD;
+	while (tmp) {
+		/* We can have a <para> element inside the variable list ?? */
+		if ((ast_xml_doc_parse_para(tmp, "", "", len, buffer))) {
+			tmp = tmp->AST_XML_NEXT;
+			continue;
+		}
+
+		if (!strcasecmp((char *)tmp->AST_XML_NAME, "variable")) {
+			/* Store the variable name in buffer. */
+			varname = ast_xml_get_attribute(tmp, "name");
+			if (varname) {
+				*buffer = ast_realloc(*buffer, *len + strlen(varname) + strlen(tabs) + 4);
+				*len += sprintf(*buffer + *len, "%s\n%s: ", tabs, varname);
+				ast_xml_free_attr(varname);
+			}
+			/* Parse the <variable> possible values. */
+			ast_xml_doc_parse_variable(tmp, "\t\t", len, buffer);
+		}
+		tmp = tmp->AST_XML_NEXT;
+	}
+	return 1;
+}
+
+/*! \brief Return the string within a node formatted with 
+ * 	   <para> and <variablelist> elements. 
+ *  \param node Parent node where content resides.
+ *  \retval NULL on error
+ *  \retval Node content on success.
  */
 static char *ast_xml_doc_get_formatted(ast_xml_node *node)
 {
 	ast_xml_node *tmp;
-	char *ret = NULL, *tmptext;
+	char *ret = NULL;
 	int len = 0;
 
-	if (!node) {
+	if (!node || !node->AST_XML_CHILD) {
 		return NULL;
 	}
 
 	tmp = node->AST_XML_CHILD;
 	while (tmp) {
-		if (!strcasecmp((char *)tmp->AST_XML_NAME, "para")) {
-			tmptext = ast_xml_get_text(tmp);
-			if (tmptext) {
-				ret = ast_realloc(ret, len + strlen(tmptext) + 2);
-				len += sprintf(ret + len, "%s\n", tmptext);
-				ast_xml_free_text(tmptext);
-			}
-		}
+		/* if found, parse a <para> element. */
+		if (ast_xml_doc_parse_para(tmp, "", "\n", &len, &ret)) {
+			tmp = tmp->AST_XML_NEXT;
+			continue;
+		}
+		
+		/* if found, parse a <variablelist> element. */
+		ast_xml_doc_parse_variablelist(tmp, "\n", &len, &ret);
+		
 		tmp = tmp->AST_XML_NEXT;
 	}
 	




More information about the asterisk-commits mailing list