CUT function. More...
#include "asterisk.h"#include "asterisk/file.h"#include "asterisk/channel.h"#include "asterisk/pbx.h"#include "asterisk/module.h"#include "asterisk/app.h"
Go to the source code of this file.
Data Structures | |
| struct | sortable_keys |
Defines | |
| #define | ERROR_NOARG (-1) |
| #define | ERROR_NOMEM (-2) |
| #define | ERROR_USAGE (-3) |
Functions | |
| static int | acf_cut_exec (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| static int | acf_cut_exec2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| static int | acf_sort_exec (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Cut out information from a string") | |
| static int | cut_internal (struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen) |
| static int | load_module (void) |
| static int | sort_internal (struct ast_channel *chan, char *data, char *buffer, size_t buflen) |
| static int | sort_subroutine (const void *arg1, const void *arg2) |
| static int | unload_module (void) |
Variables | |
| static struct ast_custom_function | acf_cut |
| static struct ast_custom_function | acf_sort |
CUT function.
Definition in file func_cut.c.
| #define ERROR_NOARG (-1) |
Definition at line 100 of file func_cut.c.
Referenced by acf_cut_exec(), acf_cut_exec2(), acf_sort_exec(), cut_internal(), and sort_internal().
| #define ERROR_NOMEM (-2) |
Definition at line 101 of file func_cut.c.
Referenced by acf_cut_exec(), acf_cut_exec2(), acf_sort_exec(), and cut_internal().
| #define ERROR_USAGE (-3) |
Definition at line 102 of file func_cut.c.
Referenced by acf_cut_exec(), acf_cut_exec2(), and cut_internal().
| static int acf_cut_exec | ( | struct ast_channel * | chan, | |
| const char * | cmd, | |||
| char * | data, | |||
| char * | buf, | |||
| size_t | len | |||
| ) | [static] |
Definition at line 259 of file func_cut.c.
References ast_copy_string(), ast_free, ast_log(), ast_str_buffer(), ast_str_create(), cut_internal(), ERROR_NOARG, ERROR_NOMEM, ERROR_USAGE, LOG_ERROR, and str.
00260 { 00261 int ret = -1; 00262 struct ast_str *str = ast_str_create(16); 00263 00264 switch (cut_internal(chan, data, &str, len)) { 00265 case ERROR_NOARG: 00266 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n"); 00267 break; 00268 case ERROR_NOMEM: 00269 ast_log(LOG_ERROR, "Out of memory\n"); 00270 break; 00271 case ERROR_USAGE: 00272 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n"); 00273 break; 00274 case 0: 00275 ret = 0; 00276 ast_copy_string(buf, ast_str_buffer(str), len); 00277 break; 00278 default: 00279 ast_log(LOG_ERROR, "Unknown internal error\n"); 00280 } 00281 ast_free(str); 00282 return ret; 00283 }
| static int acf_cut_exec2 | ( | struct ast_channel * | chan, | |
| const char * | cmd, | |||
| char * | data, | |||
| struct ast_str ** | buf, | |||
| ssize_t | len | |||
| ) | [static] |
Definition at line 285 of file func_cut.c.
References ast_log(), cut_internal(), ERROR_NOARG, ERROR_NOMEM, ERROR_USAGE, and LOG_ERROR.
00286 { 00287 int ret = -1; 00288 00289 switch (cut_internal(chan, data, buf, len)) { 00290 case ERROR_NOARG: 00291 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n"); 00292 break; 00293 case ERROR_NOMEM: 00294 ast_log(LOG_ERROR, "Out of memory\n"); 00295 break; 00296 case ERROR_USAGE: 00297 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n"); 00298 break; 00299 case 0: 00300 ret = 0; 00301 break; 00302 default: 00303 ast_log(LOG_ERROR, "Unknown internal error\n"); 00304 } 00305 00306 return ret; 00307 }
| static int acf_sort_exec | ( | struct ast_channel * | chan, | |
| const char * | cmd, | |||
| char * | data, | |||
| char * | buf, | |||
| size_t | len | |||
| ) | [static] |
Definition at line 238 of file func_cut.c.
References ast_log(), ERROR_NOARG, ERROR_NOMEM, LOG_ERROR, and sort_internal().
00239 { 00240 int ret = -1; 00241 00242 switch (sort_internal(chan, data, buf, len)) { 00243 case ERROR_NOARG: 00244 ast_log(LOG_ERROR, "SORT() requires an argument\n"); 00245 break; 00246 case ERROR_NOMEM: 00247 ast_log(LOG_ERROR, "Out of memory\n"); 00248 break; 00249 case 0: 00250 ret = 0; 00251 break; 00252 default: 00253 ast_log(LOG_ERROR, "Unknown internal error\n"); 00254 } 00255 00256 return ret; 00257 }
| AST_MODULE_INFO_STANDARD | ( | ASTERISK_GPL_KEY | , | |
| "Cut out information from a string" | ||||
| ) |
| static int cut_internal | ( | struct ast_channel * | chan, | |
| char * | data, | |||
| struct ast_str ** | buf, | |||
| ssize_t | buflen | |||
| ) | [static] |
Definition at line 155 of file func_cut.c.
References args, AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_free, ast_get_encoded_char(), ast_log(), AST_STANDARD_APP_ARGS, ast_str_append(), ast_str_buffer(), ast_str_create(), ast_str_strlen(), ast_str_substitute_variables(), ast_strdupa, ERROR_NOARG, ERROR_NOMEM, ERROR_USAGE, LOG_WARNING, and parse().
Referenced by acf_cut_exec(), and acf_cut_exec2().
00156 { 00157 char *parse, ds[2], *var_expr; 00158 size_t delim_consumed; 00159 struct ast_str *var_value; 00160 AST_DECLARE_APP_ARGS(args, 00161 AST_APP_ARG(varname); 00162 AST_APP_ARG(delimiter); 00163 AST_APP_ARG(field); 00164 ); 00165 00166 parse = ast_strdupa(data); 00167 00168 AST_STANDARD_APP_ARGS(args, parse); 00169 00170 /* Check arguments */ 00171 if (args.argc < 3) { 00172 return ERROR_NOARG; 00173 } else if (!(var_expr = alloca(strlen(args.varname) + 4))) { 00174 return ERROR_NOMEM; 00175 } 00176 00177 /* Get the value of the variable named in the 1st argument */ 00178 snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname); 00179 var_value = ast_str_create(16); 00180 ast_str_substitute_variables(&var_value, 0, chan, var_expr); 00181 00182 /* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */ 00183 if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) { 00184 ast_copy_string(ds, "-", sizeof(ds)); 00185 } 00186 ds[1] = '\0'; 00187 00188 if (ast_str_strlen(var_value)) { 00189 int curfieldnum = 1; 00190 char *curfieldptr = ast_str_buffer(var_value); 00191 int out_field_count = 0; 00192 00193 while (curfieldptr != NULL && args.field != NULL) { 00194 char *next_range = strsep(&(args.field), "&"); 00195 int start_field, stop_field; 00196 char trashchar; 00197 00198 if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) { 00199 /* range with both start and end */ 00200 } else if (sscanf(next_range, "-%30d", &stop_field) == 1) { 00201 /* range with end only */ 00202 start_field = 1; 00203 } else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) { 00204 /* range with start only */ 00205 stop_field = INT_MAX; 00206 } else if (sscanf(next_range, "%30d", &start_field) == 1) { 00207 /* single number */ 00208 stop_field = start_field; 00209 } else { 00210 /* invalid field spec */ 00211 ast_free(var_value); 00212 return ERROR_USAGE; 00213 } 00214 00215 /* Get to start, if not there already */ 00216 while (curfieldptr != NULL && curfieldnum < start_field) { 00217 strsep(&curfieldptr, ds); 00218 curfieldnum++; 00219 } 00220 00221 /* Most frequent problem is the expectation of reordering fields */ 00222 if (curfieldnum > start_field) { 00223 ast_log(LOG_WARNING, "We're already past the field you wanted?\n"); 00224 } 00225 00226 /* Output fields until we either run out of fields or stop_field is reached */ 00227 while (curfieldptr != NULL && curfieldnum <= stop_field) { 00228 char *field_value = strsep(&curfieldptr, ds); 00229 ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value); 00230 curfieldnum++; 00231 } 00232 } 00233 } 00234 ast_free(var_value); 00235 return 0; 00236 }
| static int load_module | ( | void | ) | [static] |
Definition at line 330 of file func_cut.c.
References ast_custom_function_register.
00331 { 00332 int res = 0; 00333 00334 res |= ast_custom_function_register(&acf_cut); 00335 res |= ast_custom_function_register(&acf_sort); 00336 00337 return res; 00338 }
| static int sort_internal | ( | struct ast_channel * | chan, | |
| char * | data, | |||
| char * | buffer, | |||
| size_t | buflen | |||
| ) | [static] |
Definition at line 104 of file func_cut.c.
References ast_strdupa, ERROR_NOARG, sortable_keys::key, sort_subroutine(), and value.
Referenced by acf_sort_exec().
00105 { 00106 char *strings, *ptrkey, *ptrvalue; 00107 int count=1, count2, element_count=0; 00108 struct sortable_keys *sortable_keys; 00109 00110 *buffer = '\0'; 00111 00112 if (!data) 00113 return ERROR_NOARG; 00114 00115 strings = ast_strdupa(data); 00116 00117 for (ptrkey = strings; *ptrkey; ptrkey++) { 00118 if (*ptrkey == ',') 00119 count++; 00120 } 00121 00122 sortable_keys = alloca(count * sizeof(struct sortable_keys)); 00123 00124 memset(sortable_keys, 0, count * sizeof(struct sortable_keys)); 00125 00126 /* Parse each into a struct */ 00127 count2 = 0; 00128 while ((ptrkey = strsep(&strings, ","))) { 00129 ptrvalue = strchr(ptrkey, ':'); 00130 if (!ptrvalue) { 00131 count--; 00132 continue; 00133 } 00134 *ptrvalue++ = '\0'; 00135 sortable_keys[count2].key = ptrkey; 00136 sscanf(ptrvalue, "%30f", &sortable_keys[count2].value); 00137 count2++; 00138 } 00139 00140 /* Sort the structs */ 00141 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine); 00142 00143 for (count2 = 0; count2 < count; count2++) { 00144 int blen = strlen(buffer); 00145 if (element_count++) { 00146 strncat(buffer + blen, ",", buflen - blen - 1); 00147 blen++; 00148 } 00149 strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1); 00150 } 00151 00152 return 0; 00153 }
| static int sort_subroutine | ( | const void * | arg1, | |
| const void * | arg2 | |||
| ) | [static] |
Definition at line 89 of file func_cut.c.
References sortable_keys::value.
Referenced by sort_internal().
00090 { 00091 const struct sortable_keys *one=arg1, *two=arg2; 00092 if (one->value < two->value) 00093 return -1; 00094 else if (one->value == two->value) 00095 return 0; 00096 else 00097 return 1; 00098 }
| static int unload_module | ( | void | ) | [static] |
Definition at line 320 of file func_cut.c.
References ast_custom_function_unregister().
00321 { 00322 int res = 0; 00323 00324 res |= ast_custom_function_unregister(&acf_cut); 00325 res |= ast_custom_function_unregister(&acf_sort); 00326 00327 return res; 00328 }
struct ast_custom_function acf_cut [static] |
{
.name = "CUT",
.read = acf_cut_exec,
.read2 = acf_cut_exec2,
}
Definition at line 314 of file func_cut.c.
struct ast_custom_function acf_sort [static] |
{
.name = "SORT",
.read = acf_sort_exec,
}
Definition at line 309 of file func_cut.c.
1.6.1