aboutsummaryrefslogtreecommitdiffstats
path: root/epan/uat.c
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2016-09-30 10:46:28 +0200
committerMichael Mann <mmann78@netscape.net>2016-10-01 12:08:02 +0000
commit7ea363223a5a5a872d66a65570c134c34aa51b49 (patch)
tree7ae5f1e46f8feabd026842bf19acf71767194e36 /epan/uat.c
parent2dfc0da22cc4c525c32baa2684666643d4bf23a0 (diff)
uat: allow insertion of new record at arbitrary index
This should make drag-and-drop support (reordering) in Qt easier. It also ensures that memcpy is used as fallback if copy_cb does not exist. Change-Id: Iefe358890c49dcda4727054f7a2cee05614a36f6 Reviewed-on: https://code.wireshark.org/review/17992 Reviewed-by: Peter Wu <peter@lekensteyn.nl> Petri-Dish: Peter Wu <peter@lekensteyn.nl> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Michael Mann <mmann78@netscape.net>
Diffstat (limited to 'epan/uat.c')
-rw-r--r--epan/uat.c39
1 files changed, 25 insertions, 14 deletions
diff --git a/epan/uat.c b/epan/uat.c
index 328920266d..461dee63d5 100644
--- a/epan/uat.c
+++ b/epan/uat.c
@@ -131,14 +131,7 @@ void* uat_add_record(uat_t* uat, const void* data, gboolean valid_rec) {
void* rec;
gboolean* valid;
- /* Save a copy of the raw (possibly that may contain invalid field values) data */
- g_array_append_vals (uat->raw_data, data, 1);
-
- rec = UAT_INDEX_PTR(uat, uat->raw_data->len - 1);
-
- if (uat->copy_cb) {
- uat->copy_cb(rec, data, (unsigned int) uat->record_size);
- }
+ uat_insert_record_idx(uat, uat->raw_data->len, data);
if (valid_rec) {
/* Add a "known good" record to the list to be used by the dissector */
@@ -151,14 +144,13 @@ void* uat_add_record(uat_t* uat, const void* data, gboolean valid_rec) {
}
UAT_UPDATE(uat);
+
+ valid = &g_array_index(uat->valid_data, gboolean, uat->valid_data->len-1);
+ *valid = valid_rec;
} else {
rec = NULL;
}
- g_array_append_vals (uat->valid_data, &valid_rec, 1);
- valid = &g_array_index(uat->valid_data, gboolean, uat->valid_data->len-1);
- *valid = valid_rec;
-
return rec;
}
@@ -204,6 +196,25 @@ void uat_swap(uat_t* uat, guint a, guint b) {
}
+void uat_insert_record_idx(uat_t* uat, guint idx, const void *src_record) {
+ /* Allow insert before an existing item or append after the last item. */
+ g_assert( idx <= uat->raw_data->len );
+
+ /* Store a copy of the record and invoke copy_cb to clone pointers too. */
+ g_array_insert_vals(uat->raw_data, idx, src_record, 1);
+ void *rec = UAT_INDEX_PTR(uat, idx);
+ if (uat->copy_cb) {
+ uat->copy_cb(rec, src_record, (unsigned int) uat->record_size);
+ } else {
+ memcpy(rec, src_record, (unsigned int) uat->record_size);
+ }
+
+ /* Initially assume that the record is invalid, it is not copied to the
+ * user-visible records list. */
+ gboolean valid_rec = FALSE;
+ g_array_insert_val(uat->valid_data, idx, valid_rec);
+}
+
void uat_remove_record_idx(uat_t* uat, guint idx) {
g_assert( idx < uat->raw_data->len );
@@ -342,8 +353,8 @@ gboolean uat_save(uat_t* uat, char** error) {
/* Now copy "good" raw_data entries to user_data */
for ( i = 0 ; i < uat->raw_data->len ; i++ ) {
void *rec = UAT_INDEX_PTR(uat, i);
- gboolean* valid = (gboolean*)(uat->valid_data->data + sizeof(gboolean)*i);
- if (*valid) {
+ gboolean valid = g_array_index(uat->valid_data, gboolean, i);
+ if (valid) {
g_array_append_vals(uat->user_data, rec, 1);
if (uat->copy_cb) {
uat->copy_cb(UAT_USER_INDEX_PTR(uat, uat->user_data->len - 1),