This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreadlinefromfile.cpp
59 lines (53 loc) · 1.58 KB
/
readlinefromfile.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
#include "stdafx.h"
#include "readlinefromfile.h"
ReadLineFromFile::ReadLineFromFile(const QString &fileName)
: m_file(fileName)
, m_offset(0)
, m_fileSize(0)
, m_lineStartPos(nullptr)
, m_mapStartPos(nullptr)
, m_mapEndPos(nullptr)
{
if (m_file.open(QIODevice::ReadOnly))
{
m_fileSize = m_file.size();
if (m_fileSize)
{
m_lineStartPos = m_mapStartPos = m_file.map(m_offset, qMin(mapSize, m_fileSize - m_offset));
m_mapEndPos = m_mapStartPos + qMin(mapSize, m_fileSize - m_offset);
}
}
}
ReadLineFromFile::~ReadLineFromFile()
{
if (m_mapStartPos)
m_file.unmap(m_mapStartPos);
m_file.close();
}
QByteArray ReadLineFromFile::readLine()
{
for (;;)
{
uchar *p = m_lineStartPos;
while (p < m_mapEndPos && *p != '\n')
p++;
if (p < m_mapEndPos)
{
QByteArray b(reinterpret_cast<const char *>(m_lineStartPos), p - m_lineStartPos);
m_lineStartPos = p + 1;
return b;
}
// re-map
m_file.unmap(m_mapStartPos);
m_offset += m_lineStartPos - m_mapStartPos;
if (m_offset == m_fileSize)
break;
m_lineStartPos = m_mapStartPos = m_file.map(m_offset, qMin(mapSize, m_fileSize - m_offset));
m_mapEndPos = m_mapStartPos + qMin(mapSize, m_fileSize - m_offset);
}
return QByteArray();
}
bool ReadLineFromFile::atEnd() const
{
return m_offset == m_fileSize;
}