annotate dmd/File.d @ 183:190ba98276b3

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