annotate dmd/File.d @ 16:5c9b78899f5d

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