-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reader.cpp
68 lines (58 loc) · 1.37 KB
/
Reader.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
/*
* Reader.cpp
*
* Created on: ۷ بهمن ۱۳۹۶
*
* Copyright Hedayat Vatankhah <[email protected]>, 2018-2022.
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#include "Reader.h"
#include <stdexcept>
using namespace std;
Reader::Reader(FILE *input_file) :
in_file(input_file)
#ifdef __GLIBCXX__
, filebuf(in_file, std::ios::in), in_stream(&filebuf)
#endif
{
line_buffer.reserve(1000);
}
const char* Reader::ReadLine()
{
#ifdef __GLIBCXX__
if (!std::getline(in_stream, line_buffer))
return nullptr;
return line_buffer.c_str();
#else
// a standard implementation
line_buffer.clear();
int ch;
while ((ch = getc(in_file)) != EOF && ch != '\n')
line_buffer += static_cast<char>(ch);
if (ch == EOF && line_buffer.empty())
return nullptr;
return line_buffer.c_str();
#endif
}
FileReader::FileReader(std::string file_name) :
Reader(fopen(file_name.c_str(), "r"))
{
}
FileReader::~FileReader()
{
if (in_file)
fclose(in_file);
}
PipeReader::PipeReader(std::string cmd): Reader(popen(cmd.c_str(), "r"))
{
if (!in_file)
throw std::runtime_error("Error running command: " + cmd);
}
PipeReader::~PipeReader()
{
if (in_file)
pclose(in_file);
}