Greatly optimized file-based streams
Former-commit-id: 3adef66ec27b9e423667d2d17b914a3a6c71a4ca
This commit is contained in:
parent
ddc900185d
commit
36cb681adc
|
|
@ -3,7 +3,9 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/InputStream.hpp>
|
#include <Nazara/Core/InputStream.hpp>
|
||||||
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/String.hpp>
|
#include <Nazara/Core/String.hpp>
|
||||||
|
#include <cstring>
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
NzInputStream::~NzInputStream() = default;
|
NzInputStream::~NzInputStream() = default;
|
||||||
|
|
@ -13,40 +15,54 @@ NzString NzInputStream::ReadLine(unsigned int lineSize)
|
||||||
NzString line;
|
NzString line;
|
||||||
if (lineSize == 0) // Taille maximale indéterminée
|
if (lineSize == 0) // Taille maximale indéterminée
|
||||||
{
|
{
|
||||||
while (!EndOfStream())
|
const unsigned int bufferSize = 64;
|
||||||
{
|
|
||||||
char c;
|
|
||||||
if (Read(&c, sizeof(char)) == sizeof(char))
|
|
||||||
{
|
|
||||||
if (c == '\n')
|
|
||||||
break;
|
|
||||||
|
|
||||||
line += c;
|
char buffer[bufferSize+1];
|
||||||
|
buffer[bufferSize] = '\0';
|
||||||
|
|
||||||
|
unsigned int readSize;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
readSize = Read(buffer, bufferSize);
|
||||||
|
|
||||||
|
const char* ptr = std::strchr(buffer, '\n');
|
||||||
|
if (ptr)
|
||||||
|
{
|
||||||
|
unsigned int pos = ptr-buffer;
|
||||||
|
|
||||||
|
if (m_streamOptions & nzStreamOption_Text && pos > 0 && buffer[pos-1] == '\r')
|
||||||
|
line.Append(buffer, pos-1);
|
||||||
|
else
|
||||||
|
line.Append(buffer, pos);
|
||||||
|
|
||||||
|
if (!SetCursorPos(GetCursorPos() - readSize + pos + 1))
|
||||||
|
NazaraWarning("Failed to reset cursos pos");
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
break;
|
line.Append(buffer, readSize);
|
||||||
}
|
}
|
||||||
|
while (readSize == bufferSize);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
line.Reserve(lineSize);
|
line.Resize(lineSize);
|
||||||
for (unsigned int i = 0; i < lineSize; ++i)
|
unsigned int readSize = Read(&line[0], lineSize);
|
||||||
|
unsigned int pos = line.Find('\n');
|
||||||
|
if (pos <= readSize) // Forcément trouvé, npos étant le plus grand des entiers
|
||||||
{
|
{
|
||||||
char c;
|
if (m_streamOptions & nzStreamOption_Text && pos > 0 && line[pos-1] == '\r')
|
||||||
if (Read(&c, sizeof(char)) == sizeof(char))
|
line.Resize(pos);
|
||||||
{
|
|
||||||
if (c == '\n')
|
|
||||||
break;
|
|
||||||
|
|
||||||
line += c;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
break;
|
line.Resize(pos+1);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_streamOptions & nzStreamOption_Text && !EndOfStream() && line.EndsWith('\r'))
|
if (!SetCursorPos(GetCursorPos() - readSize + pos + 1))
|
||||||
line.Resize(-1);
|
NazaraWarning("Failed to reset cursos pos");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
line.Resize(readSize);
|
||||||
|
}
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue