Bump sdb fixing signed and numeric base issues

This commit is contained in:
pancake 2014-02-18 03:03:42 +01:00
parent 6f2baaa202
commit 99787f07a7
6 changed files with 60 additions and 35 deletions

View File

@ -105,7 +105,7 @@ SdbKv *sdb_dump_next (Sdb* s);
int sdb_dump_dupnext (Sdb* s, char **key, char **value);
/* numeric */
char *sdb_itoa(ut64 n, char *s);
char *sdb_itoa(ut64 n, char *s, int base);
ut64 sdb_atoi(const char *s);
ut64 sdb_getn (Sdb* s, const char *key, ut32 *cas);
int sdb_setn (Sdb* s, const char *key, ut64 v, ut32 cas);
@ -187,7 +187,8 @@ char *sdb_apop(Sdb *s, const char *key, ut32 *cas);
/* Util.c */
int sdb_check_value(const char *s);
int sdb_check_key(const char *s);
SDB_API int sdb_isnum (const char *s);
int sdb_isnum (const char *s);
int sdb_numbase (const char *s);
#ifdef __cplusplus
}

View File

@ -122,6 +122,7 @@ next_quote:
sdb_concat (s, cmd+1, val, 0);
}
} else {
int base = sdb_numbase (sdb_getc (s, cmd+1, 0));
if (json) {
*json = 0;
if (*cmd=='+') n = sdb_json_inc (s, cmd+1, json+1, d, 0);
@ -131,10 +132,19 @@ next_quote:
if (*cmd=='+') n = sdb_inc (s, cmd+1, d, 0);
else n = sdb_dec (s, cmd+1, d, 0);
}
w = snprintf (buf, len-1, "%"ULLFMT"d", n);
if (w<0 || (size_t)w>len) {
buf = malloc (0xff);
snprintf (buf, 0xff, "%"ULLFMT"d", n);
// TODO: keep base here
if (base==16) {
w = snprintf (buf, len-1, "0x%"ULLFMT"x", n);
if (w<0 || (size_t)w>len) {
buf = malloc (0xff);
snprintf (buf, 0xff, "0x%"ULLFMT"x", n);
}
} else {
w = snprintf (buf, len-1, "%"ULLFMT"d", n);
if (w<0 || (size_t)w>len) {
buf = malloc (0xff);
snprintf (buf, 0xff, "%"ULLFMT"d", n);
}
}
}
return buf;

View File

@ -105,7 +105,7 @@ SdbKv *sdb_dump_next (Sdb* s);
int sdb_dump_dupnext (Sdb* s, char **key, char **value);
/* numeric */
char *sdb_itoa(ut64 n, char *s);
char *sdb_itoa(ut64 n, char *s, int base);
ut64 sdb_atoi(const char *s);
ut64 sdb_getn (Sdb* s, const char *key, ut32 *cas);
int sdb_setn (Sdb* s, const char *key, ut64 v, ut32 cas);
@ -187,7 +187,8 @@ char *sdb_apop(Sdb *s, const char *key, ut32 *cas);
/* Util.c */
int sdb_check_value(const char *s);
int sdb_check_key(const char *s);
SDB_API int sdb_isnum (const char *s);
int sdb_isnum (const char *s);
int sdb_numbase (const char *s);
#ifdef __cplusplus
}

View File

@ -82,7 +82,7 @@ SDB_API char *sdb_aget(Sdb *s, const char *key, int idx, ut32 *cas) {
SDB_API int sdb_ainsn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) {
char valstr[64];
return sdb_ains (s, key, idx, sdb_itoa (val, valstr), cas);
return sdb_ains (s, key, idx, sdb_itoa (val, valstr, 10), cas);
}
// TODO: done, but there's room for improvement
@ -124,15 +124,14 @@ SDB_API int sdb_ains(Sdb *s, const char *key, int idx, const char *val, ut32 cas
SDB_API int sdb_asetn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) {
char valstr[64];
return sdb_aset (s, key, idx, sdb_itoa (val, valstr), cas);
return sdb_aset (s, key, idx, sdb_itoa (val, valstr, 10), cas);
}
SDB_API int sdb_aaddn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) {
char valstr[64];
sdb_itoa (val, valstr);
if (sdb_aexists (s, key, valstr))
char valstr[64], *vs = sdb_itoa (val, valstr, 10);
if (sdb_aexists (s, key, vs))
return 0;
return sdb_aadd (s, key, idx, valstr, cas);
return sdb_aadd (s, key, idx, vs, cas);
}
SDB_API int sdb_aadd(Sdb *s, const char *key, int idx, const char *val, ut32 cas) {

View File

@ -24,9 +24,10 @@ SDB_API ut64 sdb_getn(Sdb *s, const char *key, ut32 *cas) {
}
SDB_API int sdb_setn(Sdb *s, const char *key, ut64 v, ut32 cas) {
char b[128];
sdb_itoa (v, b);
return sdb_set (s, key, b, cas);
char *val, b[128];
int numbase = sdb_numbase (sdb_getc (s, key, NULL));
val = sdb_itoa (v, b, numbase);
return sdb_set (s, key, val, cas);
}
SDB_API ut64 sdb_inc(Sdb *s, const char *key, ut64 n2, ut32 cas) {

View File

@ -54,25 +54,30 @@ SDB_API ut32 sdb_hash(const char *s, int len) {
return h;
}
/* TODO: find algorithm without strrev */
/* TODO: try to avoid the use of heap */
static void __strrev(char *s, int len) {
int i, j = len -1;
for (i=0; i<j; i++, j--) {
char c = s[i];
s[i] = s[j];
s[j] = c;
SDB_API char *sdb_itoa(ut64 n, char *s, int base) {
int i = 63;
if (!s) s = malloc(65);
s[63] = '\0';
if (*s=='-') {
memcpy (s, "0", 2);
return s;
}
}
SDB_API char *sdb_itoa(ut64 n, char *s) {
int i = 0;
if (!s) s = malloc (64);
do s[i++] = n % 10 + '0';
while ((n /= 10) > 0);
s[i] = '\0';
__strrev (s, i);
return s;
memset (s, 0, 65);
if (base==16) {
static const char* lookup = "0123456789abcdef";
do {
s[i--] = lookup[(n % 16)];
if (i==0) break;
} while(n/=16);
s[i--] = 'x';
s[i--] = '0';
} else {
do {
s[i--] = (n % 10) + '0';
if (i==0) break;
} while(n/=10);
}
return s+i+1;
}
SDB_API ut64 sdb_atoi(const char *s) {
@ -121,3 +126,11 @@ SDB_API int sdb_isnum (const char *s) {
return 1;
return 0;
}
SDB_API int sdb_numbase (const char *s) {
if (!s) return 10;
if (!strncmp (s, "0x", 2))
return 16;
if (*s=='0' && s[1]) return 8;
return 10;
}