First compiling version of the new Config abstraction, but untested atm,
[clinton/Smoothieware.git] / src / libs / ConfigSources / FileConfigSource.cpp
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 #include "libs/Kernel.h"
9 #include "ConfigValue.h"
10 #include "FileConfigSource.h"
11 #include "ConfigCache.h"
12
13 using namespace std;
14 #include <string>
15
16
17 FileConfigSource::FileConfigSource(){
18 this->config_file_found = false;
19 }
20
21 // Transfer all values found in the file to the passed cache
22 void FileConfigSource::transfer_values_to_cache( ConfigCache* cache ){
23
24 // Default empty value
25 ConfigValue* result = new ConfigValue;
26
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");
30 string buffer;
31 int c;
32 // For each line
33 do {
34 c = fgetc (lp);
35 if (c == '\n' || c == EOF){
36 // We have a new line
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;
43 begin_key = 0;
44 int j = 0;
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;
50 j++;
51 }
52
53 result = new ConfigValue;
54 result->found = true;
55 result->check_sums = check_sums;
56 result->value = buffer.substr(begin_value, buffer.find_first_of("\r\n# ", begin_value+1)-begin_value);
57
58 // Append the newly found value to the cache we were passed
59 cache->replace_or_push_back(result);
60
61 buffer.clear();
62 }else{
63 buffer += c;
64 }
65 } while (c != EOF);
66 fclose(lp);
67
68 }
69
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 ){
76 return true;
77 }else{
78 return false;
79 }
80
81 }
82
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; }
87 fclose(lp);
88 }
89
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;
95 }else{
96 printf("ERROR: no config file found\r\n");
97 }
98 }
99
100
101
102
103
104