1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 #include <stdio.h>
45 #include <string.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51
52
53
54 struct cx_properties_config_s {
55
56
57
58
59 char delimiter;
60
61
62
63
64
65 char comment1;
66
67
68
69
70
71 char comment2;
72
73
74
75
76
77 char comment3;
78
79
80
81
82
83
84
85
86 char continuation;
87 };
88
89
90
91
92 typedef struct cx_properties_config_s CxPropertiesConfig;
93
94
95
96
97 cx_attr_export
98 extern const CxPropertiesConfig cx_properties_config_default;
99
100
101
102
103 enum cx_properties_status {
104
105
106
107 CX_PROPERTIES_NO_ERROR,
108
109
110
111 CX_PROPERTIES_NO_DATA,
112
113
114
115
116
117
118 CX_PROPERTIES_INCOMPLETE_DATA,
119
120
121
122
123
124
125
126
127 CX_PROPERTIES_OK,
128
129
130
131 CX_PROPERTIES_NULL_INPUT,
132
133
134
135 CX_PROPERTIES_INVALID_EMPTY_KEY,
136
137
138
139 CX_PROPERTIES_INVALID_MISSING_DELIMITER,
140
141
142
143 CX_PROPERTIES_BUFFER_ALLOC_FAILED,
144
145
146
147
148
149 CX_PROPERTIES_READ_INIT_FAILED,
150
151
152
153
154
155 CX_PROPERTIES_READ_FAILED,
156
157
158
159
160
161 CX_PROPERTIES_SINK_FAILED,
162 };
163
164
165
166
167 typedef enum cx_properties_status CxPropertiesStatus;
168
169
170
171
172 struct cx_properties_s {
173
174
175
176 CxPropertiesConfig config;
177
178
179
180
181 CxBuffer input;
182
183
184
185
186 CxBuffer buffer;
187 };
188
189
190
191
192 typedef struct cx_properties_s CxProperties;
193
194
195
196
197
198 typedef struct cx_properties_sink_s CxPropertiesSink;
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 cx_attr_nonnull
214 typedef int(*cx_properties_sink_func)(
215 CxProperties *prop,
216 CxPropertiesSink *sink,
217 cxstring key,
218 cxstring value
219 );
220
221
222
223
224 struct cx_properties_sink_s {
225
226
227
228 void *sink;
229
230
231
232 void *data;
233
234
235
236 cx_properties_sink_func sink_func;
237 };
238
239
240
241
242
243 typedef struct cx_properties_source_s CxPropertiesSource;
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 cx_attr_nonnull
261 typedef int(*cx_properties_read_func)(
262 CxProperties *prop,
263 CxPropertiesSource *src,
264 cxstring *target
265 );
266
267
268
269
270
271
272
273
274
275 cx_attr_nonnull
276 typedef int(*cx_properties_read_init_func)(
277 CxProperties *prop,
278 CxPropertiesSource *src
279 );
280
281
282
283
284
285
286
287 cx_attr_nonnull
288 typedef void(*cx_properties_read_clean_func)(
289 CxProperties *prop,
290 CxPropertiesSource *src
291 );
292
293
294
295
296 struct cx_properties_source_s {
297
298
299
300
301
302 void *src;
303
304
305
306 void *data_ptr;
307
308
309
310 size_t data_size;
311
312
313
314 cx_properties_read_func read_func;
315
316
317
318 cx_properties_read_init_func read_init_func;
319
320
321
322
323 cx_properties_read_clean_func read_clean_func;
324 };
325
326
327
328
329
330
331
332
333 cx_attr_nonnull
334 cx_attr_export
335 void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config);
336
337
338
339
340
341
342
343
344
345
346
347
348 cx_attr_nonnull
349 cx_attr_export
350 void cxPropertiesDestroy(CxProperties *prop);
351
352
353
354
355
356
357
358
359
360 cx_attr_nonnull
361 static inline
void cxPropertiesReset(CxProperties *prop) {
362 CxPropertiesConfig config = prop->config;
363 cxPropertiesDestroy(prop);
364 cxPropertiesInit(prop, config);
365 }
366
367
368
369
370
371
372
373 #define cxPropertiesInitDefault(prop) \
374 cxPropertiesInit(prop, cx_properties_config_default)
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397 cx_attr_nonnull
398 cx_attr_access_r(
2,
3)
399 cx_attr_export
400 int cxPropertiesFilln(
401 CxProperties *prop,
402 const char *buf,
403 size_t len
404 );
405
406 #ifdef __cplusplus
407 }
408 cx_attr_nonnull
409 static inline
int cxPropertiesFill(
410 CxProperties *prop,
411 cxstring str
412 ) {
413 return cxPropertiesFilln(prop, str.ptr, str.length);
414 }
415
416 cx_attr_nonnull
417 static inline
int cxPropertiesFill(
418 CxProperties *prop,
419 cxmutstr str
420 ) {
421 return cxPropertiesFilln(prop, str.ptr, str.length);
422 }
423
424 cx_attr_nonnull
425 cx_attr_cstr_arg(
2)
426 static inline
int cxPropertiesFill(
427 CxProperties *prop,
428 const char *str
429 ) {
430 return cxPropertiesFilln(prop, str, strlen(str));
431 }
432
433 extern "C" {
434 #else
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
462
463
464
465 cx_attr_nonnull
466 static inline
int cx_properties_fill_cxstr(
467 CxProperties *prop,
468 cxstring str
469 ) {
470 return cxPropertiesFilln(prop, str.ptr, str.length);
471 }
472
473
474
475
476 cx_attr_nonnull
477 static inline
int cx_properties_fill_mutstr(
478 CxProperties *prop,
479 cxmutstr str
480 ) {
481 return cxPropertiesFilln(prop, str.ptr, str.length);
482 }
483
484
485
486
487 cx_attr_nonnull
488 cx_attr_cstr_arg(
2)
489 static 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
497
498
499
500
501
502
503
504 cx_attr_nonnull
505 cx_attr_export
506 void cxPropertiesUseStack(
507 CxProperties *prop,
508 char *buf,
509 size_t capacity
510 );
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 cx_attr_nonnull
543 cx_attr_nodiscard
544 cx_attr_export
545 CxPropertiesStatus cxPropertiesNext(
546 CxProperties *prop,
547 cxstring *key,
548 cxstring *value
549 );
550
551
552
553
554
555
556
557
558
559
560
561
562
563 cx_attr_nonnull
564 cx_attr_nodiscard
565 cx_attr_export
566 CxPropertiesSink cxPropertiesMapSink(CxMap *map);
567
568
569
570
571
572
573
574
575 cx_attr_nodiscard
576 cx_attr_export
577 CxPropertiesSource cxPropertiesStringSource(cxstring str);
578
579
580
581
582
583
584
585
586
587 cx_attr_nonnull
588 cx_attr_nodiscard
589 cx_attr_access_r(
1,
2)
590 cx_attr_export
591 CxPropertiesSource cxPropertiesCstrnSource(
const char *str,
size_t len);
592
593
594
595
596
597
598
599
600
601
602
603 cx_attr_nonnull
604 cx_attr_nodiscard
605 cx_attr_cstr_arg(
1)
606 cx_attr_export
607 CxPropertiesSource cxPropertiesCstrSource(
const char *str);
608
609
610
611
612
613
614
615
616
617
618 cx_attr_nonnull
619 cx_attr_nodiscard
620 cx_attr_access_r(
1)
621 cx_attr_export
622 CxPropertiesSource cxPropertiesFileSource(
FILE *file,
size_t chunk_size);
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653 cx_attr_nonnull
654 cx_attr_export
655 CxPropertiesStatus cxPropertiesLoad(
656 CxProperties *prop,
657 CxPropertiesSink sink,
658 CxPropertiesSource source
659 );
660
661 #ifdef __cplusplus
662 }
663 #endif
664
665 #endif
666