2020-01-06 20:21:16 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2020-02-22 16:10:32 +02:00
|
|
|
#include <toml++/toml.h>
|
2020-01-06 20:21:16 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleOutputCP(65001); //UTF-8 console output
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//read from a file
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
auto path = std::string{ argv[1] };
|
|
|
|
auto file = std::ifstream{ path };
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
std::cerr << "The file '"sv << path << "' could not be opened for reading."sv << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto table = toml::parse(file, std::move(path));
|
2020-01-07 17:52:50 +02:00
|
|
|
std::cout << toml::json_formatter{ table } << std::endl;
|
2020-01-06 20:21:16 +02:00
|
|
|
}
|
2020-02-25 23:11:40 +02:00
|
|
|
catch (const toml::parse_error& err)
|
2020-01-06 20:21:16 +02:00
|
|
|
{
|
2020-02-25 23:11:40 +02:00
|
|
|
std::cerr << "Error parsing file:\n"sv << err << std::endl;
|
2020-01-06 20:21:16 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//read directly from stdin
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto table = toml::parse(std::cin);
|
|
|
|
std::cout << toml::json_formatter{ table } << std::endl;
|
|
|
|
}
|
2020-01-07 17:52:50 +02:00
|
|
|
catch (const toml::parse_error& err)
|
2020-01-06 20:21:16 +02:00
|
|
|
{
|
2020-02-25 23:11:40 +02:00
|
|
|
std::cerr << "Error parsing stdin:\n"sv << err << std::endl;
|
2020-01-06 20:21:16 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|