diff -urN --exclude '*.*o' --exclude '.svn*' --exclude '.depend*' trunk_orig/res/Makefile trunk-arrays/res/Makefile --- trunk_orig/res/Makefile 2005-12-12 09:37:45.000000000 +0100 +++ trunk-arrays/res/Makefile 2005-12-12 17:10:30.000000000 +0100 @@ -11,7 +11,7 @@ # the GNU General Public License # -MODS=res_indications.so res_monitor.so res_adsi.so res_agi.so res_features.so +MODS=res_indications.so res_monitor.so res_adsi.so res_agi.so res_features.so res_arrays.so ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/odbcinst.h)$(wildcard $(CROSS_COMPILE_TARGET)/usr/local/include/odbcinst.h),) ifneq (${OSARCH},FreeBSD) diff -urN --exclude '*.*o' --exclude '.svn*' --exclude '.depend*' trunk_orig/res/res_arrays.c trunk-arrays/res/res_arrays.c --- trunk_orig/res/res_arrays.c 1970-01-01 01:00:00.000000000 +0100 +++ trunk-arrays/res/res_arrays.c 2005-12-12 17:05:29.000000000 +0100 @@ -0,0 +1,502 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2005, Christian Richter + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Array Related Dialplan Applications + * + */ + +#include +#include +#include + +#include "asterisk.h" + +/* ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $") */ + +#include "asterisk/lock.h" +#include "asterisk/logger.h" +#include "asterisk/channel.h" +#include "asterisk/pbx.h" +#include "asterisk/app.h" +#include "asterisk/module.h" +#include "asterisk/translate.h" +#include "asterisk/options.h" +#include "asterisk/utils.h" + +static char *tdesc = "Array Variable Applications"; + +static char *explode_app="ArraysExplode"; +static char *explode_synopsis = "Create Array from String"; + +static char *implode_app="ArraysImplode"; +static char *implode_synopsis = "Create String from Array"; + +static char *remove_idx_app="ArraysRemoveIndex"; +static char *remove_idx_synopsis = "Remove Element by Index"; + +static char *explode_descrip = +" ArraysExplode(ArrayName|ArrayData|delim)\n\n" +"Creates a Variable Array of the ArrayData String, where the elements\n" +"are delimited by delim.\n" +" ArrayName -- Name of the resulting Array Variable\n" +" ArrayData -- String which holds the Array Data\n" +" delim -- Delimiter\n" +"\n\n" +; + +static char *implode_descrip = +" ArraysImplode(StringName|ArrayName|delim)\n\n" +"Creates a String Variable of the ArrayData, where the elements\n" +"are delimited by delim.\n" +" StringName -- Name of the resulting String Variable\n" +" ArrayName -- Name of the Array\n" +" delim -- Delimiter\n" +"\n\n" +; + +static char *remove_idx_descrip = +" ArraysRemoveIdx(ArrayName|idx)\n\n" +"Removes the idx'th element of the array\n" +" ArrayName -- Name of the Array\n" +" Idx -- Index\n" +"\n\n" +; + +STANDARD_LOCAL_USER; + +LOCAL_USER_DECL; + +#define ARRAY_MAXNAME 128 + +static int ArraysExplode_exec(struct ast_channel *chan, void *data) +{ + struct localuser *u; + char *argcopy=NULL; + char *args[5]; + char *delim; + char *name; + char *string; + char *tok; + int x=0, res=0, i=0; + + + if (ast_strlen_zero(data)) { + ast_log(LOG_WARNING, "ArrayExplode requires Arguments\n"); + return -1; + } + + LOCAL_USER_ADD(u); + + argcopy = ast_strdupa(data); + if (!argcopy) { + ast_log(LOG_ERROR, "Out of memory\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + if (ast_app_separate_args(argcopy,'|',args,sizeof(args)/sizeof(args[0])) < 1) { + ast_log(LOG_WARNING, "Cannot Parse Arguments.\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + name=args[x++]; + string=args[x++]; + delim=args[x++]; + + if (!delim) { + ast_log(LOG_WARNING,"EXPLODE called without delim\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + if ( strlen(name) >= ARRAY_MAXNAME) { + ast_log(LOG_WARNING,"ArrayName should only contain 128 Characters\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + for ( tok=strsep(&string, delim); + tok; + tok=strsep(&string, delim)) { + + char myname[ARRAY_MAXNAME + 5]; + sprintf(myname,"%s[%d]",name,i); + pbx_builtin_setvar_helper(chan, myname, tok); + i++; + + if (i>99) { + ast_log(LOG_WARNING,"Explode supports only 99 Elements\n"); + break; + } + } + + LOCAL_USER_REMOVE(u); + return res; +} + + +static int array_len(struct ast_channel *chan, char *name) +{ + char varname[ARRAY_MAXNAME]; + const char *element; + int i=0; + + for ( sprintf(varname,"%s[%d]",name,i), + element=pbx_builtin_getvar_helper(chan, varname); + + element; + + sprintf(varname,"%s[%d]",name,++i), + element=pbx_builtin_getvar_helper(chan, varname)) { + + + if (i>99) { + ast_log(LOG_WARNING,"Len supports only 99 Elements\n"); + break; + } + } + + return i; + +} + +static void array_remove(struct ast_channel *chan, char *name, int idx) +{ + int len=array_len(chan, name); + int i; + char varname[ARRAY_MAXNAME]; + char varname2[ARRAY_MAXNAME]; + + for ( i=idx+1; i<=len; i++) { + sprintf(varname,"%s[%d]",name,i); + sprintf(varname2,"%s[%d]",name,i-1); + pbx_builtin_setvar_helper(chan,varname2,pbx_builtin_getvar_helper(chan, varname)); + } + + sprintf(varname,"%s[%d]",name,len-1); + pbx_builtin_setvar_helper(chan,varname,NULL); + +} + + +static int ArraysRemoveIdx_exec(struct ast_channel *chan, void *data) +{ + struct localuser *u; + char *argcopy=NULL; + char *args[3]; + char *name,*idx; + int x=0, res=0; + + if (ast_strlen_zero(data)) { + ast_log(LOG_WARNING, "ArrayExplode requires Arguments\n"); + return -1; + } + + LOCAL_USER_ADD(u); + + argcopy = ast_strdupa(data); + if (!argcopy) { + ast_log(LOG_ERROR, "Out of memory\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + if (ast_app_separate_args(argcopy,'|',args,sizeof(args)/sizeof(args[0])) < 1) { + ast_log(LOG_WARNING, "Cannot Parse Arguments.\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + name=args[x++]; + idx=args[x++]; + + array_remove(chan, name, atoi(idx)); + + + LOCAL_USER_REMOVE(u); + return res; +} + + + + + +static int ArraysImplode_exec(struct ast_channel *chan, void *data) +{ + struct localuser *u; + char *argcopy=NULL; + char *args[5]; + char *delim; + char *name; + char *string; + char varname[ARRAY_MAXNAME+4]; + int x=0, res=0, i=0; + int len=0; + + + if (ast_strlen_zero(data)) { + ast_log(LOG_WARNING, "ArrayExplode requires Arguments\n"); + return -1; + } + + LOCAL_USER_ADD(u); + + argcopy = ast_strdupa(data); + if (!argcopy) { + ast_log(LOG_ERROR, "Out of memory\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + + if (ast_app_separate_args(argcopy,'|',args,sizeof(args)/sizeof(args[0])) < 1) { + ast_log(LOG_WARNING, "Cannot Parse Arguments.\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + string=args[x++]; + name=args[x++]; + delim=args[x++]; + + if (!delim) { + ast_log(LOG_WARNING,"EXPLODE called without delim\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + /*make sure delim contains only 1 char*/ + delim[2]=0; + + if ( strlen(name) >= ARRAY_MAXNAME) { + ast_log(LOG_WARNING,"ArrayName should only contain 128 Characters\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + len=array_len(chan,name); + + if (len>99) { + ast_log(LOG_WARNING,"Implode supports only 99 Elements\n"); + len=99; + } + + { + const char *tmparr[len]; + char *arr; + int arrsize=0; + + for ( i=0; i= ARRAY_MAXNAME) { + ast_log(LOG_WARNING,"ArrayName should only contain 128 Characters\n"); + return NULL; + } + + len=array_len(chan, name); + + { + char lenstr[16]; + sprintf(lenstr,"%d",len); + ast_copy_string(buf, lenstr, len); + } + + return buf; + +} + + +static char *ARRAYGETINDEX_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) +{ + char *argcopy=NULL; + char *args[4]; + char *name, *value; + int x=0, i=0; + + + if (ast_strlen_zero(data)) { + ast_log(LOG_WARNING, "ArrayLen requires Arguments\n"); + return NULL; + } + + argcopy = ast_strdupa(data); + if (!argcopy) { + ast_log(LOG_ERROR, "Out of memory\n"); + return NULL; + } + + if (ast_app_separate_args(argcopy,'|',args,sizeof(args)/sizeof(args[0])) < 1) { + ast_log(LOG_WARNING, "Cannot Parse Arguments.\n"); + return NULL; + } + + name=args[x++]; + value=args[x++]; + + if (!value) { + ast_log(LOG_WARNING,"Value ommitted\n"); + return NULL; + } + + if ( strlen(name) >= ARRAY_MAXNAME) { + ast_log(LOG_WARNING,"ArrayName should only contain 128 Characters\n"); + + return NULL; + } + + len=array_len(chan, name); + + ast_copy_string(buf, "-1", len); + for (i=0; i