Skip to content

Commit

Permalink
refactored parser
Browse files Browse the repository at this point in the history
Mainly to simplify a the error handling code (it had gotten a bit verbose), but also to reduce compiled binary sizes.

also:
- moved windows terminal code page stuff in examples to a separate file
  • Loading branch information
marzer committed Apr 11, 2020
1 parent f399025 commit 42af364
Show file tree
Hide file tree
Showing 11 changed files with 1,293 additions and 2,378 deletions.
22 changes: 14 additions & 8 deletions examples/parse_file.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// This file is a part of toml++ and is subject to the the terms of the MIT license.
// Copyright (c) 2019-2020 Mark Gillard <[email protected]>
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT
/*
This example demonstrates how to use the toml::json_formatter to
re-serialize TOML data as JSON.
*/
#include <iostream>
#include <fstream>
#include "utf8_console.h"
#define TOML_UNRELEASED_FEATURES 1
#include <toml++/toml.h>
#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
init_utf8_console();

auto path = std::string{ argc > 1 ? argv[1] : "example.toml" };
auto file = std::ifstream{ path };
Expand All @@ -27,7 +33,7 @@ int main(int argc, char** argv)
}
catch (const toml::parse_error& err)
{
std::cerr << "Error parsing file:\n"sv << err << std::endl;
std::cerr << err << std::endl;
return 1;
}
return 0;
Expand Down
30 changes: 18 additions & 12 deletions examples/toml_to_json_transcoder.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
// This file is a part of toml++ and is subject to the the terms of the MIT license.
// Copyright (c) 2019-2020 Mark Gillard <[email protected]>
// 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.
*/
#include <iostream>
#include <fstream>
#include "utf8_console.h"
#define TOML_UNRELEASED_FEATURES 1
#include <toml++/toml.h>
#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
init_utf8_console();

//read from a file
// read from a file if a path argument is given
if (argc > 1)
{
auto path = std::string{ argv[1] };
Expand All @@ -31,23 +37,23 @@ int main(int argc, char** argv)
}
catch (const toml::parse_error& err)
{
std::cerr << "Error parsing file:\n"sv << err << std::endl;
std::cerr << err << std::endl;

return 1;
}
}

//read directly from stdin
// otherwise read directly from stdin
else
{
try
{
const auto table = toml::parse(std::cin);
const auto table = toml::parse(std::cin, "stdin"sv);
std::cout << toml::json_formatter{ table } << std::endl;
}
catch (const toml::parse_error& err)
{
std::cerr << "Error parsing stdin:\n"sv << err << std::endl;
std::cerr << err << std::endl;

return 1;
}
Expand Down
63 changes: 63 additions & 0 deletions examples/utf8_console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
//# Copyright (c) 2019-2020 Mark Gillard <[email protected]>
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT

#ifdef _WIN32

#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define NOATOM // - Atom Manager routines
#define NOBITMAP
#define NOCLIPBOARD // - Clipboard routines
#define NOCOLOR // - Screen colors
#define NOCOMM // - COMM driver routines
#define NOCTLMGR // - Control and Dialog routines
#define NODEFERWINDOWPOS // - DeferWindowPos routines
#define NODRAWTEXT // - DrawText() and DT_*
#define NOGDI // - All GDI defines and routines
#define NOGDICAPMASKS // - CC_*, LC_*, PC_*, CP_*, TC_*, RC_
#define NOHELP // - Help engine interface.
#define NOICONS // - IDI_*
#define NOKANJI // - Kanji support stuff.
#define NOKEYSTATES // - MK_*
#define NOKERNEL // - All KERNEL defines and routines
#define NOMB // - MB_* and MessageBox()
#define NOMCX // - Modem Configuration Extensions
#define NOMENUS // - MF_*
#define NOMEMMGR // - GMEM_*, LMEM_*, GHND, LHND, associated routines
#define NOMETAFILE // - typedef METAFILEPICT
#define NOMINMAX // - Macros min(a,b) and max(a,b)
#define NOMSG // - typedef MSG and associated routines
#define NONLS // - All NLS defines and routines
#define NOOPENFILE // - OpenFile(), OemToAnsi, AnsiToOem, and OF_*
#define NOPROFILER // - Profiler interface.
#define NORASTEROPS // - Binary and Tertiary raster ops
#define NOSCROLL // - SB_* and scrolling routines
#define NOSERVICE // - All Service Controller routines, SERVICE_ equates, etc.
#define NOSHOWWINDOW // - SW_*
#define NOSOUND // - Sound driver routines
#define NOSYSCOMMANDS // - SC_*
#define NOSYSMETRICS // - SM_*
#define NOTEXTMETRIC // - typedef TEXTMETRIC and associated routines
#define NOUSER // - All USER defines and routines
#define NOVIRTUALKEYCODES // - VK_*
#define NOWH // - SetWindowsHook and WH_*
#define NOWINOFFSETS // - GWL_*, GCL_*, associated routines
#define NOWINMESSAGES // - WM_*, EM_*, LB_*, CB_*
#define NOWINSTYLES // - WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
#include <Windows.h>

inline void init_utf8_console() noexcept
{
SetConsoleOutputCP(65001); //CP_UTF8
}

#else

inline void init_utf8_console() noexcept
{
// no-op
}

#endif
1 change: 0 additions & 1 deletion include/toml++/toml.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#undef TOML_DISABLE_ALL_WARNINGS
#undef TOML_POP_WARNINGS
#undef TOML_ALWAYS_INLINE
#undef TOML_NEVER_INLINE
#undef TOML_ASSUME
#undef TOML_UNREACHABLE
#undef TOML_INTERFACE
Expand Down
Loading

0 comments on commit 42af364

Please sign in to comment.