Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (c) 2003-2006 Tilghman Lesher. All rights reserved.
* Tilghman Lesher <app_cut__v003@the-tilghman.com>
*
* This code is released by the author with no restrictions on usage.
*
* 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.
*
*/
Tilghman Lesher
committed
* \brief CUT function
* \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
*
Tilghman Lesher
committed
* \ingroup functions
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
Kevin P. Fleming
committed
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*** DOCUMENTATION
<function name="SORT" language="en_US">
<synopsis>
Sorts a list of key/vals into a list of keys, based upon the vals.
</synopsis>
<syntax>
<parameter name="keyval" required="true" argsep=":">
<argument name="key1" required="true" />
<argument name="val1" required="true" />
</parameter>
<parameter name="keyvaln" multiple="true" argsep=":">
<argument name="key2" required="true" />
<argument name="val2" required="true" />
</parameter>
</syntax>
<description>
<para>Takes a comma-separated list of keys and values, each separated by a colon, and returns a
comma-separated list of the keys, sorted by their values. Values will be evaluated as
floating-point numbers.</para>
</description>
</function>
<function name="CUT" language="en_US">
<synopsis>
Slices and dices strings, based upon a named delimiter.
</synopsis>
<syntax>
<parameter name="varname" required="true">
<para>Variable you want cut</para>
</parameter>
<parameter name="char-delim" required="true">
<para>Delimiter, defaults to <literal>-</literal></para>
</parameter>
<parameter name="range-spec" required="true">
<para>Number of the field you want (1-based offset), may also be specified as a range (with <literal>-</literal>)
or group of ranges and fields (with <literal>&</literal>)</para>
</parameter>
</syntax>
<description>
<para>Cut out information from a string (<replaceable>varname</replaceable>), based upon a named delimiter.</para>
</description>
</function>
***/
/* Maximum length of any variable */
#define MAXRESULT 1024
struct sortable_keys {
char *key;
float value;
};
static int sort_subroutine(const void *arg1, const void *arg2)
const struct sortable_keys *one=arg1, *two=arg2;
return 1;
}
#define ERROR_NOARG (-1)
#define ERROR_NOMEM (-2)
#define ERROR_USAGE (-3)
static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
{
char *strings, *ptrkey, *ptrvalue;
int count=1, count2, element_count=0;
struct sortable_keys *sortable_keys;
return ERROR_NOARG;
Russell Bryant
committed
strings = ast_strdupa(data);
for (ptrkey = strings; *ptrkey; ptrkey++) {
Tilghman Lesher
committed
if (*ptrkey == ',')
count++;
}
sortable_keys = alloca(count * sizeof(struct sortable_keys));
memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
/* Parse each into a struct */
count2 = 0;
Tilghman Lesher
committed
while ((ptrkey = strsep(&strings, ","))) {
if (!ptrvalue) {
count--;
continue;
}
sortable_keys[count2].key = ptrkey;
sscanf(ptrvalue, "%f", &sortable_keys[count2].value);
count2++;
}
/* Sort the structs */
qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
for (count2 = 0; count2 < count; count2++) {
int blen = strlen(buffer);
if (element_count++) {
strncat(buffer + blen, ",", buflen - blen - 1);
strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
}
return 0;
}
static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
{
Tilghman Lesher
committed
size_t delim_consumed;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(varname);
AST_APP_ARG(delimiter);
AST_APP_ARG(field);
);
Russell Bryant
committed
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
Tilghman Lesher
committed
if (args.argc < 3) {
return ERROR_NOARG;
} else {
Tilghman Lesher
committed
char d, ds[2] = "";
char *tmp = alloca(strlen(args.varname) + 4);
snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname);
Tilghman Lesher
committed
if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed))
ast_copy_string(ds, "-", sizeof(ds));
/* String form of the delimiter, for use with strsep(3) */
Tilghman Lesher
committed
d = *ds;
pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
if (tmp2) {
int curfieldnum = 1;
while (tmp2 != NULL && args.field != NULL) {
char *nextgroup = strsep(&(args.field), "&");
int num1 = 0, num2 = MAXRESULT;
char trashchar;
if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
/* range with both start and end */
} else if (sscanf(nextgroup, "-%d", &num2) == 1) {
/* range with end */
num1 = 0;
} else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
/* range with start */
num2 = MAXRESULT;
} else if (sscanf(nextgroup, "%d", &num1) == 1) {
/* single number */
num2 = num1;
} else {
}
/* Get to start, if any */
if (num1 > 0) {
while (tmp2 != (char *)NULL + 1 && curfieldnum < num1) {
curfieldnum++;
}
}
/* Most frequent problem is the expectation of reordering fields */
ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
tmp2 = NULL;
/* Output fields until we either run out of fields or num2 is reached */
while (tmp2 != NULL && curfieldnum <= num2) {
int curlen = strlen(buffer);
snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
snprintf(buffer, buflen, "%s", tmp3);
}
return 0;
}
static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
switch (sort_internal(chan, data, buf, len)) {
case ERROR_NOARG:
ast_log(LOG_ERROR, "SORT() requires an argument\n");
break;
case ERROR_NOMEM:
ast_log(LOG_ERROR, "Out of memory\n");
break;
case 0:
break;
default:
ast_log(LOG_ERROR, "Unknown internal error\n");
}
static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
switch (cut_internal(chan, data, buf, len)) {
case ERROR_NOARG:
ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
break;
case ERROR_NOMEM:
ast_log(LOG_ERROR, "Out of memory\n");
break;
case ERROR_USAGE:
Tilghman Lesher
committed
ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
break;
default:
ast_log(LOG_ERROR, "Unknown internal error\n");
}
}
struct ast_custom_function acf_sort = {
.name = "SORT",
.read = acf_sort_exec,
};
struct ast_custom_function acf_cut = {
.name = "CUT",
.read = acf_cut_exec,
};
static int unload_module(void)
int res = 0;
Russell Bryant
committed
res |= ast_custom_function_unregister(&acf_cut);
Russell Bryant
committed
res |= ast_custom_function_unregister(&acf_sort);
return res;
static int load_module(void)
int res = 0;
Russell Bryant
committed
res |= ast_custom_function_register(&acf_cut);
Russell Bryant
committed
res |= ast_custom_function_register(&acf_sort);
return res;
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");