mirror of
https://github.com/radareorg/radare2.git
synced 2025-02-19 05:38:43 +00:00
Update SDB code and use ht_update_key API in RFlag (#12804)
This commit is contained in:
parent
9c12720fb7
commit
adab5e4302
@ -87,13 +87,16 @@ static void remove_offsetmap(RFlag *f, RFlagItem *item) {
|
||||
|
||||
static bool set_name(RFlagItem *item, const char *name) {
|
||||
r_return_val_if_fail (item && name, false);
|
||||
|
||||
char *new_name = strdup (name);
|
||||
if (!new_name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (item->name != item->realname) {
|
||||
free (item->name);
|
||||
}
|
||||
item->name = strdup (name);
|
||||
if (!item->name) {
|
||||
return false;
|
||||
}
|
||||
item->name = new_name;
|
||||
r_str_trim (item->name);
|
||||
r_name_filter (item->name, 0); // TODO: name_filter should be chopping already
|
||||
free (item->realname);
|
||||
@ -624,15 +627,13 @@ R_API void r_flag_item_set_realname(RFlagItem *item, const char *realname) {
|
||||
R_API int r_flag_rename(RFlag *f, RFlagItem *item, const char *name) {
|
||||
r_return_val_if_fail (f && item && name && *name, false);
|
||||
|
||||
// TODO: add API in ht to update the key of an existing element
|
||||
HtPPKvFreeFunc ofreefn = f->ht_name->opt.freefn;
|
||||
f->ht_name->opt.freefn = NULL;
|
||||
ht_pp_delete (f->ht_name, item->name);
|
||||
f->ht_name->opt.freefn = ofreefn;
|
||||
char *old_name = strdup (item->name);
|
||||
if (!set_name (item, name)) {
|
||||
free (old_name);
|
||||
return false;
|
||||
}
|
||||
ht_pp_insert (f->ht_name, item->name, item);
|
||||
ht_pp_update_key (f->ht_name, old_name, name);
|
||||
free (old_name);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -92,6 +92,8 @@ SDB_API void Ht_(free)(HtName_(Ht)* ht);
|
||||
SDB_API bool Ht_(insert)(HtName_(Ht)* ht, const KEY_TYPE key, VALUE_TYPE value);
|
||||
// Insert a new Key-Value pair into the hashtable, or updates the value if the key already exists.
|
||||
SDB_API bool Ht_(update)(HtName_(Ht)* ht, const KEY_TYPE key, VALUE_TYPE value);
|
||||
// Update the key of an element in the hashtable
|
||||
SDB_API bool Ht_(update_key)(HtName_(Ht)* ht, const KEY_TYPE old_key, const KEY_TYPE new_key);
|
||||
// Delete a key from the hashtable.
|
||||
SDB_API bool Ht_(delete)(HtName_(Ht)* ht, const KEY_TYPE key);
|
||||
// Find the value corresponding to the matching key.
|
||||
|
@ -238,6 +238,50 @@ SDB_API bool Ht_(update)(HtName_(Ht)* ht, const KEY_TYPE key, VALUE_TYPE value)
|
||||
return insert_update (ht, key, value, true);
|
||||
}
|
||||
|
||||
// Update the key of an element that has old_key as key and replace it with new_key
|
||||
SDB_API bool Ht_(update_key)(HtName_(Ht)* ht, const KEY_TYPE old_key, const KEY_TYPE new_key) {
|
||||
// First look for the value associated with old_key
|
||||
bool found;
|
||||
VALUE_TYPE value = Ht_(find) (ht, old_key, &found);
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Associate the existing value with new_key
|
||||
bool inserted = insert_update (ht, new_key, value, false);
|
||||
if (!inserted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove the old_key kv, paying attention to not double free the value
|
||||
HT_(Bucket) *bt = &ht->table[bucketfn (ht, old_key)];
|
||||
const int old_key_len = calcsize_key (ht, old_key);
|
||||
HT_(Kv) *kv;
|
||||
ut32 j;
|
||||
|
||||
BUCKET_FOREACH (ht, bt, j, kv) {
|
||||
if (is_kv_equal (ht, old_key, old_key_len, kv)) {
|
||||
if (!ht->opt.dupvalue) {
|
||||
// do not free the value part if dupvalue is not
|
||||
// set, because the old value has been
|
||||
// associated with the new key and it should not
|
||||
// be freed
|
||||
kv->value = HT_NULL_VALUE;
|
||||
kv->value_len = 0;
|
||||
}
|
||||
freefn (ht, kv);
|
||||
|
||||
void *src = next_kv (ht, kv);
|
||||
memmove (kv, src, (bt->count - j - 1) * ht->opt.elem_size);
|
||||
bt->count--;
|
||||
ht->count--;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Returns the corresponding SdbKv entry from the key.
|
||||
// If `found` is not NULL, it will be set to true if the entry was found, false
|
||||
// otherwise.
|
||||
|
@ -92,6 +92,8 @@ SDB_API void Ht_(free)(HtName_(Ht)* ht);
|
||||
SDB_API bool Ht_(insert)(HtName_(Ht)* ht, const KEY_TYPE key, VALUE_TYPE value);
|
||||
// Insert a new Key-Value pair into the hashtable, or updates the value if the key already exists.
|
||||
SDB_API bool Ht_(update)(HtName_(Ht)* ht, const KEY_TYPE key, VALUE_TYPE value);
|
||||
// Update the key of an element in the hashtable
|
||||
SDB_API bool Ht_(update_key)(HtName_(Ht)* ht, const KEY_TYPE old_key, const KEY_TYPE new_key);
|
||||
// Delete a key from the hashtable.
|
||||
SDB_API bool Ht_(delete)(HtName_(Ht)* ht, const KEY_TYPE key);
|
||||
// Find the value corresponding to the matching key.
|
||||
|
@ -12,7 +12,11 @@ SDB_API SdbList *ls_newf(SdbListFree freefn) {
|
||||
}
|
||||
|
||||
SDB_API SdbList *ls_new() {
|
||||
return R_NEW0 (SdbList);
|
||||
SdbList *list = R_NEW0 (SdbList);
|
||||
if (!list) {
|
||||
return NULL;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
static void ls_insertion_sort_iter(SdbListIter *iter, SdbListComparator cmp) {
|
||||
|
@ -229,6 +229,7 @@ repeat:
|
||||
if (next) *next = ';';
|
||||
eq = strchr (p, '=');
|
||||
if (eq) {
|
||||
d = 1;
|
||||
*eq++ = 0;
|
||||
if (*eq == '$') {
|
||||
next = strchr (eq + 1, ';');
|
||||
@ -245,6 +246,7 @@ repeat:
|
||||
}
|
||||
} else {
|
||||
val = NULL;
|
||||
d = 0;
|
||||
}
|
||||
if (!is_ref) {
|
||||
next = strchr (val? val: cmd, ';');
|
||||
@ -376,6 +378,7 @@ next_quote:
|
||||
fflush (stdout);
|
||||
ls_free (l);
|
||||
} else {
|
||||
d = 1;
|
||||
sdb_unset_like (s, cmd + 1);
|
||||
}
|
||||
} else if (*cmd == '+' || *cmd == '-') {
|
||||
@ -810,6 +813,7 @@ runNext:
|
||||
bufset = 0;
|
||||
}
|
||||
cmd = next + 1;
|
||||
encode = 0;
|
||||
goto repeat;
|
||||
}
|
||||
if (eq) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user