blob: 1185a08d5259e6ea9c7b445bdb1761edc9c2f86f [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 */
Sol Boucherfa7a4552015-05-07 16:41:46 -070044#define KV_PAIR_MAX_VALUE_LEN 1024
Sol Boucher65d95202015-02-27 17:07:30 -080045
46enum kv_pair_style {
47 KV_STYLE_PAIR, /* key1="value1" key2="value2" */
48 KV_STYLE_VALUE, /* | value1 | value2 | */
Sol Boucherfa7a4552015-05-07 16:41:46 -070049 KV_STYLE_LONG, /* key1 | value1 */
50 /* key2 | value2 */
Sol Boucher65d95202015-02-27 17:07:30 -080051};
52
53struct kv_pair {
54 char *key;
55 char *value;
56 struct kv_pair *next;
57};
58
Sol Boucher69b88bf2015-02-26 11:47:19 -080059extern enum kv_pair_style kv_pair_get_style(void);
Sol Boucher65d95202015-02-27 17:07:30 -080060
61extern void kv_pair_set_style(enum kv_pair_style style);
62
63/*
Sol Boucherfa7a4552015-05-07 16:41:46 -070064 * kv_pair_new - create new key=value pair
Sol Boucher65d95202015-02-27 17:07:30 -080065 *
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/*
Sol Boucherfa7a4552015-05-07 16:41:46 -070072 * kv_pair_add - add new key=value pair to list
Sol Boucher65d95202015-02-27 17:07:30 -080073 *
Sol Boucherfa7a4552015-05-07 16:41:46 -070074 * @kv_list: key=value pair list
75 * @key: key string
76 * @value: value string
Sol Boucher65d95202015-02-27 17:07:30 -080077 *
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,
Sol Boucherfa7a4552015-05-07 16:41:46 -070082 const char *key, const char *value);
Sol Boucher65d95202015-02-27 17:07:30 -080083
84/*
Sol Boucherfa7a4552015-05-07 16:41:46 -070085 * kv_pair_add_bool - add new boolean kvpair to list
Sol Boucher65d95202015-02-27 17:07:30 -080086 *
Sol Boucherfa7a4552015-05-07 16:41:46 -070087 * @kv_list: key=value pair list
88 * @key: key string
89 * @value: value
Sol Boucher65d95202015-02-27 17:07:30 -080090 *
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,
Sol Boucherfa7a4552015-05-07 16:41:46 -070095 const char *key, int value);
Sol Boucher65d95202015-02-27 17:07:30 -080096
97/*
Sol Boucherfa7a4552015-05-07 16:41:46 -070098 * kv_pair_fmt - add key=value pair based on printf format
99 * NOTE: uses variable argument list
Sol Boucher65d95202015-02-27 17:07:30 -0800100 *
Sol Boucherfa7a4552015-05-07 16:41:46 -0700101 * @kv_list: list of key=value pairs
102 * @kv_key: key string
103 * @format: printf-style format for value input
104 * @...: arguments to format
Sol Boucher65d95202015-02-27 17:07:30 -0800105 *
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,
Sol Boucherfa7a4552015-05-07 16:41:46 -0700110 const char *kv_key, const char *format, ...)
Stefan Reinauer19ba1102015-07-04 11:28:03 -0700111#if defined(_WIN32) || (_WIN64)
112 __attribute__((format(gnu_printf, 3, 4)));
113#else
Sol Boucherfa7a4552015-05-07 16:41:46 -0700114 __attribute__((format(printf, 3, 4)));
Stefan Reinauer19ba1102015-07-04 11:28:03 -0700115#endif
Sol Boucher65d95202015-02-27 17:07:30 -0800116
117/*
118 * kv_pair_free - clean a key=value pair list
119 *
Sol Boucherfa7a4552015-05-07 16:41:46 -0700120 * @kv_list: pointer to key=value list
Sol Boucher65d95202015-02-27 17:07:30 -0800121 */
122extern void kv_pair_free(struct kv_pair *kv_list);
123
124/*
125 * kv_pair_print - print a key=value pair list
126 *
Sol Boucherfa7a4552015-05-07 16:41:46 -0700127 * @kv_list: pointer to key=value list
128 * @style: print style
Sol Boucher65d95202015-02-27 17:07:30 -0800129 */
130extern void kv_pair_print_to_file(FILE* fp, struct kv_pair *kv_list,
Sol Boucherfa7a4552015-05-07 16:41:46 -0700131 enum kv_pair_style style);
Sol Boucher65d95202015-02-27 17:07:30 -0800132
133/*
134 * kv_pair_print - print a key=value pair list to gsys output
135 *
Sol Boucherfa7a4552015-05-07 16:41:46 -0700136 * @kv_list: pointer to key=value list
Sol Boucher65d95202015-02-27 17:07:30 -0800137 */
138extern void kv_pair_print(struct kv_pair *kv_list);
139
140
141/*
142 * kv_pair_get_value - return first value with key match
143 *
Sol Boucherfa7a4552015-05-07 16:41:46 -0700144 * @kv_list: pointer to key=value list
145 * @kv_key: key string
Sol Boucher65d95202015-02-27 17:07:30 -0800146 */
147extern const char *kv_pair_get_value(struct kv_pair *kv_list,
Sol Boucherfa7a4552015-05-07 16:41:46 -0700148 const char *kv_key);
Sol Boucher65d95202015-02-27 17:07:30 -0800149
150/*
151 * kv_pair_size - return number of kv pairs in the chain
152 *
Sol Boucherfa7a4552015-05-07 16:41:46 -0700153 * @kv_list: pointer to key=value list
Sol Boucher65d95202015-02-27 17:07:30 -0800154 */
155extern int kv_pair_size(struct kv_pair *kv_list);
156
157#endif /* FLASHMAP_LIB_KV_PAIR_H__ */