diff --git a/src/app/CFGFileParser.cpp b/src/app/CFGFileParser.cpp index 1df9657..06a489f 100644 --- a/src/app/CFGFileParser.cpp +++ b/src/app/CFGFileParser.cpp @@ -20,7 +20,8 @@ void *CFGFileParser::parseFile() file.close(); return NULL; } - + + _state = INIT; while(file.available()) { readChar = (char)file.read(); @@ -166,5 +167,87 @@ void *CFGFileParser::parseFile() boolean CFGFileParser::save(void *data) { - return true; + if(data == NULL) + return false; + Dictionary *ref = (Dictionary *) data; + int truncateHere(0); + char readChar(0); + + File file = _sdCardManager.open(_resource, FILE_READWRITE); + + if(!file) + { + file.close(); + return false; + } + + file.seek(0); + _state = INIT; + + //We find out where to truncate the file + while(file.available()) + { + readChar = (char)file.read(); + switch(_state) + { + case INIT: + if(readChar == '#') + { + truncateHere++; + _state = COMMENT_SECTION; + } + else + _state = DONE; + break; + case COMMENT_SECTION: + truncateHere++; + if(readChar == '\n') _state = LINE_BREAK; + break; + case LINE_BREAK: + truncateHere++; + if(readChar == '#') + _state = COMMENT_SECTION; + else if(readChar == '\n') _state = DONE; + break; + } + } + + if(!file.truncate(truncateHere)) + { + file.close(); + return false; + } + + //Let's write the settings + for(int i = 0; i < ref->count(); i++) + { + CFGParameterValue *cfgPV = ref->get(i); + + if(cfgPV->isQuotedParameter()) + { + file.write('\''); + file.print(cfgPV->getParameter()); + file.write('\''); + } + else + { + file.print(cfgPV->getParameter()); + } + + file.print(F(" : ")); + + if(cfgPV->isQuotedValue()) + { + file.write('\''); + file.print(cfgPV->stringValue()); + file.println('\''); + } + else + { + file.println(cfgPV->stringValue()); + } + } + + file.close(); + return true; } diff --git a/src/app/CFGFileParser.h b/src/app/CFGFileParser.h index f939359..5067596 100644 --- a/src/app/CFGFileParser.h +++ b/src/app/CFGFileParser.h @@ -16,7 +16,7 @@ public: protected: private: //This part handles the _buff realloc - enum State {INIT, COMMENT_SECTION, LINE_BREAK, PARAM_SECTION, ERROR, OPENING_QUOTE, SEPARATION}; + enum State {INIT, COMMENT_SECTION, LINE_BREAK, PARAM_SECTION, ERROR, OPENING_QUOTE, SEPARATION, DONE}; enum Type {PARAMETER, VALUE}; State _state; Type _type;