Loading...
Searching...
No Matches
line_stream.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include <cstring>
26
27#include "line_stream.hpp"
28
29namespace bitpit {
30
40 : m_buffer(CHUNK_SIZE)
41{
42}
43
49int LineBuffer::readLine(std::ifstream &fileHandle)
50{
51 int lineLength = 0;
52 while (true) {
53 if (m_buffer.size() - lineLength < CHUNK_SIZE) {
54 m_buffer.resize(m_buffer.size() + CHUNK_SIZE);
55 }
56
57 fileHandle.getline(m_buffer.data() + lineLength, CHUNK_SIZE);
58 lineLength = strlen(m_buffer.data());
59
60 // If we haven't find the end of the line, we keep reading
61 if (fileHandle.good() || fileHandle.eof() || fileHandle.bad()) {
62 break;
63 } else if (fileHandle.fail()) {
64 fileHandle.clear();
65 }
66 }
67
68 setg(m_buffer.data(), m_buffer.data(), m_buffer.data() + lineLength);
69
70 if ((fileHandle.eof() || fileHandle.bad()) && lineLength == 0) {
71 return -1;
72 } else {
73 return lineLength;
74 }
75}
76
82void LineBuffer::copyLine(std::string *line) const
83{
84 line->assign(eback(), egptr() - eback());
85}
86
96 : LineBuffer(), std::istream(this)
97{
98}
99
105int LineStream::readLine(std::ifstream &fileHandle)
106{
107 std::istream::clear();
108
109 return static_cast<LineBuffer *>(std::istream::rdbuf())->readLine(fileHandle);
110}
111
112}
LineBuffer defines a buffer for reading lines.
int readLine(std::ifstream &fileHandle)
void copyLine(std::string *line) const
int readLine(std::ifstream &fileHandle)
--- layout: doxygen_footer ---