Index: funcs/func_strings.c =================================================================== --- funcs/func_strings.c (revision 22123) +++ funcs/func_strings.c (working copy) @@ -422,6 +422,68 @@ .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[len]; + + 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.string))) + ast_copy_string(buf, args.string, len); + + for (pads = atoi(args.width) - strlen(args.string); pads > 0; --pads) { + if (!strncat(buf, padchar, len - strlen(buf) - 1)) { + ast_log(LOG_ERROR, + "Could not add padding to string\n"); + return -1; + } + } + + if (!args.direction || strncasecmp(args.direction, "left", sizeof(args.string))) + strncat(buf, args.string, len - strlen(buf) - 1); + + 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 +500,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 +519,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; }