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"
21 FileConfigSource::FileConfigSource(string config_file
, uint16_t name_checksum
){
22 this->name_checksum
= name_checksum
;
23 this->config_file
= config_file
;
24 this->config_file_found
= false;
27 // Transfer all values found in the file to the passed cache
28 void FileConfigSource::transfer_values_to_cache( ConfigCache
* cache
){
30 if( this->has_config_file() == false ){return;}
31 // Open the config file ( find it if we haven't already found it )
32 FILE *lp
= fopen(this->get_config_file().c_str(), "r");
36 process_char_from_ascii_config(c
= fgetc(lp
), cache
);
41 // Return true if the check_sums match
42 bool FileConfigSource::is_named( uint16_t check_sum
){
43 return check_sum
== this->name_checksum
;
46 // Write a config setting to the file
47 void FileConfigSource::write( string setting
, string value
){
48 // Open the config file ( find it if we haven't already found it )
49 FILE *lp
= fopen(this->get_config_file().c_str(), "r+");
55 if (c
== '\n' || c
== EOF
){
57 if( buffer
[0] == '#' ){ buffer
.clear(); continue; } // Ignore comments
58 if( buffer
.length() < 3 ){ buffer
.clear(); continue; } //Ignore empty lines
59 size_t begin_key
= buffer
.find_first_not_of(" \t");
60 size_t begin_value
= buffer
.find_first_not_of(" \t", buffer
.find_first_of(" \t", begin_key
));
61 // If this line matches the checksum
62 string candidate
= buffer
.substr(begin_key
, buffer
.find_first_of(" \t", begin_key
) - begin_key
);
63 if( candidate
.compare(setting
) != 0 ){ buffer
.clear(); continue; }
64 int free_space
= int(int(buffer
.find_first_of("\r\n#", begin_value
+1))-begin_value
);
65 if( int(value
.length()) >= free_space
){
66 //THEKERNEL->streams->printf("ERROR: Not enough room for value\r\n");
71 for( int i
= value
.length(); i
< free_space
; i
++){ value
+= " "; }
74 int start
= pos
- buffer
.length() + begin_value
- 1;
75 fseek(lp
, start
, SEEK_SET
);
76 fputs(value
.c_str(), lp
);
84 //THEKERNEL->streams->printf("ERROR: configuration key not found\r\n");
87 // Return the value for a specific checksum
88 string
FileConfigSource::read( uint16_t check_sums
[3] ){
92 if( this->has_config_file() == false ){return value
;}
93 // Open the config file ( find it if we haven't already found it )
94 FILE *lp
= fopen(this->get_config_file().c_str(), "r");
99 process_char_from_ascii_config(c
, check_sums
);
106 // Return wether or not we have a readable config file
107 bool FileConfigSource::has_config_file(){
108 if( this->config_file_found
){ return true; }
109 this->try_config_file(this->config_file
);
110 if( this->config_file_found
){
118 // Tool function for get_config_file
119 inline void FileConfigSource::try_config_file(string candidate
){
120 if(file_exists(candidate
)){ this->config_file_found
= true; }
123 // Get the filename for the config file
124 string
FileConfigSource::get_config_file(){
125 if( this->config_file_found
){ return this->config_file
; }
126 if( this->has_config_file() ){
127 return this->config_file
;
129 printf("ERROR: no config file found\r\n");