annotate dmd/FileName.d @ 34:544b922227c7

update to work with dmd 2.048
author korDen
date Sat, 21 Aug 2010 05:46:08 +0400
parents 5c9b78899f5d
children ccbc1e0bb3f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.FileName;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.String;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.File;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7
4
d706d958e4e8 Step 2 of restoring GC functionality.
korDen
parents: 2
diff changeset
8 import core.memory;
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
9
5
63623152e82a Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents: 4
diff changeset
10 import core.stdc.stdlib : malloc, alloca;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import core.stdc.string : memcpy, strlen;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import core.stdc.ctype : isspace;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13
34
544b922227c7 update to work with dmd 2.048
korDen
parents: 16
diff changeset
14 import std.exception : assumeUnique;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import std.string : cmp, icmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import std.file : mkdirRecurse;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17
14
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
18 version (Windows)
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
19 {
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
20 import core.sys.windows.windows;
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
21 }
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
22
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
23 version (POSIX)
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
24 {
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
25 import core.stdc.stdlib;
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
26 import core.sys.posix.sys.stat;
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
27 import std.conv;
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
28 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 class FileName : String
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 this(string str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 super(str);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 this(string path, string name)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 super(combine(path, name));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 hash_t hashCode()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 // We need a different hashCode because it must be case-insensitive
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 size_t len = str.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 hash_t hash = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 ubyte* s = cast(ubyte*)str.ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 for (;;)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 switch (len)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 case 0:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 return hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 case 1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 hash += *(cast(ubyte*)s) | 0x20;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 return hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 case 2:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 hash += *(cast(ushort*)s) | 0x2020;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 return hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 case 3:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 hash += (*cast(ushort*)s) << 8 +
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 ((cast(ubyte*)s)[2]) | 0x202020;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 hash += (*cast(int*)s) | 0x20202020;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 s += 4;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 len -= 4;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 // darwin HFS is case insensitive, though...
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 return super.hashCode();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 bool opEquals(Object obj)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 return opCmp(obj) == 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 static int equals(string name1, string name2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 return compare(name1, name2) == 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 int opCmp(Object obj)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 return compare(str, (cast(FileName)obj).str);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 static int compare(string name1, string name2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 return icmp(name1, name2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 return cmp(name1, name2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 static bool absolute(string name)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 return (*name == '\\') ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 (*name == '/') ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 (*name && name[1] == ':');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 } else version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 return (*name == '/');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 static assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 /********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 * Return filename extension (read-only).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 * Points past '.' of extension.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 * If there isn't one, return NULL.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 static string ext(string str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 foreach_reverse (i, c; str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 switch (c)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 case '.':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 return str[i+1..$];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 case '/':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 case '\\':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 case ':':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 case '/':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 } default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 string ext()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 return ext(str);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 /********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 * Return filename with extension removed.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 static string removeExt(string str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 string e = ext(str);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 if (e !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 size_t len = (e.ptr - str.ptr - 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 return str[0..len];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 return str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 /********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 * Return filename name excluding path (read-only).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 static string name(string str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 foreach_reverse(i, c; str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 switch (c)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 case '/':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 return str[i+1..$];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 case '/':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 case '\\':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 return str[i+1..$];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 case ':':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 /* The ':' is a drive letter only if it is the second
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 * character or the last character,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 * otherwise it is an ADS (Alternate Data Stream) separator.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 * Consider ADS separators as part of the file name.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 if (i == 1 || i == str.length - 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 return str[i+1..$];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 return str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 string name()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 return name(str);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 /**************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 * Return path portion of str.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 * Path will does not include trailing path separator.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 static string path(string str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 auto n = name(str).ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 if (n > str.ptr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 auto p = n - 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 if (*p == '/')
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 n--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 } else version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 if (*p == '\\' || *p == '/')
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 n--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 static assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 size_t pathlen = n - str.ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 return str[0..pathlen];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 /**************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 * Replace filename portion of path.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 static string replaceName(string path, string name)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 if (absolute(name))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 return name;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 string n = FileName.name(path);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 if (n is path)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 return name;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 size_t pathlen = n.ptr - path.ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 size_t namelen = name.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
259 char* f = cast(char*)GC.malloc(pathlen + 1 + namelen + 1);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 memcpy(f, path.ptr, pathlen);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 if (path[pathlen - 1] != '/')
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 f[pathlen] = '/';
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 pathlen++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 } else version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 if (path[pathlen - 1] != '\\' &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 path[pathlen - 1] != '/' &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 path[pathlen - 1] != ':')
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 f[pathlen] = '\\';
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 pathlen++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 static assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 memcpy(f + pathlen, name.ptr, namelen + 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 return assumeUnique(f[0..pathlen+namelen]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 static string combine(string path, string name)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 size_t pathlen;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 size_t namelen;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 if (path.length == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 return name;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 pathlen = path.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 namelen = name.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
294 char* f = cast(char*)GC.malloc(pathlen + 1 + namelen + 1);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 memcpy(f, path.ptr, pathlen);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 if (path[pathlen - 1] != '/')
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 f[pathlen] = '/';
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 pathlen++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 } else version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 if (path[pathlen - 1] != '\\' &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 path[pathlen - 1] != '/' &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 path[pathlen - 1] != ':')
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 f[pathlen] = '\\';
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 pathlen++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 static assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 memcpy(f + pathlen, name.ptr, namelen + 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 return assumeUnique(f[0..pathlen+namelen]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 static string[] splitPath(const(char)[] spath)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 char c = 0; // unnecessary initializer is for VC /W4
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 scope OutBuffer buf = new OutBuffer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 string[] array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 if (spath !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 const(char)* p = spath.ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 int len = spath.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 do
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 char instring = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 while (len > 0 && isspace(*p)) { // skip leading whitespace
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 p++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 --len;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 buf.reserve(len + 1); // guess size of path
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 for (; len; p++, len--)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 c = *p;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 switch (c)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 case '"':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 instring ^= 1; // toggle inside/outside of string
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 version (MACINTOSH) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 case ',':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 case ';':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 case ':':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 p++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 break; // note that ; cannot appear as part
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 // of a path, quotes won't protect it
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 case 0x1A: // ^Z means end of file
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 //case 0:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 case '\r':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 continue; // ignore carriage returns
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 case '~':
14
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
372 buf.writestring(to!string(getenv("HOME")));
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 version (disabled) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 case ' ':
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 case '\t': // tabs in filenames?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379 if (!instring) // if not in string
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 break; // treat as end of path
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 buf.writeByte(c);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 if (buf.offset) // if path is not empty
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390 //buf.writeByte(0); // to asciiz
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 array ~= buf.extractString();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393 } while (len > 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 return array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
398
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 static FileName defaultExt(string name, string ext)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 string e = FileName.ext(name);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402 if (e !is null) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403 // if already has an extension
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 return new FileName(name);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 size_t len = name.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408 size_t extlen = ext.length;
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
409 char* s = cast(char*)GC.malloc(len + 1 + extlen + 1);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410 memcpy(s, name.ptr, len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 s[len] = '.';
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 memcpy(s + len + 1, ext.ptr, extlen + 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414 return new FileName(assumeUnique(s[0..len+1+extlen]));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 static FileName forceExt(string name, string ext)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 string e = FileName.ext(name);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 if (e !is null) // if already has an extension
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 size_t len = e.ptr - name.ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 size_t extlen = ext.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424
5
63623152e82a Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents: 4
diff changeset
425 char* s = cast(char*)malloc(len + extlen + 1); /// !
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 memcpy(s, name.ptr, len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427 memcpy(s + len, ext.ptr, extlen + 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 return new FileName(assumeUnique(s[0..len+extlen]));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 return defaultExt(name, ext); // doesn't have one
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434 /******************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435 * Return true if extensions match.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
437
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438 bool equalsExt(string ext)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 string e = FileName.ext();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
441 if (e.length == 0 && ext.length == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
442 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
443
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
444 if (e.length == 0 || ext.length == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
445 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
446
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
447 version (POSIX) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
448 return cmp(e,ext) == 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
449 } else version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
450 return icmp(e,ext) == 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
451 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
452 static assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
453 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
454 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
455
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
456 /*************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
457 * Copy file from this to to.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
458 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
459
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
460 void CopyTo(FileName to)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
461 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
462 scope File file = new File(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
463
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
464 version (_WIN32) {
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
465 file.touchtime = GC.malloc(WIN32_FIND_DATA.sizeof); // keep same file time
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
466 } else version (POSIX) {
14
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
467 file.touchtime = GC.malloc(stat_t.sizeof); // keep same file time
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
468 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
469 static assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
470 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
471 file.readv();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
472 file.name = to;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
473 file.writev();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
474 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
475
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
476 /*************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
477 * Search Path for file.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
478 * Input:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
479 * cwd if true, search current directory before searching path
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
480 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
481
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
482 static string searchPath(Array path, string name, bool cwd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
483 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
484 if (absolute(name)) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
485 return exists(name) ? name : null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
486 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
487
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
488 if (cwd) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
489 if (exists(name)) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
490 return name;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
491 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
492 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
493
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
494 if (path !is null) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
495 foreach (i; 0..path.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
496 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
497 String p = cast(String)path.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
498 string n = combine(p.str, name);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
499
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
500 if (exists(n))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
501 return n;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
502 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
505 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
506 }
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
507
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
508 static string searchPath(string[] path, string name, bool cwd)
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
509 {
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
510 if (absolute(name)) {
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
511 return exists(name) ? name : null;
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
512 }
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
513
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
514 if (cwd) {
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
515 if (exists(name)) {
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
516 return name;
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
517 }
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
518 }
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
519
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
520 if (path !is null) {
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
521 foreach (i, p; path)
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
522 {
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
523 string n = combine(p, name);
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
524
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
525 if (exists(n))
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
526 return n;
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
527 }
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
528 }
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
529
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
530 return null;
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
531 }
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 14
diff changeset
532
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
533 static int exists(string name)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
534 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 version (POSIX) {
14
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
536 stat_t st;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
537
14
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
538 if (stat(toStringz(name), &st) < 0)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
539 return 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 if (S_ISDIR(st.st_mode))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
541 return 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
542 return 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
543 } else version (_WIN32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
544 HANDLE h = CreateFileA(toStringz(name), GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, HANDLE.init); ///
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
545 if (h == INVALID_HANDLE_VALUE) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546 return 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
547 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
548
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
549 CloseHandle(h);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
550
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
551 DWORD dw = GetFileAttributesA(name.ptr); /// CARE!
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
552 if (dw == -1L) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
553 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
554 return 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
555 } else if (dw & FILE_ATTRIBUTE_DIRECTORY) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
556 return 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
557 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
558 return 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
559 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
560 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
561 static assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
562 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
563 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
564
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
565 static void ensurePathExists(string path)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
566 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
567 try {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
568 mkdirRecurse(path);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
569 } catch {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
570 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
571 }
14
2cc604139636 Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents: 5
diff changeset
572 }