Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #587: FileScan.patch

File FileScan.patch, 6.2 kB (added by flithm, 1 year ago)

Allow blank filter to scan all file names

  • FileScan.d

    old new  
    1717 
    1818/******************************************************************************* 
    1919 
    20         Recursively scan files and directories, adding filtered files to 
    21         an output structure as we go. This can be used to produce a list 
    22         of subdirectories and the files contained therein. The following 
     20        Scan files and directories (recursively by default), adding filtered  
     21        files to an output structure as we go. This can be used to produce a  
     22        list of subdirectories and the files contained therein. The following 
    2323        example lists all files with suffix ".d" located via the current 
    2424        directory, along with the folders containing them: 
    2525 
     26    Examples: 
    2627        --- 
    2728        auto scan = new FileScan; 
    2829 
     
    4041        This is unlikely the most efficient method to scan a vast number of 
    4142        files, but operates in a convenient manner 
    4243         
     44        The above code will result in the scan terminating early if an error 
     45        is encountered along the way (such as permission denied, or a malformed 
     46        soft-link, etc).  In order to continue in the event of errors use the 
     47        stopOnError parameter of sweep: 
     48        --- 
     49        auto scan = new FileScan; 
     50 
     51        scan (".", ".d", true); 
     52         
     53        foreach (idx, file; scan.errors) 
     54            Stdout.format("Error [{0}]: {1} -- {2}", idx, file.path.toUtf8, file.msg); 
     55        --- 
    4356*******************************************************************************/ 
    4457 
    4558class FileScan 
     
    4962        uint            total_; 
    5063        FilePath[]      files_, 
    5164                        folders_; 
     65        FileScanError[] errors_; 
    5266         
    5367        /*********************************************************************** 
    5468 
     
    105119 
    106120        /*********************************************************************** 
    107121 
     122                Return all errors found in the last scan 
     123 
     124        ***********************************************************************/ 
     125 
     126        public FileScanError[] errors () 
     127        { 
     128                return errors_; 
     129        } 
     130         
     131        /*********************************************************************** 
     132 
    108133                Sweep a set of files and directories from the given parent 
    109134                path, where the files are filtered by the given suffix 
    110135         
     136                Params:  path =         The path to sweep 
     137                         match =        The pattern to match while sweeping 
     138                         stopOnError =  If true sweep will throw an exception  
     139                                        on encountering an error 
     140                         recurse =  Do a recursive scan if true 
     141         
    111142        ***********************************************************************/ 
    112143         
    113         FileScan sweep (char[] path, char[] match
     144        FileScan sweep (char[] path, char[] match, bool stopOnError = true, bool recurse = true
    114145        { 
    115146                return sweep (path, (FilePath fp, bool isDir) 
    116                              {return isDir || fp.suffix == match;}); 
     147                             {return isDir || match == "" || fp.suffix == match;}, stopOnError, recurse); 
    117148        } 
    118149 
    119150        /*********************************************************************** 
     
    121152                Sweep a set of files and directories from the given parent 
    122153                path, where the files are filtered by the provided delegate 
    123154 
     155                Params:  path =         The path to sweep 
     156                         match =        The pattern to match while sweeping 
     157                         stopOnError =  If true sweep will throw an exception  
     158                                        on encountering an error 
     159                         recurse =  Do a recursive scan if true 
     160         
    124161        ***********************************************************************/ 
    125162         
    126         FileScan sweep (char[] path, Filter filter
     163        FileScan sweep (char[] path, Filter filter, bool stopOnError = true, bool recurse = true
    127164        { 
    128165                total_ = 0; 
    129166                files_ = folders_ = null; 
    130                 return scan (new FilePath(path), filter); 
     167                errors_ = null; 
     168                return scan (new FilePath(path), filter, stopOnError, recurse); 
    131169        } 
    132170 
    133171        /*********************************************************************** 
     
    137175 
    138176        ***********************************************************************/ 
    139177 
    140         private FileScan scan (FilePath folder, Filter filter)  
     178        private FileScan scan (FilePath folder, Filter filter, bool stopOnError, bool recurse)  
    141179        { 
    142180                FilePath[] paths; 
    143181 
     
    166204                           delete p; 
    167205                } 
    168206 
     207        try { 
    169208                folder.toList (&add); 
     209                } catch (Exception e) { 
     210                    if (stopOnError) 
     211                        throw e; 
     212                    else { 
     213                        errors_ ~= new FileScanError(folder, e.msg); 
     214                        return this; 
     215                    } 
     216                } 
    170217                auto count = files_.length; 
    171218                 
    172219                foreach (path; paths) 
    173220                         if (path.isDir) 
    174                              scan (path, filter); 
     221                             if (recurse) 
     222                                  scan (path, filter, stopOnError, recurse); 
     223                             else 
     224                                  folders_ ~= path; 
    175225                         else 
    176226                            files_ ~= path; 
    177227                 
     
    182232                return this; 
    183233        } 
    184234} 
     235 
     236/******************************************************************************* 
     237 
     238    Hold information about errors encountered during the scan 
     239     
     240    Params: path =      path the triggered the errors 
     241        msg =       reason behind the error 
     242 
     243*******************************************************************************/ 
     244 
     245class FileScanError { 
     246    FilePath    path; 
     247    char []     msg; 
     248     
     249    this(FilePath Path, char [] Msg) { 
     250        path = Path; 
     251        msg = Msg; 
     252    } 
     253}