Sese Framework
2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
BaseStreamReader.h
Go to the documentation of this file.
1
// Copyright 2024 libsese
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
22
#pragma once
23
24
#include "
sese/io/ByteBuilder.h
"
25
#include "
sese/io/Stream.h
"
26
#include "
sese/util/Util.h
"
27
28
#include <sstream>
29
30
#ifdef _WIN32
31
#pragma warning(disable : 4251)
32
#endif
33
34
namespace
sese::io
{
38
template
<
typename
T =
char
>
39
class
BaseStreamReader
{
40
public
:
41
using
Ptr
= std::shared_ptr<BaseStreamReader<T>>;
42
43
public
:
48
explicit
BaseStreamReader
(
const
Stream::Ptr
&source) {
49
this->
sourceStream
= source;
50
this->
bufferStream
= std::make_unique<ByteBuilder>();
51
}
52
58
bool
read
(T &ch) {
59
if
(
bufferStream
->read(&ch,
sizeof
(T)) == 0) {
61
auto
len =
preRead
();
62
if
(0 == len) {
64
return
false
;
65
}
66
}
67
return
true
;
68
}
69
74
std::basic_string<T>
readLine
() {
75
std::basic_stringstream<T> string;
76
// auto canReadSize = bufferStream->getLength() - bufferStream->getCurrentReadPos();
77
auto
can_read_size =
bufferStream
->getReadableSize();
78
if
(can_read_size == 0) {
79
can_read_size +=
preRead
();
80
}
81
82
if
(can_read_size > 0) {
83
T ch;
84
while
(
read
(ch)) {
85
if
(ch ==
'\r'
|| ch ==
'\n'
) {
86
auto
empty =
string
.rdbuf()->in_avail() == 0;
87
if
(!empty) {
88
break
;
89
}
else
{
90
continue
;
91
}
92
}
93
string
<< ch;
94
}
95
}
96
bufferStream
->freeCapacity();
97
return
string
.str();
98
}
99
104
const
ByteBuilder::Ptr
&
getBuffer
() noexcept {
return
this->
bufferStream
; }
105
106
private
:
107
114
int64_t
preRead
() {
115
char
buffer[
STREAM_BYTE_STREAM_SIZE_FACTOR
];
116
auto
len =
sourceStream
->read(buffer,
STRING_BUFFER_SIZE_FACTOR
);
117
if
(0 < len) {
118
bufferStream
->write(buffer, len);
119
}
120
return
len;
121
}
122
123
private
:
124
Stream::Ptr
sourceStream
;
125
ByteBuilder::Ptr
bufferStream
;
126
};
127
128
using
StreamReader
=
BaseStreamReader<char>
;
129
using
WStreamReader
=
BaseStreamReader<wchar_t>
;
130
}
// namespace sese::io
sese
io
BaseStreamReader.h
Generated on Tue Jan 7 2025 15:49:05 for Sese Framework by
1.11.0