use ast_build_string() to build strings into buffers, and general cleanup of variable...
authorKevin P. Fleming <kpfleming@digium.com>
Mon, 20 Jun 2005 23:37:01 +0000 (23:37 +0000)
committerKevin P. Fleming <kpfleming@digium.com>
Mon, 20 Jun 2005 23:37:01 +0000 (23:37 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5944 65c4cc65-6c06-0410-ace0-fbb531ad65f3

cdr.c
pbx.c

diff --git a/cdr.c b/cdr.c
index 9a01ec0..9961222 100755 (executable)
--- a/cdr.c
+++ b/cdr.c
@@ -324,17 +324,14 @@ int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
        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 @@ int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, cha
                "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;
diff --git a/pbx.c b/pbx.c
index b4df9be..767023c 100755 (executable)
--- a/pbx.c
+++ b/pbx.c
@@ -5667,24 +5667,25 @@ static int pbx_builtin_goto(struct ast_channel *chan, void *data)
 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;