mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-18 14:08:19 +00:00
e0e6bbbbe2
- Use `nullptr` instead of `0`. - Use `\0` instead of `0`. - Move variable declaration.
101 lines
2.2 KiB
C++
101 lines
2.2 KiB
C++
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
#include "linereader.h"
|
|
|
|
#include <base/system.h>
|
|
|
|
void CLineReader::Init(IOHANDLE File)
|
|
{
|
|
m_BufferMaxSize = sizeof(m_aBuffer) - 1;
|
|
m_BufferSize = 0;
|
|
m_BufferPos = 0;
|
|
m_File = File;
|
|
}
|
|
|
|
char *CLineReader::Get()
|
|
{
|
|
unsigned LineStart = m_BufferPos;
|
|
bool CRLFBreak = false;
|
|
|
|
while(true)
|
|
{
|
|
if(m_BufferPos >= m_BufferSize)
|
|
{
|
|
// fetch more
|
|
|
|
// move the remaining part to the front
|
|
unsigned Left = m_BufferSize - LineStart;
|
|
|
|
if(LineStart > m_BufferSize)
|
|
Left = 0;
|
|
if(Left)
|
|
mem_move(m_aBuffer, &m_aBuffer[LineStart], Left);
|
|
m_BufferPos = Left;
|
|
|
|
// fill the buffer
|
|
unsigned Read = io_read(m_File, &m_aBuffer[m_BufferPos], m_BufferMaxSize - m_BufferPos);
|
|
m_BufferSize = Left + Read;
|
|
LineStart = 0;
|
|
|
|
if(!Read)
|
|
{
|
|
if(Left)
|
|
{
|
|
m_aBuffer[Left] = '\0'; // return the last line
|
|
m_BufferPos = Left;
|
|
m_BufferSize = Left;
|
|
if(!str_utf8_check(m_aBuffer))
|
|
{
|
|
LineStart = m_BufferPos;
|
|
CRLFBreak = false;
|
|
continue; // skip lines containing invalid UTF-8
|
|
}
|
|
return m_aBuffer;
|
|
}
|
|
return nullptr; // we are done!
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(m_aBuffer[m_BufferPos] == '\n' || m_aBuffer[m_BufferPos] == '\r')
|
|
{
|
|
// line found
|
|
if(m_aBuffer[m_BufferPos] == '\r')
|
|
{
|
|
if(m_BufferPos + 1 >= m_BufferSize)
|
|
{
|
|
// read more to get the connected '\n'
|
|
CRLFBreak = true;
|
|
++m_BufferPos;
|
|
continue;
|
|
}
|
|
else if(m_aBuffer[m_BufferPos + 1] == '\n')
|
|
m_aBuffer[m_BufferPos++] = '\0';
|
|
}
|
|
m_aBuffer[m_BufferPos++] = '\0';
|
|
if(!str_utf8_check(&m_aBuffer[LineStart]))
|
|
{
|
|
LineStart = m_BufferPos;
|
|
CRLFBreak = false;
|
|
continue; // skip lines containing invalid UTF-8
|
|
}
|
|
return &m_aBuffer[LineStart];
|
|
}
|
|
else if(CRLFBreak)
|
|
{
|
|
if(m_aBuffer[m_BufferPos] == '\n')
|
|
m_aBuffer[m_BufferPos++] = '\0';
|
|
if(!str_utf8_check(&m_aBuffer[LineStart]))
|
|
{
|
|
LineStart = m_BufferPos;
|
|
CRLFBreak = false;
|
|
continue; // skip lines containing invalid UTF-8
|
|
}
|
|
return &m_aBuffer[LineStart];
|
|
}
|
|
else
|
|
m_BufferPos++;
|
|
}
|
|
}
|
|
}
|