Merge branch 'feature/e-endstop' into merge-abc-with-homing
[clinton/Smoothieware.git] / src / libs / ConfigCache.cpp
1 #include "ConfigCache.h"
2 #include "ConfigValue.h"
3
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 {
17 for (auto &kv : store) {
18 delete kv;
19 }
20 store.clear();
21 storage_t().swap(store); // makes sure the vector releases its memory
22 }
23
24 void ConfigCache::add(ConfigValue *v)
25 {
26 store.push_back(v);
27 }
28
29 // If we find an existing value, replace it, otherwise, push it at the back of the list
30 void ConfigCache::replace_or_push_back(ConfigValue *new_value)
31 {
32 // For each already existing element
33 for(auto &cv : store) {
34 // If this configvalue matches the checksum
35 if(memcmp(new_value->check_sums, cv->check_sums, sizeof(cv->check_sums)) == 0) {
36 // Replace with the provided value
37 cv = new_value;
38 printf("WARNING: duplicate config line replaced\n");
39 return;
40 }
41 }
42
43 // Value does not already exists, add to the list
44 store.push_back(new_value);
45 }
46
47 ConfigValue *ConfigCache::lookup(const uint16_t *check_sums) const
48 {
49 for( auto &cv : store) {
50 if(memcmp(check_sums, cv->check_sums, sizeof(cv->check_sums)) == 0)
51 return cv;
52 }
53
54 return NULL;
55 }
56
57 void ConfigCache::collect(uint16_t family, uint16_t cs, vector<uint16_t> *list)
58 {
59 for( auto &kv : store ) {
60 if( kv->check_sums[2] == cs && kv->check_sums[0] == family ) {
61 // We found a module enable for this family, add it's number
62 list->push_back(kv->check_sums[1]);
63 }
64 }
65 }
66
67 void ConfigCache::dump(StreamOutput *stream)
68 {
69 int l = 1;
70 for( auto &kv : store ) {
71 ConfigValue *v = kv;
72 stream->printf("%3d - %04X %04X %04X : '%s' - found: %d, default: %d, default-double: %f, default-int: %d\n",
73 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 );
74 }
75 }