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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Aug 17 14:18:03 CDT 2008


Author: eliel
Date: Sun Aug 17 14:18:03 2008
New Revision: 138513

URL: http://svn.digium.com/view/asterisk?view=rev&rev=138513
Log:
Simplify string wrapping mechanism. We wrap the complete text to a specified
number of columns instead of wrapping every line making the code hard to modify if
we want to change tabs or spaces for formatting purposes.


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=138513&r1=138512&r2=138513
==============================================================================
--- team/group/appdocsxml/main/pbx.c (original)
+++ team/group/appdocsxml/main/pbx.c Sun Aug 17 14:18:03 2008
@@ -2796,22 +2796,16 @@
 }
 
 #ifdef XML_DOCUMENTATION
-/*! \brief Justify a text to a number of columns. 
- *  \param text Input text to be justified.
- *  \param columns Number of columns to preserve in the text.
- *  \param maxdiff Try to not cut a word when goinf down.
- *  \param postbr Put this chars after a \n.
- *  \param predatalen Length of the data already printed.
- *  \retval NULL on error.
- *  \retval The wrapped text.
+/*! \internal
+ *  \brief Calculate the space in bytes used by a format string
+ *  that will be passed to a sprintf function.
+ *  \param postbr The format string to use to calculate the length.
+ *  \retval The postbr length.
  */
