Merge branch 'feature/e-endstop' into merge-abc-with-homing
[clinton/Smoothieware.git] / src / libs / ConfigCache.cpp
CommitLineData
8d54c34c
JM
1#include "ConfigCache.h"
2#include "ConfigValue.h"
3
a2f7633f
JM
4#include "libs/StreamOutput.h"
5
6ConfigCache::ConfigCache()
7{
8}
9
10ConfigCache::~ConfigCache()
11{
12 clear();
13}
14
15void 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
24void ConfigCache::add(ConfigValue *v)
25{
34a6a1d6 26 store.push_back(v);
a2f7633f
JM
27}
28
8d54c34c
JM
29// If we find an existing value, replace it, otherwise, push it at the back of the list
30void ConfigCache::replace_or_push_back(ConfigValue *new_value)
31{
34a6a1d6
JM
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);
a2f7633f
JM
45}
46
47ConfigValue *ConfigCache::lookup(const uint16_t *check_sums) const
48{
34a6a1d6
JM
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;
a2f7633f
JM
55}
56
57void ConfigCache::collect(uint16_t family, uint16_t cs, vector<uint16_t> *list)
58{
34a6a1d6
JM
59 for( auto &kv : store ) {
60 if( kv->check_sums[2] == cs && kv->check_sums[0] == family ) {
a2f7633f 61 // We found a module enable for this family, add it's number
34a6a1d6 62 list->push_back(kv->check_sums[1]);
a2f7633f
JM
63 }
64 }
65}
66
67void ConfigCache::dump(StreamOutput *stream)
68{
d41a043b 69 int l = 1;
34a6a1d6
JM
70 for( auto &kv : store ) {
71 ConfigValue *v = kv;
a2f7633f 72 stream->printf("%3d - %04X %04X %04X : '%s' - found: %d, default: %d, default-double: %f, default-int: %d\n",
d41a043b 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 );
a2f7633f 74 }
8d54c34c 75}