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:
- 154:6e60f310ab78
- Parent:
- 153:8b32ee1ed6d6
- Child:
- 155:582462821bd7
--- a/Preferences.cpp Wed Apr 02 18:15:49 2014 +0000 +++ b/Preferences.cpp Thu Apr 03 00:50:36 2014 +0000 @@ -25,7 +25,10 @@ // default constructor Preferences::Preferences(ErrorHandler *error_handler) : BaseClass(error_handler,NULL) { + this->m_num_preferences = 0; +#ifndef _ENDPOINT_UBLOX_PLATFORM this->initialize(); +#endif } // destructor @@ -36,27 +39,27 @@ bool Preferences::initialize() { char name[PREFERENCE_NAME_LEN+1]; char value[PREFERENCE_VALUE_LEN+1]; - char buffer[PREFERENCE_NAME_LEN + PREFERENCE_VALUE_LEN + 100]; + char buffer[PREFERENCE_NAME_LEN + PREFERENCE_VALUE_LEN + 10]; bool success = true; - - // initialize + + // initialize the memory buffers and count + for(int i=0;i<MAX_NUM_PREFERENCES;++i) { + this->logger()->log("DOUG 1: index=%d",i); + memset(this->m_preferences[i].name,0,PREFERENCE_NAME_LEN+1); + this->logger()->log("DOUG 2: index=%d",i); + memset(this->m_preferences[i].value,0,PREFERENCE_VALUE_LEN+1); + } + + // initialize temp buffers memset(name,0,PREFERENCE_NAME_LEN+1); memset(value,0,PREFERENCE_VALUE_LEN+1); - memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_VALUE_LEN + 100); - - // 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; + memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_VALUE_LEN + 10); - #ifndef _ENDPOINT_UBLOX_PLATFORM // 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_VALUE_LEN + 100); + memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_VALUE_LEN + 10); int n = fscanf(fp,"%s",buffer); // loop and read each line @@ -68,17 +71,17 @@ memset(name,0,PREFERENCE_NAME_LEN+1); memset(value,0,PREFERENCE_VALUE_LEN+1); sscanf(buffer,"%s%s",name,value); - strcpy(this->m_names[this->m_num_preferences],name); - strcpy(this->m_values[this->m_num_preferences],value); + strcpy(this->m_preferences[this->m_num_preferences].name,name); + strcpy(this->m_preferences[this->m_num_preferences].value,value); // DEBUG - this->logger()->log("Installed Preference: %s=%s",this->m_names[this->m_num_preferences],this->m_values[this->m_num_preferences]); + this->logger()->log("Installed Preference: %s=%s",this->m_preferences[this->m_num_preferences].name,this->m_preferences[this->m_num_preferences].value); // increment the tally ++this->m_num_preferences; // reset the buffer and read in another line - memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_VALUE_LEN + 100); + memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_VALUE_LEN + 10); n = fscanf(fp,"%s",buffer); } @@ -93,9 +96,6 @@ this->logger()->log("Unable to open preferences file %s... Preferences not loaded.",PREFERENCES_FILE); success = false; } -#else - success = true; -#endif // return our status return success; @@ -106,7 +106,7 @@ 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] = ' '; + for(int i=0;i<strlen(this->m_preferences[index].value);++i) if (this->m_preferences[index].value[i] == ',') this->m_preferences[index].value[i] = ' '; } } @@ -145,7 +145,7 @@ 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]))); + strncpy(buffer,this->m_preferences[index].value,this->min(buffer_length,strlen(this->m_preferences[index].value))); value = buffer; //this->logger()->log("Preference GET: %s=%s",name,value); } @@ -177,7 +177,7 @@ // 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) { + if (strcmp(name,this->m_preferences[i].name) == 0) { found = true; index = i; } @@ -189,10 +189,4 @@ } // 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; - } \ No newline at end of file + int Preferences::numPreferences() { return this->m_num_preferences; } \ No newline at end of file