Implemented the save method + fixed a hidden bug

This commit is contained in:
anschrammh 2019-04-13 13:30:31 +02:00
parent fd5e921f55
commit 7598ca9100
2 changed files with 86 additions and 3 deletions

View File

@ -20,7 +20,8 @@ void *CFGFileParser::parseFile()
file.close(); file.close();
return NULL; return NULL;
} }
_state = INIT;
while(file.available()) while(file.available())
{ {
readChar = (char)file.read(); readChar = (char)file.read();
@ -166,5 +167,87 @@ void *CFGFileParser::parseFile()
boolean CFGFileParser::save(void *data) boolean CFGFileParser::save(void *data)
{ {
return true; if(data == NULL)
return false;
Dictionary<CFGParameterValue> *ref = (Dictionary<CFGParameterValue> *) 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;
} }

View File

@ -16,7 +16,7 @@ public:
protected: protected:
private: private:
//This part handles the _buff realloc //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}; enum Type {PARAMETER, VALUE};
State _state; State _state;
Type _type; Type _type;