Merged revisions 37441-37442 via svnmerge from
[asterisk-bristuff.git] / funcs / func_cut.c
blob3df16de18231b3114e1b91386840ad4a0d1e093c
1 /*
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.
18 /*! \file
20 * \brief CUT function
22 * \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
24 * \ingroup functions
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/options.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/app.h"
44 /* Maximum length of any variable */
45 #define MAXRESULT 1024
48 LOCAL_USER_DECL;
50 struct sortable_keys {
51 char *key;
52 float value;
55 static int sort_subroutine(const void *arg1, const void *arg2)
57 const struct sortable_keys *one=arg1, *two=arg2;
58 if (one->value < two->value)
59 return -1;
60 else if (one->value == two->value)
61 return 0;
62 else
63 return 1;
66 #define ERROR_NOARG (-1)
67 #define ERROR_NOMEM (-2)
68 #define ERROR_USAGE (-3)
70 static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
72 char *strings, *ptrkey, *ptrvalue;
73 int count=1, count2, element_count=0;
74 struct sortable_keys *sortable_keys;
76 memset(buffer, 0, buflen);
78 if (!data)
79 return ERROR_NOARG;
81 strings = ast_strdupa(data);
83 for (ptrkey = strings; *ptrkey; ptrkey++) {
84 if (*ptrkey == '|')
85 count++;
88 sortable_keys = alloca(count * sizeof(struct sortable_keys));
90 memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
92 /* Parse each into a struct */
93 count2 = 0;
94 while ((ptrkey = strsep(&strings, "|"))) {
95 ptrvalue = index(ptrkey, ':');
96 if (!ptrvalue) {
97 count--;
98 continue;
100 *ptrvalue++ = '\0';
101 sortable_keys[count2].key = ptrkey;
102 sscanf(ptrvalue, "%f", &sortable_keys[count2].value);
103 count2++;
106 /* Sort the structs */
107 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
109 for (count2 = 0; count2 < count; count2++) {
110 int blen = strlen(buffer);
111 if (element_count++) {
112 strncat(buffer + blen, ",", buflen - blen - 1);
113 blen++;
115 strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
118 return 0;
121 static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
123 char *parse;
124 AST_DECLARE_APP_ARGS(args,
125 AST_APP_ARG(varname);
126 AST_APP_ARG(delimiter);
127 AST_APP_ARG(field);
130 memset(buffer, 0, buflen);
132 parse = ast_strdupa(data);
134 AST_STANDARD_APP_ARGS(args, parse);
136 /* Check and parse arguments */
137 if(args.argc < 3){
138 return ERROR_NOARG;
139 } else {
140 char d, ds[2];
141 char *tmp = alloca(strlen(args.varname) + 4);
142 char varvalue[MAXRESULT], *tmp2=varvalue;
144 if (tmp) {
145 snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname);
146 memset(varvalue, 0, sizeof(varvalue));
147 } else {
148 return ERROR_NOMEM;
151 d = args.delimiter[0] ? args.delimiter[0] : '-';
153 /* String form of the delimiter, for use with strsep(3) */
154 snprintf(ds, sizeof(ds), "%c", d);
156 pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
158 if (tmp2) {
159 int curfieldnum = 1;
160 while (tmp2 != NULL && args.field != NULL) {
161 char *nextgroup = strsep(&(args.field), "&");
162 int num1 = 0, num2 = MAXRESULT;
163 char trashchar;
165 if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
166 /* range with both start and end */
167 } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
168 /* range with end */
169 num1 = 0;
170 } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
171 /* range with start */
172 num2 = MAXRESULT;
173 } else if (sscanf(nextgroup, "%d", &num1) == 1) {
174 /* single number */
175 num2 = num1;
176 } else {
177 return ERROR_USAGE;
180 /* Get to start, if any */
181 if (num1 > 0) {
182 while (tmp2 != (char *)NULL + 1 && curfieldnum < num1) {
183 tmp2 = index(tmp2, d) + 1;
184 curfieldnum++;
188 /* Most frequent problem is the expectation of reordering fields */
189 if ((num1 > 0) && (curfieldnum > num1))
190 ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
192 /* Re-null tmp2 if we added 1 to NULL */
193 if (tmp2 == (char *)NULL + 1)
194 tmp2 = NULL;
196 /* Output fields until we either run out of fields or num2 is reached */
197 while (tmp2 != NULL && curfieldnum <= num2) {
198 char *tmp3 = strsep(&tmp2, ds);
199 int curlen = strlen(buffer);
201 if (curlen)
202 snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
203 else
204 snprintf(buffer, buflen, "%s", tmp3);
206 curfieldnum++;
211 return 0;
214 static int acf_sort_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
216 struct localuser *u;
217 int ret = -1;
219 LOCAL_USER_ADD(u);
221 switch (sort_internal(chan, data, buf, len)) {
222 case ERROR_NOARG:
223 ast_log(LOG_ERROR, "SORT() requires an argument\n");
224 break;
225 case ERROR_NOMEM:
226 ast_log(LOG_ERROR, "Out of memory\n");
227 break;
228 case 0:
229 ret = 0;
230 break;
231 default:
232 ast_log(LOG_ERROR, "Unknown internal error\n");
234 LOCAL_USER_REMOVE(u);
236 return ret;
239 static int acf_cut_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
241 int ret = -1;
242 struct localuser *u;
244 LOCAL_USER_ADD(u);
246 switch (cut_internal(chan, data, buf, len)) {
247 case ERROR_NOARG:
248 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
249 break;
250 case ERROR_NOMEM:
251 ast_log(LOG_ERROR, "Out of memory\n");
252 break;
253 case ERROR_USAGE:
254 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
255 break;
256 case 0:
257 ret = 0;
258 break;
259 default:
260 ast_log(LOG_ERROR, "Unknown internal error\n");
262 LOCAL_USER_REMOVE(u);
264 return ret;
267 struct ast_custom_function acf_sort = {
268 .name = "SORT",
269 .synopsis = "Sorts a list of key/vals into a list of keys, based upon the vals",
270 .syntax = "SORT(key1:val1[...][,keyN:valN])",
271 .desc =
272 "Takes a comma-separated list of keys and values, each separated by a colon, and returns a\n"
273 "comma-separated list of the keys, sorted by their values. Values will be evaluated as\n"
274 "floating-point numbers.\n",
275 .read = acf_sort_exec,
278 struct ast_custom_function acf_cut = {
279 .name = "CUT",
280 .synopsis = "Slices and dices strings, based upon a named delimiter.",
281 .syntax = "CUT(<varname>,<char-delim>,<range-spec>)",
282 .desc =
283 " varname - variable you want cut\n"
284 " char-delim - defaults to '-'\n"
285 " range-spec - number of the field you want (1-based offset)\n"
286 " may also be specified as a range (with -)\n"
287 " or group of ranges and fields (with &)\n",
288 .read = acf_cut_exec,
291 static int unload_module(void *mod)
293 int res = 0;
295 res |= ast_custom_function_unregister(&acf_cut);
296 res |= ast_custom_function_unregister(&acf_sort);
298 STANDARD_HANGUP_LOCALUSERS;
300 return res;
303 static int load_module(void *mod)
305 int res = 0;
307 res |= ast_custom_function_register(&acf_cut);
308 res |= ast_custom_function_register(&acf_sort);
310 return res;
313 static const char *description(void)
315 return "Cut out information from a string";
318 static const char *key(void)
320 return ASTERISK_GPL_KEY;
323 STD_MOD(MOD_1, NULL, NULL, NULL);