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