From ee56a105b3d4041989689cef979f1587188b853b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 20 Jun 2017 03:14:51 +0200 Subject: [PATCH] Resizing file content one time if stream is seekable and dynamically otherwise. --- lib/tokenize/src/tokenize/Stream.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/tokenize/src/tokenize/Stream.cpp b/lib/tokenize/src/tokenize/Stream.cpp index e275247..49e1b58 100644 --- a/lib/tokenize/src/tokenize/Stream.cpp +++ b/lib/tokenize/src/tokenize/Stream.cpp @@ -30,14 +30,22 @@ void Stream::read(std::string streamName, std::istream &istream) // Store position of new section m_delimiters.push_back({m_stream.size(), streamName}); - /*istream.seekg(0, std::ios::end); - const auto streamSize = istream.tellg(); - istream.seekg(0, std::ios::beg); + try + { + istream.seekg(0, std::ios::end); + const auto streamSize = istream.tellg(); + istream.seekg(0, std::ios::beg); - const auto startPosition = m_stream.size(); + const auto startPosition = m_stream.size(); - m_stream.resize(m_stream.size() + streamSize);*/ - std::copy(std::istreambuf_iterator(istream), std::istreambuf_iterator(), std::back_inserter(m_stream)); + m_stream.resize(m_stream.size() + streamSize); + std::copy(std::istreambuf_iterator(istream), std::istreambuf_iterator(), m_stream.begin() + startPosition); + } + catch (const std::exception &exception) + { + istream.clear(); + std::copy(std::istreambuf_iterator(istream), std::istreambuf_iterator(), std::back_inserter(m_stream)); + } } ////////////////////////////////////////////////////////////////////////////////////////////////////