Mon Jun 27 16:51:14 2011

Asterisk developer's documentation


func_cut.c File Reference

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 void __reg_module (void)
static void __unreg_module (void)
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)
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_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Cut out information from a string" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static struct ast_custom_function acf_cut
static struct ast_custom_function acf_sort
static struct ast_module_infoast_module_info = &__mod_info


Detailed Description

CUT function.

Author:
Tilghman Lesher <app_cut__v003@the-tilghman.com>

Definition in file func_cut.c.


Define Documentation

#define ERROR_NOARG   (-1)

Definition at line 96 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 97 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 98 of file func_cut.c.

Referenced by acf_cut_exec(), acf_cut_exec2(), and cut_internal().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 336 of file func_cut.c.

static void __unreg_module ( void   )  [static]

Definition at line 336 of file func_cut.c.

static int acf_cut_exec ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 255 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.

00256 {
00257    int ret = -1;
00258    struct ast_str *str = ast_str_create(16);
00259 
00260    switch (cut_internal(chan, data, &str, len)) {
00261    case ERROR_NOARG:
00262       ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
00263       break;
00264    case ERROR_NOMEM:
00265       ast_log(LOG_ERROR, "Out of memory\n");
00266       break;
00267    case ERROR_USAGE:
00268       ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
00269       break;
00270    case 0:
00271       ret = 0;
00272       ast_copy_string(buf, ast_str_buffer(str), len);
00273       break;
00274    default:
00275       ast_log(LOG_ERROR, "Unknown internal error\n");
00276    }
00277    ast_free(str);
00278    return ret;
00279 }

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 281 of file func_cut.c.

References ast_log(), cut_internal(), ERROR_NOARG, ERROR_NOMEM, ERROR_USAGE, and LOG_ERROR.

00282 {
00283    int ret = -1;
00284 
00285    switch (cut_internal(chan, data, buf, len)) {
00286    case ERROR_NOARG:
00287       ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
00288       break;
00289    case ERROR_NOMEM:
00290       ast_log(LOG_ERROR, "Out of memory\n");
00291       break;
00292    case ERROR_USAGE:
00293       ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
00294       break;
00295    case 0:
00296       ret = 0;
00297       break;
00298    default:
00299       ast_log(LOG_ERROR, "Unknown internal error\n");
00300    }
00301 
00302    return ret;
00303 }

static int acf_sort_exec ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 234 of file func_cut.c.

References ast_log(), ERROR_NOARG, ERROR_NOMEM, LOG_ERROR, and sort_internal().

00235 {
00236    int ret = -1;
00237 
00238    switch (sort_internal(chan, data, buf, len)) {
00239    case ERROR_NOARG:
00240       ast_log(LOG_ERROR, "SORT() requires an argument\n");
00241       break;
00242    case ERROR_NOMEM:
00243       ast_log(LOG_ERROR, "Out of memory\n");
00244       break;
00245    case 0:
00246       ret = 0;
00247       break;
00248    default:
00249       ast_log(LOG_ERROR, "Unknown internal error\n");
00250    }
00251 
00252    return ret;
00253 }

static int cut_internal ( struct ast_channel chan,
char *  data,
struct ast_str **  buf,
ssize_t  buflen 
) [static]

Definition at line 151 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, parse(), and strsep().

Referenced by acf_cut_exec(), and acf_cut_exec2().

00152 {
00153    char *parse, ds[2], *var_expr;
00154    size_t delim_consumed;
00155    struct ast_str *var_value;
00156    AST_DECLARE_APP_ARGS(args,
00157       AST_APP_ARG(varname);
00158       AST_APP_ARG(delimiter);
00159       AST_APP_ARG(field);
00160    );
00161 
00162    parse = ast_strdupa(data);
00163 
00164    AST_STANDARD_APP_ARGS(args, parse);
00165 
00166    /* Check arguments */
00167    if (args.argc < 3) {
00168       return ERROR_NOARG;
00169    } else if (!(var_expr = alloca(strlen(args.varname) + 4))) {
00170       return ERROR_NOMEM;
00171    }
00172 
00173    /* Get the value of the variable named in the 1st argument */
00174    snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname);
00175    var_value = ast_str_create(16);
00176    ast_str_substitute_variables(&var_value, 0, chan, var_expr);
00177 
00178    /* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */
00179    if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) {
00180       ast_copy_string(ds, "-", sizeof(ds));
00181    }
00182    ds[1] = '\0';
00183 
00184    if (ast_str_strlen(var_value)) {
00185       int curfieldnum = 1;
00186       char *curfieldptr = ast_str_buffer(var_value);
00187       int out_field_count = 0;
00188 
00189       while (curfieldptr != NULL && args.field != NULL) {
00190          char *next_range = strsep(&(args.field), "&");
00191          int start_field, stop_field;
00192          char trashchar;
00193 
00194          if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) {
00195             /* range with both start and end */
00196          } else if (sscanf(next_range, "-%30d", &stop_field) == 1) {
00197             /* range with end only */
00198             start_field = 1;
00199          } else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) {
00200             /* range with start only */
00201             stop_field = INT_MAX;
00202          } else if (sscanf(next_range, "%30d", &start_field) == 1) {
00203             /* single number */
00204             stop_field = start_field;
00205          } else {
00206             /* invalid field spec */
00207             ast_free(var_value);
00208             return ERROR_USAGE;
00209          }
00210 
00211          /* Get to start, if not there already */
00212          while (curfieldptr != NULL && curfieldnum < start_field) {
00213             strsep(&curfieldptr, ds);
00214             curfieldnum++;
00215          }
00216 
00217          /* Most frequent problem is the expectation of reordering fields */
00218          if (curfieldnum > start_field) {
00219             ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
00220          }
00221 
00222          /* Output fields until we either run out of fields or stop_field is reached */
00223          while (curfieldptr != NULL && curfieldnum <= stop_field) {
00224             char *field_value = strsep(&curfieldptr, ds);
00225             ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value);
00226             curfieldnum++;
00227          }
00228       }
00229    }
00230    ast_free(var_value);
00231    return 0;
00232 }

