Skip to contentSkip to conversation

master - refactor: rename alloc_printed_tags fn to _alloc_printed_str_list and use it for string lists in general

PATCH Peter Rajnoha 1 participant 1 message Jun 29, 2015

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=e29d4773f4de731d8483a12684d5f4b96092088b
Commit:        e29d4773f4de731d8483a12684d5f4b96092088b
Parent:        77c2d11657d0b1bdb82e02cbbc6cf025d8b2f43e
Author:        Peter Rajnoha <[email protected]>
AuthorDate:    Wed May 6 13:58:17 2015 +0200
Committer:     Peter Rajnoha <[email protected]>
CommitterDate: Mon Jun 29 09:43:41 2015 +0200

refactor: rename alloc_printed_tags fn to _alloc_printed_str_list and use it for string lists in general

---
 lib/format_text/export.c        |   43 ++++++++++++++++++++++++++++++++++++++-
 lib/format_text/import-export.h |    2 -
 lib/format_text/tags.c          |   41 -------------------------------------
 3 files changed, 42 insertions(+), 44 deletions(-)
lib/format_text/export.c +42 -1
index 018772e..a1af06b 100644
@@ -372,13 +372,54 @@ static int _print_flag_config(struct formatter *f, uint64_t status, int type)
 	return 1;
 }
 
+static char *_alloc_printed_str_list(struct dm_list *list)
+{
+	struct dm_str_list *sl;
+	int first = 1;
+	size_t size = 0;
+	char *buffer, *buf;
+
+	dm_list_iterate_items(sl, list)
+		/* '"' + item + '"' + ',' + ' ' */
+		size += strlen(sl->str) + 4;
+	/* '[' + ']' + '\0' */
+	size += 3;
+
+	if (!(buffer = buf = dm_malloc(size))) {
+		log_error("Could not allocate memory for string list buffer.");
+		return NULL;
+	}
+
+	if (!emit_to_buffer(&buf, &size, "["))
+		goto_bad;
+
+	dm_list_iterate_items(sl, list) {
+		if (!first) {
+			if (!emit_to_buffer(&buf, &size, ", "))
+				goto_bad;
+		} else
+			first = 0;
+
+		if (!emit_to_buffer(&buf, &size, "\"%s\"", sl->str))
+			goto_bad;
+	}
+
+	if (!emit_to_buffer(&buf, &size, "]"))
+		goto_bad;
+
+	return buffer;
+
+bad:
+	dm_free(buffer);
+	return_NULL;
+}
 
 static int _out_tags(struct formatter *f, struct dm_list *tagsl)
 {
 	char *tag_buffer;
 
 	if (!dm_list_empty(tagsl)) {
-		if (!(tag_buffer = alloc_printed_tags(tagsl)))
+		if (!(tag_buffer = _alloc_printed_str_list(tagsl)))
 			return_0;
 		if (!out_text(f, "tags = %s", tag_buffer)) {
 			dm_free(tag_buffer);
lib/format_text/import-export.h +0 -2
index 18561d3..be88994 100644
@@ -61,8 +61,6 @@ struct text_vg_version_ops *text_vg_vsn1_init(void);
 int print_flags(uint64_t status, int type, char *buffer, size_t size);
 int read_flags(uint64_t *status, int type, const struct dm_config_value *cv);
 
-char *alloc_printed_tags(struct dm_list *tags);
-
 int text_vg_export_file(struct volume_group *vg, const char *desc, FILE *fp);
 size_t text_vg_export_raw(struct volume_group *vg, const char *desc, char **buf);
 struct volume_group *text_vg_import_file(struct format_instance *fid,
lib/format_text/tags.c +0 -41

44 lines not on this page · raw message