Fix parsing in settings file reader
- parse stringstream after "=" to the end Change-Id: Idfc7d9770630d6bd044508a1d0d1bc1fc1cdc7af Signed-off-by: Mateusz Hoppe <mateusz.hoppe@intel.com>
This commit is contained in:
parent
7e3b6d2d90
commit
54b0ac2f5c
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2017-2018 Intel Corporation
|
||||
* Copyright (C) 2017-2019 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
|
@ -20,38 +20,7 @@ SettingsFileReader::SettingsFileReader(const char *filePath) {
|
|||
settingsFile.open(filePath);
|
||||
|
||||
if (settingsFile.is_open()) {
|
||||
|
||||
stringstream ss;
|
||||
string key;
|
||||
int32_t value = 0;
|
||||
char temp = 0;
|
||||
|
||||
while (!settingsFile.eof()) {
|
||||
string tempString;
|
||||
string tempStringValue;
|
||||
getline(settingsFile, tempString);
|
||||
|
||||
ss << tempString;
|
||||
ss >> key;
|
||||
ss >> temp;
|
||||
ss >> value;
|
||||
if (!ss.fail()) {
|
||||
settingValueMap.insert(pair<string, int32_t>(key, value));
|
||||
} else {
|
||||
stringstream ss2;
|
||||
ss2 << tempString;
|
||||
ss2 >> key;
|
||||
ss2 >> temp;
|
||||
ss2 >> tempStringValue;
|
||||
if (!ss2.fail())
|
||||
settingStringMap.insert(pair<string, string>(key, tempStringValue));
|
||||
}
|
||||
|
||||
ss.str(string()); // for reset string inside stringstream
|
||||
ss.clear();
|
||||
key.clear();
|
||||
}
|
||||
|
||||
parseStream(settingsFile);
|
||||
settingsFile.close();
|
||||
}
|
||||
}
|
||||
|
@ -86,4 +55,39 @@ std::string SettingsFileReader::getSetting(const char *settingName, const std::s
|
|||
const char *SettingsFileReader::appSpecificLocation(const std::string &name) {
|
||||
return name.c_str();
|
||||
}
|
||||
|
||||
void SettingsFileReader::parseStream(std::istream &inputStream) {
|
||||
stringstream ss;
|
||||
string key;
|
||||
int32_t value = 0;
|
||||
char temp = 0;
|
||||
|
||||
while (!inputStream.eof()) {
|
||||
string tempString;
|
||||
string tempStringValue;
|
||||
getline(inputStream, tempString);
|
||||
|
||||
ss << tempString;
|
||||
ss >> key;
|
||||
ss >> temp;
|
||||
ss >> value;
|
||||
|
||||
bool isEnd = ss.eof();
|
||||
if (!ss.fail() && isEnd) {
|
||||
settingValueMap.insert(pair<string, int32_t>(key, value));
|
||||
} else {
|
||||
stringstream ss2;
|
||||
ss2 << tempString;
|
||||
ss2 >> key;
|
||||
ss2 >> temp;
|
||||
ss2 >> tempStringValue;
|
||||
if (!ss2.fail())
|
||||
settingStringMap.insert(pair<string, string>(key, tempStringValue));
|
||||
}
|
||||
|
||||
ss.str(string()); // for reset string inside stringstream
|
||||
ss.clear();
|
||||
key.clear();
|
||||
}
|
||||
}
|
||||
}; // namespace OCLRT
|
||||
|
|
|
@ -14,8 +14,6 @@
|
|||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace OCLRT {
|
||||
|
||||
class SettingsFileReader : public SettingsReader {
|
||||
|
@ -28,6 +26,7 @@ class SettingsFileReader : public SettingsReader {
|
|||
const char *appSpecificLocation(const std::string &name) override;
|
||||
|
||||
protected:
|
||||
void parseStream(std::istream &inputStream);
|
||||
std::map<std::string, int32_t> settingValueMap;
|
||||
std::map<std::string, std::string> settingStringMap;
|
||||
};
|
||||
|
|
|
@ -19,6 +19,8 @@ using namespace std;
|
|||
|
||||
class TestSettingsFileReader : public SettingsFileReader {
|
||||
public:
|
||||
using SettingsFileReader::parseStream;
|
||||
|
||||
TestSettingsFileReader(const char *filePath = nullptr) : SettingsFileReader(filePath) {
|
||||
}
|
||||
|
||||
|
@ -33,7 +35,6 @@ class TestSettingsFileReader : public SettingsFileReader {
|
|||
return settingStringMap.size();
|
||||
}
|
||||
|
||||
public:
|
||||
static const char *testPath;
|
||||
static const char *stringTestPath;
|
||||
};
|
||||
|
@ -123,3 +124,21 @@ TEST(SettingsFileReader, appSpecificLocation) {
|
|||
std::string appSpecific = "cl_cache_dir";
|
||||
EXPECT_EQ(appSpecific, reader->appSpecificLocation(appSpecific));
|
||||
}
|
||||
|
||||
TEST(SettingsFileReader, givenHexNumbersSemiColonSeparatedListInInputStreamWhenParsingThenCorrectStringValueIsStored) {
|
||||
std::unique_ptr<TestSettingsFileReader> reader = unique_ptr<TestSettingsFileReader>(new TestSettingsFileReader());
|
||||
ASSERT_NE(nullptr, reader);
|
||||
|
||||
//No settings should be parsed initially
|
||||
EXPECT_EQ(0u, reader->getValueSettingsCount());
|
||||
EXPECT_EQ(0u, reader->getStringSettingsCount());
|
||||
|
||||
stringstream inputLineWithSemiColonList("KeyName = 0x1234;0x5555");
|
||||
|
||||
reader->parseStream(inputLineWithSemiColonList);
|
||||
|
||||
string defaultStringValue = "FailedToParse";
|
||||
string returnedStringValue = reader->getSetting("KeyName", defaultStringValue);
|
||||
|
||||
EXPECT_STREQ("0x1234;0x5555", returnedStringValue.c_str());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue