2006-02-24 04:48:15 +00:00
|
|
|
/*
|
|
|
|
** configfile.cpp
|
|
|
|
** Implements the basic .ini parsing class
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
2008-12-07 00:50:04 +00:00
|
|
|
** Copyright 1998-2008 Randy Heit
|
2006-02-24 04:48:15 +00:00
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2008-01-08 01:08:27 +00:00
|
|
|
#include <ctype.h>
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
#include "doomtype.h"
|
|
|
|
#include "configfile.h"
|
2013-06-04 02:06:40 +00:00
|
|
|
#include "m_random.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
#define READBUFFERSIZE 256
|
|
|
|
|
2013-06-04 02:06:40 +00:00
|
|
|
static FRandom pr_endtag;
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile Constructor
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2015-02-07 15:35:23 +00:00
|
|
|
FConfigFile::FConfigFile () : PathName(NAME_None)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
Sections = CurrentSection = NULL;
|
|
|
|
LastSectionPtr = &Sections;
|
|
|
|
CurrentEntry = NULL;
|
2012-05-15 21:47:15 +00:00
|
|
|
OkayToWrite = true;
|
2012-07-06 02:22:58 +00:00
|
|
|
FileExisted = true;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile Constructor
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile::FConfigFile (const char *pathname,
|
|
|
|
void (*nosechandler)(const char *pathname, FConfigFile *config, void *userdata),
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
Sections = CurrentSection = NULL;
|
|
|
|
LastSectionPtr = &Sections;
|
|
|
|
CurrentEntry = NULL;
|
|
|
|
ChangePathName (pathname);
|
|
|
|
LoadConfigFile (nosechandler, userdata);
|
2012-05-15 21:47:15 +00:00
|
|
|
OkayToWrite = true;
|
2012-07-06 02:22:58 +00:00
|
|
|
FileExisted = true;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile Copy Constructor
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile::FConfigFile (const FConfigFile &other)
|
|
|
|
{
|
|
|
|
Sections = CurrentSection = NULL;
|
|
|
|
LastSectionPtr = &Sections;
|
|
|
|
CurrentEntry = NULL;
|
|
|
|
ChangePathName (other.PathName);
|
|
|
|
*this = other;
|
2012-05-15 21:47:15 +00:00
|
|
|
OkayToWrite = other.OkayToWrite;
|
2012-07-06 02:22:58 +00:00
|
|
|
FileExisted = other.FileExisted;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile Destructor
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile::~FConfigFile ()
|
|
|
|
{
|
|
|
|
FConfigSection *section = Sections;
|
|
|
|
|
|
|
|
while (section != NULL)
|
|
|
|
{
|
|
|
|
FConfigSection *nextsection = section->Next;
|
|
|
|
FConfigEntry *entry = section->RootEntry;
|
|
|
|
|
|
|
|
while (entry != NULL)
|
|
|
|
{
|
|
|
|
FConfigEntry *nextentry = entry->Next;
|
2006-05-06 23:43:44 +00:00
|
|
|
delete[] entry->Value;
|
2006-02-24 04:48:15 +00:00
|
|
|
delete[] (char *)entry;
|
|
|
|
entry = nextentry;
|
|
|
|
}
|
2008-12-07 00:50:04 +00:00
|
|
|
section->~FConfigSection();
|
2006-02-24 04:48:15 +00:00
|
|
|
delete[] (char *)section;
|
|
|
|
section = nextsection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile Copy Operator
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile &FConfigFile::operator = (const FConfigFile &other)
|
|
|
|
{
|
|
|
|
FConfigSection *fromsection, *tosection;
|
|
|
|
FConfigEntry *fromentry;
|
|
|
|
|
|
|
|
ClearConfig ();
|
|
|
|
fromsection = other.Sections;
|
|
|
|
while (fromsection != NULL)
|
|
|
|
{
|
|
|
|
fromentry = fromsection->RootEntry;
|
|
|
|
tosection = NewConfigSection (fromsection->Name);
|
|
|
|
while (fromentry != NULL)
|
|
|
|
{
|
|
|
|
NewConfigEntry (tosection, fromentry->Key, fromentry->Value);
|
|
|
|
fromentry = fromentry->Next;
|
|
|
|
}
|
|
|
|
fromsection = fromsection->Next;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: ClearConfig
|
|
|
|
//
|
|
|
|
// Removes all sections and entries from the config file.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::ClearConfig ()
|
|
|
|
{
|
|
|
|
CurrentSection = Sections;
|
|
|
|
while (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
FConfigSection *next = CurrentSection->Next;
|
|
|
|
ClearCurrentSection ();
|
|
|
|
delete CurrentSection;
|
|
|
|
CurrentSection = next;
|
|
|
|
}
|
|
|
|
Sections = NULL;
|
|
|
|
LastSectionPtr = &Sections;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: ChangePathName
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
void FConfigFile::ChangePathName (const char *pathname)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
PathName = pathname;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: CreateSectionAtStart
|
|
|
|
//
|
|
|
|
// Creates the section at the start of the file if it does not exist.
|
|
|
|
// Otherwise, simply moves the section to the start of the file.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
void FConfigFile::CreateSectionAtStart (const char *name)
|
|
|
|
{
|
|
|
|
NewConfigSection (name);
|
|
|
|
MoveSectionToStart (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: MoveSectionToStart
|
|
|
|
//
|
|
|
|
// Moves the named section to the start of the file if it exists.
|
|
|
|
// Otherwise, does nothing.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
void FConfigFile::MoveSectionToStart (const char *name)
|
|
|
|
{
|
|
|
|
FConfigSection *section = FindSection (name);
|
|
|
|
|
|
|
|
if (section != NULL)
|
|
|
|
{
|
|
|
|
FConfigSection **prevsec = &Sections;
|
|
|
|
while (*prevsec != NULL && *prevsec != section)
|
|
|
|
{
|
|
|
|
prevsec = &((*prevsec)->Next);
|
|
|
|
}
|
|
|
|
*prevsec = section->Next;
|
|
|
|
section->Next = Sections;
|
|
|
|
Sections = section;
|
|
|
|
if (LastSectionPtr == §ion->Next)
|
|
|
|
{
|
|
|
|
LastSectionPtr = prevsec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: SetSection
|
|
|
|
//
|
|
|
|
// Sets the current section to the named one, optionally creating it
|
|
|
|
// if it does not exist. Returns true if the section exists (even if
|
|
|
|
// it was newly created), false otherwise.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
bool FConfigFile::SetSection (const char *name, bool allowCreate)
|
|
|
|
{
|
|
|
|
FConfigSection *section = FindSection (name);
|
|
|
|
if (section == NULL && allowCreate)
|
|
|
|
{
|
|
|
|
section = NewConfigSection (name);
|
|
|
|
}
|
|
|
|
if (section != NULL)
|
|
|
|
{
|
|
|
|
CurrentSection = section;
|
|
|
|
CurrentEntry = section->RootEntry;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: SetFirstSection
|
|
|
|
//
|
2009-02-20 00:53:25 +00:00
|
|
|
// Sets the current section to the first one in the file. Returns
|
|
|
|
// false if there are no sections.
|
2008-12-07 00:50:04 +00:00
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
bool FConfigFile::SetFirstSection ()
|
|
|
|
{
|
|
|
|
CurrentSection = Sections;
|
|
|
|
if (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
CurrentEntry = CurrentSection->RootEntry;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: SetNextSection
|
|
|
|
//
|
2009-02-20 00:53:25 +00:00
|
|
|
// Advances the current section to the next one in the file. Returns
|
|
|
|
// false if there are no more sections.
|
2008-12-07 00:50:04 +00:00
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
bool FConfigFile::SetNextSection ()
|
|
|
|
{
|
|
|
|
if (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
CurrentSection = CurrentSection->Next;
|
|
|
|
if (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
CurrentEntry = CurrentSection->RootEntry;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: GetCurrentSection
|
|
|
|
//
|
|
|
|
// Returns the name of the current section.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
const char *FConfigFile::GetCurrentSection () const
|
|
|
|
{
|
|
|
|
if (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
return CurrentSection->Name;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: ClearCurrentSection
|
|
|
|
//
|
|
|
|
// Removes all entries from the current section.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::ClearCurrentSection ()
|
|
|
|
{
|
|
|
|
if (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
FConfigEntry *entry, *next;
|
|
|
|
|
|
|
|
entry = CurrentSection->RootEntry;
|
|
|
|
while (entry != NULL)
|
|
|
|
{
|
|
|
|
next = entry->Next;
|
|
|
|
delete[] entry->Value;
|
2006-05-10 03:08:27 +00:00
|
|
|
delete[] (char *)entry;
|
2006-02-24 04:48:15 +00:00
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
CurrentSection->RootEntry = NULL;
|
|
|
|
CurrentSection->LastEntryPtr = &CurrentSection->RootEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-20 00:53:25 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: DeleteCurrentSection
|
|
|
|
//
|
|
|
|
// Completely removes the current section. The current section is
|
|
|
|
// advanced to the next section. Returns true if there is still a
|
|
|
|
// current section.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
bool FConfigFile::DeleteCurrentSection()
|
|
|
|
{
|
|
|
|
if (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
FConfigSection *sec;
|
|
|
|
|
|
|
|
ClearCurrentSection();
|
|
|
|
|
|
|
|
// Find the preceding section.
|
|
|
|
for (sec = Sections; sec != NULL && sec->Next != CurrentSection; sec = sec->Next)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
sec->Next = CurrentSection->Next;
|
|
|
|
if (LastSectionPtr == &CurrentSection->Next)
|
|
|
|
{
|
|
|
|
LastSectionPtr = &sec->Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentSection->~FConfigSection();
|
|
|
|
delete[] (char *)CurrentSection;
|
|
|
|
|
|
|
|
CurrentSection = sec->Next;
|
|
|
|
return CurrentSection != NULL;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-07 01:14:36 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: ClearKey
|
|
|
|
//
|
|
|
|
// Removes a key from the current section, if found. If there are
|
|
|
|
// duplicates, only the first is removed.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
void FConfigFile::ClearKey(const char *key)
|
|
|
|
{
|
|
|
|
if (CurrentSection->RootEntry == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FConfigEntry **prober = &CurrentSection->RootEntry, *probe = *prober;
|
|
|
|
|
|
|
|
while (probe != NULL && stricmp(probe->Key, key) != 0)
|
|
|
|
{
|
|
|
|
prober = &probe->Next;
|
|
|
|
probe = *prober;
|
|
|
|
}
|
|
|
|
if (probe != NULL)
|
|
|
|
{
|
|
|
|
*prober = probe->Next;
|
|
|
|
if (CurrentSection->LastEntryPtr == &probe->Next)
|
|
|
|
{
|
|
|
|
CurrentSection->LastEntryPtr = prober;
|
|
|
|
}
|
|
|
|
delete[] probe->Value;
|
|
|
|
delete[] (char *)probe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: SectionIsEmpty
|
|
|
|
//
|
|
|
|
// Returns true if the current section has no entries. If there is
|
|
|
|
// no current section, it is also considered empty.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
bool FConfigFile::SectionIsEmpty()
|
|
|
|
{
|
|
|
|
return (CurrentSection == NULL) || (CurrentSection->RootEntry == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: NextInSection
|
|
|
|
//
|
|
|
|
// Provides the next key/value pair in the current section. Returns
|
|
|
|
// true if there was another, false otherwise.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
bool FConfigFile::NextInSection (const char *&key, const char *&value)
|
|
|
|
{
|
|
|
|
FConfigEntry *entry = CurrentEntry;
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CurrentEntry = entry->Next;
|
|
|
|
key = entry->Key;
|
|
|
|
value = entry->Value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: GetValueForKey
|
|
|
|
//
|
|
|
|
// Returns the value for the specified key in the current section,
|
|
|
|
// returning NULL if the key does not exist.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
const char *FConfigFile::GetValueForKey (const char *key) const
|
|
|
|
{
|
|
|
|
FConfigEntry *entry = FindEntry (CurrentSection, key);
|
|
|
|
|
|
|
|
if (entry != NULL)
|
|
|
|
{
|
|
|
|
return entry->Value;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: SetValueForKey
|
|
|
|
//
|
|
|
|
// Sets they key/value pair as specified in the current section. If
|
|
|
|
// duplicates are allowed, it always creates a new pair. Otherwise, it
|
|
|
|
// will overwrite the value of an existing key with the same name.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::SetValueForKey (const char *key, const char *value, bool duplicates)
|
|
|
|
{
|
|
|
|
if (CurrentSection != NULL)
|
|
|
|
{
|
|
|
|
FConfigEntry *entry;
|
|
|
|
|
|
|
|
if (duplicates || (entry = FindEntry (CurrentSection, key)) == NULL)
|
|
|
|
{
|
|
|
|
NewConfigEntry (CurrentSection, key, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry->SetValue (value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: FindSection
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile::FConfigSection *FConfigFile::FindSection (const char *name) const
|
|
|
|
{
|
|
|
|
FConfigSection *section = Sections;
|
|
|
|
|
|
|
|
while (section != NULL && stricmp (section->Name, name) != 0)
|
|
|
|
{
|
|
|
|
section = section->Next;
|
|
|
|
}
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: FindEntry
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile::FConfigEntry *FConfigFile::FindEntry (
|
|
|
|
FConfigFile::FConfigSection *section, const char *key) const
|
|
|
|
{
|
|
|
|
FConfigEntry *probe = section->RootEntry;
|
|
|
|
|
|
|
|
while (probe != NULL && stricmp (probe->Key, key) != 0)
|
|
|
|
{
|
|
|
|
probe = probe->Next;
|
|
|
|
}
|
|
|
|
return probe;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: NewConfigSection
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile::FConfigSection *FConfigFile::NewConfigSection (const char *name)
|
|
|
|
{
|
|
|
|
FConfigSection *section;
|
2008-12-07 00:50:04 +00:00
|
|
|
char *memblock;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
section = FindSection (name);
|
|
|
|
if (section == NULL)
|
|
|
|
{
|
|
|
|
size_t namelen = strlen (name);
|
2008-12-07 00:50:04 +00:00
|
|
|
memblock = new char[sizeof(*section)+namelen];
|
|
|
|
section = ::new(memblock) FConfigSection;
|
2006-02-24 04:48:15 +00:00
|
|
|
section->RootEntry = NULL;
|
|
|
|
section->LastEntryPtr = §ion->RootEntry;
|
|
|
|
section->Next = NULL;
|
|
|
|
memcpy (section->Name, name, namelen);
|
|
|
|
section->Name[namelen] = 0;
|
|
|
|
*LastSectionPtr = section;
|
|
|
|
LastSectionPtr = §ion->Next;
|
|
|
|
}
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: NewConfigEntry
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigFile::FConfigEntry *FConfigFile::NewConfigEntry (
|
|
|
|
FConfigFile::FConfigSection *section, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
FConfigEntry *entry;
|
|
|
|
size_t keylen;
|
|
|
|
|
|
|
|
keylen = strlen (key);
|
|
|
|
entry = (FConfigEntry *)new char[sizeof(*section)+keylen];
|
|
|
|
entry->Value = NULL;
|
|
|
|
entry->Next = NULL;
|
|
|
|
memcpy (entry->Key, key, keylen);
|
|
|
|
entry->Key[keylen] = 0;
|
|
|
|
*(section->LastEntryPtr) = entry;
|
|
|
|
section->LastEntryPtr = &entry->Next;
|
|
|
|
entry->SetValue (value);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: LoadConfigFile
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::LoadConfigFile (void (*nosechandler)(const char *pathname, FConfigFile *config, void *userdata), void *userdata)
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
FILE *file = fopen (PathName, "r");
|
2006-02-24 04:48:15 +00:00
|
|
|
bool succ;
|
|
|
|
|
2012-07-06 02:22:58 +00:00
|
|
|
FileExisted = false;
|
2006-02-24 04:48:15 +00:00
|
|
|
if (file == NULL)
|
2012-07-06 02:22:58 +00:00
|
|
|
{
|
2006-02-24 04:48:15 +00:00
|
|
|
return;
|
2012-07-06 02:22:58 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
succ = ReadConfig (file);
|
|
|
|
fclose (file);
|
2012-07-06 02:22:58 +00:00
|
|
|
FileExisted = succ;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
if (!succ)
|
|
|
|
{ // First valid line did not define a section
|
|
|
|
if (nosechandler != NULL)
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
nosechandler (PathName, this, userdata);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: ReadConfig
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
bool FConfigFile::ReadConfig (void *file)
|
|
|
|
{
|
|
|
|
char readbuf[READBUFFERSIZE];
|
|
|
|
FConfigSection *section = NULL;
|
|
|
|
ClearConfig ();
|
|
|
|
|
|
|
|
while (ReadLine (readbuf, READBUFFERSIZE, file) != NULL)
|
|
|
|
{
|
|
|
|
char *start = readbuf;
|
|
|
|
char *equalpt;
|
|
|
|
char *endpt;
|
|
|
|
|
|
|
|
// Remove white space at start of line
|
|
|
|
while (*start && *start <= ' ')
|
|
|
|
{
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
// Remove comment lines
|
|
|
|
if (*start == '#' || (start[0] == '/' && start[1] == '/'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Remove white space at end of line
|
|
|
|
endpt = start + strlen (start) - 1;
|
|
|
|
while (endpt > start && *endpt <= ' ')
|
|
|
|
{
|
|
|
|
endpt--;
|
|
|
|
}
|
|
|
|
endpt[1] = 0;
|
|
|
|
if (endpt <= start)
|
|
|
|
continue; // Nothing here
|
|
|
|
|
|
|
|
if (*start == '[')
|
|
|
|
{ // Section header
|
|
|
|
if (*endpt == ']')
|
|
|
|
*endpt = 0;
|
|
|
|
section = NewConfigSection (start+1);
|
|
|
|
}
|
|
|
|
else if (section == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Should be key=value
|
|
|
|
equalpt = strchr (start, '=');
|
|
|
|
if (equalpt != NULL && equalpt > start)
|
|
|
|
{
|
|
|
|
// Remove white space in front of =
|
|
|
|
char *whiteprobe = equalpt - 1;
|
2008-01-08 01:08:27 +00:00
|
|
|
while (whiteprobe > start && isspace(*whiteprobe))
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
whiteprobe--;
|
|
|
|
}
|
|
|
|
whiteprobe[1] = 0;
|
|
|
|
// Remove white space after =
|
|
|
|
whiteprobe = equalpt + 1;
|
2008-01-08 01:08:27 +00:00
|
|
|
while (*whiteprobe && isspace(*whiteprobe))
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
whiteprobe++;
|
|
|
|
}
|
|
|
|
*(whiteprobe - 1) = 0;
|
2013-06-04 02:06:40 +00:00
|
|
|
// Check for multi-line value
|
|
|
|
if (whiteprobe[0] == '<' && whiteprobe[1] == '<' && whiteprobe[2] == '<' && whiteprobe[3] != '\0')
|
|
|
|
{
|
|
|
|
ReadMultiLineValue (file, section, start, whiteprobe + 3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NewConfigEntry (section, start, whiteprobe);
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-04 02:06:40 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: ReadMultiLineValue
|
|
|
|
//
|
|
|
|
// Reads a multi-line value, with format as follows:
|
|
|
|
//
|
|
|
|
// key=<<<ENDTAG
|
|
|
|
// ... blah blah blah ...
|
|
|
|
// >>>ENDTAG
|
|
|
|
//
|
|
|
|
// The final ENDTAG must be on a line all by itself.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
FConfigFile::FConfigEntry *FConfigFile::ReadMultiLineValue(void *file, FConfigSection *section, const char *key, const char *endtag)
|
|
|
|
{
|
|
|
|
char readbuf[READBUFFERSIZE];
|
|
|
|
FString value;
|
|
|
|
size_t endlen = strlen(endtag);
|
|
|
|
|
|
|
|
// Keep on reading lines until we reach a line that matches >>>endtag
|
|
|
|
while (ReadLine(readbuf, READBUFFERSIZE, file) != NULL)
|
|
|
|
{
|
|
|
|
// Does the start of this line match the endtag?
|
|
|
|
if (readbuf[0] == '>' && readbuf[1] == '>' && readbuf[2] == '>' &&
|
|
|
|
strncmp(readbuf + 3, endtag, endlen) == 0)
|
|
|
|
{ // Is there nothing but line break characters after the match?
|
|
|
|
size_t i;
|
|
|
|
for (i = endlen + 3; readbuf[i] != '\0'; ++i)
|
|
|
|
{
|
|
|
|
if (readbuf[i] != '\n' && readbuf[i] != '\r')
|
|
|
|
{ // Not a line break character
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readbuf[i] == '\0')
|
|
|
|
{ // We're done; strip the previous line's line breaks, since it's not part of the value.
|
|
|
|
value.StripRight("\n\r");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Append this line to the value.
|
|
|
|
value << readbuf;
|
|
|
|
}
|
|
|
|
return NewConfigEntry(section, key, value);
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: ReadLine
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
char *FConfigFile::ReadLine (char *string, int n, void *file) const
|
|
|
|
{
|
|
|
|
return fgets (string, n, (FILE *)file);
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: WriteConfigFile
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-09-20 02:00:19 +00:00
|
|
|
bool FConfigFile::WriteConfigFile () const
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2012-07-06 02:22:58 +00:00
|
|
|
if (!OkayToWrite && FileExisted)
|
2012-05-15 21:47:15 +00:00
|
|
|
{ // Pretend it was written anyway so that the user doesn't get
|
2012-07-06 02:22:58 +00:00
|
|
|
// any "config not written" notifications, but only if the file
|
|
|
|
// already existed. Otherwise, let it write out a default one.
|
2012-05-15 21:47:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
FILE *file = fopen (PathName, "w");
|
2006-02-24 04:48:15 +00:00
|
|
|
FConfigSection *section;
|
|
|
|
FConfigEntry *entry;
|
|
|
|
|
|
|
|
if (file == NULL)
|
2006-09-20 02:00:19 +00:00
|
|
|
return false;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
WriteCommentHeader (file);
|
|
|
|
|
|
|
|
section = Sections;
|
|
|
|
while (section != NULL)
|
|
|
|
{
|
|
|
|
entry = section->RootEntry;
|
2008-12-07 00:50:04 +00:00
|
|
|
if (section->Note.IsNotEmpty())
|
|
|
|
{
|
2008-12-07 01:50:12 +00:00
|
|
|
fputs (section->Note.GetChars(), file);
|
2008-12-07 00:50:04 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
fprintf (file, "[%s]\n", section->Name);
|
|
|
|
while (entry != NULL)
|
|
|
|
{
|
2013-06-04 02:06:40 +00:00
|
|
|
if (strpbrk(entry->Value, "\r\n") == NULL)
|
|
|
|
{ // Single-line value
|
|
|
|
fprintf (file, "%s=%s\n", entry->Key, entry->Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Multi-line value
|
|
|
|
const char *endtag = GenerateEndTag(entry->Value);
|
|
|
|
fprintf (file, "%s=<<<%s\n%s\n>>>%s\n", entry->Key,
|
|
|
|
endtag, entry->Value, endtag);
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
entry = entry->Next;
|
|
|
|
}
|
|
|
|
section = section->Next;
|
2008-12-07 01:50:12 +00:00
|
|
|
fputs ("\n", file);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
fclose (file);
|
2006-09-20 02:00:19 +00:00
|
|
|
return true;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 02:06:40 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: GenerateEndTag
|
|
|
|
//
|
|
|
|
// Generates a terminator sequence for multi-line values that does
|
|
|
|
// not appear anywhere in the value.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
const char *FConfigFile::GenerateEndTag(const char *value)
|
|
|
|
{
|
|
|
|
static const char Base64Table[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
|
|
|
|
static char EndTag[25] = "EOV-";
|
|
|
|
|
|
|
|
// Try different 20-character sequences until we find one that
|
|
|
|
// isn't in the value. We create the sequences by generating two
|
|
|
|
// 64-bit random numbers and Base64 encoding the first 15 bytes
|
|
|
|
// from them.
|
|
|
|
union { QWORD rand_num[2]; BYTE rand_bytes[16]; };
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rand_num[0] = pr_endtag.GenRand64();
|
|
|
|
rand_num[1] = pr_endtag.GenRand64();
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
{
|
2015-02-07 15:35:23 +00:00
|
|
|
//DWORD three_bytes = (rand_bytes[i*3] << 16) | (rand_bytes[i*3+1] << 8) | (rand_bytes[i*3+2]); // ???
|
2013-06-04 02:06:40 +00:00
|
|
|
EndTag[4+i*4 ] = Base64Table[rand_bytes[i*3] >> 2];
|
|
|
|
EndTag[4+i*4+1] = Base64Table[((rand_bytes[i*3] & 3) << 4) | (rand_bytes[i*3+1] >> 4)];
|
|
|
|
EndTag[4+i*4+2] = Base64Table[((rand_bytes[i*3+1] & 15) << 2) | (rand_bytes[i*3+2] >> 6)];
|
|
|
|
EndTag[4+i*4+3] = Base64Table[rand_bytes[i*3+2] & 63];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (strstr(value, EndTag) != NULL);
|
|
|
|
return EndTag;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: WriteCommentHeader
|
|
|
|
//
|
|
|
|
// Override in a subclass to write a header to the config file.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::WriteCommentHeader (FILE *file) const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: FConfigEntry :: SetValue
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::FConfigEntry::SetValue (const char *value)
|
|
|
|
{
|
|
|
|
if (Value != NULL)
|
|
|
|
{
|
|
|
|
delete[] Value;
|
|
|
|
}
|
|
|
|
Value = new char[strlen (value)+1];
|
|
|
|
strcpy (Value, value);
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: GetPosition
|
|
|
|
//
|
|
|
|
// Populates a struct with the current position of the parse cursor.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::GetPosition (FConfigFile::Position &pos) const
|
|
|
|
{
|
|
|
|
pos.Section = CurrentSection;
|
|
|
|
pos.Entry = CurrentEntry;
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:50:04 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: SetPosition
|
|
|
|
//
|
|
|
|
// Sets the parse cursor to a previously retrieved position.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
void FConfigFile::SetPosition (const FConfigFile::Position &pos)
|
|
|
|
{
|
|
|
|
CurrentSection = pos.Section;
|
|
|
|
CurrentEntry = pos.Entry;
|
|
|
|
}
|
2008-12-07 00:50:04 +00:00
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// FConfigFile :: SetSectionNote
|
|
|
|
//
|
|
|
|
// Sets a comment note to be inserted into the INI verbatim directly
|
|
|
|
// ahead of the section. Notes are lost when the INI is read so must
|
|
|
|
// be explicitly set to be maintained.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
void FConfigFile::SetSectionNote(const char *section, const char *note)
|
|
|
|
{
|
|
|
|
SetSectionNote(FindSection(section), note);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FConfigFile::SetSectionNote(const char *note)
|
|
|
|
{
|
|
|
|
SetSectionNote(CurrentSection, note);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FConfigFile::SetSectionNote(FConfigSection *section, const char *note)
|
|
|
|
{
|
|
|
|
if (section != NULL)
|
|
|
|
{
|
|
|
|
if (note == NULL)
|
|
|
|
{
|
|
|
|
note = "";
|
|
|
|
}
|
|
|
|
section->Note = note;
|
|
|
|
}
|
|
|
|
}
|