Regular Expressions


Defines

#define switch_regex_safe_free(re)

Typedefs

typedef real_pcre switch_regex_t

Functions

switch_regex_tswitch_regex_compile (const char *pattern, int options, const char **errorptr, int *erroroffset, const unsigned char *tables)
int switch_regex_copy_substring (const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int size)
void switch_regex_free (void *data)
int switch_regex_perform (const char *field, const char *expression, switch_regex_t **new_re, int *ovector, uint32_t olen)
void switch_perform_substitution (switch_regex_t *re, int match_count, const char *data, const char *field_data, char *substituted, switch_size_t len, int *ovector)
switch_status_t switch_regex_match (const char *target, const char *expression)
 Function to evaluate an expression against a string.
switch_status_t switch_regex_match_partial (const char *target, const char *expression, int *partial_match)
 Function to evaluate an expression against a string.
void switch_capture_regex (switch_regex_t *re, int match_count, const char *field_data, int *ovector, const char *var, switch_cap_callback_t callback, void *user_data)
void switch_regex_set_var_callback (const char *var, const char *val, void *user_data)
void switch_regex_set_event_header_callback (const char *var, const char *val, void *user_data)

Define Documentation

#define switch_regex_safe_free ( re   ) 

Value:

if (re) {\
                                switch_regex_free(re);\
                                re = NULL;\
                        }

Definition at line 79 of file switch_regex.h.

Referenced by switch_ivr_check_presence_mapping(), switch_ivr_menu_execute(), switch_ivr_phrase_macro_event(), switch_regex_perform(), and switch_str_time().


Typedef Documentation

typedef struct real_pcre switch_regex_t

Definition at line 43 of file switch_regex.h.


Function Documentation

void switch_capture_regex ( switch_regex_t re,
int  match_count,
const char *  field_data,
int *  ovector,
const char *  var,
switch_cap_callback_t  callback,
void *  user_data 
)

Definition at line 173 of file switch_regex.c.

00176 {
00177 
00178 
00179         const char *replace;
00180         int i;
00181 
00182         for (i = 0; i < match_count; i++) {
00183                 if (pcre_get_substring(field_data, ovector, match_count, i, &replace) > 0) {
00184                         callback(var, replace, user_data);
00185                         pcre_free_substring(replace);
00186                 }
00187         }
00188 }

void switch_perform_substitution ( switch_regex_t re,
int  match_count,
const char *  data,
const char *  field_data,
char *  substituted,
switch_size_t  len,
int *  ovector 
)

Definition at line 128 of file switch_regex.c.

Referenced by switch_ivr_menu_execute(), and switch_ivr_phrase_macro_event().

00130 {
00131         char index[10] = "";
00132         const char *replace = NULL;
00133         switch_size_t x, y = 0, z = 0;
00134         int num = 0;
00135 
00136         for (x = 0; x < (len - 1) && x < strlen(data);) {
00137                 if (data[x] == '$') {
00138                         x++;
00139 
00140                         if (!(data[x] > 47 && data[x] < 58)) {
00141                                 substituted[y++] = data[x - 1];
00142                                 continue;
00143                         }
00144 
00145                         while (data[x] > 47 && data[x] < 58) {
00146                                 index[z++] = data[x];
00147                                 x++;
00148                         }
00149                         index[z++] = '\0';
00150                         z = 0;
00151                         num = atoi(index);
00152 
00153                         if (num < 0 || num > 256) {
00154                                 num = -1;
00155                         }
00156 
00157                         if (pcre_get_substring(field_data, ovector, match_count, num, &replace) > 0) {
00158                                 switch_size_t r;
00159                                 for (r = 0; r < strlen(replace); r++) {
00160                                         substituted[y++] = replace[r];
00161                                 }
00162                                 pcre_free_substring(replace);
00163                         }
00164                 } else {
00165                         substituted[y++] = data[x];
00166                         x++;
00167                 }
00168         }
00169         substituted[y++] = '\0';
00170 }

