1. wodny
  2. python-dbm-experiments

Commits

Marcin Szewczyk  committed c89cfc9

regex in C

  • Participants
  • Parent commits 66625e7
  • Branches master

Comments (0)

Files changed (2)

File Makefile

View file
  • Ignore whitespace
 dictionary-py.strings: dictionary-py.db
 	strings $< | sort -u > $@
 
-dictionary-c.db: pushtodbm dictionary.txt
-	./pushtodbm
+# No leak
+#dictionary-c.db: pushtodbm dictionary.txt
+#	./pushtodbm
+
+# No leak either
+dictionary-c.db: pushtodbm2 dictionary.txt
+	./pushtodbm2
 
 pushtodbm: pushtodbm.c
 	gcc $< -o $@ -ldb
 
+pushtodbm2: pushtodbm2.c
+	gcc $< -o $@ -ldb
+
 # No leak
 #dictionary-py.db: pushtodbm.py dictionary.txt
 #	./pushtodbm.py
 	[ -e de-en.txt ] || wget http://ftp.tu-chemnitz.de/pub/Local/urz/ding/de-en/de-en.txt.gz
 
 clean:
-	rm -f pushtodbm dictionary.* dictionary-c.* dictionary-py.*
+	rm -f pushtodbm pushtodbm2 dictionary.* dictionary-c.* dictionary-py.*

File pushtodbm2.c

View file
  • Ignore whitespace
+#include <db.h> 
+#include <string.h>
+#include <regex.h>
+#include <assert.h>
+
+int main() {
+    regex_t linere;
+    assert(regcomp(&linere, "^(\\w+) \\{(\\w)\\}", REG_EXTENDED) == 0);
+
+    DB *dbp;           /* DB structure handle */
+    u_int32_t flags;   /* database open flags */
+    int ret;           /* function return value */
+    
+    /* Initialize the structure. This
+     * database is not opened in an environment, 
+     * so the environment pointer is NULL. */
+    ret = db_create(&dbp, NULL, 0);
+    if (ret != 0) {
+      /* Error handling goes here */
+    }
+    
+    /* Database open flags */
+    flags = DB_CREATE;    /* If the database does not exist, 
+                           * create it.*/
+    
+    /* open the database */
+    ret = dbp->open(dbp,        /* DB structure pointer */
+                    NULL,       /* Transaction pointer */
+                    "dictionary-c.db", /* On-disk file that holds the database. */
+                    NULL,       /* Optional logical database name */
+                    DB_HASH,    /* Database access method */
+                    flags,      /* Open flags */
+                    0);         /* File mode (using defaults) */
+    if (ret != 0) {
+      /* Error handling goes here */
+    }
+
+    char buffer[1024];
+    char *ptr, *word, *art;
+    word = buffer;
+    DBT key, data;
+
+    FILE *f;
+
+    f = fopen("de-en.txt", "r");
+
+    while(fgets(buffer, 1024, f)) {
+         regmatch_t matches[5];
+         if(regexec(&linere, buffer, 3, matches, 0) == 0) {
+            *(buffer + matches[1].rm_eo) = '\0';
+            *(buffer + matches[2].rm_eo) = '\0';
+            art = buffer + matches[2].rm_so;
+
+            //printf("%s -- %s\n", word, art);
+
+            memset(&key, 0, sizeof(DBT));
+            memset(&data, 0, sizeof(DBT));
+            
+            key.data = word;
+            key.size = strlen(word);
+            data.data = art;
+            data.size = strlen(art); 
+            
+            ret = dbp->put(dbp, NULL, &key, &data, 0);
+        }
+    }
+
+    if (dbp != NULL)
+        dbp->close(dbp, 0); 
+
+    fclose(f);
+}