blob: 36c1e6f069fdfc26063a4cb4eaa584aaeef829c1 [file] [log] [blame]
William A. Kennington III9a512c92022-08-12 15:15:55 -07001#include <stdplus/exception.hpp>
2#include <stdplus/fd/line.hpp>
3#include <stdplus/fd/ops.hpp>
4
5namespace stdplus
6{
7namespace fd
8{
9
10LineReader::LineReader(Fd& fd) : fd(fd)
11{
12}
13
14const std::string* LineReader::readLine()
15{
16 if (hit_eof)
17 {
18 throw exception::Eof("readLine");
19 }
20 if (line_complete)
21 {
22 line.clear();
23 }
24 while (true)
25 {
26 if (buf_data.empty())
27 {
28 try
29 {
30 buf_data = read(fd, buf);
31 if (buf_data.empty())
32 {
33 return nullptr;
34 }
35 }
36 catch (const exception::Eof&)
37 {
38 hit_eof = true;
39 return &line;
40 }
41 }
42 line_complete = false;
43 for (size_t i = 0; i < buf_data.size(); ++i)
44 {
45 if (buf_data[i] == '\n')
46 {
47 line.insert(line.end(), buf_data.begin(), buf_data.begin() + i);
48 buf_data = buf_data.subspan(i + 1);
49 line_complete = true;
50 return &line;
51 }
52 }
53 line.insert(line.end(), buf_data.begin(), buf_data.end());
54 buf_data = {};
55 }
56}
57
58} // namespace fd
59} // namespace stdplus