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