Commit | Line | Data |
---|---|---|
8d54c34c JM |
1 | #include "ConfigCache.h" |
2 | #include "ConfigValue.h" | |
3 | ||
a2f7633f JM |
4 | #include "libs/StreamOutput.h" |
5 | ||
6 | ConfigCache::ConfigCache() | |
7 | { | |
8 | } | |
9 | ||
10 | ConfigCache::~ConfigCache() | |
11 | { | |
12 | clear(); | |
13 | } | |
14 | ||
15 | void ConfigCache::clear() | |
16 | { | |
d41a043b | 17 | for (auto &kv : store) { |
34a6a1d6 | 18 | delete kv; |
a2f7633f | 19 | } |
d41a043b | 20 | store.clear(); |
34a6a1d6 | 21 | storage_t().swap(store); // makes sure the vector releases its memory |
d41a043b JM |
22 | } |
23 | ||
24 | void ConfigCache::add(ConfigValue *v) | |
25 | { | |
34a6a1d6 | 26 | store.push_back(v); |
a2f7633f JM |
27 | } |
28 | ||
cab80ea9 JM |
29 | void ConfigCache::pop() |
30 | { | |
31 | auto cv= store.back(); | |
32 | store.pop_back(); | |
33 | delete cv; | |
34 | } | |
35 | ||
8d54c34c JM |
36 | // If we find an existing value, replace it, otherwise, push it at the back of the list |
37 | void ConfigCache::replace_or_push_back(ConfigValue *new_value) | |
38 | { | |
34a6a1d6 JM |
39 | // For each already existing element |
40 | for(auto &cv : store) { | |
41 | // If this configvalue matches the checksum | |
42 | if(memcmp(new_value->check_sums, cv->check_sums, sizeof(cv->check_sums)) == 0) { | |
43 | // Replace with the provided value | |
afc3f2c0 | 44 | delete cv; // free up old one |
34a6a1d6 JM |
45 | cv = new_value; |
46 | printf("WARNING: duplicate config line replaced\n"); | |
47 | return; | |
48 | } | |
49 | } | |
50 | ||
51 | // Value does not already exists, add to the list | |
52 | store.push_back(new_value); | |
a2f7633f JM |
53 | } |
54 | ||
55 | ConfigValue *ConfigCache::lookup(const uint16_t *check_sums) const | |
56 | { | |
34a6a1d6 JM |
57 | for( auto &cv : store) { |
58 | if(memcmp(check_sums, cv->check_sums, sizeof(cv->check_sums)) == 0) | |
59 | return cv; | |
60 | } | |
61 | ||
62 | return NULL; | |
a2f7633f JM |
63 | } |
64 | ||
65 | void ConfigCache::collect(uint16_t family, uint16_t cs, vector<uint16_t> *list) | |
66 | { | |
34a6a1d6 JM |
67 | for( auto &kv : store ) { |
68 | if( kv->check_sums[2] == cs && kv->check_sums[0] == family ) { | |
a2f7633f | 69 | // We found a module enable for this family, add it's number |
34a6a1d6 | 70 | list->push_back(kv->check_sums[1]); |
a2f7633f JM |
71 | } |
72 | } | |
73 | } | |
74 | ||
75 | void ConfigCache::dump(StreamOutput *stream) | |
76 | { | |
d41a043b | 77 | int l = 1; |
34a6a1d6 JM |
78 | for( auto &kv : store ) { |
79 | ConfigValue *v = kv; | |
a2f7633f | 80 | stream->printf("%3d - %04X %04X %04X : '%s' - found: %d, default: %d, default-double: %f, default-int: %d\n", |
d41a043b | 81 | l++, v->check_sums[0], v->check_sums[1], v->check_sums[2], v->value.c_str(), v->found, v->default_set, v->default_double, v->default_int ); |
a2f7633f | 82 | } |
8d54c34c | 83 | } |