Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/ConfigSources/FileConfigSource.cpp@2:1df0b61d3b5a, 2014-02-28 (annotated)
- Committer:
- Michael J. Spencer
- Date:
- Fri Feb 28 18:52:52 2014 -0800
- Revision:
- 2:1df0b61d3b5a
- Parent:
- 0:31e91bb0ef3c
- Child:
- 3:f151d08d335c
Update to latest Smoothie.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Michael J. Spencer |
2:1df0b61d3b5a | 1 | /* |
scachat | 0:31e91bb0ef3c | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 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. |
Michael J. Spencer |
2:1df0b61d3b5a | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
scachat | 0:31e91bb0ef3c | 6 | */ |
scachat | 0:31e91bb0ef3c | 7 | |
scachat | 0:31e91bb0ef3c | 8 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 9 | #include "ConfigValue.h" |
scachat | 0:31e91bb0ef3c | 10 | #include "FileConfigSource.h" |
scachat | 0:31e91bb0ef3c | 11 | #include "ConfigCache.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 12 | #include <malloc.h> |
Michael J. Spencer |
2:1df0b61d3b5a | 13 | |
Michael J. Spencer |
2:1df0b61d3b5a | 14 | |
scachat | 0:31e91bb0ef3c | 15 | using namespace std; |
scachat | 0:31e91bb0ef3c | 16 | #include <string> |
scachat | 0:31e91bb0ef3c | 17 | |
Michael J. Spencer |
2:1df0b61d3b5a | 18 | |
scachat | 0:31e91bb0ef3c | 19 | FileConfigSource::FileConfigSource(string config_file, uint16_t name_checksum){ |
scachat | 0:31e91bb0ef3c | 20 | this->name_checksum = name_checksum; |
scachat | 0:31e91bb0ef3c | 21 | this->config_file = config_file; |
scachat | 0:31e91bb0ef3c | 22 | this->config_file_found = false; |
scachat | 0:31e91bb0ef3c | 23 | } |
scachat | 0:31e91bb0ef3c | 24 | |
scachat | 0:31e91bb0ef3c | 25 | // Transfer all values found in the file to the passed cache |
scachat | 0:31e91bb0ef3c | 26 | void FileConfigSource::transfer_values_to_cache( ConfigCache* cache ){ |
Michael J. Spencer |
2:1df0b61d3b5a | 27 | |
Michael J. Spencer |
2:1df0b61d3b5a | 28 | if( this->has_config_file() == false ){return;} |
Michael J. Spencer |
2:1df0b61d3b5a | 29 | // Open the config file ( find it if we haven't already found it ) |
scachat | 0:31e91bb0ef3c | 30 | FILE *lp = fopen(this->get_config_file().c_str(), "r"); |
Michael J. Spencer |
2:1df0b61d3b5a | 31 | int c; |
Michael J. Spencer |
2:1df0b61d3b5a | 32 | // For each line |
scachat | 0:31e91bb0ef3c | 33 | do { |
Michael J. Spencer |
2:1df0b61d3b5a | 34 | process_char_from_ascii_config(c = fgetc(lp), cache); |
Michael J. Spencer |
2:1df0b61d3b5a | 35 | } while (c != EOF); |
scachat | 0:31e91bb0ef3c | 36 | fclose(lp); |
scachat | 0:31e91bb0ef3c | 37 | } |
scachat | 0:31e91bb0ef3c | 38 | |
scachat | 0:31e91bb0ef3c | 39 | // Return true if the check_sums match |
scachat | 0:31e91bb0ef3c | 40 | bool FileConfigSource::is_named( uint16_t check_sum ){ |
scachat | 0:31e91bb0ef3c | 41 | return check_sum == this->name_checksum; |
scachat | 0:31e91bb0ef3c | 42 | } |
scachat | 0:31e91bb0ef3c | 43 | |
scachat | 0:31e91bb0ef3c | 44 | // Write a config setting to the file |
scachat | 0:31e91bb0ef3c | 45 | void FileConfigSource::write( string setting, string value ){ |
scachat | 0:31e91bb0ef3c | 46 | // Open the config file ( find it if we haven't already found it ) |
scachat | 0:31e91bb0ef3c | 47 | FILE *lp = fopen(this->get_config_file().c_str(), "r+"); |
scachat | 0:31e91bb0ef3c | 48 | string buffer; |
scachat | 0:31e91bb0ef3c | 49 | int c; |
scachat | 0:31e91bb0ef3c | 50 | // For each line |
scachat | 0:31e91bb0ef3c | 51 | do { |
scachat | 0:31e91bb0ef3c | 52 | c = fgetc (lp); |
scachat | 0:31e91bb0ef3c | 53 | if (c == '\n' || c == EOF){ |
scachat | 0:31e91bb0ef3c | 54 | // We have a new line |
scachat | 0:31e91bb0ef3c | 55 | if( buffer[0] == '#' ){ buffer.clear(); continue; } // Ignore comments |
scachat | 0:31e91bb0ef3c | 56 | if( buffer.length() < 3 ){ buffer.clear(); continue; } //Ignore empty lines |
Michael J. Spencer |
2:1df0b61d3b5a | 57 | size_t begin_key = buffer.find_first_not_of(" \t"); |
Michael J. Spencer |
2:1df0b61d3b5a | 58 | size_t begin_value = buffer.find_first_not_of(" \t", buffer.find_first_of(" \t", begin_key)); |
scachat | 0:31e91bb0ef3c | 59 | // If this line matches the checksum |
Michael J. Spencer |
2:1df0b61d3b5a | 60 | string candidate = buffer.substr(begin_key, buffer.find_first_of(" \t", begin_key) - begin_key); |
scachat | 0:31e91bb0ef3c | 61 | if( candidate.compare(setting) != 0 ){ buffer.clear(); continue; } |
scachat | 0:31e91bb0ef3c | 62 | int free_space = int(int(buffer.find_first_of("\r\n#", begin_value+1))-begin_value); |
scachat | 0:31e91bb0ef3c | 63 | if( int(value.length()) >= free_space ){ |
Michael J. Spencer |
2:1df0b61d3b5a | 64 | //THEKERNEL->streams->printf("ERROR: Not enough room for value\r\n"); |
scachat | 0:31e91bb0ef3c | 65 | fclose(lp); |
scachat | 0:31e91bb0ef3c | 66 | return; |
scachat | 0:31e91bb0ef3c | 67 | } |
scachat | 0:31e91bb0ef3c | 68 | // Update value |
scachat | 0:31e91bb0ef3c | 69 | for( int i = value.length(); i < free_space; i++){ value += " "; } |
scachat | 0:31e91bb0ef3c | 70 | fpos_t pos; |
scachat | 0:31e91bb0ef3c | 71 | fgetpos( lp, &pos ); |
scachat | 0:31e91bb0ef3c | 72 | int start = pos - buffer.length() + begin_value - 1; |
scachat | 0:31e91bb0ef3c | 73 | fseek(lp, start, SEEK_SET); |
scachat | 0:31e91bb0ef3c | 74 | fputs(value.c_str(), lp); |
scachat | 0:31e91bb0ef3c | 75 | fclose(lp); |
scachat | 0:31e91bb0ef3c | 76 | return; |
scachat | 0:31e91bb0ef3c | 77 | }else{ |
scachat | 0:31e91bb0ef3c | 78 | buffer += c; |
scachat | 0:31e91bb0ef3c | 79 | } |
scachat | 0:31e91bb0ef3c | 80 | } while (c != EOF); |
scachat | 0:31e91bb0ef3c | 81 | fclose(lp); |
Michael J. Spencer |
2:1df0b61d3b5a | 82 | //THEKERNEL->streams->printf("ERROR: configuration key not found\r\n"); |
scachat | 0:31e91bb0ef3c | 83 | } |
scachat | 0:31e91bb0ef3c | 84 | |
scachat | 0:31e91bb0ef3c | 85 | // Return the value for a specific checksum |
Michael J. Spencer |
2:1df0b61d3b5a | 86 | string FileConfigSource::read( uint16_t check_sums[3] ){ |
scachat | 0:31e91bb0ef3c | 87 | |
scachat | 0:31e91bb0ef3c | 88 | string value = ""; |
scachat | 0:31e91bb0ef3c | 89 | |
scachat | 0:31e91bb0ef3c | 90 | if( this->has_config_file() == false ){return value;} |
Michael J. Spencer |
2:1df0b61d3b5a | 91 | // Open the config file ( find it if we haven't already found it ) |
scachat | 0:31e91bb0ef3c | 92 | FILE *lp = fopen(this->get_config_file().c_str(), "r"); |
Michael J. Spencer |
2:1df0b61d3b5a | 93 | int c; |
Michael J. Spencer |
2:1df0b61d3b5a | 94 | // For each line |
scachat | 0:31e91bb0ef3c | 95 | do { |
scachat | 0:31e91bb0ef3c | 96 | c = fgetc (lp); |
Michael J. Spencer |
2:1df0b61d3b5a | 97 | process_char_from_ascii_config(c, check_sums); |
Michael J. Spencer |
2:1df0b61d3b5a | 98 | } while (c != EOF); |
scachat | 0:31e91bb0ef3c | 99 | fclose(lp); |
scachat | 0:31e91bb0ef3c | 100 | |
scachat | 0:31e91bb0ef3c | 101 | return value; |
scachat | 0:31e91bb0ef3c | 102 | } |
scachat | 0:31e91bb0ef3c | 103 | |
scachat | 0:31e91bb0ef3c | 104 | // Return wether or not we have a readable config file |
scachat | 0:31e91bb0ef3c | 105 | bool FileConfigSource::has_config_file(){ |
scachat | 0:31e91bb0ef3c | 106 | if( this->config_file_found ){ return true; } |
scachat | 0:31e91bb0ef3c | 107 | this->try_config_file(this->config_file); |
scachat | 0:31e91bb0ef3c | 108 | if( this->config_file_found ){ |
scachat | 0:31e91bb0ef3c | 109 | return true; |
scachat | 0:31e91bb0ef3c | 110 | }else{ |
scachat | 0:31e91bb0ef3c | 111 | return false; |
scachat | 0:31e91bb0ef3c | 112 | } |
scachat | 0:31e91bb0ef3c | 113 | |
scachat | 0:31e91bb0ef3c | 114 | } |
scachat | 0:31e91bb0ef3c | 115 | |
scachat | 0:31e91bb0ef3c | 116 | // Tool function for get_config_file |
scachat | 0:31e91bb0ef3c | 117 | inline void FileConfigSource::try_config_file(string candidate){ |
Michael J. Spencer |
2:1df0b61d3b5a | 118 | if(file_exists(candidate)){ this->config_file_found = true; } |
scachat | 0:31e91bb0ef3c | 119 | } |
scachat | 0:31e91bb0ef3c | 120 | |
scachat | 0:31e91bb0ef3c | 121 | // Get the filename for the config file |
scachat | 0:31e91bb0ef3c | 122 | string FileConfigSource::get_config_file(){ |
scachat | 0:31e91bb0ef3c | 123 | if( this->config_file_found ){ return this->config_file; } |
scachat | 0:31e91bb0ef3c | 124 | if( this->has_config_file() ){ |
scachat | 0:31e91bb0ef3c | 125 | return this->config_file; |
scachat | 0:31e91bb0ef3c | 126 | }else{ |
scachat | 0:31e91bb0ef3c | 127 | printf("ERROR: no config file found\r\n"); |
Michael J. Spencer |
2:1df0b61d3b5a | 128 | return ""; |
scachat | 0:31e91bb0ef3c | 129 | } |
scachat | 0:31e91bb0ef3c | 130 | } |
scachat | 0:31e91bb0ef3c | 131 | |
scachat | 0:31e91bb0ef3c | 132 | |
scachat | 0:31e91bb0ef3c | 133 | |
scachat | 0:31e91bb0ef3c | 134 | |
Michael J. Spencer |
2:1df0b61d3b5a | 135 | |
Michael J. Spencer |
2:1df0b61d3b5a | 136 |