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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ags/shared/util/math.h"
#include "ags/shared/util/stream.h"
#include "ags/shared/util/text_stream_reader.h"
namespace AGS3 {
namespace AGS {
namespace Shared {
TextStreamReader::TextStreamReader(Stream *stream)
: _stream(stream) {
}
TextStreamReader::~TextStreamReader() {
// TODO: use shared ptr
delete _stream;
}
bool TextStreamReader::IsValid() const {
return _stream && _stream->CanRead();
}
const Stream *TextStreamReader::GetStream() const {
return _stream;
}
void TextStreamReader::ReleaseStream() {
_stream = nullptr;
}
bool TextStreamReader::EOS() const {
return _stream ? _stream->EOS() : true;
}
char TextStreamReader::ReadChar() {
return _stream->ReadInt8();
}
String TextStreamReader::ReadString(size_t length) {
// TODO: remove carriage-return characters
return String::FromStreamCount(_stream, length);
}
String TextStreamReader::ReadLine() {
// TODO
// Probably it is possible to group Stream::ReadString with this,
// both use similar algorythm, difference is only in terminator chars
String str;
int chars_read_last = 0;
int line_break_position = -1;
// Read a chunk of memory to buffer and seek for null-terminator,
// if not found, repeat until EOS
const int single_chunk_length = 3000;
const int max_chars = 5000000;
char char_buffer[single_chunk_length + 1];
do {
chars_read_last = _stream->Read(char_buffer, single_chunk_length);
char *seek_ptr = char_buffer;
int c;
for (c = 0; c < chars_read_last && *seek_ptr != '\n'; ++c, ++seek_ptr);
int append_length = 0;
int str_len = str.GetLength();
if (c < chars_read_last && *seek_ptr == '\n') {
line_break_position = seek_ptr - char_buffer;
if (str_len < max_chars) {
append_length = MIN(line_break_position, max_chars - str_len);
}
} else {
append_length = MIN(chars_read_last, max_chars - str_len);
}
if (append_length > 0) {
char_buffer[append_length] = '\0';
str.Append(char_buffer);
}
} while (!EOS() && line_break_position < 0);
// If null-terminator was found make sure stream is positioned at the next
// byte after line end
if (line_break_position >= 0) {
// CHECKME: what if stream does not support seek? need an algorythm fork for that
// the seek offset should be negative
_stream->Seek(line_break_position - chars_read_last + 1 /* beyond line feed */);
}
str.TrimRight('\r'); // skip carriage-return, if any
return str;
}
String TextStreamReader::ReadAll() {
size_t len = Math::InRangeOrDef<size_t>((size_t)(_stream->GetLength() - _stream->GetPosition()), SIZE_MAX);
return ReadString(len);
}
} // namespace Shared
} // namespace AGS
} // namespace AGS3
|