From 27534580a198c01fe4806d51e87c088a0444f4e8 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 30 Jan 2014 23:45:30 +0100 Subject: [PATCH] Bump sdb again --- libr/include/sdb/sdb.h | 3 ++- shlr/sdb/README.md | 2 +- shlr/sdb/src/cdb.c | 4 +--- shlr/sdb/src/main.c | 4 ++-- shlr/sdb/src/query.c | 22 ++++++++++++++-------- shlr/sdb/src/sdb.c | 2 ++ shlr/sdb/src/sdb.h | 3 ++- shlr/sdb/src/sdba.c | 4 +++- 8 files changed, 27 insertions(+), 17 deletions(-) diff --git a/libr/include/sdb/sdb.h b/libr/include/sdb/sdb.h index 733970a674..4d38d7ab33 100644 --- a/libr/include/sdb/sdb.h +++ b/libr/include/sdb/sdb.h @@ -49,7 +49,8 @@ typedef struct sdb_kv { } SdbKv; typedef struct sdb_t { - char *dir; // path+file + char *dir; // path+name + char *path; char *name; int fd; int lock; diff --git a/shlr/sdb/README.md b/shlr/sdb/README.md index a28af2e909..81fe18e3c2 100644 --- a/shlr/sdb/README.md +++ b/shlr/sdb/README.md @@ -52,7 +52,7 @@ Let's create a database! Using arrays (>=0.6): - $ sdb - '()list=1,2' '(0)list' '(0)list=foo' '()list' '(+1)list=bar' + $ sdb - '[]list=1,2' '[0]list' '[0]list=foo' '[]list' '[+1]list=bar' 1 foo 2 diff --git a/shlr/sdb/src/cdb.c b/shlr/sdb/src/cdb.c index 51494f84e1..065494e196 100644 --- a/shlr/sdb/src/cdb.c +++ b/shlr/sdb/src/cdb.c @@ -100,9 +100,7 @@ int cdb_findnext(struct cdb *c, ut32 u, const char *key, unsigned int len) { } ut32_unpack (buf, &c->hpos); c->khash = u; - u >>= 8; - u %= c->hslots; - u <<= 3; + u = ((u>>8)%c->hslots)<<3; c->kpos = c->hpos + u; } diff --git a/shlr/sdb/src/main.c b/shlr/sdb/src/main.c index 677d54111c..121a557b60 100644 --- a/shlr/sdb/src/main.c +++ b/shlr/sdb/src/main.c @@ -30,7 +30,7 @@ static void syncronize(int sig UNUSED) { // TODO: must be in sdb_sync() or wat? Sdb *n; sdb_sync (s); - n = sdb_new (s->dir, s->name, s->lock); + n = sdb_new (s->path, s->name, s->lock); sdb_free (s); s = n; } @@ -52,7 +52,7 @@ static int sdb_dump (const char *db) { static void createdb(const char *f) { char *line, *eq; - s = sdb_new (f, f, 0); + s = sdb_new (NULL, f, 0); if (!sdb_create (s)) { printf ("Cannot create database\n"); exit (1); diff --git a/shlr/sdb/src/query.c b/shlr/sdb/src/query.c index 1571b80f03..261db5fdfd 100644 --- a/shlr/sdb/src/query.c +++ b/shlr/sdb/src/query.c @@ -43,10 +43,10 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) { } if (!len || !buf) buf = malloc ((len=64)); ask = strchr (cmd, '?'); - if (*cmd == '(') { - char *tp = strchr (cmd, ')'); + if (*cmd == '[') { + char *tp = strchr (cmd, ']'); if (!tp) { - fprintf (stderr, "Missing ')'.\n"); + fprintf (stderr, "Missing ']'.\n"); return NULL; } *tp++ = 0; @@ -97,7 +97,8 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) { } return buf; } - } else if (*cmd == '(') { + } else if (*cmd == '[') { + // [?] - count elements of array if (cmd[1]=='?') { // if (!eq) { ... alength = sdb_alength (s, p); @@ -109,14 +110,19 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) { return buf; } if (cmd[1]=='+'||cmd[1]=='-') { - /* (+)foo=bla (-)foo=bla */ - if (!cmd[2] || cmd[2] ==')') { + // [+]foo remove first element */ + // [+]foo=bar PUSH */ + // [-]foo POP */ + // [-]foo=xx POP (=xx ignored) */ + if (!cmd[2] || cmd[2] ==']') { // insert if (eq) { if (cmd[1]=='+') { if (sdb_agetv (s, p, val, 0)== -1) sdb_aset (s, p, -1, val, 0); - } else sdb_adels (s, p, val, 0); + } else { + sdb_adels (s, p, val, 0); + } return NULL; } else { char *ret; @@ -125,7 +131,7 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) { ret = sdb_aget (s, p, 0, 0); // (+)foo :: remove first element sdb_adel (s, p, 0, 0); - } else { + } else { // POP ret = sdb_aget (s, p, -1, 0); // (-)foo :: remove last element sdb_adel (s, p, -1, 0); diff --git a/shlr/sdb/src/sdb.c b/shlr/sdb/src/sdb.c index cad7ac6369..893e18cc4d 100644 --- a/shlr/sdb/src/sdb.c +++ b/shlr/sdb/src/sdb.c @@ -43,9 +43,11 @@ SDB_VISIBLE Sdb* sdb_new (const char *path, const char *name, int lock) { s->fd = open (s->dir, O_RDONLY|O_BINARY); // if (s->fd == -1) // must fail if we cant open for write in sync s->name = strdup (name); + s->path = path? strdup (path): NULL; } else { s->dir = NULL; s->name = NULL; + s->path = NULL; s->fd = -1; } s->fdump = -1; diff --git a/shlr/sdb/src/sdb.h b/shlr/sdb/src/sdb.h index 733970a674..4d38d7ab33 100644 --- a/shlr/sdb/src/sdb.h +++ b/shlr/sdb/src/sdb.h @@ -49,7 +49,8 @@ typedef struct sdb_kv { } SdbKv; typedef struct sdb_t { - char *dir; // path+file + char *dir; // path+name + char *path; char *name; int fd; int lock; diff --git a/shlr/sdb/src/sdba.c b/shlr/sdb/src/sdba.c index 8b2aaf6eac..e6f0a22dbb 100644 --- a/shlr/sdb/src/sdba.c +++ b/shlr/sdb/src/sdba.c @@ -1,4 +1,4 @@ -/* sdb - LGPLv3 - Copyright 2011-2013 - pancake */ +/* sdb - LGPLv3 - Copyright 2011-2014 - pancake */ #include "sdb.h" @@ -255,6 +255,8 @@ SDB_VISIBLE int sdb_adel(Sdb *s, const char *key, int idx, ut32 cas) { if (n) { memmove (p, n+1, strlen (n+1)+1); } else { + if (p != str) + p--; // remove tailing SDB_RS *p = 0; p[1] = 0; }