[asterisk-commits] eliel: branch group/appdocsxml r145173 - /team/group/appdocsxml/main/pbx.c
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Sep 29 08:07:38 CDT 2008
Author: eliel
Date: Mon Sep 29 08:07:38 2008
New Revision: 145173
URL: http://svn.digium.com/view/asterisk?view=rev&rev=145173
Log:
Masive move to ast_str.
Standarize the xmldoc_ parameters order.
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=145173&r1=145172&r2=145173
==============================================================================
--- team/group/appdocsxml/main/pbx.c (original)
+++ team/group/appdocsxml/main/pbx.c Mon Sep 29 08:07:38 2008
@@ -3118,19 +3118,17 @@
/*! \internal
* \brief Cleanup spaces and tabs after a \n
* \param text String to be cleaned up.
- * \retval NULL on error.
- * \retval A malloc'ed string with the output (a clean string).
+ * \param output buffer (not already allocated).
*/
-static char *xmldoc_string_cleanup(const char *text)
-{
- int retlen=0;
- char *rettmp, *rettext, *tmp;
-
- /* Alloc a buffer of at least the same size as 'text' */
- if (!text || !(rettmp = ast_calloc(1, strlen(text) + 1))) {
- return NULL;
- }
-
+static void xmldoc_string_cleanup(const char *text, struct ast_str **output)
+{
+ char *tmp;
+
+ if (!text) {
+ return;
+ }
+
+ *output = ast_str_create(1);
tmp = (char *)text;
while (*tmp) {
if (*tmp == '\n' || *tmp == '\r') {
@@ -3138,18 +3136,13 @@
while (*tmp == '\t' || *tmp == '\r' || *tmp == '\n') {
tmp++;
}
- rettmp[retlen++] = ' ';
+ ast_str_append(output, 0, "%c", ' ');
continue;
} else {
- rettmp[retlen++] = *tmp;
+ ast_str_append(output, 0, "%c", *tmp);
}
tmp++;
}
-
- rettext = ast_strdup(rettmp);
- ast_free(rettmp);
-
- return rettext;
}
/*! \internal
@@ -3483,20 +3476,19 @@
* \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 len buffer length (will be modified if data is appended in buffer).
- * \param buffer This must be NULL or an ast_malloc'ed buffer. It will be used
+ * \param buffer This must be an already allocated ast_str. It will be used
* to store the result (if already has something it will be appended to the current
* string).
* \retval 1 If 'node' is a named 'para'.
* \retval 2 If data is appended in buffer.
* \retval 0 on error.
*/
-static int xmldoc_parse_para(ast_xml_node *node, const char *tabs, const char *posttabs, int *len, char **buffer)
+static int xmldoc_parse_para(ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer)
{
ast_xml_text *tmptext;
ast_xml_node *tmp;
- char *cleantext;
int ret = 0;
+ struct ast_str *tmpstr;
if (!node || !node->AST_XML_CHILD) {
return ret;
@@ -3506,8 +3498,7 @@
return ret;
}
- *buffer = ast_realloc(*buffer, *len + xmldoc_postbrlen(tabs) + 1);
- *len += sprintf(*buffer + *len, "%s", tabs);
+ ast_str_append(buffer, 0, "%s", tabs);
ret = 1;
@@ -3517,28 +3508,22 @@
tmptext = ast_xml_get_text(tmp);
if (tmptext) {
/* Strip \n etc. */
- cleantext = xmldoc_string_cleanup(tmptext);
+ xmldoc_string_cleanup(tmptext, &tmpstr);
ast_xml_free_text(tmptext);
- if (cleantext) {
+ if (tmpstr) {
if (strcasecmp((char *)tmp->AST_XML_NAME, "text")) {
- /* + 5 for= "<></>" */
- *buffer = ast_realloc(*buffer, *len + strlen(cleantext) + (strlen((char *)tmp->AST_XML_NAME) * 2) + 5 + 1);
- /* The last space is not returned by getContent? */
- *len += sprintf(*buffer + *len, "<%s>%s</%s>", tmp->AST_XML_NAME, cleantext, tmp->AST_XML_NAME);
+ ast_str_append(buffer, 0, "<%s>%s</%s>", tmp->AST_XML_NAME, tmpstr->str, tmp->AST_XML_NAME);
} else {
- *buffer = ast_realloc(*buffer, *len + strlen(cleantext) + 1);
- ast_copy_string(*buffer + *len, cleantext, strlen(cleantext) + 1);
- *len += strlen(cleantext);
+ ast_str_append(buffer, 0, "%s", tmpstr->str);
}
- ast_free(cleantext);
+ ast_free(tmpstr);
ret = 2;
}
}
tmp = tmp->AST_XML_NEXT;
}
- *buffer = ast_realloc(*buffer, *len + xmldoc_postbrlen(posttabs) + 1);
- *len += sprintf(*buffer + *len, "%s", posttabs);
+ ast_str_append(buffer, 0, "%s", posttabs);
return ret;
}
@@ -3548,12 +3533,11 @@
* \param fixnode special tag node pointer.
* \param tabs put tabs before printing the node content.
* \param posttabs put posttabs after printing node content.
- * \param len current buffer len (will be changed if data is appended to this buffer).
* \param buffer Output buffer, the special tags will be appended here.
* \retval 0 if no special element is parsed.
* \retval 1 if a special element is parsed.
*/
-static int xmldoc_parse_specialtags(ast_xml_node *fixnode, const char *tabs, const char *posttabs, int *len, char **buffer)
+static int xmldoc_parse_specialtags(ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer)
{
ast_xml_node *node = fixnode;
int ret = 0, i, count = 0;
@@ -3569,19 +3553,18 @@
/* concat data */
if (strlen(special_tags[i].init) > 0) {
- *buffer = ast_realloc(*buffer, *len + strlen(special_tags[i].init) + xmldoc_postbrlen(tabs) + 1);
- *len += sprintf(*buffer + *len, "%s%s", tabs, special_tags[i].init);
+ ast_str_append(buffer, 0, "%s%s", tabs, special_tags[i].init);
}
/* parse <para> elements inside special tags. */
node = node->AST_XML_CHILD;
while (node) {
if (count > 0) {
- if (xmldoc_parse_para(node, tabs, posttabs, len, buffer) == 2) {
+ if (xmldoc_parse_para(node, tabs, posttabs, buffer) == 2) {
ret = 2;
}
} else {
- if (xmldoc_parse_para(node, "", posttabs, len, buffer) == 2) {
+ if (xmldoc_parse_para(node, "", posttabs, buffer) == 2) {
ret = 2;
}
}
@@ -3589,8 +3572,7 @@
}
if (strlen(special_tags[i].end) > 0) {
- *buffer = ast_realloc(*buffer, *len + strlen(special_tags[i].init) + xmldoc_postbrlen(posttabs) + 1);
- *len += sprintf(*buffer + *len, "%s%s", special_tags[i].end, posttabs);
+ ast_str_append(buffer, 0, "%s%s", special_tags[i].end, posttabs);
}
break;
@@ -3602,14 +3584,13 @@
/*! \internal
* \brief Parse an <argument> element from the xml documentation.
+ * \param fixnode Pointer to the 'argument' xml node.
* \param tabs What to be printed before the argument name.
* \param buffer Output buffer to put values found inside the <argument> element.
- * \param len Output buffer length (will be modified if data is appended to buffer).
- * \param fixnode Pointer to the 'argument' xml node.
* \retval 1 If there is content inside the argument.
* \retval 0 If the argument element is not parsed, or there is no content inside it.
*/
-static int xmldoc_parse_argument(const char *tabs, char **buffer, int *len, ast_xml_node *fixnode)
+static int xmldoc_parse_argument(ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
{
ast_xml_node *node = fixnode;
ast_xml_attr *argname;
@@ -3622,25 +3603,24 @@
/* Print the argument names */
argname = ast_xml_get_attribute(node, "name");
if (argname) {
- *buffer = ast_realloc(*buffer, *len + strlen(argname) + strlen(tabs) + 1);
- *len += sprintf(*buffer + *len, "%s%s", tabs, argname);
+ ast_str_append(buffer, 0, "%s%s", tabs, argname);
ast_xml_free_attr(argname);
node = node->AST_XML_CHILD;
while (node) {
if (count > 0) {
- if (xmldoc_parse_para(node, tabs, "\n", len, buffer) == 2) {
+ if (xmldoc_parse_para(node, tabs, "\n", buffer) == 2) {
count++;
ret = 1;
- } else if (xmldoc_parse_specialtags(node, tabs, "\n", len, buffer) == 2) {
+ } else if (xmldoc_parse_specialtags(node, tabs, "\n", buffer) == 2) {
count++;
ret = 1;
}
} else {
- if (xmldoc_parse_para(node, " - ", "\n", len, buffer) == 2) {
+ if (xmldoc_parse_para(node, " - ", "\n", buffer) == 2) {
count++;
ret = 1;
- } else if (xmldoc_parse_specialtags(node, " - ", "\n", len, buffer) == 2) {
+ } else if (xmldoc_parse_specialtags(node, " - ", "\n", buffer) == 2) {
count++;
ret = 1;
}
@@ -3657,19 +3637,18 @@
* \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
+ * \param buffer This must be an already created ast_str. It will be used
* to store the result (if already has something it will be appended to the current
* string).
* \retval 0 if no data is appended.
* \retval 1 if data is appended.
*/
-static int xmldoc_parse_variable(ast_xml_node *node, const char *tabs, int *len, char **buffer)
+static int xmldoc_parse_variable(ast_xml_node *node, const char *tabs, struct ast_str **buffer)
{
ast_xml_node *tmp;
ast_xml_attr *valname;
ast_xml_text *tmptext;
- char *clean;
+ struct ast_str *cleanstr = NULL;
int ret = 0;
if (!node || !node->AST_XML_CHILD) {
@@ -3678,10 +3657,10 @@
tmp = node->AST_XML_CHILD;
while (tmp) {
- if (xmldoc_parse_para(tmp, (ret ? tabs : ""), "\n", len, buffer)) {
+ if (xmldoc_parse_para(tmp, (ret ? tabs : ""), "\n", buffer)) {
tmp = tmp->AST_XML_NEXT;
continue;
- } else if (xmldoc_parse_specialtags(tmp, (ret ? tabs : ""), "\n", len, buffer)) {
+ } else if (xmldoc_parse_specialtags(tmp, (ret ? tabs : ""), "\n", buffer)) {
tmp = tmp->AST_XML_NEXT;
continue;
}
@@ -3695,23 +3674,20 @@
valname = ast_xml_get_attribute(tmp, "name");
if (valname) {
if (!ret) {
- *buffer = ast_realloc(*buffer, *len + 2);
- *len += sprintf(*buffer + *len, "\n");
+ ast_str_append(buffer, 0, "\n");
}
ret = 1;
- *buffer = ast_realloc(*buffer, *len + strlen(valname) + strlen(tabs) + 3);
- *len += sprintf(*buffer + *len, "%s%s%c", tabs, valname, (tmptext ? ':' : '\n'));
+ ast_str_append(buffer, 0, "%s%s%c", tabs, valname, (tmptext ? ':' : '\n'));
ast_xml_free_attr(valname);
}
/* Check inside this node for any explanation about its meaning. */
if (tmptext) {
/* Cleanup text. */
- clean = xmldoc_string_cleanup(tmptext);
+ xmldoc_string_cleanup(tmptext, &cleanstr);
ast_xml_free_text(tmptext);
- if (clean) {
- *buffer = ast_realloc(*buffer, *len + strlen(clean) + 3);
- *len += sprintf(*buffer + *len, " %s\n", clean);
- ast_free(clean);
+ if (cleanstr) {
+ ast_str_append(buffer, 0, " %s\n", cleanstr->str);
+ ast_free(cleanstr);
}
}
}
@@ -3726,14 +3702,13 @@
* \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 len The current length of the buffer (if output is appended it will be modified).
- * \param buffer This must be NULL, or a ast_malloc'ed buffer. It will be used
+ * \param buffer This must be an already created ast_str. It will be used
* to store the result (if already has something it will be appended to the current
* string).
* \retval 1 If a <variablelist> element is parsed.
* \retval 0 On error.
*/
-static int xmldoc_parse_variablelist(ast_xml_node *node, const char *tabs, int *len, char **buffer)
+static int xmldoc_parse_variablelist(ast_xml_node *node, const char *tabs, struct ast_str **buffer)
{
ast_xml_node *tmp;
ast_xml_attr *varname;
@@ -3753,11 +3728,11 @@
tmp = node->AST_XML_CHILD;
while (tmp) {
/* We can have a <para> element inside the variable list */
- if ((xmldoc_parse_para(tmp, (ret ? tabs : ""), "\n", len, buffer))) {
+ if ((xmldoc_parse_para(tmp, (ret ? tabs : ""), "\n", buffer))) {
ret = 1;
tmp = tmp->AST_XML_NEXT;
continue;
- } else if ((xmldoc_parse_specialtags(tmp, (ret ? tabs : ""), "\n", len, buffer))) {
+ } else if ((xmldoc_parse_specialtags(tmp, (ret ? tabs : ""), "\n", buffer))) {
ret = 1;
tmp = tmp->AST_XML_NEXT;
}
@@ -3766,14 +3741,11 @@
/* 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);
- if (buffer) {
- *len += sprintf(*buffer + *len, "%s%s: ", tabs, varname);
- ast_xml_free_attr(varname);
- /* Parse the <variable> possible values. */
- xmldoc_parse_variable(tmp, vartabs, len, buffer);
- ret = 1;
- }
+ ast_str_append(buffer, 0, "%s%s: ", tabs, varname);
+ ast_xml_free_attr(varname);
+ /* Parse the <variable> possible values. */
+ xmldoc_parse_variable(tmp, vartabs, buffer);
+ ret = 1;
}
}
tmp = tmp->AST_XML_NEXT;
@@ -3786,13 +3758,12 @@
/*! \internal
* \brief Parse an <option> node.
+ * \param fixnode An ast_xml pointer to the <option> node.
* \param buffer The output buffer.
- * \param len The buffer length, (it will be changed if data is appended to buffer).
- * \param fixnode An ast_xml pointer to the <option> node.
* \retval 0 if no option node is parsed.
* \retval 1 if an option node is parsed.
*/
-static int xmldoc_parse_option(char **buffer, int *len, ast_xml_node *fixnode)
+static int xmldoc_parse_option(ast_xml_node *fixnode, struct ast_str **buffer)
{
ast_xml_node *node = fixnode;
int ret = 0;
@@ -3806,25 +3777,23 @@
if (!strcasecmp((char *)node->AST_XML_NAME, "argument")) {
if (!ret && node->AST_XML_CHILD) {
/* print \n */
- *buffer = ast_realloc(*buffer, *len + 2);
- *len += sprintf(*buffer + *len, "\n");
- }
- if (xmldoc_parse_argument(" ", buffer, len, node)) {
+ ast_str_append(buffer, 0, "\n");
+ }
+ if (xmldoc_parse_argument(node, " ", buffer)) {
ret = 1;
}
node = node->AST_XML_NEXT;
continue;
}
- if (xmldoc_parse_para(node, (ret ? " " : ""), "\n", len, buffer)) {
+ if (xmldoc_parse_para(node, (ret ? " " : ""), "\n", buffer)) {
ret = 1;
- } else if (xmldoc_parse_specialtags(node, (ret ? " " : ""), "\n", len, buffer)) {
+ } else if (xmldoc_parse_specialtags(node, (ret ? " " : ""), "\n", buffer)) {
ret = 1;
}
- if (xmldoc_parse_variablelist(node, " ", len, buffer)) {
- *buffer = ast_realloc(*buffer, *len + 2);
- *len += sprintf(*buffer + *len, "\n");
+ if (xmldoc_parse_variablelist(node, " ", buffer)) {
+ ast_str_append(buffer, 0, "\n");
}
node = node->AST_XML_NEXT;
@@ -3835,12 +3804,10 @@
/*! \internal
* \brief Parse an <optionlist> element from the xml documentation.
+ * \param fixnode Pointer to the optionlist xml node.
* \param buffer Output buffer to put what is inside the optionlist tag.
- * \param len Length of the passed 'buffer', it will be incremented if something
- * is appended into buffer.
- * \param fixnode Pointer to the optionlist xml node.
*/
-static void xmldoc_parse_optionlist(char **buffer, int *len, ast_xml_node *fixnode)
+static void xmldoc_parse_optionlist(ast_xml_node *fixnode, struct ast_str **buffer)
{
ast_xml_node *node = fixnode;
ast_xml_attr *optname;
@@ -3871,12 +3838,10 @@
continue;
}
- *buffer = ast_realloc(*buffer, *len + strlen(optionsyntax) + 7);
- *len += sprintf(*buffer + *len, " %s: ", optionsyntax);
-
- if (!xmldoc_parse_option(buffer, len, node)) {
- *buffer = ast_realloc(*buffer, *len + 2);
- *len += sprintf(*buffer + *len, "\n");
+ ast_str_append(buffer, 0, " %s: ", optionsyntax);
+
+ if (!xmldoc_parse_option(node, buffer)) {
+ ast_str_append(buffer, 0, "\n");
}
node = node->AST_XML_NEXT;
@@ -3887,11 +3852,10 @@
/*! \internal
* \brief Parse a 'parameter' tag inside a syntax element.
+ * \param fixnode A pointer to the 'parameter' xml node.
* \param buffer String buffer to put values found inside the parameter element.
- * \param len Buffer length (will be modified if data is appended to buffer).
- * \param fixnode A pointer to the 'parameter' xml node.
*/
-static void xmldoc_parse_parameter(char **buffer, int *len, ast_xml_node *fixnode)
+static void xmldoc_parse_parameter(ast_xml_node *fixnode, struct ast_str **buffer)
{
ast_xml_attr *paramname;
ast_xml_node *node = fixnode;
@@ -3902,11 +3866,7 @@
paramname = ast_xml_get_attribute(node, "name");
if (paramname) {
- *buffer = ast_realloc(*buffer, *len + strlen(paramname) + 2);
- if (!*buffer) {
- return;
- }
- *len += sprintf (*buffer + *len, "%s\n", paramname);
+ ast_str_append(buffer, 0, "%s\n", paramname);
ast_xml_free_attr(paramname);
} else {
return;
@@ -3915,13 +3875,13 @@
node = node->AST_XML_CHILD;
while (node) {
if (!strcasecmp((char *)node->AST_XML_NAME, "optionlist")) {
- xmldoc_parse_optionlist(buffer, len, node);
+ xmldoc_parse_optionlist(node, buffer);
} else if (!strcasecmp((char *)node->AST_XML_NAME, "argument")) {
- xmldoc_parse_argument(" ", buffer, len, node);
- } else if ((xmldoc_parse_para(node, " ", "\n", len, buffer))) {
+ xmldoc_parse_argument(node, " ", buffer);
+ } else if ((xmldoc_parse_para(node, " ", "\n", buffer))) {
node = node->AST_XML_NEXT;
continue;
- } else if ((xmldoc_parse_specialtags(node, " ", "\n", len, buffer))) {
+ } else if ((xmldoc_parse_specialtags(node, " ", "\n", buffer))) {
node = node->AST_XML_NEXT;
continue;
}
@@ -3943,8 +3903,7 @@
static char *xmldoc_build_arguments(const char *type, const char *name)
{
ast_xml_node *node;
- char *ret = NULL;
- int len = 0;
+ struct ast_str *ret = ast_str_create(1);
if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
return NULL;
@@ -3972,11 +3931,11 @@
node = node->AST_XML_CHILD;
while (node) {
- xmldoc_parse_parameter(&ret, &len, node);
+ xmldoc_parse_parameter(node, &ret);
node = node->AST_XML_NEXT;
}
- return ret;
+ return ret->str;
}
/*! \internal
@@ -3988,33 +3947,33 @@
* \retval NULL on error
* \retval Node content on success.
*/
-static char *xmldoc_get_formatted(ast_xml_node *node, int raw_output, int raw_wrap)
+static struct ast_str *xmldoc_get_formatted(ast_xml_node *node, int raw_output, int raw_wrap)
{
ast_xml_node *tmp;
- char *ret = NULL, *notcleanret;
- int len = 0;
+ struct ast_str *ret = ast_str_create(1);
+ char *notcleanret;
if (!node || !node->AST_XML_CHILD) {
- return ret;
+ return NULL;
}
if (raw_output) {
notcleanret = ast_xml_get_text(node);
- ret = xmldoc_string_cleanup(notcleanret);
+ xmldoc_string_cleanup(notcleanret, &ret);
ast_xml_free_text(notcleanret);
} else {
tmp = node->AST_XML_CHILD;
while (tmp) {
/* if found, parse a <para> element. */
- if (xmldoc_parse_para(tmp, "", "\n", &len, &ret)) {
+ if (xmldoc_parse_para(tmp, "", "\n", &ret)) {
tmp = tmp->AST_XML_NEXT;
continue;
- } else if (xmldoc_parse_specialtags(tmp, "", "\n", &len, &ret)) {
+ } else if (xmldoc_parse_specialtags(tmp, "", "\n", &ret)) {
tmp = tmp->AST_XML_NEXT;
continue;
}
/* if found, parse a <variablelist> element. */
- xmldoc_parse_variablelist(tmp, "", &len, &ret);
+ xmldoc_parse_variablelist(tmp, "", &ret);
tmp = tmp->AST_XML_NEXT;
}
}
@@ -4033,7 +3992,8 @@
static char *xmldoc_get_field(const char *type, const char *name, const char *var, int raw)
{
ast_xml_node *node;
- char *ret = NULL, *formatted;
+ char *ret = NULL;
+ struct ast_str *formatted;
if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
ast_log(LOG_WARNING, "Tried to look in XML tree with faulty values.\n");
@@ -4061,8 +4021,10 @@
}
formatted = xmldoc_get_formatted(node, raw, raw);
-
- return formatted;
+ ret = ast_strdup(formatted->str);
+ ast_free(formatted);
+
+ return ret;
}
#endif /* XML_DOCUMENTATION */
More information about the asterisk-commits
mailing list