mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 20:20:41 +02:00
reading config
This commit is contained in:
25
src/config.cpp
Normal file
25
src/config.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include "config.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <toml++/impl/parse_error.hpp>
|
||||||
|
#include <toml++/impl/parser.hpp>
|
||||||
|
#include <toml++/toml.hpp>
|
||||||
|
|
||||||
|
Config get_config(const std::string &CONFIG_FILE) {
|
||||||
|
toml::table toml_table;
|
||||||
|
try {
|
||||||
|
toml_table = toml::parse_file(CONFIG_FILE);
|
||||||
|
} catch (const toml::parse_error &err) {
|
||||||
|
std::cerr << "Parsing failed:" << std::endl << err << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Contact contact = {
|
||||||
|
toml_table["contact"]["author"].value_or(""),
|
||||||
|
toml_table["contact"]["email"].value_or(""),
|
||||||
|
toml_table["contact"]["signal"].value_or(""),
|
||||||
|
};
|
||||||
|
|
||||||
|
Config config = {contact};
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
14
src/config.hpp
Normal file
14
src/config.hpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct Contact {
|
||||||
|
std::string author;
|
||||||
|
std::string email;
|
||||||
|
std::string signal;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
Contact contact;
|
||||||
|
};
|
||||||
|
|
||||||
|
Config get_config(const std::string &CONFIG_FILE);
|
||||||
14
src/main.cpp
Normal file
14
src/main.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "config.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int main() { return 0; }
|
||||||
|
|
||||||
|
void test_config_read() {
|
||||||
|
const std::string CONFIG_PATH = "../config/config_example.toml";
|
||||||
|
Config config = get_config(CONFIG_PATH);
|
||||||
|
std::cout << "Config" << std::endl;
|
||||||
|
std::cout << "Author: " << config.contact.author << std::endl;
|
||||||
|
std::cout << "Email: " << config.contact.email << std::endl;
|
||||||
|
std::cout << "Signal: " << config.contact.signal << std::endl;
|
||||||
|
}
|
||||||
16
src/makefile
Normal file
16
src/makefile
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
CXX = g++
|
||||||
|
CXXFLAGS = -std=c++17 -Wall
|
||||||
|
|
||||||
|
all: blog
|
||||||
|
|
||||||
|
blog: main.o config.o
|
||||||
|
$(CXX) $(CXXFLAGS) main.o config.o -o blog
|
||||||
|
|
||||||
|
main.o: main.cpp config.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c main.cpp
|
||||||
|
|
||||||
|
config.o: config.cpp config.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c config.cpp
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o blog
|
||||||
Reference in New Issue
Block a user