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 authored and caoxianfei1 committed Nov 20, 2023
1 parent da164c9 commit 0b82af8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/common/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
* 2018/08/30 Wenyu Zhou Initial version
*/

#include "src/common/configuration.h"

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

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

namespace curve {
namespace common {
Expand All @@ -38,16 +39,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) { // NOLINT
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) { // NOLINT
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) { // NOLINT
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 0b82af8

Please sign in to comment.