2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2003-2006 Tilghman Lesher. All rights reserved.
6 * Tilghman Lesher <app_cut__v003@the-tilghman.com>
8 * This code is released by the author with no restrictions on usage.
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
22 * \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include "asterisk/file.h"
32 #include "asterisk/channel.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/module.h"
35 #include "asterisk/app.h"
38 <function name="SORT" language="en_US">
40 Sorts a list of key/vals into a list of keys, based upon the vals.
43 <parameter name="keyval" required="true" argsep=":">
44 <argument name="key1" required="true" />
45 <argument name="val1" required="true" />
47 <parameter name="keyvaln" multiple="true" argsep=":">
48 <argument name="key2" required="true" />
49 <argument name="val2" required="true" />
53 <para>Takes a comma-separated list of keys and values, each separated by a colon, and returns a
54 comma-separated list of the keys, sorted by their values. Values will be evaluated as
55 floating-point numbers.</para>
58 <function name="CUT" language="en_US">
60 Slices and dices strings, based upon a named delimiter.
63 <parameter name="varname" required="true">
64 <para>Variable you want cut</para>
66 <parameter name="char-delim" required="true">
67 <para>Delimiter, defaults to <literal>-</literal></para>
69 <parameter name="range-spec" required="true">
70 <para>Number of the field you want (1-based offset), may also be specified as a range (with <literal>-</literal>)
71 or group of ranges and fields (with <literal>&</literal>)</para>
75 <para>Cut out information from a string (<replaceable>varname</replaceable>), based upon a named delimiter.</para>
80 struct sortable_keys {
85 static int sort_subroutine(const void *arg1, const void *arg2)
87 const struct sortable_keys *one=arg1, *two=arg2;
88 if (one->value < two->value)
90 else if (one->value == two->value)
96 #define ERROR_NOARG (-1)
97 #define ERROR_NOMEM (-2)
98 #define ERROR_USAGE (-3)
100 static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
102 char *strings, *ptrkey, *ptrvalue;
103 int count=1, count2, element_count=0;
104 struct sortable_keys *sortable_keys;
111 strings = ast_strdupa(data);
113 for (ptrkey = strings; *ptrkey; ptrkey++) {
118 sortable_keys = alloca(count * sizeof(struct sortable_keys));
120 memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
122 /* Parse each into a struct */
124 while ((ptrkey = strsep(&strings, ","))) {
125 ptrvalue = strchr(ptrkey, ':');
131 sortable_keys[count2].key = ptrkey;
132 sscanf(ptrvalue, "%f", &sortable_keys[count2].value);
136 /* Sort the structs */
137 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
139 for (count2 = 0; count2 < count; count2++) {
140 int blen = strlen(buffer);
141 if (element_count++) {
142 strncat(buffer + blen, ",", buflen - blen - 1);
145 strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
151 static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
153 char *parse, ds[2], *var_expr;
154 size_t delim_consumed;
155 struct ast_str *var_value;
156 AST_DECLARE_APP_ARGS(args,
157 AST_APP_ARG(varname);
158 AST_APP_ARG(delimiter);
162 parse = ast_strdupa(data);
164 AST_STANDARD_APP_ARGS(args, parse);
166 /* Check arguments */
169 } else if (!(var_expr = alloca(strlen(args.varname) + 4))) {
173 /* Get the value of the variable named in the 1st argument */
174 snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname);
175 var_value = ast_str_create(16);
176 ast_str_substitute_variables(&var_value, 0, chan, var_expr);
178 /* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */
179 if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) {
180 ast_copy_string(ds, "-", sizeof(ds));
184 if (ast_str_strlen(var_value)) {
186 char *curfieldptr = ast_str_buffer(var_value);
187 int out_field_count = 0;
189 while (curfieldptr != NULL && args.field != NULL) {
190 char *next_range = strsep(&(args.field), "&");
191 int start_field, stop_field;
194 if (sscanf(next_range, "%d-%d", &start_field, &stop_field) == 2) {
195 /* range with both start and end */
196 } else if (sscanf(next_range, "-%d", &stop_field) == 1) {
197 /* range with end only */
199 } else if ((sscanf(next_range, "%d%c", &start_field, &trashchar) == 2) && (trashchar == '-')) {
200 /* range with start only */
201 stop_field = INT_MAX;
202 } else if (sscanf(next_range, "%d", &start_field) == 1) {
204 stop_field = start_field;
206 /* invalid field spec */
211 /* Get to start, if not there already */
212 while (curfieldptr != NULL && curfieldnum < start_field) {
213 strsep(&curfieldptr, ds);
217 /* Most frequent problem is the expectation of reordering fields */
218 if (curfieldnum > start_field) {
219 ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
222 /* Output fields until we either run out of fields or stop_field is reached */
223 while (curfieldptr != NULL && curfieldnum <= stop_field) {
224 char *field_value = strsep(&curfieldptr, ds);
225 ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value);
234 static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
238 switch (sort_internal(chan, data, buf, len)) {
240 ast_log(LOG_ERROR, "SORT() requires an argument\n");
243 ast_log(LOG_ERROR, "Out of memory\n");
249 ast_log(LOG_ERROR, "Unknown internal error\n");
255 static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
258 struct ast_str *str = ast_str_create(16);
260 switch (cut_internal(chan, data, &str, len)) {
262 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
265 ast_log(LOG_ERROR, "Out of memory\n");
268 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
272 ast_copy_string(buf, ast_str_buffer(str), len);
275 ast_log(LOG_ERROR, "Unknown internal error\n");
281 static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
285 switch (cut_internal(chan, data, buf, len)) {
287 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
290 ast_log(LOG_ERROR, "Out of memory\n");
293 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
299 ast_log(LOG_ERROR, "Unknown internal error\n");
305 static struct ast_custom_function acf_sort = {
307 .read = acf_sort_exec,
310 static struct ast_custom_function acf_cut = {
312 .read = acf_cut_exec,
313 .read2 = acf_cut_exec2,
316 static int unload_module(void)
320 res |= ast_custom_function_unregister(&acf_cut);
321 res |= ast_custom_function_unregister(&acf_sort);
326 static int load_module(void)
330 res |= ast_custom_function_register(&acf_cut);
331 res |= ast_custom_function_register(&acf_sort);
336 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");