diff mbox series

[02/17] dictionary: allow duplication of keys

Message ID 1511176210-28928-2-git-send-email-sbabic@denx.de
State Accepted
Headers show
Series [01/17] parser: added function to get net child in tree | expand

Commit Message

Stefano Babic Nov. 20, 2017, 11:09 a.m. UTC
Add function to just insert and not replace an entry in a dictionary.
This helps in case key is duplicated with more values.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 corelib/swupdate_dict.c | 29 +++++++++++++++++------------
 include/swupdate_dict.h |  1 +
 2 files changed, 18 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/corelib/swupdate_dict.c b/corelib/swupdate_dict.c
index 1fc8007..46e8203 100644
--- a/corelib/swupdate_dict.c
+++ b/corelib/swupdate_dict.c
@@ -43,6 +43,22 @@  static struct dict_entry *get_entry(struct dictlist *dictionary, char *key)
 	return NULL;
 }
 
+int dict_insert_entry(struct dictlist *dictionary, char *key, char *value)
+{
+	struct dict_entry *entry = (struct dict_entry *)malloc(sizeof(*entry));
+
+	if (!entry)
+		return -ENOMEM;
+
+	memset(entry, 0, sizeof(*entry));
+	entry->varname = strdup(key);
+	entry->value = strdup(value);
+
+	LIST_INSERT_HEAD(dictionary, entry, next);
+
+	return 0;
+}
+
 char *dict_get_value(struct dictlist *dictionary, char *key)
 {
 	struct dict_entry *entry = get_entry(dictionary, key);
@@ -66,18 +82,7 @@  int dict_set_value(struct dictlist *dictionary, char *key, char *value)
 		free(entry);
 	}
 
-	entry = (struct dict_entry *)malloc(sizeof(*entry));
-
-	if (!entry)
-		return -ENOMEM;
-
-	memset(entry, 0, sizeof(*entry));
-	entry->varname = strdup(key);
-	entry->value = strdup(value);
-
-	LIST_INSERT_HEAD(dictionary, entry, next);
-
-	return 0;
+	return dict_insert_entry(dictionary, key, value);
 }
 
 void dict_remove_entry(struct dict_entry *entry)
diff --git a/include/swupdate_dict.h b/include/swupdate_dict.h
index 86f739e..1bd2f5e 100644
--- a/include/swupdate_dict.h
+++ b/include/swupdate_dict.h
@@ -33,6 +33,7 @@  LIST_HEAD(dictlist, dict_entry);
 
 char *dict_get_value(struct dictlist *dictionary, char *key);
 int dict_set_value(struct dictlist *dictionary, char *key, char *value);
+int dict_insert_entry(struct dictlist *dictionary, char *key, char *value);
 void dict_remove(struct dictlist *dictionary, char *key);
 void dict_remove_entry(struct dict_entry *entry);