Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
Diff: Preferences.cpp
- Revision:
- 112:1fb53d4729af
- Child:
- 114:bd38ad417d6a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Preferences.cpp Tue Mar 18 21:46:48 2014 +0000 @@ -0,0 +1,100 @@ +/* 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" + + // default constructor + Preferences::Preferences(ErrorHandler *error_handler) { + this->m_error_handler = error_handler; + this->m_local_filesystem = new LocalFileSystem("local"); + this->m_config_file = new ConfigFile(); + this->initialize(); + } + + // destructor + Preferences::~Preferences() { + if (this->m_config_file != NULL) delete this->m_config_file; + if (this->m_local_filesystem != NULL) delete this->m_local_filesystem; + } + + // 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) 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) { + char *value = def_value; + if (this->m_config_file->getValue(name, &buffer[0], buffer_length)) value = buffer; + return value; + } + + // 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; + } \ No newline at end of file