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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Oct 21 05:46:47 CDT 2008


Author: eliel
Date: Tue Oct 21 05:46:46 2008
New Revision: 151326

URL: http://svn.digium.com/view/asterisk?view=rev&rev=151326
Log:
More fixes pointed by russelb.
 - instead of initiating a string like ast_str_create(1) that will be reallocated
   in the next append, just try to allocate the probable space we will used (and avoid some reallocations).
 - Well this is easy, dont know why I have code this in this manner but instead of doing something
   like: for(a=0;a<strlen(b);a++); just calculate strlen(b) once!!.
 - Check when calling ast_asprintf() that the allocated buffer is not NULL (Notice here that
   when calling ast_asprintf and returning the allocated buffer is not needed to check for a NULL
   value cause in case of an error we will return that NULL value.
 - Some 'const char *' parameters where being casted (uuuhhhggg) to 'char *' to move their pointers in a
   while(), dont do this eliel! just use a for() instead! (k.i.s.s.).
Thanks Russell!

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=151326&r1=151325&r2=151326
==============================================================================
--- team/group/appdocsxml/main/pbx.c (original)
+++ team/group/appdocsxml/main/pbx.c Tue Oct 21 05:46:46 2008
@@ -3162,9 +3162,11 @@
 static int xmldoc_postbrlen(const char *postbr)
 {
 	int postbrreallen = 0, i;
+	size_t postbrlen;
 
 	if (postbr) {
-		for (i = 0; i < strlen(postbr); i++) {
+		postbrlen = strlen(postbr);
+		for (i = 0; i < postbrlen; i++) {
 			if (postbr[i] == '\t') {
 				postbrreallen += 8 - (postbrreallen % 8);
 			} else {
@@ -3179,20 +3181,23 @@
  *  \brief Setup postbr to be used while wrapping the text.
  *         Add to postbr array all the spaces and tabs at the beginning of text.
  *  \param postbr output array.
- *  \param len array length.
+ *  \param len text 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;
+	int c, postbrlen = 0;
 	
 	if (!text) {
 		return;
 	}
 
-	while (*tmp && (tmp[c] == '\t' || tmp[c] == ' ') && c < len) {
-		postbr[postbrlen++] = tmp[c++];
+	for (c = 0; c < len; c++) { 
+		if (text[c] == '\t' || text[c] == ' ') {
+			postbr[postbrlen++] = text[c];
+		} else {
+			break;
+		}
 	}
 	postbr[postbrlen] = '\0';
 }
@@ -3213,7 +3218,9 @@
 	char postbr[160];
 	int sep, needtobreak = 0, colmax;
 
+	/* sanity check */
 	if (!text || columns <= 0 || maxdiff < 0) {
+		ast_log(LOG_WARNING, "Passing wrong arguments while trying to wrap the text\n");
 		return NULL;
 	}
 
@@ -3331,9 +3338,15 @@
 	char *colorized = NULL, *wrapped = NULL;
 	int i, c, colorized_len = 1, actual_len=0, len, colorsection;
 	char *tmp;
+	size_t bwinputlen;
+
+	if (!bwinput) {
+		return NULL;
+	}
+	bwinputlen = strlen(bwinput);
 
 	/* Calculate output string size. Try to figure out the needed space. */
-	for (i = 0;i < strlen(bwinput);i++) {
+	for (i = 0; i < bwinputlen;i++) {
 		colorized_len++;
 		for (c = 0; c < ARRAY_LEN(colorized_tags); c++) {
 			if (!strcasecmp(colorized_tags[c].inittag, bwinput + i)) {
@@ -3356,7 +3369,7 @@
 	term_color_code(colorized, base_fg, base_bg, 23);
 	actual_len = strlen(colorized);
 
-	for (i = 0; i < strlen(bwinput);i++) {
+	for (i = 0; i < bwinputlen;i++) {
 		colorsection = 0;
 		/* Check if we are at the begining of a tag to be colorized. */
 		for (c = 0; c < ARRAY_LEN(colorized_tags); c++) {
@@ -3411,14 +3424,20 @@
 static void xmldoc_string_cleanup(const char *text, struct ast_str **output, int lastspaces)
 {
 	int i;
-
-	*output = ast_str_create(1);
+	size_t textlen;
+
+	if (!text) {
+		return;
+	}
+	textlen = strlen(text);
+
+	*output = ast_str_create(textlen);
 	if (!(*output)) {
 		ast_log(LOG_ERROR, "Problem allocating output buffer\n");
 		return;
 	}
 
-	for (i = 0; i < strlen(text); i++) {
+	for (i = 0; i < textlen; i++) {
 		if (text[i] == '\n' || text[i] == '\r') {
 			while (text[i+1] == '\t' || text[i+1] == '\r' || text[i+1] == '\n') {
 				i++;
@@ -3880,7 +3899,7 @@
 			/* This is a special tag. */
 
 			/* concat data */
-			if (strlen(special_tags[i].init) > 0) {
+			if (!ast_strlen_zero(special_tags[i].init)) {
 				ast_str_append(buffer, 0, "%s%s", tabs, special_tags[i].init);
 			}
 
@@ -3892,7 +3911,7 @@
 				}
 			}
 
-			if (strlen(special_tags[i].end) > 0) {
+			if (!ast_strlen_zero(special_tags[i].end)) {
 				ast_str_append(buffer, 0, "%s%s", special_tags[i].end, posttabs);
 			}
 			break;
@@ -4028,7 +4047,9 @@
 
 	/* use this spacing (add 4 spaces) inside a variablelist node. */
 	ast_asprintf(&vartabs, "%s    ", tabs);
-
+	if (!vartabs) {
+		return ret;
+	}
 	for (tmp = node->AST_XML_CHILD; tmp; tmp = tmp->AST_XML_NEXT) {
 		/* We can have a <para> element inside the variable list */
 		if ((xmldoc_parse_para(tmp, (ret ? tabs : ""), "\n", buffer))) {
@@ -4094,7 +4115,7 @@
 	}
 
 	/* prepare the output string. */
-	outputstr = ast_str_create(1);
+	outputstr = ast_str_create(128);
 	if (!outputstr) {
 		return NULL;
 	}
@@ -4204,7 +4225,9 @@
 	char *optiontabs;
 
 	ast_asprintf(&optiontabs, "%s    ", tabs);
-
+	if (!optiontabs) {
+		return ret;
+	}
 	for (node = fixnode->AST_XML_CHILD; node; node = node->AST_XML_NEXT) {
 		if (!strcasecmp((char *)node->AST_XML_NAME, "argument")) {
 			/* if this is the first data appended to buffer, print a \n*/
@@ -4295,13 +4318,16 @@
 		return;
 	}
 
+	ast_asprintf(&internaltabs, "%s    ", tabs);
+	if (!internaltabs) {
+		return;
+	}
+
 	if (!hasarguments) {
 		ast_str_append(buffer, 0, "%s\n", paramname);
 		ast_xml_free_attr(paramname);
 		printed = 1;
 	}
-
-	ast_asprintf(&internaltabs, "%s    ", tabs);
 
 	for (node = node->AST_XML_CHILD; node; node = node->AST_XML_NEXT) {
 		if (!strcasecmp((char *)node->AST_XML_NAME, "optionlist")) {
@@ -4336,7 +4362,7 @@
 static char *xmldoc_build_arguments(const char *type, const char *name)
 {
 	ast_xml_node *node;
-	struct ast_str *ret = ast_str_create(1);
+	struct ast_str *ret = ast_str_create(128);
 	char *retstr = NULL;
 
 	if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
@@ -4390,7 +4416,7 @@
 static struct ast_str *xmldoc_get_formatted(ast_xml_node *node, int raw_output, int raw_wrap)
 {
 	ast_xml_node *tmp;
-	struct ast_str *ret = ast_str_create(1);
+	struct ast_str *ret = ast_str_create(128);
 	char *notcleanret, *tmpstr;
 
 	if (raw_output) {




More information about the asterisk-commits mailing list