reading config

This commit is contained in:
2025-12-30 23:00:05 +01:00
parent 6b56833b1a
commit 02e6ba8e57
4 changed files with 69 additions and 0 deletions

25
src/config.cpp Normal file
View 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
View 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
View 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
View 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