Added InputStream::GetLine(size);

Former-commit-id: 4c5afc038877bfa9865285d044663df3f7932d58
This commit is contained in:
Jérôme Leclercq 2012-09-21 12:39:40 +02:00
parent 2ea37a1877
commit cc6f2a6fc9
4 changed files with 57 additions and 54 deletions

View File

@ -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;

View File

@ -9,6 +9,8 @@
#include <Nazara/Prerequesites.hpp>
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;

View File

@ -100,7 +100,7 @@ bool NzFile::EndOfFile() const
return m_impl->EndOfFile();
}
bool NzFile::EndOfStream() const;
bool NzFile::EndOfStream() const
{
return EndOfFile();
}
@ -211,51 +211,10 @@ 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'))
NzString line = NzInputStream::GetLine(lineSize);
if (m_openMode & Text && !m_impl->EndOfFile() && 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;
}
}
return line;
}

View File

@ -3,6 +3,47 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/InputStream.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Core/Debug.hpp>
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;
}