blob: b573530c43b3235c46c07ca0e5d440a099b7f63a [file] [log] [blame]
Sol Boucher65d95202015-02-27 17:07:30 -08001/*
2 * Copyright 2010, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Alternatively, this software may be distributed under the terms of the
32 * GNU General Public License ("GPL") version 2 as published by the Free
33 * Software Foundation.
34 */
35
36#ifndef FLASHMAP_LIB_KV_PAIR_H__
37#define FLASHMAP_LIB_KV_PAIR_H__
38
39#include <stdio.h>
40#include <inttypes.h>
41#include <string.h>
42
43/* key=value string pair list */
44#define KV_PAIR_MAX_VALUE_LEN 1024
45
46enum kv_pair_style {
47 KV_STYLE_PAIR, /* key1="value1" key2="value2" */
48 KV_STYLE_VALUE, /* | value1 | value2 | */
49 KV_STYLE_LONG, /* key1 | value1 */
50 /* key2 | value2 */
51};
52
53struct kv_pair {
54 char *key;
55 char *value;
56 struct kv_pair *next;
57};
58
59extern enum kv_pair_style kv_pair_get_style();
60
61extern void kv_pair_set_style(enum kv_pair_style style);
62
63/*
64 * kv_pair_new - create new key=value pair
65 *
66 * returns pointer to new key=value pair
67 * returns NULL to indicate error
68 */
69extern struct kv_pair *kv_pair_new(void);
70
71/*
72 * kv_pair_add - add new key=value pair to list
73 *
74 * @kv_list: key=value pair list
75 * @key: key string
76 * @value: value string
77 *
78 * returns pointer to new key=value pair
79 * returns NULL to indicate error
80 */
81extern struct kv_pair *kv_pair_add(struct kv_pair *kv_list,
82 const char *key, const char *value);
83
84/*
85 * kv_pair_add_bool - add new boolean kvpair to list
86 *
87 * @kv_list: key=value pair list
88 * @key: key string
89 * @value: value
90 *
91 * returns pointer to new key=value pair
92 * returns NULL to indicate error
93 */
94extern struct kv_pair *kv_pair_add_bool(struct kv_pair *kv_list,
95 const char *key, int value);
96
97/*
98 * kv_pair_fmt - add key=value pair based on printf format
99 * NOTE: uses variable argument list
100 *
101 * @kv_list: list of key=value pairs
102 * @kv_key: key string
103 * @format: printf-style format for value input
104 * @...: arguments to format
105 *
106 * returns pointer to new key=value pair
107 * returns NULL to indicate error
108 */
109extern struct kv_pair *kv_pair_fmt(struct kv_pair *kv_list,
110 const char *kv_key, const char *format, ...)
111 __attribute__((format(printf, 3, 4)));
112
113/*
114 * kv_pair_free - clean a key=value pair list
115 *
116 * @kv_list: pointer to key=value list
117 */
118extern void kv_pair_free(struct kv_pair *kv_list);
119
120/*
121 * kv_pair_print - print a key=value pair list
122 *
123 * @kv_list: pointer to key=value list
124 * @style: print style
125 */
126extern void kv_pair_print_to_file(FILE* fp, struct kv_pair *kv_list,
127 enum kv_pair_style style);
128
129/*
130 * kv_pair_print - print a key=value pair list to gsys output
131 *
132 * @kv_list: pointer to key=value list
133 */
134extern void kv_pair_print(struct kv_pair *kv_list);
135
136
137/*
138 * kv_pair_get_value - return first value with key match
139 *
140 * @kv_list: pointer to key=value list
141 * @kv_key: key string
142 */
143extern const char *kv_pair_get_value(struct kv_pair *kv_list,
144 const char *kv_key);
145
146/*
147 * kv_pair_size - return number of kv pairs in the chain
148 *
149 * @kv_list: pointer to key=value list
150 */
151extern int kv_pair_size(struct kv_pair *kv_list);
152
153#endif /* FLASHMAP_LIB_KV_PAIR_H__ */