Index: funcs/func_strings.c =================================================================== --- funcs/func_strings.c (revision 197994) +++ funcs/func_strings.c (working copy) @@ -86,6 +86,21 @@ \ + + + Replace a set of characters with another character in a given string + + + + + + + + Iterates through a string replacing all the find-chars with + replace-char. + replace-char may be either empty or contain one character + + Check string against a regular expression. @@ -593,6 +608,58 @@ .read = filter, }; +static int replace(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) +{ + AST_DECLARE_APP_ARGS(args, + AST_APP_ARG(find); + AST_APP_ARG(replace); + AST_APP_ARG(string); + ); + AST_STANDARD_APP_ARGS(args, data); + + char *bufptr = buf; + int strlength = strlen(args.string), fndlength = strlen(args.find), replacelength = strlen(args.replace); + int a = 0, b = 0, skip = 0; + + if (args.argc != 3) { + ast_log(LOG_ERROR, "Usage: REPLACE(||)\n"); + return -1; + } + + if (ast_strlen_zero(args.find) || ast_strlen_zero(args.string) || replacelength > 1) { + ast_log(LOG_ERROR, "The characters to find and the string to search must not be empty.\n"); + ast_log(LOG_ERROR, "The replace string must contain a single charater or be empty.\n"); + return -1; + } + + ast_debug(3, "Characters to find: (%s)\n", args.find); + ast_debug(3, "Character to replace with: (%s)\n", args.replace); + ast_debug(3, "String to search: (%s)\n", args.string); + + for (a = 0; a < strlength; a++) { + skip = 0; + for (b = 0; b < fndlength; b++) { + if (args.string[a] == args.find[b]) { + skip = 1; + if (replacelength == 1) { + *bufptr++ = args.replace[0]; + } + } + } + if (skip == 0) { + *bufptr++ = args.string[a]; + } + } + + *bufptr = '\0'; + return 0; +} + +static struct ast_custom_function replace_function = { + .name = "REPLACE", + .read = replace, +}; + static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len) { @@ -1247,6 +1314,7 @@ res |= ast_custom_function_unregister(&fieldqty_function); res |= ast_custom_function_unregister(&filter_function); + res |= ast_custom_function_unregister(&replace_function); res |= ast_custom_function_unregister(&listfilter_function); res |= ast_custom_function_unregister(®ex_function); res |= ast_custom_function_unregister(&array_function); @@ -1275,6 +1343,7 @@ res |= ast_custom_function_register(&fieldqty_function); res |= ast_custom_function_register(&filter_function); + res |= ast_custom_function_register(&replace_function); res |= ast_custom_function_register(&listfilter_function); res |= ast_custom_function_register(®ex_function); res |= ast_custom_function_register(&array_function);