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:
- 118:c8a80adfe90d
- Parent:
- 117:34f0d83dcabb
- Child:
- 120:edf33bd41e4f
--- a/Preferences.cpp Wed Mar 19 17:41:33 2014 +0000 +++ b/Preferences.cpp Wed Mar 19 21:36:48 2014 +0000 @@ -24,34 +24,60 @@ // 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() { + char buffer[PREFERENCE_NAME_LEN + PREFERENCE_NAME_LEN + 10]; 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; + // 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); } - else { - // success! - this->logger()->log("Preference File: %s read successfully",PREFERENCES_FILE); - success = true; - } + + // close + fclose(fp); + + // summary + this->logger()->log("Installed %d preferences",this->m_num_preferences); } else { - // no config file object - this->logger()->log("Unable to initialize preferences: %s. No instance.",PREFERENCES_FILE); + // unable to open the preferences file - not loaded + this->logger()->log("Unable to open preferences file %s... Preferences not loaded.",PREFERENCES_FILE); success = false; } @@ -82,19 +108,54 @@ // 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,buffer_length); - if (strlen(buffer) == 0 && def_value != NULL) strncpy(buffer,def_value,this->min(buffer_length,strlen(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 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_config_file->getCount(); } + int Preferences::numPreferences() { return this->m_num_preferences; } // min function int Preferences::min(int value1,int value2) {