mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
added utils
This commit is contained in:
13
src/makefile
13
src/makefile
@@ -1,10 +1,12 @@
|
|||||||
CXX = g++
|
CXX = g++
|
||||||
CXXFLAGS = -std=c++17 -Wall
|
CXXFLAGS = -std=c++17 -Wall
|
||||||
|
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
|
||||||
|
LIBS = -lmd4c-html
|
||||||
|
|
||||||
all: blog
|
all: blog
|
||||||
|
|
||||||
blog: main.o config.o
|
blog: main.o config.o utils.o
|
||||||
$(CXX) $(CXXFLAGS) main.o config.o -o blog
|
$(CXX) $(CXXFLAGS) main.o config.o -o blog $(LIBS)
|
||||||
|
|
||||||
main.o: main.cpp config.hpp
|
main.o: main.cpp config.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c main.cpp
|
$(CXX) $(CXXFLAGS) -c main.cpp
|
||||||
@@ -12,8 +14,11 @@ main.o: main.cpp config.hpp
|
|||||||
config.o: config.cpp config.hpp
|
config.o: config.cpp config.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c config.cpp
|
$(CXX) $(CXXFLAGS) -c config.cpp
|
||||||
|
|
||||||
tests: tests.o config.o
|
utils.o: utils.cpp utils.hpp
|
||||||
g++ -std=c++17 -Wall tests.o config.o -o tests -lCatch2Main -lCatch2
|
$(CXX) $(CXXFLAGS) -c utils.cpp
|
||||||
|
|
||||||
|
tests: tests.o config.o utils.o
|
||||||
|
g++ -std=c++17 -Wall tests.o config.o utils.o -o tests $(TEST_LIBS)
|
||||||
|
|
||||||
tests.o: tests.cpp config.hpp
|
tests.o: tests.cpp config.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c tests.cpp
|
$(CXX) $(CXXFLAGS) -c tests.cpp
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
#include <catch2/catch_all.hpp>
|
#include <catch2/catch_all.hpp>
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -14,3 +15,9 @@ TEST_CASE("Checking config file read", "[config]") {
|
|||||||
REQUIRE(config.general.lang == "en");
|
REQUIRE(config.general.lang == "en");
|
||||||
REQUIRE(config.general.title == "Blog");
|
REQUIRE(config.general.title == "Blog");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("convert_string_to_set parses inline YAML string list") {
|
||||||
|
const std::string input = "[\"haha\", \"dupa\", \"xdd\"]";
|
||||||
|
const std::set<std::string> output = {"haha", "dupa", "xdd"};
|
||||||
|
REQUIRE(convert_string_to_set(input) == output);
|
||||||
|
}
|
||||||
|
|||||||
37
src/utils.cpp
Normal file
37
src/utils.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include <set>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void trim(std::string &str) {
|
||||||
|
str.erase(str.find_last_not_of(' ') + 1);
|
||||||
|
str.erase(0, str.find_first_not_of(' '));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::set<std::string> convert_string_to_set(std::string input) {
|
||||||
|
std::set<std::string> result;
|
||||||
|
std::string buffer;
|
||||||
|
size_t left, right;
|
||||||
|
left = input.find_first_of('[');
|
||||||
|
right = input.find_last_of(']');
|
||||||
|
|
||||||
|
if (left == std::string::npos || right == std::string::npos ||
|
||||||
|
right <= left) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
input = input.substr(left + 1, right - left - 1);
|
||||||
|
|
||||||
|
std::stringstream ss(input);
|
||||||
|
|
||||||
|
while (getline(ss, buffer, ',')) {
|
||||||
|
buffer.erase(0, buffer.find_first_not_of(" \t"));
|
||||||
|
buffer.erase(buffer.find_last_not_of(" \t") + 1);
|
||||||
|
|
||||||
|
if (buffer.size() >= 2 && buffer.front() == '"' && buffer.back() == '"') {
|
||||||
|
buffer = buffer.substr(1, buffer.size() - 2);
|
||||||
|
result.insert(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
21
src/utils.hpp
Normal file
21
src/utils.hpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// trim string in-place, removing all whitespaces
|
||||||
|
void trim(std::string &str);
|
||||||
|
|
||||||
|
// Convert a string containing an inline YAML/JSON-style list of strings
|
||||||
|
// into a std::set<std::string>.
|
||||||
|
//
|
||||||
|
// Supported format (single line):
|
||||||
|
// ["haha", "dupa", "xdd"]
|
||||||
|
//
|
||||||
|
// Limitations:
|
||||||
|
// - no nested structures
|
||||||
|
// - no escaping inside strings
|
||||||
|
// - elements must be quoted
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
// Input: ["haha", "dupa", "xdd"]
|
||||||
|
// Output: {"dupa", "haha", "xdd"} // sorted, unique
|
||||||
|
std::set<std::string> convert_string_to_set(std::string input);
|
||||||
Reference in New Issue
Block a user