switch_regex_t* switch_regex_compile ( const char *  pattern,
int  options,
const char **  errorptr,
int *  erroroffset,
const unsigned char *  tables 
)

Definition at line 36 of file switch_regex.c.

00038 {
00039 
00040         return pcre_compile(pattern, options, errorptr, erroroffset, tables);
00041 
00042 }

int switch_regex_copy_substring ( const char *  subject,
int *  ovector,
int  stringcount,
int  stringnumber,
char *  buffer,
int  size 
)

Definition at line 44 of file switch_regex.c.

Referenced by switch_str_time().

00045 {
00046         return pcre_copy_substring(subject, ovector, stringcount, stringnumber, buffer, size);
00047 }

void switch_regex_free ( void *  data  ) 

Definition at line 49 of file switch_regex.c.

00050 {
00051         pcre_free(data);
00052 
00053 }

switch_status_t switch_regex_match ( const char *  target,
const char *  expression 
)

Function to evaluate an expression against a string.

Parameters:
target The string to find a match in
expression The regular expression to run against the string
Returns:
Boolean if a match was found or not

Definition at line 246 of file switch_regex.c.

References switch_regex_match_partial().

Referenced by switch_ivr_dmachine_check_match(), and switch_play_and_get_digits().

00247 {
00248         int partial = 0;
00249         return switch_regex_match_partial(target, expression, &partial);
00250 }

switch_status_t switch_regex_match_partial ( const char *  target,
const char *  expression,
int *  partial_match 
)

Function to evaluate an expression against a string.

Parameters:
target The string to find a match in
expression The regular expression to run against the string
partial If non-zero returns SUCCESS if the target is a partial match, on successful return, this is set to non-zero if the match was partial and zero if it was a full match
Returns:
Boolean if a match was found or not

Definition at line 190 of file switch_regex.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by switch_regex_match().

00191 {
00192         const char *error = NULL;       /* Used to hold any errors                                           */
00193         int error_offset = 0;           /* Holds the offset of an error                                      */
00194         pcre *pcre_prepared = NULL;     /* Holds the compiled regex                                          */
00195         int match_count = 0;            /* Number of times the regex was matched                             */
00196         int offset_vectors[255];        /* not used, but has to exist or pcre won't even try to find a match */
00197         int pcre_flags = 0;
00198 
00199         /* Compile the expression */
00200         pcre_prepared = pcre_compile(expression, 0, &error, &error_offset, NULL);
00201 
00202         /* See if there was an error in the expression */
00203         if (error != NULL) {
00204                 /* Clean up after ourselves */
00205                 if (pcre_prepared) {
00206                         pcre_free(pcre_prepared);
00207                         pcre_prepared = NULL;
00208                 }
00209                 /* Note our error */
00210                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
00211                                                   "Regular Expression Error expression[%s] error[%s] location[%d]\n", expression, error, error_offset);
00212 
00213                 /* We definitely didn't match anything */
00214                 return SWITCH_STATUS_FALSE;
00215         }
00216 
00217         if (*partial) {
00218                 pcre_flags = PCRE_PARTIAL;
00219         }
00220 
00221         /* So far so good, run the regex */
00222         match_count =
00223                 pcre_exec(pcre_prepared, NULL, target, (int) strlen(target), 0, pcre_flags, offset_vectors, sizeof(offset_vectors) / sizeof(offset_vectors[0]));
00224 
00225         /* Clean up */
00226         if (pcre_prepared) {
00227                 pcre_free(pcre_prepared);
00228                 pcre_prepared = NULL;
00229         }
00230 
00231         /* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "number of matches: %d\n", match_count); */
00232 
00233         /* Was it a match made in heaven? */
00234         if (match_count > 0) {
00235                 *partial = 0;
00236                 return SWITCH_STATUS_SUCCESS;
00237         } else if (match_count == PCRE_ERROR_PARTIAL || match_count == PCRE_ERROR_BADPARTIAL) {
00238                 /* yes it is already set, but the code is clearer this way */
00239                 *partial = 1;
00240                 return SWITCH_STATUS_SUCCESS;
00241         } else {
00242                 return SWITCH_STATUS_FALSE;
00243         }
00244 }

