2010-11-20 10:37:14 +00:00
|
|
|
/* (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. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include "linereader.h"
|
|
|
|
|
2023-12-11 14:40:09 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
void CLineReader::Init(IOHANDLE File)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2021-11-06 11:57:31 +00:00
|
|
|
m_BufferMaxSize = sizeof(m_aBuffer) - 1;
|
2010-05-29 07:25:38 +00:00
|
|
|
m_BufferSize = 0;
|
|
|
|
m_BufferPos = 0;
|
2020-10-27 17:57:14 +00:00
|
|
|
m_File = File;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *CLineReader::Get()
|
|
|
|
{
|
|
|
|
unsigned LineStart = m_BufferPos;
|
2011-01-20 20:17:11 +00:00
|
|
|
bool CRLFBreak = false;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2022-02-14 23:12:52 +00:00
|
|
|
while(true)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
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
|
2024-03-11 21:47:16 +00:00
|
|
|
unsigned Read = io_read(m_File, &m_aBuffer[m_BufferPos], m_BufferMaxSize - m_BufferPos);
|
2010-05-29 07:25:38 +00:00
|
|
|
m_BufferSize = Left + Read;
|
|
|
|
LineStart = 0;
|
|
|
|
|
|
|
|
if(!Read)
|
|
|
|
{
|
|
|
|
if(Left)
|
|
|
|
{
|
2024-03-11 21:47:16 +00:00
|
|
|
m_aBuffer[Left] = '\0'; // return the last line
|
2010-05-29 07:25:38 +00:00
|
|
|
m_BufferPos = Left;
|
|
|
|
m_BufferSize = Left;
|
2024-03-11 21:43:16 +00:00
|
|
|
if(!str_utf8_check(m_aBuffer))
|
|
|
|
{
|
|
|
|
LineStart = m_BufferPos;
|
|
|
|
CRLFBreak = false;
|
|
|
|
continue; // skip lines containing invalid UTF-8
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
return m_aBuffer;
|
|
|
|
}
|
2024-03-11 21:47:16 +00:00
|
|
|
return nullptr; // we are done!
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(m_aBuffer[m_BufferPos] == '\n' || m_aBuffer[m_BufferPos] == '\r')
|
|
|
|
{
|
|
|
|
// line found
|
2011-01-20 20:17:11 +00:00
|
|
|
if(m_aBuffer[m_BufferPos] == '\r')
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(m_BufferPos + 1 >= m_BufferSize)
|
2011-01-20 20:17:11 +00:00
|
|
|
{
|
|
|
|
// read more to get the connected '\n'
|
|
|
|
CRLFBreak = true;
|
|
|
|
++m_BufferPos;
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(m_aBuffer[m_BufferPos + 1] == '\n')
|
2024-03-11 21:47:16 +00:00
|
|
|
m_aBuffer[m_BufferPos++] = '\0';
|
2011-01-20 20:17:11 +00:00
|
|
|
}
|
2024-03-11 21:47:16 +00:00
|
|
|
m_aBuffer[m_BufferPos++] = '\0';
|
2024-03-11 21:43:16 +00:00
|
|
|
if(!str_utf8_check(&m_aBuffer[LineStart]))
|
|
|
|
{
|
|
|
|
LineStart = m_BufferPos;
|
|
|
|
CRLFBreak = false;
|
|
|
|
continue; // skip lines containing invalid UTF-8
|
|
|
|
}
|
2011-01-20 20:17:11 +00:00
|
|
|
return &m_aBuffer[LineStart];
|
|
|
|
}
|
|
|
|
else if(CRLFBreak)
|
|
|
|
{
|
|
|
|
if(m_aBuffer[m_BufferPos] == '\n')
|
2024-03-11 21:47:16 +00:00
|
|
|
m_aBuffer[m_BufferPos++] = '\0';
|
2024-03-11 21:43:16 +00:00
|
|
|
if(!str_utf8_check(&m_aBuffer[LineStart]))
|
|
|
|
{
|
|
|
|
LineStart = m_BufferPos;
|
|
|
|
CRLFBreak = false;
|
|
|
|
continue; // skip lines containing invalid UTF-8
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
return &m_aBuffer[LineStart];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_BufferPos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|