enabled specifying numeric config values using all strtof capabilities
[clinton/Smoothieware.git] / src / libs / ConfigValue.h
CommitLineData
df27a6a3 1/*
c295905f
AW
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
c295905f
AW
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"
11f8ba4e 14#include "Pwm.h"
c295905f
AW
15
16
17#define error(...) (fprintf(stderr, __VA_ARGS__), exit(1))
18
19using namespace std;
20#include <vector>
21#include <string>
22#include <stdio.h>
23
24
25class ConfigValue{
26 public:
27 ConfigValue(){
28 this->found = false;
4464301d 29 this->default_set = false;
df27a6a3
MM
30 this->check_sums[0] = 0x0000;
31 this->check_sums[1] = 0x0000;
32 this->check_sums[2] = 0x0000;
c295905f
AW
33 };
34
35 ConfigValue* required(){
36 if( this->found == true ){
37 return this;
38 }else{
4464301d 39 error("could not find config setting, please see http://smoothieware.org/configuring-smoothie\r\n");
c295905f
AW
40 }
41 }
42
1ad23cd3 43 float as_number(){
c295905f
AW
44 if( this->found == false && this->default_set == true ){
45 return this->default_double;
46 }else{
7c6b4eac 47 char* endptr = NULL;
04211969 48 float result = strtof(remove_non_number(this->value).c_str(), &endptr);
7c6b4eac 49 if( endptr <= remove_non_number(this->value).c_str() ){
4464301d 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] );
c295905f 51 }
df27a6a3 52 return result;
c295905f 53 }
8bb1c877
MM
54 }
55
56 int as_int()
57 {
58 if( this->found == false && this->default_set == true ){
59 return this->default_int;
60 }else{
7c6b4eac
MM
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() ){
8bb1c877
MM
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 }
c295905f
AW
68 }
69
70 std::string as_string(){
2e760ab7 71 return this->value;
c295905f
AW
72 }
73
74 bool as_bool(){
75 if( this->found == false && this->default_set == true ){
76 return this->default_double;
77 }else{
2e760ab7 78 if( this->value.find_first_of("ty1") != string::npos ){
c295905f
AW
79 return true;
80 }else{
81 return false;
df27a6a3 82 }
c295905f
AW
83 }
84 }
85
8bb1c877
MM
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
1ad23cd3 94 ConfigValue* by_default(float val){
c295905f
AW
95 this->default_set = true;
96 this->default_double = val;
df27a6a3 97 return this;
c295905f
AW
98 }
99
100 ConfigValue* by_default(std::string val){
4464301d 101 if( this->found ){ return this; }
c295905f 102 this->default_set = true;
4464301d 103 this->value = val;
c295905f
AW
104 return this;
105 }
106
107 bool has_characters( string mask ){
df27a6a3 108 if( this->value.find_first_of(mask) != string::npos ){ return true; }else{ return false; }
c295905f
AW
109 }
110
111 bool is_inverted(){
112 return this->has_characters(string("!"));
113 }
114
8bb1c877 115 int default_int;
1ad23cd3 116 float default_double;
df27a6a3 117 uint16_t check_sums[3];
a39e1557 118 string value;
c295905f
AW
119 bool found;
120 bool default_set;
c295905f
AW
121};
122
123
124
125
126
127
128
129
130#endif