fastdo  0.5.12
filesys.hpp
浏览该文件的文档.
1 #ifndef __FILESYS_HPP__
2 #define __FILESYS_HPP__
3 
5 #if defined(_MSC_VER) || defined(WIN32)
6 
7 #else
8 #include <dirent.h>
9 #endif
10 
11 namespace winux
12 {
13 
14 // 特殊平台变量 -------------------------------------------------------------
15 #if defined(_MSC_VER) || defined(WIN32)
16 String const dirSep = "\\";
17 String const lineSep = "\r\n";
18 String const pathEnvSep = ";";
19 String const DirSep = "\\";
20 String const LineSep = "\r\n";
21 String const PathEnvSep = ";";
22 #else
23 String const dirSep = "/";
24 String const lineSep = "\n";
25 String const pathEnvSep = ":";
26 String const DirSep = "/";
27 String const LineSep = "\n";
28 String const PathEnvSep = ":";
29 #endif
30 
33 
39 WINUX_FUNC_DECL(String) FilePath( String const & fullPath, String * fileName = NULL );
40 
46 WINUX_FUNC_DECL(String) FileTitle( String const & fileName, String * extName = NULL );
47 
49 WINUX_FUNC_DECL(bool) IsAbsPath( String const & path );
50 
53 
55 WINUX_FUNC_DECL(String) RealPath( String const & path );
56 
59 
61 WINUX_FUNC_DECL(bool) SetCurrentDir( String const & path );
62 
64 WINUX_FUNC_DECL(bool) IsDir( String const & path );
65 
69 WINUX_FUNC_DECL(bool) DetectPath( String const & path, bool * isDir = NULL );
70 
72 WINUX_FUNC_DECL(ulong) FileSize( String const & filename );
74 WINUX_FUNC_DECL(uint64) FileSize64( String const & filename );
75 
77 WINUX_FUNC_DECL(bool) FileTime( String const & filename, ulong * ctime, ulong * mtime, ulong * atime );
79 WINUX_FUNC_DECL(ulong) FileCTime( String const & filename );
81 WINUX_FUNC_DECL(ulong) FileMTime( String const & filename );
83 WINUX_FUNC_DECL(ulong) FileATime( String const & filename );
84 
86 WINUX_FUNC_DECL(bool) FileTouch( String const & filename, ulong time = (ulong)-1, ulong atime = (ulong)-1 );
87 
89 WINUX_FUNC_DECL(String) PathWithSep( String const & path );
90 WINUX_FUNC_DECL(String) PathNoSep( String const & path );
93 
95 WINUX_FUNC_DECL(String) CombinePath( String const & dirPath, String const & fileName );
96 
98 WINUX_FUNC_DECL(void) FolderData( String const & path, StringArray * fileArr, StringArray * subFolderArr, int sortType = 0 );
99 
104 WINUX_FUNC_DECL(ulong) EnumFiles( String const & path, Mixed const & ext, StringArray * arrFiles, bool isRecursive = false );
105 
107 WINUX_FUNC_DECL(ulong) CommonDelete( String const & path );
108 
116 WINUX_FUNC_DECL(bool) MakeDirExists( String const & path, int mode = 0755 );
117 
119 WINUX_FUNC_DECL(AnsiString) FileGetContents( String const & filename, bool textMode = true );
120 
122 WINUX_FUNC_DECL(Buffer) FileGetContentsEx( String const & filename, bool textMode );
123 
125 WINUX_FUNC_DECL(bool) FilePutContents( String const & filename, AnsiString const & content, bool textMode = true );
126 
128 WINUX_FUNC_DECL(bool) FilePutContentsEx( String const & filename, Buffer const & content, bool textMode );
129 
131 WINUX_FUNC_DECL(void) WriteLog( String const & s );
132 
134 WINUX_FUNC_DECL(void) WriteBinLog( void const * data, int size );
135 
136 //#define __LOG__
137 #ifdef __LOG__
138 #define LOG(s) winux::WriteLog(s)
139 #define BIN_LOG(d,s) winux::WriteBinLog((d),(s))
140 #else
141 #define LOG(s)
142 #define BIN_LOG(d,s)
143 #endif
144 
145 #ifndef interface
146 #define interface struct
147 #endif
148 
151 {
152 public:
153  enum
154  {
158  };
159  FileSysError( int errType, AnsiString const & s ) throw() : Error( errType, s ) { }
160 };
161 
164 {
165 private:
166  String _path;
167  String _name;
168 #if defined(_MSC_VER) || defined(WIN32)
169  WIN32_FIND_DATA _wfd;
170  SimpleHandle<HANDLE> _findFile;
171  bool _first;
172 #else
173  SimpleHandle< DIR*> _findFile;
174 #endif
175 public:
176  DirIterator( String const & path );
178  String const & getPath() const { return _path; }
180  String const & getName() const { return _name; }
182  String getFullPath() const;
184  String getRealPath() const;
186  bool isDir() const;
188  bool next();
189 
191 };
192 
194 interface WINUX_DLL IFile
195 {
196  virtual ~IFile() { }
197 
199  virtual bool open( String const & filename, String const & mode );
201  virtual bool close();
203  virtual winux::ulong read( void * buf, winux::ulong size );
205  virtual winux::ulong write( void const * data, winux::ulong size );
206  virtual winux::ulong write( Buffer const & buf );
208  virtual bool rewind();
210  virtual bool seek( long offset );
212  virtual winux::ulong tell();
214  virtual String getLine();
216  virtual int puts( String const & str );
218  virtual bool eof();
220  virtual winux::ulong size();
222  virtual void * buffer( winux::ulong * size );
224  virtual AnsiString buffer();
225 };
226 
228 class WINUX_DLL File : public IFile
229 {
230 protected:
232  FILE * _fp;
233  bool _autoload; // 当以读取模式打开时,是否自动载入数据到缓冲区
234  winux::ulong _fileSize; // 文件的字节大小,这玩意和数据加载大小不一定相同
235  winux::ulong _loadedSize; // 实际加载的字节大小,这玩意和文件大小不一定相同
237 
238  void _loadData();
239 public:
240  File( String const & filename, String const & mode, bool autoload = true );
241  virtual ~File();
242 
243  virtual bool open( String const & filename, String const & mode );
244  virtual bool close();
245  virtual winux::ulong read( void * buf, winux::ulong size );
246  virtual winux::ulong write( void const * data, winux::ulong size );
247  virtual winux::ulong write( Buffer const & buf );
248  virtual bool rewind();
249  virtual bool seek( long offset );
250  virtual winux::ulong tell();
251  virtual String getLine();
252  virtual int puts( String const & str );
253  virtual bool eof();
254  virtual winux::ulong size();
255  virtual void * buffer( winux::ulong * size );
256  virtual AnsiString buffer();
257 
258  winux::ulong loadedSize() const { return _loadedSize; }
259  operator bool() const { return _fp != NULL; }
260 
262 };
263 
266 {
267 protected:
272  long _fileno;
273 
274  bool _isTextMode;
276 
277 public:
278  BlockOutFile( String const & filename, bool isTextMode = true, winux::ulong blockSize = 1048576 );
279 
280  bool nextBlock();
281  virtual winux::ulong write( void const * data, winux::ulong size );
282  virtual winux::ulong write( Buffer const & buf );
283  int puts( String const & str );
284 
286 };
287 
289 class WINUX_DLL BlockInFile : public File
290 {
291 protected:
296  long _index;
297 
298  bool _isTextMode;
300 public:
301  BlockInFile( String const & filename, bool isTextMode = true );
302 
303  bool nextBlock();
304  virtual bool eof();
305 
307 };
308 
309 }
310 
311 #endif //__FILESYS_HPP__
String PathWithSep(String const &path)
路径分隔符整理
StringArray _blockFiles
要加载的分块文件
Definition: filesys.hpp:299
String _filetitle
文件标题 filename
Definition: filesys.hpp:270
winux::ulong _loadedSize
Definition: filesys.hpp:235
winux::ulong _blockSize
块大小
Definition: filesys.hpp:275
bool IsAbsPath(String const &path)
判断是否为绝对路径
void FolderData(String const &path, StringArray *fileArr, StringArray *subFolderArr, int sortType=0)
获取文件夹中的文件和子文件夹,sortType:0结果不排序 1正序 2反序
String _filename
Definition: filesys.hpp:231
String _basename
文件名 filename.txt
Definition: filesys.hpp:293
bool FilePutContents(String const &filename, AnsiString const &content, bool textMode=true)
把AnsiString内容写入文件,textMode表示是否为文本模式
bool FileTime(String const &filename, ulong *ctime, ulong *mtime, ulong *atime)
获取文件时间
#define WINUX_DLL
Definition: utilities.hpp:57
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
long _index
文件索引
Definition: filesys.hpp:296
bool SetCurrentDir(String const &path)
设置当前工作目录
FileSysError(int errType, AnsiString const &s)
Definition: filesys.hpp:159
String NormalizePath(String const &path)
使路径规则化(末尾不带路径分割符)
String const & getName() const
取得文件名
Definition: filesys.hpp:180
String FilePath(String const &fullPath, String *fileName=NULL)
获取路径名(末尾不含目录分隔符)
String const & getPath() const
取得路径
Definition: filesys.hpp:178
分块输入文件
Definition: filesys.hpp:289
目录文件枚举器
Definition: filesys.hpp:163
String _extname
扩展名 .txt
Definition: filesys.hpp:295
文件系统错误类
Definition: filesys.hpp:150
String const lineSep
行分割符
Definition: filesys.hpp:17
文件操作类
Definition: filesys.hpp:228
Buffer _buf
Definition: filesys.hpp:236
文件系统自身的错误
Definition: filesys.hpp:157
String _filetitle
文件标题 filename
Definition: filesys.hpp:294
bool MakeDirExists(String const &path, int mode=0755)
确保目录路径的存在性,如果不存在则创建。
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:78
bool _autoload
Definition: filesys.hpp:233
String PathNoSep(String const &path)
std::vector< String > StringArray
Definition: utilities.hpp:171
long _fileno
文件编号
Definition: filesys.hpp:272
bool FileTouch(String const &filename, ulong time=(ulong)-1, ulong atime=(ulong)-1)
更新文件修改时间,访问时间
String const DirSep
目录分割符
Definition: filesys.hpp:19
void WriteLog(String const &s)
日志
String _dirname
目录名 path/to
Definition: filesys.hpp:292
String _basename
文件名 filename.txt
Definition: filesys.hpp:269
void WriteBinLog(void const *data, int size)
二进制日志
winux::ulong loadedSize() const
Definition: filesys.hpp:258
缓冲区,表示内存中一块2进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:436
bool _isTextMode
是否文本模式
Definition: filesys.hpp:298
bool FilePutContentsEx(String const &filename, Buffer const &content, bool textMode)
把Buffer内容写入文件,textMode表示是否为文本模式
String CombinePath(String const &dirPath, String const &fileName)
把一个目录路径和一个文件名组合成一个新路径
ulong EnumFiles(String const &path, Mixed const &ext, StringArray *arrFiles, bool isRecursive=false)
在指定路径下枚举指定扩展名的文件
String _extname
扩展名 .txt
Definition: filesys.hpp:271
bool DetectPath(String const &path, bool *isDir=NULL)
探测一个路径是存在还是不存在,是目录还是文件
winux::ulong _fileSize
Definition: filesys.hpp:234
bool IsDir(String const &path)
判断是否是一个目录
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:61
bool _isTextMode
是否文本模式
Definition: filesys.hpp:274
String FileTitle(String const &fileName, String *extName=NULL)
获取文件标题
ulong FileCTime(String const &filename)
获取文件创建时间
String GetCurrentDir(void)
返回当前工作目录(末尾不含目录分隔符)
ulong FileSize(String const &filename)
获取文件大小
String RealPath(String const &path)
计算真实路径
String GetExecutablePath(void)
获取可执行文件的全路径
FILE * _fp
Definition: filesys.hpp:232
混合体,能表示多种类型的值
Definition: utilities.hpp:586
错误类
Definition: utilities.hpp:413
分块输出文件
Definition: filesys.hpp:265
AnsiString FileGetContents(String const &filename, bool textMode=true)
载入文件内容为一个AnsiString,textMode表示是否为文本模式
ulong CommonDelete(String const &path)
通用删除,删除文件夹和文件,返回删除的文件夹和文件数
unsigned long long uint64
Definition: utilities.hpp:146
文件接口
Definition: filesys.hpp:194
unsigned long ulong
Definition: utilities.hpp:129
String const dirSep
目录分割符
Definition: filesys.hpp:16
String const PathEnvSep
PATH环境变量路径分割符
Definition: filesys.hpp:21
uint64 FileSize64(String const &filename)
获取文件大小(大于4GB的文件)
virtual ~IFile()
Definition: filesys.hpp:196
String const LineSep
行分割符
Definition: filesys.hpp:20
ulong FileMTime(String const &filename)
获取文件修改时间
std::basic_string< tchar > String
Definition: utilities.hpp:162
ulong FileATime(String const &filename)
获取文件访问时间
String _dirname
目录名 path/to
Definition: filesys.hpp:268
String const pathEnvSep
PATH环境变量路径分割符
Definition: filesys.hpp:18
Buffer FileGetContentsEx(String const &filename, bool textMode)
载入文件内容为一个Buffer,textMode表示是否为文本模式
跨平台基础功能库
Definition: archives.hpp:7