Wed, 31 Dec 2025 16:41:16 +0100
update ucx to version 4.0
| 852 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | /** | |
| 29 | * @file properties.h | |
| 30 | * @brief Interface for parsing data from properties files. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 34 | */ | |
| 35 | ||
| 36 | #ifndef UCX_PROPERTIES_H | |
| 37 | #define UCX_PROPERTIES_H | |
| 38 | ||
| 39 | #include "common.h" | |
| 40 | #include "string.h" | |
| 41 | #include "map.h" | |
| 42 | #include "buffer.h" | |
| 43 | ||
| 44 | /** | |
| 45 | * Configures the expected characters for the properties parser. | |
| 46 | */ | |
| 47 | struct cx_properties_config_s { | |
| 48 | /** | |
| 49 | * The key/value delimiter that shall be used. | |
| 50 | * This is '=' by default. | |
| 51 | */ | |
| 52 | char delimiter; | |
| 53 | ||
| 54 | /** | |
| 55 | * The first comment character. | |
| 56 | * This is '#' by default. | |
| 57 | */ | |
| 58 | char comment1; | |
| 59 | ||
| 60 | /** | |
| 61 | * The second comment character. | |
| 62 | * This is not set by default. | |
| 63 | */ | |
| 64 | char comment2; | |
| 65 | ||
| 66 | /** | |
| 67 | * The third comment character. | |
| 68 | * This is not set by default. | |
| 69 | */ | |
| 70 | char comment3; | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
71 | |
| 891 | 72 | /** |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
73 | * The character, when appearing at the end of a line, continues that line. |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
74 | * This is '\' by default. |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
75 | */ |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
76 | char continuation; |
| 852 | 77 | }; |
| 78 | ||
| 79 | /** | |
| 80 | * Typedef for the properties config. | |
| 81 | */ | |
| 82 | typedef struct cx_properties_config_s CxPropertiesConfig; | |
| 83 | ||
| 84 | /** | |
| 85 | * Default properties configuration. | |
| 86 | */ | |
| 889 | 87 | CX_EXPORT extern const CxPropertiesConfig cx_properties_config_default; |
| 852 | 88 | |
| 89 | /** | |
| 90 | * Status codes for the properties interface. | |
| 91 | */ | |
| 92 | enum cx_properties_status { | |
| 93 | /** | |
| 94 | * Everything is fine. | |
| 95 | */ | |
| 96 | CX_PROPERTIES_NO_ERROR, | |
| 97 | /** | |
| 98 | * The input buffer does not contain more data. | |
| 99 | */ | |
| 100 | CX_PROPERTIES_NO_DATA, | |
| 101 | /** | |
| 102 | * The input ends unexpectedly. | |
| 103 | * | |
| 104 | * This either happens when the last line does not terminate with a line | |
| 105 | * break, or when the input ends with a parsed key but no value. | |
| 106 | */ | |
| 107 | CX_PROPERTIES_INCOMPLETE_DATA, | |
| 108 | /** | |
| 109 | * Not used as a status and never returned by any function. | |
| 110 | * | |
| 111 | * You can use this enumerator to check for all "good" status results | |
| 112 | * by checking if the status is less than @c CX_PROPERTIES_OK. | |
| 113 | * | |
| 889 | 114 | * A "good" status means that you can refill data and continue parsing. |
| 852 | 115 | */ |
| 116 | CX_PROPERTIES_OK, | |
| 117 | /** | |
| 118 | * Input buffer is @c NULL. | |
| 119 | */ | |
| 120 | CX_PROPERTIES_NULL_INPUT, | |
| 121 | /** | |
| 889 | 122 | * The line contains a delimiter but no key. |
| 852 | 123 | */ |
| 124 | CX_PROPERTIES_INVALID_EMPTY_KEY, | |
| 125 | /** | |
| 889 | 126 | * The line contains data but no delimiter. |
| 852 | 127 | */ |
| 128 | CX_PROPERTIES_INVALID_MISSING_DELIMITER, | |
| 129 | /** | |
| 130 | * More internal buffer was needed, but could not be allocated. | |
| 131 | */ | |
| 132 | CX_PROPERTIES_BUFFER_ALLOC_FAILED, | |
| 133 | /** | |
| 891 | 134 | * A file operation failed. |
| 135 | * Only for cxPropertiesLoad(). | |
| 136 | * It is system-specific if errno is set. | |
| 852 | 137 | */ |
| 891 | 138 | CX_PROPERTIES_FILE_ERROR, |
| 852 | 139 | /** |
| 891 | 140 | * A map operation failed. |
| 141 | * Only for cxPropertiesLoad(). | |
| 852 | 142 | */ |
| 891 | 143 | CX_PROPERTIES_MAP_ERROR, |
| 852 | 144 | }; |
| 145 | ||
| 146 | /** | |
| 147 | * Typedef for the properties status enum. | |
| 148 | */ | |
| 149 | typedef enum cx_properties_status CxPropertiesStatus; | |
| 150 | ||
| 151 | /** | |
| 152 | * Interface for working with properties data. | |
| 153 | */ | |
| 154 | struct cx_properties_s { | |
| 155 | /** | |
| 156 | * The configuration. | |
| 157 | */ | |
| 158 | CxPropertiesConfig config; | |
| 159 | ||
| 160 | /** | |
| 161 | * The text input buffer. | |
| 162 | */ | |
| 163 | CxBuffer input; | |
| 164 | ||
| 165 | /** | |
| 166 | * Internal buffer. | |
| 167 | */ | |
| 168 | CxBuffer buffer; | |
| 169 | }; | |
| 170 | ||
| 171 | /** | |
| 172 | * Typedef for the properties interface. | |
| 173 | */ | |
| 174 | typedef struct cx_properties_s CxProperties; | |
| 175 | ||
| 176 | /** | |
| 177 | * Initialize a properties interface. | |
| 178 | * | |
| 179 | * @param prop the properties interface | |
| 180 | * @param config the properties configuration | |
| 181 | * @see cxPropertiesInitDefault() | |
| 182 | */ | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
183 | CX_EXTERN CX_NONNULL |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
184 | void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config); |
| 852 | 185 | |
| 186 | /** | |
| 187 | * Destroys the properties interface. | |
| 188 | * | |
| 189 | * @note Even when you are certain that you did not use the interface in a | |
| 190 | * way that caused a memory allocation, you should call this function anyway. | |
| 889 | 191 | * Future versions of the library might add features that need additional memory, |
| 192 | * and you really don't want to search the entire code where you might need to | |
| 193 | * add a call to this function. | |
| 852 | 194 | * |
| 195 | * @param prop the properties interface | |
| 196 | */ | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
197 | CX_EXTERN CX_NONNULL |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
198 | void cxPropertiesDestroy(CxProperties *prop); |
| 852 | 199 | |
| 200 | /** | |
| 201 | * Destroys and re-initializes the properties interface. | |
| 202 | * | |
| 889 | 203 | * You might want to use this to reset the parser after |
| 852 | 204 | * encountering a syntax error. |
| 205 | * | |
| 206 | * @param prop the properties interface | |
| 207 | */ | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
208 | CX_EXTERN CX_NONNULL |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
209 | void cxPropertiesReset(CxProperties *prop); |
| 852 | 210 | |
| 211 | /** | |
| 212 | * Initialize a properties parser with the default configuration. | |
| 213 | * | |
| 214 | * @param prop (@c CxProperties*) the properties interface | |
| 215 | * @see cxPropertiesInit() | |
| 216 | */ | |
| 217 | #define cxPropertiesInitDefault(prop) \ | |
| 889 | 218 | cxPropertiesInit(prop, cx_properties_config_default) |
| 852 | 219 | |
| 220 | /** | |
| 221 | * Fills the input buffer with data. | |
| 222 | * | |
| 223 | * After calling this function, you can parse the data by calling | |
| 224 | * cxPropertiesNext(). | |
| 225 | * | |
| 226 | * @remark The properties interface tries to avoid allocations. | |
| 227 | * When you use this function and cxPropertiesNext() interleaving, | |
| 228 | * no allocations are performed. However, you must not free the | |
| 229 | * pointer to the data in that case. When you invoke the fill | |
| 230 | * function more than once before calling cxPropertiesNext(), | |
| 231 | * the additional data is appended - inevitably leading to | |
| 232 | * an allocation of a new buffer and copying the previous contents. | |
| 233 | * | |
| 234 | * @param prop the properties interface | |
| 235 | * @param buf a pointer to the data | |
| 236 | * @param len the length of the data | |
| 237 | * @retval zero success | |
| 238 | * @retval non-zero a memory allocation was necessary but failed | |
| 239 | * @see cxPropertiesFill() | |
| 240 | */ | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
241 | CX_EXTERN CX_NONNULL CX_ACCESS_R(2, 3) |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
242 | int cxPropertiesFilln(CxProperties *prop, const char *buf, size_t len); |
| 889 | 243 | |
| 244 | /** | |
| 245 | * Internal function, do not use. | |
| 246 | * | |
| 247 | * @param prop the properties interface | |
| 248 | * @param str the text to fill in | |
| 249 | * @retval zero success | |
| 250 | * @retval non-zero a memory allocation was necessary but failed | |
| 251 | */ | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
252 | CX_NONNULL CX_INLINE |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
253 | int cx_properties_fill(CxProperties *prop, cxstring str) { |
| 852 | 254 | return cxPropertiesFilln(prop, str.ptr, str.length); |
| 255 | } | |
| 256 | ||
| 257 | /** | |
| 258 | * Fills the input buffer with data. | |
| 259 | * | |
| 260 | * After calling this function, you can parse the data by calling | |
| 261 | * cxPropertiesNext(). | |
| 262 | * | |
| 263 | * @attention The properties interface tries to avoid allocations. | |
| 264 | * When you use this function and cxPropertiesNext() interleaving, | |
| 265 | * no allocations are performed. However, you must not free the | |
| 266 | * pointer to the data in that case. When you invoke the fill | |
| 267 | * function more than once before calling cxPropertiesNext(), | |
| 268 | * the additional data is appended - inevitably leading to | |
| 269 | * an allocation of a new buffer and copying the previous contents. | |
| 270 | * | |
| 271 | * @param prop the properties interface | |
| 272 | * @param str the text to fill in | |
| 273 | * @retval zero success | |
| 274 | * @retval non-zero a memory allocation was necessary but failed | |
| 275 | * @see cxPropertiesFilln() | |
| 276 | */ | |
| 889 | 277 | #define cxPropertiesFill(prop, str) cx_properties_fill(prop, cx_strcast(str)) |
| 852 | 278 | |
| 279 | /** | |
| 889 | 280 | * Specifies stack memory that shall be used as an internal buffer. |
| 852 | 281 | * |
| 282 | * @param prop the properties interface | |
| 283 | * @param buf a pointer to stack memory | |
| 284 | * @param capacity the capacity of the stack memory | |
| 285 | */ | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
286 | CX_EXTERN CX_NONNULL |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
287 | void cxPropertiesUseStack(CxProperties *prop, char *buf, size_t capacity); |
| 852 | 288 | |
| 289 | /** | |
| 290 | * Retrieves the next key/value-pair. | |
| 291 | * | |
| 292 | * This function returns zero as long as there are key/value-pairs found. | |
| 293 | * If no more key/value-pairs are found, #CX_PROPERTIES_NO_DATA is returned. | |
| 294 | * | |
| 295 | * When an incomplete line is encountered, #CX_PROPERTIES_INCOMPLETE_DATA is | |
| 296 | * returned, and you can add more data with #cxPropertiesFill(). | |
| 297 | * | |
| 298 | * @remark The incomplete line will be stored in an internal buffer, which is | |
| 299 | * allocated on the heap, by default. If you want to avoid allocations, | |
| 300 | * you can specify sufficient space with cxPropertiesUseStack() after | |
| 301 | * initialization with cxPropertiesInit(). | |
| 302 | * | |
| 303 | * @attention The returned strings will point into a buffer that might not be | |
| 304 | * available later. It is strongly recommended to copy the strings for further | |
| 305 | * use. | |
| 306 | * | |
| 307 | * @param prop the properties interface | |
| 308 | * @param key a pointer to the cxstring that shall contain the property name | |
| 309 | * @param value a pointer to the cxstring that shall contain the property value | |
| 310 | * @retval CX_PROPERTIES_NO_ERROR (zero) a key/value pair was found | |
| 311 | * @retval CX_PROPERTIES_NO_DATA there is no (more) data in the input buffer | |
| 312 | * @retval CX_PROPERTIES_INCOMPLETE_DATA the data in the input buffer is incomplete | |
| 313 | * (fill more data and try again) | |
| 314 | * @retval CX_PROPERTIES_NULL_INPUT the input buffer was never filled | |
| 315 | * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key | |
| 316 | * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter | |
| 317 | * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed | |
| 318 | */ | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
319 | CX_EXTERN CX_NONNULL CX_NODISCARD |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
320 | CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value); |
| 852 | 321 | |
| 322 | /** | |
| 891 | 323 | * The size of the stack memory that `cxPropertiesLoad()` will reserve with `cxPropertiesUseStack()`. |
| 852 | 324 | */ |
| 891 | 325 | CX_EXPORT extern const unsigned cx_properties_load_buf_size; |
| 852 | 326 | |
| 327 | /** | |
| 891 | 328 | * The size of the stack memory that `cxPropertiesLoad()` will use to read contents from the file. |
| 852 | 329 | */ |
| 891 | 330 | CX_EXPORT extern const unsigned cx_properties_load_fill_size; |
| 852 | 331 | |
| 332 | /** | |
| 891 | 333 | * Internal function - use cxPropertiesLoad() instead. |
| 852 | 334 | * |
| 891 | 335 | * @param allocator the allocator for the values |
| 336 | * @param filename the file name | |
| 337 | * @param target the target map | |
| 338 | * @param config the parser config | |
| 339 | * @return status code | |
| 852 | 340 | */ |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
341 | CX_EXTERN CX_NONNULL_ARG(3) |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
342 | CxPropertiesStatus cx_properties_load(const CxAllocator *allocator, |
| 891 | 343 | cxstring filename, CxMap *target, CxPropertiesConfig config); |
| 852 | 344 | |
| 345 | /** | |
| 891 | 346 | * Loads properties from a file and inserts them into a map. |
| 852 | 347 | * |
| 891 | 348 | * Entries are added to the map, possibly overwriting existing entries. |
| 349 | * | |
| 350 | * The map must either store pointers of type @c char*, or elements of type cxmutstr. | |
| 351 | * Any other configuration is not supported. | |
| 852 | 352 | * |
| 891 | 353 | * @note When the parser finds an error, all successfully parsed keys before the error |
| 354 | * are added to the map nonetheless. | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
355 | * |
| 891 | 356 | * @param allocator the allocator for the values that will be stored in the map |
| 357 | * @param filename (any string) the absolute or relative path to the file | |
| 358 | * @param target (@c CxMap*) the map where the properties shall be added | |
| 359 | * @param config the parser config | |
| 360 | * @retval CX_PROPERTIES_NO_ERROR (zero) at least one key/value pair was found | |
| 361 | * @retval CX_PROPERTIES_NO_DATA the file is syntactically OK, but does not contain properties | |
| 362 | * @retval CX_PROPERTIES_INCOMPLETE_DATA unexpected end of file | |
| 852 | 363 | * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key |
| 364 | * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter | |
| 365 | * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed | |
| 891 | 366 | * @retval CX_PROPERTIES_FILE_ERROR a file operation failed; depending on the system @c errno might be set |
| 367 | * @retval CX_PROPERTIES_MAP_ERROR storing a key/value pair in the map failed | |
| 368 | * @see cxPropertiesLoadDefault() | |
| 852 | 369 | */ |
| 891 | 370 | #define cxPropertiesLoad(allocator, filename, target, config) \ |
| 371 | cx_properties_load(allocator, cx_strcast(filename), target, config) | |
| 372 | ||
| 373 | /** | |
| 374 | * Loads properties from a file and inserts them into a map with a default config. | |
| 375 | * | |
| 376 | * Entries are added to the map, possibly overwriting existing entries. | |
| 377 | * | |
| 378 | * The map must either store pointers of type @c char*, or elements of type cxmutstr. | |
| 379 | * Any other configuration is not supported. | |
| 380 | * | |
| 381 | * @note When the parser finds an error, all successfully parsed keys before the error | |
| 382 | * are added to the map nonetheless. | |
| 383 | * | |
| 384 | * @param allocator the allocator for the values that will be stored in the map | |
| 385 | * @param filename (any string) the absolute or relative path to the file | |
| 386 | * @param target (@c CxMap*) the map where the properties shall be added | |
| 387 | * @retval CX_PROPERTIES_NO_ERROR (zero) at least one key/value pair was found | |
| 388 | * @retval CX_PROPERTIES_NO_DATA the file is syntactically OK, but does not contain properties | |
| 389 | * @retval CX_PROPERTIES_INCOMPLETE_DATA unexpected end of file | |
| 390 | * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key | |
| 391 | * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter | |
| 392 | * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed | |
| 393 | * @retval CX_PROPERTIES_FILE_ERROR a file operation failed; depending on the system @c errno might be set | |
| 394 | * @retval CX_PROPERTIES_MAP_ERROR storing a key/value pair in the map failed | |
| 395 | * @see cxPropertiesLoad() | |
| 396 | */ | |
| 397 | #define cxPropertiesLoadDefault(allocator, filename, target) \ | |
| 398 | cx_properties_load(allocator, cx_strcast(filename), target, cx_properties_config_default) | |
| 399 | ||
| 852 | 400 | #endif // UCX_PROPERTIES_H |