[asterisk-commits] trunk - r7989 in /trunk/funcs: func_cdr.c func_cut.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Jan 11 13:52:30 CST 2006


Author: mogorman
Date: Wed Jan 11 13:52:29 2006
New Revision: 7989

URL: http://svn.digium.com/view/asterisk?rev=7989&view=rev
Log:
Added macros from 6198 and 6203.  also added better
error reporting in func_cut

Modified:
    trunk/funcs/func_cdr.c
    trunk/funcs/func_cut.c

Modified: trunk/funcs/func_cdr.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_cdr.c?rev=7989&r1=7988&r2=7989&view=diff
==============================================================================
--- trunk/funcs/func_cdr.c (original)
+++ trunk/funcs/func_cdr.c Wed Jan 11 13:52:29 2006
@@ -38,13 +38,24 @@
 #include "asterisk/app.h"
 #include "asterisk/cdr.h"
 
+enum {
+	OPT_RECURSIVE = (1 << 0),
+} cdr_option_flags;
+
+AST_APP_OPTIONS(cdr_func_options, {
+	AST_APP_OPTION('r', OPT_RECURSIVE),
+});
+
 static char *builtin_function_cdr_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
 {
 	char *ret;
-	char *mydata;
-	int argc;
-	char *argv[2];
-	int recursive = 0;
+	char *parse;
+	struct ast_flags flags = {0};
+	
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(variable);
+		AST_APP_ARG(options);
+	);
 
 	if (ast_strlen_zero(data))
 		return NULL;
@@ -52,47 +63,54 @@
 	if (!chan->cdr)
 		return NULL;
 
-	mydata = ast_strdupa(data);
-	argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
-
-	/* check for a trailing flags argument */
-	if (argc > 1) {
-		argc--;
-		if (strchr(argv[argc], 'r'))
-			recursive = 1;
+	parse = ast_strdupa(data);
+	if (!parse) {
+		ast_log(LOG_ERROR, "Out of memory!\n");
+		return NULL;
 	}
 
-	ast_cdr_getvar(chan->cdr, argv[0], &ret, buf, len, recursive);
+	AST_STANDARD_APP_ARGS(args, parse);
+	
+	if(!ast_strlen_zero(args.options) ) {
+		ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
+	}
+	ast_cdr_getvar(chan->cdr, args.variable, &ret, buf, len, (ast_test_flag(&flags,OPT_RECURSIVE) ) ? 1 : 0 );
 
 	return ret;
 }
 
 static void builtin_function_cdr_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
 {
-	char *mydata;
-	int argc;
-	char *argv[2];
-	int recursive = 0;
+	char *parse;
+	struct ast_flags flags = {0};
+
+	AST_DECLARE_APP_ARGS(args, 
+		AST_APP_ARG(variable);
+		AST_APP_ARG(options);
+	);      
 
 	if (ast_strlen_zero(data) || !value)
 		return;
 	
-	mydata = ast_strdupa(data);
-	argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
+	parse = ast_strdupa(data);
+	if (!parse) {
+		ast_log(LOG_ERROR, "Out of memory!\n");
+		return;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
 
 	/* check for a trailing flags argument */
-	if (argc > 1) {
-		argc--;
-		if (strchr(argv[argc], 'r'))
-			recursive = 1;
+	if(!ast_strlen_zero(args.options) ) {
+		ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
 	}
 
-	if (!strcasecmp(argv[0], "accountcode"))
+	if (!strcasecmp(args.variable, "accountcode"))
 		ast_cdr_setaccount(chan, value);
-	else if (!strcasecmp(argv[0], "userfield"))
+	else if (!strcasecmp(args.variable, "userfield"))
 		ast_cdr_setuserfield(chan, value);
 	else if (chan->cdr)
-		ast_cdr_setvar(chan->cdr, argv[0], value, recursive);
+		ast_cdr_setvar(chan->cdr, args.variable, value, (ast_test_flag(&flags,OPT_RECURSIVE) ) ? 1 : 0 );
 }
 
 #ifndef BUILTIN_FUNC

Modified: trunk/funcs/func_cut.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_cut.c?rev=7989&r1=7988&r2=7989&view=diff
==============================================================================
--- trunk/funcs/func_cut.c (original)
+++ trunk/funcs/func_cut.c Wed Jan 11 13:52:29 2006
@@ -133,42 +133,40 @@
 
 static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
 {
-	char *s, *args[3], *varname=NULL, *delimiter=NULL, *field=NULL;
-	int args_okay = 0;
-
-	memset(buffer, 0, buflen);
+	char *parse;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(varname);
+		AST_APP_ARG(delimiter);
+		AST_APP_ARG(field);
+	);
+
+	memset(buffer, 0, buflen); 
+	
+	parse = ast_strdupa(data);
+	if (!parse) {
+		ast_log(LOG_ERROR, "Out of memory!\n");
+		return ERROR_NOMEM;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
 
 	/* Check and parse arguments */
-	if (data) {
-		s = ast_strdupa((char *)data);
-		if (s) {
-			ast_app_separate_args(s, '|', args, 3);
-			varname = args[0];
-			delimiter = args[1];
-			field = args[2];
-
-			if (field) {
-				args_okay = 1;
-			}
-		} else {
-			return ERROR_NOMEM;
-		}
-	}
-
-	if (args_okay) {
+	if(args.argc < 3){
+		return ERROR_NOARG;
+	} else {
 		char d, ds[2];
-		char *tmp = alloca(strlen(varname) + 4);
+		char *tmp = alloca(strlen(args.varname) + 4);
 		char varvalue[MAXRESULT], *tmp2=varvalue;
 
 		if (tmp) {
-			snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
+			snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname);
 			memset(varvalue, 0, sizeof(varvalue));
 		} else {
 			return ERROR_NOMEM;
 		}
 
-		if (delimiter[0])
-			d = delimiter[0];
+		if (args.delimiter[0])
+			d = args.delimiter[0];
 		else
 			d = '-';
 
@@ -179,8 +177,8 @@
 
 		if (tmp2) {
 			int curfieldnum = 1;
-			while ((tmp2 != NULL) && (field != NULL)) {
-				char *nextgroup = strsep(&field, "&");
+			while ((tmp2 != NULL) && (args.field != NULL)) {
+				char *nextgroup = strsep(&(args.field), "&");
 				int num1 = 0, num2 = MAXRESULT;
 				char trashchar;
 
@@ -231,8 +229,6 @@
 				}
 			}
 		}
-	} else {
-		return ERROR_NOARG;
 	}
 	return 0;
 }
@@ -267,7 +263,7 @@
 
 	switch (cut_internal(chan, data, buf, len)) {
 	case ERROR_NOARG:
-		ast_log(LOG_ERROR, "CUT() requires an argument\n");
+		ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
 		break;
 	case ERROR_NOMEM:
 		ast_log(LOG_ERROR, "Out of memory\n");



More information about the asterisk-commits mailing list