00001 #ifndef __DIRECTORY_ABSTRACTION_CPP__
00002 #define __DIRECTORY_ABSTRACTION_CPP__
00003
00004 #include <system/directory_abstraction.h>
00005 #include <utilities/exception.h>
00006 #include <utilities/string_utilities.h>
00007
00008 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00009 #include <sys/types.h>
00010 #include <sys/stat.h>
00011 #include <fcntl.h>
00012 #include <dirent.h>
00013 #endif
00014
00015 namespace nitro
00016 {
00017 DirectoryAbstraction::DirectoryAbstraction( void )
00018 {
00019 FindHandle = NULL;
00020
00021 FindData = NULL;
00022 }
00023
00024 bool DirectoryAbstraction::FindFirst( const char * theDirectoryPath )
00025 {
00026 try
00027 {
00028 Release();
00029
00030 if( IsDirectory( theDirectoryPath ) == false )
00031 {
00032 throw( nitro::exception( std::string( "Path " ) + theDirectoryPath + " must lead to directory" , 1 ) );
00033 }
00034
00035 DirectoryPath = theDirectoryPath;
00036
00037 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00038
00039 FindData = ( void * ) new WIN32_FIND_DATA;
00040
00041 FindHandle = ( void * ) new HANDLE;
00042
00043 * ( ( HANDLE * )FindHandle ) = ::FindFirstFile( ( nitro::FSPath::AddEndSlash( std::string( theDirectoryPath ) ) + "*" ).c_str() , ( WIN32_FIND_DATA * )FindData );
00044
00045 return( * ( ( HANDLE * )FindHandle ) == INVALID_HANDLE_VALUE );
00046 #endif
00047
00048 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00049 FindHandle = ( void * )opendir( theDirectoryPath );
00050
00051 return( FindHandle == NULL );
00052 #endif
00053 }
00054 catch( nitro::exception e )
00055 {
00056 throw( nitro::exception( std::string( "DirectoryAbstraction::FindFirst( const char * theDirectoryPath )::" ) + e.what() , e.code() ) );
00057 }
00058 catch( ... )
00059 {
00060 throw( nitro::exception( std::string( "DirectoryAbstraction::FindFirst( const char * theDirectoryPath )::An error occured while setting folder path" ) , 0 ) );
00061 }
00062 return( true );
00063 }
00064
00065 bool DirectoryAbstraction::FindFirst( const std::string & DirectoryPath )
00066 {
00067 try
00068 {
00069 return( FindFirst( DirectoryPath.c_str() ) );
00070 }
00071 catch( nitro::exception e )
00072 {
00073 throw( nitro::exception( std::string( "DirectoryAbstraction::FindFirst( const std::string & DirectoryPath )::" ) + e.what() , e.code() ) );
00074 }
00075 catch( ... )
00076 {
00077 throw( nitro::exception( std::string( "DirectoryAbstraction::FindFirst( const std::string & DirectoryPath )::An error occured while setting folder path" ) , 0 ) );
00078 }
00079 }
00080
00081 bool DirectoryAbstraction::FindNext( void )
00082 {
00083 try
00084 {
00085 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00086 return( ::FindNextFile( * ( HANDLE * )FindHandle , ( WIN32_FIND_DATA * )FindData ) == 0 );
00087 #endif
00088
00089 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00090 return( ( FindData = readdir( ( DIR * )FindHandle ) ) == NULL );
00091 #endif
00092 }
00093 catch( nitro::exception e )
00094 {
00095 throw( nitro::exception( std::string( "DirectoryAbstraction::FindNext( void )::" ) + e.what() , e.code() ) );
00096 }
00097 catch( ... )
00098 {
00099 throw( nitro::exception( std::string( "DirectoryAbstraction::FindNext( void )::An error occured while finding next item" ) , 0 ) );
00100 }
00101 return( true );
00102 }
00103
00104 bool DirectoryAbstraction::IsDirectory( void )
00105 {
00106 try
00107 {
00108 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00109 return( IsFile() == false && IsDots() == false );
00110 #endif
00111
00112 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00113 DIR * d( opendir( ( DirectoryPath + GetName() ).c_str() ) );
00114
00115 if( d )
00116 {
00117 closedir( d );
00118 return( true );
00119 }
00120 else
00121 {
00122 return( false );
00123 }
00124 #endif
00125 }
00126 catch( nitro::exception e )
00127 {
00128 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDirectory( void )::" ) + e.what() , e.code() ) );
00129 }
00130 catch( ... )
00131 {
00132 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDirectory( void )::An error occured while getting info (f)" ) , 0 ) );
00133 }
00134 }
00135
00136 bool DirectoryAbstraction::IsFile( void )
00137 {
00138 try
00139 {
00140 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00141 return( ( ( ( WIN32_FIND_DATA * )FindData )->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 );
00142 #endif
00143
00144 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00145 struct stat s;
00146
00147 return( stat( ( std::string( DirectoryPath ) + GetName() ).c_str() , & s ) == 0 );
00148 #endif
00149
00150 }
00151 catch( nitro::exception e )
00152 {
00153 throw( nitro::exception( std::string( "DirectoryAbstraction::IsFile( void )::" ) + e.what() , e.code() ) );
00154 }
00155 catch( ... )
00156 {
00157 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDots( void )::An error occured while getting info (d)" ) , 0 ) );
00158 }
00159 return( true );
00160 }
00161
00162 bool DirectoryAbstraction::IsFile( const char * FilePath )
00163 {
00164 try
00165 {
00166 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00167 return( ( GetFileAttributes( FilePath ) & FILE_ATTRIBUTE_DIRECTORY ) == 0 );
00168 #endif
00169
00170 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00171 struct stat s;
00172
00173 return( stat( FilePath , & s ) == 0 );
00174 #endif
00175 }
00176 catch( nitro::exception e )
00177 {
00178 throw( nitro::exception( std::string( "DirectoryAbstraction::IsFile( const char * FilePath )::" ) + e.what() , e.code() ) );
00179 }
00180 catch( ... )
00181 {
00182 throw( nitro::exception( std::string( "DirectoryAbstraction::IsFile( const char * FilePath )::An error occured while getting info (d)" ) , 0 ) );
00183 }
00184 return( true );
00185 }
00186
00187 bool DirectoryAbstraction::IsFile( const std::string & FilePath )
00188 {
00189 try
00190 {
00191 return( IsFile( FilePath.c_str() ) );
00192 }
00193 catch( nitro::exception e )
00194 {
00195 throw( nitro::exception( std::string( "DirectoryAbstraction::IsFile( const std::string & FilePath )::" ) + e.what() , e.code() ) );
00196 }
00197 catch( ... )
00198 {
00199 throw( nitro::exception( std::string( "DirectoryAbstraction::IsFile( const std::string & FilePath )::An error occured while getting info (d)" ) , 0 ) );
00200 }
00201 return( true );
00202 }
00203
00204 bool DirectoryAbstraction::IsDirectory( const char * theDirectoryPath )
00205 {
00206 try
00207 {
00208 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00209 return( ( GetFileAttributes( theDirectoryPath ) & FILE_ATTRIBUTE_DIRECTORY ) != 0 );
00210 #endif
00211
00212 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00213 DIR * d( opendir( theDirectoryPath ) );
00214
00215 if( d )
00216 {
00217 closedir( d );
00218 return( true );
00219 }
00220 else
00221 {
00222 return( false );
00223 }
00224 #endif
00225 }
00226 catch( nitro::exception e )
00227 {
00228 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDirectory( const char * theDirectoryPath )::" ) + e.what() , e.code() ) );
00229 }
00230 catch( ... )
00231 {
00232 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDirectory( const char * theDirectoryPath )::An error occured while getting info (d)" ) , 0 ) );
00233 }
00234 return( true );
00235 }
00236
00237 bool DirectoryAbstraction::IsDirectory( const std::string & theDirectoryPath )
00238 {
00239 try
00240 {
00241 return( IsDirectory( theDirectoryPath.c_str() ) );
00242 }
00243 catch( nitro::exception e )
00244 {
00245 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDirectory( const std::string & theDirectoryPath )::" ) + e.what() , e.code() ) );
00246 }
00247 catch( ... )
00248 {
00249 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDirectory( const std::string & theDirectoryPath )::An error occured while getting info (d)" ) , 0 ) );
00250 }
00251 }
00252
00253 bool DirectoryAbstraction::IsDots( void )
00254 {
00255 try
00256 {
00257 return( std::string( GetName() ) == "." || std::string( GetName() ) == ".." );
00258 }
00259 catch( nitro::exception e )
00260 {
00261 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDots( void )::" ) + e.what() , e.code() ) );
00262 }
00263 catch( ... )
00264 {
00265 throw( nitro::exception( std::string( "DirectoryAbstraction::IsDots( void )::An error occured while getting info (d)" ) , 0 ) );
00266 }
00267 }
00268
00269 const char * DirectoryAbstraction::GetName( void )
00270 {
00271 try
00272 {
00273 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00274 PathBuffer = ( ( WIN32_FIND_DATA * )FindData )->cFileName;
00275 #endif
00276
00277 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00278 PathBuffer = ( ( dirent * )FindData )->d_name;
00279 #endif
00280
00281 return( PathBuffer.c_str() );
00282 }
00283 catch( nitro::exception e )
00284 {
00285 throw( nitro::exception( std::string( "DirectoryAbstraction::GetName( void )::" ) + e.what() , e.code() ) );
00286 }
00287 catch( ... )
00288 {
00289 throw( nitro::exception( std::string( "DirectoryAbstraction::GetName( void )::An error occured" ) , 0 ) );
00290 }
00291 }
00292
00293 void DirectoryAbstraction::Release( void )
00294 {
00295 try
00296 {
00297 if( FindHandle )
00298 {
00299 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00300 ::FindClose( * ( ( HANDLE * )FindHandle ) );
00301
00302 delete ( HANDLE * ) FindHandle;
00303 delete ( WIN32_FIND_DATA * ) FindData;
00304 #endif
00305
00306 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00307 closedir( ( DIR * )FindHandle );
00308 #endif
00309
00310 FindData = FindHandle = NULL;
00311 }
00312 }
00313 catch( nitro::exception e )
00314 {
00315 throw( nitro::exception( std::string( "DirectoryAbstraction::Release( void )::" ) + e.what() , e.code() ) );
00316 }
00317 catch( ... )
00318 {
00319 throw( nitro::exception( std::string( "DirectoryAbstraction::Release( void )::An error occured" ) , 0 ) );
00320 }
00321 }
00322
00323 void DirectoryAbstraction::CreateDirectory( const char * DirPath )
00324 {
00325 try
00326 {
00327 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00328 if( !::CreateDirectoryA( DirPath , NULL ) )
00329 {
00330 throw( nitro::exception( std::string( "An error occured while creating directory" ) , 0 ) );
00331 }
00332 #endif
00333
00334 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00335 if( mkdir( DirPath , S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) == -1 )
00336 {
00337 throw( nitro::exception( std::string( "An error occured while creating directory" ) , 0 ) );
00338 }
00339 #endif
00340 }
00341 catch( nitro::exception e )
00342 {
00343 throw( nitro::exception( std::string( "DirectoryAbstraction::CreateDirectory( const char * DirPath )::" ) + e.what() , e.code() ) );
00344 }
00345 catch( ... )
00346 {
00347 throw( nitro::exception( std::string( "DirectoryAbstraction::CreateDirectory( const char * DirPath )::An error occured" ) , 0 ) );
00348 }
00349 }
00350
00351 void DirectoryAbstraction::CreateDirectory( const std::string & DirPath )
00352 {
00353 try
00354 {
00355 CreateDirectory( DirPath.c_str() );
00356 }
00357 catch( nitro::exception e )
00358 {
00359 throw( nitro::exception( std::string( "DirectoryAbstraction::CreateDirectory( const std::string & DirPath )::" ) + e.what() , e.code() ) );
00360 }
00361 catch( ... )
00362 {
00363 throw( nitro::exception( std::string( "DirectoryAbstraction::CreateDirectory( const std::string & DirPath )::An error occured" ) , 0 ) );
00364 }
00365 }
00366
00367 DirectoryAbstraction::~DirectoryAbstraction()
00368 {
00369 try
00370 {
00371 Release();
00372 }
00373 catch( ... )
00374 {
00375 }
00376 }
00377
00380
00381 BEGIN_TESTING_SECTION()
00382
00383 ENABLE_CLASS_TESTING( DirectoryAbstraction )
00384
00385 CLASS_MEMBER_FUNCTION_TESTING_1( DirectoryAbstraction , aliasFindFirst , tstFindFirst , const char * , bool , bool )
00386 CLASS_MEMBER_FUNCTION_TESTING_0( DirectoryAbstraction , FindNext , tstFindNext , bool , bool )
00387 CLASS_MEMBER_FUNCTION_TESTING_0( DirectoryAbstraction , IsDirectory , tstIsDirectory , bool , bool )
00388 CLASS_MEMBER_FUNCTION_TESTING_0( DirectoryAbstraction , IsFile , tstIsFile , bool , bool )
00389 CLASS_MEMBER_FUNCTION_TESTING_0( DirectoryAbstraction , IsDots , tstIsDots , bool , bool )
00390 CLASS_MEMBER_FUNCTION_TESTING_0( DirectoryAbstraction , GetName , tstGetName , const char * , const char * )
00391
00392 FUNCTION_TESTING_1( aliasCreateDirectory , tstCreateDirectory , const char * , void , NO_RET )
00393
00394 END_TESTING_SECTION()
00395 }
00396
00397 #endif