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