Merge branch 'feature/e-endstop' into merge-abc-with-homing
[clinton/Smoothieware.git] / src / libs / ConfigValue.cpp
CommitLineData
ecd4acfb
JM
1#include "ConfigValue.h"
2#include "StreamOutputPool.h"
3
4#include "libs/Kernel.h"
5#include "libs/utils.h"
6#include "libs/Pin.h"
7#include "Pwm.h"
8
9
a2f7633f 10#define printErrorandExit(...) THEKERNEL->streams->printf(__VA_ARGS__); // exit(1);
ecd4acfb
JM
11
12#include <vector>
13#include <stdio.h>
14
15ConfigValue::ConfigValue()
d41a043b
JM
16{
17 clear();
18}
19
20void ConfigValue:: clear()
ecd4acfb
JM
21{
22 this->found = false;
23 this->default_set = false;
24 this->check_sums[0] = 0x0000;
25 this->check_sums[1] = 0x0000;
26 this->check_sums[2] = 0x0000;
a2f7633f
JM
27 this->default_double= 0.0F;
28 this->default_int= 0;
29 this->value= "";
30}
31
32ConfigValue::ConfigValue(uint16_t *cs) {
33 memcpy(this->check_sums, cs, sizeof(this->check_sums));
34 this->found = false;
35 this->default_set = false;
36 this->value= "";
37}
38
39ConfigValue::ConfigValue(const ConfigValue& to_copy)
40{
41 this->found = to_copy.found;
42 this->default_set = to_copy.default_set;
43 memcpy(this->check_sums, to_copy.check_sums, sizeof(this->check_sums));
44 this->value.assign(to_copy.value);
45}
46
47ConfigValue& ConfigValue::operator= (const ConfigValue& to_copy)
48{
49 if( this != &to_copy ){
50 this->found = to_copy.found;
51 this->default_set = to_copy.default_set;
52 memcpy(this->check_sums, to_copy.check_sums, sizeof(this->check_sums));
53 this->value.assign(to_copy.value);
54 }
55 return *this;
ecd4acfb
JM
56}
57
58ConfigValue *ConfigValue::required()
59{
a2f7633f 60 if( !this->found ) {
ecd4acfb
JM
61 printErrorandExit("could not find config setting, please see http://smoothieware.org/configuring-smoothie\r\n");
62 }
a2f7633f 63 return this;
ecd4acfb
JM
64}
65
66float ConfigValue::as_number()
67{
68 if( this->found == false && this->default_set == true ) {
69 return this->default_double;
70 } else {
71 char *endptr = NULL;
72 string str = remove_non_number(this->value);
a2f7633f
JM
73 const char *cp= str.c_str();
74 float result = strtof(cp, &endptr);
75 if( endptr <= cp ) {
76 printErrorandExit("config setting with value '%s' and checksums[%04X,%04X,%04X] is not a valid number, please see http://smoothieware.org/configuring-smoothie\r\n", this->value.c_str(), this->check_sums[0], this->check_sums[1], this->check_sums[2] );
ecd4acfb
JM
77 }
78 return result;
79 }
80}
81
82int ConfigValue::as_int()
83{
84 if( this->found == false && this->default_set == true ) {
85 return this->default_int;
86 } else {
87 char *endptr = NULL;
88 string str = remove_non_number(this->value);
a2f7633f
JM
89 const char *cp= str.c_str();
90 int result = strtol(cp, &endptr, 10);
91 if( endptr <= cp ) {
92 printErrorandExit("config setting with value '%s' and checksums[%04X,%04X,%04X] is not a valid int, please see http://smoothieware.org/configuring-smoothie\r\n", this->value.c_str(), this->check_sums[0], this->check_sums[1], this->check_sums[2] );
ecd4acfb
JM
93 }
94 return result;
95 }
96}
97
98std::string ConfigValue::as_string()
99{
100 return this->value;
101}
102
103bool ConfigValue::as_bool()
104{
105 if( this->found == false && this->default_set == true ) {
a2f7633f 106 return this->default_int;
ecd4acfb 107 } else {
a2f7633f 108 return this->value.find_first_of("ty1") != string::npos;
ecd4acfb
JM
109 }
110}
111
112ConfigValue *ConfigValue::by_default(int val)
113{
114 this->default_set = true;
115 this->default_int = val;
a2f7633f 116 this->default_double = val; // we need to set both becuase sometimes an integer is passed when it should be a float
ecd4acfb
JM
117 return this;
118}
119
120ConfigValue *ConfigValue::by_default(float val)
121{
122 this->default_set = true;
123 this->default_double = val;
124 return this;
125}
126
127ConfigValue *ConfigValue::by_default(string val)
128{
129 if( this->found ) {
130 return this;
131 }
132 this->default_set = true;
133 this->value = val;
134 return this;
135}
136
a2f7633f 137bool ConfigValue::has_characters( const char *mask )
ecd4acfb
JM
138{
139 if( this->value.find_first_of(mask) != string::npos ) {
140 return true;
141 } else {
142 return false;
143 }
144}
145
146bool ConfigValue::is_inverted()
147{
a2f7633f 148 return this->has_characters("!");
ecd4acfb
JM
149}
150