-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineReader.h
104 lines (80 loc) · 3.55 KB
/
LineReader.h
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
101
102
103
104
#pragma once
#include "CharBuffer.h"
#include "ScanFile.h"
#include <optional> // this is STL, but it does not need exceptions
#include <string_view> // this is STL, but it does not need exceptions
#include <wchar.h> // for size_t
//////////////////////////////////////////////////////////////////////////
// Implementation with 2 buffers and async calls to ReadFile()
class CSyncLineReader
{
public:
CSyncLineReader();
bool Open(const wchar_t* const filename);
void Close();
// request next matching line; line may contain '\0' and may end with '\n'; return false on error or EOF
// returned line is never empty (it contains at least one '\n' or any other character).
std::optional<std::string_view> GetNextLine();
protected:
CScanFile _file;
// Buffer structure: [ rest_of_previousline|data_read_from_file ]
// [ len = MaxLogLineLength | len = ReadChunkSize ]
CCharBuffer _buffer;
std::string_view _bufferData; // filled part of the buffer
};
//////////////////////////////////////////////////////////////////////////
// Implementation with usual sync calls to ReadFile()
class CAsyncLineReader
{
public:
CAsyncLineReader();
bool Open(const wchar_t* const filename);
void Close();
// request next matching line; line may contain '\0' and may end with '\n'; return false on error or EOF
// returned line is never empty (it contains at least one '\n' or any other character).
std::optional<std::string_view> GetNextLine();
protected:
CScanFile _file;
// Buffer structure: [ rest_of_previousline|data_read_from_file ]
// [ len = MaxLogLineLength | len = ReadChunkSize ]
bool _firstBufferIsActive = true;
CCharBuffer _buffer1;
CCharBuffer _buffer2;
std::string_view _bufferData; // filled part of the current buffer
};
//////////////////////////////////////////////////////////////////////////
// Implementation with file mapped to a big single readonly memory block
class CMappingLineReader
{
public:
bool Open(const wchar_t* const filename);
void Close();
// request next matching line; line may contain '\0' and may end with '\n'; return false on error or EOF
// returned line is never empty (it contains at least one '\n' or any other character).
std::optional<std::string_view> GetNextLine();
protected:
CScanFile _file;
bool _mappedToMemory = false;
std::string_view _bufferData; // filled part of the current buffer
};
//////////////////////////////////////////////////////////////////////////
// Implementation with 2 buffers and separate thread for sync calls to ReadFile(); synchronization is done without Windows EVENTs (manual spin locks)
class CSpinlockLineReader
{
public:
CSpinlockLineReader();
bool Open(const wchar_t* const filename);
void Close();
// request next matching line; line may contain '\0' and may end with '\n'; return false on error or EOF
// returned line is never empty (it contains at least one '\n' or any other character).
std::optional<std::string_view> GetNextLine();
protected:
CScanFile _file;
// Buffer structure: [ rest_of_previousline|data_read_from_file ]
// [ len = MaxLogLineLength | len = ReadChunkSize ]
bool _firstBufferIsActive = true;
CCharBuffer _buffer1;
CCharBuffer _buffer2;
std::string_view _bufferData; // filled part of the current buffer
};
//////////////////////////////////////////////////////////////////////////