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 | ||
8 | string ConfigSource::process_char_from_ascii_config(int c, ConfigCache *cache) | |
9 | { | |
10 | static string buffer; | |
11 | if (c == '\n' || c == EOF) { | |
12 | // We have a new line | |
13 | if( buffer[0] == '#' ) { | |
14 | buffer.clear(); // Ignore comments | |
15 | return ""; | |
16 | } | |
17 | if( buffer.length() < 3 ) { | |
18 | buffer.clear(); //Ignore empty lines | |
19 | return ""; | |
20 | } | |
21 | size_t begin_key = buffer.find_first_not_of(" \t"); | |
22 | size_t begin_value = buffer.find_first_not_of(" \t", buffer.find_first_of(" \t", begin_key)); | |
23 | ||
24 | uint16_t check_sums[3]; | |
25 | get_checksums(check_sums, buffer.substr(begin_key, buffer.find_first_of(" \t", begin_key) - begin_key).append(" ")); | |
26 | ||
27 | ConfigValue *result = new ConfigValue; | |
28 | ||
29 | result->found = true; | |
30 | result->check_sums[0] = check_sums[0]; | |
31 | result->check_sums[1] = check_sums[1]; | |
32 | result->check_sums[2] = check_sums[2]; | |
33 | ||
34 | result->value = buffer.substr(begin_value, buffer.find_first_of("\r\n# \t", begin_value + 1) - begin_value); | |
35 | // Append the newly found value to the cache we were passed | |
36 | cache->replace_or_push_back(result); | |
37 | ||
38 | buffer.clear(); | |
39 | ||
40 | ||
41 | return result->value; | |
42 | } else | |
43 | buffer += c; | |
44 | ||
45 | return ""; | |
46 | } | |
47 | ||
48 | string ConfigSource::process_char_from_ascii_config(int c, uint16_t line_checksums[3]) | |
49 | { | |
50 | static string buffer; | |
51 | string value; | |
52 | if (c == '\n' || c == EOF) { | |
53 | // We have a new line | |
54 | if( buffer[0] == '#' ) { | |
55 | buffer.clear(); // Ignore comments | |
56 | return ""; | |
57 | } | |
58 | if( buffer.length() < 3 ) { | |
59 | buffer.clear(); //Ignore empty lines | |
60 | return ""; | |
61 | } | |
62 | size_t begin_key = buffer.find_first_not_of(" \t"); | |
63 | size_t begin_value = buffer.find_first_not_of(" \t", buffer.find_first_of(" \t", begin_key)); | |
64 | ||
65 | uint16_t check_sums[3]; | |
66 | get_checksums(check_sums, buffer.substr(begin_key, buffer.find_first_of(" \t", begin_key) - begin_key).append(" ")); | |
67 | ||
68 | if(check_sums[0] == line_checksums[0] && check_sums[1] == line_checksums[1] && check_sums[2] == line_checksums[2] ) { | |
69 | value = buffer.substr(begin_value, buffer.find_first_of("\r\n# \t", begin_value + 1) - begin_value); | |
70 | buffer.clear(); | |
71 | return value; | |
72 | } | |
73 | ||
74 | buffer.clear(); | |
75 | } else | |
76 | buffer += c; | |
77 | return value; | |
78 | } |