- Timestamp:
- 08/11/09 09:56:48 (15 years ago)
- Location:
- library
- Files:
-
- 4 modified
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/osutils.cpp
r415 r497 13 13 #include "osutils.h" 14 14 #include <iostream> 15 #include <stdexcept> 16 #include <string> 15 17 16 18 #ifdef WIN32 19 #include "dirent.h" 17 20 #include <direct.h> 21 #define rmdir _rmdir 22 #define unlink _unlink 18 23 #else 24 #include <dirent.h> 25 #include <unistd.h> 19 26 #include <sys/types.h> 20 27 #include <sys/stat.h> … … 44 51 } 45 52 } 53 54 bool remove_all ( const char *path ) { 55 DIR *dir; 56 dirent *de; 57 58 bool rv = true; 59 if ( ( dir = opendir ( path ) ) != 0 ) { 60 try { 61 std::string top ( path ); 62 top += "/"; 63 64 while ( ( de = readdir ( dir ) ) != 0 ) { 65 if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) { 66 std::string subpath ( top ); 67 subpath += de->d_name; 68 remove_all ( subpath.c_str() ); 69 } 70 } 71 } catch ( ... ) { 72 closedir ( dir ); 73 throw; 74 } 75 76 closedir ( dir ); 77 78 if ( rmdir ( path ) ) { 79 std::string msg = "can't remove dir "; 80 msg += path; 81 throw std::runtime_error ( msg ); 82 } 83 } else { 84 if ( ( errno == ENOTDIR ) || // Linux 85 ( errno == EINVAL ) ) { // Windows 86 if ( unlink ( path ) ) { 87 std::string msg = "can't remove file "; 88 msg += path; 89 throw std::runtime_error ( msg ); 90 } 91 } else { 92 if ( errno != ENOENT ) { 93 std::string msg = "can't remove "; 94 msg += path; 95 throw std::runtime_error ( msg ); 96 } else { 97 // it wasn't there in the first place 98 rv = false; 99 } 100 } 101 } 102 103 return rv; 104 } -
library/bdm/osutils.h
r408 r497 27 27 */ 28 28 void makedir ( string &dirname, bool rewrite = true ); 29 30 /*! @brief Recursively removes directories and files. 31 32 @param path the name (absolute or relative) of the file or directory to 33 be removed (must not be empty). 34 35 Returns true on success, false when path couldn't be removed because 36 it didn't exist, throws an exception otherwise. 37 */ 38 bool remove_all ( const char *path ); 39 -
library/tests/test_util.cpp
r493 r497 2 2 #include "stat/exp_family.h" 3 3 #include <fstream> 4 #include <stdexcept>5 #include <string>6 #include <errno.h>7 #include <fcntl.h>8 #include <string.h>9 #include <stdio.h>10 #include <sys/stat.h>11 #include <sys/types.h>12 13 #ifdef WIN3214 #include "dirent.h"15 #include <direct.h>16 #define rmdir _rmdir17 #define unlink _unlink18 #else19 #include <dirent.h>20 #include <unistd.h>21 #endif22 4 23 5 namespace bdm { … … 32 14 src.read ( buffer, sizeof ( buffer ) - 1 ); 33 15 return std::string ( buffer ); 34 }35 36 bool remove_all ( const char *path ) {37 DIR *dir;38 dirent *de;39 40 bool rv = true;41 if ( ( dir = opendir ( path ) ) != 0 ) {42 try {43 std::string top ( path );44 top += "/";45 46 while ( ( de = readdir ( dir ) ) != 0 ) {47 if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) {48 std::string subpath ( top );49 subpath += de->d_name;50 remove_all ( subpath.c_str() );51 }52 }53 } catch ( ... ) {54 closedir ( dir );55 throw;56 }57 58 closedir ( dir );59 60 if ( rmdir ( path ) ) {61 std::string msg = "can't remove dir ";62 msg += path;63 throw std::runtime_error ( msg );64 }65 } else {66 if ( ( errno == ENOTDIR ) || // Linux67 ( errno == EINVAL ) ) { // Windows68 if ( unlink ( path ) ) {69 std::string msg = "can't remove file ";70 msg += path;71 throw std::runtime_error ( msg );72 }73 } else {74 if ( errno != ENOENT ) {75 std::string msg = "can't remove ";76 msg += path;77 throw std::runtime_error ( msg );78 } else {79 // it wasn't there in the first place80 rv = false;81 }82 }83 }84 85 return rv;86 16 } 87 17 -
library/tests/test_util.h
r486 r497 28 28 std::string load_test_file ( const char *fname ); 29 29 30 /*! \brief Recursively removes directories and files.31 32 path is the name (absolute or relative) of the file or directory to33 be removed (must not be empty). Returns true on success, false when34 path couldn't be removed because it didn't exist, throws an35 exception otherwise.36 */37 bool remove_all ( const char *path );38 39 30 double normcoef ( const epdf *ep, const itpp::vec &xb, const itpp::vec &yb, 40 31 int xn = 100, int yn = 100 );