Changeset 497

Show
Ignore:
Timestamp:
08/11/09 09:56:48 (15 years ago)
Author:
vbarta
Message:

moved cross-platform directory scanning support from tests to core (osutils)

Location:
library
Files:
4 modified
2 moved

Legend:

Unmodified
Added
Removed
  • library/bdm/osutils.cpp

    r415 r497  
    1313#include "osutils.h" 
    1414#include <iostream> 
     15#include <stdexcept> 
     16#include <string> 
    1517 
    1618#ifdef WIN32 
     19#include "dirent.h" 
    1720#include <direct.h> 
     21#define rmdir _rmdir 
     22#define unlink _unlink 
    1823#else 
     24#include <dirent.h> 
     25#include <unistd.h> 
    1926#include <sys/types.h> 
    2027#include <sys/stat.h> 
     
    4451        } 
    4552} 
     53 
     54bool 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  
    2727*/ 
    2828void 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 
     33be removed (must not be empty). 
     34 
     35Returns true on success, false when path couldn't be removed because 
     36it didn't exist, throws an exception otherwise. 
     37*/ 
     38bool remove_all ( const char *path ); 
     39 
  • library/tests/test_util.cpp

    r493 r497  
    22#include "stat/exp_family.h" 
    33#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 WIN32 
    14 #include "dirent.h" 
    15 #include <direct.h> 
    16 #define rmdir _rmdir 
    17 #define unlink _unlink 
    18 #else 
    19 #include <dirent.h> 
    20 #include <unistd.h> 
    21 #endif 
    224 
    235namespace bdm { 
     
    3214        src.read ( buffer, sizeof ( buffer ) - 1 ); 
    3315        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 ) || // Linux  
    67                      ( errno == EINVAL ) ) { // Windows 
    68                         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 place 
    80                                 rv = false; 
    81                         } 
    82                 } 
    83         } 
    84  
    85         return rv; 
    8616} 
    8717 
  • library/tests/test_util.h

    r486 r497  
    2828std::string load_test_file ( const char *fname ); 
    2929 
    30 /*! \brief Recursively removes directories and files. 
    31  
    32   path is the name (absolute or relative) of the file or directory to 
    33   be removed (must not be empty). Returns true on success, false when 
    34   path couldn't be removed because it didn't exist, throws an 
    35   exception otherwise. 
    36 */ 
    37 bool remove_all ( const char *path ); 
    38  
    3930double normcoef ( const epdf *ep, const itpp::vec &xb, const itpp::vec &yb, 
    4031                  int xn = 100, int yn = 100 );