Let's also include aclocal.m4
[asterisk-bristuff.git] / funcs / func_cut.c
blob51b9adc702900ae95e15236240efc94946a086f5
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
47 struct sortable_keys {
48 char *key;
49 float value;
52 static int sort_subroutine(const void *arg1, const void *arg2)
54 const struct sortable_keys *one=arg1, *two=arg2;
55 if (one->value < two->value)
56 return -1;
57 else if (one->value == two->value)
58 return 0;
59 else
60 return 1;
63 #define ERROR_NOARG (-1)
64 #define ERROR_NOMEM (-2)
65 #define ERROR_USAGE (-3)
67 static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
69 char *strings, *ptrkey, *ptrvalue;
70 int count=1, count2, element_count=0;
71 struct sortable_keys *sortable_keys;
73 memset(buffer, 0, buflen);
75 if (!data)
76 return ERROR_NOARG;
78 strings = ast_strdupa(data);
80 for (ptrkey = strings; *ptrkey; ptrkey++) {
81 if (*ptrkey == '|')
82 count++;
85 sortable_keys = alloca(count * sizeof(struct sortable_keys));
87 memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
89 /* Parse each into a struct */
90 count2 = 0;
91 while ((ptrkey = strsep(&strings, "|"))) {
92 ptrvalue = index(ptrkey, ':');
93 if (!ptrvalue) {
94 count--;
95 continue;
97 *ptrvalue++ = '\0';
98 sortable_keys[count2].key = ptrkey;
99 sscanf(ptrvalue, "%f", &sortable_keys[count2].value);
100 count2++;
103 /* Sort the structs */
104 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
106 for (count2 = 0; count2 < count; count2++) {
107 int blen = strlen(buffer);
108 if (element_count++) {
109 strncat(buffer + blen, ",", buflen - blen - 1);
110 blen++;
112 strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
115 return 0;
118 static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
120 char *parse;
121 AST_DECLARE_APP_ARGS(args,
122 AST_APP_ARG(varname);
123 AST_APP_ARG(delimiter);
124 AST_APP_ARG(field);
127 memset(buffer, 0, buflen);
129 parse = ast_strdupa(data);
131 AST_STANDARD_APP_ARGS(args, parse);
133 /* Check and parse arguments */
134 if(args.argc < 3){
135 return ERROR_NOARG;
136 } else {
137 char d, ds[2];
138 char *tmp = alloca(strlen(args.varname) + 4);
139 char varvalue[MAXRESULT], *tmp2=varvalue;
141 if (tmp) {
142 snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname);
143 memset(varvalue, 0, sizeof(varvalue));
144 } else {
145 return ERROR_NOMEM;
148 if (args.delimiter[0] == '\\') {
149 if (args.delimiter[1] == 'n')
150 d = '\n';
151 else if (args.delimiter[1] == 't')
152 d = '\t';
153 else if (args.delimiter[1] == 'r')
154 d = '\r';
155 else if (args.delimiter[1])
156 d = args.delimiter[1];
157 else
158 d = '-';
159 } else if (args.delimiter[0])
160 d = args.delimiter[0];
161 else
162 d = '-';
164 /* String form of the delimiter, for use with strsep(3) */
165 snprintf(ds, sizeof(ds), "%c", d);
167 pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
169 if (tmp2) {
170 int curfieldnum = 1;
171 while (tmp2 != NULL && args.field != NULL) {
172 char *nextgroup = strsep(&(args.field), "&");
173 int num1 = 0, num2 = MAXRESULT;
174 char trashchar;
176 if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
177 /* range with both start and end */
178 } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
179 /* range with end */
180 num1 = 0;
181 } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
182 /* range with start */
183 num2 = MAXRESULT;
184 } else if (sscanf(nextgroup, "%d", &num1) == 1) {
185 /* single number */
186 num2 = num1;
187 } else {
188 return ERROR_USAGE;
191 /* Get to start, if any */
192 if (num1 > 0) {
193 while (tmp2 != (char *)NULL + 1 && curfieldnum < num1) {
194 tmp2 = index(tmp2, d) + 1;
195 curfieldnum++;
199 /* Most frequent problem is the expectation of reordering fields */
200 if ((num1 > 0) && (curfieldnum > num1))
201 ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
203 /* Re-null tmp2 if we added 1 to NULL */
204 if (tmp2 == (char *)NULL + 1)
205 tmp2 = NULL;
207 /* Output fields until we either run out of fields or num2 is reached */
208 while (tmp2 != NULL && curfieldnum <= num2) {
209 char *tmp3 = strsep(&tmp2, ds);
210 int curlen = strlen(buffer);
212 if (curlen)
213 snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
214 else
215 snprintf(buffer, buflen, "%s", tmp3);
217 curfieldnum++;
222 return 0;
225 static int acf_sort_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
227 struct ast_module_user *u;
228 int ret = -1;
230 u = ast_module_user_add(chan);
232 switch (sort_internal(chan, data, buf, len)) {
233 case ERROR_NOARG:
234 ast_log(LOG_ERROR, "SORT() requires an argument\n");
235 break;
236 case ERROR_NOMEM:
237 ast_log(LOG_ERROR, "Out of memory\n");
238 break;
239 case 0:
240 ret = 0;
241 break;
242 default:
243 ast_log(LOG_ERROR, "Unknown internal error\n");
246 ast_module_user_remove(u);
248 return ret;
251 static int acf_cut_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
253 int ret = -1;
254 struct ast_module_user *u = NULL;
256 if (chan) {
257 ast_autoservice_start(chan);
258 u = ast_module_user_add(chan);
261 switch (cut_internal(chan, data, buf, len)) {
262 case ERROR_NOARG:
263 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
264 break;
265 case ERROR_NOMEM:
266 ast_log(LOG_ERROR, "Out of memory\n");
267 break;
268 case ERROR_USAGE:
269 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
270 break;
271 case 0:
272 ret = 0;
273 break;
274 default:
275 ast_log(LOG_ERROR, "Unknown internal error\n");
278 if (chan) {
279 ast_module_user_remove(u);
280 ast_autoservice_stop(chan);
283 return ret;
286 struct ast_custom_function acf_sort = {
287 .name = "SORT",
288 .synopsis = "Sorts a list of key/vals into a list of keys, based upon the vals",
289 .syntax = "SORT(key1:val1[...][,keyN:valN])",
290 .desc =
291 "Takes a comma-separated list of keys and values, each separated by a colon, and returns a\n"
292 "comma-separated list of the keys, sorted by their values. Values will be evaluated as\n"
293 "floating-point numbers.\n",
294 .read = acf_sort_exec,
297 struct ast_custom_function acf_cut = {
298 .name = "CUT",
299 .synopsis = "Slices and dices strings, based upon a named delimiter.",
300 .syntax = "CUT(<varname>,<char-delim>,<range-spec>)",
301 .desc =
302 " varname - variable you want cut\n"
303 " char-delim - defaults to '-'\n"
304 " range-spec - number of the field you want (1-based offset)\n"
305 " may also be specified as a range (with -)\n"
306 " or group of ranges and fields (with &)\n",
307 .read = acf_cut_exec,
310 static int unload_module(void)
312 int res = 0;
314 res |= ast_custom_function_unregister(&acf_cut);
315 res |= ast_custom_function_unregister(&acf_sort);
317 ast_module_user_hangup_all();
319 return res;
322 static int load_module(void)
324 int res = 0;
326 res |= ast_custom_function_register(&acf_cut);
327 res |= ast_custom_function_register(&acf_sort);
329 return res;
332 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");