Thu May 24 14:22:41 2007

Asterisk developer's documentation


func_strings.c File Reference

String manipulation dialplan functions. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/localtime.h"

Include dependency graph for func_strings.c:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static char * acf_strftime (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * builtin_function_len (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * builtin_function_regex (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * function_eval (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * function_fieldqty (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)

Variables

static struct ast_custom_function eval_function
static struct ast_custom_function fieldqty_function
static struct ast_custom_function len_function
static struct ast_custom_function regex_function
static struct ast_custom_function strftime_function


Detailed Description

String manipulation dialplan functions.

Definition in file func_strings.c.


Function Documentation

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

Definition at line 145 of file func_strings.c.

References ast_localtime(), ast_log(), ast_strdupa, ast_strlen_zero(), format, LOG_ERROR, LOG_WARNING, and strsep().

00146 {
00147    char *format, *epoch, *timezone = NULL;
00148    long epochi;
00149    struct tm time;
00150 
00151    buf[0] = '\0';
00152 
00153    if (!data) {
00154       ast_log(LOG_ERROR, "Asterisk function STRFTIME() requires an argument.\n");
00155       return buf;
00156    }
00157    
00158    format = ast_strdupa(data);
00159    if (!format) {
00160       ast_log(LOG_ERROR, "Out of memory\n");
00161       return buf;
00162    }
00163    
00164    epoch = strsep(&format, "|");
00165    timezone = strsep(&format, "|");
00166 
00167    if (ast_strlen_zero(epoch) || !sscanf(epoch, "%ld", &epochi)) {
00168       struct timeval tv = ast_tvnow();
00169       epochi = tv.tv_sec;
00170    }
00171 
00172    ast_localtime(&epochi, &time, timezone);
00173 
00174    if (!format) {
00175       format = "%c";
00176    }
00177 
00178    if (!strftime(buf, len, format, &time)) {
00179       ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
00180    }
00181    buf[len - 1] = '\0';
00182 
00183    return buf;
00184 }

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

Definition at line 125 of file func_strings.c.

00126 {
00127    int length = 0;
00128    if (data) {
00129       length = strlen(data);
00130    }
00131    snprintf(buf, len, "%d", length);
00132    return buf;
00133 }

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

Definition at line 75 of file func_strings.c.

References ast_log(), ast_strdupa, LOG_ERROR, LOG_WARNING, and strsep().

00076 {
00077    char *arg, *earg = NULL, *tmp, errstr[256] = "";
00078    int errcode;
00079    regex_t regexbuf;
00080 
00081    ast_copy_string(buf, "0", len);
00082    
00083    tmp = ast_strdupa(data);
00084    if (!tmp) {
00085       ast_log(LOG_ERROR, "Out of memory in %s(%s)\n", cmd, data);
00086       return buf;
00087    }
00088 
00089    /* Regex in quotes */
00090    arg = strchr(tmp, '"');
00091    if (arg) {
00092       earg = ++arg;
00093       strsep(&earg, "\"");
00094       if (earg) {
00095          /* Skip over any spaces before the data we are checking */
00096          while (*earg == ' ')
00097             earg++;
00098       }
00099    } else {
00100       arg = tmp;
00101    }
00102 
00103    if ((errcode = regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB))) {
00104       regerror(errcode, &regexbuf, errstr, sizeof(errstr));
00105       ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
00106    } else {
00107       if (!regexec(&regexbuf, earg ? earg : "", 0, NULL, 0))
00108          ast_copy_string(buf, "1", len); 
00109    }
00110    regfree(&regexbuf);
00111 
00112    return buf;
00113 }

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

Definition at line 196 of file func_strings.c.

References ast_log(), ast_strlen_zero(), LOG_WARNING, and pbx_substitute_variables_helper().

00197 {
00198    memset(buf, 0, len);
00199 
00200    if (ast_strlen_zero(data)) {
00201       ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
00202       return buf;
00203    }
00204 
00205    pbx_substitute_variables_helper(chan, data, buf, len - 1);
00206 
00207    return buf;
00208 }

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

Definition at line 42 of file func_strings.c.

References ast_log(), ast_strdupa, ast_strlen_zero(), LOG_ERROR, pbx_retrieve_variable(), and strsep().

00043 {
00044    char *varname, *varval, workspace[256];
00045    char *delim = ast_strdupa(data);
00046    int fieldcount = 0;
00047 
00048    if (delim) {
00049       varname = strsep(&delim, "|");
00050       pbx_retrieve_variable(chan, varname, &varval, workspace, sizeof(workspace), NULL);
00051       if (delim) {
00052          while (strsep(&varval, delim))
00053             fieldcount++;
00054       } else if (!ast_strlen_zero(varval)) {
00055          fieldcount = 1;
00056       }
00057       snprintf(buf, len, "%d", fieldcount);
00058    } else {
00059       ast_log(LOG_ERROR, "Out of memory\n");
00060       strncpy(buf, "1", len);
00061    }
00062    return buf;
00063 }


Variable Documentation

struct ast_custom_function eval_function [static]

Definition at line 213 of file func_strings.c.

struct ast_custom_function fieldqty_function [static]

Definition at line 68 of file func_strings.c.

struct ast_custom_function len_function [static]

Definition at line 138 of file func_strings.c.

struct ast_custom_function regex_function [static]

Definition at line 118 of file func_strings.c.

struct ast_custom_function strftime_function [static]

Definition at line 189 of file func_strings.c.


Generated on Thu May 24 14:22:41 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.7