Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/brofield/simpleini.git in…
Browse files Browse the repository at this point in the history
…to aseprite
  • Loading branch information
dacap committed Jan 4, 2016
2 parents d4a436a + e850d4e commit 0687587
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
69 changes: 61 additions & 8 deletions SimpleIni.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
SI_STRLESS class, or by sorting the strings external to this library.
- Usage of the <mbstring.h> header on Windows can be disabled by defining
SI_NO_MBCS. This is defined automatically on Windows CE platforms.
- Not thread-safe so manage your own locking
@section contrib CONTRIBUTIONS
Expand Down Expand Up @@ -213,6 +214,7 @@
#endif

#include <cstring>
#include <cstdlib>
#include <string>
#include <map>
#include <list>
Expand Down Expand Up @@ -1064,8 +1066,8 @@ class CSimpleIniTempl
data returned by GetSection is invalid and must not be used after
anything has been deleted from that section using this method.
Note when multiple keys is enabled, this will delete all keys with
that name; there is no way to selectively delete individual key/values
in this situation.
that name; to selectively delete individual key/values, use
DeleteValue.
@param a_pSection Section to delete key from, or if
a_pKey is NULL, the section to remove.
Expand All @@ -1084,6 +1086,33 @@ class CSimpleIniTempl
bool a_bRemoveEmpty = false
);

/** Delete an entire section, or a key from a section. If value is
provided, only remove keys with the value. Note that the data
returned by GetSection is invalid and must not be used after
anything has been deleted from that section using this method.
Note when multiple keys is enabled, all keys with the value will
be deleted.
@param a_pSection Section to delete key from, or if
a_pKey is NULL, the section to remove.
@param a_pKey Key to remove from the section. Set to
NULL to remove the entire section.
@param a_pValue Value of key to remove from the section.
Set to NULL to remove all keys.
@param a_bRemoveEmpty If the section is empty after this key has
been deleted, should the empty section be
removed?
@return true Key/value or section was deleted.
@return false Key/value or section was not found.
*/
bool DeleteValue(
const SI_CHAR * a_pSection,
const SI_CHAR * a_pKey,
const SI_CHAR * a_pValue,
bool a_bRemoveEmpty = false
);

/*-----------------------------------------------------------------------*/
/** @}
@{ @name Converter */
Expand Down Expand Up @@ -1358,7 +1387,7 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadFile(
}

// allocate and ensure NULL terminated
char * pData = new char[lSize+1];
char * pData = new(std::nothrow) char[lSize+1];
if (!pData) {
return SI_NOMEM;
}
Expand Down Expand Up @@ -1407,7 +1436,7 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadData(

// allocate memory for the data, ensure that there is a NULL
// terminator wherever the converted data ends
SI_CHAR * pData = new SI_CHAR[uLen+1];
SI_CHAR * pData = new(std::nothrow) SI_CHAR[uLen+1];
if (!pData) {
return SI_NOMEM;
}
Expand Down Expand Up @@ -1832,7 +1861,7 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::CopyString(
for ( ; a_pString[uLen]; ++uLen) /*loop*/ ;
}
++uLen; // NULL character
SI_CHAR * pCopy = new SI_CHAR[uLen];
SI_CHAR * pCopy = new(std::nothrow) SI_CHAR[uLen];
if (!pCopy) {
return SI_NOMEM;
}
Expand Down Expand Up @@ -2527,6 +2556,18 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::Delete(
const SI_CHAR * a_pKey,
bool a_bRemoveEmpty
)
{
return DeleteValue(a_pSection, a_pKey, NULL, a_bRemoveEmpty);
}

template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
bool
CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::DeleteValue(
const SI_CHAR * a_pSection,
const SI_CHAR * a_pKey,
const SI_CHAR * a_pValue,
bool a_bRemoveEmpty
)
{
if (!a_pSection) {
return false;
Expand All @@ -2544,18 +2585,30 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::Delete(
return false;
}

const static SI_STRLESS isLess = SI_STRLESS();

// remove any copied strings and then the key
typename TKeyVal::iterator iDelete;
bool bDeleted = false;
do {
iDelete = iKeyVal++;

DeleteString(iDelete->first.pItem);
DeleteString(iDelete->second);
iSection->second.erase(iDelete);
if(a_pValue == NULL ||
(isLess(a_pValue, iDelete->second) == false &&
isLess(iDelete->second, a_pValue) == false)) {
DeleteString(iDelete->first.pItem);
DeleteString(iDelete->second);
iSection->second.erase(iDelete);
bDeleted = true;
}
}
while (iKeyVal != iSection->second.end()
&& !IsLess(a_pKey, iKeyVal->first.pItem));

if(!bDeleted) {
return false;
}

// done now if the section is not empty or we are not pruning away
// the empty sections. Otherwise let it fall through into the section
// deletion code
Expand Down
9 changes: 7 additions & 2 deletions snippets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ snippets(

// DELETING DATA

// deleting a key from a section. Optionally the entire
// section may be deleted if it is now empty.
// deleting a key with a value from a section.
// Optionally the entire section may be deleted if
// it is now empty.
ini.DeleteValue("section-name", "key-name", "value",
true /*delete the section if empty*/);

// deleting a key with any value from a section.
ini.Delete("section-name", "key-name",
true /*delete the section if empty*/);

Expand Down
6 changes: 6 additions & 0 deletions testsi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ Test(
_tprintf(_T("\n-- Number of keys in section [standard] = %d\n"),
ini.GetSectionSize(_T("standard")));

// delete the key "foo" in section "standard", if it has value "bar"
ini.DeleteValue(_T("standard"), _T("foo"), _T("bar"));
pszVal = ini.GetValue(_T("standard"), _T("foo"), 0);
_tprintf(_T("\n-- Value of standard::foo is now '%s'\n"),
pszVal ? pszVal : _T("(null)"));

// delete the key "foo" in section "standard"
ini.Delete(_T("standard"), _T("foo"));
pszVal = ini.GetValue(_T("standard"), _T("foo"), 0);
Expand Down

0 comments on commit 0687587

Please sign in to comment.