Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

Preferences.cpp

Committer:
ansond
Date:
2014-03-19
Revision:
116:428281cb5066
Parent:
114:bd38ad417d6a
Child:
117:34f0d83dcabb

File content as of revision 116:428281cb5066:

/* Copyright C2013 Doug Anson, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files the "Software", to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "mbed.h"
 #include "Preferences.h"
 
 LocalFileSystem local("local");
 
 // default constructor
 Preferences::Preferences(ErrorHandler *error_handler) {
     this->m_error_handler = error_handler;
     this->m_config_file = new ConfigFile();
     this->initialize();
 }
 
 // destructor
 Preferences::~Preferences() {
     if (this->m_config_file != NULL) delete this->m_config_file;
 }
 
 // initialize the preferences from the preferences file
 bool Preferences::initialize() {
     bool success = true;
     
     // read from the config file
     if (this->m_config_file != NULL) {
         if (!this->m_config_file->read(PREFERENCES_FILE)) {
            this->logger()->log("Unable to initialize preferences: %s",PREFERENCES_FILE);
            success = false;
         }
         else {
             // success!
             this->logger()->log("Preference File: %s read successfully",PREFERENCES_FILE);
             success = true;
         }
     }
     else {
         // no config file object
         this->logger()->log("Unable to initialize preferences: %s. No instance.",PREFERENCES_FILE);
         success = false;
     }
     
     // return our status
     return success;
 }
 
 // integer preference with defaults
 int Preferences::getIntPreference(char *name,int def_value) {\
     int int_value = def_value;
     char buffer[PREFERENCE_VALUE_LEN+1];
     memset(buffer,0,PREFERENCE_VALUE_LEN+1);
     char *value = this->getPreference(name,buffer,PREFERENCE_VALUE_LEN,NULL);
     if (value != NULL && strlen(value) > 0) sscanf(buffer,"%d",&int_value);
     return int_value;
 }
  
 // boolean preference with defaults
 bool Preferences::getBooleanPreference(char *name,bool def_value) {
     bool bool_value = def_value;
     char buffer[PREFERENCE_VALUE_LEN+1];
     memset(buffer,0,PREFERENCE_VALUE_LEN+1);
     char *value = this->getPreference(name,buffer,PREFERENCE_VALUE_LEN,NULL);
     if (value != NULL && strcmp(value,"true") == 0) bool_value = true;
     if (value != NULL && strcmp(value,"false") == 0) bool_value = false;
     return bool_value;
 }
 
 // string preference with defaults
 char *Preferences::getPreference(char *name,char *buffer,int buffer_length,char *def_value) {
     if (this->m_config_file != NULL) {
         memset(buffer,0,buffer_length);
         this->m_config_file->getValue(name,&buffer[0],buffer_length);
         if (strlen(buffer) == 0 && def_value != NULL) strncpy(buffer,def_value,this->min(buffer_length,strlen(def_value)));
     }
     return buffer;
 }
 
 // get our error handler
 ErrorHandler *Preferences::logger() { return this->m_error_handler; }
 
 // get our preference count
 int Preferences::numPreferences() { return this->m_config_file->getCount(); }
 
 // min function
 int Preferences::min(int value1,int value2) {
    if (value1 < value2) return value1;
    return value2;
 }