Merge remote-tracking branch 'upstream/edge' into hack/interim-feed-hold
[clinton/Smoothieware.git] / src / libs / ConfigValue.cpp
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
10 #define printErrorandExit(...) THEKERNEL->streams->printf(__VA_ARGS__); // exit(1);
11
12 #include <vector>
13 #include <stdio.h>
14
15 ConfigValue::ConfigValue()
16 {
17 clear();
18 }
19
20 void ConfigValue:: clear()
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;
27 this->default_double= 0.0F;
28 this->default_int= 0;
29 this->value= "";
30 }
31
32 ConfigValue::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
39 ConfigValue::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
47 ConfigValue& 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;
56 }
57
58 ConfigValue *ConfigValue::required()
59 {
60 if( !this->found ) {
61 printErrorandExit("could not find config setting, please see http://smoothieware.org/configuring-smoothie\r\n");
62 }
63 return this;
64 }
65
66 float 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);
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] );
77 }
78 return result;
79 }
80 }
81
82 int 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);
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] );
93 }
94 return result;
95 }
96 }
97
98 std::string ConfigValue::as_string()
99 {
100 return this->value;
101 }
102
103 bool ConfigValue::as_bool()
104 {
105 if( this->found == false && this->default_set == true ) {
106 return this->default_int;
107 } else {
108 return this->value.find_first_of("ty1") != string::npos;
109 }
110 }
111
112 ConfigValue *ConfigValue::by_default(int val)
113 {
114 this->default_set = true;
115 this->default_int = val;
116 this->default_double = val; // we need to set both becuase sometimes an integer is passed when it should be a float
117 return this;
118 }
119
120 ConfigValue *ConfigValue::by_default(float val)
121 {
122 this->default_set = true;
123 this->default_double = val;
124 return this;
125 }
126
127 ConfigValue *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
137 bool ConfigValue::has_characters( const char *mask )
138 {
139 if( this->value.find_first_of(mask) != string::npos ) {
140 return true;
141 } else {
142 return false;
143 }
144 }
145
146 bool ConfigValue::is_inverted()
147 {
148 return this->has_characters("!");
149 }
150