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
Preferences.cpp
- Committer:
- ansond
- Date:
- 2014-03-20
- Revision:
- 120:edf33bd41e4f
- Parent:
- 118:c8a80adfe90d
- Child:
- 121:40bb95a10a0e
File content as of revision 120:edf33bd41e4f:
/* 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->initialize(); } // destructor Preferences::~Preferences() { } // initialize the preferences from the preferences file bool Preferences::initialize() { char buffer[PREFERENCE_NAME_LEN + PREFERENCE_NAME_LEN + 10]; bool success = true; // initialize the memory buffers and count for(int i=0;i<MAX_NUM_PREFERENCES;++i) { memset(this->m_names[i],0,PREFERENCE_NAME_LEN+1); memset(this->m_values[i],0,PREFERENCE_VALUE_LEN+1); } this->m_num_preferences = 0; // read and open the config file FILE *fp = fopen(PREFERENCES_FILE,"r"); if (fp != NULL) { // read in the first line memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_NAME_LEN + 10); int n = fscanf(fp,"%s",buffer); // loop and read each line while(n != EOF) { // replace the equals sign with a space for(int i=0;i<strlen(buffer);++i) if (buffer[i] == '=') buffer[i] = ' '; // parse sscanf(buffer,"%s%s",this->m_names[this->m_num_preferences],this->m_values[this->m_num_preferences]); // DEBUG this->logger()->log("Installed Preference: %s=%s",this->m_names[this->m_num_preferences],this->m_values[this->m_num_preferences]); // increment the tally ++this->m_num_preferences; // reset the buffer and read in another line memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_NAME_LEN + 10); n = fscanf(fp,"%s",buffer); } // close fclose(fp); // summary this->logger()->log("Installed %d preferences",this->m_num_preferences); } else { // unable to open the preferences file - not loaded this->logger()->log("Unable to open preferences file %s... Preferences not loaded.",PREFERENCES_FILE); success = false; } // return our status return success; } // HACK: fix up coords because IOC's POINT() macro does not like commas void Preferences::fixCoordsForIOC() { int index = this.indexOfPreference("coords"); if (index >= 0) { // remove the comma... for(int i=0;i<strlen(this->m_values[index]);++i) if (this->m_values[index][i] == ',') this->m_values[index][i] = ' '; } } // 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) { char *value = NULL; // clean the buffer memset(buffer,0,buffer_length); // get our index int index = this->indexOfPreference(name); if (index >= 0) { // fill with our value strncpy(buffer,this->m_values[index],this->min(buffer_length,strlen(this->m_values[index]))); value = buffer; } else { // fill with the default if (def_value != NULL) strncpy(buffer,def_value,this->min(buffer_length,strlen(def_value))); if (def_value != NULL) value = buffer; } // return the value return value; } // get the index of the named preference int Preferences::indexOfPreference(char *name) { bool found = false; int index = -1; // parameter check if (name != NULL && strlen(name) > 0) { // loop until we find the name.. then stop and record its index for(int i=0;i<this->m_num_preferences && !found;++i) { if (strcmp(name,this->m_names[i]) == 0) { found = true; index = i; } } } // return the index value return index; } // get our error handler ErrorHandler *Preferences::logger() { return this->m_error_handler; } // get our preference count int Preferences::numPreferences() { return this->m_num_preferences; } // min function int Preferences::min(int value1,int value2) { if (value1 < value2) return value1; return value2; }