Skip to contentSkip to conversation

stringlist-functions: add sorted insert

PATCH Sascha Hauer 1 participant 1 message Dec 17, 2011 2 messages not in the archive

lore
Share & tools
lore
#2 lore raw
In reply to a message not in the archive
Quoted 28 lines · Alexander Aring

Add sorted insert in stringlist functions. Also added function to checked if string is already in string list.

---
 include/stringlist.h |    2 ++
 lib/stringlist.c     |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)
include/stringlist.h +2 -0
index 3453e9a..c923542 100644
@@ -9,6 +9,8 @@ struct string_list {
 };
 
 int string_list_add(struct string_list *sl, char *str);
+int string_list_add_sorted(struct string_list *sl, char *str);
+int string_list_contains(struct string_list *sl, char *str);
 void string_list_print_by_column(struct string_list *sl);
 
 static inline void string_list_init(struct string_list *sl)
lib/stringlist.c +0 -0
index 9ccf8fa..65c0117 100644

lib/stringlist.c line 11

Quoted 9 lines
lib/stringlist.c +6 -0
@@ -3,6 +3,21 @@
 #include <malloc.h>
 #include <stringlist.h>
 
+static int string_list_compare(struct list_head *a, struct list_head *b)
+{
+	int result = 0;
+	char *astr, *bstr;
+	astr = ((struct string_list *)(a))->str;
+	bstr = ((struct string_list *)(b))->str;

Please do not cast to pointers of different types directly. Use container_of or list_entry.

lib/stringlist.c line 18

Quoted 7 lines
lib/stringlist.c +7 -0
+
+	result = strcmp(astr, bstr);
+
+	/* Avoid a endless loop because >=0 insert str in list
+	 * Insert string if strings are equal or is equal,
+	 * but lengths are not the same */
+	return result;

I don't understand this comment. Is it valid?

Quoted 41 lines
lib/stringlist.c +27 -0
+}
+
 int string_list_add(struct string_list *sl, char *str)
 {
 	struct string_list *new;
@@ -16,6 +31,31 @@ int string_list_add(struct string_list *sl, char *str)
 	return 0;
 }
 
+int string_list_add_sorted(struct string_list *sl, char *str)
+{
+	struct string_list *new;
+
+	new = xmalloc(sizeof(struct string_list) + strlen(str) + 1);
+
+	strcpy(new->str, str);
+
+	list_add_sort(&new->list, &sl->list, string_list_compare);
+
+	return 0;
+}
+
+int string_list_contains(struct string_list *sl, char *str)
+{
+	struct string_list *entry;
+
+	list_for_each_entry(entry, &sl->list, list) {
+		if (!strcmp(str, entry->str))
+			return 1;
+	}
+
+	return 0;
+}
+
 void string_list_print_by_column(struct string_list *sl)
 {
 	int len = 0, num, i;
Signature · 1 line
1.7.0.4
Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox