gxfilesystem.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "std.h"
  2. #include "gxfilesystem.h"
  3. static set<gxDir*> dir_set;
  4. gxFileSystem::gxFileSystem(){
  5. dir_set.clear();
  6. }
  7. gxFileSystem::~gxFileSystem(){
  8. while( dir_set.size() ) closeDir( *dir_set.begin() );
  9. }
  10. bool gxFileSystem::createDir( const std::string &dir ){
  11. return CreateDirectory( dir.c_str(),0 ) ? true : false;
  12. }
  13. bool gxFileSystem::deleteDir( const std::string &dir ){
  14. return RemoveDirectory( dir.c_str() ) ? true : false;
  15. }
  16. bool gxFileSystem::createFile( const std::string &file ){
  17. return false;
  18. }
  19. bool gxFileSystem::deleteFile( const std::string &file ){
  20. return DeleteFile( file.c_str() ) ? true : false;
  21. }
  22. bool gxFileSystem::copyFile( const std::string &src,const string &dest ){
  23. return CopyFile( src.c_str(),dest.c_str(),false ) ? true : false;
  24. }
  25. bool gxFileSystem::renameFile( const std::string &src,const std::string &dest ){
  26. return MoveFile( src.c_str(),dest.c_str() ) ? true : false;
  27. }
  28. bool gxFileSystem::setCurrentDir( const std::string &dir ){
  29. return SetCurrentDirectory( dir.c_str()) ? true : false;
  30. }
  31. string gxFileSystem::getCurrentDir()const{
  32. char buff[MAX_PATH];
  33. if( !GetCurrentDirectory( MAX_PATH,buff ) ) return "";
  34. string t=buff;if( t.size() && t[t.size()-1]!='\\' ) t+='\\';
  35. return t;
  36. }
  37. int gxFileSystem::getFileSize( const std::string &name )const{
  38. WIN32_FIND_DATA findData;
  39. HANDLE h=FindFirstFile( name.c_str(),&findData );
  40. if( h==INVALID_HANDLE_VALUE ) return 0;
  41. int n=findData.dwFileAttributes,sz=findData.nFileSizeLow;
  42. FindClose( h );return n & FILE_ATTRIBUTE_DIRECTORY ? 0 : sz;
  43. }
  44. int gxFileSystem::getFileType( const std::string &name )const{
  45. DWORD t=GetFileAttributes( name.c_str() );
  46. return t==-1 ? FILE_TYPE_NONE :
  47. (t & FILE_ATTRIBUTE_DIRECTORY ? FILE_TYPE_DIR : FILE_TYPE_FILE);
  48. }
  49. gxDir *gxFileSystem::openDir( const std::string &name,int flags ){
  50. string t=name;
  51. if( t[t.size()-1]=='\\' ) t+="*";
  52. else t+="\\*";
  53. WIN32_FIND_DATA f;
  54. HANDLE h=FindFirstFile( t.c_str(),&f );
  55. if( h!=INVALID_HANDLE_VALUE ){
  56. gxDir *d=d_new gxDir( h,f );
  57. dir_set.insert( d );
  58. return d;
  59. }
  60. return 0;
  61. }
  62. gxDir *gxFileSystem::verifyDir( gxDir *d ){
  63. return dir_set.count(d) ? d : 0;
  64. }
  65. void gxFileSystem::closeDir( gxDir *d ){
  66. if( dir_set.erase( d ) ) delete d;
  67. }