author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 19 Jan 2012 11:46:00 +0000 | |
branch | jv |
changeset 17911 | a99f15c5efa5 |
parent 17910 | 8d796ca8bd1d |
child 18011 | deb0c3355881 |
permissions | -rw-r--r-- |
7595 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5409 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
4762 | 14 |
OSFileHandle subclass:#UnixFileDescriptorHandle |
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'OS-Unix' |
|
19 |
! |
|
20 |
||
21 |
!UnixFileDescriptorHandle primitiveDefinitions! |
|
22 |
%{ |
|
23 |
#include <stdio.h> |
|
5416 | 24 |
#include <errno.h> |
4762 | 25 |
%} |
26 |
||
27 |
! ! |
|
28 |
||
7595 | 29 |
!UnixFileDescriptorHandle class methodsFor:'documentation'! |
30 |
||
31 |
copyright |
|
32 |
" |
|
33 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
34 |
All Rights Reserved |
|
35 |
||
36 |
This software is furnished under a license and may be used |
|
37 |
only in accordance with the terms of that license and with the |
|
38 |
inclusion of the above copyright notice. This software may not |
|
39 |
be provided or otherwise made available to, or used by, any |
|
40 |
other person. No title to or ownership of the software is |
|
41 |
hereby transferred. |
|
42 |
" |
|
43 |
! ! |
|
4762 | 44 |
|
5415 | 45 |
!UnixFileDescriptorHandle methodsFor:'input/output'! |
46 |
||
47 |
readBytes:count into:aByteBuffer startingAt:firstIndex |
|
48 |
"read count bytes into a byte-buffer; |
|
49 |
Return the number of bytes read (negative on error)" |
|
50 |
||
51 |
%{ |
|
52 |
unsigned char *extPtr; |
|
53 |
int nRead = -1; |
|
54 |
INT fd = (INT)(__externalAddressVal(self)); |
|
55 |
INT cnt, offs; |
|
56 |
int nInstBytes, objSize; |
|
5416 | 57 |
|
5415 | 58 |
if (! __bothSmallInteger(count, firstIndex)) { |
59 |
goto bad; |
|
60 |
} |
|
61 |
cnt = __smallIntegerVal(count); |
|
62 |
offs = __smallIntegerVal(firstIndex) - 1; |
|
63 |
||
64 |
if (fd < 0) { |
|
65 |
goto bad; |
|
66 |
} |
|
7198
62450598d208
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
5416
diff
changeset
|
67 |
if (__isExternalBytesLike(aByteBuffer)) { |
5415 | 68 |
OBJ sz; |
69 |
||
70 |
nInstBytes = 0; |
|
71 |
extPtr = (char *)(__externalBytesAddress(aByteBuffer)); |
|
72 |
sz = __externalBytesSize(aByteBuffer); |
|
73 |
if (__isSmallInteger(sz)) { |
|
74 |
objSize = __smallIntegerVal(sz); |
|
75 |
} else { |
|
76 |
objSize = -1; /* unknown */ |
|
77 |
} |
|
78 |
} else { |
|
79 |
OBJ oClass; |
|
80 |
int nInstVars; |
|
81 |
||
82 |
oClass = __Class(aByteBuffer); |
|
83 |
switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
84 |
case BYTEARRAY: |
|
85 |
case WORDARRAY: |
|
86 |
case LONGARRAY: |
|
87 |
case SWORDARRAY: |
|
88 |
case SLONGARRAY: |
|
89 |
case FLOATARRAY: |
|
90 |
case DOUBLEARRAY: |
|
91 |
break; |
|
92 |
default: |
|
93 |
goto bad; |
|
94 |
} |
|
95 |
extPtr = (char *)0; |
|
96 |
nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
97 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
98 |
objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes; |
|
99 |
} |
|
100 |
if ((offs >= 0) |
|
101 |
&& (cnt >= 0) |
|
102 |
&& ((objSize == -1) || (objSize >= (cnt + offs)))) { |
|
103 |
nRead = 0; |
|
104 |
||
105 |
do { |
|
106 |
int n; |
|
107 |
||
108 |
if (extPtr) { |
|
109 |
n = read(fd, extPtr+offs, cnt); |
|
110 |
} else { |
|
111 |
char *bp; |
|
112 |
||
113 |
/* |
|
114 |
* on interrupt, anObject may be moved to another location. |
|
115 |
* So we recompute the byte-address here. |
|
116 |
*/ |
|
117 |
bp = __ByteArrayInstPtr(aByteBuffer)->ba_element + nInstBytes; |
|
118 |
||
119 |
n = read(fd, bp + offs, cnt); |
|
120 |
} |
|
121 |
if (n > 0) { |
|
122 |
cnt -= n; |
|
123 |
offs += n; |
|
124 |
nRead += n; |
|
125 |
} else { |
|
126 |
if (n < 0) { |
|
127 |
if (errno == EINTR) { |
|
128 |
continue; |
|
129 |
} |
|
130 |
break; |
|
131 |
} |
|
132 |
} |
|
133 |
} while (cnt > 0); |
|
134 |
||
135 |
RETURN (__mkSmallInteger(nRead)); |
|
136 |
} |
|
137 |
bad: ; |
|
138 |
%}. |
|
139 |
^ self primitiveFailed |
|
140 |
||
141 |
" |
|
142 |
|h buff n| |
|
143 |
||
144 |
h := self basicNew. |
|
145 |
h setFileDescriptor:0. |
|
146 |
buff := ByteArray new:10. buff inspect. |
|
147 |
n := h readBytes:10 into:buff startingAt:1. |
|
148 |
Transcript show:n; space; showCR:buff. |
|
149 |
" |
|
150 |
! |
|
151 |
||
152 |
writeBytes:count from:aByteBuffer startingAt:firstIndex |
|
153 |
"write count bytes from a byte-buffer; |
|
154 |
Return the number of bytes written (negative on error)" |
|
155 |
||
156 |
%{ |
|
157 |
unsigned char *extPtr; |
|
158 |
int nWritten = -1; |
|
159 |
INT fd = (INT)(__externalAddressVal(self)); |
|
160 |
INT cnt, offs; |
|
161 |
int nInstBytes, objSize; |
|
5416 | 162 |
|
5415 | 163 |
if (! __bothSmallInteger(count, firstIndex)) { |
164 |
goto bad; |
|
165 |
} |
|
166 |
cnt = __smallIntegerVal(count); |
|
167 |
offs = __smallIntegerVal(firstIndex) - 1; |
|
168 |
||
169 |
if (fd < 0) { |
|
170 |
goto bad; |
|
171 |
} |
|
7198
62450598d208
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
5416
diff
changeset
|
172 |
if (__isExternalBytesLike(aByteBuffer)) { |
5415 | 173 |
OBJ sz; |
174 |
||
175 |
nInstBytes = 0; |
|
176 |
extPtr = (char *)(__externalBytesAddress(aByteBuffer)); |
|
177 |
sz = __externalBytesSize(aByteBuffer); |
|
178 |
if (__isSmallInteger(sz)) { |
|
179 |
objSize = __smallIntegerVal(sz); |
|
180 |
} else { |
|
181 |
objSize = -1; /* unknown */ |
|
182 |
} |
|
183 |
} else { |
|
184 |
OBJ oClass; |
|
185 |
int nInstVars; |
|
186 |
||
187 |
oClass = __Class(aByteBuffer); |
|
188 |
switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
189 |
case BYTEARRAY: |
|
190 |
case WORDARRAY: |
|
191 |
case LONGARRAY: |
|
192 |
case SWORDARRAY: |
|
193 |
case SLONGARRAY: |
|
194 |
case FLOATARRAY: |
|
195 |
case DOUBLEARRAY: |
|
196 |
break; |
|
197 |
default: |
|
198 |
goto bad; |
|
199 |
} |
|
200 |
extPtr = (char *)0; |
|
201 |
nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
202 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
203 |
objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes; |
|
204 |
} |
|
205 |
if ((offs >= 0) |
|
206 |
&& (cnt >= 0) |
|
207 |
&& ((objSize == -1) || (objSize >= (cnt + offs)))) { |
|
208 |
nWritten = 0; |
|
209 |
||
210 |
do { |
|
211 |
int n; |
|
212 |
||
213 |
if (extPtr) { |
|
214 |
n = write(fd, extPtr+offs, cnt); |
|
215 |
} else { |
|
216 |
char *bp; |
|
217 |
||
218 |
/* |
|
219 |
* on interrupt, anObject may be moved to another location. |
|
220 |
* So we recompute the byte-address here. |
|
221 |
*/ |
|
222 |
bp = __ByteArrayInstPtr(aByteBuffer)->ba_element + nInstBytes; |
|
223 |
||
224 |
n = write(fd, bp + offs, cnt); |
|
225 |
} |
|
226 |
if (n > 0) { |
|
227 |
cnt -= n; |
|
228 |
offs += n; |
|
229 |
nWritten += n; |
|
230 |
} else { |
|
231 |
if (n < 0) { |
|
232 |
if (errno == EINTR) { |
|
233 |
continue; |
|
234 |
} |
|
235 |
break; |
|
236 |
} |
|
237 |
} |
|
238 |
} while (cnt > 0); |
|
239 |
||
240 |
RETURN (__mkSmallInteger(nWritten)); |
|
241 |
} |
|
242 |
bad: ; |
|
243 |
%}. |
|
244 |
^ self primitiveFailed |
|
245 |
||
246 |
" |
|
247 |
|h buff n| |
|
248 |
||
249 |
h := self basicNew. |
|
250 |
h setFileDescriptor:1. |
|
251 |
buff := '12345678901234567890'. |
|
252 |
n := h writeBytes:10 from:buff startingAt:1. |
|
253 |
" |
|
254 |
! ! |
|
255 |
||
7259 | 256 |
!UnixFileDescriptorHandle methodsFor:'private-accessing'! |
5415 | 257 |
|
258 |
setFileDescriptor:anInteger |
|
259 |
||
260 |
%{ |
|
261 |
if (__isSmallInteger(anInteger)) { |
|
262 |
__externalAddressVal(self) = (OBJ)(__smallIntegerVal(anInteger)); |
|
263 |
} |
|
264 |
%} |
|
265 |
||
266 |
||
267 |
! ! |
|
268 |
||
5413 | 269 |
!UnixFileDescriptorHandle methodsFor:'release'! |
4762 | 270 |
|
5409 | 271 |
closeFile |
272 |
"close the underlying file" |
|
4762 | 273 |
|
274 |
%{ |
|
275 |
INT fd = (INT)(__externalAddressVal(self)); |
|
276 |
||
277 |
if (fd > 0) { |
|
278 |
__externalAddressVal(self) = (OBJ)-1; |
|
279 |
close(fd); |
|
280 |
} |
|
281 |
%}. |
|
282 |
||
283 |
! ! |
|
284 |
||
285 |
!UnixFileDescriptorHandle class methodsFor:'documentation'! |
|
286 |
||
287 |
version |
|
17892 | 288 |
^ '$Header: /cvs/stx/stx/libbasic/UnixFileDescriptorHandle.st,v 1.8 2003/08/30 12:37:09 cg Exp $' |
17865 | 289 |
! |
290 |
||
291 |
version_SVN |
|
17911 | 292 |
^ '$Id: UnixFileDescriptorHandle.st 10761 2012-01-19 11:46:00Z vranyj1 $' |
17846 | 293 |
! ! |
17892 | 294 |
|
17910 | 295 |
|
17911 | 296 |