Sat, 01 Mar 2025 15:02:57 +0100
complete the properties documentation
relates to #451
1143
0559812df10c
assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents:
1142
diff
changeset
|
1 | # Properties |
1142
9437530176bc
add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
2 | |
1230
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
3 | The UCX properties parser can be used to parse line based key/value strings. |
1229
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
4 | |
1230
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
5 | ## Supported Syntax |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
6 | |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
7 | Key/value pairs must be line based and separated by a single character delimter. |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
8 | The parser supports up to three different characters which introduce comments. |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
9 | All characters starting with a comment character up to the end of the line are ignored. |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
10 | Blank lines are also ignored. |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
11 | |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
12 | An example properties file looks like this: |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
13 | |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
14 | ```properties |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
15 | # Comment line at start of file |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
16 | key1 = value1 |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
17 | key2 = value2 |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
18 | # next is a blank line and will be ignored |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
19 | |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
20 | keys_are_trimmed = and_so_are_values # also a comment |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
21 | ``` |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
22 | |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
23 | > Delimiter and comment characters are configured with the `CxPropertiesConfig` structure. |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
24 | > There is also a field reserved for `continuation` which will be used as a line continuation character |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
25 | > in a future version of UCX. |
3ec9cce0de01
add information about supported properties syntax
Mike Becker <universe@uap-core.de>
parents:
1229
diff
changeset
|
26 | > In UCX 3.1 this is not implemented. |
1229
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
27 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
28 | ## Basic Parsing |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
29 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
30 | ```C |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
31 | #include <cx/properties.h> |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
32 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
33 | typedef struct cx_properties_config_s { |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
34 | char delimiter; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
35 | char comment1; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
36 | char comment2; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
37 | char comment3; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
38 | // reserved for future use - not implemented in UCX 3.1 |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
39 | char continuation; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
40 | } CxPropertiesConfig; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
41 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
42 | void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
43 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
44 | void cxPropertiesInitDefault(CxProperties *prop); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
45 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
46 | void cxPropertiesDestroy(CxProperties *prop); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
47 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
48 | void cxPropertiesReset(CxProperties *prop); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
49 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
50 | int cxPropertiesFilln(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
51 | const char *buf, size_t len); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
52 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
53 | // where S is one of cxstring, cxmutstr, char*, const char* |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
54 | int cxPropertiesFill(CxProperties *prop, S string); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
55 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
56 | CxPropertiesStatus cxPropertiesNext(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
57 | cxstring *key, cxstring *value); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
58 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
59 | void cxPropertiesUseStack(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
60 | char *buf, size_t capacity); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
61 | ``` |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
62 | |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
63 | The first step is to initialize a `CxProperties` structure with a call to `cxPropertiesInit()` using the desired config. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
64 | The shorthand `cxPropertiesInitDefault()` creates a default configuration with the equals sign `'='` as delimiter |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
65 | and the hash-symbol `'#'` as comment symbol (the other two comment symbols remain unused in the default config). |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
66 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
67 | > In a future UCX version, the default `continuation` character will be a backslash `'\'`. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
68 | > In UCX 3.1 this feature is not implemented, yet. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
69 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
70 | The actual parsing is an interleaving invocation of the `cxPropertiesFill()` (or `cxPropertiesFilln()`) and `cxPropertiesNext()` functions. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
71 | The `cxPropertiesFill()` function is a convenience function, that accepts UCX strings and normal zero-terminated C strings and behaves otherwise like `cxPropertiesFilln()`. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
72 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
73 | Filling the input buffer is cost-free if there is no data already in the input buffer. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
74 | In that case, the input buffer only stores the pointer to the original data without creating a copy. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
75 | Calling `cxPropertiesNext()` will return with `CX_PROPERTIES_NO_ERROR` (= zero) for each key/value-pair that is successfully parsed, |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
76 | and stores the pointers and lengths for both the key and the value into the structures pointed to by the `key` and `value` arguments. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
77 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
78 | When all the data from the input buffer was successfully consumed, `cxPropertiesNext()` returns `CX_PROPERTIES_NO_DATA`. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
79 | |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
80 | > This is all still free of any copies and allocations. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
81 | > That means, the pointers in `key` and `value` after `cxPropertiesNext()` returns will point into the input buffer. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
82 | > If you intend to store the key and/or the value somewhere else, it is strongly recommended to create a copy with `cx_strdup()`, |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
83 | > because you will otherwise soon end up with a dangling pointer. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
84 | > {style="note"} |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
85 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
86 | If `cxPropertiesNext()` returns `CX_PROPERTIES_INCOMPLETE_DATA` it means that the input buffer is exhausted, |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
87 | but the last line did not contain a full key/value pair. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
88 | In that case, you can call `cxPropertiesFill()` again to add more data and continue with `cxPropertiesNext()`. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
89 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
90 | Note, that adding more data to a non-empty input buffer will lead to an allocation, |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
91 | unless you specified some stack memory with `cxPropertiesUseStack()`. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
92 | The stack capacity must be large enough to contain the longest line in your data. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
93 | If the internal buffer is not large enough to contain a single line, it is extended. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
94 | If that is not possible for some reason, `cxPropertiesNext()` fails and returns `CX_PROPERTIES_BUFFER_ALLOC_FAILED`. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
95 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
96 | If you want to reuse a `CxProperties` structure with the same config, you can call `cxPropertiesReset()`, even if the last operation was a failure. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
97 | Otherwise, you should always call `cxPropertiesDestroy()` when you are done with the parser. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
98 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
99 | > It is strongly recommended to always call `cxPropertiesDestroy` when you are done with the parser, |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
100 | > even if you did not expect any allocations because you used `cxPropertiesUseStack()`. |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
101 | |
1229
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
102 | ### List of Status Codes |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
103 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
104 | Below is a full list of status codes for `cxPropertiesNext()`. |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
105 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
106 | | Status Code | Meaning | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
107 | |-----------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
108 | | CX_PROPERTIES_NO_ERROR | A key/value pair was found and returned. | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
109 | | CX_PROPERTIES_NO_DATA | The input buffer does not contain more data. | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
110 | | CX_PROPERTIES_INCOMPLETE_DATA | The input ends unexpectedly. This can happen when the last line does not terminate with a line break, or when the input ends with a parsed key but no value. Use `cxPropertiesFill()` to add more data before retrying. | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
111 | | CX_PROPERTIES_NULL_INPUT | The input buffer was never initialized. Probably you forgot to call `cxPropertiesFill()` at least once. | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
112 | | CX_PROPERTIES_INVALID_EMPTY_KEY | Only white-spaces were found on the left hand-side of the delimiter. Keys must not be empty. | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
113 | | CX_PROPERTIES_INVALID_MISSING_DELIMITER | A line contains data, but no delimiter. | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
114 | | CX_PROPERTIES_BUFFER_ALLOC_FAILED | More internal buffer was needed, but could not be allocated. | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
115 | |
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
116 | |
1229
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
117 | ## Sources and Sinks |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
118 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
119 | ```C |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
120 | #include <cx/properties.h> |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
121 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
122 | CxPropertiesSource |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
123 | cxPropertiesStringSource(cxstring str); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
124 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
125 | CxPropertiesSource |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
126 | cxPropertiesCstrSource(const char *str); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
127 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
128 | CxPropertiesSource |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
129 | cxPropertiesCstrnSource(const char *str, size_t len); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
130 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
131 | CxPropertiesSource |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
132 | cxPropertiesFileSource(FILE *file, size_t chunk_size); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
133 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
134 | CxPropertiesSink |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
135 | cxPropertiesMapSink(CxMap *map); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
136 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
137 | CxPropertiesStatus |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
138 | cxPropertiesLoad(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
139 | CxPropertiesSink sink, CxPropertiesSource source); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
140 | ``` |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
141 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
142 | The basic idea of `cxPropertiesLoad()` is that key/value-pairs are extracted from a _source_ and ingested by a _sink_. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
143 | For the most common scenarios where properties data is read from a string or a file and put into a map, several functions are available. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
144 | But you can specify your [own sources and sinks](#creating-own-sources-and-sinks), as well. |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
145 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
146 | The following example shows a simple function which loads all properties data from a file. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
147 | The `chunk_size` argument when creating the file source specifies |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
148 | how many bytes are read from the file and filled into the properties parser in one read/sink cycle. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
149 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
150 | ```C |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
151 | #include <stdio.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
152 | #include <cx/properties.h> |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
153 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
154 | int load_props_from_file(const char *filename, CxMap *map) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
155 | FILE *f = fopen(filename, "r"); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
156 | if (!f) return -1; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
157 | CxProperties prop; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
158 | cxPropertiesInitDefault(&prop); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
159 | CxPropertiesSink sink = cxPropertiesMapSink(map); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
160 | CxPropertiesSource src = cxPropertiesFileSource(f, 512); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
161 | CxPropertiesStatus status = cxPropertiesLoad(&prop, sink, src); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
162 | fclose(f); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
163 | return status; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
164 | } |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
165 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
166 | // usage: |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
167 | CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
168 | if (load_props_from_file("my-props.properties", map)) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
169 | // error handling |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
170 | } else { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
171 | // assuming my-props.properties contains the following line: |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
172 | // my-key = some value |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
173 | char *value = cxMapGet(map, "my-key"); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
174 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
175 | ``` |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
176 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
177 | > The function `cxPropertiesLoad()` should usually not return `CX_PROPERTIES_INCOMPLETE_DATA` because the parser is automatically refilled from the source. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
178 | > If it does, it could mean that the source was unable to provide all the data, or the properties data ended unexpectedly. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
179 | > The most expected status code is `CX_PROPERTIES_NO_ERROR` which means that at least one key/value-pair was found. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
180 | > If `cxPropertiesLoad()` returns `CX_PROPERTIES_NO_DATA` it means that the source did not provide any key/value-pair. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
181 | > There are several special status codes which are documented [below](#additional-status-codes). |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
182 | |
1229
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
183 | ### Creating own Sources and Sinks |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
184 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
185 | ```C |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
186 | #include <cx/properties.h> |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
187 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
188 | typedef int(*cx_properties_read_init_func)(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
189 | CxPropertiesSource *src); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
190 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
191 | typedef int(*cx_properties_read_func)(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
192 | CxPropertiesSource *src, cxstring *target); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
193 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
194 | typedef void(*cx_properties_read_clean_func)(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
195 | CxPropertiesSource *src); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
196 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
197 | typedef int(*cx_properties_sink_func)(CxProperties *prop, |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
198 | CxPropertiesSink *sink, cxstring key, cxstring value); |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
199 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
200 | typedef struct cx_properties_source_s { |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
201 | void *src; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
202 | void *data_ptr; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
203 | size_t data_size; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
204 | cx_properties_read_func read_func; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
205 | cx_properties_read_init_func read_init_func; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
206 | cx_properties_read_clean_func read_clean_func; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
207 | } CxPropertiesSource; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
208 | |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
209 | typedef struct cx_properties_sink_s { |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
210 | void *sink; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
211 | void *data; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
212 | cx_properties_sink_func sink_func; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
213 | } CxPropertiesSink; |
9899043d7e39
basic structure for properties docu
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
214 | ``` |
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
215 | |
1232
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
216 | You can create your own sources and sinks by initializing the respective structures. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
217 | For a source, only the `read_func` is mandatory, the other two functions are optional and used for initialization and cleanup, if required. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
218 | The file source created by `cxPropertiesFileSource()`, for example, |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
219 | uses the `read_init_func` to allocate, and the `read_clean_func` to free the read buffer, respectively. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
220 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
221 | Since the default map sink created by `cxPropertiesMapSink()` stores `char*` pointers into a map, |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
222 | the following example uses a different sink, which stores them as `cxmutstr` values, automatically freeing them |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
223 | when the map gets destroyed. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
224 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
225 | ```C |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
226 | #include <stdio.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
227 | #include <unistd.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
228 | #include <fcntl.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
229 | #include <sys/stat.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
230 | #include <sys/mman.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
231 | #include <cx/properties.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
232 | #include <cx/hash_map.h> |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
233 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
234 | static int prop_mmap(CxProperties *prop, CxPropertiesSource *src) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
235 | struct stat s; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
236 | int fd = open(src->src, O_RDONLY); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
237 | if (fd < 0) return -1; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
238 | // re-use the data field to store the fd |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
239 | // there are cleaner ways, but this is just for illustration |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
240 | src->src = (void*) fd; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
241 | fstat(fd, &s); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
242 | // memory map the entire file |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
243 | // and store the address and length in the properties source |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
244 | src->data_ptr = mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
245 | src->data_size = s.st_size; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
246 | return src->data_ptr == NULL; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
247 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
248 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
249 | static int prop_read(CxProperties *prop, CxPropertiesSource *src, |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
250 | cxstring *target) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
251 | // copy the address and length of the mapped data to the target |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
252 | target->ptr = src->data_ptr; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
253 | target->length = src->data_size; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
254 | // set the new size to zero to indicate that there is no more data |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
255 | src->data_size = 0; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
256 | return 0; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
257 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
258 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
259 | static void prop_unmap(CxProperties *prop, CxPropertiesSource *src) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
260 | // unmap the memory and close the file |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
261 | munmap(src->data_ptr, src->data_size); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
262 | close((int)src->src); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
263 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
264 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
265 | static int prop_sink(CxProperties *prop, CxPropertiesSink *sink, |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
266 | cxstring key, cxstring value) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
267 | CxMap *map = sink->sink; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
268 | // copy the string and store it into the map |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
269 | cxmutstr v = cx_strdup(value); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
270 | int r = cxMapPut(map, key, &v); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
271 | if (r != 0) cx_strfree(&v); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
272 | return r; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
273 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
274 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
275 | int load_props_from_file(const char *filename, CxMap *map) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
276 | CxProperties prop; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
277 | cxPropertiesInitDefault(&prop); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
278 | CxPropertiesSource src; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
279 | src.src = (void*) filename; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
280 | src.read_init_func = prop_mmap; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
281 | src.read_func = prop_read; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
282 | src.read_clean_func = prop_unmap; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
283 | CxPropertiesSink sink; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
284 | sink.sink = map; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
285 | sink.sink_func = prop_sink; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
286 | return cxPropertiesLoad(&prop, sink, src); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
287 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
288 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
289 | int main() { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
290 | // in contrast to the default map sink, |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
291 | // this one here stores the UCX strings by value |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
292 | CxMap *map = cxHashMapCreateSimple(sizeof(cxmutstr)); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
293 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
294 | // automatically free the UCX string when removed from the map |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
295 | cxDefineDestructor(map, cx_strfree); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
296 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
297 | // use our custom load function to load the data from the file |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
298 | if (load_props_from_file("my-props.properties", map)) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
299 | fputs("Error reading properties.\n", stderr); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
300 | return 1; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
301 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
302 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
303 | // output the read key/value pairs for illustration |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
304 | CxMapIterator iter = cxMapIterator(map); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
305 | cx_foreach(CxMapEntry *, entry, iter) { |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
306 | cxstring k = cx_strn(entry->key->data, entry->key->len); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
307 | cxmutstr *v = entry->value; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
308 | printf("%.*s = %.*s\n", |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
309 | (int) k.length, k.ptr, (int) v->length, v->ptr); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
310 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
311 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
312 | // freeing the map also frees the strings |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
313 | // because we have registered cx_strfree() as destructor function |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
314 | cxMapFree(map); |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
315 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
316 | return 0; |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
317 | } |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
318 | ``` |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
319 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
320 | > A cleaner implementation that does not produce a warning for bluntly casting an `int` to a `void*` |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
321 | > can be achieved by declaring a struct that contains the information, allocate memory for |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
322 | > that struct, and store the pointer in `data_ptr`. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
323 | > For illustrating how properties sources and sinks can be implemented, this was not necessary. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
324 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
325 | ### Additional Status Codes |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
326 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
327 | For sources and sinks there are three additional special status codes, |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
328 | which only appear as return values for `cxPropertiesLoad()`. |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
329 | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
330 | | Status Code | Meaning | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
331 | |-----------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
332 | | CX_PROPERTIES_READ_INIT_FAILED | Initializing the properties source failed and the `cx_properties_read_init_func` returned non-zero. | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
333 | | CX_PROPERTIES_READ_FAILED | Reading from a properties source failed and the `cx_properties_read_func` returned non-zero. | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
334 | | CX_PROPERTIES_SINK_FAILED | Sinking a key/value-pair failed and the `cx_properties_sink_func` returned non-zero. | |
781bd188f1c0
complete the properties documentation
Mike Becker <universe@uap-core.de>
parents:
1231
diff
changeset
|
335 | |
1231
a9f9c59e0b63
write basic parsing documentation
Mike Becker <universe@uap-core.de>
parents:
1230
diff
changeset
|
336 | |
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
337 | <seealso> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
338 | <category ref="apidoc"> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
339 | <a href="https://ucx.sourceforge.io/api/properties_8h.html">properties.h</a> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
340 | </category> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
341 | </seealso> |