]> git.nbdom.net Git - nb.git/commitdiff
sqlite
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 26 Mar 2016 16:47:52 +0000 (16:47 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 26 Mar 2016 16:47:52 +0000 (16:47 +0000)
src/sqlite/Makefile [new file with mode: 0644]
src/sqlite/nb.c [new file with mode: 0644]

diff --git a/src/sqlite/Makefile b/src/sqlite/Makefile
new file mode 100644 (file)
index 0000000..4334d2d
--- /dev/null
@@ -0,0 +1,8 @@
+CC=g++
+CFLAGS=--shared
+all:
+       $(CC) $(CFLAGS) nb.c -o nb.o
+test:
+       echo Hello
+clean:
+       rm nb.o
diff --git a/src/sqlite/nb.c b/src/sqlite/nb.c
new file mode 100644 (file)
index 0000000..3a0c1ab
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+  http://stackoverflow.com/questions/5179451/gcc-regular-expressions
+  https://github.com/ralight/sqlite3-pcre/blob/master/pcre.c
+*/
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <regex.h>
+// NB 26.03.16 #include <pcre.h>
+#include <sqlite3.h>
+
+// g++ --shared -fPIC -I sqlite-autoconf-3071100 test.c -o test.o
+
+typedef sqlite3_int64 i64;
+char* sanitize(char*);
+// NB 26.03.16 char* stringReplace(char* subj,int start,int end,char* replace);
+// NB 26.03.16 char* pcreReplace(char* re,char* replace,char* txt);
+static void noaccentFunc(sqlite3_context *context, int argc, sqlite3_value **argv);
+static char* noaccent( char* str );
+
+
+/*
+  Sqlite functions
+*/
+// NB 26.03.16 static void pcreReplaceFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
+// NB 26.03.16   char *exp = (char *)sqlite3_value_text(argv[0]);
+// NB 26.03.16   char *replace = (char *)sqlite3_value_text(argv[1]);
+// NB 26.03.16   char *str = (char *)sqlite3_value_text(argv[2]);
+// NB 26.03.16   sqlite3_result_text(context, pcreReplace(exp,replace,str), -1, SQLITE_TRANSIENT);
+// NB 26.03.16   //return noaccent(sqlite3_value_text(argv[0]));
+// NB 26.03.16 }
+
+static void noaccentFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
+  //char str = (char *)sqlite3_value_text(argv[0]);
+  char *str = (char *)sqlite3_value_text(argv[0]);
+  sqlite3_result_text(context, noaccent(str), -1, SQLITE_TRANSIENT);
+  //return noaccent(sqlite3_value_text(argv[0]));
+}
+
+static void strcmpFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
+  char *v1 = (char *)sqlite3_value_text(argv[0]);
+  char *v2 = (char *)sqlite3_value_text(argv[1]);
+   //return sqlite3_result_int(context, v1 == v2);
+  sqlite3_result_int(context, strcmp(noaccent(v1),noaccent(v2)));
+  //return sqlite3_result_int(context, strcmp(*v1,*v2));
+}
+
+static void ifelseFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
+  int flag = sqlite3_value_int(argv[0]);
+  char *v1 = (char *)sqlite3_value_text(argv[1]);
+  char *v2 = (char *)sqlite3_value_text(argv[2]);
+
+  if (flag != 0)
+    sqlite3_result_text(context, v1, -1, SQLITE_TRANSIENT);
+  else
+    sqlite3_result_text(context, v2, -1, SQLITE_TRANSIENT);
+}
+
+int sqlite3_extension_init(sqlite3 *db, char **err) {
+  sqlite3_create_function( db, "ifelse", 3, SQLITE_ANY, NULL, ifelseFunc, NULL, NULL);
+  sqlite3_create_function( db, "strcmp", 2, SQLITE_ANY, NULL, strcmpFunc, NULL, NULL);
+  sqlite3_create_function( db, "noaccent", 1, SQLITE_ANY, NULL, noaccentFunc, NULL, NULL);
+// NB 26.03.16   sqlite3_create_function( db, "pcreReplace", 3, SQLITE_ANY, NULL, pcreReplaceFunc, NULL, NULL);
+  return 0;
+}
+
+/*
+  Functions
+*/
+static char* noaccent( char* str ) {
+  char *p = str;
+  while ( (*p)!=0 ) {
+    const char*
+      //   "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"
+      tr = "AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiiOnooooo/0uuuuypy";
+    unsigned char ch = (*p);
+    if ( ch >=192 ) {
+      (*p) = tr[ ch-192 ];
+    }
+    ++p; // http://stackoverflow.com/questions/14094621/
+  }
+  return str;
+}
+
+// NB 26.03.16 char* pcreReplace(char* re,char* replace,char* txt) {
+// NB 26.03.16     const char *error;
+// NB 26.03.16     int erroffset;
+// NB 26.03.16     int ovector[186];
+// NB 26.03.16 
+// NB 26.03.16     pcre *r =  pcre_compile(re, PCRE_CASELESS|PCRE_DOTALL, &error, &erroffset, NULL);
+// NB 26.03.16     int rc = pcre_exec(r, NULL, txt, strlen(txt), 0, 0, ovector, 186);
+// NB 26.03.16     //printf("%s\n",txt);
+// NB 26.03.16 
+// NB 26.03.16     if(rc<0)
+// NB 26.03.16         return txt;
+// NB 26.03.16 
+// NB 26.03.16     //printf("%i",ovector[0]);
+// NB 26.03.16     //char c1[1024];
+// NB 26.03.16     //pcre_copy_substring(txt, ovector, rc,1,c1, 1024);
+// NB 26.03.16     txt = stringReplace(txt,ovector[0],ovector[2],replace);
+// NB 26.03.16     return txt;
+// NB 26.03.16 }
+
+
+// NB 26.03.16 char* stringReplace(char* subj,int start,int end,char* replace) {
+// NB 26.03.16     int newLength = strlen(subj)-(end-start+1)+strlen(replace);
+// NB 26.03.16     //char newString[newLength];
+// NB 26.03.16     char *newString = malloc (sizeof (char) * newLength);
+// NB 26.03.16     int i;
+// NB 26.03.16     int itr=0;
+// NB 26.03.16     for(i=0;i<strlen(subj);i++)
+// NB 26.03.16     {
+// NB 26.03.16         if(i<start || i>end)
+// NB 26.03.16         {
+// NB 26.03.16             newString[itr]=subj[i];
+// NB 26.03.16             itr++;
+// NB 26.03.16         }
+// NB 26.03.16     }
+// NB 26.03.16     newString[newLength] = '\0';
+// NB 26.03.16     return newString;
+// NB 26.03.16 }