Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
Diff: libs/ConfigSources/FileConfigSource.cpp
- Revision:
- 2:1df0b61d3b5a
- Parent:
- 0:31e91bb0ef3c
- Child:
- 3:f151d08d335c
--- a/libs/ConfigSources/FileConfigSource.cpp Sat Mar 01 02:37:29 2014 +0000 +++ b/libs/ConfigSources/FileConfigSource.cpp Fri Feb 28 18:52:52 2014 -0800 @@ -1,17 +1,21 @@ -/* +/* This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). 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. 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. - You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. + You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. */ #include "libs/Kernel.h" #include "ConfigValue.h" #include "FileConfigSource.h" #include "ConfigCache.h" +#include <malloc.h> + + using namespace std; #include <string> + FileConfigSource::FileConfigSource(string config_file, uint16_t name_checksum){ this->name_checksum = name_checksum; this->config_file = config_file; @@ -20,42 +24,16 @@ // Transfer all values found in the file to the passed cache void FileConfigSource::transfer_values_to_cache( ConfigCache* cache ){ - // Default empty value - ConfigValue* result = new ConfigValue; - if( this->has_config_file() == false ){ - return; - } - // Open the config file ( find it if we haven't already found it ) + + if( this->has_config_file() == false ){return;} + // Open the config file ( find it if we haven't already found it ) FILE *lp = fopen(this->get_config_file().c_str(), "r"); - string buffer; - int c; - // For each line + int c; + // For each line do { - c = fgetc (lp); - if (c == '\n' || c == EOF){ - // We have a new line - if( buffer[0] == '#' ){ buffer.clear(); continue; } // Ignore comments - if( buffer.length() < 3 ){ buffer.clear(); continue; } //Ignore empty lines - size_t begin_key = buffer.find_first_not_of(" "); - size_t begin_value = buffer.find_first_not_of(" ", buffer.find_first_of(" ", begin_key)); - string key = buffer.substr(begin_key, buffer.find_first_of(" ", begin_key) - begin_key).append(" "); - vector<uint16_t> check_sums = get_checksums(key); - - result = new ConfigValue; - result->found = true; - result->check_sums = check_sums; - result->value = buffer.substr(begin_value, buffer.find_first_of("\r\n# ", begin_value+1)-begin_value); - - // Append the newly found value to the cache we were passed - cache->replace_or_push_back(result); - - buffer.clear(); - }else{ - buffer += c; - } - } while (c != EOF); + process_char_from_ascii_config(c = fgetc(lp), cache); + } while (c != EOF); fclose(lp); - } // Return true if the check_sums match @@ -65,7 +43,6 @@ // Write a config setting to the file void FileConfigSource::write( string setting, string value ){ -/* // Open the config file ( find it if we haven't already found it ) FILE *lp = fopen(this->get_config_file().c_str(), "r+"); string buffer; @@ -77,14 +54,14 @@ // We have a new line if( buffer[0] == '#' ){ buffer.clear(); continue; } // Ignore comments if( buffer.length() < 3 ){ buffer.clear(); continue; } //Ignore empty lines - size_t begin_key = buffer.find_first_not_of(" "); - size_t begin_value = buffer.find_first_not_of(" ", buffer.find_first_of(" ", begin_key)); + size_t begin_key = buffer.find_first_not_of(" \t"); + size_t begin_value = buffer.find_first_not_of(" \t", buffer.find_first_of(" \t", begin_key)); // If this line matches the checksum - string candidate = buffer.substr(begin_key, buffer.find_first_of(" ", begin_key) - begin_key); + string candidate = buffer.substr(begin_key, buffer.find_first_of(" \t", begin_key) - begin_key); if( candidate.compare(setting) != 0 ){ buffer.clear(); continue; } int free_space = int(int(buffer.find_first_of("\r\n#", begin_value+1))-begin_value); if( int(value.length()) >= free_space ){ - //this->kernel->serial->printf("ERROR: Not enough room for value\r\n"); + //THEKERNEL->streams->printf("ERROR: Not enough room for value\r\n"); fclose(lp); return; } @@ -102,42 +79,23 @@ } } while (c != EOF); fclose(lp); - //this->kernel->serial->printf("ERROR: configuration key not found\r\n"); - */ + //THEKERNEL->streams->printf("ERROR: configuration key not found\r\n"); } // Return the value for a specific checksum -string FileConfigSource::read( vector<uint16_t> check_sums ){ +string FileConfigSource::read( uint16_t check_sums[3] ){ string value = ""; if( this->has_config_file() == false ){return value;} - // Open the config file ( find it if we haven't already found it ) + // Open the config file ( find it if we haven't already found it ) FILE *lp = fopen(this->get_config_file().c_str(), "r"); - string buffer; - int c; - // For each line + int c; + // For each line do { c = fgetc (lp); - if (c == '\n' || c == EOF){ - // We have a new line - if( buffer[0] == '#' ){ buffer.clear(); continue; } // Ignore comments - if( buffer.length() < 3 ){ buffer.clear(); continue; } //Ignore empty lines - size_t begin_key = buffer.find_first_not_of(" "); - size_t begin_value = buffer.find_first_not_of(" ", buffer.find_first_of(" ", begin_key)); - string key = buffer.substr(begin_key, buffer.find_first_of(" ", begin_key) - begin_key).append(" "); - vector<uint16_t> line_checksums = get_checksums(key); - - if(check_sums == line_checksums){ - value = buffer.substr(begin_value, buffer.find_first_of("\r\n# ", begin_value+1)-begin_value); - break; - } - - buffer.clear(); - }else{ - buffer += c; - } - } while (c != EOF); + process_char_from_ascii_config(c, check_sums); + } while (c != EOF); fclose(lp); return value; @@ -157,9 +115,7 @@ // Tool function for get_config_file inline void FileConfigSource::try_config_file(string candidate){ - if(file_exists(candidate)){ - this->config_file_found = true; - } + if(file_exists(candidate)){ this->config_file_found = true; } } // Get the filename for the config file @@ -169,9 +125,12 @@ return this->config_file; }else{ printf("ERROR: no config file found\r\n"); + return ""; } } + +