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 51a6a31 commit df47393
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 179 deletions.
18 changes: 10 additions & 8 deletions src/systemd-sonic-generator/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CC=gcc
CFLAGS=-std=gnu99 -D_GNU_SOURCE

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

BINARY = systemd-sonic-generator
MAIN_TARGET = $(BINARY)_1.0.0_$(CONFIGURED_ARCH).deb
Expand All @@ -13,10 +13,10 @@ $(addprefix $(DEST)/, $(MAIN_TARGET)): $(DEST)/% :
mv ../$(MAIN_TARGET) $(DEST)/
rm ../$(BINARY)-* ../$(BINARY)_*

$(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 @@ -30,10 +30,12 @@ test: ssg_test
./ssg_test

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

systemd-sonic-generator.o: systemd-sonic-generator.c
$(CC) $(CFLAGS) -D_SSG_UNITTEST -o $@ -c $^
systemd-sonic-generator.o: systemd-sonic-generator.cpp
$(CXX) $(CXXFLAGS) -ggdb -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[] = "test@.service";
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 df47393

Please sign in to comment.