D Paste by Chris Miller
Description: createPath()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import std.file; // Written by Christopher E. Miller. See license under code. // Allows you to create a bunch of nesting directories all at once. // Make sure -dirpath- does not include a file on the end // or it will be treated as a directory. // Returns number of directories created. size_t createPath(char[] dirpath) { size_t result = 0; size_t iw = 0; if(!dirpath.length) dirpath = "."; const char dirsep1 = '/'; version(Windows) { const char dirsep2 = '\\'; if(dirpath.length >= 2) { if(dirpath[1] == ':') { iw = 2; // Don't attempt to create root. if(dirpath.length >= 3 && (dirpath[2] == '\\' || dirpath[2] == '/')) { iw++; } } else if(dirpath[0] == '\\' && dirpath[1] == '\\') { iw = 2; // Skip share indicator. } } } else { const char dirsep2 = dirsep1; // Unix path. if(dirpath.length >= 1 && dirpath[0] == '/') { iw = 1; // Don't attempt to create root. } } for(;; iw++) { if(iw == dirpath.length || dirpath[iw] == dirsep1 || dirpath[iw] == dirsep2) { if(!std.file.exists(dirpath[0 .. iw])) { std.file.mkdir(dirpath[0 .. iw]); result++; } if(iw == dirpath.length) break; } } return result; } /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |