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

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.


Define Documentation

#define switch_regex_safe_free re   ) 
 

Value:

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


Typedef Documentation

typedef struct real_pcre switch_regex_t
 


Function Documentation

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
 

00123 {
00124         char index[10] = "";
00125         char replace[1024] = "";
00126         switch_size_t x, y = 0, z = 0;
00127         int num = 0;
00128 
00129         for (x = 0; x < (len - 1) && x < strlen(data);) {
00130                 if (data[x] == '$') {
00131                         x++;
00132 
00133                         if (!(data[x] > 47 && data[x] < 58)) {
00134                                 substituted[y++] = data[x - 1];
00135                                 continue;
00136                         }
00137 
00138                         while (data[x] > 47 && data[x] < 58) {
00139                                 index[z++] = data[x];
00140                                 x++;
00141                         }
00142                         index[z++] = '\0';
00143                         z = 0;
00144                         num = atoi(index);
00145 
00146                         if (num < 0 || num > 256) {
00147                                 num = -1;
00148                         }
00149 
00150                         if (pcre_copy_substring(field_data, ovector, match_count, num, replace, sizeof(replace)) > 0) {
00151                                 switch_size_t r;
00152                                 for (r = 0; r < strlen(replace); r++) {
00153                                         substituted[y++] = replace[r];
00154                                 }
00155                         }
00156                 } else {
00157                         substituted[y++] = data[x];
00158                         x++;
00159                 }
00160         }
00161         substituted[y++] = '\0';
00162 }

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

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
 

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

void switch_regex_free void *  data  ) 
 

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
00220 {
00221         int partial = 0;
00222         return switch_regex_match_partial(target, expression, &partial);
00223 }

Here is the call graph for this function:

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
00165 {
00166         const char *error = NULL;       /* Used to hold any errors                                           */
00167         int error_offset = 0;           /* Holds the offset of an error                                      */
00168         pcre *pcre_prepared = NULL;     /* Holds the compiled regex                                          */
00169         int match_count = 0;            /* Number of times the regex was matched                             */
00170         int offset_vectors[2];          /* not used, but has to exist or pcre won't even try to find a match */
00171         int pcre_flags = 0;
00172         
00173         /* Compile the expression */
00174         pcre_prepared = pcre_compile(expression, 0, &error, &error_offset, NULL);
00175 
00176         /* See if there was an error in the expression */
00177         if (error != NULL) {
00178                 /* Clean up after ourselves */
00179                 if (pcre_prepared) {
00180                         pcre_free(pcre_prepared);
00181                         pcre_prepared = NULL;
00182                 }
00183                 /* Note our error */
00184                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
00185                                                   "Regular Expression Error expression[%s] error[%s] location[%d]\n", expression, error, error_offset);
00186 
00187                 /* We definitely didn't match anything */
00188                 return SWITCH_STATUS_FALSE;
00189         }
00190         
00191         if (*partial) {
00192                 pcre_flags = PCRE_PARTIAL;
00193         }
00194         
00195         /* So far so good, run the regex */
00196         match_count = pcre_exec(pcre_prepared, NULL, target, (int) strlen(target), 0, pcre_flags, offset_vectors, sizeof(offset_vectors) / sizeof(offset_vectors[0]));
00197 
00198         /* Clean up */
00199         if (pcre_prepared) {
00200                 pcre_free(pcre_prepared);
00201                 pcre_prepared = NULL;
00202         }
00203 
00204         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "number of matches: %d\n", match_count);
00205 
00206         /* Was it a match made in heaven? */
00207         if (match_count > 0) {
00208                 *partial = 0;
00209                 return SWITCH_STATUS_SUCCESS;
00210         } else if (match_count == PCRE_ERROR_PARTIAL) {
00211                 /* yes it is already set, but the code is clearer this way */
00212                 *partial = 1;
00213                 return SWITCH_STATUS_SUCCESS;
00214         } else {
00215                 return SWITCH_STATUS_FALSE;
00216         }
00217 }

Here is the call graph for this function:

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

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

Here is the call graph for this function:


Generated on Fri Oct 10 11:25:13 2008 for FreeSWITCH by  doxygen 1.3.9.1