Wed Aug 18 22:34:24 2010

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)
#define MAXRESULT   1024

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_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, char *buffer, size_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_DEFAULT , .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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, }
ast_custom_function acf_cut
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 56 of file func_cut.c.

Referenced by acf_cut_exec(), acf_sort_exec(), cut_internal(), and sort_internal().

#define ERROR_NOMEM   (-2)

Definition at line 57 of file func_cut.c.

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

#define ERROR_USAGE   (-3)

Definition at line 58 of file func_cut.c.

Referenced by acf_cut_exec(), and cut_internal().

#define MAXRESULT   1024

Definition at line 38 of file func_cut.c.

Referenced by cut_internal(), exec_exec(), substituted(), and tryexec_exec().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 296 of file func_cut.c.

static void __unreg_module ( void   )  [static]

Definition at line 296 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 228 of file func_cut.c.

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

00229 {
00230    int ret = -1;
00231 
00232    switch (cut_internal(chan, data, buf, len)) {
00233    case ERROR_NOARG:
00234       ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
00235       break;
00236    case ERROR_NOMEM:
00237       ast_log(LOG_ERROR, "Out of memory\n");
00238       break;
00239    case ERROR_USAGE:
00240       ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
00241       break;
00242    case 0:
00243       ret = 0;
00244       break;
00245    default:
00246       ast_log(LOG_ERROR, "Unknown internal error\n");
00247    }
00248 
00249    return ret;
00250 }

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

Definition at line 207 of file func_cut.c.

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

00208 {
00209    int ret = -1;
00210 
00211    switch (sort_internal(chan, data, buf, len)) {
00212    case ERROR_NOARG:
00213       ast_log(LOG_ERROR, "SORT() requires an argument\n");
00214       break;
00215    case ERROR_NOMEM:
00216       ast_log(LOG_ERROR, "Out of memory\n");
00217       break;
00218    case 0:
00219       ret = 0;
00220       break;
00221    default:
00222       ast_log(LOG_ERROR, "Unknown internal error\n");
00223    }
00224 
00225    return ret;
00226 }

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

Definition at line 111 of file func_cut.c.

References AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_get_encoded_char(), ast_log(), AST_STANDARD_APP_ARGS, ast_strdupa, chan, ERROR_NOARG, ERROR_NOMEM, ERROR_USAGE, LOG_WARNING, MAXRESULT, parse(), pbx_substitute_variables_helper(), and strsep().

Referenced by acf_cut_exec().

00112 {
00113    char *parse;
00114    size_t delim_consumed;
00115    AST_DECLARE_APP_ARGS(args,
00116       AST_APP_ARG(varname);
00117       AST_APP_ARG(delimiter);
00118       AST_APP_ARG(field);
00119    );
00120 
00121    *buffer = '\0';
00122 
00123    parse = ast_strdupa(data);
00124 
00125    AST_STANDARD_APP_ARGS(args, parse);
00126 
00127    /* Check and parse arguments */
00128    if (args.argc < 3) {
00129       return ERROR_NOARG;
00130    } else {
00131       char d, ds[2] = "";
00132       char *tmp = alloca(strlen(args.varname) + 4);
00133       char varvalue[MAXRESULT], *tmp2=varvalue;
00134 
00135       if (tmp) {
00136          snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname);
00137       } else {
00138          return ERROR_NOMEM;
00139       }
00140 
00141       if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed))
00142          ast_copy_string(ds, "-", sizeof(ds));
00143 
00144       /* String form of the delimiter, for use with strsep(3) */
00145       d = *ds;
00146 
00147       pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
00148 
00149       if (tmp2) {
00150          int curfieldnum = 1, firstfield = 1;
00151          while (tmp2 != NULL && args.field != NULL) {
00152             char *nextgroup = strsep(&(args.field), "&");
00153             int num1 = 0, num2 = MAXRESULT;
00154             char trashchar;
00155 
00156             if (sscanf(nextgroup, "%30d-%30d", &num1, &num2) == 2) {
00157                /* range with both start and end */
00158             } else if (sscanf(nextgroup, "-%30d", &num2) == 1) {
00159                /* range with end */
00160                num1 = 0;
00161             } else if ((sscanf(nextgroup, "%30d%1c", &num1, &trashchar) == 2) && (trashchar == '-')) {
00162                /* range with start */
00163                num2 = MAXRESULT;
00164             } else if (sscanf(nextgroup, "%30d", &num1) == 1) {
00165                /* single number */
00166                num2 = num1;
00167             } else {
00168                return ERROR_USAGE;
00169             }
00170 
00171             /* Get to start, if any */
00172             if (num1 > 0) {
00173                while (tmp2 != (char *)NULL + 1 && curfieldnum < num1) {
00174                   tmp2 = strchr(tmp2, d) + 1;
00175                   curfieldnum++;
00176                }
00177             }
00178 
00179             /* Most frequent problem is the expectation of reordering fields */
00180             if ((num1 > 0) && (curfieldnum > num1))
00181                ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
00182 
00183             /* Re-null tmp2 if we added 1 to NULL */
00184             if (tmp2 == (char *)NULL + 1)
00185                tmp2 = NULL;
00186 
00187             /* Output fields until we either run out of fields or num2 is reached */
00188             while (tmp2 != NULL && curfieldnum <= num2) {
00189                char *tmp3 = strsep(&tmp2, ds);
00190                int curlen = strlen(buffer);
00191 
00192                if (firstfield) {
00193                   snprintf(buffer, buflen, "%s", tmp3);
00194                   firstfield = 0;
00195                } else {
00196                   snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
00197                }
00198 
00199                curfieldnum++;
00200             }
00201          }
00202       }
00203    }
00204    return 0;
00205 }