-static char *xmldoc_string_wrap(const char *text, int columns, int maxdiff, const char *postbr, int predatalen)
-{
-	char *tmp, *ret, *t;
-	char *in = (char *)text;
-	int count = 1, tmplen = 0, i, postbrlen = 0, postbrreallen = 0;
-	int sep, needtobreak = 0, colmax;
-	
+static int xmldoc_postbrlen(const char *postbr)
+{
+	int postbrreallen = 0, i;
+
 	if (postbr) {
 		for (i = 0; i < strlen(postbr); i++) {
 			if (postbr[i] == '\t') {
@@ -2820,36 +2814,79 @@
 				postbrreallen++;
 			}
 		}
-		postbrlen = strlen(postbr);
-	}
-
+	}
+	return postbrreallen;
+}
+
+/*! \internal
+ *  \brief Setup postbr to be used while wrapping the text. 
+ *  Add to postbr array all the spaces and tabs at the beginning of the line.
+ *  \param postbr output array. 
+ *  \param len array length.
+ *  \param text Text with format string before the actual string.
+ */
+static void xmldoc_setpostbr(char *postbr, size_t len, const char *text)
+{
+	int c = 0, postbrlen = 0;
+	char *tmp = (char *)text;
+	
 	if (!text) {
+		return;
+	}
+
+	while (*tmp && (tmp[c] == '\t' || tmp[c] == ' ') && c < len) {
+		postbr[postbrlen++] = tmp[c++];
+	}
+	postbr[postbrlen] = '\0';
+}
+
+/*! \brief Justify a text to a number of columns. 
+ *  \param text Input text to be justified.
+ *  \param columns Number of columns to preserve in the text.
+ *  \param maxdiff Try to not cut a word when goinf down.
+ *  \retval NULL on error.
+ *  \retval The wrapped text.
+ */
+static char *xmldoc_string_wrap(char *text, int columns, int maxdiff)
+{
+	char *tmp, *ret, *t;
+	char *in = (char *)text;
+	int count = 1, tmplen = 0, i, postbrreallen = 0;
+	char postbr[160];
+	int sep, needtobreak = 0, colmax;
+	
+	if (!text || columns <= 0 || maxdiff < 0) {
 		return NULL;
 	}
 
 	/* XXX: We could calculate better how much space do we need. */
-	tmp = ast_calloc(1, strlen(text) * ((postbrreallen ? postbrreallen : 1) + 1));
+	tmp = ast_calloc(1, strlen(text) * 3);
 
 	if (!tmp) {
 		return NULL;
 	}
 
-	colmax = columns - postbrreallen - predatalen;
+	/* Check for blanks and tabs and put them in postbr. */
+	xmldoc_setpostbr(postbr, sizeof(postbr), text);	
+	postbrreallen = xmldoc_postbrlen(postbr);
+	colmax = columns - postbrreallen;
+	
 	while (*in) {
 		if (needtobreak || !(count % colmax)) {
 			if (*in == ' ') {
 				tmp[tmplen++] = '\n';
-				for (i = 0; i < postbrlen; i++) {
+				for (i = 0; i < strlen(postbr); i++) {
 					tmp[tmplen++] = postbr[i];
 				}
 				needtobreak = 0;
-			} else {
+			} else if (*in != '\n') {
 				needtobreak = 1;
 				t = in;
 				sep = maxdiff;
 				while (*t && sep > 0) {
+					t++;
 					/* Wait for the next space? */
-					if (*(++t) == ' ') {
+					if (*t == ' ') {
 						break;
 					}
 					sep--;
@@ -2859,7 +2896,8 @@
 					t = in;
 					sep = (maxdiff % tmplen);
 					while (sep > 0) {
-						if (*(--t) == ' ') {
+						t--;
+						if (*t == ' ') {
 							break;
 						}
 						sep--;
@@ -2872,7 +2910,7 @@
 					}
 					tmp[tmplen++] = '-';
 					tmp[tmplen++] = '\n';
-					for (i = 0; i < postbrlen; i++) {
+					for (i = 0; i < strlen(postbr); i++) {
 						tmp[tmplen++] = postbr[i];
 					}
 					needtobreak = 0;
@@ -2882,6 +2920,12 @@
 			while (*in == ' ') {
 				in++;
 			}	
+		}
+		if (*in == '\n') {
+			xmldoc_setpostbr(postbr, sizeof(postbr), in + 1);
+			postbrreallen = xmldoc_postbrlen(postbr);
+			colmax = columns - postbrreallen;
+			needtobreak = 0, count = 1;
 		}
 		tmp[tmplen++] = *in;
 		count++;
@@ -2988,17 +3032,20 @@
  *  \param reverse We are going backwards while generating the syntax?
  *  \param len Current length of 'syntax' buffer.
  *  \param syntax Output buffer for the concatenated values.
- *  \param fmt A format string that will be used in a sprintf call
- *  \param fmtrev A format string that will be used in a sprintf call when reverse=1
+ *  \param fmt A format string that will be used in a sprintf call.
+ *  \param fmtrev A format string that will be used in a sprintf call if reverse == 1.
  */
 static void xmldoc_reverse_helper(int reverse, int *len, char **syntax, const char *fmt, const char *fmtrev, ...)
 {
 	int totlen, tmpfmtlen;
-	char *tmpfmt = NULL, tmp;
+	char *tmpfmt, tmp;
 	va_list ap;
 
 	va_start(ap, fmtrev);
-	ast_vasprintf(&tmpfmt, (reverse ? fmtrev : fmt), ap);
+	if (ast_vasprintf(&tmpfmt, (reverse ? fmtrev : fmt), ap) < 0) {
+		va_end(ap);
+		return;
+	}
 	va_end(ap);
 
 	tmpfmtlen = strlen(tmpfmt);
@@ -3064,8 +3111,7 @@
 	if (!node || !node->AST_XML_CHILD) {
 		/* If the syntax field is not found, at least print the
 		   application/function name. */
-		syntax = ast_malloc(strlen(name) + 3);
-		sprintf(syntax, "%s()", name);
+		ast_asprintf(&syntax, "%s()", name);
 		return syntax;
 	}
 
@@ -3133,7 +3179,7 @@
 	}
 
 	/* init syntax string. */
-	xmldoc_reverse_helper(reverse, &len, &syntax, "%s(", ")", name);
+	xmldoc_reverse_helper(reverse, &len, &syntax, "%s(", "(", name);
 
 	while (node) {
 		if (strcasecmp((char *)node->AST_XML_NAME, "parameter")) {
@@ -3149,8 +3195,7 @@
 				ast_free(syntax);
 			}
 			/* to give up is ok? */
-			syntax = ast_malloc(strlen(name) + 3);
-			sprintf(syntax, "%s()", name);
+			ast_asprintf(&syntax, "%s()", name);
 			return syntax;
 		}
 
@@ -3224,7 +3269,7 @@
 static int xmldoc_parse_para(ast_xml_node *node, const char *tabs, const char *posttabs, int *len, char **buffer, int predatalen)
 {
 	ast_xml_text *tmptext;
-	char *cleantext, *wraptext;
+	char *cleantext;
 
 	if (!node || !node->AST_XML_CHILD) {
 		return 0;
@@ -3240,13 +3285,10 @@
 		/* Strip \n etc. */
 		cleantext = xmldoc_string_cleanup(tmptext);
 		ast_xml_free_text(tmptext);
-		/* wrapped text. */
-		wraptext = xmldoc_string_wrap(cleantext, XMLDOC_TEXT_COLUMNS, XMLDOC_MAX_DIFF, tabs, predatalen);
-		ast_free(cleantext);
-		if (wraptext) {
-			*buffer = ast_realloc(*buffer, *len + strlen(tabs) + strlen(wraptext) + strlen(posttabs) + 1);
-			*len += sprintf(*buffer + *len, "%s%s%s", tabs, wraptext, posttabs);
-			ast_free(wraptext);
+		if (cleantext) {
+			*buffer = ast_realloc(*buffer, *len + strlen(tabs) + strlen(cleantext) + strlen(posttabs) + 1);
+			*len += sprintf(*buffer + *len, "%s%s%s", tabs, cleantext, posttabs);
+			ast_free(cleantext);
 		}
 	}
 
@@ -3269,7 +3311,7 @@
 	ast_xml_node *tmp;
 	ast_xml_attr *valname;
 	ast_xml_text *tmptext;
-	char *wrap, *clean;
+	char *clean;
 
 	if (!node || !node->AST_XML_CHILD) {
 		return;
@@ -3299,15 +3341,10 @@
 				/* Cleanup text. */			
 				clean = xmldoc_string_cleanup(tmptext);
 				ast_xml_free_text(tmptext);
-				/* Wrap text (+5 for string formatting purposes: '\n: ') */
-				wrap = xmldoc_string_wrap(clean, XMLDOC_TEXT_COLUMNS, XMLDOC_MAX_DIFF, tabs, strlen(valname) + 5);
 				if (clean) {
+					*buffer = ast_realloc(*buffer, *len + strlen(clean) + 2);
+					*len += sprintf(*buffer + *len, " %s", clean);
 					ast_free(clean);
-				}
-				if (wrap) {
-					*buffer = ast_realloc(*buffer, *len + strlen(wrap) + 2);
-					*len += sprintf(*buffer + *len, " %s", wrap);
-					ast_free(wrap);
 				}
 			}
 		}
@@ -3377,7 +3414,7 @@
 static char *xmldoc_get_formatted(ast_xml_node *node, int raw_output, int raw_wrap)
 {
 	ast_xml_node *tmp;
-	char *ret = NULL, *notcleanret, *notwrapped;
+	char *ret = NULL, *notcleanret;
 	int len = 0;
 
 	if (!node || !node->AST_XML_CHILD) {
@@ -3386,14 +3423,8 @@
 
 	if (raw_output) {
 		notcleanret = ast_xml_get_text(node);
-		notwrapped = xmldoc_string_cleanup(notcleanret);
+		ret = xmldoc_string_cleanup(notcleanret);
 		ast_xml_free_text(notcleanret);
-		if (raw_wrap) {
-			ret = xmldoc_string_wrap(notwrapped, XMLDOC_TEXT_COLUMNS, XMLDOC_MAX_DIFF, "", 0);
-			ast_free(notwrapped);
-		} else {
-			ret = notwrapped;
-		}
 	} else {
 		tmp = node->AST_XML_CHILD;
 		while (tmp) {
@@ -3422,6 +3453,7 @@
 static char *xmldoc_get_field(const char *type, const char *name, const char *var, int raw)
 {
 	ast_xml_node *node;
+	char *formatted, *ret = NULL;
 
 	if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
 		ast_log(LOG_WARNING, "Tried to look in XML tree with faulty values.\n");
@@ -3448,7 +3480,13 @@
 		return NULL;
 	}
 
-	return xmldoc_get_formatted(node, raw, raw);
+	formatted = xmldoc_get_formatted(node, raw, raw);
+	if (formatted) {
+		ret = xmldoc_string_wrap(formatted, XMLDOC_TEXT_COLUMNS, XMLDOC_MAX_DIFF);
+		ast_free(formatted);
+	}
+
+	return ret;
 }
 #endif /* XML_DOCUMENTATION */
 




More information about the asterisk-commits mailing list