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)

Files:
1 modified

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}