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