tomlplusplus/examples/toml_to_json_transcoder.cpp
Mark Gillard 40ffee43fb added json_formatter
also:
- added `toml_version.h`
- added version number to toml.hpp
- added `node::visit()`
- added `table::empty()`
- added `array::empty()`
- added version number to `toml.hpp`
- added ostream operator overload for `source_position`
- added tests for string escape sequences
- added tests for + in bare keys (toml/issues/687)
- added tests for hexfloat literals (toml/issues/562)
- added c++20 char8_t detection to meson
- moved third party submodules to `/extern`
- refactored noexcept version of `parse_result` to be more useful
- refactored all code to 'mostly' stick to a 120 column limit
- fixed some minor stuff picked up by gcc9
2020-01-06 22:24:53 +02:00

65 lines
1.2 KiB
C++

#include <toml++/toml.h>
#include <iostream>
#include <fstream>
#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));
std::cout << table << std::endl;
}
catch (const toml::parse_error & err)
{
std::cerr
<< "Error parsing file '"sv << *err.where().path
<< "':\n"sv << err.what()
<< "\n ("sv << err.where().begin << ")"sv
<< std::endl;
return 1;
}
}
//read directly from stdin
else
{
try
{
const auto table = toml::parse(std::cin);
std::cout << toml::json_formatter{ table } << std::endl;
}
catch (const toml::parse_error & err)
{
std::cerr
<< "Error parsing stdin:\n"sv << err.what()
<< "\n ("sv << err.where().begin << ")"sv
<< std::endl;
return 1;
}
}
return 0;
}