src/cx/properties.h

changeset 928
d2d42cb1d59e
parent 924
3c90dfc35f06
equal deleted inserted replaced
927:71e7e9ba4b97 928:d2d42cb1d59e
36 #ifndef UCX_PROPERTIES 36 #ifndef UCX_PROPERTIES
37 #define UCX_PROPERTIES 37 #define UCX_PROPERTIES
38 38
39 #include "common.h" 39 #include "common.h"
40 #include "string.h" 40 #include "string.h"
41 #include "map.h"
41 #include "array_list.h" 42 #include "array_list.h"
43
44 #include <stdio.h>
42 45
43 struct cx_properties_config_s { 46 struct cx_properties_config_s {
44 /** 47 /**
45 * The key/value delimiter that shall be used. 48 * The key/value delimiter that shall be used.
46 * This is '=' by default. 49 * This is '=' by default.
100 * This either happens when the last line does not terminate with a line 103 * This either happens when the last line does not terminate with a line
101 * break, or when the input ends with a parsed key but no value. 104 * break, or when the input ends with a parsed key but no value.
102 */ 105 */
103 CX_PROPERTIES_INCOMPLETE_DATA, 106 CX_PROPERTIES_INCOMPLETE_DATA,
104 /** 107 /**
108 * Not used as a status and never returned by any function.
109 *
110 * You can use this enumerator to check for all "good" status results
111 * by checking if the status is less than \c CX_PROPERTIES_OK.
112 *
113 * A "good" status means, that you can refill data and continue parsing.
114 */
115 CX_PROPERTIES_OK,
116 /**
105 * Input buffer is \c NULL. 117 * Input buffer is \c NULL.
106 */ 118 */
107 CX_PROPERTIES_NULL_INPUT, 119 CX_PROPERTIES_NULL_INPUT,
108 /** 120 /**
109 * The line contains a delimiter, but no key. 121 * The line contains a delimiter, but no key.
115 CX_PROPERTIES_INVALID_MISSING_DELIMITER, 127 CX_PROPERTIES_INVALID_MISSING_DELIMITER,
116 /** 128 /**
117 * More internal buffer was needed, but could not be allocated. 129 * More internal buffer was needed, but could not be allocated.
118 */ 130 */
119 CX_PROPERTIES_BUFFER_ALLOC_FAILED, 131 CX_PROPERTIES_BUFFER_ALLOC_FAILED,
132 /**
133 * Initializing the properties source failed.
134 *
135 * @see cx_properties_read_init_func
136 */
137 CX_PROPERTIES_READ_INIT_FAILED,
138 /**
139 * Reading from a properties source failed.
140 *
141 * @see cx_properties_read_func
142 */
143 CX_PROPERTIES_READ_FAILED,
144 /**
145 * Sinking a k/v-pair failed.
146 *
147 * @see cx_properties_sink_func
148 */
149 CX_PROPERTIES_SINK_FAILED,
120 }; 150 };
151
152 /**
153 * Typedef for the properties status enum.
154 */
155 typedef enum cx_properties_status CxPropertiesStatus;
121 156
122 /** 157 /**
123 * Interface for working with properties data. 158 * Interface for working with properties data.
124 */ 159 */
125 struct cx_properties_s { 160 struct cx_properties_s {
167 /** 202 /**
168 * Typedef for the properties interface. 203 * Typedef for the properties interface.
169 */ 204 */
170 typedef struct cx_properties_s CxProperties; 205 typedef struct cx_properties_s CxProperties;
171 206
207
208 /**
209 * Typedef for a properties sink.
210 */
211 typedef struct cx_properties_sink_s CxPropertiesSink;
212
213 /**
214 * A function that consumes a k/v-pair in a sink.
215 *
216 * The sink could be e.g. a map and the sink function would be calling
217 * a map function to store the k/v-pair.
218 *
219 * @param prop the properties interface that wants to sink a k/v-pair
220 * @param sink the sink
221 * @param key the key
222 * @param value the value
223 * @return zero on success, non-zero when sinking the k/v-pair failed
224 */
225 __attribute__((__nonnull__))
226 typedef int(*cx_properties_sink_func)(
227 CxProperties *prop,
228 CxPropertiesSink *sink,
229 cxstring key,
230 cxstring value
231 );
232
233 /**
234 * Defines a sink for k/v-pairs.
235 */
236 struct cx_properties_sink_s {
237 /**
238 * The sink object.
239 */
240 void *sink;
241 /**
242 * Optional custom data.
243 */
244 void *data;
245 /**
246 * A function for consuming k/v-pairs into the sink.
247 */
248 cx_properties_sink_func sink_func;
249 };
250
251
252 /**
253 * Typedef for a properties source.
254 */
255 typedef struct cx_properties_source_s CxPropertiesSource;
256
257 /**
258 * A function that reads data from a source.
259 *
260 * When the source is depleted, implementations SHALL provide an empty
261 * string in the \p target and return zero.
262 * A non-zero return value is only permitted in case of an error.
263 *
264 * The meaning of the optional parameters is implementation-dependent.
265 *
266 * @param prop the properties interface that wants to read from the source
267 * @param src the source
268 * @param target a string buffer where the read data shall be stored
269 * @return zero on success, non-zero when reading data failed
270 */
271 __attribute__((__nonnull__))
272 typedef int(*cx_properties_read_func)(
273 CxProperties *prop,
274 CxPropertiesSource *src,
275 cxstring *target
276 );
277
278 /**
279 * A function that may initialize additional memory for the source.
280 *
281 * @param prop the properties interface that wants to read from the source
282 * @param src the source
283 * @return zero when initialization was successful, non-zero otherwise
284 */
285 __attribute__((__nonnull__))
286 typedef int(*cx_properties_read_init_func)(
287 CxProperties *prop,
288 CxPropertiesSource *src
289 );
290
291 /**
292 * A function that cleans memory initialized by the read_init_func.
293 *
294 * @param prop the properties interface that wants to read from the source
295 * @param src the source
296 */
297 __attribute__((__nonnull__))
298 typedef void(*cx_properties_read_clean_func)(
299 CxProperties *prop,
300 CxPropertiesSource *src
301 );
302
303 /**
304 * Defines a properties source.
305 */
306 struct cx_properties_source_s {
307 /**
308 * The source object.
309 *
310 * For example a file stream or a string.
311 */
312 void *src;
313 /**
314 * Optional additional data pointer.
315 */
316 void *data_ptr;
317 /**
318 * Optional size information.
319 */
320 size_t data_size;
321 /**
322 * A function that reads data from the source.
323 */
324 cx_properties_read_func read_func;
325 /**
326 * Optional function that may prepare the source for reading data.
327 */
328 cx_properties_read_init_func read_init_func;
329 /**
330 * Optional function that cleans additional memory allocated by the
331 * read_init_func.
332 */
333 cx_properties_read_clean_func read_clean_func;
334 };
172 335
173 /** 336 /**
174 * Initialize a properties interface. 337 * Initialize a properties interface.
175 * 338 *
176 * @param prop the properties interface 339 * @param prop the properties interface
249 * 412 *
250 * @param prop the properties interface 413 * @param prop the properties interface
251 * @param buf a pointer to stack memory 414 * @param buf a pointer to stack memory
252 * @param capacity the capacity of the stack memory 415 * @param capacity the capacity of the stack memory
253 */ 416 */
417 __attribute__((__nonnull__))
254 void cxPropertiesUseStack( 418 void cxPropertiesUseStack(
255 CxProperties *prop, 419 CxProperties *prop,
256 char *buf, 420 char *buf,
257 size_t capacity 421 size_t capacity
258 ); 422 );
275 * @param key a pointer to the cxstring that shall contain the property name 439 * @param key a pointer to the cxstring that shall contain the property name
276 * @param value a pointer to the cxstring that shall contain the property value 440 * @param value a pointer to the cxstring that shall contain the property value
277 * @return the status code as defined above 441 * @return the status code as defined above
278 * @see cxPropertiesFill() 442 * @see cxPropertiesFill()
279 */ 443 */
280 enum cx_properties_status cxPropertiesNext( 444 __attribute__((__nonnull__, __warn_unused_result__))
445 CxPropertiesStatus cxPropertiesNext(
281 CxProperties *prop, 446 CxProperties *prop,
282 cxstring *key, 447 cxstring *key,
283 cxstring *value 448 cxstring *value
284 ); 449 );
285 450
451 /**
452 * Creates a properties sink for an UCX map.
453 *
454 * The values stored in the map will be pointers to strings allocated
455 * by #cx_strdup_a().
456 * The default stdlib allocator will be used, unless you specify a custom
457 * allocator in the optional \c data of the sink.
458 *
459 * @param map the map that shall consume the k/v-pairs.
460 * @return the sink
461 * @see cxPropertiesLoad()
462 */
463 __attribute__((__nonnull__, __warn_unused_result__))
464 CxPropertiesSink cxPropertiesMapSink(CxMap *map);
465
466 /**
467 * Creates a properties source based on an UCX string.
468 *
469 * @param str the string
470 * @return the properties source
471 * @see cxPropertiesLoad()
472 */
473 __attribute__((__warn_unused_result__))
474 CxPropertiesSource cxPropertiesStringSource(cxstring str);
475
476 /**
477 * Creates a properties source based on C string with the specified length.
478 *
479 * @param str the string
480 * @param len the length
481 * @return the properties source
482 * @see cxPropertiesLoad()
483 */
484 __attribute__((__nonnull__, __warn_unused_result__))
485 CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len);
486
487 /**
488 * Creates a properties source based on a C string.
489 *
490 * The length will be determined with strlen(), so the string MUST be
491 * zero-terminated.
492 *
493 * @param str the string
494 * @return the properties source
495 * @see cxPropertiesLoad()
496 */
497 __attribute__((__nonnull__, __warn_unused_result__))
498 CxPropertiesSource cxPropertiesCstrSource(const char *str);
499
500 /**
501 * Creates a properties source based on an FILE.
502 *
503 * @param file the file
504 * @param chunk_size how many bytes may be read in one operation
505 *
506 * @return the properties source
507 * @see cxPropertiesLoad()
508 */
509 __attribute__((__nonnull__, __warn_unused_result__))
510 CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size);
511
512
513 /**
514 * Loads properties data from a source and transfers it to a sink.
515 *
516 * This function tries to read as much data from the source as possible.
517 * When the source was completely consumed and at least on k/v-pair was found,
518 * the return value will be #CX_PROPERTIES_NO_ERROR.
519 * When the source was consumed but no k/v-pairs were found, the return value
520 * will be #CX_PROPERTIES_NO_DATA.
521 * The other result codes apply, according to their description.
522 *
523 * @param prop the properties interface
524 * @param sink the sink
525 * @param source the source
526 * @return the status of the last operation
527 */
528 __attribute__((__nonnull__))
529 CxPropertiesStatus cxPropertiesLoad(
530 CxProperties *prop,
531 CxPropertiesSink sink,
532 CxPropertiesSource source
533 );
534
286 #endif // UCX_PROPERTIES 535 #endif // UCX_PROPERTIES

mercurial