Utility.cpp revision b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7
1ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard/*
2b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * Copyright (c) 2011-2014, Intel Corporation
3b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * All rights reserved.
4ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard *
5b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * Redistribution and use in source and binary forms, with or without modification,
6b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * are permitted provided that the following conditions are met:
7ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard *
8b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 1. Redistributions of source code must retain the above copyright notice, this
9b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * list of conditions and the following disclaimer.
10ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard *
11b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 2. Redistributions in binary form must reproduce the above copyright notice,
12b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * this list of conditions and the following disclaimer in the documentation and/or
13b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * other materials provided with the distribution.
14b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
15b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 3. Neither the name of the copyright holder nor the names of its contributors
16b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * may be used to endorse or promote products derived from this software without
17b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * specific prior written permission.
18b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
19b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard */
30ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
31ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard#include "Utility.h"
32ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
33390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard#include <sstream>
34390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard#include <iterator>
35390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
36390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard// Format string list
37390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnardvoid CUtility::asString(const std::list<std::string>& lstr,
38390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        std::string& strOutput,
39390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        const std::string& strSeparator)
40ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard{
41390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::ostringstream ostrFormatedList;
42390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
43390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::copy(lstr.begin(), lstr.end(),
44390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard              std::ostream_iterator<std::string>(ostrFormatedList, strSeparator.c_str()));
45ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
46390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    strOutput = ostrFormatedList.str();
47ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
48390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    // Remove last separator
49390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    if (strOutput.size() > strSeparator.size()) {
50ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
51390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard        strOutput.erase(strOutput.size() - strSeparator.size());
52ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard    }
53ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard}
54390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
55390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard// Format string map
56390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnardvoid CUtility::asString(const std::map<std::string, std::string>& mapStr,
57390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        std::string& strOutput,
58390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        const std::string& strItemSeparator,
59390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        const std::string& strKeyValueSeparator)
60390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard{
61390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::list<std::string> listKeysValues;
62390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
63390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::map<std::string, std::string>::const_iterator iter;
64390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
65390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    for(iter = mapStr.begin(); iter != mapStr.end(); ++iter) {
66390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
67390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard        listKeysValues.push_back(iter->first + strKeyValueSeparator + iter->second);
68390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    }
69390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
70390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    CUtility::asString(listKeysValues, strOutput, strItemSeparator);
71390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard}
72390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
73