From cc6f2a6fc9d5a55fde96f15e634038ca166a3abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Fri, 21 Sep 2012 12:39:40 +0200 Subject: [PATCH] Added InputStream::GetLine(size); Former-commit-id: 4c5afc038877bfa9865285d044663df3f7932d58 --- include/Nazara/Core/File.hpp | 2 +- include/Nazara/Core/InputStream.hpp | 3 ++ src/Nazara/Core/File.cpp | 49 ++----------------------- src/Nazara/Core/InputStream.cpp | 57 +++++++++++++++++++++++++---- 4 files changed, 57 insertions(+), 54 deletions(-) diff --git a/include/Nazara/Core/File.hpp b/include/Nazara/Core/File.hpp index c912fd84b..a8a4ed19e 100644 --- a/include/Nazara/Core/File.hpp +++ b/include/Nazara/Core/File.hpp @@ -72,7 +72,7 @@ class NAZARA_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable NzString GetFileName() const; time_t GetLastAccessTime() const; time_t GetLastWriteTime() const; - NzString GetLine(unsigned int lineSize = 0); + NzString GetLine(unsigned int lineSize = 0) override; nzUInt64 GetSize() const; bool IsOpen() const; diff --git a/include/Nazara/Core/InputStream.hpp b/include/Nazara/Core/InputStream.hpp index eca1a42b0..f8e734cb2 100644 --- a/include/Nazara/Core/InputStream.hpp +++ b/include/Nazara/Core/InputStream.hpp @@ -9,6 +9,8 @@ #include +class NzString; + class NzInputStream { public: @@ -17,6 +19,7 @@ class NzInputStream virtual bool EndOfStream() const = 0; virtual nzUInt64 GetCursorPos() const = 0; + virtual NzString GetLine(unsigned int lineSize = 0); virtual nzUInt64 GetSize() const = 0; virtual std::size_t Read(void* buffer, std::size_t size) = 0; diff --git a/src/Nazara/Core/File.cpp b/src/Nazara/Core/File.cpp index d01aefc7c..7f9d117f1 100644 --- a/src/Nazara/Core/File.cpp +++ b/src/Nazara/Core/File.cpp @@ -100,7 +100,7 @@ bool NzFile::EndOfFile() const return m_impl->EndOfFile(); } -bool NzFile::EndOfStream() const; +bool NzFile::EndOfStream() const { return EndOfFile(); } @@ -211,50 +211,9 @@ NzString NzFile::GetLine(unsigned int lineSize) } #endif - NzString line; - if (lineSize == 0) // Taille maximale indéterminée - { - while (!m_impl->EndOfFile()) - { - char c; - if (m_impl->Read(&c, sizeof(char)) == sizeof(char)) - { - if (c == '\n') - { - if (m_openMode & Text && line.EndsWith('\r')) - line.Resize(-1); - - break; - } - - line += c; - } - else - break; - } - } - else - { - line.Reserve(lineSize); - for (unsigned int i = 0; i < lineSize; ++i) - { - char c; - if (m_impl->Read(&c, sizeof(char)) == sizeof(char)) - { - if (c == '\n') - { - if (m_openMode & Text && line.EndsWith('\r')) - line.Resize(-1); - - break; - } - - line += c; - } - else - break; - } - } + NzString line = NzInputStream::GetLine(lineSize); + if (m_openMode & Text && !m_impl->EndOfFile() && line.EndsWith('\r')) + line.Resize(-1); return line; } diff --git a/src/Nazara/Core/InputStream.cpp b/src/Nazara/Core/InputStream.cpp index 2759f4d83..f2ef22a25 100644 --- a/src/Nazara/Core/InputStream.cpp +++ b/src/Nazara/Core/InputStream.cpp @@ -1,8 +1,49 @@ -// Copyright (C) 2012 Jérôme Leclercq -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include - -NzInputStream::~NzInputStream() = default; +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +NzInputStream::~NzInputStream() = default; + +NzString NzInputStream::GetLine(unsigned int lineSize) +{ + NzString line; + if (lineSize == 0) // Taille maximale indéterminée + { + while (!EndOfStream()) + { + char c; + if (Read(&c, sizeof(char)) == sizeof(char)) + { + if (c == '\n') + break; + + line += c; + } + else + break; + } + } + else + { + line.Reserve(lineSize); + for (unsigned int i = 0; i < lineSize; ++i) + { + char c; + if (Read(&c, sizeof(char)) == sizeof(char)) + { + if (c == '\n') + break; + + line += c; + } + else + break; + } + } + + return line; +}