| 1 | #include "utils.h" |
| 2 | #include "ConfigSource.h" |
| 3 | #include "ConfigValue.h" |
| 4 | #include "ConfigCache.h" |
| 5 | |
| 6 | #include "stdio.h" |
| 7 | |
| 8 | ConfigValue* ConfigSource::process_line(const string &buffer) |
| 9 | { |
| 10 | if( buffer[0] == '#' ) { |
| 11 | return NULL; |
| 12 | } |
| 13 | if( buffer.length() < 3 ) { |
| 14 | return NULL; |
| 15 | } |
| 16 | |
| 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 |
| 19 | |
| 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 | } |
| 25 | |
| 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 | } |
| 31 | |
| 32 | string key= buffer.substr(begin_key, end_key - begin_key); |
| 33 | uint16_t check_sums[3]; |
| 34 | get_checksums(check_sums, key); |
| 35 | |
| 36 | ConfigValue *result = new ConfigValue; |
| 37 | |
| 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]; |
| 42 | |
| 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); |
| 46 | |
| 47 | //printf("key: %s, value: %s\n\n", key.c_str(), result->value.c_str()); |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | ConfigValue* ConfigSource::process_line_from_ascii_config(const string &buffer, ConfigCache *cache) |
| 52 | { |
| 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); |
| 57 | return result; |
| 58 | } |
| 59 | return NULL; |
| 60 | } |
| 61 | |
| 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; |
| 69 | } |
| 70 | delete result; |
| 71 | } |
| 72 | return value; |
| 73 | } |