[Asterisk-cvs] asterisk cdr.c,1.45,1.46 pbx.c,1.255,1.256

kpfleming at lists.digium.com kpfleming at lists.digium.com
Mon Jun 20 19:36:08 CDT 2005


Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv1865

Modified Files:
	cdr.c pbx.c 
Log Message:
use ast_build_string() to build strings into buffers, and general cleanup of variable serializing functions used by 'show channel' (bug #4558)


Index: cdr.c
===================================================================
RCS file: /usr/cvsroot/asterisk/cdr.c,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -d -r1.45 -r1.46
--- cdr.c	17 Jun 2005 12:51:03 -0000	1.45
+++ cdr.c	20 Jun 2005 23:37:01 -0000	1.46
@@ -324,17 +324,14 @@
 	return x;
 }
 
-#define CDR_CLEN 18
 int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, char delim, char sep, int recur) 
 {
 	struct ast_var_t *variables;
-	struct varshead *headp;
-	char *var=NULL ,*val=NULL;
-	char *tmp = NULL;
+	char *var, *val;
+	char *tmp;
 	char workspace[256];
-	int workspacelen;
-	int total = 0, x = 0, i = 0;
-	const char *cdrcols[CDR_CLEN] = { 
+	int total = 0, x = 0, i;
+	const char *cdrcols[] = { 
 		"clid",
 		"src",
 		"dst",
@@ -355,46 +352,36 @@
 		"userfield"
 	};
 
+	memset(buf, 0, size);
 
-	memset(buf,0,size);
-	while (cdr) {
-		x++;
-		if (x > 1) {
-			strncat(buf, "\n", size);
-		}
-		headp = &cdr->varshead;
-		AST_LIST_TRAVERSE(headp,variables,entries) {
-			if (cdr && variables &&
+	for (; cdr; cdr = recur ? cdr->next : NULL) {
+		if (++x > 1)
+			ast_build_string(&buf, &size, "\n");
+
+		AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
+			if (variables &&
 			    (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
 			    !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
-				snprintf(buf + strlen(buf), size - strlen(buf), "level %d: %s%c%s%c", x, var, delim, val, sep);
-				if (strlen(buf) >= size) {
-					ast_log(LOG_ERROR,"Data Buffer Size Exceeded!\n");
-					break;
-				}
-				total++;
+				if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, var, delim, val, sep)) {
+ 					ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
+ 					break;
+				} else
+					total++;
 			} else 
 				break;
 		}
-		for (i = 0 ; i < CDR_CLEN; i++) {
-			workspacelen = sizeof(workspace);
-			ast_cdr_getvar(cdr, cdrcols[i], &tmp, workspace, workspacelen, 0);
+
+		for (i = 0; i < (sizeof(cdrcols) / sizeof(cdrcols[0])); i++) {
+			ast_cdr_getvar(cdr, cdrcols[i], &tmp, workspace, sizeof(workspace), 0);
 			if (!tmp)
 				continue;
 			
-			snprintf(buf + strlen(buf), size - strlen(buf), "level %d: %s%c%s%c", x, cdrcols[i], delim, tmp, sep);
-			if (strlen(buf) >= size) {
-				ast_log(LOG_ERROR,"Data Buffer Size Exceeded!\n");
+			if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, cdrcols[i], delim, tmp, sep)) {
+				ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
 				break;
-			}
-			total++;
-		}
-
-		if (!recur) {
-			break;
+			} else
+				total++;
 		}
-
-		cdr = cdr->next;
 	}
 
 	return total;

Index: pbx.c
===================================================================
RCS file: /usr/cvsroot/asterisk/pbx.c,v
retrieving revision 1.255
retrieving revision 1.256
diff -u -d -r1.255 -r1.256
--- pbx.c	6 Jun 2005 22:12:18 -0000	1.255
+++ pbx.c	20 Jun 2005 23:37:01 -0000	1.256
@@ -5667,24 +5667,25 @@
 int pbx_builtin_serialize_variables(struct ast_channel *chan, char *buf, size_t size) 
 {
 	struct ast_var_t *variables;
-	struct varshead *headp;
-	char *var=NULL ,*val=NULL;
+	char *var, *val;
 	int total = 0;
 
-	memset(buf,0,size);
-	if (chan) {
-		headp=&chan->varshead;
-		AST_LIST_TRAVERSE(headp,variables,entries) {
-			if(chan && variables && (var=ast_var_name(variables)) && (val=ast_var_value(variables)) && !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
-				snprintf(buf + strlen(buf), size - strlen(buf), "%s=%s\n", var, val);
-				if(strlen(buf) >= size) {
-					ast_log(LOG_ERROR,"Data Buffer Size Exceeded!\n");
-					break;
-				}
-				total++;
-			} else 
+	if (!chan)
+		return 0;
+
+	memset(buf, 0, size);
+
+	AST_LIST_TRAVERSE(&chan->varshead, variables, entries) {
+		if(variables &&
+		   (var=ast_var_name(variables)) && (val=ast_var_value(variables)) &&
+		   !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
+			if (ast_build_string(&buf, &size, "%s=%s\n", var, val)) {
+				ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
 				break;
-		}
+			} else
+				total++;
+		} else 
+			break;
 	}
 	
 	return total;




More information about the svn-commits mailing list