YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
File.cpp
浏览该文件的文档.
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 #include "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_File
30 #include YFM_YSLib_Service_FileSystem
31 
32 namespace YSLib
33 {
34 
36  : fp(), fsize(0)
37 {}
38 File::File(const char* filename, const char* mode)
39  : File()
40 {
41  if(Open(filename, mode))
42  {
43  Seek(0, SEEK_END);
44  fsize = GetPosition();
45  Rewind();
46  }
47 }
48 File::File(const char* filename, std::ios_base::openmode mode)
49  : File(filename, ystdex::openmode_conv(mode))
50 {}
51 File::File(const String& filename, const ucs2_t* mode)
52  : File()
53 {
54  if(Open(filename, mode))
55  {
56  Seek(0, SEEK_END);
57  fsize = GetPosition();
58  Rewind();
59  }
60 }
61 File::File(const String& filename, std::ios_base::openmode mode)
62  : File(filename, String(ystdex::openmode_conv(mode)).c_str())
63 {}
64 
66 {
67  Close();
68 }
69 
70 void
71 File::CheckSize()
72 {
73  Seek(0, SEEK_END);
74  fsize = GetPosition();
75  Rewind();
76 }
77 
78 void
80 {
81  if(*this)
82  std::fclose(fp);
83 }
84 
85 bool
86 File::Open(const char* filename, const char* mode)
87 {
88  Close();
89  if((fp = ufopen(filename, mode)))
90  CheckSize();
91  return fp;
92 }
93 bool
94 File::Open(const char* filename, std::ios_base::openmode mode)
95 {
96  return Open(filename, ystdex::openmode_conv(mode));
97 }
98 bool
99 File::Open(const String& filename, const ucs2_t* mode)
100 {
101  Close();
102  if((fp = ufopen(filename.c_str(), mode)))
103  CheckSize();
104  return fp;
105 }
106 bool
107 File::Open(const String& filename, std::ios_base::openmode mode)
108 {
109  return Open(filename, String(ystdex::openmode_conv(mode)).c_str());
110 }
111 
112 bool
113 File::Truncate(size_t size) const
114 {
115  return truncate(GetPtr(), size);
116 }
117 
118 } // namespace YSLib;
119 
std::FILE ConversionState fp
Definition: chrproc.h:88
YF_API bool truncate(std::FILE *, std::size_t) ynothrow
截断文件至指定长度。
void Close()
检查文件有效长度后读位置返回文件起始。
Definition: File.cpp:79
文件基类。
Definition: File.h:43
bool Open(const String &, const ucs2_t *=u"rb")
Definition: File.cpp:99
const char * openmode_conv(std::ios_base::openmode)
Definition: cstdio.cpp:49
YSLib 标准字符串(使用 UCS-2 作为内部编码)。
Definition: ystring.h:47
fp fsize Seek
Definition: File.h:104
File()
构造。
Definition: File.cpp:35
virtual ~File()
析构。
Definition: File.cpp:65
YF_API std::FILE * ufopen(const char *filename, const char *mode) ynothrow
以 UTF-8 文件名打开文件。
size_t fsize
文件大小。
Definition: File.h:49
char16_t ucs2_t
UCS-2 字符类型。
Definition: chrdef.h:44
std::FILE * fp
Definition: File.h:47