-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinereader.cpp
100 lines (89 loc) · 1.49 KB
/
linereader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "myutils.h"
#include "linereader.h"
LineReader::LineReader()
{
Clear();
}
LineReader::~LineReader()
{
Close();
}
void LineReader::Open(const string &FileName)
{
m_FileName = FileName;
m_f = OpenStdioFile(FileName);
m_Buffer = myalloc(char, LR_BUFF);
m_BufferBytes = 0;
m_BufferOffset = 0;
m_LineNr = 0;
m_EOF = false;
}
void LineReader::Clear()
{
m_f = 0;
m_Buffer = 0;
m_BufferOffset = 0;
m_BufferBytes = 0;
m_LineNr = 0;
m_EOF = true;
}
void LineReader::Close()
{
if (m_f == 0)
return;
CloseStdioFile(m_f);
m_f = 0;
myfree(m_Buffer);
Clear();
}
bool LineReader::ReadLine(t_LineBuff &Line)
{
if (m_EOF)
return false;
Line.Alloc(32*1024);
char *LineData = Line.Data;
unsigned Length = 0;
for (;;)
{
if (m_BufferOffset >= m_BufferBytes)
{
FillBuff();
if (m_EOF)
{
Line.Size = Length;
if (Length == 0)
return false;
LineData[Length] = 0;
++m_LineNr;
return true;
}
}
char c = m_Buffer[m_BufferOffset++];
if (c == '\r')
continue;
if (c == '\n')
{
LineData[Length] = 0;
Line.Size = Length;
++m_LineNr;
return true;
}
if (Length == Line.MaxSize)
{
Line.Size = Length;
Line.Alloc(Line.MaxSize + 32*1024);
LineData = Line.Data;
}
LineData[Length++] = c;
}
}
void LineReader::FillBuff()
{
if (m_EOF)
return;
uint32 BytesToRead = LR_BUFF;
m_BufferOffset = 0;
m_BufferBytes = ReadStdioFile_NoFail(m_f, m_Buffer, LR_BUFF);
if (m_BufferBytes == 0)
m_EOF = true;
}