annotate dmd/File.d @ 174:af724d3510d7

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