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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "filereader.h"
#include <iostream>
#include <unistd.h>
int GetOpt (int argc, char *argv[], const char *optionsString,
const char* &optionArgument,
int &optionIndex)
{
optind = optionIndex;
int rc = getopt(argc, argv, optionsString);
optionArgument = optarg;
optionIndex = optind;
return rc;
}
FileReader::FileReader()
: _backing(),
_file(&std::cin),
_bufsize(1024*1024),
_buf(_bufsize),
_lastReadPos(0),
_nextReadPos(0),
_bufused(0),
_bufpos(0)
{
}
FileReader::~FileReader()
{
}
bool
FileReader::Open(const char *filename)
{
_backing = std::make_unique<std::ifstream>(filename);
_file = _backing.get();
return (bool)*_file;
}
bool
FileReader::OpenStdin()
{
_file = &std::cin;
return true;
}
bool
FileReader::Reset()
{
_file->clear();
return SetFilePos(0);
}
bool
FileReader::SetFilePos(int64_t pos)
{
_bufused = _bufpos = 0;
_lastReadPos = _nextReadPos = pos;
_file->seekg(pos);
return bool(*_file);
}
int64_t
FileReader::GetFileSize()
{
_file->seekg (0, std::ifstream::end);
return _file->tellg();
}
uint64_t
FileReader::FindNextLine(int64_t pos)
{
char buf[100];
SetFilePos(pos);
ReadLine(buf, 100);
return GetFilePos();
}
void
FileReader::FillBuffer()
{
_lastReadPos = _nextReadPos;
_file->read(&_buf[0], _bufsize);
auto wasRead = _file->gcount(); // may be -1
_nextReadPos += wasRead;
_bufused = wasRead;
_bufpos = 0;
}
ssize_t
FileReader::ReadLine(char *buf, size_t bufsize)
{
int c;
size_t len;
len = 0;
c = ReadByte();
if (c == -1)
return -1;
while (c != -1 && c != '\n' && c != '\r') {
if (len < bufsize - 1)
buf[len] = c;
len++;
c = ReadByte();
}
if (_bufpos == _bufused)
FillBuffer();
if ((_bufused > _bufpos) &&
((c == '\n' && _buf[_bufpos] == '\r') ||
(c == '\r' && _buf[_bufpos] == '\n')))
_bufpos++;
if (len < bufsize)
buf[len] = '\0'; // terminate string
else
buf[bufsize - 1] = '\0'; // terminate string
return len;
}
void
FileReader::Close()
{
if (_backing) {
_backing->close();
}
}
|