Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ConfigSource.h Source File

ConfigSource.h

00001 /*
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       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.
00004       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.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
00006 */
00007 
00008 #ifndef CONFIGSOURCE_H
00009 #define CONFIGSOURCE_H
00010 
00011 using namespace std;
00012 #include <vector>
00013 #include <string>
00014 #include "ConfigValue.h"
00015 #include "ConfigCache.h"
00016 
00017 class ConfigValue;
00018 
00019 class ConfigSource {
00020     public:
00021         ConfigSource(){}
00022 
00023         // Read each value, and append it as a ConfigValue to the config_cache we were passed
00024         virtual void transfer_values_to_cache( ConfigCache* ) = 0;
00025         virtual bool is_named( uint16_t check_sum ) = 0;
00026         virtual void write( string setting, string value ) = 0;
00027         virtual string read( uint16_t check_sums[3] ) = 0;
00028 
00029         uint16_t name_checksum;
00030     protected:
00031         virtual string process_char_from_ascii_config(int c, ConfigCache* cache) {
00032             static string buffer;
00033             if (c == '\n' || c == EOF)
00034             {
00035                 // We have a new line
00036                 if( buffer[0] == '#' ){ buffer.clear(); return ""; } // Ignore comments
00037                 if( buffer.length() < 3 ){ buffer.clear(); return ""; } //Ignore empty lines
00038                 size_t begin_key = buffer.find_first_not_of(" ");
00039                 size_t begin_value = buffer.find_first_not_of(" ", buffer.find_first_of(" ", begin_key));
00040 
00041                 uint16_t check_sums[3];
00042                 get_checksums(check_sums, buffer.substr(begin_key,  buffer.find_first_of(" ", begin_key) - begin_key).append(" "));
00043 
00044                 ConfigValue* result = new ConfigValue;
00045 
00046                 result->found = true;
00047                 result->check_sums[0] = check_sums[0];
00048                 result->check_sums[1] = check_sums[1];
00049                 result->check_sums[2] = check_sums[2];
00050 
00051                 result->value = buffer.substr(begin_value, buffer.find_first_of("\r\n# ", begin_value+1)-begin_value);
00052                 // Append the newly found value to the cache we were passed
00053                 cache->replace_or_push_back(result);
00054 
00055                 buffer.clear();
00056 
00057                 return result->value;
00058             }
00059             else
00060                 buffer += c;
00061 
00062             return "";
00063         };
00064         virtual string process_char_from_ascii_config(int c, uint16_t line_checksums[3]) {
00065             static string buffer;
00066             string value;
00067             if (c == '\n' || c == EOF)
00068             {
00069                 // We have a new line
00070                 if( buffer[0] == '#' ){ buffer.clear(); return ""; } // Ignore comments
00071                 if( buffer.length() < 3 ){ buffer.clear(); return ""; } //Ignore empty lines
00072                 size_t begin_key = buffer.find_first_not_of(" ");
00073                 size_t begin_value = buffer.find_first_not_of(" ", buffer.find_first_of(" ", begin_key));
00074 
00075                 uint16_t check_sums[3];
00076                 get_checksums(check_sums, buffer.substr(begin_key,  buffer.find_first_of(" ", begin_key) - begin_key).append(" "));
00077 
00078                 if(check_sums[0] == line_checksums[0] && check_sums[1] == line_checksums[1] && check_sums[2] == line_checksums[2] ){
00079                     value = buffer.substr(begin_value, buffer.find_first_of("\r\n# ", begin_value+1)-begin_value);
00080                     buffer.clear();
00081                     return value;
00082                 }
00083 
00084                 buffer.clear();
00085             }
00086             else
00087                 buffer += c;
00088             return value;
00089         };
00090 };
00091 
00092 
00093 
00094 #endif