2 #include "ConfigSource.h"
3 #include "ConfigValue.h"
4 #include "ConfigCache.h"
8 ConfigValue
* ConfigSource::process_line(const string
&buffer
)
10 if( buffer
[0] == '#' ) {
13 if( buffer
.length() < 3 ) {
17 size_t begin_key
= buffer
.find_first_not_of(" \t");
18 if(begin_key
== string::npos
|| buffer
[begin_key
] == '#') return NULL
; // comment line or blank line
20 size_t end_key
= buffer
.find_first_of(" \t", begin_key
);
21 if(end_key
== string::npos
) {
22 printf("ERROR: config file line %s is invalid, no key value pair found\r\n", buffer
.c_str());
26 size_t begin_value
= buffer
.find_first_not_of(" \t", end_key
);
27 if(begin_value
== string::npos
|| buffer
[begin_value
] == '#') {
28 printf("ERROR: config file line %s has no value\r\n", buffer
.c_str());
32 string key
= buffer
.substr(begin_key
, end_key
- begin_key
);
33 uint16_t check_sums
[3];
34 get_checksums(check_sums
, key
);
36 ConfigValue
*result
= new ConfigValue
;
39 result
->check_sums
[0] = check_sums
[0];
40 result
->check_sums
[1] = check_sums
[1];
41 result
->check_sums
[2] = check_sums
[2];
43 size_t end_value
= buffer
.find_first_of("\r\n# \t", begin_value
+ 1);
44 size_t vsize
= end_value
== string::npos
? end_value
: end_value
- begin_value
;
45 result
->value
= buffer
.substr(begin_value
, vsize
);
47 //printf("key: %s, value: %s\n\n", key.c_str(), result->value.c_str());
51 ConfigValue
* ConfigSource::process_line_from_ascii_config(const string
&buffer
, ConfigCache
*cache
)
53 ConfigValue
*result
= process_line(buffer
);
55 // Append the newly found value to the cache we were passed
56 cache
->replace_or_push_back(result
);
62 string
ConfigSource::process_line_from_ascii_config(const string
&buffer
, uint16_t line_checksums
[3])
65 ConfigValue
*result
= process_line(buffer
);
67 if(result
->check_sums
[0] == line_checksums
[0] && result
->check_sums
[1] == line_checksums
[1] && result
->check_sums
[2] == line_checksums
[2]) {