Utility.cpp revision a9be2d378b7ad84e679a48efa81f42fb54f85d9a
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>
35a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi#include <stdint.h>
36390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
37d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalveusing std::string;
38d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalve
39390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard// Format string list
40390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnardvoid CUtility::asString(const std::list<std::string>& lstr,
41390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        std::string& strOutput,
42390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        const std::string& strSeparator)
43ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard{
44390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::ostringstream ostrFormatedList;
45390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
46390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::copy(lstr.begin(), lstr.end(),
47390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard              std::ostream_iterator<std::string>(ostrFormatedList, strSeparator.c_str()));
48ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
49390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    strOutput = ostrFormatedList.str();
50ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
51390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    // Remove last separator
52390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    if (strOutput.size() > strSeparator.size()) {
53ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard
54390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard        strOutput.erase(strOutput.size() - strSeparator.size());
55ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard    }
56ace81f873b910493ab884dc5a6a38ba6ec3d56d3Kevin Rocard}
57390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
58390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard// Format string map
59390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnardvoid CUtility::asString(const std::map<std::string, std::string>& mapStr,
60390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        std::string& strOutput,
61390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        const std::string& strItemSeparator,
62390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard                        const std::string& strKeyValueSeparator)
63390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard{
64390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::list<std::string> listKeysValues;
65390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
66390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    std::map<std::string, std::string>::const_iterator iter;
67390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
68390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    for(iter = mapStr.begin(); iter != mapStr.end(); ++iter) {
69390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
70390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard        listKeysValues.push_back(iter->first + strKeyValueSeparator + iter->second);
71390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    }
72390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
73390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    CUtility::asString(listKeysValues, strOutput, strItemSeparator);
74390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard}
75390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
76a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivivoid CUtility::appendTitle(string& strTo, const string& strTitle)
77a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi{
78a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    strTo += "\n" + strTitle + "\n";
79a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi
80a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    uint32_t uiLength = strTitle.size();
81a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi
82a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    while (uiLength--) {
83a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi
84a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi        strTo += "=";
85a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    }
86a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    strTo += "\n";
87a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi}
88