1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Initial implementation of the ini library.

This commit is contained in:
Sandu Liviu Catalin 2016-02-21 15:55:50 +02:00
parent 35d662a02f
commit 22f1bb5a0c
4 changed files with 1370 additions and 1 deletions

View File

@ -254,6 +254,8 @@
<Unit filename="../source/Library/Format.hpp" />
<Unit filename="../source/Library/Hashing.cpp" />
<Unit filename="../source/Library/Hashing.hpp" />
<Unit filename="../source/Library/INI.cpp" />
<Unit filename="../source/Library/INI.hpp" />
<Unit filename="../source/Library/Math.cpp" />
<Unit filename="../source/Library/Math.hpp" />
<Unit filename="../source/Library/Numeric.cpp" />

656
source/Library/INI.cpp Normal file
View File

@ -0,0 +1,656 @@
// ------------------------------------------------------------------------------------------------
#include "Library/INI.hpp"
// ------------------------------------------------------------------------------------------------
#include <errno.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
Int32 IniEntries::Cmp(const IniEntries & o)
{
if (m_Elem == o.m_Elem)
return 0;
else if (this > &o)
return 1;
else
return -1;
}
// ------------------------------------------------------------------------------------------------
CSStr IniEntries::ToString() const
{
return GetItem();
}
// ------------------------------------------------------------------------------------------------
void IniEntries::Next()
{
// Are there any other elements ahead?
if (!m_List.empty() && m_Elem != m_List.end())
++m_Elem; /* Go ahead one element */
}
// ------------------------------------------------------------------------------------------------
void IniEntries::Prev()
{
// Are there any other elements behind?
if (!m_List.empty() && m_Elem != m_List.begin())
--m_Elem; /* Go back one element */
}
// ------------------------------------------------------------------------------------------------
void IniEntries::Advance(Int32 n)
{
// Are there any other elements ahead?
if (m_List.empty() || m_Elem == m_List.end())
return;
// Jump as many elements as possible
while ((--n >= 0) && m_Elem != m_List.end()) ++m_Elem;
}
// ------------------------------------------------------------------------------------------------
void IniEntries::Retreat(Int32 n)
{
// Are there any other elements behind?
if (m_List.empty() || m_Elem == m_List.begin())
return;
// Jump as many elements as possible
while ((--n >= 0) && m_Elem != m_List.begin()) --m_Elem;
}
// ------------------------------------------------------------------------------------------------
CSStr IniEntries::GetItem() const
{
if (m_List.empty() || m_Elem == m_List.end())
SqThrow("Invalid INI entry [item]");
else
return m_Elem->pItem;
return _SC("");
}
// ------------------------------------------------------------------------------------------------
CSStr IniEntries::GetComment() const
{
if (m_List.empty() || m_Elem == m_List.end())
SqThrow("Invalid INI entry [comment]");
else
return m_Elem->pComment;
return _SC("");
}
// ------------------------------------------------------------------------------------------------
Int32 IniEntries::GetOrder() const
{
if (m_List.empty() || m_Elem == m_List.end())
SqThrow("Invalid INI entry [order]");
else
return m_Elem->nOrder;
return -1;
}
// ------------------------------------------------------------------------------------------------
IniDocument::IniDocument()
: m_Doc(false, false, true)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
IniDocument::IniDocument(bool utf8, bool multikey, bool multiline)
: m_Doc(utf8, multikey, multiline)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
IniDocument::~IniDocument()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Int32 IniDocument::Cmp(const IniDocument & o)
{
if (m_Doc == o.m_Doc)
return 0;
else if (this > &o)
return 1;
else
return -1;
}
// ------------------------------------------------------------------------------------------------
CSStr IniDocument::ToString() const
{
return _SC("");
}
// ------------------------------------------------------------------------------------------------
void IniDocument::LoadFile(CSStr filepath)
{
if (!Validate())
return; /* Unable to proceed */
else if (!filepath)
{
SqThrow("Invalid ini filepath");
return; /* Nothing to load */
}
// Attempt to load the file from disk
const SI_Error ini_ret = m_Doc->LoadFile(filepath);
// See if the file could be loaded
if (ini_ret < 0)
{
// Identify the error type
switch (ini_ret)
{
case SI_FAIL:
SqThrow("Failed to load the ini file. Probably invalid");
break;
case SI_NOMEM:
SqThrow("Ran out of memory while loading the ini file");
break;
case SI_FILE:
SqThrow("Failed to load the ini file. %s", strerror(errno));
break;
default:
SqThrow("Failed to load the ini file for some unforeseen reason");
}
}
}
// ------------------------------------------------------------------------------------------------
void IniDocument::LoadData(CSStr source, Int32 size)
{
if (!Validate())
return; /* Unable to proceed */
else if (!source)
{
SqThrow("Invalid ini source");
return; /* Nothing to load */
}
// Attempt to load the source from memory
const SI_Error ini_ret = m_Doc->LoadData(source, size < 0 ? strlen(source) : size);
// See if the source could be loaded
if (ini_ret < 0)
{
// Identify the error type
switch (ini_ret)
{
case SI_FAIL:
SqThrow("Failed to load the ini data. Probably invalid");
break;
case SI_NOMEM:
SqThrow("Ran out of memory while loading the ini data");
break;
default:
SqThrow("Failed to load the ini data for some unforeseen reason");
}
}
}
// ------------------------------------------------------------------------------------------------
void IniDocument::SaveFile(CSStr filepath, bool signature)
{
if (!Validate())
return; /* Unable to proceed */
else if (!filepath)
{
SqThrow("Invalid ini filepath");
return; /* Nothing to load */
}
// Attempt to save the file to disk
const SI_Error ini_ret = m_Doc->SaveFile(filepath, signature);
// See if the file could be saved
if (ini_ret == SI_FAIL)
{
SqThrow("Failed to save the ini file. Probably invalid");
}
}
// ------------------------------------------------------------------------------------------------
Object IniDocument::SaveData(bool signature)
{
if (!Validate())
return NullObject(); /* Unable to proceed */
// The string where the content will be saved
String source;
// Attempt to save the data to string
const SI_Error ini_ret = m_Doc->Save(source, signature);
// See if the data could be saved
if (ini_ret == SI_FAIL)
{
SqThrow("Failed to save the ini data. Probably invalid");
return NullObject(); /* Unable to proceed */
}
// Transform it into a script object
sq_pushstring(DefaultVM::Get(), source.c_str(), source.size());
// Get the object from the stack
Var< Object & > var(DefaultVM::Get(), -1);
// Pop the created object from the stack
if (!var.value.IsNull())
{
sq_pop(DefaultVM::Get(), 1);
}
// Return the script object
return var.value;
}
// ------------------------------------------------------------------------------------------------
IniEntries IniDocument::GetAllSections() const
{
if (!Validate())
return IniEntries(); /* Unable to proceed */
// Prepare a container to receive the entries
static Container entries;
// Clear previous values (if any)
entries.clear();
// Obtain all sections from the ini document
m_Doc->GetAllSections(entries);
// Return the entries and take over content
return IniEntries(m_Doc, entries);
}
// ------------------------------------------------------------------------------------------------
IniEntries IniDocument::GetAllKeys(CSStr section) const
{
if (!Validate())
return IniEntries(); /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return IniEntries(); /* Unable to proceed */
}
// Prepare a container to receive the entries
static Container entries;
// Clear previous values (if any)
entries.clear();
// Obtain all sections from the ini document
m_Doc->GetAllKeys(section, entries);
// Return the entries and take over content
return IniEntries(m_Doc, entries);
}
// ------------------------------------------------------------------------------------------------
IniEntries IniDocument::GetAllValues(CSStr section, CSStr key) const
{
if (!Validate())
return IniEntries(); /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return IniEntries(); /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return IniEntries(); /* Unable to proceed */
}
// Prepare a container to receive the entries
static Container entries;
// Clear previous values (if any)
entries.clear();
// Obtain all sections from the ini document
m_Doc->GetAllValues(section, key, entries);
// Return the entries and take over content
return IniEntries(m_Doc, entries);
}
// ------------------------------------------------------------------------------------------------
Int32 IniDocument::GetSectionSize(CSStr section) const
{
if (!Validate())
(void)(section); /* Just ignore... */
else if (!section)
SqThrow("Invalid ini section");
// Return the requested information
else
return m_Doc->GetSectionSize(section);
// Return invalid size
return -1;
}
// ------------------------------------------------------------------------------------------------
bool IniDocument::HasMultipleKeys(CSStr section, CSStr key) const
{
if (!Validate())
return false; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return false; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return false; /* Unable to proceed */
}
// Where to retrive whether the key has multiple instances
bool multiple = false;
// Attempt to query the information
if (m_Doc->GetValue(section, key, NULL, &multiple) == NULL)
return true; /* Doesn't exist */
return multiple;
}
// ------------------------------------------------------------------------------------------------
CCStr IniDocument::GetValue(CSStr section, CSStr key, CSStr def) const
{
if (!Validate())
return _SC(""); /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return _SC(""); /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return _SC(""); /* Unable to proceed */
}
// Attempt to query the information and return it
return m_Doc->GetValue(section, key, def, NULL);
}
// ------------------------------------------------------------------------------------------------
SQInteger IniDocument::GetInteger(CSStr section, CSStr key, SQInteger def) const
{
if (!Validate())
return 0; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return 0; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return 0; /* Unable to proceed */
}
// Attempt to query the information and return it
return (SQInteger)m_Doc->GetLongValue(section, key, def, NULL);
}
// ------------------------------------------------------------------------------------------------
SQFloat IniDocument::GetFloat(CSStr section, CSStr key, SQFloat def) const
{
if (!Validate())
return 0.0; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return 0.0; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return 0.0; /* Unable to proceed */
}
// Attempt to query the information and return it
return (SQFloat)m_Doc->GetDoubleValue(section, key, def, NULL);
}
// ------------------------------------------------------------------------------------------------
bool IniDocument::GetBoolean(CSStr section, CSStr key, bool def) const
{
if (!Validate())
return false; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return false; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return false; /* Unable to proceed */
}
// Attempt to query the information and return it
return m_Doc->GetBoolValue(section, key, def, NULL);
}
// ------------------------------------------------------------------------------------------------
void IniDocument::SetValue(CSStr section, CSStr key, CSStr value, bool force, CSStr comment)
{
if (!Validate())
return; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return; /* Unable to proceed */
}
// Attempt to apply the specified information
const SI_Error ini_ret = m_Doc->SetValue(section, key, value, comment, force);
// See if the information could be applied
if (ini_ret < 0)
{
// Identify the error type
switch (ini_ret)
{
case SI_FAIL:
SqThrow("Failed to set the ini value. Probably invalid");
break;
case SI_NOMEM:
SqThrow("Ran out of memory while setting ini value");
break;
default:
SqThrow("Failed to set the ini value for some unforeseen reason");
}
}
}
// ------------------------------------------------------------------------------------------------
void IniDocument::SetInteger(CSStr section, CSStr key, SQInteger value, bool hex, bool force, CSStr comment)
{
if (!Validate())
return; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return; /* Unable to proceed */
}
// Attempt to apply the specified information
const SI_Error ini_ret = m_Doc->SetLongValue(section, key, value, comment, hex, force);
// See if the information could be applied
if (ini_ret < 0)
{
// Identify the error type
switch (ini_ret)
{
case SI_FAIL:
SqThrow("Failed to set the ini integer. Probably invalid");
break;
case SI_NOMEM:
SqThrow("Ran out of memory while setting ini integer");
break;
default:
SqThrow("Failed to set the ini integer for some unforeseen reason");
}
}
}
// ------------------------------------------------------------------------------------------------
void IniDocument::SetFloat(CSStr section, CSStr key, SQFloat value, bool force, CSStr comment)
{
if (!Validate())
return; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return; /* Unable to proceed */
}
// Attempt to apply the specified information
const SI_Error ini_ret = m_Doc->SetDoubleValue(section, key, value, comment, force);
// See if the information could be applied
if (ini_ret < 0)
{
// Identify the error type
switch (ini_ret)
{
case SI_FAIL:
SqThrow("Failed to set the ini float. Probably invalid");
break;
case SI_NOMEM:
SqThrow("Ran out of memory while setting ini float");
break;
default:
SqThrow("Failed to set the ini float for some unforeseen reason");
}
}
}
// ------------------------------------------------------------------------------------------------
void IniDocument::SetBoolean(CSStr section, CSStr key, bool value, bool force, CSStr comment)
{
if (!Validate())
return; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return; /* Unable to proceed */
}
// Attempt to apply the specified information
const SI_Error ini_ret = m_Doc->SetBoolValue(section, key, value, comment, force);
// See if the information could be applied
if (ini_ret < 0)
{
// Identify the error type
switch (ini_ret)
{
case SI_FAIL:
SqThrow("Failed to set the ini boolean. Probably invalid");
break;
case SI_NOMEM:
SqThrow("Ran out of memory while setting ini boolean");
break;
default:
SqThrow("Failed to set the ini boolean for some unforeseen reason");
}
}
}
// ------------------------------------------------------------------------------------------------
bool IniDocument::DeleteValue(CSStr section, CSStr key, CSStr value, bool empty)
{
if (!Validate())
return false; /* Unable to proceed */
else if (!section)
{
SqThrow("Invalid ini section");
return false; /* Unable to proceed */
}
else if (!key)
{
SqThrow("Invalid ini key");
return false; /* Unable to proceed */
}
// Attempt to remove the specified value
return m_Doc->DeleteValue(section, key, value, empty);
}
// ================================================================================================
void Register_INI(HSQUIRRELVM vm)
{
Table inins(vm);
inins.Bind(_SC("Entries"), Class< IniEntries >(vm, _SC("SqIniEntries"))
/* Constructors */
.Ctor()
.Ctor< const IniEntries & >()
/* Core Metamethods */
.Func(_SC("_tostring"), &IniEntries::ToString)
.Func(_SC("_cmp"), &IniEntries::Cmp)
/* Properties */
.Prop(_SC("Valid"), &IniEntries::IsValid)
.Prop(_SC("Empty"), &IniEntries::IsEmpty)
.Prop(_SC("Size"), &IniEntries::GetSize)
.Prop(_SC("Item"), &IniEntries::GetItem)
.Prop(_SC("Comment"), &IniEntries::GetComment)
.Prop(_SC("Order"), &IniEntries::GetOrder)
/* Functions */
.Func(_SC("Reset"), &IniEntries::Reset)
.Func(_SC("Next"), &IniEntries::Next)
.Func(_SC("Prev"), &IniEntries::Prev)
.Func(_SC("Advance"), &IniEntries::Advance)
.Func(_SC("Retreat"), &IniEntries::Retreat)
.Func(_SC("Sort"), &IniEntries::Sort)
.Func(_SC("SortByKeyOrder"), &IniEntries::SortByKeyOrder)
.Func(_SC("SortByLoadOrder"), &IniEntries::SortByLoadOrder)
.Func(_SC("SortByLoadOrderEmptyFirst"), &IniEntries::SortByLoadOrderEmptyFirst)
);
inins.Bind(_SC("Document"), Class< IniDocument, NoCopy< IniDocument > >(vm, _SC("SqIniDocument"))
/* Constructors */
.Ctor()
.Ctor< bool, bool, bool >()
/* Core Metamethods */
.Func(_SC("_tostring"), &IniDocument::ToString)
.Func(_SC("_cmp"), &IniDocument::Cmp)
/* Properties */
.Prop(_SC("Valid"), &IniDocument::IsValid)
.Prop(_SC("Empty"), &IniDocument::IsEmpty)
.Prop(_SC("Unicode"), &IniDocument::GetUnicode, &IniDocument::SetUnicode)
.Prop(_SC("MultiKey"), &IniDocument::GetMultiKey, &IniDocument::SetMultiKey)
.Prop(_SC("MultiLine"), &IniDocument::GetMultiLine, &IniDocument::SetMultiLine)
.Prop(_SC("Spaces"), &IniDocument::GetSpaces, &IniDocument::SetSpaces)
/* Functions */
.Func(_SC("Reset"), &IniDocument::Reset)
.Func(_SC("LoadFile"), &IniDocument::LoadFile)
.Overload< void (IniDocument::*)(CSStr) >(_SC("LoadData"), &IniDocument::LoadData)
.Overload< void (IniDocument::*)(CSStr, Int32) >(_SC("LoadData"), &IniDocument::LoadData)
.Overload< void (IniDocument::*)(CSStr) >(_SC("SaveFile"), &IniDocument::SaveFile)
.Overload< void (IniDocument::*)(CSStr, bool) >(_SC("SaveFile"), &IniDocument::SaveFile)
.Func(_SC("SaveData"), &IniDocument::SaveData)
.Func(_SC("GetSections"), &IniDocument::GetAllSections)
.Func(_SC("GetKeys"), &IniDocument::GetAllKeys)
.Func(_SC("GetValues"), &IniDocument::GetAllValues)
.Func(_SC("GetSectionSize"), &IniDocument::GetSectionSize)
.Func(_SC("HasMultipleKeys"), &IniDocument::HasMultipleKeys)
.Func(_SC("GetValue"), &IniDocument::GetValue)
.Func(_SC("GetInteger"), &IniDocument::GetInteger)
.Func(_SC("GetFloat"), &IniDocument::GetFloat)
.Func(_SC("GetBoolean"), &IniDocument::GetBoolean)
.Overload< void (IniDocument::*)(CSStr, CSStr, CSStr) >(_SC("SetValue"), &IniDocument::SetValue)
.Overload< void (IniDocument::*)(CSStr, CSStr, CSStr, bool) >(_SC("SetValue"), &IniDocument::SetValue)
.Overload< void (IniDocument::*)(CSStr, CSStr, CSStr, bool, CSStr) >(_SC("SetValue"), &IniDocument::SetValue)
.Overload< void (IniDocument::*)(CSStr, CSStr, SQInteger) >(_SC("SetInteger"), &IniDocument::SetInteger)
.Overload< void (IniDocument::*)(CSStr, CSStr, SQInteger, bool) >(_SC("SetInteger"), &IniDocument::SetInteger)
.Overload< void (IniDocument::*)(CSStr, CSStr, SQInteger, bool, bool) >(_SC("SetInteger"), &IniDocument::SetInteger)
.Overload< void (IniDocument::*)(CSStr, CSStr, SQInteger, bool, bool, CSStr) >(_SC("SetInteger"), &IniDocument::SetInteger)
.Overload< void (IniDocument::*)(CSStr, CSStr, SQFloat) >(_SC("SetFloat"), &IniDocument::SetFloat)
.Overload< void (IniDocument::*)(CSStr, CSStr, SQFloat, bool) >(_SC("SetFloat"), &IniDocument::SetFloat)
.Overload< void (IniDocument::*)(CSStr, CSStr, SQFloat, bool, CSStr) >(_SC("SetFloat"), &IniDocument::SetFloat)
.Overload< void (IniDocument::*)(CSStr, CSStr, bool) >(_SC("SetBoolean"), &IniDocument::SetBoolean)
.Overload< void (IniDocument::*)(CSStr, CSStr, bool, bool) >(_SC("SetBoolean"), &IniDocument::SetBoolean)
.Overload< void (IniDocument::*)(CSStr, CSStr, bool, bool, CSStr) >(_SC("SetBoolean"), &IniDocument::SetBoolean)
.Overload< bool (IniDocument::*)(CSStr) >(_SC("DeleteValue"), &IniDocument::DeleteValue)
.Overload< bool (IniDocument::*)(CSStr, CSStr) >(_SC("DeleteValue"), &IniDocument::DeleteValue)
.Overload< bool (IniDocument::*)(CSStr, CSStr, CSStr) >(_SC("DeleteValue"), &IniDocument::DeleteValue)
.Overload< bool (IniDocument::*)(CSStr, CSStr, CSStr, bool) >(_SC("DeleteValue"), &IniDocument::DeleteValue)
);
RootTable(vm).Bind(_SC("SqIni"), inins);
}
} // Namespace:: SqMod

