Mon Mar 20 08:25:59 2006

Asterisk developer's documentation


Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

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"

Go to the source code of this file.

Functions

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

Variables

ast_custom_function eval_function
ast_custom_function fieldqty_function
ast_custom_function len_function
ast_custom_function regex_function
ast_custom_function strftime_function


Detailed Description

String manipulation dialplan functions.

Definition in file func_strings.c.


Function Documentation

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

Definition at line 146 of file func_strings.c.

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

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

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

Definition at line 126 of file func_strings.c.

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

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, and LOG_WARNING.

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       arg++;
00093       earg = strrchr(arg, '"');
00094       if (earg) {
00095          *earg++ = '\0';
00096          /* Skip over any spaces before the data we are checking */
00097          while (*earg == ' ')
00098             earg++;
00099       }
00100    } else {
00101       arg = tmp;
00102    }
00103 
00104    if ((errcode = regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB))) {
00105       regerror(errcode, &regexbuf, errstr, sizeof(errstr));
00106       ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
00107    } else {
00108       if (!regexec(&regexbuf, earg ? earg : "", 0, NULL, 0))
00109          ast_copy_string(buf, "1", len); 
00110    }
00111    regfree(&regexbuf);
00112 
00113    return buf;
00114 }

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

Definition at line 197 of file func_strings.c.

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

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

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 214 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 139 of file func_strings.c.

struct ast_custom_function regex_function [static]
 

Definition at line 119 of file func_strings.c.

struct ast_custom_function strftime_function [static]
 

Definition at line 190 of file func_strings.c.


Generated on Mon Mar 20 08:25:59 2006 for Asterisk - the Open Source PBX by  doxygen 1.3.9.1