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