Tizen 2.1 base
[external/device-mapper.git] / lib / format_text / tags.c
1 /*
2  * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
7  * This copyrighted material is made available to anyone wishing to use,
8  * modify, copy, or redistribute it subject to the terms and conditions
9  * of the GNU Lesser General Public License v.2.1.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include "lib.h"
17 #include "metadata.h"
18 #include "import-export.h"
19 #include "str_list.h"
20 #include "lvm-string.h"
21
22 char *alloc_printed_tags(struct dm_list *tags)
23 {
24         struct str_list *sl;
25         int first = 1;
26         size_t size = 0;
27         char *buffer, *buf;
28
29         dm_list_iterate_items(sl, tags)
30                 /* '"' + tag + '"' + ',' + ' ' */
31                 size += strlen(sl->str) + 4;
32         /* '[' + ']' + '\0' */
33         size += 3;
34
35         if (!(buffer = buf = dm_malloc(size))) {
36                 log_error("Could not allocate memory for tag list buffer.");
37                 return NULL;
38         }
39
40         if (!emit_to_buffer(&buf, &size, "["))
41                 goto_bad;
42
43         dm_list_iterate_items(sl, tags) {
44                 if (!first) {
45                         if (!emit_to_buffer(&buf, &size, ", "))
46                                 goto_bad;
47                 } else
48                         first = 0;
49
50                 if (!emit_to_buffer(&buf, &size, "\"%s\"", sl->str))
51                         goto_bad;
52         }
53
54         if (!emit_to_buffer(&buf, &size, "]"))
55                 goto_bad;
56
57         return buffer;
58
59 bad:
60         dm_free(buffer);
61         return_NULL;
62 }
63
64 int read_tags(struct dm_pool *mem, struct dm_list *tags, const struct config_value *cv)
65 {
66         if (cv->type == CFG_EMPTY_ARRAY)
67                 return 1;
68
69         while (cv) {
70                 if (cv->type != CFG_STRING) {
71                         log_error("Found a tag that is not a string");
72                         return 0;
73                 }
74
75                 if (!str_list_add(mem, tags, dm_pool_strdup(mem, cv->v.str)))
76                         return_0;
77
78                 cv = cv->next;
79         }
80
81         return 1;
82 }