From 390b36d8129d3ece769c8542d9d3d3895ab13fbb Mon Sep 17 00:00:00 2001 From: Frederic Boisnard Date: Thu, 23 May 2013 15:28:31 +0200 Subject: [PFW] Add getFormattedMapping for InstanceConfigurableElements BZ: 99822 The mapping data need to be printed, however helper methods are missing to achieve this easily. This patch aims to add a new method "getFormattedMapping()" for CMappingData, allowing to format the mapping data as a string. Change-Id: Ia030c6b88905fcb7591ad45339712051eb88d1c1 Signed-off-by: Frederic Boisnard Reviewed-on: http://android.intel.com:8080/109301 Reviewed-by: Benavoli, Patrick Reviewed-by: De Chivre, Renaud Reviewed-by: Rocard, KevinX Reviewed-by: Denneulin, Guillaume Tested-by: Barthes, FabienX Reviewed-by: buildbot Tested-by: buildbot --- utility/Utility.cpp | 48 ++++++++++++++++++++++++++++++++++-------------- utility/Utility.h | 28 ++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 16 deletions(-) (limited to 'utility') 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& lstr, std::string& strOutput, const std::string& strSeparator) +#include +#include + +// Format string list +void CUtility::asString(const std::list& lstr, + std::string& strOutput, + const std::string& strSeparator) { - bool bStart = true; + std::ostringstream ostrFormatedList; + + std::copy(lstr.begin(), lstr.end(), + std::ostream_iterator(ostrFormatedList, strSeparator.c_str())); - std::list::const_iterator iterator(lstr.begin()); - std::list::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& mapStr, + std::string& strOutput, + const std::string& strItemSeparator, + const std::string& strKeyValueSeparator) +{ + std::list listKeysValues; + + std::map::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 #include +#include class CUtility { public: - // Concatenate string list - static void concatenate(const std::list& 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& 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& mapStr, + std::string& strOutput, + const std::string& strItemSeparator = ", ", + const std::string& strKeyValueSeparator = ":"); }; #endif // UTILITY_H -- cgit v1.1