From 4e44390090eb13f5d127c3517ecd434cc25cd55a Mon Sep 17 00:00:00 2001 From: Niels Date: Mon, 29 Dec 2014 21:13:03 +0100 Subject: [PATCH] + fixed a bug in the stream input --- .gitignore | 2 ++ Makefile.am | 6 +++++- benchmark/parse.cc | 8 ++++++++ src/JSON.cc | 11 +++++------ 4 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 benchmark/parse.cc diff --git a/.gitignore b/.gitignore index 81f918822..233e353cc 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ test-driver Makefile.in *.plist + +json_parser diff --git a/Makefile.am b/Makefile.am index 5c1a51965..49140d759 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -noinst_PROGRAMS = json_unit +noinst_PROGRAMS = json_unit json_parser FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder @@ -6,6 +6,10 @@ json_unit_SOURCES = src/JSON.cc src/JSON.h test/catch.hpp test/JSON_unit.cc json_unit_CXXFLAGS = $(FLAGS) -std=c++11 json_unit_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/test -Dprivate=public +json_parser_SOURCES = src/JSON.cc src/JSON.h benchmark/parse.cc +json_parser_CXXFLAGS = $(FLAGS) -std=c++11 +json_parser_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/benchmark + cppcheck: cppcheck --enable=all --inconclusive --std=c++11 src/JSON.* diff --git a/benchmark/parse.cc b/benchmark/parse.cc new file mode 100644 index 000000000..02547afed --- /dev/null +++ b/benchmark/parse.cc @@ -0,0 +1,8 @@ +#include "JSON.h" + +int main() +{ + JSON j; + j << std::cin; + return 0; +} diff --git a/src/JSON.cc b/src/JSON.cc index c9b9fcdb2..5445404e4 100644 --- a/src/JSON.cc +++ b/src/JSON.cc @@ -1775,14 +1775,13 @@ Initialize the JSON parser given an input stream \p _is. */ JSON::Parser::Parser(std::istream& _is) { - // determine length of input stream - _is.seekg(0, std::ios::end); - _length = static_cast(_is.tellg()); - _is.seekg(0, std::ios::beg); + // copy stream to string + std::istreambuf_iterator eos; + std::string string_input(std::istreambuf_iterator(_is), eos); - // copy stream to buffer + _length = string_input.size(); _buffer = new char[_length + 1]; - _is.read(_buffer, static_cast(_length)); + std::strcpy(_buffer, string_input.c_str()); // read first character next();