static int load_module ( void   )  [static]

Definition at line 286 of file func_cut.c.

References acf_cut, acf_sort, and ast_custom_function_register.

00287 {
00288    int res = 0;
00289 
00290    res |= ast_custom_function_register(&acf_cut);
00291    res |= ast_custom_function_register(&acf_sort);
00292 
00293    return res;
00294 }

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

Definition at line 60 of file func_cut.c.

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

Referenced by acf_sort_exec().

00061 {
00062    char *strings, *ptrkey, *ptrvalue;
00063    int count=1, count2, element_count=0;
00064    struct sortable_keys *sortable_keys;
00065 
00066    *buffer = '\0';
00067 
00068    if (!data)
00069       return ERROR_NOARG;
00070 
00071    strings = ast_strdupa(data);
00072 
00073    for (ptrkey = strings; *ptrkey; ptrkey++) {
00074       if (*ptrkey == ',')
00075          count++;
00076    }
00077 
00078    sortable_keys = alloca(count * sizeof(struct sortable_keys));
00079 
00080    memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
00081 
00082    /* Parse each into a struct */
00083    count2 = 0;
00084    while ((ptrkey = strsep(&strings, ","))) {
00085       ptrvalue = strchr(ptrkey, ':');
00086       if (!ptrvalue) {
00087          count--;
00088          continue;
00089       }
00090       *ptrvalue++ = '\0';
00091       sortable_keys[count2].key = ptrkey;
00092       sscanf(ptrvalue, "%30f", &sortable_keys[count2].value);
00093       count2++;
00094    }
00095 
00096    /* Sort the structs */
00097    qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
00098 
00099    for (count2 = 0; count2 < count; count2++) {
00100       int blen = strlen(buffer);
00101       if (element_count++) {
00102          strncat(buffer + blen, ",", buflen - blen - 1);
00103          blen++;
00104       }
00105       strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
00106    }
00107 
00108    return 0;
00109 }

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

Definition at line 45 of file func_cut.c.

References sortable_keys::value.

Referenced by sort_internal().

00046 {
00047    const struct sortable_keys *one=arg1, *two=arg2;
00048    if (one->value < two->value)
00049       return -1;
00050    else if (one->value == two->value)
00051       return 0;
00052    else
00053       return 1;
00054 }

static int unload_module ( void   )  [static]

Definition at line 276 of file func_cut.c.

References acf_cut, acf_sort, and ast_custom_function_unregister().

00277 {
00278    int res = 0;
00279 
00280    res |= ast_custom_function_unregister(&acf_cut);
00281    res |= ast_custom_function_unregister(&acf_sort);
00282 
00283    return res;
00284 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, } [static]

Definition at line 296 of file func_cut.c.

struct ast_custom_function acf_cut

Definition at line 263 of file func_cut.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function acf_sort

Definition at line 252 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 296 of file func_cut.c.


Generated on Wed Aug 18 22:34:24 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7