annotate dmd/FileName.d @ 192:eb38fdcb3e62 default tip

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