Implemented the save method + fixed a hidden bug
This commit is contained in:
parent
fd5e921f55
commit
7598ca9100
@ -21,6 +21,7 @@ void *CFGFileParser::parseFile()
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_state = INIT;
|
||||
while(file.available())
|
||||
{
|
||||
readChar = (char)file.read();
|
||||
@ -166,5 +167,87 @@ void *CFGFileParser::parseFile()
|
||||
|
||||
boolean CFGFileParser::save(void *data)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user