Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions parameter/MappingData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ bool CMappingData::getValue(const std::string& strkey, const std::string*& pStrV

std::string CMappingData::asString() const
{
std::string strValue;

CUtility::asString(_keyToValueMap, strValue, ", ", ":");

return strValue;
return CUtility::asString(_keyToValueMap, ", ", ":");
}

bool CMappingData::addValue(const std::string& strkey, const std::string& strValue)
Expand Down
6 changes: 2 additions & 4 deletions parameter/SystemClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ bool CSystemClass::loadSubsystemsFromSharedLibraries(core::Results& errors,

if (!lstrPluginFiles.empty()) {
// Unable to load at least one plugin
string strPluginUnloaded;
CUtility::asString(lstrPluginFiles, strPluginUnloaded, ", ");

errors.push_back("Unable to load the following plugins: " + strPluginUnloaded + ".");
errors.push_back("Unable to load the following plugins: " +
CUtility::asString(lstrPluginFiles, ", ") + ".");
return false;
}

Expand Down
3 changes: 1 addition & 2 deletions parameter/log/include/log/LogWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ class LogWrapper
*/
LogWrapper& operator<<(const std::list<std::string>& logs)
{
std::string formatedLogs;
std::string separator = "\n" + mProlog;
CUtility::asString(logs, formatedLogs, separator);
std::string formatedLogs = CUtility::asString(logs, separator);

// Check if there is something in the log to know if we have to add a prefix
if (!mLog.str().empty() && mLog.str()[mLog.str().length() - 1] == separator[0]) {
Expand Down
10 changes: 2 additions & 8 deletions remote-processor/RemoteCommandHandlerTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,11 @@ class TRemoteCommandHandlerTemplate : public IRemoteCommandHandler

std::string strUsage = pRemoteCommandParserItem->usage();

strResult += strUsage;

// Align
size_t spacesToAdd = _maxCommandUsageLength + 5 - strUsage.length();

while (spacesToAdd--) {

strResult += " ";
}

strResult += std::string("=> ") + std::string(pRemoteCommandParserItem->getDescription()) + "\n";
strResult += strUsage + std::string(spacesToAdd, ' ') + "=> " +
pRemoteCommandParserItem->getDescription() + '\n';
}
}

Expand Down
4 changes: 1 addition & 3 deletions test/test-platform/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ int main(int argc, char *argv[])

// All arguments should have been consumed
if (not options.empty()) {
std::string extraArgs;
CUtility::asString(options, extraArgs);
showInvalidUsage("Unexpected extra arguments: " + extraArgs);
showInvalidUsage("Unexpected extra arguments: " + CUtility::asString(options));
return 3;
}

Expand Down
12 changes: 12 additions & 0 deletions utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ install(FILES
Utility.h
convert.hpp
DESTINATION "include/utility")

if(BUILD_TESTING)
# Add unit test
add_executable(utilityUnitTest test/utility.cpp)

target_link_libraries(utilityUnitTest pfw_utility catch)
add_test(NAME utilityUnitTest
COMMAND utilityUnitTest)

# Custom function defined in the top-level CMakeLists
set_test_env(utilityUnitTest)
endif()
48 changes: 14 additions & 34 deletions utility/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,38 @@

#include <sstream>
#include <iterator>
#include <stdint.h>
#include <algorithm>

using std::string;

// Format string list
void CUtility::asString(const std::list<std::string>& lstr,
std::string& strOutput,
std::string CUtility::asString(const std::list<std::string>& lstr,
const std::string& strSeparator)
{
std::ostringstream ostrFormatedList;

std::copy(lstr.begin(), lstr.end(),
std::ostream_iterator<std::string>(ostrFormatedList, strSeparator.c_str()));

strOutput = ostrFormatedList.str();

// Remove last separator
if (strOutput.size() > strSeparator.size()) {

strOutput.erase(strOutput.size() - strSeparator.size());
}
return accumulate1<std::string>(begin(lstr), end(lstr),
[strSeparator](string acc, string right) {
return acc + strSeparator + right;
});
}

// 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::string CUtility::asString(const std::map<std::string, std::string>& mapStr,
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);
for (const auto & item: mapStr) {
listKeysValues.emplace_back(item.first + strKeyValueSeparator + item.second);
}

CUtility::asString(listKeysValues, strOutput, strItemSeparator);
return asString(listKeysValues, strItemSeparator);
}

void CUtility::appendTitle(string& strTo, const string& strTitle)
{
strTo += "\n" + strTitle + "\n";

size_t uiLength = strTitle.size();

while (uiLength--) {

strTo += "=";
}
strTo += "\n";
strTo += "\n" + strTitle + "\n" +
string(strTitle.size(), '=') + "\n";
}

bool CUtility::isHexadecimal(const string& strValue)
Expand Down
19 changes: 14 additions & 5 deletions utility/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,30 @@
#include <list>
#include <map>
#include <sstream>
#include <algorithm>

class CUtility
{
public:
template<class T, class InputIt, class BinaryOperation>
static T accumulate1(InputIt first, InputIt last, BinaryOperation op, T empty = T{})
{
if (first == last) { return empty; }
auto init = *first++;

return std::accumulate(first, last, init, op);
}

/**
* 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
*
* @return the concatenated elements.
*/
static void asString(const std::list<std::string>& lstr,
std::string& strOutput,
static std::string asString(const std::list<std::string>& lstr,
const std::string& separator = "\n");

/**
Expand All @@ -59,8 +69,7 @@ class CUtility
* @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,
static std::string asString(const std::map<std::string, std::string>& mapStr,
const std::string& strItemSeparator = ", ",
const std::string& strKeyValueSeparator = ":");

Expand Down
172 changes: 172 additions & 0 deletions utility/test/utility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2011-2014, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "Utility.h"
#include <map>
#include <catch.hpp>

using std::list;
using std::string;

SCENARIO("accumulate1<int>") {
struct Test
{
list<int> input;
std::function<int (int, int)> binaryOpt;
int empty;
int result;
int resultNoEmpty;
};
const list<Test> tests =
{
{{}, nullptr, 21, 21, 0},
{{5}, nullptr, -1, 5, 5},
{{5, 2}, [](int, int){return 73;}, -1, 73, 73},
{{2, 3, 7}, [](int l, int r){return l * r ;}, -1, 42, 42},
{{1, 10, 100}, [](int l, int r){return l + r ;}, -1, 111, 111}
};
for (auto &test : tests) {
CAPTURE(Catch::toString(test.input));
const auto &first = begin(test.input);
const auto &last = end(test.input);
REQUIRE(CUtility::accumulate1(first, last, test.binaryOpt, test.empty) == test.result);
REQUIRE(CUtility::accumulate1<int>(first, last, test.binaryOpt) == test.resultNoEmpty);
}
}


SCENARIO("asString(list)") {
struct Test
{
string title;
list<string> input;
string separator;
string result;
string resultNoSep;
};
const list<Test> tests =
{
{"Empty list", {}, "aa", "", ""},
{"One element", {"a"}, "<>", "a", "a"},
{"Three elem list", {"1", "2", "3"}, "**", "1**2**3", "1\n2\n3"},
{"No separator", {"12", "ab", "+-"}, "", "12ab+-", "12\nab\n+-"},
{"empty elem list", {"a", "b", "", "d"}, "|", "a|b||d", "a\nb\n\nd"},
};
for (auto &test : tests) {
CAPTURE(Catch::toString(test.input));
WHEN("Separator, " + test.title) {
CAPTURE(test.separator);
REQUIRE(CUtility::asString(test.input, test.separator) == test.result);
}
THEN("No separator, " + test.title) {
REQUIRE(CUtility::asString(test.input) == test.resultNoSep);
}
}
}

SCENARIO("asString(map)") {
using std::map;

using Map = map<string, string>;
struct Test
{
Map input;
string itemSep;
string keyValueSep;
string result;
string resultNoKeyValueSep;
string resultNoSep;
};
const list<Test> tests =
{
{{}, "itemSep", "keyValueSep", "", "", ""},
{ Map{{"a", "b"},
{"c", "d"},
{"e", "f"}}, // input
" - ", "\n", // item & keyValue sep
"a - b\nc - d\ne - f", //result
"a:b\nc:d\ne:f", //resultNoKeyValueSep
"a:b, c:d, e:f" //resultNoSep
}
};
for (const auto &test : tests) {
CAPTURE(Catch::toString(test.input));
CAPTURE(test.keyValueSep);
CAPTURE(test.itemSep);
REQUIRE(CUtility::asString(test.input, test.keyValueSep, test.itemSep) == test.result);
REQUIRE(CUtility::asString(test.input, test.keyValueSep) == test.resultNoKeyValueSep);
REQUIRE(CUtility::asString(test.input) == test.resultNoSep);
}

}

std::string quote(std::string toQuote) { return '"' + toQuote + '"'; }

SCENARIO("appendTitle") {
struct Test
{
string initial;
string title;
string result;
};
const list<Test> tests =
{
{"", "abc", "\nabc\n===\n"},
{"start", "title", "start\ntitle\n=====\n"}
};
for (auto &test : tests) {
GIVEN("A title: " + quote(test.title)) {
CAPTURE(test.initial);
CAPTURE(test.title);

WHEN("Appending to: " + quote(test.initial)) {
string output = test.initial;
THEN("Result should be:\n" + quote(test.result)) {
CUtility::appendTitle(output, test.title);
CHECK(output == test.result);
}
}
}
}
}

SCENARIO("isNotHexadecimal") {
for (auto &str : {"a", "0", "012", "13", "ABC", "Oxa"}) {
CAPTURE(str);
CHECK(not CUtility::isHexadecimal(str));
}
}

SCENARIO("isHexadecimal") {
for (auto str : {"0xa", "0X0", "0x012", "0x13", "0xConsider as hexa as stariting with 0x"}) {
CAPTURE(str);
CHECK(CUtility::isHexadecimal(str));
}
}