- fixed the config loader to read long line without messing up the data.

This commit is contained in:
Christoph Oelckers 2020-02-02 11:49:36 +01:00
parent 4d7a43004c
commit c9d7f21bf1
2 changed files with 41 additions and 55 deletions

View file

@ -617,15 +617,15 @@ void FConfigFile::LoadConfigFile ()
// //
//==================================================================== //====================================================================
bool FConfigFile::ReadConfig (void *file) bool FConfigFile::ReadConfig (FileReader *file)
{ {
uint8_t readbuf[READBUFFERSIZE]; TArray<uint8_t> readbuf;
FConfigSection *section = NULL; FConfigSection *section = NULL;
ClearConfig (); ClearConfig ();
while (ReadLine ((char*)readbuf, READBUFFERSIZE, file) != NULL) while (ReadLine (readbuf, file) != NULL)
{ {
uint8_t *start = readbuf; uint8_t *start = readbuf.Data();
uint8_t *equalpt; uint8_t *equalpt;
uint8_t *endpt; uint8_t *endpt;
@ -639,14 +639,7 @@ bool FConfigFile::ReadConfig (void *file)
{ {
continue; continue;
} }
// Do not process tail of long line
const bool longline = (READBUFFERSIZE - 1) == strlen((char*)readbuf) && '\n' != readbuf[READBUFFERSIZE - 2];
if (longline)
{
endpt = start + READBUFFERSIZE - 2;
}
else
{
// Remove white space at end of line // Remove white space at end of line
endpt = start + strlen ((char*)start) - 1; endpt = start + strlen ((char*)start) - 1;
while (endpt > start && *endpt <= ' ') while (endpt > start && *endpt <= ' ')
@ -657,7 +650,6 @@ bool FConfigFile::ReadConfig (void *file)
endpt[1] = 0; endpt[1] = 0;
if (endpt <= start) if (endpt <= start)
continue; // Nothing here continue; // Nothing here
}
if (*start == '[') if (*start == '[')
{ // Section header { // Section header
@ -693,31 +685,6 @@ bool FConfigFile::ReadConfig (void *file)
{ {
ReadMultiLineValue (file, section, (char*)start, (char*)whiteprobe + 3); ReadMultiLineValue (file, section, (char*)start, (char*)whiteprobe + 3);
} }
else if (longline)
{
const FString key = (char*)start;
FString value = (char*)whiteprobe;
while (ReadLine ((char*)readbuf, READBUFFERSIZE, file) != NULL)
{
const size_t endpos = (0 == readbuf[0]) ? 0 : (strlen((char*)readbuf) - 1);
const bool endofline = '\n' == readbuf[endpos];
if (endofline)
{
readbuf[endpos] = 0;
}
value += (char*)readbuf;
if (endofline)
{
break;
}
}
NewConfigEntry (section, key.GetChars(), value.GetChars());
}
else else
{ {
NewConfigEntry (section, (char*)start, (char*)whiteprobe); NewConfigEntry (section, (char*)start, (char*)whiteprobe);
@ -742,18 +709,18 @@ bool FConfigFile::ReadConfig (void *file)
// //
//==================================================================== //====================================================================
FConfigFile::FConfigEntry *FConfigFile::ReadMultiLineValue(void *file, FConfigSection *section, const char *key, const char *endtag) FConfigFile::FConfigEntry *FConfigFile::ReadMultiLineValue(FileReader *file, FConfigSection *section, const char *key, const char *endtag)
{ {
char readbuf[READBUFFERSIZE]; TArray<uint8_t> readbuf;
FString value; FString value;
size_t endlen = strlen(endtag); size_t endlen = strlen(endtag);
// Keep on reading lines until we reach a line that matches >>>endtag // Keep on reading lines until we reach a line that matches >>>endtag
while (ReadLine(readbuf, READBUFFERSIZE, file) != NULL) while (ReadLine(readbuf, file) != NULL)
{ {
// Does the start of this line match the endtag? // Does the start of this line match the endtag?
if (readbuf[0] == '>' && readbuf[1] == '>' && readbuf[2] == '>' && if (readbuf[0] == '>' && readbuf[1] == '>' && readbuf[2] == '>' &&
strncmp(readbuf + 3, endtag, endlen) == 0) strncmp((char*)readbuf.Data() + 3, endtag, endlen) == 0)
{ // Is there nothing but line break characters after the match? { // Is there nothing but line break characters after the match?
size_t i; size_t i;
for (i = endlen + 3; readbuf[i] != '\0'; ++i) for (i = endlen + 3; readbuf[i] != '\0'; ++i)
@ -781,9 +748,28 @@ FConfigFile::FConfigEntry *FConfigFile::ReadMultiLineValue(void *file, FConfigSe
// //
//==================================================================== //====================================================================
char *FConfigFile::ReadLine (char *string, int n, void *file) const uint8_t *FConfigFile::ReadLine(TArray<uint8_t>& string, FileReader* file) const
{ {
return ((FileReader *)file)->Gets (string, n); uint8_t byte;
string.Clear();
while (file->Read(&byte, 1))
{
if (byte == 0)
{
break;
}
if (byte != '\r')
{
string.Push(byte);
if (byte == '\n')
{
break;
}
}
}
if (string.Size() == 0) return nullptr;
string.Push(0);
return string.Data();
} }
//==================================================================== //====================================================================

View file

@ -76,8 +76,8 @@ public:
protected: protected:
virtual void WriteCommentHeader (FileWriter *file) const; virtual void WriteCommentHeader (FileWriter *file) const;
virtual char *ReadLine (char *string, int n, void *file) const; uint8_t *ReadLine (TArray<uint8_t> &string, FileReader *file) const;
bool ReadConfig (void *file); bool ReadConfig (FileReader *file);
static const char *GenerateEndTag(const char *value); static const char *GenerateEndTag(const char *value);
void RenameSection(const char *oldname, const char *newname) const; void RenameSection(const char *oldname, const char *newname) const;
@ -113,7 +113,7 @@ private:
FConfigEntry *FindEntry (FConfigSection *section, const char *key) const; FConfigEntry *FindEntry (FConfigSection *section, const char *key) const;
FConfigSection *NewConfigSection (const char *name); FConfigSection *NewConfigSection (const char *name);
FConfigEntry *NewConfigEntry (FConfigSection *section, const char *key, const char *value); FConfigEntry *NewConfigEntry (FConfigSection *section, const char *key, const char *value);
FConfigEntry *ReadMultiLineValue (void *file, FConfigSection *section, const char *key, const char *terminator); FConfigEntry *ReadMultiLineValue (FileReader *file, FConfigSection *section, const char *key, const char *terminator);
void SetSectionNote (FConfigSection *section, const char *note); void SetSectionNote (FConfigSection *section, const char *note);
public: public: