Updated the code, the save feature needs to be tested carefully before using it
This commit is contained in:
parent
e9625b909c
commit
ef2f986581
@ -13,15 +13,16 @@ void *CFGFileParser::parseFile()
|
|||||||
CFGDictionary<CFGParameterValue> *dictioRef = new CFGDictionary<CFGParameterValue>;
|
CFGDictionary<CFGParameterValue> *dictioRef = new CFGDictionary<CFGParameterValue>;
|
||||||
char readChar(0), *parsedParameter(NULL), *parsedValue(NULL);
|
char readChar(0), *parsedParameter(NULL), *parsedValue(NULL);
|
||||||
|
|
||||||
|
if(!_sdCardManager.isMounted()) return NULL;
|
||||||
|
|
||||||
file = _sdCardManager.open(_resource);
|
file = _sdCardManager.open(_resource);
|
||||||
if(!file)
|
if(!file)
|
||||||
{
|
{
|
||||||
delete dictioRef;
|
delete dictioRef;
|
||||||
file.close();
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state = INIT;
|
_state = INIT;
|
||||||
while(file.available())
|
while(file.available())
|
||||||
{
|
{
|
||||||
readChar = (char)file.read();
|
readChar = (char)file.read();
|
||||||
@ -167,17 +168,18 @@ void *CFGFileParser::parseFile()
|
|||||||
|
|
||||||
boolean CFGFileParser::save(void *data)
|
boolean CFGFileParser::save(void *data)
|
||||||
{
|
{
|
||||||
if(data == NULL)
|
if(data == NULL) return false;
|
||||||
return false;
|
|
||||||
Dictionary<CFGParameterValue> *ref = (Dictionary<CFGParameterValue> *) data;
|
Dictionary<CFGParameterValue> *ref = (Dictionary<CFGParameterValue> *) data;
|
||||||
int truncateHere(0);
|
uint64_t truncateHere(0);
|
||||||
char readChar(0);
|
char readChar(0);
|
||||||
|
|
||||||
|
if(!_sdCardManager.isMounted()) return NULL;
|
||||||
|
|
||||||
File file = _sdCardManager.open(_resource, FILE_READWRITE);
|
File file = _sdCardManager.open(_resource, FILE_READWRITE);
|
||||||
|
|
||||||
if(!file)
|
if(!file)
|
||||||
{
|
{
|
||||||
file.close();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,65 +189,65 @@ boolean CFGFileParser::save(void *data)
|
|||||||
//We find out where to truncate the file
|
//We find out where to truncate the file
|
||||||
while(file.available())
|
while(file.available())
|
||||||
{
|
{
|
||||||
readChar = (char)file.read();
|
readChar = (char)file.read();
|
||||||
switch(_state)
|
switch(_state)
|
||||||
{
|
{
|
||||||
case INIT:
|
case INIT:
|
||||||
if(readChar == '#')
|
if(readChar == '#')
|
||||||
{
|
{
|
||||||
truncateHere++;
|
truncateHere++;
|
||||||
_state = COMMENT_SECTION;
|
_state = COMMENT_SECTION;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_state = DONE;
|
_state = DONE;
|
||||||
break;
|
break;
|
||||||
case COMMENT_SECTION:
|
case COMMENT_SECTION:
|
||||||
truncateHere++;
|
truncateHere++;
|
||||||
if(readChar == '\n') _state = LINE_BREAK;
|
if(readChar == '\n') _state = LINE_BREAK;
|
||||||
break;
|
break;
|
||||||
case LINE_BREAK:
|
case LINE_BREAK:
|
||||||
truncateHere++;
|
truncateHere++;
|
||||||
if(readChar == '#')
|
if(readChar == '#')
|
||||||
_state = COMMENT_SECTION;
|
_state = COMMENT_SECTION;
|
||||||
else if(readChar == '\n') _state = DONE;
|
else if(readChar == '\n') _state = DONE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!file.truncate(truncateHere))
|
if(!file.truncate(truncateHere))
|
||||||
{
|
{
|
||||||
file.close();
|
file.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Let's write the settings
|
//Let's write the settings
|
||||||
for(int i = 0; i < ref->count(); i++)
|
for(int i = 0; i < ref->count(); i++)
|
||||||
{
|
{
|
||||||
CFGParameterValue *cfgPV = ref->getAt(i);
|
CFGParameterValue *cfgPV = ref->getAt(i);
|
||||||
|
|
||||||
if(cfgPV->isQuotedParameter())
|
if(cfgPV->isQuotedParameter())
|
||||||
{
|
{
|
||||||
file.write('\'');
|
file.write('\'');
|
||||||
file.print(cfgPV->getParameter());
|
file.print(cfgPV->getParameter());
|
||||||
file.write('\'');
|
file.write('\'');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
file.print(cfgPV->getParameter());
|
file.print(cfgPV->getParameter());
|
||||||
}
|
}
|
||||||
|
|
||||||
file.print(F(" : "));
|
file.print(F(" : "));
|
||||||
|
|
||||||
if(cfgPV->isQuotedValue())
|
if(cfgPV->isQuotedValue())
|
||||||
{
|
{
|
||||||
file.write('\'');
|
file.write('\'');
|
||||||
file.print(cfgPV->stringValue());
|
file.print(cfgPV->stringValue());
|
||||||
file.println('\'');
|
file.println('\'');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
file.println(cfgPV->stringValue());
|
file.println(cfgPV->stringValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
|
@ -7,6 +7,9 @@
|
|||||||
#include "definition.h"
|
#include "definition.h"
|
||||||
#include "CFGParameterValue.h"
|
#include "CFGParameterValue.h"
|
||||||
|
|
||||||
|
//Forward class declaration because of cross includes...
|
||||||
|
class SDCardManager;
|
||||||
|
|
||||||
class CFGFileParser : public AbstractParser
|
class CFGFileParser : public AbstractParser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Loading…
Reference in New Issue
Block a user