int switch_regex_perform ( const char *  field,
const char *  expression,
switch_regex_t **  new_re,
int *  ovector,
uint32_t  olen 
)

Definition at line 55 of file switch_regex.c.

References switch_ast2regex(), SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), switch_regex_safe_free, and switch_safe_free.

Referenced by switch_ivr_check_presence_mapping(), switch_ivr_menu_execute(), switch_ivr_phrase_macro_event(), and switch_str_time().

00056 {
00057         const char *error = NULL;
00058         int erroffset = 0;
00059         pcre *re = NULL;
00060         int match_count = 0;
00061         char *tmp = NULL;
00062         uint32_t flags = 0;
00063         char abuf[256] = "";
00064 
00065         if (!(field && expression)) {
00066                 return 0;
00067         }
00068 
00069         if (*expression == '_') {
00070                 if (switch_ast2regex(expression + 1, abuf, sizeof(abuf))) {
00071                         expression = abuf;
00072                 }
00073         }
00074 
00075         if (*expression == '/') {
00076                 char *opts = NULL;
00077                 tmp = strdup(expression + 1);
00078                 assert(tmp);
00079                 if ((opts = strrchr(tmp, '/'))) {
00080                         *opts++ = '\0';
00081                 } else {
00082                         goto end;
00083                 }
00084                 expression = tmp;
00085                 if (opts) {
00086                         if (strchr(opts, 'i')) {
00087                                 flags |= PCRE_CASELESS;
00088                         }
00089                         if (strchr(opts, 's')) {
00090                                 flags |= PCRE_DOTALL;
00091                         }
00092                 }
00093         }
00094 
00095         re = pcre_compile(expression,   /* the pattern */
00096                                           flags,        /* default options */
00097                                           &error,       /* for error message */
00098                                           &erroffset,   /* for error offset */
00099                                           NULL);        /* use default character tables */
00100         if (error) {
00101                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "COMPILE ERROR: %d [%s][%s]\n", erroffset, error, expression);
00102                 switch_regex_safe_free(re);
00103                 goto end;
00104         }
00105 
00106         match_count = pcre_exec(re,     /* result of pcre_compile() */
00107                                                         NULL,   /* we didn't study the pattern */
00108                                                         field,  /* the subject string */
00109                                                         (int) strlen(field),    /* the length of the subject string */
00110                                                         0,      /* start at offset 0 in the subject */
00111                                                         0,      /* default options */
00112                                                         ovector,        /* vector of integers for substring information */
00113                                                         olen);  /* number of elements (NOT size in bytes) */
00114 
00115 
00116         if (match_count <= 0) {
00117                 switch_regex_safe_free(re);
00118                 match_count = 0;
00119         }
00120 
00121         *new_re = (switch_regex_t *) re;
00122 
00123   end:
00124         switch_safe_free(tmp);
00125         return match_count;
00126 }

void switch_regex_set_event_header_callback ( const char *  var,
const char *  val,
void *  user_data 
)

Definition at line 259 of file switch_regex.c.

References switch_event_add_header_string(), and SWITCH_STACK_BOTTOM.

00260 {
00261 
00262         switch_event_t *event = (switch_event_t *) user_data;
00263         switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, var, val);
00264 }

void switch_regex_set_var_callback ( const char *  var,
const char *  val,
void *  user_data 
)

Definition at line 252 of file switch_regex.c.

References switch_channel_add_variable_var_check(), switch_core_session_get_channel(), SWITCH_FALSE, and SWITCH_STACK_PUSH.


Generated on Wed May 16 04:00:19 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7