Commit | Line | Data |
---|---|---|
ecd4acfb JM |
1 | #include "utils.h" |
2 | #include "ConfigSource.h" | |
8d54c34c JM |
3 | #include "ConfigValue.h" |
4 | #include "ConfigCache.h" | |
5 | ||
ecd4acfb JM |
6 | #include "stdio.h" |
7 | ||
577414f6 | 8 | ConfigValue* ConfigSource::process_line(const string &buffer) |
ecd4acfb | 9 | { |
577414f6 JM |
10 | if( buffer[0] == '#' ) { |
11 | return NULL; | |
12 | } | |
13 | if( buffer.length() < 3 ) { | |
14 | return NULL; | |
15 | } | |
ecd4acfb | 16 | |
577414f6 JM |
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 | |
ecd4acfb | 19 | |
577414f6 JM |
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()); | |
23 | return NULL; | |
24 | } | |
ecd4acfb | 25 | |
577414f6 JM |
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()); | |
29 | return NULL; | |
30 | } | |
ecd4acfb | 31 | |
d8cff5af | 32 | string key= buffer.substr(begin_key, end_key - begin_key); |
577414f6 JM |
33 | uint16_t check_sums[3]; |
34 | get_checksums(check_sums, key); | |
ecd4acfb | 35 | |
577414f6 | 36 | ConfigValue *result = new ConfigValue; |
ecd4acfb | 37 | |
577414f6 JM |
38 | result->found = true; |
39 | result->check_sums[0] = check_sums[0]; | |
40 | result->check_sums[1] = check_sums[1]; | |
41 | result->check_sums[2] = check_sums[2]; | |
ecd4acfb | 42 | |
577414f6 JM |
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); | |
ecd4acfb | 46 | |
577414f6 JM |
47 | //printf("key: %s, value: %s\n\n", key.c_str(), result->value.c_str()); |
48 | return result; | |
ecd4acfb JM |
49 | } |
50 | ||
f45e6161 | 51 | ConfigValue* ConfigSource::process_line_from_ascii_config(const string &buffer, ConfigCache *cache) |
ecd4acfb | 52 | { |
577414f6 JM |
53 | ConfigValue *result = process_line(buffer); |
54 | if(result != NULL) { | |
55 | // Append the newly found value to the cache we were passed | |
56 | cache->replace_or_push_back(result); | |
f45e6161 | 57 | return result; |
577414f6 | 58 | } |
f45e6161 | 59 | return NULL; |
577414f6 | 60 | } |
ecd4acfb | 61 | |
577414f6 JM |
62 | string ConfigSource::process_line_from_ascii_config(const string &buffer, uint16_t line_checksums[3]) |
63 | { | |
64 | string value= ""; | |
65 | ConfigValue *result = process_line(buffer); | |
66 | if(result != NULL) { | |
67 | if(result->check_sums[0] == line_checksums[0] && result->check_sums[1] == line_checksums[1] && result->check_sums[2] == line_checksums[2]) { | |
68 | value= result->value; | |
ecd4acfb | 69 | } |
577414f6 JM |
70 | delete result; |
71 | } | |
ecd4acfb JM |
72 | return value; |
73 | } |