github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/sqlite3/_example/mod_regexp/sqlite3_mod_regexp.c (about)

     1  #include <pcre.h>
     2  #include <string.h>
     3  #include <stdio.h>
     4  #include <sqlite3ext.h>
     5  
     6  SQLITE_EXTENSION_INIT1
     7  static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
     8    if (argc >= 2) {
     9      const char *target  = (const char *)sqlite3_value_text(argv[1]);
    10      const char *pattern = (const char *)sqlite3_value_text(argv[0]);
    11      const char* errstr = NULL;
    12      int erroff = 0;
    13      int vec[500];
    14      int n, rc;
    15      pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL);
    16      rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500); 
    17      if (rc <= 0) {
    18        sqlite3_result_error(context, errstr, 0);
    19        return;
    20      }
    21      sqlite3_result_int(context, 1);
    22    }
    23  }
    24  
    25  #ifdef _WIN32
    26  __declspec(dllexport)
    27  #endif
    28  int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) {
    29    SQLITE_EXTENSION_INIT2(api);
    30    return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, (void*)db, regexp_func, NULL, NULL);
    31  }