moved util function
authorArthur Wolf <wolf.arthur@gmail.com>
Tue, 27 Sep 2011 13:39:53 +0000 (15:39 +0200)
committerArthur Wolf <wolf.arthur@gmail.com>
Tue, 27 Sep 2011 13:39:53 +0000 (15:39 +0200)
src/libs/Config.cpp
src/libs/Config.h
src/libs/utils.cpp [moved from src/utils.c with 77% similarity]
src/libs/utils.h
src/modules/utils/simpleshell/SimpleShell.cpp
src/modules/utils/simpleshell/SimpleShell.h

index 4ba6dac..81b4b48 100644 (file)
@@ -17,6 +17,25 @@ Config::Config(){
     config_file_found = false; 
 }
 
+void Config::on_module_loaded(){
+    this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
+}
+
+// When a new line is received, check if it is a command, and if it is, act upon it
+void Config::on_console_line_received( void* argument ){
+    string possible_command = *static_cast<string*>(argument);
+
+    // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
+    unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) );  // todo: put this method somewhere more convenient
+    this->kernel->serial->printf("checksum>%u\r\n", check_sum);   
+
+    // Act depending on command
+    switch( check_sum ){
+        //case config-get_checksum: this->    ; break; 
+    }
+}
+
+
 // Get a value from the configuration as a string
 // Because we don't like to waste space in Flash with lengthy config parameter names, we take a checksum instead so that the name does not have to be stored
 // See get_checksum
@@ -45,12 +64,12 @@ string Config::get_string(uint16_t check_sum){
     printf("ERROR: configuration key not found\r\n");
 }
 
+// Get a value from the file as a number
 double Config::get(uint16_t check_sum){
     return atof(this->get_string( check_sum ).c_str());
 }
 
-
-
+// Get the filename for the config file
 string Config::get_config_file(){
     if( this->config_file_found ){ return this->config_file; }
     this->try_config_file("/local/config");
@@ -62,7 +81,8 @@ string Config::get_config_file(){
     }
 }
 
-void Config::try_config_file(string candidate){
+// Tool function for get_config_file
+inline void Config::try_config_file(string candidate){
     FILE *lp = fopen(candidate.c_str(), "r");
     if(lp){ this->config_file_found = true; this->config_file = candidate; }
     fclose(lp);
index d628ab4..f75edc3 100644 (file)
 #include <string>
 using std::string;
 
+#define config_get_checksum        46310
+#define config_set_checksum        55538
+#define config_load_checksum       3143
+
 class Config : public Module {
     public:
         Config();
+
+        void on_module_loaded();
+        void on_console_line_received( void* argument );
         string get_string(uint16_t check_sum);
         double get(uint16_t check_sum);
         string get_config_file();
similarity index 77%
rename from src/utils.c
rename to src/libs/utils.cpp
index ada35d3..676e7be 100644 (file)
@@ -32,3 +32,13 @@ string shift_parameter( string &parameters ){
     parameters = parameters.substr(beginning+1, parameters.size());
     return temp;
 }
+
+// Separate command from arguments
+string get_arguments( string possible_command ){
+    size_t beginning = possible_command.find_first_of(" ");
+    if( beginning == string::npos ){ return ""; } 
+    return possible_command.substr( beginning+1, possible_command.size() - beginning);
+}
+
+
+
index 09fb47d..f80bfce 100644 (file)
@@ -11,6 +11,8 @@ uint16_t get_checksum(string to_check);
 
 string shift_parameter( string &parameters );
 
+string get_arguments( string possible_command );
+
 
 
 
index 096ee12..1dadd2d 100644 (file)
@@ -19,19 +19,12 @@ void SimpleShell::on_console_line_received( void* argument ){
     
     // Act depending on command
     switch( check_sum ){
-        case ls_command_checksum      : this->ls_command( this->get_arguments(possible_command)); break;
-        case cd_command_checksum      : this->cd_command( this->get_arguments(possible_command)); break;
-        case cat_command_checksum     : this->cat_command(this->get_arguments(possible_command)); break;
+        case ls_command_checksum      : this->ls_command( get_arguments(possible_command)); break;
+        case cd_command_checksum      : this->cd_command( get_arguments(possible_command)); break;
+        case cat_command_checksum     : this->cat_command(get_arguments(possible_command)); break;
     }
 }
 
-// Separate command from arguments
-string SimpleShell::get_arguments( string possible_command ){
-    size_t beginning = possible_command.find_first_of(" ");
-    if( beginning == string::npos ){ return ""; } 
-    return possible_command.substr( beginning+1, possible_command.size() - beginning);
-}
-
 // Convert a path indication ( absolute or relative ) into a path ( absolute )
 string SimpleShell::absolute_from_relative( string path ){
     if( path[0] == '/' ){ return path; }
index fa16776..c82ab1b 100644 (file)
@@ -18,7 +18,6 @@ class SimpleShell : public Module {
 
         void on_module_loaded();
         void on_console_line_received( void* argument );
-        string get_arguments( string possible_command );
         string absolute_from_relative( string path );
         void ls_command( string parameters );
         void cd_command( string parameters );