comparison tango/tango/io/DeviceConduit.d @ 132:1700239cab2e trunk

[svn r136] MAJOR UNSTABLE UPDATE!!! Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
author lindquist
date Fri, 11 Jan 2008 17:57:40 +0100
parents
children 0e28624814e8
comparison
equal deleted inserted replaced
131:5825d48b27d1 132:1700239cab2e
1 /*******************************************************************************
2
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
4
5 license: BSD style: $(LICENSE)
6
7 version: Initial release: May 2005
8
9 author: Kris
10
11 *******************************************************************************/
12
13 module tango.io.DeviceConduit;
14
15 private import tango.sys.Common;
16
17 public import tango.io.Conduit;
18
19 private import tango.core.Exception;
20
21 /*******************************************************************************
22
23 Implements a means of reading and writing a file device. Conduits
24 are the primary means of accessing external data, and are usually
25 routed through a Buffer.
26
27 *******************************************************************************/
28
29 class DeviceConduit : Conduit
30 {
31 /// expose in superclass definition also
32 public alias Conduit.error error;
33
34 /***********************************************************************
35
36 Throw an IOException noting the last error
37
38 ***********************************************************************/
39
40 final void error ()
41 {
42 super.error (toString() ~ " :: " ~ SysError.lastMsg);
43 }
44
45 /***********************************************************************
46
47 Return the name of this device
48
49 ***********************************************************************/
50
51 override char[] toString()
52 {
53 return "<device>";
54 }
55
56 /***********************************************************************
57
58 Return a preferred size for buffering conduit I/O
59
60 ***********************************************************************/
61
62 override uint bufferSize ()
63 {
64 return 1024 * 16;
65 }
66
67 /***********************************************************************
68
69 Windows-specific code
70
71 ***********************************************************************/
72
73 version (Win32)
74 {
75 protected HANDLE handle;
76
77 /***************************************************************
78
79 Gain access to the standard IO handles (console etc).
80
81 ***************************************************************/
82
83 protected void reopen (Handle handle)
84 {
85 this.handle = cast(HANDLE) handle;
86 }
87
88 /***************************************************************
89
90 Return the underlying OS handle of this Conduit
91
92 ***************************************************************/
93
94 final override Handle fileHandle ()
95 {
96 return cast(Handle) handle;
97 }
98
99 /***************************************************************
100
101 Release the underlying file
102
103 ***************************************************************/
104
105 override void detach ()
106 {
107 if (handle)
108 if (! CloseHandle (handle))
109 error ();
110 handle = cast(HANDLE) null;
111 }
112
113 /***************************************************************
114
115 Read a chunk of bytes from the file into the provided
116 array (typically that belonging to an IBuffer).
117
118 Returns the number of bytes read, or Eof when there is
119 no further data
120
121 ***************************************************************/
122
123 override uint read (void[] dst)
124 {
125 DWORD read;
126 void *p = dst.ptr;
127
128 if (! ReadFile (handle, p, dst.length, &read, null))
129 // make Win32 behave like linux
130 if (SysError.lastCode is ERROR_BROKEN_PIPE)
131 return Eof;
132 else
133 error ();
134
135 if (read is 0 && dst.length > 0)
136 return Eof;
137 return read;
138 }
139
140 /***************************************************************
141
142 Write a chunk of bytes to the file from the provided
143 array (typically that belonging to an IBuffer)
144
145 ***************************************************************/
146
147 override uint write (void[] src)
148 {
149 DWORD written;
150
151 if (! WriteFile (handle, src.ptr, src.length, &written, null))
152 error ();
153
154 return written;
155 }
156 }
157
158
159 /***********************************************************************
160
161 Unix-specific code.
162
163 ***********************************************************************/
164
165 version (Posix)
166 {
167 protected int handle = -1;
168
169 /***************************************************************
170
171 Gain access to the standard IO handles (console etc).
172
173 ***************************************************************/
174
175 protected void reopen (Handle handle)
176 {
177 this.handle = handle;
178 }
179
180 /***************************************************************
181
182 Return the underlying OS handle of this Conduit
183
184 ***************************************************************/
185
186 final override Handle fileHandle ()
187 {
188 return cast(Handle) handle;
189 }
190
191 /***************************************************************
192
193 Release the underlying file
194
195 ***************************************************************/
196
197 override void detach ()
198 {
199 if (handle >= 0)
200 if (posix.close (handle) is -1)
201 error ();
202 handle = -1;
203 }
204
205 /***************************************************************
206
207 Read a chunk of bytes from the file into the provided
208 array (typically that belonging to an IBuffer)
209
210 ***************************************************************/
211
212 override uint read (void[] dst)
213 {
214 int read = posix.read (handle, dst.ptr, dst.length);
215 if (read == -1)
216 error ();
217 else
218 if (read is 0 && dst.length > 0)
219 return Eof;
220 return read;
221 }
222
223 /***************************************************************
224
225 Write a chunk of bytes to the file from the provided
226 array (typically that belonging to an IBuffer)
227
228 ***************************************************************/
229
230 override uint write (void[] src)
231 {
232 int written = posix.write (handle, src.ptr, src.length);
233 if (written is -1)
234 error ();
235 return written;
236 }
237 }
238 }
239
240