mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] improve string_list
@ 2011-06-10  6:57 Sascha Hauer
  2011-06-10  6:57 ` [PATCH 1/2] stringlist: use seperately allocated string Sascha Hauer
  2011-06-10  6:57 ` [PATCH 2/2] stringlist: implement string_list_asprintf Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2011-06-10  6:57 UTC (permalink / raw)
  To: barebox

This implements string_list_asprintf to put an entry to
a string_list using printf format.

Sascha Hauer (2):
      stringlist: use seperately allocated string.
      stringlist: implement string_list_asprintf

 include/stringlist.h |    7 +++++--
 lib/stringlist.c     |   28 +++++++++++++++++++++++++---
 2 files changed, 30 insertions(+), 5 deletions(-)


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] stringlist: use seperately allocated string.
  2011-06-10  6:57 [PATCH] improve string_list Sascha Hauer
@ 2011-06-10  6:57 ` Sascha Hauer
  2011-06-10  6:57 ` [PATCH 2/2] stringlist: implement string_list_asprintf Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2011-06-10  6:57 UTC (permalink / raw)
  To: barebox

Allocate the string in string list seperately instead of
embedding a zero length string into struct stringlist.
Besides looking cleaner this allows us to implement a
string_list_asprintf.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/stringlist.h |    6 ++++--
 lib/stringlist.c     |    6 +++---
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/include/stringlist.h b/include/stringlist.h
index 3453e9a..86edcf0 100644
--- a/include/stringlist.h
+++ b/include/stringlist.h
@@ -5,7 +5,7 @@
 
 struct string_list {
 	struct list_head list;
-	char str[0];
+	char *str;
 };
 
 int string_list_add(struct string_list *sl, char *str);
@@ -20,8 +20,10 @@ static inline void string_list_free(struct string_list *sl)
 {
 	struct string_list *entry, *safe;
 
-	list_for_each_entry_safe(entry, safe, &sl->list, list)
+	list_for_each_entry_safe(entry, safe, &sl->list, list) {
+		free(entry->str);
 		free(entry);
+	}
 }
 
 #endif /* __STRING_H */
diff --git a/lib/stringlist.c b/lib/stringlist.c
index 9ccf8fa..3c8ecec 100644
--- a/lib/stringlist.c
+++ b/lib/stringlist.c
@@ -1,15 +1,15 @@
 #include <common.h>
 #include <xfuncs.h>
 #include <malloc.h>
+#include <xfuncs.h>
 #include <stringlist.h>
 
 int string_list_add(struct string_list *sl, char *str)
 {
 	struct string_list *new;
 
-	new = xmalloc(sizeof(struct string_list) + strlen(str) + 1);
-
-	strcpy(new->str, str);
+	new = xmalloc(sizeof(*new));
+	new->str = xstrdup(str);
 
 	list_add_tail(&new->list, &sl->list);
 
-- 
1.7.5.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] stringlist: implement string_list_asprintf
  2011-06-10  6:57 [PATCH] improve string_list Sascha Hauer
  2011-06-10  6:57 ` [PATCH 1/2] stringlist: use seperately allocated string Sascha Hauer
@ 2011-06-10  6:57 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2011-06-10  6:57 UTC (permalink / raw)
  To: barebox

Useful for allocating a string list entry on the fly.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/stringlist.h |    1 +
 lib/stringlist.c     |   22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/include/stringlist.h b/include/stringlist.h
index 86edcf0..1e2616d 100644
--- a/include/stringlist.h
+++ b/include/stringlist.h
@@ -9,6 +9,7 @@ struct string_list {
 };
 
 int string_list_add(struct string_list *sl, char *str);
+int string_list_asprintf(struct string_list *sl, const char *fmt, ...);
 void string_list_print_by_column(struct string_list *sl);
 
 static inline void string_list_init(struct string_list *sl)
diff --git a/lib/stringlist.c b/lib/stringlist.c
index 3c8ecec..1015cac 100644
--- a/lib/stringlist.c
+++ b/lib/stringlist.c
@@ -1,6 +1,7 @@
 #include <common.h>
 #include <xfuncs.h>
 #include <malloc.h>
+#include <errno.h>
 #include <xfuncs.h>
 #include <stringlist.h>
 
@@ -16,6 +17,27 @@ int string_list_add(struct string_list *sl, char *str)
 	return 0;
 }
 
+int string_list_asprintf(struct string_list *sl, const char *fmt, ...)
+{
+	struct string_list *new;
+	va_list args;
+
+	new = xmalloc(sizeof(*new));
+
+	va_start(args, fmt);
+
+	new->str = asprintf(fmt, args);
+
+	va_end(args);
+
+	if (!new->str) {
+		free(new);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 void string_list_print_by_column(struct string_list *sl)
 {
 	int len = 0, num, i;
-- 
1.7.5.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-06-10  6:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-10  6:57 [PATCH] improve string_list Sascha Hauer
2011-06-10  6:57 ` [PATCH 1/2] stringlist: use seperately allocated string Sascha Hauer
2011-06-10  6:57 ` [PATCH 2/2] stringlist: implement string_list_asprintf Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox