ucx/properties.c

changeset 854
1c8401ece69e
parent 852
83fdf679df99
equal deleted inserted replaced
853:2ad93ebdc8d9 854:1c8401ece69e
30 30
31 #include <assert.h> 31 #include <assert.h>
32 32
33 const CxPropertiesConfig cx_properties_config_default = { 33 const CxPropertiesConfig cx_properties_config_default = {
34 '=', 34 '=',
35 //'\\',
36 '#', 35 '#',
37 '\0', 36 '\0',
38 '\0' 37 '\0',
38 '\\',
39 }; 39 };
40 40
41 void cxPropertiesInit( 41 void cxPropertiesInit(
42 CxProperties *prop, 42 CxProperties *prop,
43 CxPropertiesConfig config 43 CxPropertiesConfig config
224 return CX_PROPERTIES_NO_ERROR; 224 return CX_PROPERTIES_NO_ERROR;
225 } else { 225 } else {
226 return CX_PROPERTIES_INVALID_EMPTY_KEY; 226 return CX_PROPERTIES_INVALID_EMPTY_KEY;
227 } 227 }
228 } 228 }
229 // unreachable - either we returned or skipped a blank line
230 assert(false);
231 } 229 }
232 230
233 // when we come to this point, all data must have been read 231 // when we come to this point, all data must have been read
234 assert(cxBufferEof(&prop->buffer)); 232 assert(cxBufferEof(&prop->buffer));
235 assert(cxBufferEof(&prop->input)); 233 assert(cxBufferEof(&prop->input));
252 } 250 }
253 251
254 CxPropertiesSink cxPropertiesMapSink(CxMap *map) { 252 CxPropertiesSink cxPropertiesMapSink(CxMap *map) {
255 CxPropertiesSink sink; 253 CxPropertiesSink sink;
256 sink.sink = map; 254 sink.sink = map;
257 sink.data = cxDefaultAllocator; 255 sink.data = (void*) cxDefaultAllocator;
258 sink.sink_func = cx_properties_sink_map; 256 sink.sink_func = cx_properties_sink_map;
259 return sink; 257 return sink;
260 } 258 }
261 259
262 static int cx_properties_read_string( 260 static int cx_properties_read_string(
280 CxPropertiesSource *src, 278 CxPropertiesSource *src,
281 cxstring *target 279 cxstring *target
282 ) { 280 ) {
283 target->ptr = src->data_ptr; 281 target->ptr = src->data_ptr;
284 target->length = fread(src->data_ptr, 1, src->data_size, src->src); 282 target->length = fread(src->data_ptr, 1, src->data_size, src->src);
285 return ferror(src->src); 283 return ferror((FILE*)src->src);
286 } 284 }
287 285
288 static int cx_properties_read_init_file( 286 static int cx_properties_read_init_file(
289 cx_attr_unused CxProperties *prop, 287 cx_attr_unused CxProperties *prop,
290 CxPropertiesSource *src 288 CxPropertiesSource *src
360 } 358 }
361 } 359 }
362 360
363 // transfer the data from the source to the sink 361 // transfer the data from the source to the sink
364 CxPropertiesStatus status; 362 CxPropertiesStatus status;
363 CxPropertiesStatus kv_status = CX_PROPERTIES_NO_DATA;
365 bool found = false; 364 bool found = false;
366 while (true) { 365 while (true) {
367 // read input 366 // read input
368 cxstring input; 367 cxstring input;
369 if (source.read_func(prop, &source, &input)) { 368 if (source.read_func(prop, &source, &input)) {
371 break; 370 break;
372 } 371 }
373 372
374 // no more data - break 373 // no more data - break
375 if (input.length == 0) { 374 if (input.length == 0) {
376 status = found ? CX_PROPERTIES_NO_ERROR : CX_PROPERTIES_NO_DATA; 375 if (found) {
376 // something was found, check the last kv_status
377 if (kv_status == CX_PROPERTIES_INCOMPLETE_DATA) {
378 status = CX_PROPERTIES_INCOMPLETE_DATA;
379 } else {
380 status = CX_PROPERTIES_NO_ERROR;
381 }
382 } else {
383 // nothing found
384 status = CX_PROPERTIES_NO_DATA;
385 }
377 break; 386 break;
378 } 387 }
379 388
380 // set the input buffer and read the k/v-pairs 389 // set the input buffer and read the k/v-pairs
381 cxPropertiesFill(prop, input); 390 cxPropertiesFill(prop, input);
382 391
383 CxPropertiesStatus kv_status;
384 do { 392 do {
385 cxstring key, value; 393 cxstring key, value;
386 kv_status = cxPropertiesNext(prop, &key, &value); 394 kv_status = cxPropertiesNext(prop, &key, &value);
387 if (kv_status == CX_PROPERTIES_NO_ERROR) { 395 if (kv_status == CX_PROPERTIES_NO_ERROR) {
388 found = true; 396 found = true;

mercurial