1 /* 2 Copyright (c) 2014-2021 Martin Cejp 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dlib.filesystem.windows.directory; 30 31 version(Windows) 32 { 33 import dlib.filesystem.filesystem; 34 import dlib.filesystem.dirrange; 35 import dlib.filesystem.windows.common; 36 37 import std.conv; 38 import std.range; 39 40 class WindowsDirectory: Directory 41 { 42 FileSystem fs; 43 HANDLE find = INVALID_HANDLE_VALUE; 44 string prefix; 45 46 WIN32_FIND_DATAW entry; 47 bool entryValid = false; 48 49 this(FileSystem fs, string path, string prefix) 50 { 51 this.fs = fs; 52 this.prefix = prefix; 53 54 find = FindFirstFileW(toUTF16z(path ~ `\*.*`), &entry); 55 56 if (find != INVALID_HANDLE_VALUE) 57 entryValid = true; 58 } 59 60 ~this() 61 { 62 close(); 63 } 64 65 void close() 66 { 67 if (find != INVALID_HANDLE_VALUE) 68 { 69 FindClose(find); 70 find = INVALID_HANDLE_VALUE; 71 } 72 } 73 74 InputRange!DirEntry contents() 75 { 76 return new DirRange(delegate bool(out DirEntry de) 77 { 78 for (;;) 79 { 80 WIN32_FIND_DATAW* entry = nextEntry(); 81 82 if (entry == null) 83 return false; 84 else 85 { 86 size_t len = wcslen(entry.cFileName.ptr); 87 string name = to!string(entry.cFileName[0..len]); 88 89 if (name == "." || name == "..") 90 continue; 91 92 de.name = name; 93 94 if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 95 de.isDirectory = true; 96 else 97 de.isFile = true; 98 99 return true; 100 } 101 } 102 }); 103 } 104 105 private WIN32_FIND_DATAW* nextEntry() 106 { 107 if (entryValid) 108 { 109 entryValid = false; 110 return &entry; 111 } 112 113 if (find == INVALID_HANDLE_VALUE || !FindNextFileW(find, &entry)) 114 return null; 115 116 return &entry; 117 } 118 } 119 }