709
source/Library/INI.hpp Normal file
View File

@ -0,0 +1,709 @@
#ifndef _LIBRARY_INI_HPP_
#define _LIBRARY_INI_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <SimpleIni.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class IniEntries;
class IniDocument;
/* ------------------------------------------------------------------------------------------------
* Manages a reference counted ini source instance.
*/
class IniRef
{
// --------------------------------------------------------------------------------------------
friend class IniDocument;
private:
// --------------------------------------------------------------------------------------------
typedef CSimpleIniA Source;
// --------------------------------------------------------------------------------------------
typedef unsigned int Counter;
// --------------------------------------------------------------------------------------------
Source* m_Ptr; /* The ini reader, writer and manager instance. */
Counter* m_Ref; /* Reference count to the managed instance. */
/* --------------------------------------------------------------------------------------------
* Grab a strong reference to a ini instance.
*/
void Grab()
{
if (m_Ptr)
{
++(*m_Ref);
}
}
/* --------------------------------------------------------------------------------------------
* Drop a strong reference to a ini instance.
*/
void Drop()
{
if (m_Ptr && --(*m_Ref) == 0)
{
delete m_Ptr;
delete m_Ref;
m_Ptr = NULL;
m_Ref = NULL;
}
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
IniRef(bool utf8, bool multikey, bool multiline)
: m_Ptr(new Source(utf8, multikey, multiline)), m_Ref(new Counter(1))
{
/* ... */
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
IniRef()
: m_Ptr(NULL), m_Ref(NULL)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
IniRef(const IniRef & o)
: m_Ptr(o.m_Ptr), m_Ref(o.m_Ref)
{
Grab();
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~IniRef()
{
Drop();
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
IniRef & operator = (const IniRef & o)
{
if (m_Ptr != o.m_Ptr)
{
Drop();
m_Ptr = o.m_Ptr;
m_Ref = o.m_Ref;
Grab();
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two ini instances.
*/
bool operator == (const IniRef & o) const
{
return (m_Ptr == o.m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two ini instances.
*/
bool operator != (const IniRef & o) const
{
return (m_Ptr != o.m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean for use in boolean operations.
*/
operator bool () const
{
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Member operator for dereferencing the managed pointer.
*/
Source * operator -> () const
{
assert(m_Ptr != NULL);
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Indirection operator for obtaining a reference of the managed pointer.
*/
Source & operator * () const
{
assert(m_Ptr != NULL);
return *m_Ptr;
}
};
/* ------------------------------------------------------------------------------------------------
* Class that can access and iterate a series of entries in the ini document.
*/
class IniEntries
{
// --------------------------------------------------------------------------------------------
friend class IniDocument;
protected:
// --------------------------------------------------------------------------------------------
typedef CSimpleIniA Source;
// --------------------------------------------------------------------------------------------
typedef Source::TNamesDepend Container;
// --------------------------------------------------------------------------------------------
typedef Container::iterator Iterator;
/* ---------------------------------------------------------------------------------------------
* Default constructor.
*/
IniEntries(const IniRef & ini, Container & list)
: m_Doc(ini), m_List(), m_Elem()
{
m_List.swap(list);
Reset();
}
private:
// ---------------------------------------------------------------------------------------------
IniRef m_Doc; /* The document that contains the elements. */
Container m_List; /* The list of elements to iterate. */
Iterator m_Elem; /* The currently processed element. */
public:
/* ---------------------------------------------------------------------------------------------
* Default constructor. (null)
*/
IniEntries()
: m_Doc(), m_List(), m_Elem(m_List.end())
{
/* ... */
}
/* ---------------------------------------------------------------------------------------------
* Copy constructor.
*/
IniEntries(const IniEntries & o)
: m_Doc(o.m_Doc), m_List(o.m_List), m_Elem()
{
Reset();
}
/* ---------------------------------------------------------------------------------------------
* Destructor.
*/
~IniEntries()
{
/* ... */
}
/* ---------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
IniEntries & operator = (const IniEntries & o)
{
m_Doc = o.m_Doc;
m_List = o.m_List;
Reset();
return *this;
}
/* ---------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const IniEntries & o);
/* ---------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* ---------------------------------------------------------------------------------------------
* Return whether the current element is valid and can be accessed.
*/
bool IsValid() const
{
return !(m_List.empty() || m_Elem == m_List.end());
}
/* ---------------------------------------------------------------------------------------------
* Return whether the entry list is empty.
*/
bool IsEmpty() const
{
return m_List.empty();
}
/* ---------------------------------------------------------------------------------------------
* Return the total entries in the list.
*/
Int32 GetSize() const
{
return (Int32)m_List.size();
}
/* ---------------------------------------------------------------------------------------------
* Reset the internal iterator to the first element.
*/
void Reset()
{
if (m_List.empty())
m_Elem = m_List.end();
else
m_Elem = m_List.begin();
}
/* ---------------------------------------------------------------------------------------------
* Go to the next element.
*/
void Next();
/* ---------------------------------------------------------------------------------------------
* Go to the previous element.
*/
void Prev();
/* ---------------------------------------------------------------------------------------------
* Advance a certain number of elements.
*/
void Advance(Int32 n);
/* ---------------------------------------------------------------------------------------------
* Retreat a certain number of elements.
*/
void Retreat(Int32 n);
/* ---------------------------------------------------------------------------------------------
* Sort the entries using the default options.
*/
void Sort()
{
if (!m_List.empty())
m_List.sort(Source::Entry::KeyOrder());
}
/* ---------------------------------------------------------------------------------------------
* Sort the entries by name of key only.
*/
void SortByKeyOrder()
{
if (!m_List.empty())
m_List.sort(Source::Entry::KeyOrder());
}
/* ---------------------------------------------------------------------------------------------
* Sort the entries by their load order and then name of key.
*/
void SortByLoadOrder()
{
if (!m_List.empty())
m_List.sort(Source::Entry::LoadOrder());
}
/* ---------------------------------------------------------------------------------------------
* Sort the entries by their load order but empty name always goes first.
*/
void SortByLoadOrderEmptyFirst()
{
if (!m_List.empty())
m_List.sort(Source::Entry::LoadOrderEmptyFirst());
}
/* ---------------------------------------------------------------------------------------------
* Retrieve the string value of the current element item.
*/
CSStr GetItem() const;
/* ---------------------------------------------------------------------------------------------
* Retrieve the string value of the current element comment.
*/
CSStr GetComment() const;
/* ---------------------------------------------------------------------------------------------
* Retrieve the order of the current element.
*/
Int32 GetOrder() const;
};
/* ------------------------------------------------------------------------------------------------
* Class that can read/write and alter the contents of ini files.
*/
class IniDocument
{
protected:
// --------------------------------------------------------------------------------------------
typedef CSimpleIniA Source;
// --------------------------------------------------------------------------------------------
typedef Source::TNamesDepend Container;
/* ---------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
IniDocument(const IniDocument & o);
/* ---------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
IniDocument & operator = (const IniDocument & o);
/* ---------------------------------------------------------------------------------------------
* Validate the document reference.
*/
bool Validate() const
{
if (m_Doc)
return true;
SqThrow("Invalid ini document reference");
return false;
}
private:
// ---------------------------------------------------------------------------------------------
IniRef m_Doc; /* The main ini document instance. */
public:
/* ---------------------------------------------------------------------------------------------
* Default constructor.
*/
IniDocument();
/* ---------------------------------------------------------------------------------------------
* Explicit constructor.
*/
IniDocument(bool utf8, bool multikey, bool multiline);
/* ---------------------------------------------------------------------------------------------
* Destructor.
*/
~IniDocument();
/* ---------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const IniDocument & o);
/* ---------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* ---------------------------------------------------------------------------------------------
* See whether this instance references a valid ini document.
*/
bool IsValid() const
{
return m_Doc;
}
/* ---------------------------------------------------------------------------------------------
* See whether any data has been loaded into this document.
*/
bool IsEmpty() const
{
return m_Doc->IsEmpty();
}
/* ---------------------------------------------------------------------------------------------
* Deallocate all memory stored by this document.
*/
void Reset() const
{
m_Doc->Reset();
}
/* ---------------------------------------------------------------------------------------------
* See whether the ini data is treated as unicode.
*/
bool GetUnicode() const
{
return m_Doc->IsUnicode();
}
/* ---------------------------------------------------------------------------------------------
* Set whether the ini data should be treated as unicode.
*/
void SetUnicode(bool toggle)
{
m_Doc->SetUnicode(toggle);
}
/* ---------------------------------------------------------------------------------------------
* See whether multiple identical keys be permitted in the file.
*/
bool GetMultiKey() const
{
return m_Doc->IsMultiKey();
}
/* ---------------------------------------------------------------------------------------------
* Set whether multiple identical keys be permitted in the file.
*/
void SetMultiKey(bool toggle)
{
m_Doc->SetMultiKey(toggle);
}
/* ---------------------------------------------------------------------------------------------
* See whether data values are permitted to span multiple lines in the file.
*/
bool GetMultiLine() const
{
return m_Doc->IsMultiLine();
}
/* ---------------------------------------------------------------------------------------------
* Set whether data values are permitted to span multiple lines in the file.
*/
void SetMultiLine(bool toggle)
{
m_Doc->SetMultiLine(toggle);
}
/* ---------------------------------------------------------------------------------------------
* See whether spaces are added around the equals sign when writing key/value pairs out.
*/
bool GetSpaces() const
{
return m_Doc->UsingSpaces();
}
/* ---------------------------------------------------------------------------------------------
* Set whether spaces are added around the equals sign when writing key/value pairs out.
*/
void SetSpaces(bool toggle)
{
m_Doc->SetSpaces(toggle);
}
/* ---------------------------------------------------------------------------------------------
* Load an INI file from disk into memory.
*/
void LoadFile(CSStr filepath);
/* ---------------------------------------------------------------------------------------------
* Load INI file data direct from a string.
*/
void LoadData(CSStr source)
{
LoadData(source, -1);
}
/* ---------------------------------------------------------------------------------------------
* Load INI file data direct from a string.
*/
void LoadData(CSStr source, Int32 size);
/* ---------------------------------------------------------------------------------------------
* Save an INI file from memory to disk.
*/
void SaveFile(CSStr filepath)
{
SaveFile(filepath, true);
}
/* ---------------------------------------------------------------------------------------------
* Save an INI file from memory to disk.
*/
void SaveFile(CSStr filepath, bool signature);
/* ---------------------------------------------------------------------------------------------
* Save the INI data to a string.
*/
Object SaveData(bool signature);
/* ---------------------------------------------------------------------------------------------
* Retrieve all section names.
*/
IniEntries GetAllSections() const;
/* ---------------------------------------------------------------------------------------------
* Retrieve all unique key names in a section.
*/
IniEntries GetAllKeys(CSStr section) const;
/* ---------------------------------------------------------------------------------------------
* Retrieve all values for a specific key.
*/
IniEntries GetAllValues(CSStr section, CSStr key) const;
/* ---------------------------------------------------------------------------------------------
* Query the number of keys in a specific section.
*/
Int32 GetSectionSize(CSStr section) const;
/* ---------------------------------------------------------------------------------------------
* See whether a certain key has multiple instances.
*/
bool HasMultipleKeys(CSStr section, CSStr key) const;
/* ---------------------------------------------------------------------------------------------
* Retrieve the value for a specific key.
*/
CCStr GetValue(CSStr section, CSStr key, CSStr def) const;
/* ---------------------------------------------------------------------------------------------
* Retrieve a numeric value for a specific key.
*/
SQInteger GetInteger(CSStr section, CSStr key, SQInteger def) const;
/* ---------------------------------------------------------------------------------------------
* Retrieve a numeric value for a specific key.
*/
SQFloat GetFloat(CSStr section, CSStr key, SQFloat def) const;
/* ---------------------------------------------------------------------------------------------
* Retrieve a boolean value for a specific key.
*/
bool GetBoolean(CSStr section, CSStr key, bool def) const;
/* ---------------------------------------------------------------------------------------------
* Add or update a section or value.
*/
void SetValue(CSStr section, CSStr key, CSStr value)
{
SetValue(section, key, value, false, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a section or value.
*/
void SetValue(CSStr section, CSStr key, CSStr value, bool force)
{
SetValue(section, key, value, force, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a section or value.
*/
void SetValue(CSStr section, CSStr key, CSStr value, bool force, CSStr comment);
/* ---------------------------------------------------------------------------------------------
* Add or update a numeric value.
*/
void SetInteger(CSStr section, CSStr key, SQInteger value)
{
SetInteger(section, key, value, false, false, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a numeric value.
*/
void SetInteger(CSStr section, CSStr key, SQInteger value, bool hex)
{
SetInteger(section, key, value, hex, false, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a numeric value.
*/
void SetInteger(CSStr section, CSStr key, SQInteger value, bool hex, bool force)
{
SetInteger(section, key, value, hex, force, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a numeric value.
*/
void SetInteger(CSStr section, CSStr key, SQInteger value, bool hex, bool force, CSStr comment);
/* ---------------------------------------------------------------------------------------------
* Add or update a double value.
*/
void SetFloat(CSStr section, CSStr key, SQFloat value)
{
SetFloat(section, key, value, false, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a double value.
*/
void SetFloat(CSStr section, CSStr key, SQFloat value, bool force)
{
SetFloat(section, key, value, force, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a double value.
*/
void SetFloat(CSStr section, CSStr key, SQFloat value, bool force, CSStr comment);
/* ---------------------------------------------------------------------------------------------
* Add or update a double value.
*/
void SetBoolean(CSStr section, CSStr key, bool value)
{
SetBoolean(section, key, value, false, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a double value.
*/
void SetBoolean(CSStr section, CSStr key, bool value, bool force)
{
SetBoolean(section, key, value, force, NULL);
}
/* ---------------------------------------------------------------------------------------------
* Add or update a boolean value.
*/
void SetBoolean(CSStr section, CSStr key, bool value, bool force, CSStr comment);
/* ---------------------------------------------------------------------------------------------
* Delete an entire section, or a key from a section.
*/
bool DeleteValue(CSStr section)
{
return DeleteValue(section, NULL, NULL, false);
}
/* ---------------------------------------------------------------------------------------------
* Delete an entire section, or a key from a section.
*/
bool DeleteValue(CSStr section, CSStr key)
{
return DeleteValue(section, key, NULL, false);
}
/* ---------------------------------------------------------------------------------------------
* Delete an entire section, or a key from a section.
*/
bool DeleteValue(CSStr section, CSStr key, CSStr value)
{
return DeleteValue(section, key, value, false);
}
/* ---------------------------------------------------------------------------------------------
* Delete an entire section, or a key from a section.
*/
bool DeleteValue(CSStr section, CSStr key, CSStr value, bool empty);
};
} // Namespace:: SqMod
#endif // _LIBRARY_INI_HPP_

View File

@ -35,6 +35,7 @@ extern void Register_Entity(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
extern void Register_Hash(HSQUIRRELVM vm);
extern void Register_INI(HSQUIRRELVM vm);
extern void Register_Numeric(HSQUIRRELVM vm);
extern void Register_Random(HSQUIRRELVM vm);
extern void Register_SQLite(HSQUIRRELVM vm);
@ -77,8 +78,9 @@ bool RegisterAPI(HSQUIRRELVM vm)
Register_CTextdraw(vm);
Register_CVehicle(vm);
Register_Random(vm);
Register_Hash(vm);
Register_INI(vm);
Register_Random(vm);
Register_Numeric(vm);
//Register_SQLite(vm);
Register_String(vm);