fastdo  0.6.16
filesys.hpp
浏览该文件的文档.
1 #ifndef __FILESYS_HPP__
2 #define __FILESYS_HPP__
3 //
4 // filesys 文件系统相关的功能
5 //
6 
7 #if defined(_MSC_VER) || defined(WIN32)
8 
9 #else
10 //#include <dirent.h> // opendir(), readdir()
11 #include <glob.h> // glob()
12 #endif
13 
14 namespace winux
15 {
16 // 特殊平台变量 -------------------------------------------------------------
17 String const DirSep_WIN = $T("\\");
18 String const LineSep_WIN = $T("\r\n");
19 String const PathEnvSep_WIN = $T(";");
20 String const DirSep_UNIX = $T("/");
21 String const LineSep_UNIX = $T("\n");
22 String const PathEnvSep_UNIX = $T(":");
23 String const DirSep_DARWIN = $T("/");
24 String const LineSep_DARWIN = $T("\r");
25 String const PathEnvSep_DARWIN = $T(":");
26 
27 #if defined(OS_WIN)
28 String const DirSep = $T("\\");
29 String const LineSep = $T("\r\n");
30 String const PathEnvSep = $T(";");
31 #elif defined(OS_DARWIN)
32 String const DirSep = $T("/");
33 String const LineSep = $T("\r");
34 String const PathEnvSep = $T(":");
35 #else
36 String const DirSep = $T("/");
37 String const LineSep = $T("\n");
38 String const PathEnvSep = $T(":");
39 #endif
40 
43 
49 WINUX_FUNC_DECL(String) FilePath( String const & fullPath, String * fileName = NULL );
50 
56 WINUX_FUNC_DECL(String) FileTitle( String const & fileName, String * extName = NULL );
57 
59 WINUX_FUNC_DECL(bool) IsAbsPath( String const & path );
60 
63 
65 WINUX_FUNC_DECL(String) RealPath( String const & path );
66 
70 WINUX_FUNC_DECL(String) RealPathEx( String const & path, String const & workDirAbsPath );
71 
74 
76 WINUX_FUNC_DECL(bool) SetCurrentDir( String const & path );
77 
79 WINUX_FUNC_DECL(bool) IsDir( String const & path );
80 
84 WINUX_FUNC_DECL(bool) DetectPath( String const & path, bool * isDir = NULL );
85 
87 WINUX_FUNC_DECL(uint64) FileSize( String const & filename );
88 
96 WINUX_FUNC_DECL(bool) FileTime( String const & filename, time_t * ctime, time_t * mtime, time_t * atime );
98 WINUX_FUNC_DECL(time_t) FileCTime( String const & filename );
100 WINUX_FUNC_DECL(time_t) FileMTime( String const & filename );
102 WINUX_FUNC_DECL(time_t) FileATime( String const & filename );
103 
105 WINUX_FUNC_DECL(bool) FileTouch( String const & filename, time_t time = (time_t)-1, time_t atime = (time_t)-1 );
106 
108 WINUX_FUNC_DECL(String) PathWithSep( String const & path );
110 WINUX_FUNC_DECL(String) PathNoSep( String const & path );
115 
119 WINUX_FUNC_DECL(String) CombinePath( String const & dirPath, String const & fileName );
120 
122 WINUX_FUNC_DECL(void) FolderData( String const & path, StringArray * fileArr, StringArray * subFolderArr, int sortType = 0 );
123 
131 WINUX_FUNC_DECL(size_t) EnumFiles( String const & path, Mixed const & ext, StringArray * arrFiles, bool isRecursive = false );
132 
138 WINUX_FUNC_DECL(bool) UnlinkFile( String const & filePath );
139 
141 WINUX_FUNC_DECL(bool) RemoveDir( String const & dirPath );
142 
146 WINUX_FUNC_DECL(bool) RemovePath( String const & path );
147 
149 WINUX_FUNC_DECL(bool) RenamePath( String const & oldPath, String const & newPath );
150 
152 WINUX_FUNC_DECL(size_t) CommonDelete( String const & path );
153 
161 WINUX_FUNC_DECL(bool) MakeDirExists( String const & path, int mode = 0755 );
162 
164 
166 {
177 };
178 
184 WINUX_FUNC_DECL(FileEncoding) RecognizeFileEncoding( winux::Buffer const & content, size_t * pIndex, size_t limitSize = -1 );
185 
187 template < typename _ChTy >
188 XString<_ChTy> NewlineFromFile( _ChTy const * content, size_t len, bool b );
189 template <>
190 WINUX_FUNC_DECL(XString<char>) NewlineFromFile( char const * content, size_t len, bool b );
191 template <>
192 WINUX_FUNC_DECL(XString<wchar>) NewlineFromFile( wchar const * content, size_t len, bool b );
193 template <>
194 WINUX_FUNC_DECL(XString<char16>) NewlineFromFile( char16 const * content, size_t len, bool b );
195 template <>
196 WINUX_FUNC_DECL(XString<char32>) NewlineFromFile( char32 const * content, size_t len, bool b );
197 
199 template < typename _ChTy >
200 XString<_ChTy> NewlineToFile( _ChTy const * content, size_t len, bool b );
201 template <>
202 WINUX_FUNC_DECL(XString<char>) NewlineToFile( char const * content, size_t len, bool b );
203 template <>
204 WINUX_FUNC_DECL(XString<wchar>) NewlineToFile( wchar const * content, size_t len, bool b );
205 template <>
206 WINUX_FUNC_DECL(XString<char16>) NewlineToFile( char16 const * content, size_t len, bool b );
207 template <>
208 WINUX_FUNC_DECL(XString<char32>) NewlineToFile( char32 const * content, size_t len, bool b );
209 
216 WINUX_FUNC_DECL(String) FileGetString( String const & filename, FileEncoding encoding = feMultiByte );
217 
219 WINUX_FUNC_DECL(AnsiString) FileGetContents( String const & filename, bool textMode = true );
220 
222 WINUX_FUNC_DECL(Buffer) FileGetContentsEx( String const & filename, bool textMode );
223 
230 WINUX_FUNC_DECL(bool) FilePutString( String const & filename, String const & content, FileEncoding encoding = feMultiByte );
231 
233 WINUX_FUNC_DECL(bool) FilePutContents( String const & filename, AnsiString const & content, bool textMode = true );
234 
236 WINUX_FUNC_DECL(bool) FilePutContentsEx( String const & filename, Buffer const & content, bool textMode );
237 
244 WINUX_FUNC_DECL(String) BackupFile( String const & filePath, String const & bakDir = $T(""), String const & fmt = $T("%f_v%v%E") );
245 
246 
248 class FileSysError : public Error
249 {
250 public:
251  enum
252  {
258  };
259  FileSysError( int errType, AnsiString const & s ) throw() : Error( errType, s ) { }
260 };
261 
264 {
265 public:
269  DirIterator( String const & path, String const & pattern = $T("*") );
271  ~DirIterator();
273  String const & getPath() const { return _path; }
275  String const & getName() const { return _name; }
277  String getFullPath() const;
279  String getRealPath() const;
281  bool isDir() const;
283  bool next();
284 
285 private:
286  String _path;
287  String _name;
288 #if defined(_MSC_VER) || defined(WIN32)
289  WIN32_FIND_DATA _wfd;
290  SimpleHandle<HANDLE> _findFile;
291  bool _first;
292 #else
293  #if defined(__USE_LARGEFILE64)
294  glob64_t _findFile;
295  #else
296  glob_t _findFile;
297  #endif
298  size_t _curr;
299 #endif
300 
302 };
303 
306 {
307  seekSet = 0,
308  seekCur = 1,
309  seekEnd = 2
310 };
311 
313 interface WINUX_DLL IFile
314 {
315  virtual ~IFile() { }
316 
318  virtual bool open( String const & filename, String const & mode );
320  virtual bool close();
322  virtual size_t read( void * buf, size_t size );
324  Buffer read( size_t size );
326  template < typename _PodType >
327  bool readType( _PodType * data, size_t size = sizeof(_PodType) )
328  {
329  return this->read( data, size ) == size;
330  }
332  template < typename _PodType >
333  _PodType readType( size_t size = sizeof(_PodType) )
334  {
335  _PodType data;
336  this->readType( &data, size );
337  return data;
338  }
340  virtual size_t write( void const * data, size_t size );
342  size_t write( Buffer const & buf );
344  template < typename _PodType >
345  bool writeType( _PodType const & data, size_t size = sizeof(_PodType) )
346  {
347  return this->write( &data, size ) == size;
348  }
350  virtual bool rewind();
352  virtual bool seek( offset_t offset, SeekType origin = seekSet );
354  virtual size_t tell();
356  virtual String getLine();
358  virtual int puts( String const & str );
360  virtual bool eof();
362  virtual size_t size();
368  virtual Buffer buffer( bool isPeek = true );
374  String entire( FileEncoding encoding = feMultiByte, bool convertNewline = false );
375 };
376 
378 class WINUX_DLL MemoryFile : public IFile
379 {
380 public:
382  MemoryFile();
388  MemoryFile( void const * data, size_t size, bool isPeek = false );
393  MemoryFile( Buffer const & buf, bool isPeek = false );
398  MemoryFile( AnsiString const & content, bool isPeek = false );
399 
405  virtual bool open( String const & content, String const & mode = Literal<String::value_type>::nulStr ) override;
406  virtual bool close() override;
407  virtual size_t read( void * buf, size_t size ) override;
408  virtual size_t write( void const * data, size_t size ) override;
409  virtual bool rewind() override;
410  virtual bool seek( offset_t offset, SeekType origin = seekSet ) override;
411  virtual size_t tell() override;
412  virtual String getLine() override;
414  virtual int puts( String const & str ) override;
415  virtual bool eof() override;
416  virtual size_t size() override;
417  virtual Buffer buffer( bool isPeek = true ) override;
418 
419  using IFile::read;
420  using IFile::write;
421 
422 protected:
425 
427 };
428 
430 class WINUX_DLL File : public IFile
431 {
432 public:
433  File();
434  File( String const & filename, String const & mode );
435  virtual ~File();
436 
437  virtual bool open( String const & filename, String const & mode ) override;
438  virtual bool close() override;
439  virtual size_t read( void * buf, size_t size ) override;
440  virtual size_t write( void const * data, size_t size ) override;
441  virtual bool rewind() override;
442  virtual bool seek( offset_t offset, SeekType origin = seekSet ) override;
443  virtual size_t tell() override;
444  virtual String getLine() override;
445  virtual bool eof() override;
446  virtual size_t size() override;
447  virtual Buffer buffer( bool isPeek = true ) override;
448 
449  using IFile::read;
450  using IFile::write;
451 
453  FILE * get() const { return _fp; }
454 
456  int getFd() const;
457 
458  #if defined(OS_WIN)
459 
460  HANDLE getOsHandle() const;
461  #else
462 
463  int getOsHandle() const;
464  #endif
465 
466  operator bool() const { return _fp != NULL; }
467 
468 protected:
469  FILE * _fp;
470 
472 };
473 
476 {
477 public:
478  BlockOutFile( String const & filename, bool isTextMode = true, size_t blockSize = 1048576 );
479 
481  bool nextBlock();
482  virtual size_t write( void const * data, size_t size ) override;
483  using File::write;
484 
485 protected:
490  long _fileno;
491 
492  bool _isTextMode;
493  size_t _blockSize;
494  size_t _writtenSize;
495 
497 };
498 
500 class WINUX_DLL BlockInFile : public File
501 {
502 public:
503  BlockInFile( String const & filename, bool isTextMode = true );
504 
506  bool nextBlock();
507  virtual String getLine() override;
508  virtual bool eof() override;
509 
510 protected:
515  long _index;
516 
517  bool _isTextMode;
519 
521 };
522 
523 } // namespace winux
524 
525 #endif //__FILESYS_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
UTF-16大端序
Definition: filesys.hpp:173
String PathWithSep(String const &path)
路径末尾带分隔符。如果path是空串则不作处理
StringArray _blockFiles
要加载的分块文件
Definition: filesys.hpp:518
文件系统错误:无效参数
Definition: filesys.hpp:257
String _filetitle
文件标题 filename
Definition: filesys.hpp:488
String BackupFile(String const &filePath, String const &bakDir=$T(""), String const &fmt=$T("%f_v%v%E"))
在给定目录备份指定文件
bool FileTime(String const &filename, time_t *ctime, time_t *mtime, time_t *atime)
获取文件时间
bool IsAbsPath(String const &path)
判断是否为绝对路径
char32_t char32
Definition: utilities.hpp:223
void FolderData(String const &path, StringArray *fileArr, StringArray *subFolderArr, int sortType=0)
获取文件夹中的文件和子文件夹。sortType:0结果不排序 1正序 2反序
String _basename
文件名 filename.txt
Definition: filesys.hpp:512
bool FilePutContents(String const &filename, AnsiString const &content, bool textMode=true)
把AnsiString内容写入文件,textMode表示是否为文本模式
文件系统错误:无权限拒绝访问
Definition: filesys.hpp:256
UTF-32小端序
Definition: filesys.hpp:174
#define WINUX_DLL
Definition: utilities.hpp:60
wchar_t wchar
Definition: utilities.hpp:229
相对于当前位置
Definition: filesys.hpp:308
long _index
文件索引
Definition: filesys.hpp:515
bool SetCurrentDir(String const &path)
设置当前工作目录
SeekType
Seek类型
Definition: filesys.hpp:305
time_t FileCTime(String const &filename)
获取文件创建时间
FileSysError(int errType, AnsiString const &s)
Definition: filesys.hpp:259
String NormalizePath(String const &path)
使路径规则化(末尾不带路径分割符)
String const & getName() const
取得文件名
Definition: filesys.hpp:275
String FilePath(String const &fullPath, String *fileName=NULL)
获取文件目录路径(末尾不含目录分隔符)
String const & getPath() const
取得路径
Definition: filesys.hpp:273
XString< tchar > String
Definition: utilities.hpp:261
bool UnlinkFile(String const &filePath)
断开路径与磁盘文件的链接,即删除文件(只能删除文件,不能删除目录)
未指定
Definition: filesys.hpp:167
virtual size_t write(void const *data, size_t size) override
写数据,返回写入的字节数
bool writeType(_PodType const &data, size_t size=sizeof(_PodType))
写POD类型数据
Definition: filesys.hpp:345
分块输入文件
Definition: filesys.hpp:500
virtual size_t read(void *buf, size_t size)
读数据,返回读取的字节数
目录文件枚举器
Definition: filesys.hpp:263
UTF-32大端序
Definition: filesys.hpp:176
winux::byte * _p
Definition: filesys.hpp:424
FileEncoding RecognizeFileEncoding(winux::Buffer const &content, size_t *pIndex, size_t limitSize=-1)
识别文件编码
String _extname
扩展名 .txt
Definition: filesys.hpp:514
String const DirSep_UNIX
Unix目录分割符
Definition: filesys.hpp:20
文件系统错误类
Definition: filesys.hpp:248
相对于结束位置
Definition: filesys.hpp:309
文件操作类
Definition: filesys.hpp:430
GrowBuffer _buf
Definition: filesys.hpp:423
size_t CommonDelete(String const &path)
通用删除。删除文件夹和文件,返回删除的文件夹和文件数
String _filetitle
文件标题 filename
Definition: filesys.hpp:513
virtual size_t write(void const *data, size_t size)
写数据,返回写入的字节数
String const LineSep_UNIX
Unix行分割符
Definition: filesys.hpp:21
bool MakeDirExists(String const &path, int mode=0755)
确保目录路径的存在性,如果不存在则创建。
bool RemovePath(String const &path)
删除文件和空目录。如果路径指向的是一个非空目录则失败
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
UTF-8 with BOM.
Definition: filesys.hpp:170
String PathNoSep(String const &path)
路径末尾不带分隔符。如果path是空串则不作处理
time_t FileATime(String const &filename)
获取文件访问时间
long _fileno
文件编号
Definition: filesys.hpp:490
bool RenamePath(String const &oldPath, String const &newPath)
重命名文件或目录,或者将文件或目录从一个目录移动到另一个目录
String const DirSep
目录分割符
Definition: filesys.hpp:36
FileEncoding
文件编码
Definition: filesys.hpp:165
bool FilePutString(String const &filename, String const &content, FileEncoding encoding=feMultiByte)
把字符串内容写入文件
String _dirname
目录名 path/to
Definition: filesys.hpp:511
内存式文件
Definition: filesys.hpp:378
bool RemoveDir(String const &dirPath)
删除空目录。如果目录非空则失败
UTF-16小端序
Definition: filesys.hpp:171
String _basename
文件名 filename.txt
Definition: filesys.hpp:487
String const LineSep_WIN
Windows行分割符
Definition: filesys.hpp:18
std::basic_string< _ChTy, std::char_traits< _ChTy >, std::allocator< _ChTy > > XString
Definition: utilities.hpp:255
size_t _blockSize
块大小
Definition: filesys.hpp:493
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:906
bool _isTextMode
是否文本模式
Definition: filesys.hpp:517
bool FilePutContentsEx(String const &filename, Buffer const &content, bool textMode)
把Buffer内容写入文件,textMode表示是否为文本模式
bool readType(_PodType *data, size_t size=sizeof(_PodType))
读POD类型数据
Definition: filesys.hpp:327
String RealPathEx(String const &path, String const &workDirAbsPath)
根据指定工作目录计算绝对路径,不会检查存在性
String CombinePath(String const &dirPath, String const &fileName)
把一个目录路径和一个文件名组合成一个新路径
文件系统错误:文件或目录不存在
Definition: filesys.hpp:255
String _extname
扩展名 .txt
Definition: filesys.hpp:489
bool DetectPath(String const &path, bool *isDir=NULL)
探测一个路径是存在还是不存在,是目录还是文件
size_t EnumFiles(String const &path, Mixed const &ext, StringArray *arrFiles, bool isRecursive=false)
在指定路径下枚举指定扩展名的文件
bool IsDir(String const &path)
判断是否是一个目录
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
bool _isTextMode
是否文本模式
Definition: filesys.hpp:492
String FileTitle(String const &fileName, String *extName=NULL)
获取文件标题
time_t FileMTime(String const &filename)
获取文件修改时间
unsigned char byte
Definition: utilities.hpp:249
相对于开头位置
Definition: filesys.hpp:307
String FileGetString(String const &filename, FileEncoding encoding=feMultiByte)
载入文件内容为文本字符串
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:1103
UTF-32大端序
Definition: filesys.hpp:175
String GetCurrentDir(void)
返回当前工作目录(末尾不含目录分隔符)
_PodType readType(size_t size=sizeof(_PodType))
读POD类型数据
Definition: filesys.hpp:333
XString< _ChTy > NewlineFromFile(_ChTy const *content, size_t len, bool b)
从文件中换行到程序环境中。b表示是否反序,配合IsLittleEndian()/IsBigEndian()使用
String RealPath(String const &path)
根据当前工作目录计算绝对路径,不会检查存在性
String GetExecutablePath(void)
获取可执行文件的全路径
char16_t char16
Definition: utilities.hpp:222
uint64 FileSize(String const &filename)
获取文件大小(也适用于大于4GB的文件)
FILE * _fp
FILE*指针
Definition: filesys.hpp:469
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
UTF-8.
Definition: filesys.hpp:169
错误类
Definition: utilities.hpp:838
分块输出文件
Definition: filesys.hpp:475
XString< _ChTy > NewlineToFile(_ChTy const *content, size_t len, bool b)
从程序环境中换行到文件中。b表示是否反序,配合IsLittleEndian()/IsBigEndian()使用
AnsiString FileGetContents(String const &filename, bool textMode=true)
载入文件内容为一个AnsiString,textMode表示是否为文本模式
String const DirSep_WIN
Windows目录分割符
Definition: filesys.hpp:17
UTF-16大端序
Definition: filesys.hpp:172
文件接口
Definition: filesys.hpp:313
unsigned __int64 uint64
Definition: utilities.hpp:230
String const PathEnvSep
PATH环境变量路径分割符
Definition: filesys.hpp:38
String const PathEnvSep_UNIX
Unix PATH环境变量路径分割符
Definition: filesys.hpp:22
virtual ~IFile()
Definition: filesys.hpp:315
String const DirSep_DARWIN
Apple系统目录分割符
Definition: filesys.hpp:23
String const LineSep
行分割符
Definition: filesys.hpp:37
XStringArray< tchar > StringArray
Definition: utilities.hpp:272
bool FileTouch(String const &filename, time_t time=(time_t)-1, time_t atime=(time_t)-1)
更新文件修改时间,访问时间
String _dirname
目录名 path/to
Definition: filesys.hpp:486
多字节
Definition: filesys.hpp:168
Buffer FileGetContentsEx(String const &filename, bool textMode)
载入文件内容为一个Buffer,textMode表示是否为文本模式
String const LineSep_DARWIN
Apple系统行分割符
Definition: filesys.hpp:24
String const PathEnvSep_WIN
Windows PATH环境变量路径分割符
Definition: filesys.hpp:19
跨平台基础功能库
Definition: archives.hpp:7
size_t _writtenSize
以写入数据大小
Definition: filesys.hpp:494
String const PathEnvSep_DARWIN
Apple系统PATH环境变量路径分割符
Definition: filesys.hpp:25
intptr_t offset_t
Definition: utilities.hpp:225