Mercurial > projects > ldc
annotate tango/tango/io/DeviceConduit.d @ 136:0e28624814e8 trunk
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
author | lindquist |
---|---|
date | Thu, 17 Jan 2008 03:15:12 +0100 |
parents | 1700239cab2e |
children | 0ab29b838084 |
rev | line source |
---|---|
132 | 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 | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
132
diff
changeset
|
21 private extern(C) int printf(char*, ...); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
132
diff
changeset
|
22 |
132 | 23 /******************************************************************************* |
24 | |
25 Implements a means of reading and writing a file device. Conduits | |
26 are the primary means of accessing external data, and are usually | |
27 routed through a Buffer. | |
28 | |
29 *******************************************************************************/ | |
30 | |
31 class DeviceConduit : Conduit | |
32 { | |
33 /// expose in superclass definition also | |
34 public alias Conduit.error error; | |
35 | |
36 /*********************************************************************** | |
37 | |
38 Throw an IOException noting the last error | |
39 | |
40 ***********************************************************************/ | |
41 | |
42 final void error () | |
43 { | |
44 super.error (toString() ~ " :: " ~ SysError.lastMsg); | |
45 } | |
46 | |
47 /*********************************************************************** | |
48 | |
49 Return the name of this device | |
50 | |
51 ***********************************************************************/ | |
52 | |
53 override char[] toString() | |
54 { | |
55 return "<device>"; | |
56 } | |
57 | |
58 /*********************************************************************** | |
59 | |
60 Return a preferred size for buffering conduit I/O | |
61 | |
62 ***********************************************************************/ | |
63 | |
64 override uint bufferSize () | |
65 { | |
66 return 1024 * 16; | |
67 } | |
68 | |
69 /*********************************************************************** | |
70 | |
71 Windows-specific code | |
72 | |
73 ***********************************************************************/ | |
74 | |
75 version (Win32) | |
76 { | |
77 protected HANDLE handle; | |
78 | |
79 /*************************************************************** | |
80 | |
81 Gain access to the standard IO handles (console etc). | |
82 | |
83 ***************************************************************/ | |
84 | |
85 protected void reopen (Handle handle) | |
86 { | |
87 this.handle = cast(HANDLE) handle; | |
88 } | |
89 | |
90 /*************************************************************** | |
91 | |
92 Return the underlying OS handle of this Conduit | |
93 | |
94 ***************************************************************/ | |
95 | |
96 final override Handle fileHandle () | |
97 { | |
98 return cast(Handle) handle; | |
99 } | |
100 | |
101 /*************************************************************** | |
102 | |
103 Release the underlying file | |
104 | |
105 ***************************************************************/ | |
106 | |
107 override void detach () | |
108 { | |
109 if (handle) | |
110 if (! CloseHandle (handle)) | |
111 error (); | |
112 handle = cast(HANDLE) null; | |
113 } | |
114 | |
115 /*************************************************************** | |
116 | |
117 Read a chunk of bytes from the file into the provided | |
118 array (typically that belonging to an IBuffer). | |
119 | |
120 Returns the number of bytes read, or Eof when there is | |
121 no further data | |
122 | |
123 ***************************************************************/ | |
124 | |
125 override uint read (void[] dst) | |
126 { | |
127 DWORD read; | |
128 void *p = dst.ptr; | |
129 | |
130 if (! ReadFile (handle, p, dst.length, &read, null)) | |
131 // make Win32 behave like linux | |
132 if (SysError.lastCode is ERROR_BROKEN_PIPE) | |
133 return Eof; | |
134 else | |
135 error (); | |
136 | |
137 if (read is 0 && dst.length > 0) | |
138 return Eof; | |
139 return read; | |
140 } | |
141 | |
142 /*************************************************************** | |
143 | |
144 Write a chunk of bytes to the file from the provided | |
145 array (typically that belonging to an IBuffer) | |
146 | |
147 ***************************************************************/ | |
148 | |
149 override uint write (void[] src) | |
150 { | |
151 DWORD written; | |
152 | |
153 if (! WriteFile (handle, src.ptr, src.length, &written, null)) | |
154 error (); | |
155 | |
156 return written; | |
157 } | |
158 } | |
159 | |
160 | |
161 /*********************************************************************** | |
162 | |
163 Unix-specific code. | |
164 | |
165 ***********************************************************************/ | |
166 | |
167 version (Posix) | |
168 { | |
169 protected int handle = -1; | |
170 | |
171 /*************************************************************** | |
172 | |
173 Gain access to the standard IO handles (console etc). | |
174 | |
175 ***************************************************************/ | |
176 | |
177 protected void reopen (Handle handle) | |
178 { | |
179 this.handle = handle; | |
180 } | |
181 | |
182 /*************************************************************** | |
183 | |
184 Return the underlying OS handle of this Conduit | |
185 | |
186 ***************************************************************/ | |
187 | |
188 final override Handle fileHandle () | |
189 { | |
190 return cast(Handle) handle; | |
191 } | |
192 | |
193 /*************************************************************** | |
194 | |
195 Release the underlying file | |
196 | |
197 ***************************************************************/ | |
198 | |
199 override void detach () | |
200 { | |
201 if (handle >= 0) | |
202 if (posix.close (handle) is -1) | |
203 error (); | |
204 handle = -1; | |
205 } | |
206 | |
207 /*************************************************************** | |
208 | |
209 Read a chunk of bytes from the file into the provided | |
210 array (typically that belonging to an IBuffer) | |
211 | |
212 ***************************************************************/ | |
213 | |
214 override uint read (void[] dst) | |
215 { | |
216 int read = posix.read (handle, dst.ptr, dst.length); | |
217 if (read == -1) | |
218 error (); | |
219 else | |
220 if (read is 0 && dst.length > 0) | |
221 return Eof; | |
222 return read; | |
223 } | |
224 | |
225 /*************************************************************** | |
226 | |
227 Write a chunk of bytes to the file from the provided | |
228 array (typically that belonging to an IBuffer) | |
229 | |
230 ***************************************************************/ | |
231 | |
232 override uint write (void[] src) | |
233 { | |
234 int written = posix.write (handle, src.ptr, src.length); | |
235 if (written is -1) | |
236 error (); | |
237 return written; | |
238 } | |
239 } | |
240 } | |
241 | |
242 |