1 #include "ConfigValue.h"
2 #include "StreamOutputPool.h"
4 #include "libs/Kernel.h"
5 #include "libs/utils.h"
10 #define printErrorandExit(...) THEKERNEL->streams->printf(__VA_ARGS__); // exit(1);
15 ConfigValue::ConfigValue()
20 void ConfigValue:: clear()
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
;
32 ConfigValue::ConfigValue(uint16_t *cs
) {
33 memcpy(this->check_sums
, cs
, sizeof(this->check_sums
));
35 this->default_set
= false;
39 ConfigValue::ConfigValue(const ConfigValue
& to_copy
)
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
);
47 ConfigValue
& ConfigValue::operator= (const ConfigValue
& to_copy
)
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
);
58 ConfigValue
*ConfigValue::required()
61 printErrorandExit("could not find config setting, please see http://smoothieware.org/configuring-smoothie\r\n");
66 float ConfigValue::as_number()
68 if( this->found
== false && this->default_set
== true ) {
69 return this->default_double
;
72 string str
= remove_non_number(this->value
);
73 const char *cp
= str
.c_str();
74 float result
= strtof(cp
, &endptr
);
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] );
82 int ConfigValue::as_int()
84 if( this->found
== false && this->default_set
== true ) {
85 return this->default_int
;
88 string str
= remove_non_number(this->value
);
89 const char *cp
= str
.c_str();
90 int result
= strtol(cp
, &endptr
, 10);
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] );
98 std::string
ConfigValue::as_string()
103 bool ConfigValue::as_bool()
105 if( this->found
== false && this->default_set
== true ) {
106 return this->default_int
;
108 return this->value
.find_first_of("ty1") != string::npos
;
112 ConfigValue
*ConfigValue::by_default(int val
)
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
120 ConfigValue
*ConfigValue::by_default(float val
)
122 this->default_set
= true;
123 this->default_double
= val
;
127 ConfigValue
*ConfigValue::by_default(string val
)
132 this->default_set
= true;
137 bool ConfigValue::has_characters( const char *mask
)
139 if( this->value
.find_first_of(mask
) != string::npos
) {
146 bool ConfigValue::is_inverted()
148 return this->has_characters("!");