BufferedReader.cxx 3.12 KB
Newer Older
1
/*
2
 * Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com>
3
 *
4 5 6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
7
 *
8 9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 29 30 31 32 33
 */

#include "BufferedReader.hxx"
#include "Reader.hxx"
#include "util/TextFile.hxx"

34
#include <cstdint>
35 36 37 38
#include <stdexcept>

#include <string.h>

39 40 41 42 43 44 45
bool
BufferedReader::Fill(bool need_more)
{
	if (eof)
		return !need_more;

	auto w = buffer.Write();
46
	if (w.empty()) {
47 48 49 50 51
		if (buffer.GetCapacity() >= MAX_SIZE)
			return !need_more;

		buffer.Grow(buffer.GetCapacity() * 2);
		w = buffer.Write();
52
		assert(!w.empty());
53 54
	}

55
	size_t nbytes = reader.Read(w.data, w.size);
56 57 58 59 60 61 62 63 64
	if (nbytes == 0) {
		eof = true;
		return !need_more;
	}

	buffer.Append(nbytes);
	return true;
}

65 66 67 68 69 70 71 72 73 74 75 76 77
void *
BufferedReader::ReadFull(size_t size)
{
	while (true) {
		auto r = Read();
		if (r.size >= size)
			return r.data;

		if (!Fill(true))
			throw std::runtime_error("Premature end of file");
	}
}

78
size_t
79
BufferedReader::ReadFromBuffer(WritableBuffer<void> dest) noexcept
80 81 82 83
{
	auto src = Read();
	size_t nbytes = std::min(src.size, dest.size);
	memcpy(dest.data, src.data, nbytes);
84
	Consume(nbytes);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	return nbytes;
}

void
BufferedReader::ReadFull(WritableBuffer<void> _dest)
{
	auto dest = WritableBuffer<uint8_t>::FromVoid(_dest);
	assert(dest.size == _dest.size);

	while (true) {
		size_t nbytes = ReadFromBuffer(dest.ToVoid());
		dest.skip_front(nbytes);
		if (dest.size == 0)
			break;

		if (!Fill(true))
			throw std::runtime_error("Premature end of file");
	}
}

105 106 107 108 109
char *
BufferedReader::ReadLine()
{
	do {
		char *line = ReadBufferedLine(buffer);
110 111
		if (line != nullptr) {
			++line_number;
112
			return line;
113
		}
114 115
	} while (Fill(true));

116
	if (!eof || buffer.empty())
117 118 119
		return nullptr;

	auto w = buffer.Write();
120
	if (w.empty()) {
121 122
		buffer.Grow(buffer.GetCapacity() + 1);
		w = buffer.Write();
123
		assert(!w.empty());
124 125 126 127 128 129 130
	}

	/* terminate the last line */
	w[0] = 0;

	char *line = buffer.Read().data;
	buffer.Clear();
131
	++line_number;
132 133
	return line;
}