-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicgrep_ext.cpp
38 lines (32 loc) · 1.13 KB
/
icgrep_ext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <sqlite3ext.h> /* Do not use <sqlite3.h>! */
#include "icgrep_common.h"
SQLITE_EXTENSION_INIT1
using namespace buffer;
static void icgrep_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
const char * regex = (const char *)sqlite3_value_text(argv[0]);
char * strToSearch = (char *)sqlite3_value_text(argv[1]);
const size_t len = strlen(static_cast<const char *>(strToSearch));
AlignedBuffer<char> buffer(len + 1);
buffer.writeData(0, strToSearch, len);
bool matchFound;
icgrep_grep(regex, &buffer, &matchFound);
sqlite3_result_int(context, static_cast<int>(matchFound));
}
extern "C" {
int sqlite3IcgrepInit(sqlite3 *db) {
int rc = SQLITE_OK;
rc = sqlite3_create_function(db, "icgrep", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
0, icgrep_func, 0, 0);
return rc;
}
#ifndef SQLITE_CORE
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_icgrepext_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
return sqlite3IcgrepInit(db);
}
#endif // SQLITE_CORE
} // extern "C"