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 17 17 18 18 /******************************************************************************* 19 19 20 Recursively scan files and directories, adding filtered files to21 an output structure as we go. This can be used to produce a list22 of subdirectories and the files contained therein. The following20 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 23 23 example lists all files with suffix ".d" located via the current 24 24 directory, along with the folders containing them: 25 25 26 Examples: 26 27 --- 27 28 auto scan = new FileScan; 28 29 … … 40 41 This is unlikely the most efficient method to scan a vast number of 41 42 files, but operates in a convenient manner 42 43 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 --- 43 56 *******************************************************************************/ 44 57 45 58 class FileScan … … 49 62 uint total_; 50 63 FilePath[] files_, 51 64 folders_; 65 FileScanError[] errors_; 52 66 53 67 /*********************************************************************** 54 68 … … 105 119 106 120 /*********************************************************************** 107 121 122 Return all errors found in the last scan 123 124 ***********************************************************************/ 125 126 public FileScanError[] errors () 127 { 128 return errors_; 129 } 130 131 /*********************************************************************** 132 108 133 Sweep a set of files and directories from the given parent 109 134 path, where the files are filtered by the given suffix 110 135 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 111 142 ***********************************************************************/ 112 143 113 FileScan sweep (char[] path, char[] match )144 FileScan sweep (char[] path, char[] match, bool stopOnError = true, bool recurse = true) 114 145 { 115 146 return sweep (path, (FilePath fp, bool isDir) 116 {return isDir || fp.suffix == match;});147 {return isDir || match == "" || fp.suffix == match;}, stopOnError, recurse); 117 148 } 118 149 119 150 /*********************************************************************** … … 121 152 Sweep a set of files and directories from the given parent 122 153 path, where the files are filtered by the provided delegate 123 154 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 124 161 ***********************************************************************/ 125 162 126 FileScan sweep (char[] path, Filter filter )163 FileScan sweep (char[] path, Filter filter, bool stopOnError = true, bool recurse = true) 127 164 { 128 165 total_ = 0; 129 166 files_ = folders_ = null; 130 return scan (new FilePath(path), filter); 167 errors_ = null; 168 return scan (new FilePath(path), filter, stopOnError, recurse); 131 169 } 132 170 133 171 /*********************************************************************** … … 137 175 138 176 ***********************************************************************/ 139 177 140 private FileScan scan (FilePath folder, Filter filter )178 private FileScan scan (FilePath folder, Filter filter, bool stopOnError, bool recurse) 141 179 { 142 180 FilePath[] paths; 143 181 … … 166 204 delete p; 167 205 } 168 206 207 try { 169 208 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 } 170 217 auto count = files_.length; 171 218 172 219 foreach (path; paths) 173 220 if (path.isDir) 174 scan (path, filter); 221 if (recurse) 222 scan (path, filter, stopOnError, recurse); 223 else 224 folders_ ~= path; 175 225 else 176 226 files_ ~= path; 177 227 … … 182 232 return this; 183 233 } 184 234 } 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 245 class FileScanError { 246 FilePath path; 247 char [] msg; 248 249 this(FilePath Path, char [] Msg) { 250 path = Path; 251 msg = Msg; 252 } 253 }










