Mercurial > projects > ddmd
annotate dmd/File.d @ 146:af7e5ebef6ad
redundant extern(C)
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Tue, 14 Sep 2010 23:34:50 +0100 |
parents | e28b18c23469 |
children | af724d3510d7 |
rev | line source |
---|---|
0 | 1 module dmd.File; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.FileName; |
5 import dmd.Array; | |
6 import dmd.Util; | |
7 | |
8 import core.stdc.stdlib; | |
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:
4
diff
changeset
|
9 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:
4
diff
changeset
|
10 { |
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:
4
diff
changeset
|
11 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:
4
diff
changeset
|
12 } |
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:
4
diff
changeset
|
13 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:
4
diff
changeset
|
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:
4
diff
changeset
|
15 import core.sys.posix.fcntl; |
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:
4
diff
changeset
|
16 import core.stdc.errno; |
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:
4
diff
changeset
|
17 import core.sys.posix.unistd; |
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:
4
diff
changeset
|
18 import core.sys.posix.utime; |
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:
4
diff
changeset
|
19 import core.stdc.stdio; |
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:
4
diff
changeset
|
20 } |
0 | 21 |
22 import std.string : toStringz; | |
23 | |
4 | 24 import core.memory; |
2 | 25 |
0 | 26 class File |
27 { | |
28 int ref_; // != 0 if this is a reference to someone else's buffer | |
29 ubyte* buffer; // data for our file | |
30 uint len; // amount of data in buffer[] | |
31 void* touchtime; // system time to use for file | |
32 | |
33 FileName name; // name of our file | |
34 | |
35 this(string n) | |
36 { | |
37 name = new FileName(n); | |
38 } | |
39 | |
40 this(FileName n) | |
41 { | |
42 name = n; | |
43 } | |
44 | |
45 ~this() | |
46 { | |
47 if (buffer !is null) { | |
48 if (ref_ == 0) { | |
2 | 49 ///free(buffer); |
0 | 50 } else { |
114 | 51 version (Windows) { |
0 | 52 if (ref_ == 2) { |
53 UnmapViewOfFile(buffer); | |
54 } | |
55 } | |
56 } | |
57 } | |
58 | |
59 if (touchtime !is null) { | |
2 | 60 ///free(touchtime); |
0 | 61 } |
62 } | |
63 | |
64 void mark() | |
65 { | |
66 ///mem.mark(buffer); | |
67 ///mem.mark(touchtime); | |
68 ///mem.mark(name); | |
69 } | |
70 | |
71 string toChars() | |
72 { | |
73 return name.toChars(); | |
74 } | |
75 | |
76 /* Read file, return !=0 if error | |
77 */ | |
78 | |
79 int read() | |
80 { | |
114 | 81 version (Posix) |
82 { | |
0 | 83 int result = 0; |
84 | |
85 string name = this.name.toChars(); | |
86 | |
87 //printf("File::read('%s')\n",name); | |
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:
4
diff
changeset
|
88 int fd = open(toStringz(name), O_RDONLY); |
0 | 89 if (fd == -1) { |
90 result = errno; | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
91 printf("file: %s\n", toStringz(name)); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
92 printf("\topen error, errno = %d\n", errno); |
0 | 93 goto err1; |
94 } | |
95 | |
96 if (ref_ == 0) { | |
2 | 97 ///free(buffer); |
0 | 98 } |
99 | |
100 ref_ = 0; // we own the buffer now | |
101 | |
102 //printf("\tfile opened\n"); | |
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:
4
diff
changeset
|
103 stat_t buf; |
0 | 104 if (fstat(fd, &buf)) { |
105 printf("\tfstat error, errno = %d\n", errno); | |
106 goto err2; | |
107 } | |
108 | |
109 off_t size = buf.st_size; | |
2 | 110 buffer = cast(ubyte*)GC.malloc(size + 2); |
0 | 111 if (buffer is null) { |
112 printf("\tmalloc error, errno = %d\n", errno); | |
113 goto err2; | |
114 } | |
115 | |
116 ssize_t numread = .read(fd, buffer, size); | |
117 if (numread != size) { | |
118 printf("\tread error, errno = %d\n",errno); | |
119 goto err2; | |
120 } | |
121 | |
122 if (touchtime !is null) { | |
123 memcpy(touchtime, &buf, buf.sizeof); | |
124 } | |
125 | |
126 if (close(fd) == -1) { | |
127 printf("\tclose error, errno = %d\n",errno); | |
128 goto err; | |
129 } | |
130 | |
131 len = size; | |
132 | |
133 // Always store a wchar ^Z past end of buffer so scanner has a sentinel | |
134 buffer[size] = 0; // ^Z is obsolete, use 0 | |
135 buffer[size + 1] = 0; | |
136 | |
137 return 0; | |
138 | |
139 err2: | |
140 close(fd); | |
141 | |
142 err: | |
2 | 143 ///free(buffer); |
0 | 144 buffer = null; |
145 len = 0; | |
146 | |
147 err1: | |
148 result = 1; | |
149 return result; | |
114 | 150 } else version (Windows) { |
0 | 151 DWORD size; |
152 DWORD numread; | |
153 HANDLE h; | |
154 int result = 0; | |
155 | |
156 string name = this.name.toChars(); | |
157 //std.stdio.writeln("Open file ", name); | |
158 | |
159 h = CreateFileA(toStringz(name), GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, HANDLE.init); | |
160 if (h == INVALID_HANDLE_VALUE) { | |
161 goto err1; | |
162 } | |
163 | |
164 if (!ref_) { | |
2 | 165 ///free(buffer); |
0 | 166 } |
167 ref_ = 0; | |
168 | |
169 size = GetFileSize(h, null); | |
2 | 170 buffer = cast(ubyte*) GC.malloc(size + 2); |
0 | 171 if (!buffer) |
172 goto err2; | |
173 | |
174 if (ReadFile(h, buffer, size, &numread, null) != TRUE) | |
175 goto err2; | |
176 | |
177 if (numread != size) | |
178 goto err2; | |
179 | |
180 if (touchtime) { | |
181 if (!GetFileTime(h, null, null, &(cast(WIN32_FIND_DATA*)touchtime).ftLastWriteTime)) | |
182 goto err2; | |
183 } | |
184 | |
185 if (!CloseHandle(h)) | |
186 goto err; | |
187 | |
188 len = size; | |
189 | |
190 // Always store a wchar ^Z past end of buffer so scanner has a sentinel | |
191 buffer[size] = 0; // ^Z is obsolete, use 0 | |
192 buffer[size + 1] = 0; | |
193 return 0; | |
194 | |
195 err2: | |
196 CloseHandle(h); | |
197 err: | |
2 | 198 ///free(buffer); |
0 | 199 buffer = null; |
200 len = 0; | |
201 | |
202 err1: | |
203 result = 1; | |
204 return result; | |
205 } else { | |
206 static assert(0); | |
207 } | |
208 } | |
209 | |
210 /* Write file, either succeed or fail | |
211 * with error message & exit. | |
212 */ | |
213 | |
214 void readv() | |
215 { | |
216 if (read()) | |
217 error("Error reading file '%s'\n",name.toChars()); | |
218 } | |
219 | |
220 /* Read file, return !=0 if error | |
221 */ | |
222 | |
223 int mmread() | |
224 { | |
225 assert(false); | |
226 } | |
227 | |
228 /* Write file, either succeed or fail | |
229 * with error message & exit. | |
230 */ | |
231 | |
232 void mmreadv() | |
233 { | |
234 assert(false); | |
235 } | |
236 | |
237 /* Write file, return !=0 if error | |
238 */ | |
239 | |
240 /********************************************* | |
241 * Write a file. | |
242 * Returns: | |
243 * 0 success | |
244 */ | |
245 int write() | |
246 { | |
247 version (POSIX) { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
248 //assert(false); |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
249 |
0 | 250 int fd; |
251 ssize_t numwritten; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
252 const(char)* name = toStringz(this.name.toChars()); |
0 | 253 fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
254 if (fd == -1) | |
255 goto err; | |
256 | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
257 numwritten = core.sys.posix.unistd.write(fd, buffer, len); |
0 | 258 if (len != numwritten) |
259 goto err2; | |
260 | |
261 if (close(fd) == -1) | |
262 goto err; | |
263 | |
264 if (touchtime) | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
265 { utimbuf ubuf; |
0 | 266 |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
267 ubuf.actime = (cast(stat_t *)touchtime).st_atime; |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
268 ubuf.modtime = (cast(stat_t *)touchtime).st_mtime; |
0 | 269 if (utime(name, &ubuf)) |
270 goto err; | |
271 } | |
272 return 0; | |
273 | |
274 err2: | |
275 close(fd); | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
276 .remove(name); |
0 | 277 err: |
278 return 1; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
279 |
114 | 280 } else version (Windows) { |
0 | 281 HANDLE h; |
282 DWORD numwritten; | |
283 | |
284 const(char)* name = toStringz(this.name.toChars()); | |
285 h = CreateFileA(name, GENERIC_WRITE, 0, null, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, null); | |
286 if (h == INVALID_HANDLE_VALUE) | |
287 goto err; | |
288 | |
289 if (WriteFile(h, buffer, len, &numwritten, null) != TRUE) | |
290 goto err2; | |
291 | |
292 if (len != numwritten) | |
293 goto err2; | |
294 | |
295 if (touchtime) { | |
296 SetFileTime(h, null, null, &(cast(WIN32_FIND_DATA*)touchtime).ftLastWriteTime); | |
297 } | |
298 if (!CloseHandle(h)) | |
299 goto err; | |
300 return 0; | |
301 | |
302 err2: | |
303 CloseHandle(h); | |
304 DeleteFileA(name); | |
305 err: | |
306 return 1; | |
307 } else { | |
308 static assert(false); | |
309 } | |
310 } | |
311 | |
312 /* Write file, either succeed or fail | |
313 * with error message & exit. | |
314 */ | |
315 | |
316 void writev() | |
317 { | |
318 if (write()) { | |
319 error("Error writing file '%s'\n", name.toChars()); | |
320 } | |
321 } | |
322 | |
323 /* Return !=0 if file exists. | |
324 * 0: file doesn't exist | |
325 * 1: normal file | |
326 * 2: directory | |
327 */ | |
328 | |
329 /* Append to file, return !=0 if error | |
330 */ | |
331 | |
332 int append() | |
333 { | |
334 assert(false); | |
335 } | |
336 | |
337 /* Append to file, either succeed or fail | |
338 * with error message & exit. | |
339 */ | |
340 | |
341 void appendv() | |
342 { | |
343 assert(false); | |
344 } | |
345 | |
346 /* Return !=0 if file exists. | |
347 * 0: file doesn't exist | |
348 * 1: normal file | |
349 * 2: directory | |
350 */ | |
351 | |
352 int exists() | |
353 { | |
354 assert(false); | |
355 } | |
356 | |
357 /* Given wildcard filespec, return an array of | |
358 * matching File's. | |
359 */ | |
360 | |
361 static Array match(char*) | |
362 { | |
363 assert(false); | |
364 } | |
365 | |
366 static Array match(FileName *) | |
367 { | |
368 assert(false); | |
369 } | |
370 | |
371 // Compare file times. | |
372 // Return <0 this < f | |
373 // =0 this == f | |
374 // >0 this > f | |
375 int compareTime(File f) | |
376 { | |
377 assert(false); | |
378 } | |
379 | |
380 // Read system file statistics | |
381 void stat() | |
382 { | |
383 assert(false); | |
384 } | |
385 | |
386 /* Set buffer | |
387 */ | |
388 | |
389 void setbuffer(void* buffer, uint len) | |
390 { | |
391 this.buffer = cast(ubyte*)buffer; | |
392 this.len = len; | |
393 } | |
394 | |
395 void checkoffset(size_t offset, size_t nbytes) | |
396 { | |
397 assert(false); | |
398 } | |
399 | |
400 void remove() // delete file | |
401 { | |
402 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:
4
diff
changeset
|
403 .remove(toStringz(this.name.toChars())); |
0 | 404 } else version (_WIN32) { |
405 DeleteFileA(toStringz(this.name.toChars())); | |
406 } else { | |
407 assert(0); | |
408 } | |
409 } | |
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:
4
diff
changeset
|
410 } |