1 /*
2 Copyright (c) 2016-2021 Timur Gafarov
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.stdposixdir;
30 
31 import std..string;
32 import std.conv;
33 import std.range;
34 import core.stdc..string;
35 version(Posix)
36 {
37     import core.sys.posix.dirent;
38 }
39 import dlib.core.memory;
40 import dlib.filesystem.filesystem;
41 import dlib.text.str;
42 
43 version(Posix):
44 
45 class StdPosixDirEntryRange: InputRange!(DirEntry)
46 {
47     DIR* dir;
48     dirent* de = null;
49     DirEntry currentEntry;
50     bool _empty = false;
51 
52     this(DIR* dir)
53     {
54         this.dir = dir;
55         readNextEntry();
56     }
57 
58     void readNextEntry()
59     {
60         de = readdir(dir);
61         if (de)
62         {
63             string name = getFileName(de);
64             if (name == "." || name == "..")
65                 readNextEntry();
66             else
67             {
68                 bool isFile = (de.d_type == DT_REG);
69                 bool isDir = (de.d_type == DT_DIR);
70                 currentEntry = DirEntry(getFileName(de), isFile, isDir);
71             }
72         }
73         else
74             _empty = true;
75     }
76 
77     string getFileName(dirent* d)
78     {
79         return cast(string)d.d_name[0..strlen(d.d_name.ptr)];
80     }
81 
82     void reset()
83     {
84         rewinddir(dir);
85         _empty = false;
86     }
87 
88     override DirEntry front()
89     {
90         return currentEntry;
91     }
92 
93     override void popFront()
94     {
95         readNextEntry();
96     }
97 
98     override DirEntry moveFront()
99     {
100         readNextEntry();
101         return currentEntry;
102     }
103 
104     override bool empty()
105     {
106         return _empty;
107     }
108 
109     int opApply(scope int delegate(DirEntry) dg)
110     {
111         while(!_empty)
112         {
113             dg(currentEntry);
114             readNextEntry();
115         }
116 
117         return 0;
118     }
119 
120     int opApply(scope int delegate(size_t, DirEntry) dg)
121     {
122         size_t i = 0;
123         while(!_empty)
124         {
125             dg(i, currentEntry);
126             readNextEntry();
127             i++;
128         }
129 
130         return 0;
131     }
132 }
133 
134 class StdPosixDirectory: Directory
135 {
136     DIR* dir;
137     StdPosixDirEntryRange drange;
138 
139     this(string path)
140     {
141         String pathz = String(path);
142         dir = opendir(pathz.ptr);
143         pathz.free();
144     }
145 
146     ~this()
147     {
148         if (drange)
149         {
150             Delete(drange);
151             drange = null;
152         }
153 
154         close();
155     }
156 
157     void close()
158     {
159         if (dir)
160         {
161             closedir(dir);
162             dir = null;
163         }
164     }
165 
166     StdPosixDirEntryRange contents()
167     {
168         if (drange)
169             drange.reset();
170         else
171             drange = New!StdPosixDirEntryRange(dir);
172         return drange;
173     }
174 }