Merge branch 'edge' into button
[clinton/Smoothieware.git] / src / libs / ConfigValue.h
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 #ifndef CONFIGVALUE_H
9 #define CONFIGVALUE_H
10
11 #include "libs/Kernel.h"
12 #include "libs/utils.h"
13 #include "libs/Pin.h"
14 #include "Pwm.h"
15
16
17 #define error(...) (fprintf(stderr, __VA_ARGS__), exit(1))
18
19 using namespace std;
20 #include <vector>
21 #include <string>
22 #include <stdio.h>
23
24
25 class ConfigValue{
26 public:
27 ConfigValue(){
28 this->found = false;
29 this->default_set = false;
30 this->check_sums[0] = 0x0000;
31 this->check_sums[1] = 0x0000;
32 this->check_sums[2] = 0x0000;
33 };
34
35 ConfigValue* required(){
36 if( this->found == true ){
37 return this;
38 }else{
39 error("could not find config setting, please see http://smoothieware.org/configuring-smoothie\r\n");
40 }
41 }
42
43 double as_number(){
44 if( this->found == false && this->default_set == true ){
45 return this->default_double;
46 }else{
47 char* endptr = NULL;
48 double result = strtod(remove_non_number(this->value).c_str(), &endptr);
49 if( endptr <= remove_non_number(this->value).c_str() ){
50 error("config setting with value '%s' and checksums[%u,%u,%u] 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] );
51 }
52 return result;
53 }
54 }
55
56 int as_int()
57 {
58 if( this->found == false && this->default_set == true ){
59 return this->default_int;
60 }else{
61 char* endptr = NULL;
62 int result = strtol(remove_non_number(this->value).c_str(), &endptr, 10);
63 if( endptr <= remove_non_number(this->value).c_str() ){
64 error("config setting with value '%s' and checksums[%u,%u,%u] 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] );
65 }
66 return result;
67 }
68 }
69
70 std::string as_string(){
71 return this->value;
72 }
73
74 bool as_bool(){
75 if( this->found == false && this->default_set == true ){
76 return this->default_double;
77 }else{
78 if( this->value.find_first_of("ty1") != string::npos ){
79 return true;
80 }else{
81 return false;
82 }
83 }
84 }
85
86 ConfigValue* by_default(int val)
87 {
88 this->default_set = true;
89 this->default_int = val;
90 this->default_double = val;
91 return this;
92 }
93
94 ConfigValue* by_default(double val){
95 this->default_set = true;
96 this->default_double = val;
97 return this;
98 }
99
100 ConfigValue* by_default(std::string val){
101 if( this->found ){ return this; }
102 this->default_set = true;
103 this->value = val;
104 return this;
105 }
106
107 bool has_characters( string mask ){
108 if( this->value.find_first_of(mask) != string::npos ){ return true; }else{ return false; }
109 }
110
111 bool is_inverted(){
112 return this->has_characters(string("!"));
113 }
114
115 int default_int;
116 double default_double;
117 uint16_t check_sums[3];
118 string value;
119 bool found;
120 bool default_set;
121 };
122
123
124
125
126
127
128
129
130 #endif