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

Add a method to the INI library to allow processing section keys through a custom functor.

This commit is contained in:
Sandu Liviu Catalin 2016-06-18 20:30:48 +03:00
parent b59710ddeb
commit 54531071c7

View File

@ -164,7 +164,7 @@
- Not thread-safe so manage your own locking - Not thread-safe so manage your own locking
@section contrib CONTRIBUTIONS @section contrib CONTRIBUTIONS
- 2010/05/03: Tobias Gehrig: added GetDoubleValue() - 2010/05/03: Tobias Gehrig: added GetDoubleValue()
@section licence MIT LICENCE @section licence MIT LICENCE
@ -530,7 +530,7 @@ public:
bool IsMultiLine() const { return m_bAllowMultiLine; } bool IsMultiLine() const { return m_bAllowMultiLine; }
/** Should spaces be added around the equals sign when writing key/value /** Should spaces be added around the equals sign when writing key/value
pairs out. When true, the result will be "key = value". When false, pairs out. When true, the result will be "key = value". When false,
the result will be "key=value". This value may be changed at any time. the result will be "key=value". This value may be changed at any time.
\param a_bSpaces Add spaces around the equals sign? \param a_bSpaces Add spaces around the equals sign?
@ -541,7 +541,7 @@ public:
/** Query the status of spaces output */ /** Query the status of spaces output */
bool UsingSpaces() const { return m_bSpaces; } bool UsingSpaces() const { return m_bSpaces; }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/** @} /** @}
@{ @name Loading INI Data */ @{ @name Loading INI Data */
@ -752,6 +752,12 @@ public:
/** @} /** @}
@{ @name Accessing INI Data */ @{ @name Accessing INI Data */
template<class SI_CALLBACK>
bool ProcAllValues(
const SI_CHAR * a_pSection,
SI_CALLBACK callback
) const;
/** Retrieve all section names. The list is returned as an STL vector of /** Retrieve all section names. The list is returned as an STL vector of
names and can be iterated or searched as necessary. Note that the names and can be iterated or searched as necessary. Note that the
sort order of the returned strings is NOT DEFINED. You can sort sort order of the returned strings is NOT DEFINED. You can sort
@ -771,8 +777,8 @@ public:
) const; ) const;
/** Retrieve all unique key names in a section. The sort order of the /** Retrieve all unique key names in a section. The sort order of the
returned strings is NOT DEFINED. You can sort the names into the load returned strings is NOT DEFINED. You can sort the names into the load
order if desired. Search this file for ".sort" for an example. Only order if desired. Search this file for ".sort" for an example. Only
unique key names are returned. unique key names are returned.
NOTE! This structure contains only pointers to strings. The actual NOTE! This structure contains only pointers to strings. The actual
@ -793,8 +799,8 @@ public:
) const; ) const;
/** Retrieve all values for a specific key. This method can be used when /** Retrieve all values for a specific key. This method can be used when
multiple keys are both enabled and disabled. Note that the sort order multiple keys are both enabled and disabled. Note that the sort order
of the returned strings is NOT DEFINED. You can sort the names into of the returned strings is NOT DEFINED. You can sort the names into
the load order if desired. Search this file for ".sort" for an example. the load order if desired. Search this file for ".sort" for an example.
NOTE! The returned values are pointers to string data stored in memory NOTE! The returned values are pointers to string data stored in memory
@ -915,7 +921,7 @@ public:
Strings starting with "t", "y", "on" or "1" are returned as logically true. Strings starting with "t", "y", "on" or "1" are returned as logically true.
Strings starting with "f", "n", "of" or "0" are returned as logically false. Strings starting with "f", "n", "of" or "0" are returned as logically false.
For all other values the default is returned. Character comparisons are For all other values the default is returned. Character comparisons are
case-insensitive. case-insensitive.
@param a_pSection Section to search @param a_pSection Section to search
@ -954,9 +960,9 @@ public:
character starting every line). character starting every line).
@param a_bForceReplace Should all existing values in a multi-key INI @param a_bForceReplace Should all existing values in a multi-key INI
file be replaced with this entry. This option has file be replaced with this entry. This option has
no effect if not using multi-key files. The no effect if not using multi-key files. The
difference between Delete/SetValue and SetValue difference between Delete/SetValue and SetValue
with a_bForceReplace = true, is that the load with a_bForceReplace = true, is that the load
order and comment will be preserved this way. order and comment will be preserved this way.
@return SI_Error See error definitions @return SI_Error See error definitions
@ -978,19 +984,19 @@ public:
when multiple keys are enabled. when multiple keys are enabled.
@param a_pSection Section to add or update @param a_pSection Section to add or update
@param a_pKey Key to add or update. @param a_pKey Key to add or update.
@param a_nValue Value to set. @param a_nValue Value to set.
@param a_pComment Comment to be associated with the key. See the @param a_pComment Comment to be associated with the key. See the
notes on SetValue() for comments. notes on SetValue() for comments.
@param a_bUseHex By default the value will be written to the file @param a_bUseHex By default the value will be written to the file
in decimal format. Set this to true to write it in decimal format. Set this to true to write it
as hexadecimal. as hexadecimal.
@param a_bForceReplace Should all existing values in a multi-key INI @param a_bForceReplace Should all existing values in a multi-key INI
file be replaced with this entry. This option has file be replaced with this entry. This option has
no effect if not using multi-key files. The no effect if not using multi-key files. The
difference between Delete/SetLongValue and difference between Delete/SetLongValue and
SetLongValue with a_bForceReplace = true, is that SetLongValue with a_bForceReplace = true, is that
the load order and comment will be preserved this the load order and comment will be preserved this
way. way.
@return SI_Error See error definitions @return SI_Error See error definitions
@ -1010,16 +1016,16 @@ public:
when multiple keys are enabled. when multiple keys are enabled.
@param a_pSection Section to add or update @param a_pSection Section to add or update
@param a_pKey Key to add or update. @param a_pKey Key to add or update.
@param a_nValue Value to set. @param a_nValue Value to set.
@param a_pComment Comment to be associated with the key. See the @param a_pComment Comment to be associated with the key. See the
notes on SetValue() for comments. notes on SetValue() for comments.
@param a_bForceReplace Should all existing values in a multi-key INI @param a_bForceReplace Should all existing values in a multi-key INI
file be replaced with this entry. This option has file be replaced with this entry. This option has
no effect if not using multi-key files. The no effect if not using multi-key files. The
difference between Delete/SetDoubleValue and difference between Delete/SetDoubleValue and
SetDoubleValue with a_bForceReplace = true, is that SetDoubleValue with a_bForceReplace = true, is that
the load order and comment will be preserved this the load order and comment will be preserved this
way. way.
@return SI_Error See error definitions @return SI_Error See error definitions
@ -1038,16 +1044,16 @@ public:
when multiple keys are enabled. when multiple keys are enabled.
@param a_pSection Section to add or update @param a_pSection Section to add or update
@param a_pKey Key to add or update. @param a_pKey Key to add or update.
@param a_bValue Value to set. @param a_bValue Value to set.
@param a_pComment Comment to be associated with the key. See the @param a_pComment Comment to be associated with the key. See the
notes on SetValue() for comments. notes on SetValue() for comments.
@param a_bForceReplace Should all existing values in a multi-key INI @param a_bForceReplace Should all existing values in a multi-key INI
file be replaced with this entry. This option has file be replaced with this entry. This option has
no effect if not using multi-key files. The no effect if not using multi-key files. The
difference between Delete/SetBoolValue and difference between Delete/SetBoolValue and
SetBoolValue with a_bForceReplace = true, is that SetBoolValue with a_bForceReplace = true, is that
the load order and comment will be preserved this the load order and comment will be preserved this
way. way.
@return SI_Error See error definitions @return SI_Error See error definitions
@ -1168,9 +1174,9 @@ private:
comment character starting every line). comment character starting every line).
@param a_bForceReplace Should all existing values in a multi-key INI @param a_bForceReplace Should all existing values in a multi-key INI
file be replaced with this entry. This option has file be replaced with this entry. This option has
no effect if not using multi-key files. The no effect if not using multi-key files. The
difference between Delete/AddEntry and AddEntry difference between Delete/AddEntry and AddEntry
with a_bForceReplace = true, is that the load with a_bForceReplace = true, is that the load
order and comment will be preserved this way. order and comment will be preserved this way.
@param a_bCopyStrings Should copies of the strings be made or not. @param a_bCopyStrings Should copies of the strings be made or not.
If false then the pointers will be used as is. If false then the pointers will be used as is.
@ -1265,7 +1271,7 @@ private:
/** Should spaces be written out surrounding the equals sign? */ /** Should spaces be written out surrounding the equals sign? */
bool m_bSpaces; bool m_bSpaces;
/** Next order value, used to ensure sections and keys are output in the /** Next order value, used to ensure sections and keys are output in the
same order that they are loaded/added. same order that they are loaded/added.
*/ */
@ -1385,14 +1391,14 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadFile(
if (lSize == 0) { if (lSize == 0) {
return SI_OK; return SI_OK;
} }
// allocate and ensure NULL terminated // allocate and ensure NULL terminated
char * pData = new(std::nothrow) char[lSize+1]; char * pData = new(std::nothrow) char[lSize+1];
if (!pData) { if (!pData) {
return SI_NOMEM; return SI_NOMEM;
} }
pData[lSize] = 0; pData[lSize] = 0;
// load data into buffer // load data into buffer
fseek(a_fpFile, 0, SEEK_SET); fseek(a_fpFile, 0, SEEK_SET);
size_t uRead = fread(pData, sizeof(char), lSize, a_fpFile); size_t uRead = fread(pData, sizeof(char), lSize, a_fpFile);
@ -2045,15 +2051,15 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetLongValue(
} }
// any invalid strings will return the default value // any invalid strings will return the default value
if (*pszSuffix) { if (*pszSuffix) {
return a_nDefault; return a_nDefault;
} }
return nValue; return nValue;
} }
template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER> template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
SI_Error SI_Error
CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetLongValue( CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetLongValue(
const SI_CHAR * a_pSection, const SI_CHAR * a_pSection,
const SI_CHAR * a_pKey, const SI_CHAR * a_pKey,
@ -2077,7 +2083,7 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetLongValue(
// convert to output text // convert to output text
SI_CHAR szOutput[64]; SI_CHAR szOutput[64];
SI_CONVERTER c(m_bStoreIsUtf8); SI_CONVERTER c(m_bStoreIsUtf8);
c.ConvertFromStore(szInput, strlen(szInput) + 1, c.ConvertFromStore(szInput, strlen(szInput) + 1,
szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); szOutput, sizeof(szOutput) / sizeof(SI_CHAR));
// actually add it // actually add it
@ -2108,15 +2114,15 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetDoubleValue(
double nValue = strtod(szValue, &pszSuffix); double nValue = strtod(szValue, &pszSuffix);
// any invalid strings will return the default value // any invalid strings will return the default value
if (!pszSuffix || *pszSuffix) { if (!pszSuffix || *pszSuffix) {
return a_nDefault; return a_nDefault;
} }
return nValue; return nValue;
} }
template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER> template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
SI_Error SI_Error
CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetDoubleValue( CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetDoubleValue(
const SI_CHAR * a_pSection, const SI_CHAR * a_pSection,
const SI_CHAR * a_pKey, const SI_CHAR * a_pKey,
@ -2139,7 +2145,7 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetDoubleValue(
// convert to output text // convert to output text
SI_CHAR szOutput[64]; SI_CHAR szOutput[64];
SI_CONVERTER c(m_bStoreIsUtf8); SI_CONVERTER c(m_bStoreIsUtf8);
c.ConvertFromStore(szInput, strlen(szInput) + 1, c.ConvertFromStore(szInput, strlen(szInput) + 1,
szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); szOutput, sizeof(szOutput) / sizeof(SI_CHAR));
// actually add it // actually add it
@ -2182,7 +2188,7 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetBoolValue(
} }
template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER> template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
SI_Error SI_Error
CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetBoolValue( CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetBoolValue(
const SI_CHAR * a_pSection, const SI_CHAR * a_pSection,
const SI_CHAR * a_pKey, const SI_CHAR * a_pKey,
@ -2200,13 +2206,13 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetBoolValue(
// convert to output text // convert to output text
SI_CHAR szOutput[64]; SI_CHAR szOutput[64];
SI_CONVERTER c(m_bStoreIsUtf8); SI_CONVERTER c(m_bStoreIsUtf8);
c.ConvertFromStore(pszInput, strlen(pszInput) + 1, c.ConvertFromStore(pszInput, strlen(pszInput) + 1,
szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); szOutput, sizeof(szOutput) / sizeof(SI_CHAR));
// actually add it // actually add it
return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true); return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true);
} }
template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER> template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
bool bool
CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetAllValues( CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetAllValues(
@ -2292,6 +2298,40 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetSection(
return 0; return 0;
} }
/*@@@*/
template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
template<class SI_CALLBACK>
bool
CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::ProcAllValues(
const SI_CHAR * a_pSection,
SI_CALLBACK callback
) const
{
typedef typename CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::Entry::LoadOrder TKeyOrder;
typedef std::multimap<Entry,const SI_CHAR *,TKeyOrder> TKeyValOrd;
if (!a_pSection) {
return false;
}
typename TSection::const_iterator iSection = m_data.find(a_pSection);
if (iSection == m_data.end()) {
return false;
}
TKeyValOrd section(iSection->second.begin(), iSection->second.end());
typename TKeyValOrd::const_iterator iKeyVal = section.begin();
for (;iKeyVal != section.end(); ++iKeyVal ) {
if (!callback(iKeyVal->first.pItem, iKeyVal->second)) {
return false; // premature stop
}
}
return true;
}
template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER> template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
void void
CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetAllSections( CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetAllSections(