Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 1 | /* |
| 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 Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 44 | #define KV_PAIR_MAX_VALUE_LEN 1024 |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 45 | |
| 46 | enum kv_pair_style { |
| 47 | KV_STYLE_PAIR, /* key1="value1" key2="value2" */ |
| 48 | KV_STYLE_VALUE, /* | value1 | value2 | */ |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 49 | KV_STYLE_LONG, /* key1 | value1 */ |
| 50 | /* key2 | value2 */ |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | struct kv_pair { |
| 54 | char *key; |
| 55 | char *value; |
| 56 | struct kv_pair *next; |
| 57 | }; |
| 58 | |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 59 | extern enum kv_pair_style kv_pair_get_style(void); |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 60 | |
| 61 | extern void kv_pair_set_style(enum kv_pair_style style); |
| 62 | |
| 63 | /* |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 64 | * kv_pair_new - create new key=value pair |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 65 | * |
| 66 | * returns pointer to new key=value pair |
| 67 | * returns NULL to indicate error |
| 68 | */ |
| 69 | extern struct kv_pair *kv_pair_new(void); |
| 70 | |
| 71 | /* |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 72 | * kv_pair_add - add new key=value pair to list |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 73 | * |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 74 | * @kv_list: key=value pair list |
| 75 | * @key: key string |
| 76 | * @value: value string |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 77 | * |
| 78 | * returns pointer to new key=value pair |
| 79 | * returns NULL to indicate error |
| 80 | */ |
| 81 | extern struct kv_pair *kv_pair_add(struct kv_pair *kv_list, |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 82 | const char *key, const char *value); |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 83 | |
| 84 | /* |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 85 | * kv_pair_add_bool - add new boolean kvpair to list |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 86 | * |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 87 | * @kv_list: key=value pair list |
| 88 | * @key: key string |
| 89 | * @value: value |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 90 | * |
| 91 | * returns pointer to new key=value pair |
| 92 | * returns NULL to indicate error |
| 93 | */ |
| 94 | extern struct kv_pair *kv_pair_add_bool(struct kv_pair *kv_list, |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 95 | const char *key, int value); |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 96 | |
| 97 | /* |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 98 | * kv_pair_fmt - add key=value pair based on printf format |
| 99 | * NOTE: uses variable argument list |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 100 | * |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 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 |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 105 | * |
| 106 | * returns pointer to new key=value pair |
| 107 | * returns NULL to indicate error |
| 108 | */ |
| 109 | extern struct kv_pair *kv_pair_fmt(struct kv_pair *kv_list, |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 110 | const char *kv_key, const char *format, ...) |
Stefan Reinauer | 19ba110 | 2015-07-04 11:28:03 -0700 | [diff] [blame] | 111 | #if defined(_WIN32) || (_WIN64) |
| 112 | __attribute__((format(gnu_printf, 3, 4))); |
| 113 | #else |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 114 | __attribute__((format(printf, 3, 4))); |
Stefan Reinauer | 19ba110 | 2015-07-04 11:28:03 -0700 | [diff] [blame] | 115 | #endif |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * kv_pair_free - clean a key=value pair list |
| 119 | * |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 120 | * @kv_list: pointer to key=value list |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 121 | */ |
| 122 | extern void kv_pair_free(struct kv_pair *kv_list); |
| 123 | |
| 124 | /* |
| 125 | * kv_pair_print - print a key=value pair list |
| 126 | * |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 127 | * @kv_list: pointer to key=value list |
| 128 | * @style: print style |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 129 | */ |
| 130 | extern void kv_pair_print_to_file(FILE* fp, struct kv_pair *kv_list, |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 131 | enum kv_pair_style style); |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * kv_pair_print - print a key=value pair list to gsys output |
| 135 | * |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 136 | * @kv_list: pointer to key=value list |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 137 | */ |
| 138 | extern 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 Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 144 | * @kv_list: pointer to key=value list |
| 145 | * @kv_key: key string |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 146 | */ |
| 147 | extern const char *kv_pair_get_value(struct kv_pair *kv_list, |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 148 | const char *kv_key); |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * kv_pair_size - return number of kv pairs in the chain |
| 152 | * |
Sol Boucher | fa7a455 | 2015-05-07 16:41:46 -0700 | [diff] [blame] | 153 | * @kv_list: pointer to key=value list |
Sol Boucher | 65d9520 | 2015-02-27 17:07:30 -0800 | [diff] [blame] | 154 | */ |
| 155 | extern int kv_pair_size(struct kv_pair *kv_list); |
| 156 | |
| 157 | #endif /* FLASHMAP_LIB_KV_PAIR_H__ */ |