YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
yfile.h
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2009 - 2013.
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_Core_yfile_h_
29 #define YSL_INC_Core_yfile_h_ 1
30 
31 #include "../Core/ycutil.h"
32 #include <cwctype>
33 #include "../Adaptor/YTextBase.h"
34 
36 
41 class YF_API File : private noncopyable
42 {
43 private:
45  std::FILE* fp;
46 
47  size_t fsize;
48 
49 public:
54  File();
60  //@
61  explicit
62  File(const_path_t, const char* = "rb");
63  File(const_path_t, std::ios_base::openmode);
64  explicit
65  File(const String&, const ucs2_t* = u"rb");
66  File(const String&, std::ios_base::openmode);
68 
72  virtual
73  ~File();
74 
79  PDefHOp(bool, !, ) const ynothrow
80  ImplRet(!bool(*this))
81 
86  explicit DefCvt(const ynothrow, bool, fp)
87 
88  DefGetter(const ynothrow, FILE*, Ptr, fp)
89  DefGetter(const ynothrow, size_t, Size, fsize)
90 
93  DefGetter(const ynothrow, ptrdiff_t, Position, std::ftell(fp))
94 
100  PDefH(int, Seek, ptrdiff_t offset, int whence) const
101  ImplRet(std::fseek(fp, offset, whence))
102 
107  PDefH(int, CheckEOF, ) const
108  ImplRet(std::feof(fp))
109 
110 private:
115  void
116  CheckSize();
117 
118 public:
124  void
125  Close();
126 
135  PDefH(int, Flush, ) const
136  ImplRet(std::fflush(fp))
137 
146  bool
147  Open(const_path_t, const char* = "rb");
148  bool
149  Open(const_path_t, std::ios_base::openmode);
150  bool
151  Open(const String&, const ucs2_t* = u"rb");
152  bool
153  Open(const String&, std::ios_base::openmode);
155 
163  PDefH(size_t, Read, void* ptr, size_t size = 1U, size_t nmemb = 1U) const
164  ImplRet(std::fread(ptr, size, nmemb, fp))
165 
169  PDefH(void, Rewind, ) const
170  ImplRet(std::rewind(fp))
171 
178  virtual bool
179  Truncate(size_t) const;
180 
188  PDefH(size_t, Write, void* ptr, size_t size = 1U, size_t nmemb = 1U) const
189  ImplRet(std::fwrite(ptr, size, nmemb, fp))
190 };
191 
198 template<typename _tChar>
199 File&
200 operator>>(File& f, typename std::char_traits<_tChar>::char_type& c)
201 {
202  YAssert(bool(f), "Invalid file found.");
203 
204  const auto fp(f.GetPtr());
205 
206  c = std::fgetc(fp);
207  return f;
208 }
215 template<typename _tString>
216 File&
217 operator>>(File& f, _tString& str)
218 {
219  YAssert(bool(f), "Invalid file found.");
220 
221  const auto fp(f.GetPtr());
222  int c;
223 
224  while((c = std::fgetc(fp)) > 0 && !std::iswspace(c))
225  str += c;
226  return f;
227 }
228 
229 
236 inline File&
237 operator<<(File& f, char c)
238 {
239  YAssert(bool(f), "Invalid file found.");
240 
241  std::fputc(c, f.GetPtr());
242  return f;
243 }
250 inline File&
251 operator<<(File& f, const char* str)
252 {
253  YAssert(bool(f), "Invalid file found.");
254 
255  std::fputs(str, f.GetPtr());
256  return f;
257 }
265 template<typename _tString>
266 File&
267 operator<<(File& f, const _tString& str)
268 {
269  YAssert(bool(f), "Invalid file found.");
270 
271  std::fputs(reinterpret_cast<const char*>(str.c_str()), f.GetPtr());
272  return f;
273 }
274 
275 YSL_END
276 
277 #endif
278