2020-04-11 16:43:38 +00:00
|
|
|
// This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
// Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/*
|
|
|
|
|
|
|
|
This example demonstrates how to parse TOML from a file and
|
|
|
|
re-serialize it (print it out) to stdout.
|
|
|
|
|
|
|
|
*/
|
2020-01-06 18:21:16 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2020-04-11 16:43:38 +00:00
|
|
|
#include "utf8_console.h"
|
|
|
|
#define TOML_UNRELEASED_FEATURES 1
|
2020-02-22 14:10:32 +00:00
|
|
|
#include <toml++/toml.h>
|
2020-01-06 18:21:16 +00:00
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-04-11 16:43:38 +00:00
|
|
|
init_utf8_console();
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-04-11 16:43:38 +00:00
|
|
|
// read from a file if a path argument is given
|
2020-01-06 18:21:16 +00:00
|
|
|
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 15:52:50 +00:00
|
|
|
std::cout << toml::json_formatter{ table } << std::endl;
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-02-25 21:11:40 +00:00
|
|
|
catch (const toml::parse_error& err)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-04-11 16:43:38 +00:00
|
|
|
std::cerr << err << std::endl;
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 16:43:38 +00:00
|
|
|
// otherwise read directly from stdin
|
2020-01-06 18:21:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-11 16:43:38 +00:00
|
|
|
const auto table = toml::parse(std::cin, "stdin"sv);
|
2020-01-06 18:21:16 +00:00
|
|
|
std::cout << toml::json_formatter{ table } << std::endl;
|
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
catch (const toml::parse_error& err)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-04-11 16:43:38 +00:00
|
|
|
std::cerr << err << std::endl;
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|