ucx
UAP Common Extensions
Loading...
Searching...
No Matches
properties.h
Go to the documentation of this file.
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 */
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#include <stdio.h>
45#include <string.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
60
66
72
78
79 /*
80 * The character, when appearing at the end of a line, continues that line.
81 * This is '\' by default.
82 */
87};
88
93
97cx_attr_export
99
163
168
188
193
194
199
215 CxProperties *prop,
217 cxstring key,
218 cxstring value
219);
220
238
239
244
262 CxProperties *prop,
264 cxstring *target
265);
266
277 CxProperties *prop,
279);
280
289 CxProperties *prop,
291);
292
325
334cx_attr_export
336
349cx_attr_export
351
361static inline void cxPropertiesReset(CxProperties *prop) {
362 CxPropertiesConfig config = prop->config;
364 cxPropertiesInit(prop, config);
365}
366
373#define cxPropertiesInitDefault(prop) \
374 cxPropertiesInit(prop, cx_properties_config_default)
375
399cx_attr_export
401 CxProperties *prop,
402 const char *buf,
403 size_t len
404);
405
406#ifdef __cplusplus
407} // extern "C"
409static inline int cxPropertiesFill(
410 CxProperties *prop,
411 cxstring str
412) {
413 return cxPropertiesFilln(prop, str.ptr, str.length);
414}
415
417static inline int cxPropertiesFill(
418 CxProperties *prop,
419 cxmutstr str
420) {
421 return cxPropertiesFilln(prop, str.ptr, str.length);
422}
423
426static inline int cxPropertiesFill(
427 CxProperties *prop,
428 const char *str
429) {
430 return cxPropertiesFilln(prop, str, strlen(str));
431}
432
433extern "C" {
434#else // __cplusplus
455#define cxPropertiesFill(prop, str) _Generic((str), \
456 cxstring: cx_properties_fill_cxstr, \
457 cxmutstr: cx_properties_fill_mutstr, \
458 char*: cx_properties_fill_str, \
459 const char*: cx_properties_fill_str) \
460 (prop, str)
461
466static inline int cx_properties_fill_cxstr(
467 CxProperties *prop,
468 cxstring str
469) {
470 return cxPropertiesFilln(prop, str.ptr, str.length);
471}
472
478 CxProperties *prop,
479 cxmutstr str
480) {
481 return cxPropertiesFilln(prop, str.ptr, str.length);
482}
483
489static inline int cx_properties_fill_str(
490 CxProperties *prop,
491 const char *str
492) {
493 return cxPropertiesFilln(prop, str, strlen(str));
494}
495#endif
496
505cx_attr_export
507 CxProperties *prop,
508 char *buf,
509 size_t capacity
510);
511
544cx_attr_export
546 CxProperties *prop,
547 cxstring *key,
548 cxstring *value
549);
550
565cx_attr_export
567
576cx_attr_export
578
590cx_attr_export
591CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len);
592
606cx_attr_export
608
621cx_attr_export
622CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size);
623
624
654cx_attr_export
656 CxProperties *prop,
657 CxPropertiesSink sink,
658 CxPropertiesSource source
659);
660
661#ifdef __cplusplus
662} // extern "C"
663#endif
664
665#endif // UCX_PROPERTIES_H
Advanced buffer implementation.
Common definitions and feature checks.
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_access_r(...)
Specifies that the function will only read through the given pointer.
Definition common.h:225
#define cx_attr_cstr_arg(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:206
Interface for map implementations.
static int cx_properties_fill_str(CxProperties *prop, const char *str)
Fills the input buffer with data.
Definition properties.h:489
static int cx_properties_fill_mutstr(CxProperties *prop, cxmutstr str)
Fills the input buffer with data.
Definition properties.h:477
static int cx_properties_fill_cxstr(CxProperties *prop, cxstring str)
Fills the input buffer with data.
Definition properties.h:466
CxPropertiesSource cxPropertiesCstrSource(const char *str)
Creates a properties source based on a C string.
void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config)
Initialize a properties interface.
int(* cx_properties_read_init_func)(CxProperties *prop, CxPropertiesSource *src)
A function that may initialize additional memory for the source.
Definition properties.h:276
int(* cx_properties_read_func)(CxProperties *prop, CxPropertiesSource *src, cxstring *target)
A function that reads data from a source.
Definition properties.h:261
const CxPropertiesConfig cx_properties_config_default
Default properties configuration.
void cxPropertiesDestroy(CxProperties *prop)
Destroys the properties interface.
CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value)
Retrieves the next key/value-pair.
CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len)
Creates a properties source based on C string with the specified length.
int(* cx_properties_sink_func)(CxProperties *prop, CxPropertiesSink *sink, cxstring key, cxstring value)
A function that consumes a k/v-pair in a sink.
Definition properties.h:214
int cxPropertiesFilln(CxProperties *prop, const char *buf, size_t len)
Fills the input buffer with data.
CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size)
Creates a properties source based on an FILE.
CxPropertiesStatus cxPropertiesLoad(CxProperties *prop, CxPropertiesSink sink, CxPropertiesSource source)
Loads properties data from a source and transfers it to a sink.
CxPropertiesSink cxPropertiesMapSink(CxMap *map)
Creates a properties sink for an UCX map.
CxPropertiesSource cxPropertiesStringSource(cxstring str)
Creates a properties source based on an UCX string.
#define cxPropertiesFill(prop, str)
Fills the input buffer with data.
Definition properties.h:455
void cxPropertiesUseStack(CxProperties *prop, char *buf, size_t capacity)
Specifies stack memory that shall be used as internal buffer.
void(* cx_properties_read_clean_func)(CxProperties *prop, CxPropertiesSource *src)
A function that cleans memory initialized by the read_init_func.
Definition properties.h:288
cx_properties_status
Status codes for the properties interface.
Definition properties.h:103
@ CX_PROPERTIES_SINK_FAILED
Sinking a k/v-pair failed.
Definition properties.h:161
@ CX_PROPERTIES_NO_ERROR
Everything is fine.
Definition properties.h:107
@ CX_PROPERTIES_INVALID_EMPTY_KEY
The line contains a delimiter, but no key.
Definition properties.h:135
@ CX_PROPERTIES_OK
Not used as a status and never returned by any function.
Definition properties.h:127
@ CX_PROPERTIES_NULL_INPUT
Input buffer is NULL.
Definition properties.h:131
@ CX_PROPERTIES_READ_FAILED
Reading from a properties source failed.
Definition properties.h:155
@ CX_PROPERTIES_INCOMPLETE_DATA
The input ends unexpectedly.
Definition properties.h:118
@ CX_PROPERTIES_READ_INIT_FAILED
Initializing the properties source failed.
Definition properties.h:149
@ CX_PROPERTIES_BUFFER_ALLOC_FAILED
More internal buffer was needed, but could not be allocated.
Definition properties.h:143
@ CX_PROPERTIES_NO_DATA
The input buffer does not contain more data.
Definition properties.h:111
@ CX_PROPERTIES_INVALID_MISSING_DELIMITER
The line contains data, but no delimiter.
Definition properties.h:139
enum cx_properties_status CxPropertiesStatus
Typedef for the properties status enum.
Definition properties.h:167
static void cxPropertiesReset(CxProperties *prop)
Destroys and re-initializes the properties interface.
Definition properties.h:361
Strings that know their length.
Structure for the UCX buffer data.
Definition buffer.h:157
Structure for the UCX map.
Definition map.h:61
The UCX string structure.
Definition string.h:51
char * ptr
A pointer to the string.
Definition string.h:56
size_t length
The length of the string.
Definition string.h:58
Configures the expected characters for the properties parser.
Definition properties.h:54
char continuation
Reserved for future use.
Definition properties.h:86
char delimiter
The key/value delimiter that shall be used.
Definition properties.h:59
char comment3
The third comment character.
Definition properties.h:77
char comment2
The second comment character.
Definition properties.h:71
char comment1
The first comment character.
Definition properties.h:65
Interface for working with properties data.
Definition properties.h:172
CxBuffer input
The text input buffer.
Definition properties.h:181
CxPropertiesConfig config
The configuration.
Definition properties.h:176
CxBuffer buffer
Internal buffer.
Definition properties.h:186
Defines a sink for k/v-pairs.
Definition properties.h:224
cx_properties_sink_func sink_func
A function for consuming k/v-pairs into the sink.
Definition properties.h:236
void * sink
The sink object.
Definition properties.h:228
void * data
Optional custom data.
Definition properties.h:232
Defines a properties source.
Definition properties.h:296
cx_properties_read_init_func read_init_func
Optional function that may prepare the source for reading data.
Definition properties.h:318
void * src
The source object.
Definition properties.h:302
void * data_ptr
Optional additional data pointer.
Definition properties.h:306
size_t data_size
Optional size information.
Definition properties.h:310
cx_properties_read_func read_func
A function that reads data from the source.
Definition properties.h:314
cx_properties_read_clean_func read_clean_func
Optional function that cleans additional memory allocated by the read_init_func.
Definition properties.h:323
The UCX string structure for immutable (constant) strings.
Definition string.h:69
size_t length
The length of the string.
Definition string.h:76
const char * ptr
A pointer to the immutable string.
Definition string.h:74