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/>.
8 #include "libs/Kernel.h"
9 #include "ConfigValue.h"
10 #include "FileConfigSource.h"
11 #include "ConfigCache.h"
17 FileConfigSource::FileConfigSource(){
18 this->config_file_found
= false;
21 // Transfer all values found in the file to the passed cache
22 void FileConfigSource::transfer_values_to_cache( ConfigCache
* cache
){
24 // Default empty value
25 ConfigValue
* result
= new ConfigValue
;
27 if( this->has_config_file() == false ){return;}
28 // Open the config file ( find it if we haven't already found it )
29 FILE *lp
= fopen(this->get_config_file().c_str(), "r");
35 if (c
== '\n' || c
== EOF
){
37 if( buffer
[0] == '#' ){ buffer
.clear(); continue; } // Ignore comments
38 if( buffer
.length() < 3 ){ buffer
.clear(); continue; } //Ignore empty lines
39 size_t begin_key
= buffer
.find_first_not_of(" ");
40 size_t begin_value
= buffer
.find_first_not_of(" ", buffer
.find_first_of(" ", begin_key
));
41 string key
= buffer
.substr(begin_key
, buffer
.find_first_of(" ", begin_key
) - begin_key
).append(" ");
42 vector
<uint16_t> check_sums
;
45 while( begin_key
< key
.size() ){
46 size_t end_key
= key
.find_first_of(" .", begin_key
);
47 string key_node
= key
.substr(begin_key
, end_key
- begin_key
);
48 check_sums
.push_back(get_checksum(key_node
));
49 begin_key
= end_key
+ 1;
53 result
= new ConfigValue
;
55 result
->check_sums
= check_sums
;
56 result
->value
= buffer
.substr(begin_value
, buffer
.find_first_of("\r\n# ", begin_value
+1)-begin_value
);
58 // Append the newly found value to the cache we were passed
59 cache
->replace_or_push_back(result
);
70 // Return wether or not we have a readable config file
71 bool FileConfigSource::has_config_file(){
72 if( this->config_file_found
){ return true; }
73 this->try_config_file("/local/config");
74 this->try_config_file("/sd/config");
75 if( this->config_file_found
){
83 // Tool function for get_config_file
84 inline void FileConfigSource::try_config_file(string candidate
){
85 FILE *lp
= fopen(candidate
.c_str(), "r");
86 if(lp
){ this->config_file_found
= true; this->config_file
= candidate
; }
90 // Get the filename for the config file
91 string
FileConfigSource::get_config_file(){
92 if( this->config_file_found
){ return this->config_file
; }
93 if( this->has_config_file() ){
94 return this->config_file
;
96 printf("ERROR: no config file found\r\n");