-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.cpp
More file actions
138 lines (107 loc) · 3.58 KB
/
Copy pathdata.cpp
File metadata and controls
138 lines (107 loc) · 3.58 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* File: /lib/impl/stream/data.cpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#include <cstring>
#include <algorithm>
#include <cpp/lib_debug>
#include "./data.hpp"
namespace lib::stream::impl {
// IMPLEMENTATION lib::stream::impl::data
data::data( const lib::data::cbuffer_t & read_buff )
: read_buff {read_buff}
{}
data::data( const lib::data::buffer_t & write_buff )
: read_buff {write_buff}
, write_buff {write_buff}
{}
void data::reset( const lib::data::cbuffer_t & read_buff_/* = {}*/ ) {
read_buff = read_buff_;
write_buff = {};
read_pos_ = write_pos_ = 0;
}
void data::reset( const lib::data::buffer_t & write_buff_ ) {
read_buff = write_buff_;
write_buff = write_buff_;
read_pos_ = write_pos_ = 0;
}
// IMPLEMENTATION lib::stream::impl::data: lib::data::rstream_t
lib::data::result_t data::read( const lib::data::buffer_t & buffer )/* override*/ {
const auto & peek_count = peek( buffer );
CPP_ASSERT( peek_count.success() );
read_pos_ += peek_count;
return peek_count;
}
lib::data::result_t data::peek( const lib::data::buffer_t & buffer )/* override*/ {
const auto & read_size_ = read_size();
CPP_ASSERT( read_size_.success() );
const auto count = ::std::min( buffer.size(), read_size_.value() );
::std::memcpy( buffer.data(), &read_buff[read_pos_], count );
return count;
}
bool data::read_flush()/* override*/ {
read_pos_ = 0;
return true;
}
lib::data::result_t data::read_size()/* override*/ {
return read_buff.size() - read_pos_;
}
lib::data::result_t data::read_pos( usize position )/* override*/ {
return read_pos_ = ::std::min( position, read_buff.size() );
}
lib::data::result_t data::read_pos()/* override*/ {
return read_pos_;
}
lib::data::cbuffer_t data::read_cbuffer( bool flush )/* override*/ {
if ( flush and not read_flush() )
return {};
const auto & read_size_ = read_size();
CPP_ASSERT( read_size_.success() );
return {&read_buff[read_pos_], read_size_.value()};
}
// IMPLEMENTATION lib::stream::impl::data: lib::data::wstream_t
lib::data::result_t data::write( const lib::data::cbuffer_t & buffer )/* override*/ {
const auto & write_size_ = write_size();
CPP_ASSERT( write_size_.success() );
const auto count = ::std::min( buffer.size(), write_size_.value() );
::std::memcpy( &write_buff[write_pos_], buffer.data(), count );
write_pos_ += count;
return count;
}
bool data::write_flush()/* override*/ {
write_pos_ = 0;
return true;
}
lib::data::result_t data::write_size()/* override*/ {
return write_buff.size() - write_pos_;
}
lib::data::result_t data::write_pos( usize position )/* override*/ {
return write_pos_ = ::std::min( position, write_buff.size() );
}
lib::data::result_t data::write_pos()/* override*/ {
return write_pos_;
}
lib::data::buffer_t data::write_buffer( bool flush )/* override*/ {
if ( flush and not write_flush() )
return {};
const auto & write_size_ = write_size();
CPP_ASSERT( write_size_.success() );
return {&write_buff[write_pos_], write_size_.value()};
}
// IMPLEMENTATION lib::stream::impl::data: lib::data::rwstream_t
bool data::flush()/* override*/ {
return read_flush() and write_flush();
}
lib::data::result_t data::size()/* override*/ {
return readonly() ? read_size() : write_size();
}
lib::data::result_t data::pos( usize position )/* override*/ {
return readonly() ? read_pos( position ) : write_pos( position );
}
lib::data::result_t data::pos()/* override*/ {
return readonly() ? read_pos() : write_pos();
}
} // namespace lib::stream::impl