Thu Jul 9 13:40:34 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: 197244 $")
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, 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, "%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 (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 }
00206 
00207 static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
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 }
00227 
00228 static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
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 }
00251 
00252 struct ast_custom_function acf_sort = {
00253    .name = "SORT",
00254    .synopsis = "Sorts a list of key/vals into a list of keys, based upon the vals",
00255    .syntax = "SORT(key1:val1[...][,keyN:valN])",
00256    .desc =
00257 "Takes a comma-separated list of keys and values, each separated by a colon, and returns a\n"
00258 "comma-separated list of the keys, sorted by their values.  Values will be evaluated as\n"
00259 "floating-point numbers.\n",
00260    .read = acf_sort_exec,
00261 };
00262 
00263 struct ast_custom_function acf_cut = {
00264    .name = "CUT",
00265    .synopsis = "Slices and dices strings, based upon a named delimiter.",
00266    .syntax = "CUT(<varname>,<char-delim>,<range-spec>)",
00267    .desc =
00268 "  varname    - variable you want cut\n"
00269 "  char-delim - defaults to '-'\n"
00270 "  range-spec - number of the field you want (1-based offset)\n"
00271 "             may also be specified as a range (with -)\n"
00272 "             or group of ranges and fields (with &)\n",
00273    .read = acf_cut_exec,
00274 };
00275 
00276 static int unload_module(void)
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 }
00285 
00286 static int load_module(void)
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 }
00295 
00296 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");

Generated on Thu Jul 9 13:40:35 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7