Skip to content

Commit

Permalink
[ssg]: Use C++ strings for text handling (#18596)
Browse files Browse the repository at this point in the history
Replace char* and C string functions with C++ strings.
  • Loading branch information
theasianpianist committed Apr 9, 2024
1 parent 6bfb85a commit 1e1876a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 177 deletions.
16 changes: 9 additions & 7 deletions src/systemd-sonic-generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ CC=gcc
CFLAGS += -std=gnu99 -D_GNU_SOURCE

CXX=g++
CXXFLAGS += -std=c++11 -D_GNU_SOURCE
LDFLAGS += -lpthread -lboost_filesystem -lboost_system -lgtest
CXXFLAGS += -std=c++11 -D_GNU_SOURCE -I ./
LDFLAGS += -l stdc++ -lpthread -lboost_filesystem -lboost_system -lgtest

BINARY = systemd-sonic-generator

$(BINARY): systemd-sonic-generator.c
$(BINARY): systemd-sonic-generator.cpp
rm -f ./systemd-sonic-generator

$(CC) $(CFLAGS) -o $@ $^
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

install: $(BINARY)
mkdir -p $(DESTDIR)
Expand All @@ -24,10 +24,12 @@ test: ssg_test
./ssg_test

ssg_test: ssg-test.cc systemd-sonic-generator.o
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
$(CXX) $(CXXFLAGS) -ggdb -o $@ $^ $(LDFLAGS)

systemd-sonic-generator.o: systemd-sonic-generator.cpp
$(CXX) $(CXXFLAGS) -ggdb -D_SSG_UNITTEST -o $@ -c $^

systemd-sonic-generator.o: systemd-sonic-generator.c
$(CC) $(CFLAGS) -D_SSG_UNITTEST -o $@ -c $^
all: $(BINARY) test

clean:
rm -f ./systemd-sonic-generator
Expand Down
11 changes: 4 additions & 7 deletions src/systemd-sonic-generator/ssg-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ class SsgMainTest : public SsgFunctionTest {

/* Find a string in a file */
bool find_string_in_file(std::string str,
std::string file_name,
int num_asics) {
std::string file_name) {
bool found = false;
std::string line;

Expand Down Expand Up @@ -214,7 +213,7 @@ class SsgMainTest : public SsgFunctionTest {
/* Run once for single instance */
finished = true;
}
EXPECT_EQ(find_string_in_file(str_t, target, num_asics),
EXPECT_EQ(find_string_in_file(str_t, target),
expected_result)
<< "Error validating " + str_t + " in " + target;
}
Expand Down Expand Up @@ -460,9 +459,8 @@ TEST_F(SsgFunctionTest, insert_instance_number) {
char input[] = "[email protected]";
for (int i = 0; i <= 100; ++i) {
std::string out = "test@" + std::to_string(i) + ".service";
char* ret = insert_instance_number(input, i);
ASSERT_NE(ret, nullptr);
EXPECT_STREQ(ret, out.c_str());
std::string ret = insert_instance_number(input, i);
EXPECT_EQ(ret, out);
}
}

Expand Down Expand Up @@ -513,7 +511,6 @@ TEST_F(SsgFunctionTest, get_unit_files) {

/* TEST ssg_main() argv error */
TEST_F(SsgMainTest, ssg_main_argv) {
FILE* fp;
std::vector<char*> argv_;
std::vector<std::string> arguments = {
"ssg_main",
Expand Down
Loading

0 comments on commit 1e1876a

Please sign in to comment.