YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
File.h
浏览该文件的文档.
1 /*
2  © 2009-2014 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 YSL_INC_Service_File_h_
29 #define YSL_INC_Service_File_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YCoreUtilities
33 #include <cwctype>
34 #include YFM_YSLib_Adaptor_YTextBase
35 
36 namespace YSLib
37 {
38 
43 class YF_API File : private noncopyable
44 {
45 private:
47  std::FILE* fp;
48  size_t fsize;
50 
51 public:
56  File();
62  //@
64  explicit
65  File(const char*, const char* = "rb");
67  File(const char*, std::ios_base::openmode);
68  explicit
69  File(const String&, const ucs2_t* = u"rb");
70  File(const String&, std::ios_base::openmode);
72 
76  virtual
77  ~File();
78 
83  PDefHOp(bool, !, ) const ynothrow
84  ImplRet(!bool(*this))
85 
90  explicit DefCvt(const ynothrow, bool, fp)
91 
92  DefGetter(const ynothrow, FILE*, Ptr, fp)
93  DefGetter(const ynothrow, size_t, Size, fsize)
94 
97  DefGetter(const ynothrow, ptrdiff_t, Position, std::ftell(fp))
98 
104  PDefH(int, Seek, ptrdiff_t offset, int whence) const
105  ImplRet(std::fseek(fp, offset, whence))
106 
111  PDefH(int, CheckEOF, ) const
112  ImplRet(std::feof(fp))
113 
114 private:
119  void
120  CheckSize();
121 
122 public:
128  void
129  Close();
130 
139  PDefH(int, Flush, ) const
140  ImplRet(std::fflush(fp))
141 
150  bool
152  Open(const char*, const char* = "rb");
154  bool
155  Open(const char*, std::ios_base::openmode);
156  bool
157  Open(const String&, const ucs2_t* = u"rb");
158  bool
159  Open(const String&, std::ios_base::openmode);
161 
169  PDefH(size_t, Read, void* ptr, size_t size = 1U, size_t nmemb = 1U) const
170  ImplRet(std::fread(ptr, size, nmemb, fp))
171 
175  PDefH(void, Rewind, ) const
176  ImplExpr(std::rewind(fp))
177 
184  virtual bool
185  Truncate(size_t) const;
186 
194  PDefH(size_t, Write, void* ptr, size_t size = 1U, size_t nmemb = 1U) const
195  ImplRet(std::fwrite(ptr, size, nmemb, fp))
196 };
197 
208 template<typename _tChar>
209 File&
210 operator>>(File& f, typename std::char_traits<_tChar>::char_type& c)
211 {
212  YAssert(bool(f), "Invalid file found.");
213 
214  c = std::fgetc(f.GetPtr());
215  return f;
216 }
221 template<typename _tString>
222 File&
223 operator>>(File& f, _tString& str)
224 {
225  YAssert(bool(f), "Invalid file found.");
226 
227  int c;
228 
229  while((c = std::fgetc(f.GetPtr())) > 0 && !std::iswspace(c))
230  str += c;
231  return f;
232 }
233 
238 inline File&
240 {
241  YAssert(bool(f), "Invalid file found.");
242  std::fputc(c, f.GetPtr());
243  return f;
244 }
249 inline File&
250 operator<<(File& f, const char* str)
251 {
252  YAssert(bool(f), "Invalid file found.");
253  std::fputs(str, f.GetPtr());
254  return f;
255 }
261 template<typename _tString>
262 File&
263 operator<<(File& f, const _tString& str)
264 {
265  YAssert(bool(f), "Invalid file found.");
266  std::fputs(reinterpret_cast<const char*>(str.c_str()), f.GetPtr());
267  return f;
268 }
270 
271 } // namespace YSLib;
272 
273 #endif
274 
std::FILE ConversionState fp
Definition: chrproc.h:88
File & operator>>(File &f, _tString &str)
从指定文件读空白符分隔的字符串。
Definition: File.h:223
#define ImplRet(...)
Definition: YBaseMacro.h:97
#define YF_API
Definition: Platform.h:64
文件基类。
Definition: File.h:43
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
不可复制对象:禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: utility.hpp:75
YSLib 标准字符串(使用 UCS-2 作为内部编码)。
Definition: ystring.h:47
#define ImplExpr(...)
Definition: YBaseMacro.h:93
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
_tWidget _fCallable && f
Definition: ywgtevt.h:597
char16_t ucs2_t
UCS-2 字符类型。
Definition: chrdef.h:44
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
File & operator<<(File &f, const Configuration &conf)
std::FILE * fp
Definition: File.h:47
屏幕区域大小。
Definition: ygdibase.h:249
void Close(IWidget &wgt)
Definition: ywidget.cpp:95
#define YAssert(_expr, _msg)
Definition: cassert.h:73