static int load_module ( void   )  [static]

Definition at line 326 of file func_cut.c.

References acf_cut, acf_sort, and ast_custom_function_register.

00327 {
00328    int res = 0;
00329 
00330    res |= ast_custom_function_register(&acf_cut);
00331    res |= ast_custom_function_register(&acf_sort);
00332 
00333    return res;
00334 }

static int sort_internal ( struct ast_channel chan,
char *  data,
char *  buffer,
size_t  buflen 
) [static]

Definition at line 100 of file func_cut.c.

References ast_strdupa, ERROR_NOARG, sortable_keys::key, sort_subroutine(), strsep(), and value.

Referenced by acf_sort_exec().

00101 {
00102    char *strings, *ptrkey, *ptrvalue;
00103    int count=1, count2, element_count=0;
00104    struct sortable_keys *sortable_keys;
00105 
00106    *buffer = '\0';
00107 
00108    if (!data)
00109       return ERROR_NOARG;
00110 
00111    strings = ast_strdupa(data);
00112 
00113    for (ptrkey = strings; *ptrkey; ptrkey++) {
00114       if (*ptrkey == ',')
00115          count++;
00116    }
00117 
00118    sortable_keys = alloca(count * sizeof(struct sortable_keys));
00119 
00120    memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
00121 
00122    /* Parse each into a struct */
00123    count2 = 0;
00124    while ((ptrkey = strsep(&strings, ","))) {
00125       ptrvalue = strchr(ptrkey, ':');
00126       if (!ptrvalue) {
00127          count--;
00128          continue;
00129       }
00130       *ptrvalue++ = '\0';
00131       sortable_keys[count2].key = ptrkey;
00132       sscanf(ptrvalue, "%30f", &sortable_keys[count2].value);
00133       count2++;
00134    }
00135 
00136    /* Sort the structs */
00137    qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
00138 
00139    for (count2 = 0; count2 < count; count2++) {
00140       int blen = strlen(buffer);
00141       if (element_count++) {
00142          strncat(buffer + blen, ",", buflen - blen - 1);
00143          blen++;
00144       }
00145       strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
00146    }
00147 
00148    return 0;
00149 }

static int sort_subroutine ( const void *  arg1,
const void *  arg2 
) [static]

Definition at line 85 of file func_cut.c.

References sortable_keys::value.

Referenced by sort_internal().

00086 {
00087    const struct sortable_keys *one=arg1, *two=arg2;
00088    if (one->value < two->value)
00089       return -1;
00090    else if (one->value == two->value)
00091       return 0;
00092    else
00093       return 1;
00094 }

static int unload_module ( void   )  [static]

Definition at line 316 of file func_cut.c.

References acf_cut, acf_sort, and ast_custom_function_unregister().

00317 {
00318    int res = 0;
00319 
00320    res |= ast_custom_function_unregister(&acf_cut);
00321    res |= ast_custom_function_unregister(&acf_sort);
00322 
00323    return res;
00324 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Cut out information from a string" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 336 of file func_cut.c.

struct ast_custom_function acf_cut [static]

Initial value:

 {
   .name = "CUT",
   .read = acf_cut_exec,
   .read2 = acf_cut_exec2,
}

Definition at line 310 of file func_cut.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function acf_sort [static]

Initial value:

 {
   .name = "SORT",
   .read = acf_sort_exec,
}

Definition at line 305 of file func_cut.c.

Referenced by load_module(), and unload_module().

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 336 of file func_cut.c.


Generated on Mon Jun 27 16:51:14 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7