diff --git a/.gitignore b/.gitignore index 81f91882..233e353c 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 5c1a5196..49140d75 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 00000000..02547afe --- /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 c9b9fcdb..5445404e 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();