forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilestream.hpp
286 lines (264 loc) · 5.49 KB
/
filestream.hpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// solid/frame/file/filestream.hpp
//
// Copyright (c) 2014 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef SOLID_FRAME_FILE_STREAM_HPP
#define SOLID_FRAME_FILE_STREAM_HPP
#include "solid/frame/file/filestore.hpp"
#include <streambuf>
#include <istream>
#include <ostream>
#include <iostream>
namespace solid{
namespace frame{
namespace file{
class FileBuf: public std::streambuf{
public:
FileBuf(
FilePointerT &_rptr,
char* _buf = nullptr, size_t _bufcp = 0
);
FileBuf(
char* _buf = nullptr, size_t _bufcp = 0
);
~FileBuf();
FilePointerT& device();
void device(FilePointerT &_rptr);
protected:
/*virtual*/ std::streamsize showmanyc();
/*virtual*/ int_type underflow();
/*virtual*/ int_type uflow();
/*virtual*/ int_type pbackfail(int_type _c = traits_type::eof());
/*virtual*/ int_type overflow(int_type __c = traits_type::eof());
/*virtual*/ pos_type seekoff(
off_type _off, std::ios_base::seekdir _way,
std::ios_base::openmode _mode = std::ios_base::in | std::ios_base::out
);
/*virtual*/ pos_type seekpos(
pos_type _pos,
std::ios_base::openmode _mode = std::ios_base::in | std::ios_base::out
);
/*virtual*/ int sync();
///*virtual*/ void imbue(const locale& _loc);
/*virtual*/ std::streamsize xsgetn(char_type* _s, std::streamsize _n);
/*virtual*/ std::streamsize xsputn(const char_type* _s, std::streamsize _n);
private:
bool hasBuf()const{
return bufcp != 0;
}
bool hasPut()const{
return pptr() != nullptr;
}
bool hasGet()const{
return gptr() != egptr();
}
int writeAll(const char *_s, size_t _n);
void resetBoth(){
char *end = buf + bufcp;
setg(end, end, end);
setp(buf, end - 1);
}
void resetGet(){
char *end = buf + bufcp;
setg(end, end, end);
}
void resetPut(){
char *end = buf + bufcp - 1;
setp(buf, end);
}
bool flushPut();
private:
FilePointerT dev;
char *buf;
const size_t bufcp;
int64_t off;
};
template <size_t BufCp = 0>
class FileIStream;
template <>
class FileIStream<0>: public std::istream{
public:
explicit FileIStream(
FilePointerT &_rdev, const size_t _bufcp = 0
): std::istream(nullptr),
pb(_bufcp ? new char[_bufcp] : nullptr),
buf(_rdev, pb, _bufcp){
rdbuf(&buf);
}
FileIStream(
const size_t _bufcp = 0
): std::istream(nullptr),
pb(_bufcp ? new char[_bufcp] : nullptr),
buf(pb, _bufcp)
{
rdbuf(&buf);
}
~FileIStream(){
delete []pb;
}
FilePointerT& device(){
return buf.device();
}
void device(FilePointerT &_rdev){
buf.device(_rdev);
}
void close(){
buf.pubsync();
buf.device().clear();
}
private:
char *pb;
FileBuf buf;
};
template <size_t BufCp>
class FileIStream: public std::istream{
public:
explicit FileIStream(FilePointerT &_rdev):std::istream(nullptr), buf(_rdev, b, BufCp){
rdbuf(&buf);
}
FileIStream():std::istream(nullptr), buf(b, BufCp){
rdbuf(&buf);
}
FilePointerT& device(){
return buf.device();
}
void device(FilePointerT &_rdev){
buf.device(_rdev);
}
void close(){
buf.pubsync();
buf.device().clear();
}
private:
char b[BufCp];
FileBuf buf;
};
template <size_t BufCp = 0>
class FileOStream;
template <>
class FileOStream<0>: public std::ostream{
public:
explicit FileOStream(
FilePointerT &_rdev,
const size_t _bufcp = 0
): std::ostream(), pb(_bufcp ? new char[_bufcp] : nullptr),
buf(_rdev, pb, _bufcp)
{
rdbuf(&buf);
}
FileOStream(
const size_t _bufcp = 0
): std::ostream(), pb(_bufcp ? new char[_bufcp] : nullptr),
buf(pb, _bufcp)
{
rdbuf(&buf);
}
~FileOStream(){
delete []pb;
}
FilePointerT& device(){
return buf.device();
}
void device(FilePointerT &_rdev){
buf.device(_rdev);
}
void close(){
buf.pubsync();
buf.device().clear();
}
private:
char *pb;
FileBuf buf;
};
template <size_t BufCp>
class FileOStream: public std::ostream{
public:
explicit FileOStream(FilePointerT &_rdev):std::ostream(), buf(_rdev, b, BufCp){
rdbuf(&buf);
}
FileOStream():std::ostream(), buf(b, BufCp){
rdbuf(&buf);
}
FilePointerT& device(){
return buf.device();
}
void device(FilePointerT &_rdev){
buf.device(_rdev);
}
void close(){
buf.pubsync();
buf.device().clear();
}
private:
char b[BufCp];
FileBuf buf;
};
template <size_t BufCp = 0>
class FileIOStream;
template <>
class FileIOStream<0>: public std::iostream{
public:
explicit FileIOStream(
FilePointerT &_rdev,
const size_t _bufcp = 0
): std::iostream(nullptr), pb(_bufcp ? new char[_bufcp] : nullptr),
buf(_rdev, pb, _bufcp)
{
rdbuf(&buf);
}
FileIOStream(
const size_t _bufcp = 0
): std::iostream(nullptr), pb(_bufcp ? new char[_bufcp] : nullptr),
buf(pb, _bufcp)
{
rdbuf(&buf);
}
~FileIOStream(){
delete []pb;
}
FilePointerT& device(){
return buf.device();
}
void device(FilePointerT &_rdev){
buf.device(_rdev);
}
void close(){
buf.pubsync();
buf.device().clear();
}
private:
char *pb;
FileBuf buf;
};
template <size_t BufCp>
class FileIOStream: public std::iostream{
public:
explicit FileIOStream(FilePointerT &_rdev):std::iostream(nullptr), buf(_rdev, b, BufCp){
rdbuf(&buf);
}
FileIOStream():std::iostream(nullptr), buf(b, BufCp){
rdbuf(&buf);
}
FilePointerT& device(){
return buf.device();
}
void device(FilePointerT &_rdev){
buf.device(_rdev);
}
void close(){
buf.pubsync();
buf.device().clear();
}
private:
char b[BufCp];
FileBuf buf;
};
}//namespace file
}//namespace frame
}//namespace solid
#endif