]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add regex api func
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 9 Nov 2007 15:26:32 +0000 (15:26 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 9 Nov 2007 15:26:32 +0000 (15:26 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6193 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_utils.h
src/mod/applications/mod_commands/mod_commands.c
src/mod/applications/mod_expr/mod_expr.c
src/switch_utils.c

index edbfc905a9ebbc0fb4048c9fd654daf56beb583f..13fc9dbdd978a35b4b52c13f2f4dca884e443db1 100644 (file)
@@ -302,6 +302,7 @@ SWITCH_DECLARE(switch_bool_t) switch_is_number(const char *str);
 SWITCH_DECLARE(char *) switch_strip_spaces(const char *str);
 SWITCH_DECLARE(const char *) switch_stristr(const char *str, const char *instr);
 SWITCH_DECLARE(switch_bool_t) switch_is_lan_addr(const char *ip);
+SWITCH_DECLARE(char *) switch_replace_char(char *str, char from, char to, switch_bool_t dup);
 
 /*!
   \brief Escape a string by prefixing a list of characters with an escape character
index da98974df8aaa70ec240aaea3c9b912bbce922c6..193fad78e8535c631452b8d72ba47bfed26db621 100644 (file)
@@ -41,6 +41,61 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load);
 SWITCH_MODULE_DEFINITION(mod_commands, mod_commands_load, NULL, NULL);
 
 
+SWITCH_STANDARD_API(regex_function)
+{
+       switch_regex_t *re = NULL;
+       int ovector[30];
+       int argc;
+    char *mydata = NULL, *argv[3];
+       size_t len = 0;
+       char *substituted = NULL;
+       int proceed = 0;
+
+    if (!cmd) {
+        goto error;
+    }
+
+    mydata = strdup(cmd);
+    assert(mydata);
+       
+    argc = switch_separate_string(mydata, '|', argv, (sizeof(argv) / sizeof(argv[0])));
+
+    if (argc < 2) {
+        goto error;
+    }
+
+
+       if ((proceed = switch_regex_perform(argv[0], argv[1], &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
+               if (argc > 2) {
+                       len = strlen(argv[0]) * 3;
+                       substituted = malloc(len);
+                       assert(substituted);
+                       memset(substituted, 0, len);
+                       switch_replace_char(argv[2], '%','$', SWITCH_FALSE);
+                       switch_perform_substitution(re, proceed, argv[2], argv[0], substituted, len, ovector);
+
+                       stream->write_function(stream, "%s", substituted);                      
+                       free(substituted);
+               } else {
+                       stream->write_function(stream, "true");
+               }
+       } else {
+               stream->write_function(stream, "false");
+       }
+
+       goto ok;
+
+ error:
+    stream->write_function(stream, "!err!");    
+ ok:
+
+       switch_regex_safe_free(re);
+    switch_safe_free(mydata);
+       
+    return SWITCH_STATUS_SUCCESS;
+
+}
+
 typedef enum {
     O_NONE,
     O_EQ,
@@ -1640,6 +1695,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
        SWITCH_ADD_API(commands_api_interface, "xml_wrap", "Wrap another api command in xml", xml_wrap_api_function, "<command> <args>");
        SWITCH_ADD_API(commands_api_interface, "is_lan_addr", "see if an ip is a lan addr", lan_addr_function, "<ip>");
        SWITCH_ADD_API(commands_api_interface, "qq", "Eval a conditional", qq_function, "<expr> ? <true val> : <false val>");
+       SWITCH_ADD_API(commands_api_interface, "regex", "Eval a regex", regex_function, "<data>|<pattern>[|<subst string>]");
 
        /* indicate that the module should continue to be loaded */
        return SWITCH_STATUS_NOUNLOAD;
index 197154224d2c4adf50be1f354e8983a78e4062d2..ea7f1ed3ef4ccaa1eaadd3e9bb65a54efeb4f745 100644 (file)
@@ -111,7 +111,7 @@ SWITCH_STANDARD_API(expr_function)
 
     /* Parse expression */
     err = exprParse(e, (char *)expr);
-    printf("WTF %s\n", expr);
+
     if(err != EXPR_ERROR_NOERROR)
         goto error;
 
index eb37b28ad89de1b7762eba489479b51f9321bc59..91c2e7db2a3c8c48ceda1aa9e7929f2fd94b6174 100644 (file)
@@ -223,6 +223,26 @@ SWITCH_DECLARE(switch_bool_t) switch_is_lan_addr(const char *ip)
 }
 
 
+SWITCH_DECLARE(char *) switch_replace_char(char *str, char from, char to, switch_bool_t dup)
+{
+       char *p;
+
+       if (dup) {
+               p = strdup(str);
+               assert(p);
+       } else {
+               p = str;
+       }
+
+       for(;p && *p; p++) {
+               if (*p == from) {
+                       *p = to;
+               }
+       }
+
+       return p;
+}
+
 SWITCH_DECLARE(char *) switch_strip_spaces(const char *str)
 {
        const char *sp = str;