1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* 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.
*
*/
/*! \file
*
* \brief CUT function
*
* \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
*
* \ingroup functions
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 370655 $")
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
/*** 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>
***/
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;
if (one->value < two->value)
return -1;
else if (one->value == two->value)
return 0;
else
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;
*buffer = '\0';
if (!data)
return ERROR_NOARG;
strings = ast_strdupa(data);
for (ptrkey = strings; *ptrkey; ptrkey++) {
if (*ptrkey == ',')
count++;
}
sortable_keys = ast_alloca(count * sizeof(struct sortable_keys));
memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
/* Parse each into a struct */
count2 = 0;
while ((ptrkey = strsep(&strings, ","))) {
ptrvalue = strchr(ptrkey, ':');
if (!ptrvalue) {
count--;
continue;
}
*ptrvalue++ = '\0';
sortable_keys[count2].key = ptrkey;
sscanf(ptrvalue, "%30f", &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);
blen++;
}
strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
}
return 0;
}
static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
{
char *parse, ds[2], *var_expr;
size_t delim_consumed;
struct ast_str *var_value;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(varname);
AST_APP_ARG(delimiter);
AST_APP_ARG(field);
);
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
/* Check arguments */
if (args.argc < 3) {
return ERROR_NOARG;
}
var_expr = ast_alloca(strlen(args.varname) + 4);
/* Get the value of the variable named in the 1st argument */
snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname);
var_value = ast_str_create(16);
ast_str_substitute_variables(&var_value, 0, chan, var_expr);
/* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */
if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) {
ast_copy_string(ds, "-", sizeof(ds));
}
ds[1] = '\0';
if (ast_str_strlen(var_value)) {
int curfieldnum = 1;
char *curfieldptr = ast_str_buffer(var_value);
int out_field_count = 0;
while (curfieldptr != NULL && args.field != NULL) {
char *next_range = strsep(&(args.field), "&");
int start_field, stop_field;
char trashchar;
if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) {
/* range with both start and end */
} else if (sscanf(next_range, "-%30d", &stop_field) == 1) {
/* range with end only */
start_field = 1;
} else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) {
/* range with start only */
stop_field = INT_MAX;
} else if (sscanf(next_range, "%30d", &start_field) == 1) {
/* single number */
stop_field = start_field;
} else {
/* invalid field spec */
ast_free(var_value);
return ERROR_USAGE;
}
/* Get to start, if not there already */
while (curfieldptr != NULL && curfieldnum < start_field) {
strsep(&curfieldptr, ds);
curfieldnum++;
}
/* Most frequent problem is the expectation of reordering fields */
if (curfieldnum > start_field) {
ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
}
/* Output fields until we either run out of fields or stop_field is reached */
while (curfieldptr != NULL && curfieldnum <= stop_field) {
char *field_value = strsep(&curfieldptr, ds);
ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value);
curfieldnum++;
}
}
}
ast_free(var_value);
return 0;
}
static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
int ret = -1;
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:
ret = 0;
break;
default:
ast_log(LOG_ERROR, "Unknown internal error\n");
}
return ret;
}
static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
int ret = -1;
struct ast_str *str = ast_str_create(16);
switch (cut_internal(chan, data, &str, 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:
ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
break;
case 0:
ret = 0;
ast_copy_string(buf, ast_str_buffer(str), len);
break;
default:
ast_log(LOG_ERROR, "Unknown internal error\n");
}
ast_free(str);
return ret;
}
static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
int ret = -1;
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:
ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
break;
case 0:
ret = 0;
break;
default:
ast_log(LOG_ERROR, "Unknown internal error\n");
}
return ret;
}
static struct ast_custom_function acf_sort = {
.name = "SORT",
.read = acf_sort_exec,
};
static struct ast_custom_function acf_cut = {
.name = "CUT",
.read = acf_cut_exec,
.read2 = acf_cut_exec2,
};
static int unload_module(void)
{
int res = 0;
res |= ast_custom_function_unregister(&acf_cut);
res |= ast_custom_function_unregister(&acf_sort);
return res;
}
static int load_module(void)
{
int res = 0;
res |= ast_custom_function_register(&acf_cut);
res |= ast_custom_function_register(&acf_sort);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");
|