add config support
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
/simple
|
||||
/simplecpp
|
||||
/repro
|
||||
@@ -0,0 +1,59 @@
|
||||
EXEC = simple simplecpp
|
||||
|
||||
# set up CFLAGS / CXXFLAGS
|
||||
CFLAGS = -std=c17 -Wmissing-declarations -Wall -Wextra -MMD
|
||||
|
||||
ifdef DEBUG
|
||||
CFLAGS += -O0 -g -fsanitize=address,undefined
|
||||
else
|
||||
CFLAGS += -O3 -DNDEBUG
|
||||
endif
|
||||
# Add -fpic only for non-Windows platforms
|
||||
ifneq ($(OS), Windows_NT)
|
||||
CFLAGS += -fpic
|
||||
endif
|
||||
|
||||
CXXFLAGS = $(subst -std=c17,-std=c++20,$(CFLAGS))
|
||||
|
||||
# Test if C++ is available
|
||||
HAVE_CXX := $(shell command -v $(CXX) 2> /dev/null)
|
||||
|
||||
# Test for C++20 support
|
||||
CXX20_SUPPORT := $(shell echo "int main(){}" | $(CXX) -std=c++20 -x c++ -c - -o /dev/null 2>/dev/null && echo yes || echo no)
|
||||
|
||||
|
||||
|
||||
all: $(EXEC)
|
||||
|
||||
simple: simple.c ../src/libtomlc17.a
|
||||
$(CC) $(CFLAGS) -o $@ $@.c -L../src -ltomlc17
|
||||
|
||||
repro: repro.c ../src/libtomlc17.a
|
||||
$(CC) $(CFLAGS) -o $@ $@.c -L../src -ltomlc17
|
||||
|
||||
|
||||
simplecpp: simplecpp.cpp ../src/libtomlc17.a
|
||||
ifndef HAVE_CXX
|
||||
@echo "INFO: skipping $@ because no $(CXX) compiler found"
|
||||
else ifeq ($(CXX20_SUPPORT),no)
|
||||
@echo "INFO: skipping $@ because $(CXX) does not support C++20"
|
||||
else
|
||||
$(CXX) $(CXXFLAGS) -o $@ $@.cpp -L ../src -l tomlc17
|
||||
endif
|
||||
|
||||
-include simple.d simplecpp.d
|
||||
|
||||
test: all
|
||||
./simple > /dev/null
|
||||
[ ! -x ./simplecpp ] || ./simplecpp > /dev/null
|
||||
|
||||
clean:
|
||||
rm -f *.o *.d *.a $(EXEC)
|
||||
|
||||
distclean: clean
|
||||
|
||||
format:
|
||||
clang-format -i *.[ch] *.cpp
|
||||
|
||||
.PHONY: all clean distclean format
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "../src/tomlc17.h"
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *PATH = "/tmp/t.toml";
|
||||
|
||||
static void setup() {
|
||||
const char *text =
|
||||
"[default]\n"
|
||||
"\n"
|
||||
"[wayland_displays.\"$WAYLAND_DISPLAY\"]\n"
|
||||
"seats = [ \"$XDG_SEAT\" ] \n"
|
||||
"[[clipboards.Default.mime_type_groups]]\n"
|
||||
"group = [ \"TEXT\", \"STRING\", \"UTF8_STRING\", \"text/plain\" ]\n"
|
||||
"xxxx xx xx\n";
|
||||
|
||||
(void)text;
|
||||
FILE *fp = fopen(PATH, "w");
|
||||
fprintf(fp, "%s", text);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
static void run() {
|
||||
|
||||
toml_result_t root = toml_parse_file_ex(PATH);
|
||||
|
||||
if (!root.ok) {
|
||||
fprintf(stderr, "toml_parse_file_ex: %s\n", root.errmsg);
|
||||
toml_free(root);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
toml_datum_t wayland_displays =
|
||||
toml_seek(root.toptab, "main.wayland_displays");
|
||||
toml_datum_t clipboards = toml_seek(root.toptab, "main.clipboards");
|
||||
(void)
|
||||
|
||||
toml_free(root);
|
||||
}
|
||||
|
||||
int main() {
|
||||
setup();
|
||||
run();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Parse the config file simple.toml:
|
||||
*
|
||||
* [server]
|
||||
* host = "www.example.com"
|
||||
* port = [8080, 8181, 8282]
|
||||
*
|
||||
*/
|
||||
#include "../src/tomlc17.h"
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void error(const char *msg, const char *msg1) {
|
||||
fprintf(stderr, "ERROR: %s%s\n", msg, msg1 ? msg1 : "");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Parse the toml file
|
||||
toml_result_t result = toml_parse_file_ex("simple.toml");
|
||||
|
||||
// Check for parse error
|
||||
if (!result.ok) {
|
||||
error(result.errmsg, 0);
|
||||
}
|
||||
|
||||
// Extract values
|
||||
toml_datum_t host = toml_seek(result.toptab, "server.host");
|
||||
toml_datum_t port = toml_seek(result.toptab, "server.port");
|
||||
|
||||
// Print server.host
|
||||
if (host.type != TOML_STRING) {
|
||||
error("missing or invalid 'server.host' property in config", 0);
|
||||
}
|
||||
printf("server.host = %s\n", host.u.s);
|
||||
|
||||
// Print server.port
|
||||
if (port.type != TOML_ARRAY) {
|
||||
error("missing or invalid 'server.port' property in config", 0);
|
||||
}
|
||||
printf("server.port = [");
|
||||
for (int i = 0; i < port.u.arr.size; i++) {
|
||||
toml_datum_t elem = port.u.arr.elem[i];
|
||||
if (elem.type != TOML_INT64) {
|
||||
error("server.port element not an integer", 0);
|
||||
}
|
||||
printf("%s%d", i ? ", " : "", (int)elem.u.int64);
|
||||
}
|
||||
printf("]\n");
|
||||
|
||||
// Negative test
|
||||
toml_datum_t dummy = toml_seek(result.toptab, "server.dummy");
|
||||
if (dummy.type != TOML_UNKNOWN) {
|
||||
printf("Error: dummy element exists\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Done!
|
||||
toml_free(result);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
[server]
|
||||
host = "www.example.com"
|
||||
port = [8080, 8181, 8282]
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Parse the config file simple.toml:
|
||||
*
|
||||
* [server]
|
||||
* host = "www.example.com"
|
||||
* port = [8080, 8181, 8282]
|
||||
*
|
||||
*/
|
||||
#include "../src/tomlcpp.hpp"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using std::cout;
|
||||
|
||||
static void error(const char *msg) {
|
||||
fprintf(stderr, "ERROR: %s\n", msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Parse the toml file
|
||||
toml::Result result = toml::parse_file_ex("simple.toml");
|
||||
|
||||
// Check for parse error
|
||||
if (!result.ok()) {
|
||||
error(result.errmsg());
|
||||
}
|
||||
|
||||
// Extract values
|
||||
std::string host;
|
||||
std::vector<int64_t> port;
|
||||
|
||||
try {
|
||||
// use the Datum::get() method
|
||||
host = result.get({"server", "host"})->as_str().value();
|
||||
} catch (const std::bad_optional_access &ex) {
|
||||
error("missing or invalid 'server.host' property in config");
|
||||
}
|
||||
|
||||
try {
|
||||
// use the Datum::seek() method
|
||||
port = result.seek("server.port")->as_intvec().value();
|
||||
} catch (const std::bad_optional_access &ex) {
|
||||
error("missing or invalid 'server.port' property in config");
|
||||
}
|
||||
|
||||
// Print values
|
||||
cout << "server.host = " << host << "\n";
|
||||
cout << "server.port = [";
|
||||
for (size_t i = 0; i < port.size(); i++) {
|
||||
cout << (i ? ", " : "") << port[i];
|
||||
}
|
||||
cout << "]\n";
|
||||
|
||||
// Done! No need to call toml_free(). Result::~Result() will handle
|
||||
// destruction.
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user