diff options
Diffstat (limited to 'utility')
-rw-r--r-- | utility/Utility.cpp | 48 | ||||
-rw-r--r-- | utility/Utility.h | 28 |
2 files changed, 60 insertions, 16 deletions
diff --git a/utility/Utility.cpp b/utility/Utility.cpp index afedc07..ccf3bc6 100644 --- a/utility/Utility.cpp +++ b/utility/Utility.cpp @@ -24,23 +24,43 @@ #include "Utility.h" -// Concatenate string list -void CUtility::concatenate(const std::list<std::string>& lstr, std::string& strOutput, const std::string& strSeparator) +#include <sstream> +#include <iterator> + +// Format string list +void CUtility::asString(const std::list<std::string>& lstr, + std::string& strOutput, + const std::string& strSeparator) { - bool bStart = true; + std::ostringstream ostrFormatedList; + + std::copy(lstr.begin(), lstr.end(), + std::ostream_iterator<std::string>(ostrFormatedList, strSeparator.c_str())); - std::list<std::string>::const_iterator iterator(lstr.begin()); - std::list<std::string>::const_iterator end(lstr.end()); + strOutput = ostrFormatedList.str(); - while (iterator != end) { + // Remove last separator + if (strOutput.size() > strSeparator.size()) { - if (bStart) { - // Do not add a separator before first element - bStart = false; - } else { - // Add the separator bettween each element - strOutput += strSeparator; - } - strOutput += *iterator++; + strOutput.erase(strOutput.size() - strSeparator.size()); } } + +// Format string map +void CUtility::asString(const std::map<std::string, std::string>& mapStr, + std::string& strOutput, + const std::string& strItemSeparator, + const std::string& strKeyValueSeparator) +{ + std::list<std::string> listKeysValues; + + std::map<std::string, std::string>::const_iterator iter; + + for(iter = mapStr.begin(); iter != mapStr.end(); ++iter) { + + listKeysValues.push_back(iter->first + strKeyValueSeparator + iter->second); + } + + CUtility::asString(listKeysValues, strOutput, strItemSeparator); +} + diff --git a/utility/Utility.h b/utility/Utility.h index 7c395c0..ec82278 100644 --- a/utility/Utility.h +++ b/utility/Utility.h @@ -27,12 +27,36 @@ #include <string> #include <list> +#include <map> class CUtility { public: - // Concatenate string list - static void concatenate(const std::list<std::string>& lstr, std::string& strOutput, const std::string& separator = "\n"); + /** + * Format the items of a map into a string as a list of key-value pairs. The map must be + * composed of pairs of strings. + * + * @param[in] mapStr A map of strings + * @param[out] strOutput The output string + * @param[in] separator The separator to use between each item + */ + static void asString(const std::list<std::string>& lstr, + std::string& strOutput, + const std::string& separator = "\n"); + + /** + * Format the items of a map into a string as a list of key-value pairs. The map must be + * composed of pairs of strings. + * + * @param[in] mapStr A map of strings + * @param[out] strOutput The output string + * @param[in] strItemSeparator The separator to use between each item (key-value pair) + * @param[in] strKeyValueSeparator The separator to use between key and value + */ + static void asString(const std::map<std::string, std::string>& mapStr, + std::string& strOutput, + const std::string& strItemSeparator = ", ", + const std::string& strKeyValueSeparator = ":"); }; #endif // UTILITY_H |