[asterisk-commits] tilghman: trunk r89114 - in /trunk: apps/app_readfile.c funcs/func_env.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Nov 8 11:32:16 CST 2007


Author: tilghman
Date: Thu Nov  8 11:32:15 2007
New Revision: 89114

URL: http://svn.digium.com/view/asterisk?view=rev&rev=89114
Log:
Add the FILE() dialplan function and deprecate ReadFile.

Modified:
    trunk/apps/app_readfile.c
    trunk/funcs/func_env.c

Modified: trunk/apps/app_readfile.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_readfile.c?view=diff&rev=89114&r1=89113&r2=89114
==============================================================================
--- trunk/apps/app_readfile.c (original)
+++ trunk/apps/app_readfile.c Thu Nov  8 11:32:15 2007
@@ -58,6 +58,7 @@
 	int res=0;
 	char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
 	int len=0;
+	static int deprecation_warning = 0;
 
 	if (ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "ReadFile require an argument!\n");
@@ -69,6 +70,9 @@
 	varname = strsep(&s, "=");
 	file = strsep(&s, ",");
 	length = s;
+
+	if (deprecation_warning++ % 10 == 0)
+		ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
 
 	if (!varname || !file) {
 		ast_log(LOG_ERROR, "No file or variable specified!\n");

Modified: trunk/funcs/func_env.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_env.c?view=diff&rev=89114&r1=89113&r2=89114
==============================================================================
--- trunk/funcs/func_env.c (original)
+++ trunk/funcs/func_env.c Thu Nov  8 11:32:15 2007
@@ -107,6 +107,49 @@
 			break;
 		}
 	}
+
+	return 0;
+}
+
+static int file_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(filename);
+		AST_APP_ARG(offset);
+		AST_APP_ARG(length);
+	);
+	int offset = 0, length;
+	char *contents;
+
+	AST_STANDARD_APP_ARGS(args, data);
+	if (args.argc > 1)
+		offset = atoi(args.offset);
+
+	if (args.argc > 2) {
+		if ((length = atoi(args.length)) < 1) {
+			ast_log(LOG_WARNING, "Invalid length '%s'.  Returning the max (%d)\n", args.length, len);
+			length = len;
+		} else if (length > len) {
+			ast_log(LOG_WARNING, "Length %d is greater than the max (%d).  Truncating output.\n", length, len);
+			length = len;
+		}
+	} else
+		length = len;
+
+	if (!(contents = ast_read_textfile(args.filename)))
+		return -1;
+
+	if (offset >= 0)
+		ast_copy_string(buf, &contents[offset], length);
+	else {
+		size_t tmp = strlen(contents);
+		if (offset * -1 > tmp) {
+			ast_log(LOG_WARNING, "Offset is larger than the file size.\n");
+			offset = tmp * -1;
+		}
+		ast_copy_string(buf, &contents[tmp + offset], length);
+	}
+	ast_free(contents);
 
 	return 0;
 }
@@ -136,12 +179,29 @@
 		"  M - Returns the epoch at which the file was last modified\n",
 };
 
+static struct ast_custom_function file_function = {
+	.name = "FILE",
+	.synopsis = "Obtains the contents of a file",
+	.syntax = "FILE(<filename>,<offset>,<length>)",
+	.read = file_read,
+	/*
+	 * Some enterprising programmer could probably add write functionality
+	 * to FILE(), although I'm not sure how useful it would be.  Hence why
+	 * it's called FILE and not READFILE (like the app was).
+	 */
+	.desc =
+"<offset> may be specified as any number.  If negative, <offset> specifies\n"
+"    the number of bytes back from the end of the file.\n"
+"<length>, if specified, will limit the length of the data read to that size.\n",
+};
+
 static int unload_module(void)
 {
 	int res = 0;
 
 	res |= ast_custom_function_unregister(&env_function);
 	res |= ast_custom_function_unregister(&stat_function);
+	res |= ast_custom_function_unregister(&file_function);
 
 	return res;
 }
@@ -152,6 +212,7 @@
 
 	res |= ast_custom_function_register(&env_function);
 	res |= ast_custom_function_register(&stat_function);
+	res |= ast_custom_function_register(&file_function);
 
 	return res;
 }




More information about the asterisk-commits mailing list