Index: funcs/func_strings.c =================================================================== --- funcs/func_strings.c (revision 24424) +++ funcs/func_strings.c (working copy) @@ -422,6 +422,63 @@ .desc = "Example: ${KEYPADHASH(Les)} returns \"537\"\n", }; +static int justify(struct ast_channel *chan, char *cmd, char *data, + char *buf, size_t len) +{ + strcpy(buf, ""); + + AST_DECLARE_APP_ARGS(args, + AST_APP_ARG(string); + AST_APP_ARG(width); + AST_APP_ARG(direction); + AST_APP_ARG(padchar); + ); + + int pads; + char padchar[2]; + + if (!data) { + ast_log (LOG_ERROR, + "JUSTIFY requires an argument.\n"); + return -1; + } + + AST_STANDARD_APP_ARGS(args, data); + + if (ast_strlen_zero(args.string) || ast_strlen_zero(args.width)) { + ast_log(LOG_ERROR, + "Required format not supplied to JUSTIFY(|[|][|])"); + return -1; + } + + if (!ast_strlen_zero(args.padchar) && strlen(args.padchar) > 1) { + ast_log(LOG_ERROR, "padchar must be a single character!\n"); + return -1; + } + + ast_copy_string(padchar, S_OR(args.padchar, " "), sizeof(padchar));; + + if (args.direction && !strncasecmp(args.direction, "left", sizeof(args.direction))) + ast_build_string(&buf, &len, "%s", args.string); + + for (pads = atoi(args.width) - strlen(args.string); pads > 0; --pads) + ast_build_string(&buf, &len, "%s", padchar); + + if (!args.direction || strncasecmp(args.direction, "left", sizeof(args.direction))) + ast_build_string(&buf, &len, "%s", args.string); + + return 0; +} + +static struct ast_custom_function justify_function = { + .name = "JUSTIFY", + .synopsis = "Left or right (default) justify the string padded with an optional characater (default ' ').", + .syntax = "JUSTIFY(|[|][|])", + .read = justify, + .desc = "Example: ${JUSTIFY(hello,7,right,X) should return \"XXhello\"\n", +}; + + static char *tdesc = "String handling dialplan functions"; static int unload_module(void *mod) @@ -438,6 +495,7 @@ res |= ast_custom_function_unregister(&strptime_function); res |= ast_custom_function_unregister(&eval_function); res |= ast_custom_function_unregister(&keypadhash_function); + res |= ast_custom_function_unregister(&justify_function); return res; } @@ -456,6 +514,7 @@ res |= ast_custom_function_register(&strptime_function); res |= ast_custom_function_register(&eval_function); res |= ast_custom_function_register(&keypadhash_function); + res |= ast_custom_function_register(&justify_function); return res; }