As pointed out on the -dev list, actually use the result of find_peer() so that
[asterisk-bristuff.git] / funcs / func_cut.c
blob169fed6b5b6f91a0448fac4e254cde79614f1cad
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 "asterisk/file.h"
32 #include "asterisk/channel.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/module.h"
35 #include "asterisk/app.h"
37 /* Maximum length of any variable */
38 #define MAXRESULT 1024
40 struct sortable_keys {
41 char *key;
42 float value;
45 static int sort_subroutine(const void *arg1, const void *arg2)
47 const struct sortable_keys *one=arg1, *two=arg2;
48 if (one->value < two->value)
49 return -1;
50 else if (one->value == two->value)
51 return 0;
52 else
53 return 1;
56 #define ERROR_NOARG (-1)
57 #define ERROR_NOMEM (-2)
58 #define ERROR_USAGE (-3)
60 static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
62 char *strings, *ptrkey, *ptrvalue;
63 int count=1, count2, element_count=0;
64 struct sortable_keys *sortable_keys;
66 *buffer = '\0';
68 if (!data)
69 return ERROR_NOARG;
71 strings = ast_strdupa(data);
73 for (ptrkey = strings; *ptrkey; ptrkey++) {
74 if (*ptrkey == ',')
75 count++;
78 sortable_keys = alloca(count * sizeof(struct sortable_keys));
80 memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
82 /* Parse each into a struct */
83 count2 = 0;
84 while ((ptrkey = strsep(&strings, ","))) {
85 ptrvalue = index(ptrkey, ':');
86 if (!ptrvalue) {
87 count--;
88 continue;
90 *ptrvalue++ = '\0';
91 sortable_keys[count2].key = ptrkey;
92 sscanf(ptrvalue, "%f", &sortable_keys[count2].value);
93 count2++;
96 /* Sort the structs */
97 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
99 for (count2 = 0; count2 < count; count2++) {
100 int blen = strlen(buffer);
101 if (element_count++) {
102 strncat(buffer + blen, ",", buflen - blen - 1);
103 blen++;
105 strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
108 return 0;
111 static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
113 char *parse;
114 size_t delim_consumed;
115 AST_DECLARE_APP_ARGS(args,
116 AST_APP_ARG(varname);
117 AST_APP_ARG(delimiter);
118 AST_APP_ARG(field);
121 *buffer = '\0';
123 parse = ast_strdupa(data);
125 AST_STANDARD_APP_ARGS(args, parse);
127 /* Check and parse arguments */
128 if (args.argc < 3) {
129 return ERROR_NOARG;
130 } else {
131 char d, ds[2] = "";
132 char *tmp = alloca(strlen(args.varname) + 4);
133 char varvalue[MAXRESULT], *tmp2=varvalue;
135 if (tmp) {
136 snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname);
137 } else {
138 return ERROR_NOMEM;
141 if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed))
142 ast_copy_string(ds, "-", sizeof(ds));
144 /* String form of the delimiter, for use with strsep(3) */
145 d = *ds;
147 pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
149 if (tmp2) {
150 int curfieldnum = 1;
151 while (tmp2 != NULL && args.field != NULL) {
152 char *nextgroup = strsep(&(args.field), "&");
153 int num1 = 0, num2 = MAXRESULT;
154 char trashchar;
156 if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
157 /* range with both start and end */
158 } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
159 /* range with end */
160 num1 = 0;
161 } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
162 /* range with start */
163 num2 = MAXRESULT;
164 } else if (sscanf(nextgroup, "%d", &num1) == 1) {
165 /* single number */
166 num2 = num1;
167 } else {
168 return ERROR_USAGE;
171 /* Get to start, if any */
172 if (num1 > 0) {
173 while (tmp2 != (char *)NULL + 1 && curfieldnum < num1) {
174 tmp2 = index(tmp2, d) + 1;
175 curfieldnum++;
179 /* Most frequent problem is the expectation of reordering fields */
180 if ((num1 > 0) && (curfieldnum > num1))
181 ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
183 /* Re-null tmp2 if we added 1 to NULL */
184 if (tmp2 == (char *)NULL + 1)
185 tmp2 = NULL;
187 /* Output fields until we either run out of fields or num2 is reached */
188 while (tmp2 != NULL && curfieldnum <= num2) {
189 char *tmp3 = strsep(&tmp2, ds);
190 int curlen = strlen(buffer);
192 if (curlen)
193 snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
194 else
195 snprintf(buffer, buflen, "%s", tmp3);
197 curfieldnum++;
202 return 0;
205 static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
207 int ret = -1;
209 switch (sort_internal(chan, data, buf, len)) {
210 case ERROR_NOARG:
211 ast_log(LOG_ERROR, "SORT() requires an argument\n");
212 break;
213 case ERROR_NOMEM:
214 ast_log(LOG_ERROR, "Out of memory\n");
215 break;
216 case 0:
217 ret = 0;
218 break;
219 default:
220 ast_log(LOG_ERROR, "Unknown internal error\n");
223 return ret;
226 static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
228 int ret = -1;
230 if (chan)
231 ast_autoservice_start(chan);
233 switch (cut_internal(chan, data, buf, len)) {
234 case ERROR_NOARG:
235 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
236 break;
237 case ERROR_NOMEM:
238 ast_log(LOG_ERROR, "Out of memory\n");
239 break;
240 case ERROR_USAGE:
241 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
242 break;
243 case 0:
244 ret = 0;
245 break;
246 default:
247 ast_log(LOG_ERROR, "Unknown internal error\n");
250 if (chan)
251 ast_autoservice_stop(chan);
253 return ret;
256 struct ast_custom_function acf_sort = {
257 .name = "SORT",
258 .synopsis = "Sorts a list of key/vals into a list of keys, based upon the vals",
259 .syntax = "SORT(key1:val1[...][,keyN:valN])",
260 .desc =
261 "Takes a comma-separated list of keys and values, each separated by a colon, and returns a\n"
262 "comma-separated list of the keys, sorted by their values. Values will be evaluated as\n"
263 "floating-point numbers.\n",
264 .read = acf_sort_exec,
267 struct ast_custom_function acf_cut = {
268 .name = "CUT",
269 .synopsis = "Slices and dices strings, based upon a named delimiter.",
270 .syntax = "CUT(<varname>,<char-delim>,<range-spec>)",
271 .desc =
272 " varname - variable you want cut\n"
273 " char-delim - defaults to '-'\n"
274 " range-spec - number of the field you want (1-based offset)\n"
275 " may also be specified as a range (with -)\n"
276 " or group of ranges and fields (with &)\n",
277 .read = acf_cut_exec,
280 static int unload_module(void)
282 int res = 0;
284 res |= ast_custom_function_unregister(&acf_cut);
285 res |= ast_custom_function_unregister(&acf_sort);
287 return res;
290 static int load_module(void)
292 int res = 0;
294 res |= ast_custom_function_register(&acf_cut);
295 res |= ast_custom_function_register(&acf_sort);
297 return res;
300 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");