Skip to content

Commit

Permalink
fix read config
Browse files Browse the repository at this point in the history
Signed-off-by: Ziy1-Tan <[email protected]>
  • Loading branch information
Ziy1-Tan committed Nov 17, 2023
1 parent a22e6d1 commit 5692311
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/common/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
*/

#include "src/common/configuration.h"
#include "src/common/string_util.h"

#include <fstream>
#include <glog/logging.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>

namespace curve {
namespace common {
Expand All @@ -38,16 +38,17 @@ bool Configuration::LoadConfig() {
if (cFile.is_open()) {
std::string line;
while (getline(cFile, line)) {
// FIXME: may not remove middle spaces
line.erase(std::remove_if(line.begin(), line.end(), isspace),
line.end());
Trim(line);
if (line.empty() || line[0] == '#') continue;

int delimiterPos = line.find("=");
std::string key = line.substr(0, delimiterPos);
Trim(key);

int commentPos = line.find("#");
std::string value = line.substr(delimiterPos + 1,
commentPos - delimiterPos - 1);
Trim(value);
SetValue(key, value);
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <glog/logging.h>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>

namespace curve {
Expand Down Expand Up @@ -165,6 +166,27 @@ inline std::string ToHexString(void* p) {
return oss.str();
}

// trim from start (in place)
inline void LTrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](const char &ch) {
return !std::isspace(ch);
}));
}

// trim from end (in place)
inline void RTrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](const char &ch) { return !std::isspace(ch); })
.base(),
s.end());
}

// trim from both ends (in place)
inline void Trim(std::string &s) {
LTrim(s);
RTrim(s);
}

} // namespace common
} // namespace curve

Expand Down
51 changes: 50 additions & 1 deletion test/common/string_util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace curve {
namespace common {

TEST(Common, SpliteString) {
TEST(Common, SplitString) {
const struct {std::string path; std::string sep;
const int size; std::vector<std::string> items;}
testCases[] = {
Expand Down Expand Up @@ -59,5 +59,54 @@ TEST(Common, StringToUll) {
str = "ffff";
ASSERT_FALSE(StringToUll(str, &out));
}

TEST(StringUtilTest, LTrim) {
std::array<std::array<std::string, 2>, 4> cases = {
std::array<std::string, 2>{"hello", "hello"},
std::array<std::string, 2>{"", ""},
std::array<std::string, 2>{" ", ""},
std::array<std::string, 2>{" hello", "hello"},
};

for (auto &c : cases) {
curve::common::LTrim(c[0]);
EXPECT_EQ(c[0], c[1]);
}
}

TEST(StringUtilTest, RTrim) {
std::array<std::array<std::string, 2>, 4> cases = {
std::array<std::string, 2>{"hello", "hello"},
std::array<std::string, 2>{"", ""},
std::array<std::string, 2>{" ", ""},
std::array<std::string, 2>{"hello ", "hello"},
};

for (auto &c : cases) {
curve::common::RTrim(c[0]);
EXPECT_EQ(c[0], c[1]);
}
}

TEST(StringUtilTest, Trim) {
std::array<std::array<std::string, 2>, 10> cases = {
std::array<std::string, 2>{"hello", "hello"},
std::array<std::string, 2>{" hello", "hello"},
std::array<std::string, 2>{"hello ", "hello"},
std::array<std::string, 2>{" hello ", "hello"},
std::array<std::string, 2>{"S3 Browser", "S3 Browser"},
std::array<std::string, 2>{"S3 Browser ", "S3 Browser"},
std::array<std::string, 2>{" S3 Browser", "S3 Browser"},
std::array<std::string, 2>{" S3 Browser ", "S3 Browser"},
std::array<std::string, 2>{" ", ""},
std::array<std::string, 2>{"", ""},
};

for (auto &c : cases) {
curve::common::Trim(c[0]);
EXPECT_EQ(c[0], c[1]);
}
}

} // namespace common
} // namespace curve

0 comments on commit 5692311

Please sign in to comment.