From 481f377cfe586389d2eb14387e480321a887874f Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 4 Jan 2015 12:05:28 +0100 Subject: [PATCH] + guard call to std::ios_base::sync_with_stdio --- src/JSON.cc | 15 ++++++++++++--- src/JSON.h | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/JSON.cc b/src/JSON.cc index e7186560..ed2e4949 100644 --- a/src/JSON.cc +++ b/src/JSON.cc @@ -22,6 +22,7 @@ //////////////////// std::mutex JSON::_token; +bool JSON::Parser::firstCall = true; /////////////////////////////////// @@ -1776,9 +1777,17 @@ Initialize the JSON parser given an input stream \p _is. */ JSON::Parser::Parser(std::istream& _is) { - // from http://www.manticmoo.com/articles/jeff/programming/c++/making-io-streams-efficient-in-c++.php - // Don't sync C++ and C I/O - std::ios_base::sync_with_stdio(false); + // On first call, switch off syncing between C++ and C I/O. This call must + // be done before first I/O operation as the behavior may be undefined + // otherwise. + if (firstCall) + { + firstCall = false; + // Don't sync C++ and C I/O + // from http://www.manticmoo.com/articles/jeff/programming/c++/making-io-streams-efficient-in-c++.php + std::ios_base::sync_with_stdio(false); + } + while (_is) { std::string input_line; diff --git a/src/JSON.h b/src/JSON.h index 5d512fc4..14ca857c 100644 --- a/src/JSON.h +++ b/src/JSON.h @@ -432,6 +432,10 @@ class JSON char _current {}; /// the position inside the input buffer size_t _pos = 0; + + private: + /// variable to guard std::ios_base::sync_with_stdio + static bool firstCall; }; };