YSTest: cstdio.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

ystdex/cstdio.h
浏览该文件的文档.
1 /*
2 © 2011-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_ystdex_cstdio_h_
29 #define YB_INC_ystdex_cstdio_h_ 1
30
31 #include "cassert.h" // for <cstdio> and yconstraint;
32 #include <cstdarg> // for std::va_list;
33 #include <memory> // for std::unique_ptr;
34 #include <ios> // for std::ios_base::openmode;
35 #include "operators.hpp" // for input_iteratable;
36
37 namespace ystdex
38 {
39
48 YB_API YB_NONNULL(1) size_t
49 vfmtlen(const char*, std::va_list) ynothrow;
50 YB_API YB_NONNULL(1) size_t
51 vfmtlen(const wchar_t*, std::va_list) ynothrow;
53
54
60 inline int
61 setnbuf(std::FILE* stream) ynothrow
62 {
63 yconstraint(stream);
64 return std::setvbuf(stream, {}, _IONBF, 0);
65 }
66
73 YB_API bool
74 fexists(const char*) ynothrow;
75
76
82 using unique_file_ptr = std::unique_ptr<std::FILE, decltype(std::fclose)&>;
83
84
95 YB_API YB_PURE const char*
96 openmode_conv(std::ios_base::openmode) ynothrow;
105 YB_API YB_PURE std::ios_base::openmode
106 openmode_conv(const char*) ynothrow;
108
109
114 class YB_API ifile_iterator
115 : private input_iteratable<ifile_iterator, const byte&>
116 {
117 protected:
118 using traits_type = std::iterator<std::input_iterator_tag, byte, ptrdiff_t,
119 const byte*, const byte&>;
120
121 public:
123
124 using iterator_category = std::input_iterator_tag;
125 using value_type = byte;
126 using difference_type = ptrdiff_t;
127 using pointer = const byte*;
128 using reference = const byte&;
130 using char_type = byte;
132 using int_type = int;
133
134 private:
139 std::FILE* stream{};
140 char_type value;
141
142 public:
149 yconstfn
150 ifile_iterator()
151 : value()
152 {}
159 explicit
160 ifile_iterator(std::FILE* ptr)
161 : stream(ptr)
162 {
163 yconstraint(ptr);
164 ++*this;
165 }
169 yconstfn
170 ifile_iterator(const ifile_iterator&) = default;
171 ~ifile_iterator() = default;
172
174 ifile_iterator&
175 operator=(const ifile_iterator&) = default;
176
178 yconstfn reference
179 operator*() const ynothrow
180 {
181 return value;
182 }
183
192 ifile_iterator&
193 operator++();
194
195 friend yconstfn bool
196 operator==(const ifile_iterator& x, const ifile_iterator& y)
197 {
198 return x.stream == y.stream;
199 }
200
201 yconstfn std::FILE*
202 get_stream() const
203 {
204 return stream;
205 }
206
211 friend bool
212 is_undereferenceable(const ifile_iterator& i) ynothrow
213 {
214 return !i.get_stream();
215 }
216
222 int_type
224 sputbackc(char_type c)
225 {
226 yconstraint(stream);
227 return std::ungetc(c, stream);
228 }
233 int_type
234 sputbackc(char_type c, std::FILE* s)
235 {
236 yconstraint(!stream || stream == s);
237 stream = s;
238 return sputbackc(c);
239 }
240
242 int_type
243 sungetc()
244 {
245 return sputbackc(value);
246 }
248 int_type
249 sungetc(std::FILE* s)
250 {
251 return sputbackc(value, s);
252 }
254 };
255
256
262 class YB_API block_buffer
263 {
264 private:
265 bool need_flush = {};
266 std::unique_ptr<byte[]> content;
267
268 public:
269 block_buffer(size_t n)
270 : content(new byte[n]())
271 {}
272 block_buffer(std::unique_ptr<byte[]> p)
273 : content(std::move(p))
274 {}
275 block_buffer(block_buffer&&) = default;
276
277 block_buffer&
278 operator=(block_buffer&&) = default;
279
280 void
281 clear() ynothrow
282 {
283 flush();
284 content.reset();
285 }
286
287 bool
288 empty() ynothrow
289 {
290 return !content;
291 }
292
301 void
302 fill(size_t offset, size_t n = 1, byte value = {}) ynothrowv;
303
304 void
305 flush() ynothrow
306 {
307 need_flush = {};
308 }
309
310 byte*
311 get() const ynothrow
312 {
313 return content.get();
314 }
315
316 bool
317 modified() const ynothrow
318 {
319 return need_flush;
320 }
321
330 void
331 read(void* dst, size_t offset, size_t n) const ynothrowv;
332
333 void
334 set(std::unique_ptr<byte[]> p) ynothrow
335 {
336 content = std::move(p);
337 }
338
339 friend void
340 swap(block_buffer& x, block_buffer& y) ynothrow
341 {
342 std::swap(x.need_flush, y.need_flush),
343 x.content.swap(y.content);
344 }
345
354 void
355 write(size_t offset, const void* src, size_t n) ynothrowv;
356 };
357
358 } // namespace ystdex;
359
360 #endif
361
ystdex::ifile_iterator::difference_type
ptrdiff_t difference_type
Definition: ystdex/cstdio.h:126
ystdex::ifile_iterator::get_stream
std::FILE * get_stream() const
Definition: ystdex/cstdio.h:202
ystdex::ifile_iterator::pointer
const byte * pointer
Definition: ystdex/cstdio.h:127
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:95
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
ystdex::ifile_iterator::iterator_category
std::input_iterator_tag iterator_category
Definition: ystdex/cstdio.h:124
ystdex::block_buffer::flush
void flush()
Definition: ystdex/cstdio.h:305
ystdex::details::ebases
Definition: operators.hpp:100
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
NPL::x
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term ValueNode &node const ValueNode &node const _type & x
Definition: SContext.h:94
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
ystdex::ifile_iterator::ifile_iterator
ifile_iterator()
无参数构造。
Definition: ystdex/cstdio.h:150
ystdex::block_buffer::modified
bool modified() const
Definition: ystdex/cstdio.h:317
ystdex::ifile_iterator::char_type
byte char_type
Definition: ystdex/cstdio.h:130
ystdex::ifile_iterator::traits_type
std::iterator< std::input_iterator_tag, byte, ptrdiff_t, const byte *, const byte & > traits_type
Definition: ystdex/cstdio.h:119
ystdex::ifile_iterator::sputbackc
int_type sputbackc(char_type c)
向流中写回字符。
Definition: ystdex/cstdio.h:224
ystdex::unique_file_ptr
std::unique_ptr< std::FILE, decltype(std::fclose)& > unique_file_ptr
独占所有权的 C 标准库流指针或实现提供的等价类型。
Definition: ystdex/cstdio.h:82
ystdex::ifile_iterator
基于 ISO C 标准库的流只读迭代器。
Definition: ystdex/cstdio.h:114
ystdex::block_buffer::clear
void clear()
Definition: ystdex/cstdio.h:281
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:527
ystdex::ifile_iterator::ifile_iterator
ifile_iterator(std::FILE *ptr)
构造：使用流指针。
Definition: ystdex/cstdio.h:160
ystdex::ifile_iterator::value
char_type value
Definition: ystdex/cstdio.h:140
ystdex::block_buffer::set
void set(std::unique_ptr< byte[]> p)
Definition: ystdex/cstdio.h:334
ystdex::block_buffer::block_buffer
block_buffer(std::unique_ptr< byte[]> p)
Definition: ystdex/cstdio.h:272
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::setnbuf
int setnbuf(std::FILE *stream)
关闭流缓冲。
Definition: ystdex/cstdio.h:61
ystdex::ifile_iterator::sungetc
int_type sungetc(std::FILE *s)
Definition: ystdex/cstdio.h:249
YSLib::i
return i
Definition: YCoreUtilities.h:176
ystdex::ifile_iterator::int_type
int int_type
Definition: ystdex/cstdio.h:132
ystdex::fexists
bool fexists(const char *)
判断指定路径的文件是否存在。
Definition: cstdio.cpp:59
ystdex::block_buffer::content
std::unique_ptr< byte[]> content
Definition: ystdex/cstdio.h:266
cassert.h
ISO C 断言/调试跟踪扩展。
ystdex::ifile_iterator::sputbackc
int_type sputbackc(char_type c, std::FILE *s)
Definition: ystdex/cstdio.h:234
ystdex::ifile_iterator::value_type
byte value_type
Definition: ystdex/cstdio.h:125
ystdex::vfmtlen
size_t vfmtlen(const char *, std::va_list)
计算指定格式字符串输出的占用的字节数。
Definition: cstdio.cpp:39
ystdex::ifile_iterator::operator==
friend bool operator==(const ifile_iterator &x, const ifile_iterator &y)
Definition: ystdex/cstdio.h:196
operators.hpp
重载操作符。
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
ystdex::block_buffer::swap
friend void swap(block_buffer &x, block_buffer &y)
Definition: ystdex/cstdio.h:340
ystdex::block_buffer::block_buffer
block_buffer(size_t n)
Definition: ystdex/cstdio.h:269
std
Definition: memory.hpp:1183
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:834
ystdex::ifile_iterator::operator*
reference operator*() const
Definition: ystdex/cstdio.h:179
CHRLib::c
c
Definition: CharacterProcessing.h:81
ystdex::ifile_iterator::sungetc
int_type sungetc()
Definition: ystdex/cstdio.h:243
ystdex::ifile_iterator::is_undereferenceable
friend bool is_undereferenceable(const ifile_iterator &i)
Definition: ystdex/cstdio.h:212
ystdex::ifile_iterator::stream
std::FILE * stream
流指针。
Definition: ystdex/cstdio.h:139
YB_API
#define YB_API
YBase 应用程序编程接口：用于向库文件约定链接。
Definition: ydef.h:592
YSLib::IO::int
YF_API int
Definition: File.h:51
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:88
ystdex::block_buffer::empty
bool empty()
Definition: ystdex/cstdio.h:288
ystdex::openmode_conv
const char * openmode_conv(std::ios_base::openmode)
ISO C/C++ 标准输入输出接口打开模式转换。
Definition: cstdio.cpp:68
ystdex::write
std::basic_ostream< _tChar, typename _tString::traits_type > & write(std::basic_ostream< _tChar, typename _tString::traits_type > &os, const _tString &str, typename _tString::size_type pos=0, typename _tString::size_type n=_tString::npos)
非格式输出。
Definition: string.hpp:981
ystdex::block_buffer
块缓冲。
Definition: ystdex/cstdio.h:262
ystdex::ifile_iterator::reference
const byte & reference
Definition: ystdex/cstdio.h:128
CHRLib::p
const auto p(d)