Fri Jul 24 00:40:56 2009

Asterisk developer's documentation


func_cut.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (c) 2003-2006 Tilghman Lesher.  All rights reserved.
00005  *
00006  * Tilghman Lesher <app_cut__v003@the-tilghman.com>
00007  *
00008  * This code is released by the author with no restrictions on usage.
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  */
00017 
00018 /*! \file
00019  * 
00020  * \brief CUT function
00021  *
00022  * \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
00023  *
00024  * \ingroup functions
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 163256 $")
00030 
00031 #include "asterisk/file.h"
00032 #include "asterisk/channel.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/module.h"
00035 #include "asterisk/app.h"
00036 
00037 /* Maximum length of any variable */
00038 #define MAXRESULT 1024
00039 
00040 struct sortable_keys {
00041    char *key;
00042    float value;
00043 };
00044 
00045 static int sort_subroutine(const void *arg1, const void *arg2)
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 }
00055 
00056 #define ERROR_NOARG  (-1)
00057 #define ERROR_NOMEM  (-2)
00058 #define ERROR_USAGE  (-3)
00059 
00060 static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
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, "%f", &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 }
00110 
00111 static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
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;
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, "%d-%d", &num1, &num2) == 2) {
00157                /* range with both start and end */
00158             } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
00159                /* range with end */
00160                num1 = 0;
00161             } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
00162                /* range with start */
00163                num2 = MAXRESULT;
00164             } else if (sscanf(nextgroup, "%d", &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 (curlen)
00193                   snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
00194                else
00195                   snprintf(buffer, buflen, "%s", tmp3);
00196 
00197                curfieldnum++;
00198             }
00199          }
00200       }
00201    }
00202    return 0;
00203 }
00204 
00205 static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00206 {
00207    int ret = -1;
00208 
00209    switch (sort_internal(chan, data, buf, len)) {
00210    case ERROR_NOARG:
00211       ast_log(LOG_ERROR, "SORT() requires an argument\n");
00212       break;
00213    case ERROR_NOMEM:
00214       ast_log(LOG_ERROR, "Out of memory\n");
00215       break;
00216    case 0:
00217       ret = 0;
00218       break;
00219    default:
00220       ast_log(LOG_ERROR, "Unknown internal error\n");
00221    }
00222 
00223    return ret;
00224 }
00225 
00226 static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00227 {
00228    int ret = -1;
00229 
00230    switch (cut_internal(chan, data, buf, len)) {
00231    case ERROR_NOARG:
00232       ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
00233       break;
00234    case ERROR_NOMEM:
00235       ast_log(LOG_ERROR, "Out of memory\n");
00236       break;
00237    case ERROR_USAGE:
00238       ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
00239       break;
00240    case 0:
00241       ret = 0;
00242       break;
00243    default:
00244       ast_log(LOG_ERROR, "Unknown internal error\n");
00245    }
00246 
00247    return ret;
00248 }
00249 
00250 struct ast_custom_function acf_sort = {
00251    .name = "SORT",
00252    .synopsis = "Sorts a list of key/vals into a list of keys, based upon the vals",
00253    .syntax = "SORT(key1:val1[...][,keyN:valN])",
00254    .desc =
00255 "Takes a comma-separated list of keys and values, each separated by a colon, and returns a\n"
00256 "comma-separated list of the keys, sorted by their values.  Values will be evaluated as\n"
00257 "floating-point numbers.\n",
00258    .read = acf_sort_exec,
00259 };
00260 
00261 struct ast_custom_function acf_cut = {
00262    .name = "CUT",
00263    .synopsis = "Slices and dices strings, based upon a named delimiter.",
00264    .syntax = "CUT(<varname>,<char-delim>,<range-spec>)",
00265    .desc =
00266 "  varname    - variable you want cut\n"
00267 "  char-delim - defaults to '-'\n"
00268 "  range-spec - number of the field you want (1-based offset)\n"
00269 "             may also be specified as a range (with -)\n"
00270 "             or group of ranges and fields (with &)\n",
00271    .read = acf_cut_exec,
00272 };
00273 
00274 static int unload_module(void)
00275 {
00276    int res = 0;
00277 
00278    res |= ast_custom_function_unregister(&acf_cut);
00279    res |= ast_custom_function_unregister(&acf_sort);
00280 
00281    return res;
00282 }
00283 
00284 static int load_module(void)
00285 {
00286    int res = 0;
00287 
00288    res |= ast_custom_function_register(&acf_cut);
00289    res |= ast_custom_function_register(&acf_sort);
00290 
00291    return res;
00292 }
00293 
00294 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");

Generated on Fri Jul 24 00:40:56 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7