summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Rajnoha <prajnoha@redhat.com>2015-05-06 13:58:17 +0200
committerPeter Rajnoha <prajnoha@redhat.com>2015-06-29 09:43:41 +0200
commite29d4773f4de731d8483a12684d5f4b96092088b (patch)
tree68c0a536e75784c06e40accebca7535d898175bf
parent77c2d11657d0b1bdb82e02cbbc6cf025d8b2f43e (diff)
downloadlvm2-e29d4773f4de731d8483a12684d5f4b96092088b.tar.gz
refactor: rename alloc_printed_tags fn to _alloc_printed_str_list and use it for string lists in general
-rw-r--r--lib/format_text/export.c43
-rw-r--r--lib/format_text/import-export.h2
-rw-r--r--lib/format_text/tags.c41
3 files changed, 42 insertions, 44 deletions
diff --git a/lib/format_text/export.c b/lib/format_text/export.c
index 018772eb8..a1af06b05 100644
--- a/lib/format_text/export.c
+++ b/lib/format_text/export.c
@@ -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);
diff --git a/lib/format_text/import-export.h b/lib/format_text/import-export.h
index 18561d334..be889944e 100644
--- a/lib/format_text/import-export.h
+++ b/lib/format_text/import-export.h
@@ -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,
diff --git a/lib/format_text/tags.c b/lib/format_text/tags.c
index c081167c3..e22ca28db 100644
--- a/lib/format_text/tags.c
+++ b/lib/format_text/tags.c
@@ -19,44 +19,3 @@
#include "str_list.h"
#include "lvm-string.h"
-char *alloc_printed_tags(struct dm_list *tagsl)
-{
- struct dm_str_list *sl;
- int first = 1;
- size_t size = 0;
- char *buffer, *buf;
-
- dm_list_iterate_items(sl, tagsl)
- /* '"' + tag + '"' + ',' + ' ' */
- size += strlen(sl->str) + 4;
- /* '[' + ']' + '\0' */
- size += 3;
-
- if (!(buffer = buf = dm_malloc(size))) {
- log_error("Could not allocate memory for tag list buffer.");
- return NULL;
- }
-
- if (!emit_to_buffer(&buf, &size, "["))
- goto_bad;
-
- dm_list_iterate_items(sl, tagsl) {
- 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;
-}