author | Claus Gittinger <cg@exept.de> |
Tue, 29 Dec 1998 17:56:57 +0100 | |
changeset 3950 | f9c4485a91d1 |
parent 3949 | 904575569fb4 |
child 3956 | 51f1a9a4d63f |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
2264 | 3 |
All Rights Reserved |
1 | 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 |
||
13 |
ReadWriteStream subclass:#ExternalStream |
|
2920 | 14 |
instanceVariableNames:'filePointer mode buffered binary eolMode hitEOF didWrite |
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
15 |
lastErrorNumber readAhead' |
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
16 |
classVariableNames:'Lobby LastErrorNumber InvalidReadSignal InvalidWriteSignal |
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
17 |
InvalidModeSignal OpenErrorSignal StreamNotOpenSignal |
2920 | 18 |
InvalidOperationSignal DefaultEOLMode ReadMode ReadWriteMode |
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
19 |
WriteMode AppendMode CreateReadWriteMode StreamIOErrorSignal' |
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
20 |
poolDictionaries:'' |
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
21 |
category:'Streams-External' |
1 | 22 |
! |
23 |
||
216 | 24 |
!ExternalStream primitiveDefinitions! |
1 | 25 |
%{ |
2896 | 26 |
|
27 |
#ifdef __openVMS__ |
|
28 |
# undef __new |
|
29 |
#endif |
|
30 |
||
1 | 31 |
#include <stdio.h> |
437 | 32 |
#define _STDIO_H_INCLUDED_ |
33 |
||
2903 | 34 |
#ifndef NO_FCNTL_H |
35 |
# include <fcntl.h> |
|
36 |
# define _FCNTL_H_INCLUDED_ |
|
37 |
#endif |
|
437 | 38 |
|
10 | 39 |
#include <errno.h> |
437 | 40 |
#define _ERRNO_H_INCLUDED_ |
42 | 41 |
|
1141 | 42 |
#ifdef LINUX |
43 |
/* use inline string macros */ |
|
44 |
# define __STRINGDEFS__ |
|
45 |
# include <linuxIntern.h> |
|
46 |
#endif |
|
47 |
||
42 | 48 |
#ifdef hpux |
49 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
|
50 |
#endif |
|
216 | 51 |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
52 |
#ifndef SEEK_SET |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
53 |
# define SEEK_SET 0 |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
54 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
55 |
#ifndef SEEK_CUR |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
56 |
# define SEEK_CUR 1 |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
57 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
58 |
#ifndef SEEK_END |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
59 |
# define SEEK_END 2 |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
60 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
61 |
|
2961 | 62 |
#ifdef __VMS__ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
63 |
# define CLEAR_ERRNO errno = 0; |
2961 | 64 |
#else |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
65 |
# define CLEAR_ERRNO /* nothing */ |
2961 | 66 |
#endif |
67 |
||
3420 | 68 |
#ifdef LATER |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
69 |
#define __isFilePointer(x) (__Class(x) == ExternalStream__FilePointer) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
70 |
#define __CHANGECLASS(o, x) (__qClass(o) = (x), o) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
71 |
#define __MKFILEPOINTER(f) __CHANGECLASS(__MKEXTERNALADDRESS(f), ExternalStream__FilePointer) |
3420 | 72 |
#else |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
73 |
#define __MKFILEPOINTER(f) __MKEXTERNALADDRESS(f) |
3420 | 74 |
#endif |
75 |
||
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
76 |
#ifdef xxxWIN32 |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
77 |
# define NO_STDIO |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
78 |
# define __HANDLEVal(o) (HFILE)(__externalAddressVal(o)) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
79 |
# define READ(f, cp, n) _lread((f), (cp), (n)) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
80 |
# define WRITE(f, cp, n) _lwrite((f), (cp), (n)) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
81 |
# define FFLUSH(fp) /* nothing */ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
82 |
# undef STDIO_NEEDS_FSEEK |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
83 |
# define FILEPOINTER HFILE |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
84 |
# define FILENO(f) (f) |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
85 |
#else |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
86 |
# define STDIO_NEEDS_FSEEK |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
87 |
# define FILEPOINTER FILE * |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
88 |
# define READ(f, cp, n) read(f, cp, n) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
89 |
# define WRITE(f, cp, n) write(f, cp, n) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
90 |
# define FFLUSH(fp) fflush(fp) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
91 |
# define FILENO(f) fileno(f) |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
92 |
#endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
93 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
94 |
|
2596 | 95 |
#if defined(WIN32) && defined(NO_STDIO) |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
96 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
97 |
# ifdef i386 |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
98 |
# define _X86_ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
99 |
# endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
100 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
101 |
# undef INT |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
102 |
# undef Array |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
103 |
# undef Number |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
104 |
# undef Method |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
105 |
# undef Point |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
106 |
# undef Rectangle |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
107 |
# undef Block |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
108 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
109 |
/* # include <windows.h> /* */ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
110 |
# include <stdarg.h> /* */ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
111 |
# include <windef.h> /* */ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
112 |
# include <winbase.h> /* */ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
113 |
# include <wingdi.h> /* */ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
114 |
# include <winuser.h> /* */ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
115 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
116 |
# ifdef __DEF_Array |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
117 |
# define Array __DEF_Array |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
118 |
# endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
119 |
# ifdef __DEF_Number |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
120 |
# define Number __DEF_Number |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
121 |
# endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
122 |
# ifdef __DEF_Method |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
123 |
# define Method __DEF_Method |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
124 |
# endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
125 |
# ifdef __DEF_Point |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
126 |
# define Point __DEF_Point |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
127 |
# endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
128 |
# ifdef __DEF_Block |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
129 |
# define Block __DEF_Block |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
130 |
# endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
131 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
132 |
# define stat _stat |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
133 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
134 |
#endif /* WIN32 */ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
135 |
|
3681 | 136 |
extern char *__survStartPtr, *__survEndPtr; |
137 |
||
3682 | 138 |
#ifdef DEBUGGING |
139 |
# define DEBUGBUFFER(buf) \ |
|
3681 | 140 |
if (((char *)(buf) >= __survStartPtr) \ |
141 |
&& ((char *)(buf) < __survEndPtr)) { \ |
|
142 |
__fatal0("read into survivor\n"); \ |
|
143 |
} |
|
144 |
||
3682 | 145 |
#else |
146 |
# define DEBUGBUFFER(buf) /* nothing */ |
|
147 |
#endif |
|
148 |
||
216 | 149 |
/* |
150 |
* stdio library requires an fseek before reading whenever a file |
|
151 |
* is open for read/write and the last operation was a write. |
|
152 |
* (also vice-versa). |
|
153 |
* All code should use the following macro before doing reads: |
|
154 |
*/ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
155 |
#ifdef STDIO_NEEDS_FSEEK |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
156 |
# define OPT_FSEEK(f, pos, whence) fseek(f, pos, whence) |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
157 |
#else |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
158 |
# define OPT_FSEEK(f, pos, whence) /* nothing */ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
159 |
#endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
160 |
|
216 | 161 |
#define __READING__(f) \ |
1133 | 162 |
if ((__INST(didWrite) != false) \ |
163 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
3681 | 164 |
__INST(didWrite) = false; \ |
165 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
216 | 166 |
} |
167 |
||
168 |
#define __WRITING__(f) \ |
|
1133 | 169 |
if ((__INST(didWrite) != true) \ |
170 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
3681 | 171 |
__INST(didWrite) = true; \ |
172 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
216 | 173 |
} |
174 |
||
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
175 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
176 |
#ifdef NO_STDIO |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
177 |
#define __UNGETC__(c, f, isBuffered) \ |
2963 | 178 |
__INST(readAhead) = __MKSMALLINT((c)); |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
179 |
#else |
2264 | 180 |
#define __UNGETC__(c, f, isBuffered) \ |
181 |
if (isBuffered) { \ |
|
3681 | 182 |
ungetc((c), (f)); \ |
2264 | 183 |
} else { \ |
3681 | 184 |
__INST(readAhead) = __MKSMALLINT((c)); \ |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
185 |
} |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
186 |
#endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
187 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
188 |
#ifdef NO_STDIO |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
189 |
#define __READBYTE__(ret, f, buf, isBuffered) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
190 |
{ \ |
3681 | 191 |
OBJ rA = __INST(readAhead); \ |
192 |
if (rA != nil) { \ |
|
193 |
*(buf) = __intVal(rA); \ |
|
194 |
DEBUGBUFFER(buf); \ |
|
195 |
__INST(readAhead) = nil; \ |
|
196 |
(ret) = 1; \ |
|
197 |
} else { \ |
|
198 |
for (;;) { \ |
|
199 |
CLEAR_ERRNO; \ |
|
200 |
(ret) = READ(f, buf, 1); \ |
|
201 |
DEBUGBUFFER(buf); \ |
|
202 |
if ((ret) >= 0 || errno != EINTR) \ |
|
203 |
break; \ |
|
204 |
__HANDLE_INTERRUPTS__; \ |
|
205 |
} \ |
|
206 |
} \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
207 |
} |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
208 |
#else |
2264 | 209 |
#define __READBYTE__(ret, f, buf, isBuffered) \ |
210 |
if (isBuffered) { \ |
|
3681 | 211 |
for (;;) { \ |
212 |
CLEAR_ERRNO; \ |
|
213 |
(ret) = getc(f); \ |
|
214 |
if ((ret) >= 0) { \ |
|
215 |
DEBUGBUFFER(buf); \ |
|
216 |
*(buf) = (ret); \ |
|
217 |
(ret) = 1; \ |
|
218 |
} else if (ferror(f)) { \ |
|
219 |
if (errno == EINTR) { \ |
|
220 |
__HANDLE_INTERRUPTS__; \ |
|
221 |
clearerr(f); \ |
|
222 |
continue; \ |
|
223 |
} \ |
|
224 |
} else \ |
|
225 |
(ret) = 0; \ |
|
226 |
break; \ |
|
227 |
} \ |
|
2264 | 228 |
} else { \ |
3681 | 229 |
OBJ rA = __INST(readAhead); \ |
230 |
if (rA != nil) { \ |
|
231 |
*(buf) = __intVal(rA); \ |
|
232 |
DEBUGBUFFER(buf); \ |
|
233 |
__INST(readAhead) = nil; \ |
|
234 |
(ret) = 1; \ |
|
235 |
} else { \ |
|
236 |
for (;;) { \ |
|
237 |
CLEAR_ERRNO; \ |
|
238 |
(ret) = read(fileno(f), buf, 1); \ |
|
239 |
DEBUGBUFFER(buf); \ |
|
240 |
if ((ret) >= 0 || errno != EINTR) \ |
|
241 |
break; \ |
|
242 |
__HANDLE_INTERRUPTS__; \ |
|
243 |
} \ |
|
244 |
} \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
245 |
} |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
246 |
#endif |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
247 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
248 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
249 |
* read_bytes into a c-buffer |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
250 |
* (which may NOT move) |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
251 |
*/ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
252 |
#ifdef NO_STDIO |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
253 |
#define __READBYTES__(ret, f, buf, cnt, isBuffered) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
254 |
{ \ |
3681 | 255 |
int __offs = 0; \ |
256 |
\ |
|
257 |
while (__offs < (cnt)) { \ |
|
258 |
OBJ rA = __INST(readAhead); \ |
|
259 |
if (rA != nil) { \ |
|
260 |
(buf)[__offs] = __intVal(rA); \ |
|
261 |
DEBUGBUFFER(buf); \ |
|
262 |
__INST(readAhead) = nil; \ |
|
263 |
(ret) = 1; \ |
|
264 |
} else { \ |
|
265 |
CLEAR_ERRNO; \ |
|
266 |
(ret) = READ(f, (buf)+__offs, (cnt)-__offs); \ |
|
267 |
DEBUGBUFFER(buf); \ |
|
268 |
if ((ret) <= 0) { \ |
|
269 |
if ((ret) < 0 && errno == EINTR) { \ |
|
270 |
__HANDLE_INTERRUPTS__; \ |
|
271 |
continue; \ |
|
272 |
} \ |
|
273 |
break; \ |
|
274 |
} \ |
|
275 |
} \ |
|
276 |
__offs += (ret); \ |
|
277 |
} \ |
|
278 |
if (__offs > 0) \ |
|
279 |
(ret) = __offs; \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
280 |
} |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
281 |
#else |
2264 | 282 |
#define __READBYTES__(ret, f, buf, cnt, isBuffered) \ |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
283 |
(ret) = 0; \ |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
284 |
if (isBuffered) { \ |
3681 | 285 |
int __offs = 0; \ |
286 |
while (__offs < (cnt)) { \ |
|
287 |
CLEAR_ERRNO; \ |
|
288 |
(ret) = getc(f); \ |
|
289 |
if ((ret) < 0) { \ |
|
290 |
if (ferror(f)) { \ |
|
291 |
if (errno == EINTR) { \ |
|
292 |
__HANDLE_INTERRUPTS__; \ |
|
293 |
clearerr(f); \ |
|
294 |
continue; \ |
|
295 |
} \ |
|
296 |
} else { \ |
|
297 |
(ret) = 0; \ |
|
298 |
} \ |
|
299 |
break; \ |
|
300 |
} \ |
|
301 |
DEBUGBUFFER(buf); \ |
|
302 |
(buf)[__offs++] = (ret); \ |
|
303 |
} \ |
|
304 |
if (__offs > 0) \ |
|
305 |
(ret) = __offs; \ |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
306 |
} else { \ |
3681 | 307 |
int __offs = 0; \ |
308 |
int fd = fileno(f); \ |
|
309 |
\ |
|
310 |
while (__offs < (cnt)) { \ |
|
311 |
OBJ rA = __INST(readAhead); \ |
|
312 |
if (rA != nil) { \ |
|
313 |
DEBUGBUFFER(buf); \ |
|
314 |
(buf)[__offs] = __intVal(rA); \ |
|
315 |
__INST(readAhead) = nil; \ |
|
316 |
(ret) = 1; \ |
|
317 |
} else { \ |
|
318 |
CLEAR_ERRNO; \ |
|
319 |
(ret) = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
320 |
DEBUGBUFFER(buf); \ |
|
321 |
if ((ret) <= 0) { \ |
|
322 |
if ((ret) < 0 && errno == EINTR) { \ |
|
323 |
__HANDLE_INTERRUPTS__; \ |
|
324 |
continue; \ |
|
325 |
} \ |
|
326 |
break; \ |
|
327 |
} \ |
|
328 |
} \ |
|
329 |
__offs += (ret); \ |
|
330 |
} \ |
|
331 |
if (__offs > 0) \ |
|
332 |
(ret) = __offs; \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
333 |
} |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
334 |
|
3554
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
335 |
#if defined(F_GETFL) && defined(F_SETFL) && defined(FNDELAY) |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
336 |
# define GETFLAGS(fd) \ |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
337 |
fcntl(fd, F_GETFL, 0); |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
338 |
|
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
339 |
# define SETFLAGS(fd, flags) \ |
3681 | 340 |
fcntl(fd, F_SETFL, flags) |
3554
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
341 |
|
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
342 |
# define SETNONBLOCKING(fd) \ |
3681 | 343 |
{ \ |
344 |
int flags; \ |
|
345 |
flags = fcntl(fd, F_GETFL, 0); \ |
|
346 |
if (flags >= 0) { \ |
|
347 |
fcntl(fd, F_SETFL, flags | FNDELAY); \ |
|
348 |
} \ |
|
349 |
} |
|
3554
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
350 |
#else |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
351 |
# define GETFLAGS(fd) 0 |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
352 |
# define SETFLAGS(fd, flags) /* nothing */ |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
353 |
# define SETNONBLOCKING(fd) /* nothing */ |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
354 |
#endif |
320cbfef9382
made nextAvail really unblocking
Claus Gittinger <cg@exept.de>
parents:
3551
diff
changeset
|
355 |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
356 |
#define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered) \ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
357 |
{ \ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
358 |
int __offs = 0; \ |
3681 | 359 |
int oldFlags; \ |
360 |
\ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
361 |
(ret) = 0; \ |
3681 | 362 |
oldFlags = GETFLAGS(fileno(f)); \ |
363 |
SETNONBLOCKING(fileno(f)); \ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
364 |
if (isBuffered) { \ |
3681 | 365 |
while (__offs < (cnt)) { \ |
366 |
CLEAR_ERRNO; \ |
|
367 |
(ret) = getc(f); \ |
|
368 |
if ((ret) < 0) { \ |
|
369 |
if (ferror(f)) { \ |
|
370 |
if (errno == EINTR) { \ |
|
371 |
clearerr(f); \ |
|
372 |
break; \ |
|
373 |
} \ |
|
374 |
} else { \ |
|
375 |
(ret) = 0; \ |
|
376 |
} \ |
|
377 |
break; \ |
|
378 |
} \ |
|
379 |
(buf)[__offs++] = (ret); \ |
|
380 |
DEBUGBUFFER(buf); \ |
|
381 |
} \ |
|
382 |
if (__offs > 0) \ |
|
383 |
(ret) = __offs; \ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
384 |
} else { \ |
3681 | 385 |
int fd = fileno(f); \ |
386 |
\ |
|
387 |
while (__offs < (cnt)) { \ |
|
388 |
OBJ rA = __INST(readAhead); \ |
|
389 |
if (rA != nil) { \ |
|
390 |
(buf)[__offs] = __intVal(rA); \ |
|
391 |
DEBUGBUFFER(buf); \ |
|
392 |
__INST(readAhead) = nil; \ |
|
393 |
(ret) = 1; \ |
|
394 |
__offs += (ret); \ |
|
395 |
continue; \ |
|
396 |
} \ |
|
397 |
CLEAR_ERRNO; \ |
|
398 |
(ret) = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
399 |
DEBUGBUFFER(buf); \ |
|
400 |
if ((ret) < 0) { \ |
|
401 |
if (errno == EINTR) { \ |
|
402 |
break; \ |
|
403 |
} \ |
|
404 |
break; \ |
|
405 |
} \ |
|
406 |
__offs += (ret); \ |
|
407 |
break; \ |
|
408 |
} \ |
|
409 |
if (__offs > 0) \ |
|
410 |
(ret) = __offs; \ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
411 |
} \ |
3681 | 412 |
SETFLAGS(fileno(f), oldFlags); \ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
413 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
414 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
415 |
#endif |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
416 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
417 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
418 |
* read_bytes into an object |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
419 |
* (which may be moved by GC) |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
420 |
*/ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
421 |
#ifdef NO_STDIO |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
422 |
#define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
423 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
424 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
425 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
426 |
char *buf = (char *)(obj); \ |
3681 | 427 |
\ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
428 |
(ret) = 0; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
429 |
{ \ |
3681 | 430 |
while (__offs < (cnt)) { \ |
431 |
OBJ rA = __INST(readAhead); \ |
|
432 |
if (rA != nil) { \ |
|
433 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
434 |
DEBUGBUFFER(buf); \ |
|
435 |
__INST(readAhead) = nil; \ |
|
436 |
(ret) = 1; \ |
|
437 |
} else { \ |
|
438 |
CLEAR_ERRNO; \ |
|
439 |
(ret) = READ(f, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
440 |
DEBUGBUFFER(buf); \ |
|
441 |
if ((ret) <= 0) { \ |
|
442 |
if ((ret) < 0 && errno == EINTR) { \ |
|
443 |
__HANDLE_INTERRUPTS__; \ |
|
444 |
/* refetch */ \ |
|
445 |
buf = (char *)(obj); \ |
|
446 |
continue; \ |
|
447 |
} \ |
|
448 |
break; \ |
|
449 |
} \ |
|
450 |
} \ |
|
451 |
__offs += (ret); \ |
|
452 |
} \ |
|
453 |
if (__offs > 0) \ |
|
454 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
455 |
} \ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
456 |
} |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
457 |
#else |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
458 |
#define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
459 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
460 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
461 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
462 |
char *buf = (char *)(obj); \ |
3681 | 463 |
\ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
464 |
(ret) = 0; \ |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
465 |
if (isBuffered) { \ |
3681 | 466 |
while (__offs < (cnt)) { \ |
467 |
CLEAR_ERRNO; \ |
|
468 |
(ret) = getc(f); \ |
|
469 |
if ((ret) < 0) { \ |
|
470 |
if (ferror(f)) { \ |
|
471 |
if (errno == EINTR) { \ |
|
472 |
__HANDLE_INTERRUPTS__; \ |
|
473 |
clearerr(f); \ |
|
474 |
/* refetch */ \ |
|
475 |
buf = (char *)(obj); \ |
|
476 |
DEBUGBUFFER(buf); \ |
|
477 |
continue; \ |
|
478 |
} \ |
|
479 |
} else { \ |
|
480 |
(ret) = 0; \ |
|
481 |
} \ |
|
482 |
break; \ |
|
483 |
} \ |
|
484 |
(buf)[__ooffs+__offs] = (ret); \ |
|
485 |
DEBUGBUFFER(buf); \ |
|
486 |
__offs++; \ |
|
487 |
} \ |
|
488 |
if (__offs > 0) \ |
|
489 |
(ret) = __offs; \ |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
490 |
} else { \ |
3681 | 491 |
int fd = fileno(f); \ |
492 |
\ |
|
493 |
while (__offs < (cnt)) { \ |
|
494 |
OBJ rA = __INST(readAhead); \ |
|
495 |
if (rA != nil) { \ |
|
496 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
497 |
DEBUGBUFFER(buf); \ |
|
498 |
__INST(readAhead) = nil; \ |
|
499 |
(ret) = 1; \ |
|
500 |
} else { \ |
|
501 |
CLEAR_ERRNO; \ |
|
502 |
(ret) = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
503 |
DEBUGBUFFER(buf); \ |
|
504 |
if ((ret) <= 0) { \ |
|
505 |
if ((ret) < 0 && errno == EINTR) { \ |
|
506 |
__HANDLE_INTERRUPTS__; \ |
|
507 |
/* refetch */ \ |
|
508 |
buf = (char *)(obj); \ |
|
509 |
continue; \ |
|
510 |
} \ |
|
511 |
break; \ |
|
512 |
} \ |
|
513 |
} \ |
|
514 |
__offs += (ret); \ |
|
515 |
} \ |
|
516 |
if (__offs > 0) \ |
|
517 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
518 |
} \ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
519 |
} |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
520 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
521 |
#define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
522 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
523 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
524 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
525 |
char *buf = (char *)(obj); \ |
3681 | 526 |
\ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
527 |
(ret) = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
528 |
if (isBuffered) { \ |
3681 | 529 |
while (__offs < (cnt)) { \ |
530 |
CLEAR_ERRNO; \ |
|
531 |
(ret) = getc(f); \ |
|
532 |
if ((ret) < 0) { \ |
|
533 |
if (ferror(f)) { \ |
|
534 |
if (errno == EINTR) { \ |
|
535 |
clearerr(f); \ |
|
536 |
/* refetch */ \ |
|
537 |
buf = (char *)(obj); \ |
|
538 |
break; \ |
|
539 |
} \ |
|
540 |
} else { \ |
|
541 |
(ret) = 0; \ |
|
542 |
} \ |
|
543 |
break; \ |
|
544 |
} \ |
|
545 |
(buf)[__ooffs+__offs] = (ret); \ |
|
546 |
DEBUGBUFFER(buf); \ |
|
547 |
__offs++; \ |
|
548 |
} \ |
|
549 |
if (__offs > 0) \ |
|
550 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
551 |
} else { \ |
3681 | 552 |
int fd = fileno(f); \ |
553 |
\ |
|
554 |
while (__offs < (cnt)) { \ |
|
555 |
OBJ rA = __INST(readAhead); \ |
|
556 |
if (rA != nil) { \ |
|
557 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
558 |
DEBUGBUFFER(buf); \ |
|
559 |
__INST(readAhead) = nil; \ |
|
560 |
(ret) = 1; \ |
|
561 |
__offs += (ret); \ |
|
562 |
continue; \ |
|
563 |
} \ |
|
564 |
CLEAR_ERRNO; \ |
|
565 |
(ret) = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
566 |
DEBUGBUFFER(buf); \ |
|
567 |
if ((ret) < 0 && errno == EINTR) { \ |
|
568 |
/* refetch */ \ |
|
569 |
buf = (char *)(obj); \ |
|
570 |
break; \ |
|
571 |
} \ |
|
572 |
__offs += (ret); \ |
|
573 |
break; \ |
|
574 |
} \ |
|
575 |
if (__offs > 0) \ |
|
576 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
577 |
} \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
578 |
} |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
579 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
580 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
581 |
#endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
582 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
583 |
#ifdef NO_STDIO |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
584 |
#define __WRITEBYTE__(ret, f, buf, isBuffered) \ |
3681 | 585 |
for (;;) { \ |
586 |
CLEAR_ERRNO; \ |
|
587 |
(ret) = WRITE(f, buf, 1); \ |
|
588 |
if ((ret) >= 0 || errno != EINTR) \ |
|
589 |
break; \ |
|
590 |
__HANDLE_INTERRUPTS__; \ |
|
591 |
} |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
592 |
#else |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
593 |
#define __WRITEBYTE__(ret, f, buf, isBuffered) \ |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
594 |
if (isBuffered) { \ |
3681 | 595 |
for (;;) { \ |
596 |
CLEAR_ERRNO; \ |
|
597 |
ret = putc(*(buf), f); \ |
|
598 |
if ((ret) >= 0) { \ |
|
599 |
(ret) = 1; \ |
|
600 |
} else if (ferror(f)) { \ |
|
601 |
if (errno == EINTR) { \ |
|
602 |
__HANDLE_INTERRUPTS__; \ |
|
603 |
clearerr(f); \ |
|
604 |
continue; \ |
|
605 |
} \ |
|
606 |
} else \ |
|
607 |
(ret) = 0; \ |
|
608 |
break; \ |
|
609 |
} \ |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
610 |
} else { \ |
3681 | 611 |
for (;;) { \ |
612 |
CLEAR_ERRNO; \ |
|
613 |
(ret) = write(fileno(f), buf, 1); \ |
|
614 |
if ((ret) >= 0 || errno != EINTR) \ |
|
615 |
break; \ |
|
616 |
__HANDLE_INTERRUPTS__; \ |
|
617 |
} \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
618 |
} |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
619 |
#endif |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
620 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
621 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
622 |
* write_bytes from a c-buffer |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
623 |
* (which may NOT move) |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
624 |
*/ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
625 |
#ifdef NO_STDIO |
2961 | 626 |
#define __WRITEBYTES__(ret, f, buf, cnt, isBuffered) \ |
627 |
(ret) = 0; \ |
|
628 |
{ \ |
|
3681 | 629 |
int __offs = 0; \ |
630 |
while (__offs < (cnt)) { \ |
|
631 |
CLEAR_ERRNO; \ |
|
632 |
ret = WRITE(f, (buf)+__offs, (cnt)-__offs); \ |
|
633 |
if (ret <= 0) { \ |
|
634 |
if (ret < 0 && errno == EINTR) { \ |
|
635 |
__HANDLE_INTERRUPTS__; \ |
|
636 |
continue; \ |
|
637 |
} \ |
|
638 |
break; \ |
|
639 |
} \ |
|
640 |
__offs += (ret); \ |
|
641 |
} \ |
|
642 |
if (__offs > 0) \ |
|
643 |
(ret) = __offs; \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
644 |
} |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
645 |
#else |
2961 | 646 |
#define __WRITEBYTES__(ret, f, buf, cnt, isBuffered) \ |
647 |
(ret) = 0; \ |
|
648 |
if (isBuffered) { \ |
|
3681 | 649 |
int __offs = 0; \ |
650 |
while (__offs < (cnt)) { \ |
|
651 |
CLEAR_ERRNO; \ |
|
652 |
ret = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\ |
|
653 |
if ((ret) <= 0) { \ |
|
654 |
if (ferror(f)) { \ |
|
655 |
if (errno == EINTR) { \ |
|
656 |
__HANDLE_INTERRUPTS__; \ |
|
657 |
clearerr(f); \ |
|
658 |
continue; \ |
|
659 |
} \ |
|
660 |
break; \ |
|
661 |
} else { \ |
|
662 |
(ret) = 0; \ |
|
663 |
} \ |
|
664 |
} \ |
|
665 |
__offs += (ret); \ |
|
666 |
} \ |
|
667 |
if (__offs > 0) \ |
|
668 |
(ret) = __offs; \ |
|
2961 | 669 |
} else { \ |
3681 | 670 |
int __offs = 0; \ |
671 |
while (__offs < (cnt)) { \ |
|
672 |
CLEAR_ERRNO; \ |
|
673 |
ret = write(fileno(f), (buf)+__offs, (cnt)-__offs);\ |
|
674 |
if (ret <= 0) { \ |
|
675 |
if (ret < 0 && errno == EINTR) { \ |
|
676 |
__HANDLE_INTERRUPTS__; \ |
|
677 |
continue; \ |
|
678 |
} \ |
|
679 |
break; \ |
|
680 |
} \ |
|
681 |
__offs += (ret); \ |
|
682 |
} \ |
|
683 |
if (__offs > 0) \ |
|
684 |
(ret) = __offs; \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
685 |
} |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
686 |
#endif |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
687 |
|
223 | 688 |
/* |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
689 |
* write_bytes from an object |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
690 |
* (which may be moved around by GC) |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
691 |
*/ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
692 |
#ifdef NO_STDIO |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
693 |
#define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
694 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
695 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
696 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
697 |
char *buf = (char *)(obj); \ |
3681 | 698 |
\ |
2961 | 699 |
(ret) = 0; \ |
700 |
{ \ |
|
3681 | 701 |
while (__offs < (cnt)) { \ |
702 |
CLEAR_ERRNO; \ |
|
703 |
ret = WRITE(f, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
704 |
if (ret <= 0) { \ |
|
705 |
if (ret < 0 && errno == EINTR) { \ |
|
706 |
__HANDLE_INTERRUPTS__; \ |
|
707 |
/* refetch */ \ |
|
708 |
buf = (char *)(obj); \ |
|
709 |
continue; \ |
|
710 |
} \ |
|
711 |
break; \ |
|
712 |
} \ |
|
713 |
__offs += (ret); \ |
|
714 |
} \ |
|
715 |
if (__offs > 0) \ |
|
716 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
717 |
} \ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
718 |
} |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
719 |
#else |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
720 |
#define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
721 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
722 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
723 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
724 |
char *buf = (char *)(obj); \ |
3681 | 725 |
\ |
2961 | 726 |
(ret) = 0; \ |
727 |
if (isBuffered) { \ |
|
3681 | 728 |
while (__offs < (cnt)) { \ |
729 |
CLEAR_ERRNO; \ |
|
730 |
ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \ |
|
731 |
if ((ret) <= 0) { \ |
|
732 |
if (ferror(f)) { \ |
|
733 |
if (errno == EINTR) { \ |
|
734 |
__HANDLE_INTERRUPTS__; \ |
|
735 |
/* refetch */ \ |
|
736 |
buf = (char *)(obj); \ |
|
737 |
clearerr(f); \ |
|
738 |
continue; \ |
|
739 |
} \ |
|
740 |
break; \ |
|
741 |
} else { \ |
|
742 |
(ret) = 0; \ |
|
743 |
} \ |
|
744 |
} \ |
|
745 |
__offs += (ret); \ |
|
746 |
} \ |
|
747 |
if (__offs > 0) \ |
|
748 |
(ret) = __offs; \ |
|
2961 | 749 |
} else { \ |
3681 | 750 |
while (__offs < (cnt)) { \ |
751 |
CLEAR_ERRNO; \ |
|
752 |
ret = write(fileno(f), (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
753 |
if (ret <= 0) { \ |
|
754 |
if (ret < 0 && errno == EINTR) { \ |
|
755 |
__HANDLE_INTERRUPTS__; \ |
|
756 |
/* refetch */ \ |
|
757 |
buf = (char *)(obj); \ |
|
758 |
continue; \ |
|
759 |
} \ |
|
760 |
break; \ |
|
761 |
} \ |
|
762 |
__offs += (ret); \ |
|
763 |
} \ |
|
764 |
if (__offs > 0) \ |
|
765 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
766 |
} \ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
767 |
} |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
768 |
#endif |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
769 |
|
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
770 |
/* |
223 | 771 |
* on some systems errno is a macro ... check for it here |
772 |
*/ |
|
773 |
#ifndef errno |
|
774 |
extern errno; |
|
775 |
#endif |
|
776 |
||
1 | 777 |
%} |
188 | 778 |
! ! |
1 | 779 |
|
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
780 |
!ExternalStream class methodsFor:'documentation'! |
22 | 781 |
|
88 | 782 |
copyright |
783 |
" |
|
784 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
2264 | 785 |
All Rights Reserved |
88 | 786 |
|
787 |
This software is furnished under a license and may be used |
|
788 |
only in accordance with the terms of that license and with the |
|
789 |
inclusion of the above copyright notice. This software may not |
|
790 |
be provided or otherwise made available to, or used by, any |
|
791 |
other person. No title to or ownership of the software is |
|
792 |
hereby transferred. |
|
793 |
" |
|
794 |
! |
|
795 |
||
22 | 796 |
documentation |
797 |
" |
|
68 | 798 |
ExternalStream defines protocol common to Streams which have a file-descriptor and |
799 |
represent some file or communicationChannel of the underlying OperatingSystem. |
|
800 |
ExternalStream is abstract; concrete classes are FileStream, PipeStream etc. |
|
77 | 801 |
|
159 | 802 |
ExternalStreams can be in two modes: text- (the default) and binary-mode. |
68 | 803 |
In text-mode, the elements read/written are characters; |
804 |
while in binary-mode the basic elements are bytes which read/write as SmallIntegers |
|
805 |
in the range 0..255. |
|
22 | 806 |
|
68 | 807 |
Also, the stream can be either in buffered or unbuffered mode. In buffered mode, |
808 |
data is not written until either a cr is written (in text mode) or a synchronizeOutput |
|
809 |
is sent (in both modes). |
|
22 | 810 |
|
77 | 811 |
The underlying OperatingSystem streams may either be closed explicitely (sending a close) |
68 | 812 |
or just forgotten - in this case, the garbage collector will eventually collect the |
77 | 813 |
object AND a close will be performed automatically (but you will NOT know when this |
814 |
happens - so it is recommended, that you close your files when no longer needed). |
|
159 | 815 |
Closing is also suggested, since if smalltalk is finished (be it by purpose, or due to |
77 | 816 |
some crash) the data will not be in the file, if unclosed. |
817 |
All streams understand the close message, so it never hurts to use it (it is defined as |
|
818 |
a noop in one of the superclasses). |
|
22 | 819 |
|
68 | 820 |
Most of the methods found here redefine inherited methods for better performance, |
821 |
since I/O from/to files should be fast. |
|
22 | 822 |
|
68 | 823 |
Recovering a snapshot: |
1295 | 824 |
not all streams can be restored to the state they had before - see the implementation of |
825 |
reOpen in subclasses for more information. |
|
826 |
For streams sitting on some communication channel (i.e. Pipes and Sockets) you should |
|
827 |
reestablish the stream upon image restart (make someone dependent on ObjectMemory). |
|
828 |
FileStreams are reopened and positioned to their offset they had at snapshot time. |
|
829 |
This may fail, if the file was removed or renamed - or lead to confusion |
|
830 |
if the contents changed in the meantime. |
|
831 |
Therefore, it is a good idea to reopen files and check for these things at restart time. |
|
832 |
||
833 |
[Instance variables:] |
|
834 |
||
2264 | 835 |
filePointer <Integer> the unix FILE*; somehow mapped to an integer |
836 |
(notice: not the fd) |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
837 |
on Windows: the fileHandle |
2264 | 838 |
mode <Symbol> #readwrite, #readonly or #writeonly |
839 |
buffered <Boolean> true, if buffered (i.e. collects characters - does |
|
840 |
not output immediately) |
|
841 |
binary <Boolean> true if in binary mode (reads bytes instead of chars) |
|
2920 | 842 |
eolMode <Symbol> one of nil, #cr or #crlf. |
843 |
determines how lines should be terminated. |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
844 |
nil -> newLine (as in Unix); |
2920 | 845 |
#crlf -> with cr-lf (as in MSDOS) |
846 |
#cr -> with cr (as in VMS) |
|
2264 | 847 |
hitEOF <Boolean> true, if EOF was reached |
848 |
||
849 |
lastErrorNumber <Integer> the value of errno (only valid right after the error - |
|
850 |
updated with next i/o operation) |
|
1295 | 851 |
|
852 |
[Class variables:] |
|
2264 | 853 |
Lobby <Registry> keeps track of used ext-streams (to free up FILE*'s) |
854 |
||
855 |
StreamErrorSignal <Signal> parent of all stream errors (see Stream class) |
|
856 |
InvalidReadSignal <Signal> raised on read from writeonly stream |
|
857 |
InvalidWriteSignal <Signal> raised on write to readonly stream |
|
858 |
InvalidModeSignal <Signal> raised on text I/O with binary-stream |
|
859 |
or binary I/O with text-stream |
|
860 |
OpenErrorSignal <Signal> raised if open fails |
|
861 |
StreamNotOpenSignal <Signal> raised on I/O with non-open stream |
|
159 | 862 |
|
863 |
Additional notes: |
|
864 |
This class is implemented using the underlying stdio-c library package, which |
|
865 |
has both advantages and disadvantages: since it is portable (posix defined), porting |
|
866 |
ST/X to non-Unix machines is simplified. The disadvantage is that the stdio library |
|
867 |
has big problems handling unbounded Streams, since the EOF handling in stdio is |
|
868 |
not prepared for data to arrive after EOF has been reached - time will show, if we need |
|
869 |
a complete rewrite for UnboundedStream ... |
|
325 | 870 |
|
188 | 871 |
Also, depending on the system, the stdio library behaves infriendly when signals |
872 |
occur while reading (for example, timer interrupts) - on real unixes (i.e. BSD) the signal |
|
873 |
is handled transparently - on SYS5.3 (i.e. non unixes :-) the read operation returns |
|
1295 | 874 |
an error and errno is set to EINTR. |
875 |
Thats what the ugly code around all getc-calls is for ... |
|
876 |
Since things get more and more ugly - we will rewrite ExternalStream |
|
877 |
completely, to NOT use any stdio stuff (and do its buffering itself). |
|
325 | 878 |
|
879 |
Notice that typical stdio's use a single errno global variable to return an error code, |
|
880 |
this was bad design in the stdio lib (right from the very beginning), since its much |
|
881 |
harder to deal with this in the presence of lightweight processes, where errno gets |
|
882 |
overwritten by an I/O operation done in another thread. (stdio should have been written |
|
883 |
to return errno as a negative number ...). |
|
884 |
To deal with this, the scheduler treats errno like a per-thread private variable, |
|
885 |
and saves/restores the errno setting when switching to another thread. |
|
886 |
(Notice that some thread packages do this also, but ST/X's thread implementation |
|
887 |
does not depend on those, but instead uses a portable private package). |
|
888 |
||
216 | 889 |
Finally, if an stdio-stream is open for both reading and writing, we have to call |
890 |
fseek whenever we are about to read after write and vice versa. |
|
891 |
Two macros (__READING__ and __WRITING__) have been defined to be used before every |
|
892 |
fread/fgetc and fwrite/putc respectively. |
|
1295 | 893 |
|
894 |
[author:] |
|
2264 | 895 |
Claus Gittinger |
896 |
Stefan Vogel (many, many fixes ...) |
|
1295 | 897 |
|
898 |
[see also:] |
|
2264 | 899 |
FileStream Socket PipeStream |
900 |
Filename OperatingSystem |
|
22 | 901 |
" |
3255 | 902 |
! |
903 |
||
904 |
examples |
|
905 |
" |
|
906 |
open a file, read the contents and display it in a textView: |
|
907 |
||
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
908 |
|topView scrollPane textView fileStream text| |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
909 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
910 |
topView := StandardSystemView new. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
911 |
topView label:'contents of Makefile'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
912 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
913 |
scrollPane := HVScrollableView in:topView. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
914 |
scrollPane origin:0.0@0.0 corner:1.0@1.0. |
3255 | 915 |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
916 |
textView := EditTextView new. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
917 |
scrollPane scrolledView:textView. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
918 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
919 |
fileStream := 'Makefile' asFilename readStream. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
920 |
text := fileStream upToEnd. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
921 |
fileStream close. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
922 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
923 |
textView contents:text. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
924 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
925 |
topView open. |
3255 | 926 |
|
927 |
||
928 |
||
929 |
Notice, all of the above can also be done (simply) as: |
|
930 |
||
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
931 |
EditTextView openOn:'Makefile' |
3255 | 932 |
" |
22 | 933 |
! ! |
934 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
935 |
!ExternalStream class methodsFor:'initialization'! |
1 | 936 |
|
2920 | 937 |
initDefaultEOLMode |
938 |
OperatingSystem isUNIXlike ifTrue:[ |
|
939 |
"/ unix EOL conventions |
|
940 |
DefaultEOLMode := #nl |
|
941 |
] ifFalse:[ |
|
942 |
OperatingSystem isVMSlike ifTrue:[ |
|
943 |
"/ vms EOL conventions |
|
944 |
DefaultEOLMode := #cr |
|
945 |
] ifFalse:[ |
|
946 |
"/ msdos EOL conventions |
|
947 |
DefaultEOLMode := #crlf |
|
948 |
] |
|
949 |
] |
|
950 |
! |
|
951 |
||
952 |
initModeStrings |
|
953 |
"initialize modeStrings which are passed down to the underlying |
|
954 |
fopen/fdopen functions." |
|
955 |
||
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
956 |
OperatingSystem isMSDOSlike ifTrue:[ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
957 |
ReadMode := 'rb'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
958 |
ReadWriteMode := 'rb+'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
959 |
WriteMode := 'wb'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
960 |
AppendMode := 'ab+'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
961 |
CreateReadWriteMode := 'wb+'. |
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
962 |
] ifFalse:[ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
963 |
ReadMode := 'r'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
964 |
ReadWriteMode := 'r+'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
965 |
WriteMode := 'w'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
966 |
AppendMode := 'a+'. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
967 |
CreateReadWriteMode := 'w+'. |
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
968 |
] |
2920 | 969 |
! |
970 |
||
1 | 971 |
initialize |
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
972 |
OpenErrorSignal isNil ifTrue:[ |
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
973 |
OpenErrorSignal := StreamErrorSignal newSignalMayProceed:true. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
974 |
OpenErrorSignal nameClass:self message:#openErrorSignal. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
975 |
OpenErrorSignal notifierString:'open error'. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
976 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
977 |
InvalidReadSignal := ReadErrorSignal newSignalMayProceed:false. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
978 |
InvalidReadSignal nameClass:self message:#invalidReadSignal. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
979 |
InvalidReadSignal notifierString:'read error'. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
980 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
981 |
InvalidWriteSignal := WriteErrorSignal newSignalMayProceed:false. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
982 |
InvalidWriteSignal nameClass:self message:#invalidWriteSignal. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
983 |
InvalidWriteSignal notifierString:'write error'. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
984 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
985 |
InvalidModeSignal := StreamErrorSignal newSignalMayProceed:false. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
986 |
InvalidModeSignal nameClass:self message:#invalidModeSignal. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
987 |
InvalidModeSignal notifierString:'binary/text mode mismatch'. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
988 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
989 |
InvalidOperationSignal := StreamErrorSignal newSignalMayProceed:false. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
990 |
InvalidOperationSignal nameClass:self message:#invalidOperationSignal. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
991 |
InvalidOperationSignal notifierString:'unsupported file operation'. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
992 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
993 |
StreamNotOpenSignal := StreamErrorSignal newSignalMayProceed:false. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
994 |
StreamNotOpenSignal nameClass:self message:#streamNotOpenSignal. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
995 |
StreamNotOpenSignal notifierString:'stream is not open'. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
996 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
997 |
StreamIOErrorSignal := StreamErrorSignal newSignalMayProceed:false. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
998 |
StreamIOErrorSignal nameClass:self message:#streamIOErrorSignal. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
999 |
StreamIOErrorSignal notifierString:'I/O error'. |
159 | 1000 |
]. |
1001 |
||
31 | 1002 |
Lobby isNil ifTrue:[ |
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1003 |
Lobby := Registry new. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1004 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1005 |
"want to get informed when returning from snapshot" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1006 |
ObjectMemory addDependent:self |
2920 | 1007 |
]. |
1008 |
self initDefaultEOLMode. |
|
1009 |
self initModeStrings. |
|
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1010 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1011 |
"Modified: / 21.5.1998 / 16:33:53 / cg" |
1 | 1012 |
! |
1013 |
||
1014 |
reOpenFiles |
|
1015 |
"reopen all files (if possible) after a snapShot load" |
|
1016 |
||
301 | 1017 |
Lobby do:[:aFileStream | |
2264 | 1018 |
aFileStream reOpen |
2920 | 1019 |
]. |
1 | 1020 |
! |
1021 |
||
1469 | 1022 |
update:something with:aParameter from:changedObject |
1 | 1023 |
"have to reopen files when returning from snapshot" |
1024 |
||
1025 |
something == #returnFromSnapshot ifTrue:[ |
|
2920 | 1026 |
self reOpenFiles. |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
1027 |
self initDefaultEOLMode |
1 | 1028 |
] |
1469 | 1029 |
|
1030 |
"Created: 15.6.1996 / 15:19:59 / cg" |
|
1 | 1031 |
! ! |
1032 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1033 |
!ExternalStream class methodsFor:'instance creation'! |
612 | 1034 |
|
2920 | 1035 |
forFileDescriptor:aFileDescriptor mode:modeString |
1036 |
"given a fileDescriptor, create an ExternalStream object |
|
1037 |
to operate on this fd. |
|
1038 |
The mode-argument is passed to the fdopen call. |
|
1039 |
This may be used to wrap fd's as returned by user |
|
1040 |
primitive code, or to wrap pipe-fds into externalStreams." |
|
1041 |
||
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1042 |
|newStream| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1043 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1044 |
newStream := self basicNew. |
2920 | 1045 |
newStream text; buffered:true; eolMode:DefaultEOLMode; clearEOF. |
3535 | 1046 |
^ newStream connectTo:aFileDescriptor withMode:modeString |
2920 | 1047 |
|
1048 |
" |
|
1049 |
the example below will probably fail (15 is a random FD): |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1050 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1051 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1052 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1053 |
s := ExternalStream forFileDescriptor:15 mode:'r'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1054 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1055 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1056 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1057 |
"Created: 29.2.1996 / 18:05:00 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1058 |
"Modified: 29.2.1996 / 18:17:07 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1059 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1060 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1061 |
forReadWriteToFileDescriptor:aFileDescriptor |
2920 | 1062 |
"given a fileDescriptor, create an ExternalStream object |
1063 |
to read/write from/to this fd. This may be used to wrap fd's |
|
1064 |
as returned by user primitive code, or to wrap pipe- |
|
1065 |
filedescriptors into externalStreams." |
|
1066 |
||
3535 | 1067 |
^ (self forFileDescriptor:aFileDescriptor mode:ReadWriteMode) readwrite |
2920 | 1068 |
|
1069 |
" |
|
1070 |
the example below will probably fail (15 is a random FD): |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1071 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1072 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1073 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1074 |
s := ExternalStream forReadWriteToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1075 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1076 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1077 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1078 |
"Created: 29.2.1996 / 18:15:08 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1079 |
"Modified: 29.2.1996 / 18:16:25 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1080 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1081 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1082 |
forReadingFromFileDescriptor:aFileDescriptor |
2920 | 1083 |
"given a fileDescriptor, create an ExternalStream object |
1084 |
to read from this fd. This may be used to wrap fd's |
|
1085 |
as returned by user primitive code, or to wrap pipe- |
|
1086 |
filedescriptors into externalStreams." |
|
1087 |
||
3535 | 1088 |
^ (self forFileDescriptor:aFileDescriptor mode:ReadMode) readonly |
2920 | 1089 |
|
1090 |
" |
|
1091 |
the example below will probably fail (15 is a random FD): |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1092 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1093 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1094 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1095 |
s := ExternalStream forReadingFromFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1096 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1097 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1098 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1099 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1100 |
|pipe readFd writeFd rs ws| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1101 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1102 |
'create OS pipe ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1103 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1104 |
pipe := OperatingSystem makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1105 |
readFd := pipe at:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1106 |
writeFd := pipe at:2. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1107 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1108 |
'connect Smalltalk streams ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1109 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1110 |
rs := ExternalStream forReadingFromFileDescriptor:readFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1111 |
ws := ExternalStream forWritingToFileDescriptor:writeFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1112 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1113 |
'read ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1114 |
[ |
3535 | 1115 |
1 to:10 do:[:i | |
1116 |
Transcript showCR:rs nextLine |
|
1117 |
]. |
|
1118 |
rs close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1119 |
] forkAt:7. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1120 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1121 |
'write ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1122 |
[ |
3535 | 1123 |
1 to:10 do:[:i | |
1124 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1125 |
]. |
|
1126 |
ws close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1127 |
] fork. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1128 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1129 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1130 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1131 |
"Created: 29.2.1996 / 18:14:24 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1132 |
"Modified: 29.2.1996 / 18:25:02 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1133 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1134 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1135 |
forWritingToFileDescriptor:aFileDescriptor |
2920 | 1136 |
"given a fileDescriptor, create an ExternalStream object |
1137 |
to write to this fd. This may be used to wrap fd's |
|
1138 |
as returned by user primitive code, or to wrap pipe- |
|
1139 |
filedescriptors into externalStreams." |
|
1140 |
||
3535 | 1141 |
^ (self forFileDescriptor:aFileDescriptor mode:WriteMode) writeonly |
2920 | 1142 |
|
1143 |
" |
|
1144 |
the example below will probably fail (15 is a random FD): |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1145 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1146 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1147 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1148 |
s := ExternalStream forWritingToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1149 |
s binary. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1150 |
s nextPut:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1151 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1152 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1153 |
"Created: 29.2.1996 / 18:14:43 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1154 |
"Modified: 29.2.1996 / 18:15:54 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1155 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1156 |
|
3535 | 1157 |
makePTYPair |
1158 |
"return an array with two streams - the first one is the master, |
|
1159 |
the second the slave of a ptym/ptys pair. |
|
1160 |
This is much like a bidirectional pipe, but allows signals & |
|
1161 |
control chars to be passed through the connection. |
|
1162 |
This is needed to execute a shell in a view. |
|
1163 |
This is the higher level equivalent of OperatingSystem>>makePTYPair |
|
1164 |
(which returns an array of file-descriptors)." |
|
1165 |
||
1166 |
|ptyPair m s| |
|
1167 |
||
1168 |
ptyPair := OperatingSystem makePTYPair. |
|
1169 |
ptyPair notNil ifTrue:[ |
|
1170 |
m := self forReadWriteToFileDescriptor:(ptyPair at:1). |
|
1171 |
s := self forReadWriteToFileDescriptor:(ptyPair at:2). |
|
3536 | 1172 |
ptyPair at:1 put:m. |
1173 |
ptyPair at:2 put:s. |
|
1174 |
^ ptyPair |
|
3535 | 1175 |
]. |
1176 |
^ nil |
|
1177 |
||
1178 |
" |
|
1179 |
ExternalStream makePTYPair. |
|
1180 |
" |
|
1181 |
||
1182 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
|
1183 |
! |
|
1184 |
||
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1185 |
makePipe |
2920 | 1186 |
"return an array with two streams - the first one for reading, |
1187 |
the second for writing. |
|
1188 |
This is the higher level equivalent of OperatingSystem>>makePipe |
|
1189 |
(which returns an array of file-descriptors)." |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1190 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1191 |
|pipe rs ws| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1192 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1193 |
pipe := OperatingSystem makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1194 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1195 |
pipe notNil ifTrue:[ |
2264 | 1196 |
rs := self forReadingFromFileDescriptor:(pipe at:1). |
1197 |
ws := self forWritingToFileDescriptor:(pipe at:2). |
|
1198 |
^ Array with:rs with:ws |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1199 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1200 |
^ nil |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1201 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1202 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1203 |
|pipe rs ws| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1204 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1205 |
pipe := ExternalStream makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1206 |
rs := pipe at:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1207 |
ws := pipe at:2. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1208 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1209 |
'read ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1210 |
[ |
2264 | 1211 |
1 to:10 do:[:i | |
1212 |
Transcript showCR:rs nextLine |
|
1213 |
]. |
|
1214 |
rs close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1215 |
] forkAt:7. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1216 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1217 |
'write ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1218 |
[ |
2264 | 1219 |
1 to:10 do:[:i | |
1220 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1221 |
]. |
|
1222 |
ws close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1223 |
] fork. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1224 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1225 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1226 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1227 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1228 |
|
612 | 1229 |
new |
1230 |
|newStream| |
|
1231 |
||
1232 |
newStream := self basicNew. |
|
2920 | 1233 |
newStream text; buffered:true; eolMode:DefaultEOLMode; clearEOF. |
612 | 1234 |
^ newStream |
1235 |
! ! |
|
1236 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1237 |
!ExternalStream class methodsFor:'Signal constants'! |
159 | 1238 |
|
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1239 |
inaccessibleSignal |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1240 |
"ST-80 compatibility: return openErrorSignal" |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1241 |
|
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1242 |
^ self openErrorSignal |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1243 |
|
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1244 |
"Created: 2.7.1996 / 12:27:16 / stefan" |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1245 |
! |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1246 |
|
612 | 1247 |
invalidModeSignal |
1248 |
"return the signal raised when doing text-I/O with a binary stream |
|
1249 |
or binary-I/O with a text stream" |
|
1250 |
||
1251 |
^ InvalidModeSignal |
|
159 | 1252 |
! |
1253 |
||
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1254 |
invalidOperationSignal |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1255 |
"return the signal raised when an unsupported or invalid |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1256 |
I/O operation is attempted" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1257 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1258 |
^ InvalidOperationSignal |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1259 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1260 |
|
159 | 1261 |
invalidReadSignal |
1262 |
"return the signal raised when reading from writeonly streams" |
|
1263 |
||
1264 |
^ InvalidReadSignal |
|
1265 |
! |
|
1266 |
||
1267 |
invalidWriteSignal |
|
1268 |
"return the signal raised when writing to readonly streams" |
|
1269 |
||
1270 |
^ InvalidWriteSignal |
|
1271 |
! |
|
1272 |
||
612 | 1273 |
openErrorSignal |
1274 |
"return the signal raised when a file open failed" |
|
1275 |
||
1276 |
^ OpenErrorSignal |
|
1277 |
! |
|
1278 |
||
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1279 |
streamIOErrorSignal |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1280 |
"return the signal raised when an I/O error occurs. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1281 |
(for example, a device-IO-error, or reading an NFS-dir, |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1282 |
which is no longer available and has been mounted soft)" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1283 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1284 |
^ StreamIOErrorSignal |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1285 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1286 |
"Created: / 21.5.1998 / 16:32:55 / cg" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1287 |
! |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1288 |
|
159 | 1289 |
streamNotOpenSignal |
1290 |
"return the signal raised on I/O with closed streams" |
|
1291 |
||
1292 |
^ StreamNotOpenSignal |
|
2 | 1293 |
! ! |
1294 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1295 |
!ExternalStream class methodsFor:'error handling'! |
92 | 1296 |
|
1297 |
lastErrorNumber |
|
159 | 1298 |
"return the errno of the last error" |
92 | 1299 |
|
1300 |
^ LastErrorNumber |
|
159 | 1301 |
|
1302 |
" |
|
1303 |
ExternalStream lastErrorNumber |
|
1304 |
" |
|
92 | 1305 |
! |
1306 |
||
1307 |
lastErrorString |
|
1308 |
"return a message string describing the last error" |
|
1309 |
||
1310 |
^ OperatingSystem errorTextForNumber:LastErrorNumber |
|
1311 |
||
1312 |
" |
|
1313 |
ExternalStream lastErrorString |
|
1314 |
" |
|
1315 |
! ! |
|
1316 |
||
3939
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1317 |
!ExternalStream methodsFor:'Signal constants'! |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1318 |
|
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1319 |
invalidReadSignal |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1320 |
^ self class invalidReadSignal |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1321 |
|
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1322 |
"Created: / 3.12.1998 / 15:12:06 / cg" |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1323 |
! |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1324 |
|
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1325 |
invalidWriteSignal |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1326 |
^ self class invalidWriteSignal |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1327 |
|
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1328 |
"Created: / 3.12.1998 / 15:12:10 / cg" |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1329 |
! ! |
52e4ebbde474
oops - missing signal accessors
Claus Gittinger <cg@exept.de>
parents:
3938
diff
changeset
|
1330 |
|
3044 | 1331 |
!ExternalStream methodsFor:'Squeak compatibility'! |
1332 |
||
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1333 |
nextInto:aByteArrayOrString |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1334 |
"read elements into the argument, whose size determines the amount |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1335 |
of bytes to read. If not enough elements could be read, return |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1336 |
a copy of the argument; otherwise, return the filled argument." |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1337 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1338 |
|n nWanted| |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1339 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1340 |
nWanted := aByteArrayOrString size. |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1341 |
n := self nextAvailableBytes:nWanted into:aByteArrayOrString startingAt:1. |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1342 |
n == nWanted ifTrue:[^ aByteArrayOrString]. |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1343 |
^ aByteArrayOrString copyTo:n |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1344 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1345 |
! |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1346 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1347 |
readInto:aByteArrayOrString startingAt:index count:nBytes |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1348 |
"same as #nextBytes:into:startingAt: |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1349 |
Squeak compatibility." |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1350 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1351 |
^ self nextBytes:nBytes into:aByteArrayOrString startingAt:index. |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1352 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1353 |
! |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1354 |
|
3044 | 1355 |
readOnly |
1356 |
"Squeak compatibility: make the stream readOnly" |
|
1357 |
||
1358 |
mode := #readonly |
|
1359 |
||
1360 |
"Modified: 20.10.1997 / 19:23:04 / cg" |
|
1361 |
"Created: 20.10.1997 / 19:23:19 / cg" |
|
1362 |
! ! |
|
1363 |
||
1 | 1364 |
!ExternalStream methodsFor:'accessing'! |
1365 |
||
612 | 1366 |
binary |
1367 |
"switch to binary mode - default is text" |
|
1368 |
||
1369 |
binary := true |
|
1 | 1370 |
! |
1371 |
||
1372 |
buffered:aBoolean |
|
10 | 1373 |
"turn buffering on or off - default is on" |
1 | 1374 |
|
10 | 1375 |
buffered := aBoolean |
1 | 1376 |
! |
1377 |
||
1378 |
contents |
|
2 | 1379 |
"return the contents of the file from the current position up-to |
1380 |
the end. If the stream is in binary mode, a ByteArray containing |
|
1381 |
the byte values is returned. |
|
1382 |
In text-mode, a collection of strings, each representing one line, |
|
1383 |
is returned." |
|
1384 |
||
1385 |
|text l chunks sizes chunk byteCount cnt bytes offset| |
|
1386 |
||
1387 |
binary ifTrue:[ |
|
2264 | 1388 |
"adding to a ByteArray produces quadratic time-space |
1389 |
behavior - therefore we allocate chunks, and concatenate them |
|
1390 |
at the end." |
|
1391 |
||
1392 |
chunks := OrderedCollection new. |
|
1393 |
sizes := OrderedCollection new. |
|
1394 |
byteCount := 0. |
|
1395 |
[self atEnd] whileFalse:[ |
|
1396 |
chunk := ByteArray uninitializedNew:4096. |
|
1397 |
cnt := self nextBytes:(chunk size) into:chunk. |
|
1398 |
cnt notNil ifTrue:[ |
|
1399 |
chunks add:chunk. |
|
1400 |
sizes add:cnt. |
|
1401 |
byteCount := byteCount + cnt |
|
1402 |
] |
|
1403 |
]. |
|
1404 |
||
1405 |
"now, create one big ByteArray" |
|
1406 |
bytes := ByteArray uninitializedNew:byteCount. |
|
1407 |
offset := 1. |
|
1408 |
1 to:chunks size do:[:index | |
|
1409 |
chunk := chunks at:index. |
|
1410 |
cnt := sizes at:index. |
|
1411 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk. |
|
1412 |
offset := offset + cnt |
|
1413 |
]. |
|
1414 |
^ bytes |
|
2 | 1415 |
]. |
1 | 1416 |
|
244 | 1417 |
text := StringCollection new. |
1 | 1418 |
[self atEnd] whileFalse:[ |
2264 | 1419 |
l := self nextLine. |
1420 |
l isNil ifTrue:[ |
|
1421 |
^ text |
|
1422 |
]. |
|
1423 |
text add:l |
|
1 | 1424 |
]. |
1425 |
^ text |
|
1426 |
! |
|
1427 |
||
612 | 1428 |
contentsOfEntireFile |
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1429 |
"ST-80 compatibility: return contents as a String (or byteArray, if in binary mode). |
3876 | 1430 |
See also #contents, which returns the lines as stringCollection for text files." |
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1431 |
|
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1432 |
|data| |
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1433 |
|
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1434 |
data := self contents. |
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1435 |
binary ifFalse:[ |
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1436 |
^ data asString. |
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1437 |
]. |
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1438 |
^ data |
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1439 |
|
3876 | 1440 |
"Modified: / 3.7.1996 / 13:22:16 / stefan" |
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
1441 |
"Modified: / 27.11.1998 / 16:29:43 / cg" |
2 | 1442 |
! |
1443 |
||
612 | 1444 |
contentsSpecies |
1445 |
"return the kind of object to be returned by sub-collection builders |
|
1446 |
(such as upTo)" |
|
159 | 1447 |
|
1448 |
binary ifTrue:[ |
|
2264 | 1449 |
^ ByteArray |
159 | 1450 |
]. |
612 | 1451 |
^ String |
2 | 1452 |
! |
1453 |
||
2920 | 1454 |
eolMode:aSymbolOrNil |
1455 |
"specify how end-of-line (EOL) is to be marked. |
|
1456 |
The argument may be one of: |
|
1457 |
#crlf -> add a CR-NL, as in MSDOS |
|
1458 |
#cr -> add a CR, as in VMS |
|
1459 |
#nl -> add a NL, as in Unix |
|
1460 |
anyOther -> like #nl |
|
1461 |
" |
|
1462 |
||
1463 |
eolMode := aSymbolOrNil |
|
1464 |
! |
|
1465 |
||
612 | 1466 |
fileDescriptor |
1467 |
"return the fileDescriptor of the receiver - |
|
1468 |
notice: this one returns the underlying OSs fileDescriptor - |
|
1469 |
this may not be available on all platforms (i.e. non unix systems)." |
|
1 | 1470 |
|
1471 |
%{ /* NOCONTEXT */ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1472 |
#ifdef WIN32 |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1473 |
RETURN (nil); |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1474 |
#else |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1475 |
FILEPOINTER f; |
612 | 1476 |
OBJ fp; |
1477 |
||
1133 | 1478 |
if ((fp = __INST(filePointer)) != nil) { |
2264 | 1479 |
f = __FILEVal(fp); |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1480 |
RETURN ( __MKINT(fileno(f)) ); |
1 | 1481 |
} |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1482 |
#endif |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
1483 |
%}. |
612 | 1484 |
^ self errorNotOpen |
1485 |
! |
|
1486 |
||
1487 |
filePointer |
|
1488 |
"return the filePointer of the receiver - |
|
1489 |
notice: for portability stdio is used; this means you will get |
|
1490 |
a FILE * - not a fileDescriptor. |
|
1491 |
(what you really get is a corresponding integer). |
|
1492 |
You cannot do much with the returned value |
|
1493 |
- except passing it to a primitive, for example." |
|
1494 |
||
1495 |
^ filePointer |
|
1496 |
! |
|
1497 |
||
3016 | 1498 |
lineEndTransparent |
1499 |
eolMode := #cr |
|
1500 |
! |
|
1501 |
||
612 | 1502 |
readonly |
1503 |
"set access mode to readonly" |
|
1504 |
||
1505 |
mode := #readonly |
|
1506 |
! |
|
1507 |
||
1508 |
readwrite |
|
1509 |
"set access mode to readwrite" |
|
1510 |
||
1511 |
mode := #readwrite |
|
1512 |
! |
|
1513 |
||
1514 |
text |
|
1515 |
"switch to text mode - default is text" |
|
1516 |
||
1517 |
binary := false |
|
1518 |
! |
|
1519 |
||
1520 |
useCRLF:aBoolean |
|
2920 | 1521 |
"turn on or off CRLF sending (instead of LF only) - default is off. |
1522 |
This method is provided for backward compatibility - see #eolMode: |
|
1523 |
which offers another choice." |
|
1524 |
||
1525 |
aBoolean ifTrue:[ |
|
1526 |
eolMode := #crlf |
|
1527 |
] ifFalse:[ |
|
1528 |
eolMode := #nl |
|
1529 |
]. |
|
1 | 1530 |
! |
1531 |
||
612 | 1532 |
writeonly |
1533 |
"set access mode to writeonly" |
|
1534 |
||
1535 |
mode := #writeonly |
|
1536 |
! ! |
|
1537 |
||
1538 |
!ExternalStream methodsFor:'error handling'! |
|
1539 |
||
1540 |
argumentMustBeCharacter |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1541 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1542 |
|
612 | 1543 |
"report an error, that the argument must be a character" |
1544 |
||
1545 |
^ self error:'argument must be a character' |
|
1546 |
! |
|
1547 |
||
1548 |
argumentMustBeInteger |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1549 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1550 |
|
612 | 1551 |
"report an error, that the argument must be an integer" |
1552 |
||
1553 |
^ self error:'argument must be an integer' |
|
77 | 1554 |
! |
1555 |
||
612 | 1556 |
argumentMustBeString |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1557 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1558 |
|
612 | 1559 |
"report an error, that the argument must be a string" |
1560 |
||
1561 |
^ self error:'argument must be a string' |
|
1562 |
! |
|
1563 |
||
1564 |
errorBinary |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1565 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1566 |
|
612 | 1567 |
"report an error, that the stream is in binary mode" |
1568 |
||
2920 | 1569 |
^ self invalidModeSignal |
2264 | 1570 |
raiseRequestWith:self |
1571 |
errorString:(self class name , ' is in binary mode') |
|
1572 |
in:thisContext sender |
|
612 | 1573 |
! |
1574 |
||
1575 |
errorNotBinary |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1576 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1577 |
|
612 | 1578 |
"report an error, that the stream is not in binary mode" |
1579 |
||
2920 | 1580 |
^ self invalidModeSignal |
2264 | 1581 |
raiseRequestWith:self |
1582 |
errorString:(self class name , ' is not in binary mode') |
|
1583 |
in:thisContext sender |
|
1 | 1584 |
! |
1585 |
||
612 | 1586 |
errorNotBuffered |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1587 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1588 |
|
612 | 1589 |
"report an error, that the stream is not in buffered mode" |
1590 |
||
1591 |
^ StreamErrorSignal |
|
2264 | 1592 |
raiseRequestWith:self |
1593 |
errorString:(self class name , ' is unbuffered - operation not allowed') |
|
1594 |
in:thisContext sender |
|
612 | 1595 |
! |
1596 |
||
1597 |
errorNotOpen |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1598 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1599 |
|
612 | 1600 |
"report an error, that the stream has not been opened" |
1601 |
||
3467 | 1602 |
^ StreamNotOpenSignal |
1603 |
raiseRequestWith:self |
|
1604 |
errorString:(self class name , ' not open') |
|
1605 |
in:thisContext sender |
|
1606 |
||
1607 |
"Modified: / 19.5.1998 / 22:07:52 / cg" |
|
612 | 1608 |
! |
1609 |
||
1610 |
errorOpen |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1611 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1612 |
|
612 | 1613 |
"report an error, that the stream is already opened" |
1614 |
||
2920 | 1615 |
^ self openErrorSignal |
2264 | 1616 |
raiseRequestWith:self |
1617 |
errorString:(self class name , ' is already open') |
|
1618 |
in:thisContext sender |
|
612 | 1619 |
! |
1620 |
||
1621 |
errorReadOnly |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1622 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1623 |
|
612 | 1624 |
"report an error, that the stream is a readOnly stream" |
1625 |
||
2920 | 1626 |
^ self invalidWriteSignal |
2264 | 1627 |
raiseRequestWith:self |
1628 |
errorString:(self class name , ' is readonly') |
|
1629 |
in:thisContext sender |
|
612 | 1630 |
! |
1631 |
||
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1632 |
errorUnsupportedOperation |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1633 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1634 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1635 |
"report an error, that some unsupported operation was attempted" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1636 |
|
2920 | 1637 |
^ self invalidOperationSignal |
2264 | 1638 |
raiseRequestWith:self |
1639 |
errorString:'unsupported operation' |
|
1640 |
in:thisContext sender |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1641 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1642 |
|
612 | 1643 |
errorWriteOnly |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1644 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1645 |
|
612 | 1646 |
"report an error, that the stream is a writeOnly stream" |
1647 |
||
2920 | 1648 |
^ self invalidReadSignal |
2264 | 1649 |
raiseRequestWith:self |
1650 |
errorString:(self class name , ' is writeonly') |
|
1651 |
in:thisContext sender |
|
2 | 1652 |
! |
1653 |
||
612 | 1654 |
ioError |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1655 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1656 |
|
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1657 |
"report an error, that some I/O error occured. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1658 |
(for example, a device-IO-error, or reading an NFS-dir, |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1659 |
which is no longer available and has been mounted soft)" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1660 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1661 |
^ StreamIOErrorSignal |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1662 |
raiseRequestWith:self |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1663 |
errorString:('I/O error: ' , self lastErrorString) |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1664 |
in:thisContext sender |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1665 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1666 |
"Modified: / 21.5.1998 / 16:33:13 / cg" |
612 | 1667 |
! |
1668 |
||
1669 |
lastErrorNumber |
|
1670 |
"return the last error" |
|
1671 |
||
1672 |
^ lastErrorNumber |
|
1673 |
! |
|
1674 |
||
1675 |
lastErrorString |
|
1676 |
"return a message string describing the last error" |
|
1677 |
||
1678 |
(lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[ |
|
2264 | 1679 |
^ 'I/O error' |
612 | 1680 |
]. |
1681 |
^ OperatingSystem errorTextForNumber:lastErrorNumber |
|
2 | 1682 |
! |
1683 |
||
612 | 1684 |
openError |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1685 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1686 |
|
612 | 1687 |
"report an error, that the open failed" |
1688 |
||
3235
cc254b307159
FileStream redefines #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3221
diff
changeset
|
1689 |
^ self class openErrorSignal |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
1690 |
raiseRequestWith:self |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
1691 |
errorString:('error on open: ' , self lastErrorString) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
1692 |
in:thisContext sender |
3235
cc254b307159
FileStream redefines #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3221
diff
changeset
|
1693 |
|
cc254b307159
FileStream redefines #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3221
diff
changeset
|
1694 |
"Modified: / 28.1.1998 / 14:37:42 / stefan" |
612 | 1695 |
! |
1696 |
||
1697 |
readError |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1698 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1699 |
|
612 | 1700 |
"report an error, that some read error occured" |
1701 |
||
1702 |
^ ReadErrorSignal |
|
2264 | 1703 |
raiseRequestWith:self |
1704 |
errorString:('read error: ' , self lastErrorString) |
|
1705 |
in:thisContext sender |
|
612 | 1706 |
! |
1707 |
||
1708 |
writeError |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1709 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
1710 |
|
612 | 1711 |
"report an error, that some write error occured" |
1712 |
||
1713 |
^ WriteErrorSignal |
|
2264 | 1714 |
raiseRequestWith:self |
1715 |
errorString:('write error: ' , self lastErrorString) |
|
1716 |
in:thisContext sender |
|
612 | 1717 |
! ! |
1718 |
||
1719 |
!ExternalStream methodsFor:'instance release'! |
|
1720 |
||
1721 |
closeFile |
|
1722 |
"low level close - may be redefined in subclasses" |
|
2 | 1723 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
1724 |
%{ |
216 | 1725 |
OBJ fp; |
2 | 1726 |
|
1133 | 1727 |
if ((fp = __INST(filePointer)) != nil) { |
2264 | 1728 |
__INST(filePointer) = nil; |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1729 |
#if defined(WIN32) && defined(NO_STDIO) |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1730 |
CloseHandle(__HANDLEVal(fp)); |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1731 |
#else |
2264 | 1732 |
__BEGIN_INTERRUPTABLE__ |
1733 |
fclose(__FILEVal(fp)); |
|
1734 |
__END_INTERRUPTABLE__ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1735 |
#endif |
10 | 1736 |
} |
1737 |
%} |
|
1738 |
! |
|
1739 |
||
612 | 1740 |
disposed |
1741 |
"some Stream has been collected - close the file if not already done" |
|
1742 |
||
1743 |
self closeFile |
|
1744 |
! |
|
1745 |
||
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1746 |
setFileDescriptor:anInteger mode:openMode |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1747 |
"set the filePointer based upon a given fileDescriptor - |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1748 |
notice: this one is based on the underlying OSs fileDescriptor - |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1749 |
this may not be available on all platforms (i.e. non unix systems)." |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1750 |
|
3681 | 1751 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1752 |
FILEPOINTER f; |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1753 |
OBJ fp; |
2765 | 1754 |
FILE *fdopen(); |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1755 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1756 |
if (__isSmallInteger(anInteger) && |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1757 |
__isString(openMode) && |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1758 |
(f = fdopen(__intVal(anInteger), __stringVal(openMode))) != 0 |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1759 |
) { |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1760 |
__PROTECT__(self); |
3420 | 1761 |
fp = __MKFILEPOINTER(f); |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1762 |
__UNPROTECT__(self); |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1763 |
__INST(filePointer) = fp; __STORE(self, fp); |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1764 |
RETURN (self); |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1765 |
} |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1766 |
%}. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1767 |
^ self primitiveFailed |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1768 |
! |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1769 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1770 |
setFilePointer:something |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1771 |
"set the filePointer to the given one; |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1772 |
low level private & friend interface; may also be used to connect to some |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1773 |
externally provided file handle." |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1774 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1775 |
filePointer := something |
1 | 1776 |
! |
1777 |
||
612 | 1778 |
shallowCopyForFinalization |
1779 |
"return a copy for finalization-registration; |
|
1780 |
since all we need at finalization time is the fileDescriptor, |
|
1781 |
a cheaper copy is possible." |
|
1782 |
||
1783 |
^ self class basicNew setFilePointer:filePointer |
|
1 | 1784 |
! |
1785 |
||
612 | 1786 |
shutDown |
1787 |
"close the stream - added for protocol compatibility with PipeStream. |
|
1788 |
see comment there" |
|
1789 |
||
3949 | 1790 |
self isOpen ifTrue:[ |
1791 |
Lobby unregister:self. |
|
1792 |
self closeFile |
|
1793 |
] |
|
1642
91c6860f1c9d
when shutting down, also tell the Lobby
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
1794 |
|
91c6860f1c9d
when shutting down, also tell the Lobby
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
1795 |
"Modified: 30.8.1996 / 00:39:21 / cg" |
1 | 1796 |
! ! |
1797 |
||
2 | 1798 |
!ExternalStream methodsFor:'line reading/writing'! |
1 | 1799 |
|
1800 |
nextLine |
|
1801 |
"read the next line (characters up to newline). |
|
1802 |
Return a string containing those characters excluding the newline. |
|
1803 |
If the previous-to-last character is a cr, this is also removed, |
|
68 | 1804 |
so its possible to read alien (i.e. ms-dos) text as well. |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1805 |
The line must be shorter than 16K characters - otherwise an error is signalled." |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1806 |
|
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1807 |
|line| |
971
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
1808 |
|
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
1809 |
%{ /* STACK:34000 */ |
1 | 1810 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1811 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1812 |
int len, ret; |
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
1813 |
char buffer[32*1024]; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
1814 |
char *rslt, *nextPtr, *limit; |
22 | 1815 |
int fd, ch; |
1068
92fdcccf12a3
alles MIST - zurueck zu 1.89 und NOCONTEXT entfernt
Claus Gittinger <cg@exept.de>
parents:
1067
diff
changeset
|
1816 |
int _buffered; |
216 | 1817 |
OBJ fp; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1818 |
int lineTooLong = 0; |
2920 | 1819 |
int cutOff = 0; |
1 | 1820 |
|
1133 | 1821 |
__INST(lastErrorNumber) = nil; |
1822 |
if (((fp = __INST(filePointer)) != nil) |
|
3878
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1823 |
&& (__INST(mode) != @symbol(writeonly)) |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1824 |
&& (__INST(binary) != true) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1825 |
) { |
3878
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1826 |
f = __FILEVal(fp); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1827 |
buffer[0] = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1828 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1829 |
_buffered = (__INST(buffered) == true); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1830 |
if (_buffered) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1831 |
__READING__(f); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1832 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1833 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1834 |
rslt = nextPtr = buffer; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1835 |
limit = buffer + sizeof(buffer) - 2; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1836 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1837 |
for (;;) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1838 |
__READBYTE__(ret, f, nextPtr, _buffered); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1839 |
if (ret <= 0) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1840 |
if (nextPtr == buffer) |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1841 |
rslt = NULL; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1842 |
if (ret == 0) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1843 |
__INST(hitEOF) = true; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1844 |
break; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1845 |
} else { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1846 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1847 |
goto err; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1848 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1849 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1850 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1851 |
if (*nextPtr == '\n') { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1852 |
cutOff = 1; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1853 |
*nextPtr = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1854 |
break; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1855 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1856 |
if (*nextPtr == '\r') { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1857 |
char peekChar; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1858 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1859 |
/* |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1860 |
* peek ahead for a newLine ... |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1861 |
*/ |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1862 |
__READBYTE__(ret, f, &peekChar, _buffered); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1863 |
if (ret <= 0) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1864 |
cutOff = 1; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1865 |
*nextPtr = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1866 |
if (ret == 0) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1867 |
__INST(hitEOF) = true; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1868 |
break; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1869 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1870 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1871 |
goto err; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1872 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1873 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1874 |
if (peekChar == '\n') { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1875 |
cutOff = 2; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1876 |
*nextPtr = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1877 |
break; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1878 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1879 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1880 |
__UNGETC__(peekChar, f, _buffered); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1881 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1882 |
cutOff = 1; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1883 |
*nextPtr = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1884 |
break; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1885 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1886 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1887 |
nextPtr++; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1888 |
if (nextPtr >= limit) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1889 |
*nextPtr = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1890 |
lineTooLong = 1; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1891 |
if (@global(InfoPrinting) == true) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1892 |
fprintf(stderr, "ExtStream [warning]: line truncated in nextLine\n"); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1893 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1894 |
break; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1895 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1896 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1897 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1898 |
if (rslt != NULL) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1899 |
len = nextPtr-buffer; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1900 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1901 |
if (__INST(position) != nil) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1902 |
int __p; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1903 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1904 |
__p = __intVal(__INST(position)) + len + cutOff; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1905 |
__INST(position) = __MKSMALLINT(__p); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1906 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1907 |
/* remove any EOL character */ |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1908 |
if (len != 0) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1909 |
if (buffer[len-1] == '\n') { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1910 |
buffer[--len] = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1911 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1912 |
if ((len != 0) && (buffer[len-1] == '\r')) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1913 |
buffer[--len] = '\0'; |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1914 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1915 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1916 |
line = __MKSTRING_L(buffer, len); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1917 |
if (! lineTooLong) { |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1918 |
RETURN ( line ); |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1919 |
} |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1920 |
} |
1 | 1921 |
} |
1478
aae07192b7d1
labels in empty statements need a semi
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
1922 |
err: ; |
159 | 1923 |
%}. |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1924 |
line notNil ifTrue:[ |
3878
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1925 |
"/ the line as read is longer than 32k characters (boy - what a line) |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1926 |
"/ The exception could be handled by reading more and returning the |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1927 |
"/ concatenation in your exception handler (the receiver and the partial |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1928 |
"/ line are passed as parameter) |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1929 |
|
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1930 |
^ LineTooLongErrorSignal |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1931 |
raiseRequestWith:(Array with:self with:line) |
defeba69f8fe
raise separate LineTooLongErrorSignal in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3876
diff
changeset
|
1932 |
errorString:('line too long read error') |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1933 |
]. |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1934 |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
1935 |
hitEOF ifTrue:[^ self pastEnd]. |
159 | 1936 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
2 | 1937 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
1 | 1938 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1939 |
^ self errorBinary |
1 | 1940 |
! |
1941 |
||
1942 |
nextPutLine:aString |
|
2920 | 1943 |
"write the characters in aString and append an end-of-Line marker |
1944 |
(LF, CR or CRLF - depending in the setting of eolMode)" |
|
1 | 1945 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
1946 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1947 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1948 |
int len, cnt, len1, _buffered; |
216 | 1949 |
OBJ pos, fp; |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1950 |
char *cp; |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1951 |
int o_offs; |
1 | 1952 |
|
1133 | 1953 |
__INST(lastErrorNumber) = nil; |
1954 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 1955 |
&& (__INST(mode) != @symbol(readonly)) |
1956 |
&& (__INST(binary) != true) |
|
1957 |
&& __isString(aString) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1958 |
) { |
2264 | 1959 |
f = __FILEVal(fp); |
1960 |
len = __stringSize(aString); |
|
1961 |
||
1962 |
if (_buffered = (__INST(buffered) == true)) { |
|
1963 |
__WRITING__(f) |
|
1964 |
} |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1965 |
#ifdef BUGGY |
2264 | 1966 |
__WRITEBYTES__(cnt, f, __stringVal(aString), len, _buffered); |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1967 |
#else |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1968 |
o_offs = (char *)__stringVal(aString)-(char *)aString; |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1969 |
__WRITEBYTES_OBJ__(cnt, f, aString, o_offs, len, _buffered); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1970 |
#endif |
2264 | 1971 |
if (cnt == len) { |
2920 | 1972 |
OBJ mode = __INST(eolMode); |
1973 |
||
2264 | 1974 |
len1 = len; |
1975 |
||
2920 | 1976 |
if (mode == @symbol(cr)) { |
1977 |
cp = "\r"; len = 1; |
|
1978 |
} else if (mode == @symbol(crlf)) { |
|
2264 | 1979 |
cp = "\r\n"; len = 2; |
1980 |
} else { |
|
1981 |
cp = "\n"; len = 1; |
|
1982 |
} |
|
1983 |
__WRITEBYTES__(cnt, f, cp, len, _buffered); |
|
1984 |
||
1985 |
if (cnt > 0) { |
|
1986 |
pos = __INST(position); |
|
1987 |
if (pos != nil) { |
|
2920 | 1988 |
int __p; |
1989 |
||
1990 |
__p = __intVal(pos) + len1 + cnt; |
|
1991 |
__INST(position) = __MKSMALLINT(__p); |
|
2264 | 1992 |
} |
1993 |
RETURN ( self ); |
|
1994 |
} |
|
1995 |
} |
|
1996 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1 | 1997 |
} |
159 | 1998 |
%}. |
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1999 |
super nextPutLine:aString. |
1 | 2000 |
! |
2001 |
||
2002 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil |
|
68 | 2003 |
"read from aStream up to and including a line starting with aStringOrNil |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2004 |
and append all lines to self. |
159 | 2005 |
Can be used to copy/create large files or copy from a pipe/socket. |
68 | 2006 |
|
22 | 2007 |
If aStringOrNil is nil or not matched, copy preceeds to the end. |
2008 |
(this allows for example to read a Socket and transfer the data quickly |
|
2009 |
into a file - without creating zillions of temporary strings)" |
|
1 | 2010 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2011 |
|srcFilePointer readError line| |
1 | 2012 |
|
2331 | 2013 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
2014 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
1 | 2015 |
srcFilePointer := aStream filePointer. |
2331 | 2016 |
srcFilePointer isNil ifTrue:[aStream errorNotOpen. ^ self]. |
12 | 2017 |
|
2018 |
%{ /* STACK:2000 */ |
|
22 | 2019 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2020 |
#ifndef NO_STDIO |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2021 |
FILEPOINTER dst; |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2022 |
FILEPOINTER src; |
1 | 2023 |
char *matchString; |
2024 |
int matchLen = 0; |
|
12 | 2025 |
char buffer[1024]; |
1 | 2026 |
|
1133 | 2027 |
__INST(lastErrorNumber) = nil; |
249 | 2028 |
if (__isSmallInteger(srcFilePointer)) { |
2264 | 2029 |
if ((aStringOrNil == nil) || __isString(aStringOrNil)) { |
2030 |
if (aStringOrNil != nil) { |
|
2031 |
matchString = (char *) __stringVal(aStringOrNil); |
|
2032 |
matchLen = __stringSize(aStringOrNil); |
|
2033 |
} |
|
2034 |
dst = __FILEVal(__INST(filePointer)); |
|
2035 |
src = __FILEVal(srcFilePointer); |
|
2036 |
__BEGIN_INTERRUPTABLE__ |
|
2037 |
errno = 0; |
|
2038 |
||
2039 |
__WRITING__(dst) |
|
2040 |
||
2041 |
for (;;) { |
|
2042 |
if (fgets(buffer, sizeof(buffer)-1, src) == NULL) { |
|
2043 |
if (ferror(src)) { |
|
2044 |
readError = __MKSMALLINT(errno); |
|
2045 |
__END_INTERRUPTABLE__ |
|
2046 |
goto err; |
|
2047 |
} |
|
2048 |
break; |
|
2049 |
} |
|
2050 |
if (fputs(buffer, dst) == EOF) { |
|
2051 |
if (ferror(dst)) { |
|
2052 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2053 |
__END_INTERRUPTABLE__ |
|
2054 |
goto err; |
|
2055 |
} |
|
2056 |
break; |
|
2057 |
} |
|
12 | 2058 |
#ifndef OLD |
2264 | 2059 |
if (__INST(buffered) == false) { |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2060 |
FFLUSH(dst); |
2264 | 2061 |
} |
12 | 2062 |
#endif |
2264 | 2063 |
if (matchLen) { |
2064 |
if (strncmp(matchString, buffer, matchLen) == 0) |
|
2065 |
break; |
|
2066 |
} |
|
2067 |
} |
|
2068 |
__END_INTERRUPTABLE__ |
|
2069 |
__INST(position) = nil; |
|
2070 |
RETURN (self); |
|
2071 |
} |
|
1 | 2072 |
} |
159 | 2073 |
err: ; |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2074 |
#endif /* ! NO_STDIO */ |
159 | 2075 |
%}. |
2076 |
readError ifTrue:[ |
|
2264 | 2077 |
aStream setLastErrorNumber:readError. |
2331 | 2078 |
aStream readError. |
2079 |
^ self |
|
159 | 2080 |
]. |
2331 | 2081 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
2082 |
buffered ifFalse:[self errorNotBuffered. ^ self]. |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2083 |
|
159 | 2084 |
" |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2085 |
argument error or unimplemented; try it linewise |
159 | 2086 |
" |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2087 |
[aStream atEnd] whileFalse:[ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2088 |
line := aStream nextLine. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2089 |
line isNil ifTrue:[ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2090 |
^ self. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2091 |
]. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2092 |
self nextPutLine:line. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2093 |
(aStringOrNil notNil and:[line startsWith:aStringOrNil]) ifTrue:[ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2094 |
^ self |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2095 |
] |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2096 |
]. |
1 | 2097 |
! |
2098 |
||
2099 |
peekForLineStartingWith:aString |
|
2100 |
"read ahead for next line starting with aString; |
|
159 | 2101 |
return the line-string if found, or nil if EOF is encountered. |
2102 |
If matched, not advance position behond that line |
|
2103 |
i.e. nextLine will read the matched line. |
|
2104 |
If not matched, reposition to original position for firther reading." |
|
1 | 2105 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2106 |
|firstPos lastPos line| |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2107 |
|
1 | 2108 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
2109 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2 | 2110 |
binary ifTrue:[^ self errorBinary]. |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2111 |
buffered ifFalse:[^ self errorNotBuffered]. |
12 | 2112 |
|
2113 |
%{ /* STACK: 2000 */ |
|
2920 | 2114 |
#ifndef NO_STDIO |
2115 |
||
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2116 |
FILEPOINTER f; |
1 | 2117 |
int l; |
12 | 2118 |
char buffer[1024]; |
1 | 2119 |
char *cp; |
2120 |
char *matchString; |
|
2920 | 2121 |
int gotFirst = 0; |
2122 |
#ifdef __VMS__ |
|
2123 |
fpos_t firstpos, lastpos; |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2124 |
# define FTELL(__f__, __pos__) fgetpos(__f__, &(__pos__)) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2125 |
# define FSEEK(__f__, __pos__) fsetpos(__f__, &(__pos__)) |
2920 | 2126 |
#else |
2127 |
long firstpos, lastpos; |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2128 |
# define FTELL(__f__, __pos__) __pos__ = ftell(__f__) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2129 |
# define FSEEK(__f__, __pos__) fseek(__f__, __pos__, SEEK_SET) |
2920 | 2130 |
#endif |
2131 |
||
1133 | 2132 |
__INST(lastErrorNumber) = nil; |
54 | 2133 |
if (__isString(aString)) { |
2264 | 2134 |
matchString = (char *) __stringVal(aString); |
2135 |
l = __stringSize(aString); |
|
2136 |
||
2137 |
f = __FILEVal(__INST(filePointer)); |
|
2138 |
__READING__(f) |
|
2139 |
||
2140 |
for (;;) { |
|
2920 | 2141 |
FTELL(f, lastpos); |
2142 |
if (!gotFirst) { |
|
2143 |
firstpos = lastpos; |
|
2144 |
gotFirst = 1; |
|
2145 |
} |
|
2264 | 2146 |
|
2147 |
__BEGIN_INTERRUPTABLE__ |
|
2148 |
do { |
|
2149 |
cp = fgets(buffer, sizeof(buffer)-1, f); |
|
2150 |
} while ((cp == NULL) && ferror(f) && (errno == EINTR) && (clearerr(f), 1)); |
|
2151 |
buffer[sizeof(buffer)-1] = '\0'; |
|
2152 |
__END_INTERRUPTABLE__ |
|
2153 |
||
2154 |
if (cp == NULL) { |
|
2155 |
if (ferror(f)) { |
|
2156 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2157 |
goto err; |
|
2158 |
} else { |
|
2920 | 2159 |
FSEEK(f, firstpos); |
2264 | 2160 |
RETURN (nil); |
2161 |
} |
|
2162 |
} |
|
2163 |
if (strncmp(cp, matchString, l) == 0) { |
|
2920 | 2164 |
FSEEK(f, lastpos); |
2264 | 2165 |
break; |
2166 |
} |
|
2167 |
} |
|
2168 |
/* remove EOL character */ |
|
2169 |
cp = buffer; |
|
2920 | 2170 |
while (*cp && (*cp != '\n') && (*cp != '\r')) cp++; |
2264 | 2171 |
*cp = '\0'; |
2172 |
RETURN ( __MKSTRING(buffer) ); |
|
1 | 2173 |
} |
159 | 2174 |
err: ; |
2920 | 2175 |
|
2176 |
# undef FTELL |
|
2177 |
# undef FSEEK |
|
2178 |
||
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2179 |
#endif |
159 | 2180 |
%}. |
2181 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2182 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2183 |
"/ try using generic code ... |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2184 |
|
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2185 |
firstPos := self position. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2186 |
[self atEnd] whileFalse:[ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2187 |
lastPos := self position. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2188 |
line := self nextLine. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2189 |
line isNil ifTrue:[ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2190 |
self position:firstPos. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2191 |
^ nil |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2192 |
]. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2193 |
(line startsWith:aString) ifTrue:[ |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2194 |
self position:lastPos. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2195 |
^ line |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2196 |
] |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2197 |
]. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2198 |
self position:firstPos. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2199 |
^ nil |
1 | 2200 |
! |
2201 |
||
2202 |
peekForLineStartingWithAny:aCollectionOfStrings |
|
2203 |
"read ahead for next line starting with any of aCollectionOfStrings; |
|
2204 |
return the index in aCollection if found, nil otherwise.. |
|
2205 |
If no match, do not change position; otherwise advance right before the |
|
2206 |
matched line so that nextLine will return this line." |
|
2207 |
||
2208 |
|line startPos linePos index| |
|
2209 |
||
2210 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2211 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2 | 2212 |
binary ifTrue:[^ self errorBinary]. |
2213 |
||
1 | 2214 |
startPos := self position. |
2215 |
[self atEnd] whileFalse:[ |
|
2264 | 2216 |
linePos := self position. |
2217 |
line := self nextLine. |
|
2218 |
line notNil ifTrue:[ |
|
2219 |
index := 1. |
|
2220 |
aCollectionOfStrings do:[:prefix | |
|
2221 |
(line startsWith:prefix) ifTrue:[ |
|
2222 |
self position:linePos. |
|
2223 |
^ index |
|
2224 |
]. |
|
2225 |
index := index + 1 |
|
2226 |
] |
|
2227 |
] |
|
1 | 2228 |
]. |
2229 |
self position:startPos. |
|
2230 |
^ nil |
|
2231 |
! ! |
|
2232 |
||
612 | 2233 |
!ExternalStream methodsFor:'misc functions'! |
2234 |
||
2235 |
async:aBoolean |
|
779 | 2236 |
"set/clear the async attribute - if set, the availability of data on |
612 | 2237 |
the receiver will trigger an ioInterrupt. |
779 | 2238 |
If cleared (which is the default) no special notification is made. |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2239 |
Notice: |
2264 | 2240 |
not every OS supports this |
2241 |
- check with OS>>supportsIOInterrupts before using" |
|
92 | 2242 |
|
2243 |
|fd| |
|
2244 |
||
159 | 2245 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2246 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2247 |
OperatingSystem supportsIOInterrupts ifFalse:[ |
2264 | 2248 |
^ self errorUnsupportedOperation |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2249 |
]. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2250 |
|
92 | 2251 |
fd := self fileDescriptor. |
612 | 2252 |
aBoolean ifTrue:[ |
2264 | 2253 |
^ OperatingSystem enableIOInterruptsOn:fd |
612 | 2254 |
]. |
2255 |
^ OperatingSystem disableIOInterruptsOn:fd |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2256 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2257 |
"Modified: 11.1.1997 / 17:50:21 / cg" |
92 | 2258 |
! |
2259 |
||
612 | 2260 |
backStep |
2261 |
"step back one element - |
|
2262 |
redefined, since position is redefined here" |
|
2263 |
||
2264 |
self position:(self position - 1) |
|
362 | 2265 |
! |
2266 |
||
612 | 2267 |
blocking:aBoolean |
2268 |
"set/clear the blocking attribute - if set (which is the default) |
|
2269 |
a read (using next) on the receiver will block until data is available. |
|
2270 |
If cleared, a read operation will immediately return with a value of |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2271 |
nil. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2272 |
Turning off blocking is useful when reading from PipeStreams |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2273 |
or Sockets, and the amount of data to be read is not known |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2274 |
in advance. However, the data must then be read using #nextBytes: |
3174 | 2275 |
methods, in order to avoid other (pastEnd) exceptions." |
92 | 2276 |
|
159 | 2277 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
612 | 2278 |
^ OperatingSystem setBlocking:aBoolean on:(self fileDescriptor) |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2279 |
|
3174 | 2280 |
"Modified: / 11.1.1997 / 17:48:01 / cg" |
2281 |
"Modified: / 15.1.1998 / 11:49:48 / stefan" |
|
612 | 2282 |
! |
2283 |
||
2284 |
close |
|
2285 |
"close the stream - tell operating system" |
|
2286 |
||
3949 | 2287 |
self isOpen ifTrue:[ |
2288 |
Lobby unregister:self. |
|
2289 |
self closeFile. |
|
2290 |
] |
|
612 | 2291 |
! |
2292 |
||
2293 |
create |
|
2294 |
"create the stream |
|
2295 |
- this must be redefined in subclass" |
|
2296 |
||
2297 |
^ self subclassResponsibility |
|
2298 |
! |
|
2299 |
||
2300 |
ioctl:ioctlNumber |
|
2301 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2302 |
on the underlying file; no arguments are passed." |
|
2303 |
||
2920 | 2304 |
^ self ioctl:ioctlNumber with:nil |
2305 |
! |
|
2306 |
||
2307 |
ioctl:ioctlNumber with:arg |
|
2308 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2309 |
on the underlying file; the argument is passed as argument. |
|
2310 |
This is not used by ST/X, but provided for special situations |
|
2311 |
- for example, to control proprietrary I/O devices. |
|
2312 |
||
2313 |
Since the type of the argument depends on the ioctl being |
|
2314 |
performed, different arg types are allowed here. |
|
2315 |
If the argument is nil, an ioctl without argument is performed. |
|
2316 |
If the argument is an integral number, its directly passed; |
|
2317 |
if its a kind of ByteArray (ByteArray, String or Structure), |
|
2318 |
or external data (ExternalBytes or ExternalAddress), |
|
2319 |
a pointer to the data is passed. |
|
2320 |
This allows performing most ioctls |
|
2321 |
- however, it might be tricky to setup the buffer. |
|
2322 |
Be careful in what you pass - ST/X cannot validate its correctness." |
|
2323 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2324 |
%{ |
2896 | 2325 |
#if !defined(MSDOS_LIKE) |
2326 |
# if !defined(__openVMS__) |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2327 |
FILEPOINTER f; |
2920 | 2328 |
int ret, ioNum; |
612 | 2329 |
OBJ fp; |
2920 | 2330 |
INT ioArg; |
612 | 2331 |
|
1133 | 2332 |
__INST(lastErrorNumber) = nil; |
2333 |
if ((fp = __INST(filePointer)) != nil) { |
|
2920 | 2334 |
if (__isSmallInteger(ioctlNumber) |
2335 |
&& (__isSmallInteger(arg) |
|
2336 |
|| (arg == nil) |
|
2337 |
|| __isBytes(arg) |
|
2338 |
|| __isExternalBytes(arg) |
|
2339 |
|| __isExternalAddress(arg))) { |
|
2340 |
f = __FILEVal(fp); |
|
2264 | 2341 |
ioNum = __intVal(ioctlNumber); |
2342 |
||
2343 |
__BEGIN_INTERRUPTABLE__ |
|
2344 |
do { |
|
2345 |
errno = 0; |
|
2920 | 2346 |
if (arg == nil) { |
2347 |
ioArg = 0; |
|
2348 |
} else if (__isSmallInteger(arg)) { |
|
2349 |
ioArg = __intVal(arg); |
|
2350 |
} else if (__isExternalBytes(arg)) { |
|
2351 |
ioArg = (INT)(__externalBytesAddress(arg)); |
|
2352 |
} else if (__isExternalAddress(arg)) { |
|
2353 |
ioArg = (INT)(__externalAddressVal(arg)); |
|
2354 |
} else { |
|
2355 |
ioArg = (INT)(__ByteArrayInstPtr(arg)->ba_element); |
|
2356 |
} |
|
2357 |
ret = ioctl(fileno(f), ioNum, ioArg); |
|
2264 | 2358 |
} while ((ret < 0) && (errno == EINTR)); |
2359 |
__END_INTERRUPTABLE__ |
|
2360 |
||
2361 |
if (ret >= 0) { |
|
2362 |
RETURN ( __MKSMALLINT(ret) ); |
|
2363 |
} |
|
2364 |
__INST(position) = nil; |
|
2365 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2366 |
} |
|
612 | 2367 |
} |
2896 | 2368 |
# endif |
791 | 2369 |
#endif |
612 | 2370 |
%}. |
2371 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
2372 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2373 |
" |
|
2920 | 2374 |
argument is not an integer |
2375 |
" |
|
2376 |
(ioctlNumber isMemberOf:SmallInteger) ifFalse:[^ self primitiveFailed]. |
|
612 | 2377 |
" |
2920 | 2378 |
the system does not support ioctl (MSDOS or VMS) |
612 | 2379 |
" |
2920 | 2380 |
^ self errorUnsupportedOperation |
612 | 2381 |
! |
2382 |
||
2383 |
open |
|
2384 |
"open the stream |
|
2385 |
- this must be redefined in subclass" |
|
2386 |
||
2387 |
^ self subclassResponsibility |
|
2388 |
! |
|
2389 |
||
2390 |
position |
|
2391 |
"return the position |
|
2392 |
- this must be redefined in subclass" |
|
2393 |
||
2394 |
^ self subclassResponsibility |
|
2395 |
! |
|
2396 |
||
2397 |
position:anInteger |
|
2398 |
"set the position |
|
2399 |
- this must be redefined in subclass" |
|
2400 |
||
2401 |
^ self subclassResponsibility |
|
2402 |
! |
|
2403 |
||
2404 |
reset |
|
2405 |
"set the read position to the beginning of the collection" |
|
2406 |
||
2407 |
self position:"0" 1 |
|
2408 |
! |
|
2409 |
||
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
2410 |
setToEnd |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
2411 |
"redefined since it must be implemented differently" |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
2412 |
|
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
2413 |
^ self subclassResponsibility |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
2414 |
! |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
2415 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2416 |
truncateTo:newSize |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2417 |
"truncate the underlying OS file to newSize. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2418 |
Warning: this may not be implemented on all platforms." |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2419 |
|
1068
92fdcccf12a3
alles MIST - zurueck zu 1.89 und NOCONTEXT entfernt
Claus Gittinger <cg@exept.de>
parents:
1067
diff
changeset
|
2420 |
%{ |
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2421 |
#ifdef HAS_FTRUNCATE |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2422 |
FILEPOINTER f; |
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2423 |
OBJ fp; |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2424 |
|
1133 | 2425 |
if (((fp = __INST(filePointer)) != nil) |
2426 |
&& (__INST(mode) != @symbol(readonly))) { |
|
2264 | 2427 |
if (__isSmallInteger(newSize)) { |
2428 |
f = __FILEVal(fp); |
|
2429 |
||
2430 |
if (__INST(buffered) == true) { |
|
2431 |
__READING__(f) |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2432 |
FFLUSH(f); |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2433 |
OPT_FSEEK(f, 0L, SEEK_END); /* needed in stdio */ |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2434 |
} |
2264 | 2435 |
ftruncate(fileno(f), __intVal(newSize)); |
2436 |
RETURN (self); |
|
2437 |
} |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2438 |
} |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2439 |
#endif |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2440 |
%}. |
2331 | 2441 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
2442 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
2443 |
self errorUnsupportedOperation |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2444 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2445 |
" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2446 |
|s| |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2447 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2448 |
s := 'test' asFilename writeStream. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2449 |
s next:1000 put:$a. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2450 |
s truncateTo:100. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2451 |
s close. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2452 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2453 |
('test' asFilename fileSize) printNL |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2454 |
" |
612 | 2455 |
! ! |
2456 |
||
2457 |
!ExternalStream methodsFor:'non homogenous reading'! |
|
2458 |
||
3674 | 2459 |
nextAvailable:count |
2460 |
"return the next count elements of the stream as aCollection. |
|
2461 |
If the stream reaches the end before count elements have been read, |
|
2462 |
return what is available. (i.e. a shorter collection). |
|
2463 |
The type of collection is specified in #contentsSpecies." |
|
2464 |
||
2465 |
|buffer n |
|
2466 |
cnt "{ Class: SmallInteger }"| |
|
2467 |
||
2468 |
binary ifTrue:[ |
|
2469 |
buffer := ByteArray new:count |
|
2470 |
] ifFalse:[ |
|
2471 |
buffer := String new:count |
|
2472 |
]. |
|
2473 |
n := self nextAvailableBytes:count into:buffer startingAt:1. |
|
2474 |
n == 0 ifTrue:[^ nil]. |
|
2475 |
||
2476 |
n ~~ count ifTrue:[ |
|
2477 |
^ buffer copyTo:n |
|
2478 |
]. |
|
2479 |
^ buffer. |
|
2480 |
||
2481 |
" |
|
2482 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:3 |
|
2483 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:10 |
|
2484 |
(ReadStream on:'hello') nextAvailable:3 |
|
2485 |
(ReadStream on:'hello') nextAvailable:10 |
|
2486 |
" |
|
2487 |
||
2488 |
"Modified: / 16.6.1998 / 15:52:41 / cg" |
|
2489 |
||
2490 |
! |
|
2491 |
||
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2492 |
nextAvailableBytes:count into:anObject startingAt:start |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2493 |
"read the next count bytes into an object and return the number of |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2494 |
bytes read or the number of bytes read, if EOF is encountered before, |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2495 |
or no more bytes are available for reading (from the pipe/socket). |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2496 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2497 |
If the receiver is some socket/pipe-like stream, an exception |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2498 |
is raised if the connection is broken. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2499 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2500 |
Notice, that in contrast to other methods, |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2501 |
this does NOT return nil on EOF, but the actual count. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2502 |
Thus allowing read of partial blocks. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2503 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2504 |
The object must have non-pointer indexed instvars |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2505 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2506 |
If anObject is a string or byteArray and reused, this provides the |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2507 |
fastest possible physical I/O (since no new objects are allocated). |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2508 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2509 |
Use with care - non object oriented I/O. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2510 |
Warning: in general, you cannot use this method to pass data from other |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2511 |
architectures since it does not care for byte order or float representation." |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2512 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2513 |
%{ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2514 |
FILEPOINTER f; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2515 |
int cnt, offs, ret, _buffered; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2516 |
int objSize, nInstVars, nInstBytes; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2517 |
char *cp; |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2518 |
char *extPtr; |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2519 |
OBJ pos, fp, oClass; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2520 |
int o_offs; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2521 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2522 |
__INST(lastErrorNumber) = nil; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2523 |
if (((fp = __INST(filePointer)) != nil) |
3681 | 2524 |
&& (__INST(mode) != @symbol(writeonly)) |
2525 |
&& __bothSmallInteger(count, start) |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2526 |
) { |
3681 | 2527 |
f = __FILEVal(fp); |
2528 |
||
2529 |
cnt = __intVal(count); |
|
2530 |
offs = __intVal(start) - 1; |
|
2531 |
||
2532 |
oClass = __Class(anObject); |
|
2533 |
if (oClass == ExternalBytes) { |
|
2534 |
OBJ sz; |
|
2535 |
||
2536 |
nInstBytes = 0; |
|
2537 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
2538 |
sz = __externalBytesSize(anObject); |
|
2539 |
if (__isSmallInteger(sz)) { |
|
2540 |
objSize = __intVal(sz); |
|
2541 |
} else { |
|
2542 |
objSize = 0; /* unknown */ |
|
2543 |
} |
|
2544 |
} else { |
|
2545 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
2546 |
case BYTEARRAY: |
|
2547 |
case WORDARRAY: |
|
2548 |
case LONGARRAY: |
|
2549 |
case SWORDARRAY: |
|
2550 |
case SLONGARRAY: |
|
2551 |
case FLOATARRAY: |
|
2552 |
case DOUBLEARRAY: |
|
2553 |
break; |
|
2554 |
default: |
|
2555 |
goto bad; |
|
2556 |
} |
|
2557 |
extPtr = (char *)0; |
|
2558 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2559 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
2560 |
objSize = __Size(anObject) - nInstBytes; |
|
2561 |
} |
|
2562 |
||
2563 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
2564 |
_buffered = (__INST(buffered) == true); |
|
2565 |
if (_buffered) { |
|
2566 |
__READING__(f); |
|
2567 |
} |
|
2568 |
||
2569 |
if (extPtr) { |
|
2570 |
__READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered); |
|
2571 |
} else { |
|
2572 |
/* |
|
2573 |
* on interrupt, anObject may be moved to another location. |
|
2574 |
* So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__, |
|
2575 |
* to get a new address. |
|
2576 |
*/ |
|
2577 |
offs += nInstBytes; |
|
2578 |
__READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered); |
|
2579 |
} |
|
2580 |
if (ret > 0) { |
|
2581 |
pos = __INST(position); |
|
2582 |
if (pos != nil) { |
|
2583 |
__INST(position) = __MKSMALLINT(__intVal(pos) + ret); |
|
2584 |
} |
|
2585 |
RETURN (__MKSMALLINT(ret)); |
|
2586 |
} |
|
2587 |
if (ret == 0) { |
|
2588 |
__INST(hitEOF) = true; |
|
2589 |
} else /* ret < 0 */ { |
|
2590 |
__INST(position) = nil; |
|
2591 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2592 |
} |
|
2593 |
} |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2594 |
} |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2595 |
bad: ; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2596 |
%}. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2597 |
(hitEOF and:[self pastEnd isNil]) ifTrue:[^ 0]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2598 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2599 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2600 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2601 |
" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2602 |
count not integer or arg not bit-like (String, ByteArray etc) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2603 |
" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2604 |
^ self primitiveFailed |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2605 |
! |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2606 |
|
612 | 2607 |
nextByte |
2608 |
"read the next byte and return it as an Integer; return nil on error. |
|
2609 |
This is allowed in both text and binary modes, always returning the |
|
2610 |
bytes binary value as an integer in 0..255." |
|
2611 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2612 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2613 |
FILEPOINTER f; |
612 | 2614 |
unsigned char byte; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2615 |
int ret, _buffered; |
612 | 2616 |
OBJ fp; |
2617 |
||
1133 | 2618 |
__INST(lastErrorNumber) = nil; |
2619 |
if (((fp = __INST(filePointer)) != nil) |
|
2620 |
&& (__INST(mode) != @symbol(writeonly))) { |
|
2264 | 2621 |
f = __FILEVal(fp); |
2622 |
||
2623 |
_buffered = (__INST(buffered) == true); |
|
2624 |
if (_buffered) { |
|
2625 |
__READING__(f) |
|
2626 |
} |
|
2627 |
__READBYTE__(ret, f, &byte, _buffered); |
|
2628 |
if (ret > 0) { |
|
2629 |
if (__INST(position) != nil) |
|
2630 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1); |
|
2631 |
RETURN (__MKSMALLINT(byte)); |
|
2632 |
} |
|
2633 |
||
2634 |
if (ret == 0) { |
|
2635 |
__INST(hitEOF) = true; |
|
2636 |
} else /* ret < 0 */ { |
|
2637 |
__INST(position) = nil; |
|
2638 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2639 |
} |
|
612 | 2640 |
} |
2641 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
2642 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 2643 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
2644 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2645 |
^ self errorWriteOnly |
|
2646 |
! |
|
2647 |
||
2648 |
nextBytes:count into:anObject startingAt:start |
|
2649 |
"read the next count bytes into an object and return the number of |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2650 |
bytes read or the number of bytes read, if EOF is encountered before. |
612 | 2651 |
If the receiver is some socket/pipe-like stream, an exception |
2652 |
is raised if the connection is broken. |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2653 |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2654 |
Warning: if used with a pipe/socket, this blocks until the requested number |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2655 |
of bytes have been read. See #nextAvailableBytes:into:startingAt: |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2656 |
to only read whats there. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
2657 |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2658 |
Notice, that in contrast to other methods, |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2659 |
this does NOT return nil on EOF, but the actual count. |
612 | 2660 |
Thus allowing read of partial blocks. |
2661 |
||
2662 |
The object must have non-pointer indexed instvars |
|
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2663 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray), |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2664 |
or an externalBytes object (with known size). |
612 | 2665 |
If anObject is a string or byteArray and reused, this provides the |
2666 |
fastest possible physical I/O (since no new objects are allocated). |
|
2667 |
||
2668 |
Use with care - non object oriented I/O. |
|
2669 |
Warning: in general, you cannot use this method to pass data from other |
|
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2670 |
architectures (unless you prepared the buffer with care), |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2671 |
since it does not care for byte order or float representation." |
612 | 2672 |
|
2673 |
%{ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2674 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2675 |
int cnt, offs, ret, _buffered; |
612 | 2676 |
int objSize, nInstVars, nInstBytes; |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2677 |
char *extPtr; |
612 | 2678 |
OBJ pos, fp, oClass; |
2679 |
||
1133 | 2680 |
__INST(lastErrorNumber) = nil; |
2681 |
if (((fp = __INST(filePointer)) != nil) |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2682 |
&& (__INST(mode) != @symbol(writeonly)) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2683 |
&& __bothSmallInteger(count, start) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2684 |
) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2685 |
f = __FILEVal(fp); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2686 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2687 |
cnt = __intVal(count); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2688 |
offs = __intVal(start) - 1; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2689 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2690 |
oClass = __Class(anObject); |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2691 |
if (oClass == ExternalBytes) { |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2692 |
OBJ sz; |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2693 |
|
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2694 |
nInstBytes = 0; |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2695 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2696 |
sz = __externalBytesSize(anObject); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2697 |
if (__isSmallInteger(sz)) { |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2698 |
objSize = __intVal(sz); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2699 |
} else { |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2700 |
objSize = 0; /* unknown */ |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2701 |
} |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2702 |
} else { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2703 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2704 |
case BYTEARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2705 |
case WORDARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2706 |
case LONGARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2707 |
case SWORDARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2708 |
case SLONGARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2709 |
case FLOATARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2710 |
case DOUBLEARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2711 |
break; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2712 |
default: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2713 |
goto bad; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2714 |
} |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2715 |
extPtr = (char *)0; |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2716 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2717 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2718 |
objSize = __Size(anObject) - nInstBytes; |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2719 |
} |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2720 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2721 |
_buffered = (__INST(buffered) == true); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2722 |
if (_buffered) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2723 |
__READING__(f); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2724 |
} |
2861 | 2725 |
|
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2726 |
if (extPtr) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2727 |
__READBYTES__(ret, f, extPtr+offs, cnt, _buffered); |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2728 |
} else { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2729 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2730 |
* on interrupt, anObject may be moved to another location. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2731 |
* So we pass anObject, and the offset to the __READBYTES_OBJ__ macro. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2732 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2733 |
offs += nInstBytes; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2734 |
__READBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered); |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
2735 |
} |
2861 | 2736 |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2737 |
if (ret > 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2738 |
pos = __INST(position); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2739 |
if (pos != nil) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2740 |
__INST(position) = __MKSMALLINT(__intVal(pos) + ret); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2741 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2742 |
RETURN (__MKSMALLINT(ret)); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2743 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2744 |
if (ret == 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2745 |
__INST(hitEOF) = true; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2746 |
} else /* ret < 0 */ { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2747 |
__INST(position) = nil; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2748 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2749 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
2750 |
} |
612 | 2751 |
} |
2752 |
bad: ; |
|
2753 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
2754 |
(hitEOF and:[self pastEnd isNil]) ifTrue:[^ 0]. |
612 | 2755 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
2756 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2757 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2758 |
" |
|
2759 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
2760 |
" |
|
2761 |
^ self primitiveFailed |
|
92 | 2762 |
! |
2763 |
||
612 | 2764 |
nextLong |
2765 |
"Read four bytes (msb-first) and return the value as a 32-bit signed Integer. |
|
2766 |
The returned value may be a LargeInteger. |
|
2767 |
(msb-first for compatibility with other smalltalks)" |
|
2768 |
||
2769 |
^ self nextUnsignedLongMSB:true |
|
2770 |
! |
|
2771 |
||
2772 |
nextLongMSB:msbFlag |
|
2773 |
"Read four bytes and return the value as a 32-bit signed Integer, |
|
2774 |
which may be a LargeInteger. |
|
2775 |
If msbFlag is true, value is read with most-significant byte first, |
|
2776 |
otherwise least-significant byte comes first. |
|
2777 |
A nil is returned, if EOF is hit before all 4 bytes have been read. |
|
2778 |
Works in both binary and text modes." |
|
2779 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2780 |
%{ |
612 | 2781 |
OBJ fp; |
2782 |
||
1133 | 2783 |
__INST(lastErrorNumber) = nil; |
2784 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 2785 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2786 |
) { |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2787 |
FILEPOINTER f; |
2264 | 2788 |
int ret; |
2789 |
int value; |
|
2790 |
char buffer[4]; |
|
2791 |
||
2792 |
f = __FILEVal(fp); |
|
2793 |
__READBYTES__(ret, f, buffer, 4, __INST(buffered) == true); |
|
2794 |
||
2795 |
if (ret == 4) { |
|
2796 |
if (__INST(position) != nil) { |
|
2797 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 4); |
|
2798 |
} |
|
2799 |
if (msbFlag == true) { |
|
2800 |
value = (buffer[0] & 0xFF); |
|
2801 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
2802 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
2803 |
value = (value << 8) | (buffer[3] & 0xFF); |
|
2804 |
} else { |
|
2805 |
value = (buffer[3] & 0xFF); |
|
2806 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
2807 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
2808 |
value = (value << 8) | (buffer[0] & 0xFF); |
|
2809 |
} |
|
2810 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) { |
|
2811 |
RETURN ( __MKSMALLINT(value)); |
|
2812 |
} |
|
2813 |
RETURN ( __MKLARGEINT(value) ); |
|
2814 |
} |
|
2815 |
||
2816 |
if (ret < 0) { |
|
2817 |
__INST(position) = nil; |
|
2818 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2819 |
} else /* ret == 0 */ { |
|
2820 |
__INST(hitEOF) = true; |
|
2821 |
} |
|
612 | 2822 |
} |
2823 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
2824 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 2825 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
2826 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2827 |
^ self readError. |
|
1079 | 2828 |
|
362 | 2829 |
! |
2830 |
||
612 | 2831 |
nextShortMSB:msbFlag |
2832 |
"Read two bytes and return the value as a 16-bit signed Integer. |
|
2833 |
If msbFlag is true, value is read with most-significant byte first, |
|
2834 |
otherwise least-significant byte comes first. |
|
2835 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
2836 |
Works in both binary and text modes." |
|
2837 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2838 |
%{ |
612 | 2839 |
OBJ fp; |
2840 |
||
1133 | 2841 |
__INST(lastErrorNumber) = nil; |
2842 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 2843 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2844 |
) { |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2845 |
FILEPOINTER f; |
2264 | 2846 |
int ret; |
2847 |
short value; |
|
2848 |
char buffer[2]; |
|
2849 |
||
2850 |
f = __FILEVal(fp); |
|
2851 |
__READBYTES__(ret, f, buffer, 2, __INST(buffered) == true); |
|
2852 |
||
2853 |
if (ret == 2) { |
|
2854 |
if (__INST(position) != nil) { |
|
2855 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 2); |
|
2856 |
} |
|
2857 |
if (msbFlag == true) { |
|
2858 |
value = ((buffer[0] & 0xFF) << 8) | (buffer[1] & 0xFF); |
|
2859 |
} else { |
|
2860 |
value = ((buffer[1] & 0xFF) << 8) | (buffer[0] & 0xFF); |
|
2861 |
} |
|
2862 |
RETURN (__MKSMALLINT(value)); |
|
2863 |
} |
|
2864 |
||
2865 |
if (ret < 0) { |
|
2866 |
__INST(position) = nil; |
|
2867 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2868 |
} else /* ret == 0 */ { |
|
2869 |
__INST(hitEOF) = true; |
|
2870 |
} |
|
612 | 2871 |
} |
2872 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
2873 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 2874 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
2875 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2876 |
^ self readError. |
|
2877 |
! |
|
2878 |
||
2879 |
nextUnsignedLongMSB:msbFlag |
|
2880 |
"Read four bytes and return the value as a 32-bit unsigned Integer, which may be |
|
2881 |
a LargeInteger. |
|
2882 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
2883 |
least-significant byte comes first. |
|
2884 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read. |
|
2885 |
Works in both binary and text modes." |
|
2886 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2887 |
%{ |
612 | 2888 |
OBJ fp; |
2889 |
||
1133 | 2890 |
__INST(lastErrorNumber) = nil; |
2891 |
if (((fp = __INST(filePointer)) != nil) |
|
3456
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2892 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2893 |
) { |
3456
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2894 |
FILEPOINTER f; |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2895 |
int ret; |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2896 |
unsigned INT value; |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2897 |
unsigned char buffer[4]; |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2898 |
|
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2899 |
f = __FILEVal(fp); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2900 |
__READBYTES__(ret, f, buffer, 4, __INST(buffered) == true); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2901 |
|
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2902 |
if (ret == 4) { |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2903 |
if (__INST(position) != nil) { |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2904 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 4); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2905 |
} |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2906 |
if (msbFlag == true) { |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2907 |
value = (buffer[0] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2908 |
value = (value << 8) | (buffer[1] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2909 |
value = (value << 8) | (buffer[2] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2910 |
value = (value << 8) | (buffer[3] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2911 |
} else { |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2912 |
value = (buffer[3] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2913 |
value = (value << 8) | (buffer[2] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2914 |
value = (value << 8) | (buffer[1] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2915 |
value = (value << 8) | (buffer[0] & 0xFF); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2916 |
} |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2917 |
#ifdef alpha64 |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2918 |
value &= 0xFFFFFFFF; |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2919 |
#endif |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2920 |
if (value <= _MAX_INT) { |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2921 |
RETURN ( __MKSMALLINT(value)); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2922 |
} |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2923 |
RETURN ( __MKULARGEINT(value) ); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2924 |
} |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2925 |
|
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2926 |
if (ret < 0) { |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2927 |
__INST(position) = nil; |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2928 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2929 |
} else /* ret == 0 */ { |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2930 |
__INST(hitEOF) = true; |
0cdf451360b9
oops - nextUnsignedLong:MSB: on alpha.
Claus Gittinger <cg@exept.de>
parents:
3445
diff
changeset
|
2931 |
} |
612 | 2932 |
} |
2933 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
2934 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 2935 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
2936 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2937 |
^ self readError. |
|
2938 |
! |
|
2939 |
||
2940 |
nextUnsignedShortMSB:msbFlag |
|
2941 |
"Read two bytes and return the value as a 16-bit unsigned Integer. |
|
1079 | 2942 |
If msbFlag is true, value is read with most-significant byte first, |
2943 |
otherwise least-significant byte comes first. |
|
612 | 2944 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
2945 |
Works in both binary and text modes." |
|
2946 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2947 |
%{ |
612 | 2948 |
OBJ fp; |
2949 |
||
1133 | 2950 |
__INST(lastErrorNumber) = nil; |
2951 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 2952 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2953 |
) { |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2954 |
FILEPOINTER f; |
2264 | 2955 |
int ret; |
2956 |
unsigned int value; |
|
2957 |
char buffer[2]; |
|
2958 |
||
2959 |
f = __FILEVal(fp); |
|
2960 |
__READBYTES__(ret, f, buffer, 2, __INST(buffered) == true); |
|
2961 |
||
2962 |
if (ret == 2) { |
|
2963 |
if (__INST(position) != nil) { |
|
2964 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 2); |
|
2965 |
} |
|
2966 |
if (msbFlag == true) { |
|
2967 |
value = ((buffer[0] & 0xFF) << 8) | (buffer[1] & 0xFF); |
|
2968 |
} else { |
|
2969 |
value = ((buffer[1] & 0xFF) << 8) | (buffer[0] & 0xFF); |
|
2970 |
} |
|
2971 |
RETURN (__MKSMALLINT(value)); |
|
2972 |
} |
|
2973 |
||
2974 |
if (ret < 0) { |
|
2975 |
__INST(position) = nil; |
|
2976 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2977 |
} else /* ret == 0 */ { |
|
2978 |
__INST(hitEOF) = true; |
|
2979 |
} |
|
612 | 2980 |
} |
2981 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
2982 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 2983 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
2984 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2985 |
^ self readError. |
|
2986 |
! |
|
2987 |
||
2988 |
nextWord |
|
2989 |
"in text-mode: |
|
2264 | 2990 |
read the alphaNumeric next word (i.e. up to non letter-or-digit). |
2991 |
return a string containing those characters. |
|
612 | 2992 |
in binary-mode: |
2264 | 2993 |
read two bytes (msb-first) and return the value as a 16-bit |
2994 |
unsigned Integer (for compatibility with other smalltalks)" |
|
612 | 2995 |
|
2996 |
binary ifTrue:[ |
|
2264 | 2997 |
^ self nextUnsignedShortMSB:true |
92 | 2998 |
]. |
612 | 2999 |
^ self nextAlphaNumericWord |
3000 |
! ! |
|
3001 |
||
3002 |
!ExternalStream methodsFor:'non homogenous writing'! |
|
3003 |
||
3004 |
nextPutByte:aByteValue |
|
3005 |
"write a byte. |
|
3006 |
Works in both binary and text modes." |
|
3007 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3008 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3009 |
FILEPOINTER f; |
612 | 3010 |
char c; |
3011 |
OBJ pos, fp; |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3012 |
int cnt, _buffered; |
612 | 3013 |
|
1133 | 3014 |
__INST(lastErrorNumber) = nil; |
3015 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 3016 |
&& (__INST(mode) != @symbol(readonly)) |
3017 |
&& __isSmallInteger(aByteValue) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3018 |
|
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3019 |
) { |
2264 | 3020 |
c = __intVal(aByteValue); |
3021 |
f = __FILEVal(fp); |
|
3022 |
if (_buffered = (__INST(buffered) == true)) { |
|
3023 |
__WRITING__(f) |
|
3024 |
} |
|
3025 |
__WRITEBYTE__(cnt, f, &c, _buffered); |
|
3026 |
if (cnt == 1) { |
|
3027 |
pos = __INST(position); |
|
3028 |
if (pos != nil) |
|
3029 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
3030 |
RETURN (self); |
|
3031 |
} |
|
3032 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
612 | 3033 |
} |
3034 |
%}. |
|
2331 | 3035 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
3036 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3037 |
self writeError. |
|
612 | 3038 |
! |
3039 |
||
3040 |
nextPutBytes:count from:anObject startingAt:start |
|
3041 |
"write count bytes from an object starting at index start. |
|
3042 |
return the number of bytes written - which could be 0. |
|
3043 |
The object must have non-pointer indexed instvars |
|
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3044 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3045 |
or an externalBytes object (with known size). |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3046 |
|
612 | 3047 |
Use with care - non object oriented i/o. |
3048 |
Warning: in general, you cannot use this method to pass data to other |
|
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3049 |
architectures (unless you prepared the buffer with care), |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3050 |
since it does not care for byte order or float representation." |
612 | 3051 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3052 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3053 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3054 |
int cnt, len, offs, ret; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3055 |
int objSize, nInstVars, nInstBytes, _buffered; |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3056 |
char *extPtr; |
612 | 3057 |
OBJ oClass, pos, fp; |
3058 |
||
1133 | 3059 |
__INST(lastErrorNumber) = nil; |
3060 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 3061 |
&& (__INST(mode) != @symbol(readonly)) |
3062 |
&& __bothSmallInteger(count, start) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3063 |
) { |
2264 | 3064 |
len = __intVal(count); |
3065 |
offs = __intVal(start) - 1; |
|
3066 |
f = __FILEVal(fp); |
|
3067 |
||
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3068 |
oClass = __Class(anObject); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3069 |
if (oClass == ExternalBytes) { |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3070 |
OBJ sz; |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3071 |
|
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3072 |
nInstBytes = 0; |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3073 |
extPtr = (char *)__externalBytesAddress(anObject); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3074 |
sz = __externalBytesSize(anObject); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3075 |
if (__isSmallInteger(sz)) { |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3076 |
objSize = __intVal(sz); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3077 |
} else { |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3078 |
objSize = 0; /* unknown */ |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3079 |
} |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3080 |
} else { |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3081 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3082 |
case BYTEARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3083 |
case WORDARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3084 |
case LONGARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3085 |
case SWORDARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3086 |
case SLONGARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3087 |
case FLOATARRAY: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3088 |
case DOUBLEARRAY: |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3089 |
break; |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3090 |
default: |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3091 |
goto bad; |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3092 |
} |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3093 |
extPtr = (char *)0; |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3094 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3095 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3096 |
objSize = __Size(anObject) - nInstBytes; |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3097 |
} |
2264 | 3098 |
if ( (offs >= 0) && (len >= 0) && (objSize >= (len + offs)) ) { |
3099 |
if (_buffered = (__INST(buffered) == true)) { |
|
3100 |
__WRITING__(f) |
|
3101 |
} |
|
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3102 |
|
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3103 |
if (extPtr) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3104 |
__WRITEBYTES__(cnt, f, extPtr+offs, len, _buffered); |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3105 |
} else { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3106 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3107 |
* on interrupt, anObject may be moved to another location. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3108 |
* So we pass anObject, and the offset to the __WRITEBYTES_OBJ__ macro. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3109 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3110 |
offs += nInstBytes; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3111 |
__WRITEBYTES_OBJ__(cnt, f, anObject, offs, len, _buffered); |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3112 |
} |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3113 |
|
2264 | 3114 |
if (cnt >= 0) { |
3115 |
pos = __INST(position); |
|
3116 |
if (pos != nil) |
|
3117 |
__INST(position) = __MKSMALLINT(__intVal(pos) + cnt); |
|
3118 |
RETURN ( __MKSMALLINT(cnt) ); |
|
3119 |
} |
|
3120 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3121 |
} |
|
612 | 3122 |
} |
3123 |
bad: ; |
|
3124 |
%}. |
|
3125 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
3126 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3127 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
3128 |
^ self primitiveFailed |
|
3129 |
! |
|
3130 |
||
3131 |
nextPutLong:aNumber MSB:msbFlag |
|
3132 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
3133 |
true, data is written most-significant byte first; otherwise least |
|
3134 |
first. |
|
3135 |
Works in both binary and text modes." |
|
3136 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3137 |
%{ |
612 | 3138 |
int num; |
3139 |
char bytes[4]; |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3140 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3141 |
int cnt, _buffered; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3142 |
OBJ fp, pos; |
612 | 3143 |
|
1133 | 3144 |
__INST(lastErrorNumber) = nil; |
3145 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 3146 |
&& (__INST(mode) != @symbol(readonly)) |
3147 |
&& __isSmallInteger(aNumber) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3148 |
) { |
2264 | 3149 |
num = __intVal(aNumber); |
3150 |
if (msbFlag == true) { |
|
3151 |
bytes[0] = (num >> 24) & 0xFF; |
|
3152 |
bytes[1] = (num >> 16) & 0xFF; |
|
3153 |
bytes[2] = (num >> 8) & 0xFF; |
|
3154 |
bytes[3] = num & 0xFF; |
|
3155 |
} else { |
|
3156 |
bytes[3] = (num >> 24) & 0xFF; |
|
3157 |
bytes[2] = (num >> 16) & 0xFF; |
|
3158 |
bytes[1] = (num >> 8) & 0xFF; |
|
3159 |
bytes[0] = num & 0xFF; |
|
3160 |
} |
|
3161 |
||
3162 |
f = __FILEVal(fp); |
|
3163 |
if (_buffered = (__INST(buffered) == true)) { |
|
3164 |
__WRITING__(f) |
|
3165 |
} |
|
3166 |
__WRITEBYTES__(cnt, f, bytes, 4, _buffered); |
|
3167 |
||
3168 |
if (cnt == 4) { |
|
3169 |
if ((pos = __INST(position)) != nil) { |
|
3170 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 4); |
|
3171 |
} |
|
3172 |
RETURN ( self ); |
|
3173 |
} |
|
3174 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
612 | 3175 |
} |
3176 |
%}. |
|
2331 | 3177 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
3178 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3179 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
612 | 3180 |
|
3181 |
aNumber isInteger ifTrue:[ |
|
2331 | 3182 |
^ super nextPutLong:aNumber MSB:msbFlag |
92 | 3183 |
]. |
612 | 3184 |
self argumentMustBeInteger |
3185 |
! |
|
3186 |
||
3187 |
nextPutShort:aNumber MSB:msbFlag |
|
3188 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
3189 |
true, data is written most-significant byte first; otherwise least |
|
3190 |
first. |
|
3191 |
Works in both binary and text modes." |
|
3192 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3193 |
%{ |
612 | 3194 |
int num; |
3195 |
char bytes[2]; |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3196 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3197 |
int cnt, _buffered; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3198 |
OBJ fp, pos; |
612 | 3199 |
|
1133 | 3200 |
__INST(lastErrorNumber) = nil; |
3201 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 3202 |
&& (__INST(mode) != @symbol(readonly)) |
3203 |
&& __isSmallInteger(aNumber) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3204 |
) { |
2264 | 3205 |
num = __intVal(aNumber); |
3206 |
if (msbFlag == true) { |
|
3207 |
bytes[0] = (num >> 8) & 0xFF; |
|
3208 |
bytes[1] = num & 0xFF; |
|
3209 |
} else { |
|
3210 |
bytes[1] = (num >> 8) & 0xFF; |
|
3211 |
bytes[0] = num & 0xFF; |
|
3212 |
} |
|
3213 |
||
3214 |
f = __FILEVal(fp); |
|
3215 |
if (_buffered = (__INST(buffered) == true)) { |
|
3216 |
__WRITING__(f) |
|
3217 |
} |
|
3218 |
__WRITEBYTES__(cnt, f, bytes, 2, _buffered); |
|
3219 |
||
3220 |
if (cnt == 2) { |
|
3221 |
if ((pos = __INST(position)) != nil) { |
|
3222 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 2); |
|
3223 |
} |
|
3224 |
RETURN ( self ); |
|
3225 |
} |
|
3226 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
612 | 3227 |
} |
3228 |
%}. |
|
2331 | 3229 |
lastErrorNumber notNil ifTrue:[^ self writeError. ^ self]. |
3230 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3231 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
612 | 3232 |
self argumentMustBeInteger |
10 | 3233 |
! ! |
612 | 3234 |
|
3235 |
!ExternalStream methodsFor:'private'! |
|
3236 |
||
3237 |
clearEOF |
|
3238 |
hitEOF := false |
|
3239 |
! |
|
3240 |
||
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3241 |
connectTo:aFileDescriptor withMode:openmode |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3242 |
"connect a fileDescriptor; openmode is the string defining the way to open. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3243 |
This can be used to connect an extrnally provided fileDescriptor (from |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3244 |
primitive code) or a pipeFileDescriptor (as returned by makePipe) to |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3245 |
a Stream object. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3246 |
The openMode ('r', 'w' etc.) must match the mode in which |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3247 |
the fileDescriptor was originally opened (otherwise i/o errors will be reported later)." |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3248 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3249 |
|retVal| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3250 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3251 |
filePointer notNil ifTrue:[^ self errorOpen]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3252 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3253 |
FILEPOINTER f; |
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3254 |
OBJ fp; |
2765 | 3255 |
FILE *fdopen(); |
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3256 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3257 |
if (__isSmallInteger(aFileDescriptor) |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3258 |
&& (__qClass(openmode)== @global(String))) { |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3259 |
f = (FILEPOINTER) fdopen(__intVal(aFileDescriptor), (char *)__stringVal(openmode)); |
2264 | 3260 |
if (f == NULL) { |
3261 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3262 |
__INST(position) = nil; |
|
3263 |
} else { |
|
3420 | 3264 |
__INST(filePointer) = fp = __MKFILEPOINTER(f); __STORE(self, fp); |
2264 | 3265 |
__INST(position) = __MKSMALLINT(1); |
3266 |
retVal = self; |
|
3267 |
} |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3268 |
} |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3269 |
%}. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3270 |
retVal notNil ifTrue:[ |
2264 | 3271 |
buffered := true. "default is buffered" |
3272 |
Lobby register:self |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3273 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3274 |
lastErrorNumber notNil ifTrue:[ |
2264 | 3275 |
" |
3276 |
the open failed for some reason ... |
|
3277 |
" |
|
3278 |
^ self openError |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3279 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3280 |
^ retVal |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3281 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
3282 |
|
2963 | 3283 |
open:aPath withMode:mode |
3284 |
"low level open; opens the file/device and sets the filePointer instance |
|
3285 |
variable. Careful: this does not care for any other state." |
|
3286 |
||
3287 |
|ok| |
|
3288 |
||
3289 |
ok := false. |
|
3290 |
%{ |
|
3291 |
FILE *f; |
|
3292 |
OBJ fp; |
|
3293 |
||
3294 |
if (__isString(aPath)) { |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3295 |
__BEGIN_INTERRUPTABLE__ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3296 |
do { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3297 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(mode)); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3298 |
} while ((f == NULL) && (errno == EINTR)); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3299 |
__END_INTERRUPTABLE__ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3300 |
if (f == NULL) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3301 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3302 |
} else { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3303 |
__INST(filePointer) = fp = __MKFILEPOINTER(f); __STORE(self, fp); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3304 |
ok = true; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3305 |
} |
2963 | 3306 |
} |
3307 |
%}. |
|
3308 |
ok ifFalse:[ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3309 |
" |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3310 |
the open failed for some reason ... |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3311 |
" |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3312 |
^ self openError |
2963 | 3313 |
]. |
3314 |
Lobby register:self. |
|
3315 |
! |
|
3316 |
||
612 | 3317 |
reOpen |
3318 |
"sent after snapin to reopen streams. |
|
3319 |
cannot reopen here since I am abstract and have no device knowledge" |
|
3320 |
||
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
3321 |
self class name errorPrint. ' [warning]: automatic reOpen not supported - stream closed' errorPrintCR. |
612 | 3322 |
filePointer := nil. |
3323 |
Lobby unregister:self. |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
3324 |
|
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2120
diff
changeset
|
3325 |
"Modified: 10.1.1997 / 17:50:44 / cg" |
612 | 3326 |
! |
3327 |
||
3328 |
setLastError:aNumber |
|
3329 |
lastErrorNumber := aNumber |
|
3330 |
! ! |
|
3331 |
||
3332 |
!ExternalStream methodsFor:'queries'! |
|
3333 |
||
3334 |
isBinary |
|
3335 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
3336 |
The default when created is false." |
|
3337 |
||
3338 |
^ binary |
|
3339 |
! |
|
3340 |
||
3341 |
isExternalStream |
|
3342 |
"return true, if the receiver is some kind of externalStream; |
|
3343 |
true is returned here - the method redefined from Object." |
|
3344 |
||
3345 |
^ true |
|
3346 |
! |
|
3347 |
||
3863 | 3348 |
isOpen |
3349 |
"return true, if this stream is open" |
|
3350 |
||
3351 |
^ filePointer notNil |
|
3352 |
! |
|
3353 |
||
612 | 3354 |
isReadable |
3355 |
"return true, if this stream can be read from" |
|
3356 |
||
3357 |
^ (mode ~~ #writeonly) |
|
3358 |
! |
|
3359 |
||
3360 |
isWritable |
|
3361 |
"return true, if this stream can be written to" |
|
3362 |
||
3363 |
^ (mode ~~ #readonly) |
|
3364 |
! ! |
|
3365 |
||
3366 |
!ExternalStream methodsFor:'reading'! |
|
3367 |
||
3368 |
next |
|
3369 |
"return the next element; advance read position. |
|
3370 |
In binary mode, an integer is returned, otherwise a character. |
|
3371 |
If there are no more elements, nil is returned." |
|
1 | 3372 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3373 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3374 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3375 |
int ret, _buffered; |
612 | 3376 |
OBJ pos, fp; |
3377 |
unsigned char ch; |
|
3378 |
||
1133 | 3379 |
__INST(lastErrorNumber) = nil; |
3380 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 3381 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3382 |
) { |
2264 | 3383 |
f = __FILEVal(fp); |
3384 |
||
3385 |
_buffered = (__INST(buffered) == true); |
|
3386 |
if (_buffered) { |
|
3387 |
__READING__(f) |
|
3388 |
} |
|
3389 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3390 |
||
3391 |
if (ret > 0) { |
|
3392 |
pos = __INST(position); |
|
3393 |
if (__isSmallInteger(pos)) { |
|
3394 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
3395 |
} else { |
|
3396 |
__INST(position) = nil; |
|
3397 |
} |
|
3398 |
if (__INST(binary) == true) { |
|
3399 |
RETURN ( __MKSMALLINT(ch) ); |
|
3400 |
} |
|
3401 |
RETURN ( __MKCHARACTER(ch) ); |
|
3402 |
} |
|
3403 |
||
3404 |
__INST(position) = nil; |
|
3405 |
if (ret < 0) { |
|
3406 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3407 |
} else /* ret == 0 */ { |
|
3408 |
__INST(hitEOF) = true; |
|
3409 |
} |
|
612 | 3410 |
} |
3411 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
3412 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 3413 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
3414 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2331 | 3415 |
^ self errorWriteOnly |
612 | 3416 |
! |
3417 |
||
3418 |
next:count |
|
3419 |
"return the next count elements of the stream as a collection. |
|
3420 |
Redefined to return a String or ByteArray instead of the default: Array." |
|
3421 |
||
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3422 |
|coll nRead| |
612 | 3423 |
|
3424 |
binary ifTrue:[ |
|
2264 | 3425 |
coll := ByteArray uninitializedNew:count |
612 | 3426 |
] ifFalse:[ |
2264 | 3427 |
coll := String new:count |
612 | 3428 |
]. |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3429 |
nRead := self nextBytes:count into:coll startingAt:1. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3430 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3431 |
"/ for readStream protocol compatibility, |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3432 |
"/ we must raise an exception here. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3433 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3434 |
nRead < count ifTrue:[ |
2264 | 3435 |
^ self pastEnd |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3436 |
]. |
612 | 3437 |
^ coll |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3438 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3439 |
"Modified: 11.1.1997 / 17:44:17 / cg" |
612 | 3440 |
! |
3441 |
||
3442 |
peek |
|
3443 |
"return the element to be read next without advancing read position. |
|
3444 |
In binary mode, an integer is returned, otherwise a character. |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3445 |
If there are no more elements, nil is returned." |
612 | 3446 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3447 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3448 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3449 |
unsigned char c; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3450 |
int ret, _buffered; |
216 | 3451 |
OBJ fp; |
3672
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3452 |
OBJ ra; |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3453 |
|
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3454 |
if ((ra = __INST(readAhead)) != nil) { |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3455 |
if (__INST(binary) == true) { |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3456 |
RETURN ( ra ); |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3457 |
} |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3458 |
c = __intVal(ra); |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
3459 |
RETURN ( __MKCHARACTER(c) ); |
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
3460 |
} |
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
3461 |
|
1133 | 3462 |
__INST(lastErrorNumber) = nil; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3463 |
|
1133 | 3464 |
if (((fp = __INST(filePointer)) != nil) |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3465 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3466 |
) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3467 |
f = __FILEVal(fp); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3468 |
_buffered = (__INST(buffered) == true); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3469 |
if (_buffered) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3470 |
__READING__(f) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3471 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3472 |
__READBYTE__(ret, f, &c, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3473 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3474 |
if (ret > 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3475 |
__UNGETC__(c, f, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3476 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3477 |
if (__INST(binary) == true) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3478 |
RETURN ( __MKSMALLINT(c) ); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3479 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3480 |
RETURN ( __MKCHARACTER(c) ); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3481 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3482 |
if (ret < 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3483 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3484 |
} else /* ret == 0 */ { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3485 |
__INST(hitEOF) = true; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3486 |
} |
1 | 3487 |
} |
159 | 3488 |
%}. |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3489 |
hitEOF ifTrue:[^ self pastEnd]. |
216 | 3490 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
1 | 3491 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
159 | 3492 |
^ self errorWriteOnly |
1295 | 3493 |
! |
3494 |
||
3495 |
upToEnd |
|
3496 |
"return a collection of the elements up-to the end. |
|
3497 |
Return nil if the stream-end is reached before." |
|
3498 |
||
3499 |
|answerStream ch| |
|
3500 |
||
3501 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
3502 |
[self atEnd] whileFalse:[ |
|
2264 | 3503 |
ch := self next. |
3504 |
ch notNil ifTrue:[ |
|
3505 |
answerStream nextPut:ch |
|
3506 |
] |
|
1295 | 3507 |
]. |
3508 |
^ answerStream contents |
|
3509 |
||
3510 |
"Created: 25.4.1996 / 14:40:31 / cg" |
|
612 | 3511 |
! ! |
3512 |
||
3513 |
!ExternalStream methodsFor:'reimplemented for speed'! |
|
159 | 3514 |
|
3515 |
nextAlphaNumericWord |
|
379 | 3516 |
"read the next word (i.e. up to non letter-or-digit) after first |
3517 |
skipping any whiteSpace. |
|
3518 |
Return a string containing those characters. |
|
3519 |
There is a limit of 1023 characters in the word - if longer, |
|
3520 |
it is truncated." |
|
159 | 3521 |
|
3522 |
%{ /* STACK: 2000 */ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3523 |
FILEPOINTER f; |
159 | 3524 |
int len; |
3525 |
char buffer[1024]; |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3526 |
unsigned char ch; |
159 | 3527 |
int cnt = 0; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3528 |
int ret, _buffered; |
216 | 3529 |
OBJ fp; |
3530 |
||
1133 | 3531 |
if (((fp = __INST(filePointer)) != nil) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3532 |
&& (__INST(mode) != @symbol(writeonly)) |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3533 |
) { |
2264 | 3534 |
f = __FILEVal(fp); |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3535 |
_buffered = (__INST(buffered) == true); |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3536 |
if (_buffered) { |
2264 | 3537 |
__READING__(f) |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3538 |
} |
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
3539 |
|
2264 | 3540 |
/* |
3541 |
* skip whiteSpace first ... |
|
3542 |
*/ |
|
3543 |
do { |
|
3544 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3545 |
if (ret > 0) |
|
3546 |
cnt++; |
|
3547 |
else |
|
3548 |
break; |
|
3549 |
} while ( |
|
379 | 3550 |
#ifndef NON_ASCII |
2264 | 3551 |
(ch < ' ') || |
379 | 3552 |
#endif |
2264 | 3553 |
(ch == ' ' ) || (ch == '\t') || (ch == '\r') |
3554 |
|| (ch == '\n') || (ch == 0x0b)); |
|
3555 |
||
3556 |
len = 0; |
|
3557 |
while (ret > 0 && |
|
3558 |
(((ch >= 'a') && (ch <= 'z')) || |
|
3559 |
((ch >= 'A') && (ch <= 'Z')) || |
|
3560 |
((ch >= '0') && (ch <= '9'))) |
|
3561 |
) { |
|
3562 |
buffer[len++] = ch; |
|
3563 |
if (len >= (sizeof(buffer)-1)) { |
|
3564 |
/* emergency */ |
|
3565 |
break; |
|
3566 |
} |
|
3567 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3568 |
if (ret > 0) |
|
3569 |
cnt++; |
|
3570 |
else |
|
3571 |
break; |
|
3572 |
} |
|
3573 |
||
3574 |
if (ret <= 0) { |
|
3575 |
if (ret < 0) { |
|
3576 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3577 |
goto err; |
|
3578 |
} |
|
3579 |
else if (ret == 0) |
|
3580 |
__INST(hitEOF) = true; |
|
3581 |
} else { |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3582 |
__UNGETC__(ch, f, _buffered); |
2264 | 3583 |
cnt--; |
3584 |
} |
|
3585 |
||
3586 |
if (__INST(position) != nil) { |
|
3587 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + cnt); |
|
3588 |
} |
|
3589 |
if (len != 0) { |
|
3590 |
buffer[len] = '\0'; |
|
3591 |
RETURN ( __MKSTRING_L(buffer, len) ); |
|
3592 |
} |
|
159 | 3593 |
} |
3594 |
err: ; |
|
3595 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
3596 |
hitEOF ifTrue:[^ self pastEnd]. |
159 | 3597 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
3598 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3599 |
^ self errorWriteOnly |
|
1 | 3600 |
! |
3601 |
||
3602 |
nextChunk |
|
3603 |
"return the next chunk, i.e. all characters up to the next |
|
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
3604 |
exclamation mark. Within the chunk, exclamation marks have to be doubled - |
216 | 3605 |
except within primitive code (this exception was added to make it easier |
3606 |
to edit primitive code with external editors). This means, that other |
|
3607 |
Smalltalks cannot always read chunks containing primitive code - but that |
|
3608 |
doesnt really matter, since C-primitives are an ST/X feature anyway. |
|
3609 |
Reimplemented here for more speed." |
|
3610 |
||
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3611 |
|retVal outOfMemory buffer| |
1 | 3612 |
|
159 | 3613 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
3614 |
binary ifTrue:[^ self errorBinary]. |
|
3615 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
216 | 3616 |
|
2854
3c8bf66e4e7d
give nextChunk a bit more stack
Claus Gittinger <cg@exept.de>
parents:
2766
diff
changeset
|
3617 |
%{ /* STACK: 12000 */ |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3618 |
/* |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3619 |
* the main trick (and a bit of complication) here |
2874
020627074c9e
oops - nextChunk could access invalid memory due
Claus Gittinger <cg@exept.de>
parents:
2861
diff
changeset
|
3620 |
* is to read the first 8k into a stack-buffer. |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3621 |
* Since most chunks fit into that, |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3622 |
* this avoids creating lots of garbage for thos small chunks. |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3623 |
*/ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3624 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3625 |
int nread = 0, done = 0; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3626 |
unsigned char c, lastC, peekC; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3627 |
int ret; |
1823 | 3628 |
unsigned char *bufferPtr = (unsigned char *)0; |
2874
020627074c9e
oops - nextChunk could access invalid memory due
Claus Gittinger <cg@exept.de>
parents:
2861
diff
changeset
|
3629 |
unsigned char fastBuffer[8000]; |
1 | 3630 |
REGISTER int index; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3631 |
int currSize, fastFlag, _buffered; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3632 |
int atBeginOfLine = 1, inPrimitive = 0; |
2920 | 3633 |
int haveCR = 0, haveNL = 0; |
216 | 3634 |
|
1133 | 3635 |
__INST(lastErrorNumber) = nil; |
3636 |
f = __FILEVal(__INST(filePointer)); |
|
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
3637 |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3638 |
_buffered = (__INST(buffered) == true); |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3639 |
if (_buffered) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3640 |
__READING__(f) |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3641 |
} |
159 | 3642 |
|
1 | 3643 |
/* |
3644 |
* skip spaces |
|
3645 |
*/ |
|
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
3646 |
c = '\n'; |
1 | 3647 |
while (! done) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3648 |
lastC = c; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3649 |
__READBYTE__(ret, f, &c, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3650 |
if (ret <= 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3651 |
goto err; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3652 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3653 |
nread++; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3654 |
atBeginOfLine = 0; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3655 |
switch (c) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3656 |
case '\n': |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3657 |
case ' ': |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3658 |
case '\t': |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3659 |
case '\r': |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3660 |
case '\b': |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3661 |
case '\014': |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3662 |
break; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3663 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3664 |
default: |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3665 |
atBeginOfLine = (lastC == '\n'); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3666 |
done = 1; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3667 |
break; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3668 |
} |
1 | 3669 |
} |
3670 |
||
3671 |
/* |
|
3672 |
* read chunk into a buffer |
|
3673 |
*/ |
|
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3674 |
bufferPtr = fastBuffer; fastFlag = 1; |
216 | 3675 |
currSize = sizeof(fastBuffer); |
362 | 3676 |
|
1 | 3677 |
index = 0; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3678 |
for (;;) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3679 |
__READBYTE__(ret, f, &peekC, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3680 |
if ((c == '%') && atBeginOfLine && ret > 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3681 |
if (peekC == '{') { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3682 |
inPrimitive = 1; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3683 |
} else if (peekC == '}') { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3684 |
inPrimitive = 0; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3685 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3686 |
} else if (c == '!' && !inPrimitive) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3687 |
if (ret > 0 && peekC == '!') { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3688 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3689 |
* convert double-! to a single ! |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3690 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3691 |
__READBYTE__(ret, f, &peekC, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3692 |
if (ret > 0) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3693 |
nread++; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3694 |
} else { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3695 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3696 |
* End of chunk, push back lookahead character |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3697 |
* and leave loop. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3698 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3699 |
if (ret > 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3700 |
__UNGETC__(peekC, f, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3701 |
} else |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3702 |
ret = 1; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3703 |
break; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3704 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3705 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3706 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3707 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3708 |
* do we have to resize the buffer ? |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3709 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3710 |
if ((index+2) >= currSize) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3711 |
OBJ newBuffer; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3712 |
unsigned char *nbp; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3713 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3714 |
newBuffer = __MKEMPTYSTRING(currSize * 2); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3715 |
if (!fastFlag) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3716 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3717 |
* refetch - buffer could be moved by GC |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3718 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3719 |
bufferPtr = __stringVal(buffer); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3720 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3721 |
if (newBuffer == nil) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3722 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3723 |
* mhmh - chunk seems to be very big .... |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3724 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3725 |
outOfMemory = true; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3726 |
goto err; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3727 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3728 |
nbp = __stringVal(newBuffer); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3729 |
bcopy(bufferPtr, nbp, index); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3730 |
bufferPtr = nbp; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3731 |
bufferPtr[index] = '\0'; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3732 |
buffer = newBuffer; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3733 |
fastFlag = 0; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3734 |
currSize = currSize * 2; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3735 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3736 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3737 |
if (!fastFlag) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3738 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3739 |
* old buffer may have moved (interrupt while reading) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3740 |
* - refetch pointer |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3741 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3742 |
bufferPtr = __stringVal(buffer); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3743 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3744 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3745 |
/* |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3746 |
* filter out cr-nl; make it nl |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3747 |
*/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3748 |
if (c == '\n') { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3749 |
haveNL = 1; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3750 |
if (index > 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3751 |
if (bufferPtr[index-1] == '\r') { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3752 |
index--; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3753 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3754 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3755 |
} else { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3756 |
if (c == '\r') { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3757 |
haveCR = 1; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3758 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3759 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3760 |
bufferPtr[index++] = c; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3761 |
if (ret <= 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3762 |
if (ret == 0) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3763 |
break; /* End of chunk reached */ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3764 |
goto err; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3765 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3766 |
nread++; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3767 |
atBeginOfLine = (c == '\n'); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3768 |
c = peekC; |
1 | 3769 |
} |
92 | 3770 |
|
1 | 3771 |
/* |
2920 | 3772 |
* care for CR-limited files. |
3773 |
* Replace CR's by NL's |
|
3774 |
*/ |
|
3775 |
if (!haveNL && haveCR) { |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3776 |
int i; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3777 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3778 |
for (i=index-1; i>=0; i--) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3779 |
if (bufferPtr[i] == '\r') { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3780 |
bufferPtr[i] = '\n'; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3781 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3782 |
} |
2920 | 3783 |
} |
3784 |
||
3785 |
/* |
|
1 | 3786 |
* make it a string |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3787 |
* be careful here - allocating a new string may make the |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3788 |
* existing buffer move around. Need to check if copying from |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3789 |
* fast (C) buffer or from real (ST) buffer. |
1 | 3790 |
*/ |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3791 |
if (fastFlag) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3792 |
retVal = __MKSTRING_L(bufferPtr, index); |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
3793 |
} else { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3794 |
retVal = __MKSTRING_ST_L(buffer, index); |
216 | 3795 |
} |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3796 |
|
1478
aae07192b7d1
labels in empty statements need a semi
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
3797 |
err: ; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3798 |
if (ret <= 0) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3799 |
if (ret == 0) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3800 |
__INST(hitEOF) = true; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3801 |
else /* ret < 0 */ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3802 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3803 |
} |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3804 |
if (__INST(position) != nil) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3805 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + nread); |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3806 |
} |
159 | 3807 |
%}. |
370 | 3808 |
retVal isNil ifTrue:[ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3809 |
"/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3810 |
"/ arrive here with retVal==nil either on error or premature EOF |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3811 |
"/ or if running out of malloc-memory |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3812 |
"/ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3813 |
hitEOF ifTrue:[^ self pastEnd]. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3814 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3815 |
outOfMemory == true ifTrue:[ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3816 |
" |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3817 |
buffer memory allocation failed. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3818 |
When we arrive here, there was no memory available for the |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3819 |
chunk. (seems to be too big of a chunk ...) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3820 |
Bad luck - you should increase the ulimit and/or swap space on your machine. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3821 |
" |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3822 |
^ ObjectMemory allocationFailureSignal raise. |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3823 |
] |
216 | 3824 |
]. |
1 | 3825 |
^ retVal |
612 | 3826 |
! |
3827 |
||
3828 |
nextMatchFor:anObject |
|
3829 |
"skip all objects up-to and including anObject, return anObject on success, |
|
3830 |
nil if end-of-file is reached before. The next read operation will return |
|
3831 |
the element after anObject. |
|
3832 |
Only single byte characters are currently supported." |
|
3833 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3834 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3835 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3836 |
int ret, peekValue; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3837 |
char c; |
612 | 3838 |
OBJ fp; |
3839 |
||
1133 | 3840 |
__INST(lastErrorNumber) = nil; |
3841 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 3842 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3843 |
) { |
2264 | 3844 |
if ((__INST(binary) == true) && __isSmallInteger(anObject)) { |
3845 |
peekValue = __intVal(anObject) & 0xFF; |
|
3846 |
} else { |
|
3847 |
if ((__INST(binary) != true) && __isCharacter(anObject)) { |
|
3848 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
|
3849 |
} else { |
|
3850 |
peekValue = -1; |
|
3851 |
} |
|
3852 |
} |
|
3853 |
||
3854 |
if (peekValue >= 0) { |
|
3855 |
int _buffered; |
|
3856 |
||
3857 |
__INST(position) = nil; |
|
3858 |
f = __FILEVal(fp); |
|
3859 |
||
3860 |
if (_buffered = (__INST(buffered) == true)) { |
|
3861 |
__READING__(f) |
|
3862 |
} |
|
3863 |
||
3864 |
for (;;) { |
|
3865 |
__READBYTE__(ret, f, &c, _buffered); |
|
3866 |
if (ret <= 0) { |
|
3867 |
if (ret < 0) |
|
3868 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3869 |
else |
|
3870 |
__INST(hitEOF) = true; |
|
3871 |
break; |
|
3872 |
} |
|
3873 |
if (c == peekValue) { |
|
3874 |
RETURN (anObject); |
|
3875 |
} |
|
3876 |
} |
|
3877 |
} |
|
612 | 3878 |
} |
3879 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
3880 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 3881 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
3882 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3883 |
^ super nextMatchFor:anObject |
|
3884 |
! |
|
3885 |
||
3886 |
peekFor:anObject |
|
3887 |
"return true and move past next element, if next == something. |
|
3888 |
Otherwise, stay and return false. False is also returned |
|
3889 |
when EOF is encountered. |
|
3890 |
The argument must be an integer if in binary, a character if in |
|
3891 |
text mode; only single byte characters are currently supported." |
|
3892 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3893 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3894 |
FILEPOINTER f; |
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
3895 |
unsigned char c; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
3896 |
int ret, peekValue, _buffered; |
612 | 3897 |
OBJ fp; |
3898 |
||
1133 | 3899 |
__INST(lastErrorNumber) = nil; |
3900 |
if (((fp = __INST(filePointer)) != nil) |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3901 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3902 |
) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3903 |
if (__INST(binary) == true) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3904 |
if (__isSmallInteger(anObject)) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3905 |
peekValue = __intVal(anObject) & 0xFF; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3906 |
} else { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3907 |
goto bad; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3908 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3909 |
} else { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3910 |
if (__isCharacter(anObject)) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3911 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3912 |
} else { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3913 |
goto bad; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3914 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3915 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3916 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3917 |
f = __FILEVal(fp); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3918 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3919 |
_buffered = (__INST(buffered) == true); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3920 |
if (_buffered) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3921 |
__READING__(f) |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3922 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3923 |
__READBYTE__(ret, f, &c, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3924 |
if (ret > 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3925 |
if (c == peekValue) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3926 |
OBJ pos; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3927 |
|
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3928 |
if ((pos = __INST(position)) != nil) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3929 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3930 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3931 |
RETURN (true); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3932 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3933 |
__UNGETC__(c, f, _buffered); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3934 |
RETURN (false); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3935 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3936 |
if (ret < 0) { |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3937 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3938 |
} else /* ret == 0 */ |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3939 |
__INST(hitEOF) = true; |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3940 |
} |
612 | 3941 |
bad: ; |
3942 |
%}. |
|
3943 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
3944 |
hitEOF ifTrue:[self pastEnd. ^ false]. |
612 | 3945 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
3946 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3947 |
^ super peekFor:anObject |
|
3948 |
! |
|
3949 |
||
3950 |
skipLine |
|
3951 |
"read the next line (characters up to newline) skip only; |
|
3952 |
return nil if EOF reached, self otherwise. |
|
3953 |
Not allowed in binary mode." |
|
3954 |
||
3955 |
%{ /* STACK:2000 */ |
|
3956 |
||
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3957 |
FILEPOINTER f; |
612 | 3958 |
OBJ fp; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3959 |
int ret, _buffered; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3960 |
char c; |
612 | 3961 |
|
1133 | 3962 |
__INST(lastErrorNumber) = nil; |
3963 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 3964 |
&& (__INST(mode) != @symbol(writeonly)) |
3965 |
&& (__INST(binary) != true) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3966 |
) { |
2264 | 3967 |
f = __FILEVal(fp); |
3968 |
if (_buffered = (__INST(buffered) == true)) { |
|
3969 |
__READING__(f) |
|
3970 |
} |
|
3971 |
for (;;) { |
|
3972 |
__READBYTE__(ret, f, &c, _buffered); |
|
3973 |
if (ret > 0) { |
|
3974 |
if (c == '\n') |
|
3975 |
RETURN(self) |
|
3976 |
} else |
|
3977 |
break; |
|
3978 |
} |
|
3979 |
if (ret < 0) { |
|
3980 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3981 |
} else /* ret == 0 */ { |
|
3982 |
__INST(hitEOF) = true; |
|
3983 |
} |
|
612 | 3984 |
} |
3985 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
3986 |
hitEOF ifTrue:[^ self pastEnd]. |
2331 | 3987 |
lastErrorNumber notNil ifTrue:[self readError. ^ self]. |
3988 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3989 |
binary ifTrue:[self errorBinary. ^ self]. |
|
3990 |
self errorWriteOnly |
|
612 | 3991 |
! |
3992 |
||
3993 |
skipSeparators |
|
3994 |
"skip all whitespace; next will return next non-white-space character |
|
3995 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
3996 |
- reimplemented for speed" |
|
3997 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3998 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3999 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4000 |
char c; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
4001 |
int ret, _buffered; |
612 | 4002 |
OBJ fp; |
4003 |
||
1133 | 4004 |
__INST(lastErrorNumber) = nil; |
4005 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 4006 |
&& (__INST(mode) != @symbol(writeonly)) |
4007 |
&& (__INST(binary) != true) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4008 |
) { |
2264 | 4009 |
f = __FILEVal(fp); |
4010 |
_buffered = (__INST(buffered) == true); |
|
4011 |
if (_buffered) { |
|
4012 |
__READING__(f) |
|
4013 |
} |
|
4014 |
||
4015 |
while (1) { |
|
4016 |
__READBYTE__(ret, f, &c, _buffered); |
|
4017 |
if (ret <= 0) { |
|
4018 |
if (ret < 0) { |
|
4019 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4020 |
} else /* ret == 0 */ { |
|
4021 |
__INST(hitEOF) = true; |
|
4022 |
} |
|
4023 |
break; |
|
4024 |
} |
|
4025 |
switch (c) { |
|
4026 |
case ' ': |
|
4027 |
case '\t': |
|
4028 |
case '\n': |
|
4029 |
case '\r': |
|
4030 |
case '\b': |
|
4031 |
case '\f': |
|
4032 |
break; |
|
4033 |
||
4034 |
default: |
|
4035 |
__UNGETC__(c, f, _buffered); |
|
4036 |
RETURN ( __MKCHARACTER(c & 0xFF) ); |
|
4037 |
} |
|
4038 |
} |
|
612 | 4039 |
} |
4040 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
4041 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 4042 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
4043 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4044 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
4045 |
^ self errorBinary. |
|
4046 |
! |
|
4047 |
||
4048 |
skipSeparatorsExceptCR |
|
4049 |
"skip all whitespace but no newlines; |
|
4050 |
next will return next non-white-space character |
|
4051 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
4052 |
- reimplemented for speed" |
|
4053 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4054 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4055 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4056 |
char c; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
4057 |
int ret, _buffered; |
612 | 4058 |
OBJ fp; |
4059 |
||
1133 | 4060 |
__INST(lastErrorNumber) = nil; |
4061 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 4062 |
&& (__INST(mode) != @symbol(writeonly)) |
4063 |
&& (__INST(binary) != true) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4064 |
) { |
2264 | 4065 |
f = __FILEVal(fp); |
4066 |
_buffered = (__INST(buffered) == true); |
|
4067 |
if (_buffered) { |
|
4068 |
__READING__(f) |
|
4069 |
} |
|
4070 |
while (1) { |
|
4071 |
__READBYTE__(ret, f, &c, _buffered); |
|
4072 |
if (ret <= 0) { |
|
4073 |
if (ret < 0) { |
|
4074 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4075 |
} else /* ret == 0 */ { |
|
4076 |
__INST(hitEOF) = true; |
|
4077 |
} |
|
4078 |
break; |
|
4079 |
} |
|
4080 |
switch (c) { |
|
4081 |
case ' ': |
|
4082 |
case '\t': |
|
4083 |
case '\b': |
|
4084 |
case '\f': |
|
4085 |
break; |
|
4086 |
||
4087 |
default: |
|
4088 |
__UNGETC__(c, f, _buffered); |
|
4089 |
RETURN ( __MKCHARACTER(c & 0xFF) ); |
|
4090 |
} |
|
4091 |
} |
|
612 | 4092 |
} |
4093 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
4094 |
hitEOF ifTrue:[^ self pastEnd]. |
612 | 4095 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
4096 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4097 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4098 |
^ self errorBinary. |
612 | 4099 |
! |
4100 |
||
4101 |
skipThrough:aCharacter |
|
4102 |
"skip all characters up-to and including aCharacter. Return the receiver if |
|
671 | 4103 |
skip was successful, otherwise (i.e. if not found) return nil. |
612 | 4104 |
The next read operation will return the character after aCharacter. |
4105 |
The argument, aCharacter must be character, or integer when in binary mode. |
|
4106 |
Only single byte characters are currently supported." |
|
4107 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4108 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4109 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4110 |
REGISTER int cSearch; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4111 |
unsigned char c; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4112 |
int ret, _buffered; |
612 | 4113 |
OBJ fp; |
4114 |
||
1133 | 4115 |
__INST(lastErrorNumber) = nil; |
4116 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 4117 |
&& (__INST(mode) != @symbol(writeonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4118 |
) { |
2264 | 4119 |
if (__INST(binary) == true) { |
4120 |
/* searched for object must be a smallInteger */ |
|
4121 |
if (! __isSmallInteger(aCharacter)) goto badArgument; |
|
4122 |
cSearch = __intVal(aCharacter); |
|
4123 |
} else { |
|
4124 |
/* searched for object must be a character */ |
|
4125 |
if (! __isCharacter(aCharacter)) goto badArgument; |
|
4126 |
cSearch = __intVal(_characterVal(aCharacter)); |
|
4127 |
} |
|
4128 |
/* Q: should we just say: "not found" ? */ |
|
4129 |
if ((cSearch < 0) || (cSearch > 255)) goto badArgument; |
|
4130 |
||
4131 |
f = __FILEVal(fp); |
|
4132 |
if (_buffered = (__INST(buffered) == true)) { |
|
4133 |
__READING__(f) |
|
4134 |
} |
|
4135 |
||
4136 |
while (1) { |
|
4137 |
__READBYTE__(ret, f, &c, _buffered); |
|
4138 |
if (ret <= 0) { |
|
4139 |
if (ret < 0) { |
|
4140 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4141 |
} else /* ret == 0 */ { |
|
4142 |
__INST(hitEOF) = true; |
|
4143 |
} |
|
4144 |
break; |
|
4145 |
} |
|
4146 |
if (c == cSearch) { |
|
4147 |
RETURN (self); |
|
4148 |
} |
|
4149 |
} |
|
612 | 4150 |
} |
4151 |
badArgument: ; |
|
4152 |
%}. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1141
diff
changeset
|
4153 |
hitEOF ifTrue:[^ self pastEnd]. |
2331 | 4154 |
lastErrorNumber notNil ifTrue:[self readError. ^ self]. |
4155 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4156 |
(mode == #writeonly) ifTrue:[self errorWriteOnly. ^ self]. |
|
612 | 4157 |
" |
4158 |
argument must be integer/character in binary mode, |
|
4159 |
character in text mode |
|
4160 |
" |
|
2331 | 4161 |
self error:'invalid argument'. |
612 | 4162 |
|
4163 |
" |
|
4164 |
|s| |
|
4165 |
s := 'Makefile' asFilename readStream. |
|
4166 |
s skipThrough:$=. |
|
4167 |
s next:10 |
|
4168 |
" |
|
4169 |
! |
|
4170 |
||
4171 |
skipToAll:aString |
|
4172 |
"skip for the sequence given by the argument, aCollection; |
|
1490
713852290c8b
use skipThroughAll from superclass.
Claus Gittinger <cg@exept.de>
parents:
1478
diff
changeset
|
4173 |
return nil if not found, self otherwise. |
713852290c8b
use skipThroughAll from superclass.
Claus Gittinger <cg@exept.de>
parents:
1478
diff
changeset
|
4174 |
On a successful match, the next read will return characters of aString." |
612 | 4175 |
|
4176 |
|oldPos| |
|
4177 |
||
4178 |
oldPos := self position. |
|
4179 |
(self skipThroughAll:aString) isNil ifTrue:[ |
|
2264 | 4180 |
" |
4181 |
restore position |
|
4182 |
" |
|
4183 |
self position:oldPos. |
|
4184 |
^ nil |
|
612 | 4185 |
]. |
4186 |
" |
|
4187 |
position before match-string |
|
4188 |
" |
|
4189 |
self position:(self position - aString size). |
|
4190 |
^ self |
|
4191 |
||
4192 |
" |
|
4193 |
|s| |
|
4194 |
s := 'Makefile' asFilename readStream. |
|
4195 |
s skipToAll:'are'. |
|
1490
713852290c8b
use skipThroughAll from superclass.
Claus Gittinger <cg@exept.de>
parents:
1478
diff
changeset
|
4196 |
s next:10 |
612 | 4197 |
" |
1490
713852290c8b
use skipThroughAll from superclass.
Claus Gittinger <cg@exept.de>
parents:
1478
diff
changeset
|
4198 |
|
713852290c8b
use skipThroughAll from superclass.
Claus Gittinger <cg@exept.de>
parents:
1478
diff
changeset
|
4199 |
"Modified: 26.6.1996 / 09:22:05 / cg" |
1 | 4200 |
! ! |
612 | 4201 |
|
4202 |
!ExternalStream methodsFor:'testing'! |
|
4203 |
||
4204 |
atEnd |
|
4205 |
"return true, if position is at end" |
|
4206 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4207 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4208 |
FILEPOINTER f; |
612 | 4209 |
OBJ fp; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4210 |
char c; |
2861 | 4211 |
int ret, _buffered; |
612 | 4212 |
|
1133 | 4213 |
if (__INST(hitEOF) == true) { |
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4214 |
RETURN (true); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4215 |
} |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4216 |
if (__bothSmallInteger(__INST(position), __INST(readLimit))) { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4217 |
if (__intVal(__INST(position)) > __intVal(__INST(readLimit))) { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4218 |
RETURN (true); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4219 |
} |
612 | 4220 |
} |
4221 |
||
1133 | 4222 |
__INST(lastErrorNumber) = nil; |
4223 |
||
4224 |
if ((fp = __INST(filePointer)) != nil) { |
|
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4225 |
f = __FILEVal(fp); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4226 |
|
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4227 |
if (_buffered = (__INST(buffered) == true)) { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4228 |
__READING__(f); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4229 |
} else { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4230 |
if (__INST(readAhead) != nil) { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4231 |
RETURN (false); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4232 |
} |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4233 |
} |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4234 |
|
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4235 |
/* |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4236 |
* read ahead ... |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4237 |
*/ |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4238 |
do { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4239 |
__BEGIN_INTERRUPTABLE__ |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4240 |
__READBYTE__(ret, f, &c, _buffered); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4241 |
__END_INTERRUPTABLE__ |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4242 |
} while ((ret < 0) && (errno == EINTR)); |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
4243 |
|
3569 | 4244 |
#if defined(WIN32) && defined(EPIPE) |
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4245 |
if (errno == EPIPE) |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4246 |
ret = 0; |
3569 | 4247 |
#endif |
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4248 |
if (ret > 0) { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4249 |
__UNGETC__(c&0xff, f, _buffered); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4250 |
RETURN (false); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4251 |
} |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4252 |
|
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4253 |
if (ret == 0) { |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4254 |
__INST(hitEOF) = true; |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4255 |
RETURN (true); |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4256 |
} |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4257 |
|
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4258 |
/* ret < 0 */ |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4259 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
612 | 4260 |
} |
4261 |
%}. |
|
4262 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4263 |
^ self errorNotOpen |
|
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4264 |
|
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4265 |
"Modified: / 30.10.1998 / 20:16:06 / cg" |
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
4266 |
|
612 | 4267 |
! |
4268 |
||
4269 |
canReadWithoutBlocking |
|
4270 |
"return true, if any data is available for reading (i.e. |
|
4271 |
a read operation will not block the smalltalk process), false otherwise." |
|
4272 |
||
4273 |
|fd| |
|
4274 |
||
2986
c6957cf6128f
Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents:
2963
diff
changeset
|
4275 |
readAhead notNil ifTrue:[^ true]. |
612 | 4276 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
4277 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4278 |
||
4279 |
fd := self fileDescriptor. |
|
4280 |
^ OperatingSystem readCheck:fd |
|
4281 |
||
4282 |
" |
|
4283 |
|pipe| |
|
4284 |
||
4285 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
4286 |
pipe canReadWithoutBlocking ifTrue:[ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
4287 |
Transcript showCR:'data available' |
612 | 4288 |
] ifFalse:[ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
4289 |
Transcript showCR:'no data available' |
612 | 4290 |
]. |
4291 |
pipe close |
|
4292 |
" |
|
2986
c6957cf6128f
Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents:
2963
diff
changeset
|
4293 |
|
c6957cf6128f
Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents:
2963
diff
changeset
|
4294 |
"Modified: 25.9.1997 / 13:08:45 / stefan" |
612 | 4295 |
! |
4296 |
||
4297 |
canWriteWithoutBlocking |
|
4298 |
"return true, if data can be written into the stream |
|
4299 |
(i.e. a write operation will not block the smalltalk process)." |
|
4300 |
||
4301 |
|fd| |
|
4302 |
||
4303 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4304 |
mode == #readonly ifTrue:[^ self errorReadOnly]. |
|
4305 |
||
4306 |
fd := self fileDescriptor. |
|
4307 |
^ OperatingSystem writeCheck:fd |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4308 |
! |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4309 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4310 |
numAvailable |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4311 |
"return the number of bytes available for reading" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4312 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4313 |
|fd| |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4314 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4315 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4316 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4317 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4318 |
fd := self fileDescriptor. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4319 |
^ OperatingSystem numAvailableForReadOn:fd. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4320 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4321 |
"/ self atEnd ifTrue:[^ 0]. |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
4322 |
"/ self canReadWithoutBlocking ifTrue:[ |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
4323 |
"/ ^ 1 |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
4324 |
"/ ]. |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
4325 |
"/ ^ 0 |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4326 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4327 |
"Created: / 4.2.1998 / 16:56:01 / cg" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
4328 |
"Modified: / 4.2.1998 / 17:31:11 / cg" |
612 | 4329 |
! ! |
4330 |
||
4331 |
!ExternalStream methodsFor:'waiting for I/O'! |
|
4332 |
||
4333 |
readWait |
|
4334 |
"suspend the current process, until the receiver |
|
4335 |
becomes ready for reading. If data is already available, |
|
4336 |
return immediate. |
|
4337 |
The other threads are not affected by the wait." |
|
4338 |
||
4339 |
self readWaitWithTimeoutMs:nil |
|
4340 |
! |
|
4341 |
||
3016 | 4342 |
readWaitTimeoutMs:timeout |
4343 |
"ST-80 compatibility" |
|
4344 |
^ self readWaitWithTimeoutMs:timeout |
|
4345 |
! |
|
4346 |
||
612 | 4347 |
readWaitWithTimeout:timeout |
4348 |
"suspend the current process, until the receiver |
|
4349 |
becomes ready for reading or a timeout (in seconds) expired. |
|
4350 |
If data is already available, return immediate. |
|
4351 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4352 |
The other threads are not affected by the wait." |
|
4353 |
||
4354 |
^ self readWaitWithTimeoutMs:timeout * 1000 |
|
4355 |
! |
|
4356 |
||
4357 |
readWaitWithTimeoutMs:timeout |
|
4358 |
"suspend the current process, until the receiver |
|
4359 |
becomes ready for reading or a timeout (in milliseconds) expired. |
|
4360 |
If data is already available, return immediate. |
|
4361 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4362 |
The other threads are not affected by the wait." |
|
4363 |
||
4364 |
|fd inputSema hasData wasBlocked| |
|
4365 |
||
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
4366 |
readAhead notNil ifTrue:[^ false]. |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
4367 |
|
612 | 4368 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
4369 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4370 |
||
4371 |
fd := self fileDescriptor. |
|
4372 |
(OperatingSystem readCheck:fd) ifTrue:[^ false]. |
|
4373 |
||
4374 |
wasBlocked := OperatingSystem blockInterrupts. |
|
4375 |
hasData := OperatingSystem readCheck:fd. |
|
4376 |
hasData ifFalse:[ |
|
2264 | 4377 |
inputSema := Semaphore new name:'readWait'. |
4378 |
[ |
|
4379 |
timeout notNil ifTrue:[ |
|
4380 |
Processor signal:inputSema afterMilliseconds:timeout |
|
4381 |
]. |
|
4382 |
Processor signal:inputSema onInput:fd. |
|
4383 |
Processor activeProcess state:#ioWait. |
|
4384 |
inputSema wait. |
|
4385 |
Processor disableSemaphore:inputSema. |
|
4386 |
hasData := OperatingSystem readCheck:fd |
|
4387 |
] valueOnUnwindDo:[ |
|
4388 |
Processor disableSemaphore:inputSema. |
|
4389 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4390 |
] |
|
612 | 4391 |
]. |
4392 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4393 |
^ hasData not |
|
4394 |
! |
|
4395 |
||
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4396 |
readWriteWait |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4397 |
"suspend the current process, until the receiver |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4398 |
becomes ready for writing or reading. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4399 |
Return immediate if the receiver is already ready. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4400 |
The other threads are not affected by the wait. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4401 |
This is actually only used with sockets, to wait for a connect to |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4402 |
be finished." |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4403 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4404 |
self readWriteWaitWithTimeoutMs:nil |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4405 |
! |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4406 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4407 |
readWriteWaitWithTimeoutMs:timeout |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4408 |
"suspend the current process, until the receiver |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4409 |
becomes ready for reading or writing or a timeout (in seconds) expired. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4410 |
Return true if a timeout occured (i.e. false, if data is available). |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4411 |
Return immediate if the receiver is already ready. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4412 |
The other threads are not affected by the wait. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4413 |
This is actually only used with sockets, to wait for a connect to |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4414 |
be finished." |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4415 |
|
3672
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
4416 |
|fd sema canReadWrite wasBlocked| |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4417 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4418 |
filePointer isNil ifTrue:[ |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4419 |
^ self errorNotOpen |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4420 |
]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4421 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4422 |
fd := self fileDescriptor. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4423 |
(OperatingSystem readWriteCheck:fd) ifTrue:[^ false]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4424 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4425 |
wasBlocked := OperatingSystem blockInterrupts. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4426 |
canReadWrite := OperatingSystem readWriteCheck:fd. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4427 |
canReadWrite ifFalse:[ |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4428 |
sema := Semaphore new name:'readWriteWait'. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4429 |
[ |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4430 |
timeout notNil ifTrue:[ |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4431 |
Processor signal:sema afterMilliseconds:timeout |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4432 |
]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4433 |
Processor signal:sema onOutput:fd. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4434 |
Processor signal:sema onInput:fd. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4435 |
Processor activeProcess state:#ioWait. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4436 |
sema wait. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4437 |
Processor disableSemaphore:sema. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4438 |
canReadWrite := OperatingSystem readWriteCheck:fd |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4439 |
] valueOnUnwindDo:[ |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4440 |
Processor disableSemaphore:sema. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4441 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4442 |
] |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4443 |
]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4444 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4445 |
^ canReadWrite not |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4446 |
! |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
4447 |
|
612 | 4448 |
writeWait |
4449 |
"suspend the current process, until the receiver |
|
4450 |
becomes ready for writing. |
|
4451 |
Return immediate if the receiver is already ready. |
|
4452 |
The other threads are not affected by the wait." |
|
4453 |
||
4454 |
self writeWaitWithTimeoutMs:nil |
|
4455 |
! |
|
4456 |
||
3016 | 4457 |
writeWaitTimeoutMs:timeout |
4458 |
"ST-80 compatibility" |
|
4459 |
^ self writeWaitWithTimeoutMs:timeout |
|
4460 |
! |
|
4461 |
||
612 | 4462 |
writeWaitWithTimeout:timeout |
4463 |
"suspend the current process, until the receiver |
|
4464 |
becomes ready for writing or a timeout (in seconds) expired. |
|
4465 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4466 |
Return immediate if the receiver is already ready. |
|
4467 |
The other threads are not affected by the wait." |
|
4468 |
||
4469 |
^ self writeWaitWithTimeoutMs:timeout * 1000 |
|
4470 |
! |
|
4471 |
||
4472 |
writeWaitWithTimeoutMs:timeout |
|
4473 |
"suspend the current process, until the receiver |
|
4474 |
becomes ready for writing or a timeout (in seconds) expired. |
|
4475 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4476 |
Return immediate if the receiver is already ready. |
|
4477 |
The other threads are not affected by the wait." |
|
4478 |
||
4479 |
|fd outputSema canWrite wasBlocked| |
|
4480 |
||
4481 |
filePointer isNil ifTrue:[ |
|
2264 | 4482 |
^ self errorNotOpen |
612 | 4483 |
]. |
4484 |
mode == #readonly ifTrue:[ |
|
2264 | 4485 |
^ self errorReadOnly |
612 | 4486 |
]. |
4487 |
||
4488 |
fd := self fileDescriptor. |
|
4489 |
(OperatingSystem writeCheck:fd) ifTrue:[^ false]. |
|
4490 |
||
4491 |
wasBlocked := OperatingSystem blockInterrupts. |
|
4492 |
canWrite := OperatingSystem writeCheck:fd. |
|
4493 |
canWrite ifFalse:[ |
|
2264 | 4494 |
outputSema := Semaphore new name:'writeWait'. |
4495 |
[ |
|
4496 |
timeout notNil ifTrue:[ |
|
4497 |
Processor signal:outputSema afterMilliseconds:timeout |
|
4498 |
]. |
|
4499 |
Processor signal:outputSema onOutput:fd. |
|
4500 |
Processor activeProcess state:#ioWait. |
|
4501 |
outputSema wait. |
|
4502 |
Processor disableSemaphore:outputSema. |
|
4503 |
canWrite := OperatingSystem writeCheck:fd |
|
4504 |
] valueOnUnwindDo:[ |
|
4505 |
Processor disableSemaphore:outputSema. |
|
4506 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4507 |
] |
|
612 | 4508 |
]. |
4509 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4510 |
^ canWrite not |
|
4511 |
! ! |
|
4512 |
||
4513 |
!ExternalStream methodsFor:'writing'! |
|
4514 |
||
4515 |
cr |
|
4516 |
"append an end-of-line character (or CRLF if in crlf mode). |
|
4517 |
reimplemented for speed" |
|
4518 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4519 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4520 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4521 |
int len, cnt, _buffered; |
612 | 4522 |
OBJ fp; |
4523 |
char *cp; |
|
4524 |
||
1133 | 4525 |
__INST(lastErrorNumber) = nil; |
4526 |
if (((fp = __INST(filePointer)) != nil) |
|
2331 | 4527 |
&& (__INST(mode) != @symbol(readonly)) |
4528 |
&& (__INST(binary) != true) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4529 |
) { |
2331 | 4530 |
f = __FILEVal(fp); |
4531 |
||
4532 |
if (_buffered = (__INST(buffered) == true)) { |
|
4533 |
__WRITING__(f) |
|
4534 |
} |
|
2920 | 4535 |
{ |
4536 |
OBJ mode = __INST(eolMode); |
|
4537 |
||
4538 |
if (mode == @symbol(cr)) { |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
4539 |
cp = "\r"; len = 1; |
2920 | 4540 |
} else if (mode == @symbol(crlf)) { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
4541 |
cp = "\r\n"; len = 2; |
2920 | 4542 |
} else { |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
4543 |
cp = "\n"; len = 1; |
2920 | 4544 |
} |
2331 | 4545 |
} |
4546 |
__WRITEBYTES__(cnt, f, cp, len, _buffered); |
|
4547 |
||
4548 |
if (cnt == len) { |
|
4549 |
if (__INST(position) != nil) { |
|
4550 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + len); |
|
4551 |
} |
|
4552 |
RETURN ( self ); |
|
4553 |
} |
|
4554 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
612 | 4555 |
} |
4556 |
%}. |
|
2326 | 4557 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
4558 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4559 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
4560 |
self errorBinary |
|
612 | 4561 |
! |
4562 |
||
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4563 |
flush |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4564 |
"write all buffered data - ignored if unbuffered" |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4565 |
|
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4566 |
%{ |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4567 |
OBJ fp; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4568 |
|
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4569 |
__INST(lastErrorNumber) = nil; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4570 |
if ((fp = __INST(filePointer)) != nil) { |
2264 | 4571 |
if (__INST(mode) != @symbol(readonly)) { |
4572 |
if (__INST(buffered) == true) { |
|
4573 |
__BEGIN_INTERRUPTABLE__ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4574 |
FFLUSH( __FILEVal(fp) ); |
2264 | 4575 |
__END_INTERRUPTABLE__ |
4576 |
} |
|
4577 |
} |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4578 |
} |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4579 |
%} |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4580 |
! |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4581 |
|
612 | 4582 |
nextPut:aCharacter |
4583 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
4584 |
Only single-byte characters are currently supported" |
|
4585 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4586 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4587 |
FILEPOINTER f; |
612 | 4588 |
char c; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4589 |
int cnt, _buffered; |
612 | 4590 |
OBJ pos, fp; |
4591 |
||
1133 | 4592 |
__INST(lastErrorNumber) = nil; |
4593 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 4594 |
&& (__INST(mode) != @symbol(readonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4595 |
) { |
2264 | 4596 |
if (__INST(binary) != true) { |
4597 |
if (__isCharacter(aCharacter)) { |
|
4598 |
c = __intVal(__characterVal(aCharacter)) & 0xFF; |
|
612 | 4599 |
doWrite: |
2264 | 4600 |
f = __FILEVal(fp); |
4601 |
||
4602 |
||
4603 |
if (_buffered = (__INST(buffered) == true)) { |
|
4604 |
__WRITING__(f) |
|
4605 |
} |
|
4606 |
__WRITEBYTE__(cnt, f, &c, _buffered); |
|
4607 |
||
4608 |
if (cnt == 1) { |
|
4609 |
pos = __INST(position); |
|
4610 |
if (pos != nil) { |
|
4611 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
4612 |
} |
|
4613 |
RETURN ( self ); |
|
4614 |
} |
|
4615 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4616 |
} |
|
4617 |
} else { |
|
4618 |
if (__isSmallInteger(aCharacter)) { |
|
4619 |
c = __intVal(aCharacter); |
|
4620 |
goto doWrite; |
|
4621 |
} |
|
4622 |
} |
|
612 | 4623 |
} |
4624 |
%}. |
|
2331 | 4625 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
4626 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4627 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
4628 |
binary ifFalse:[self argumentMustBeCharacter. ^ self]. |
|
4629 |
self argumentMustBeInteger. |
|
612 | 4630 |
! |
4631 |
||
4632 |
nextPutAll:aCollection |
|
4633 |
"write all elements of the argument, aCollection. |
|
4634 |
Reimplemented for speed when writing strings or byteArrays. |
|
4635 |
For others, falls back to general method in superclass." |
|
4636 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4637 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4638 |
FILEPOINTER f; |
612 | 4639 |
int len, cnt; |
4640 |
OBJ pos, fp; |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4641 |
int _buffered; |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4642 |
int o_offs; |
612 | 4643 |
|
1133 | 4644 |
__INST(lastErrorNumber) = nil; |
4645 |
||
4646 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 4647 |
&& (__INST(mode) != @symbol(readonly)) |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4648 |
) { |
2264 | 4649 |
f = __FILEVal(fp); |
4650 |
if (_buffered = (__INST(buffered) == true)) { |
|
4651 |
__WRITING__(f) |
|
4652 |
} |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4653 |
|
2264 | 4654 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
4655 |
len = __stringSize(aCollection); |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4656 |
#ifdef BUGGY |
2264 | 4657 |
__WRITEBYTES__(cnt, f, __stringVal(aCollection), len, _buffered); |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4658 |
#else |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4659 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4660 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4661 |
#endif |
2264 | 4662 |
} else { |
4663 |
if (__INST(binary) == true) { |
|
4664 |
int offs; |
|
4665 |
||
4666 |
if (__isByteArray(aCollection)) { |
|
4667 |
offs = 0; |
|
3221
21085ae8e1be
eliminated some single-underscore macro refs (stringSize, arraySize ...)
Claus Gittinger <cg@exept.de>
parents:
3200
diff
changeset
|
4668 |
len = __byteArraySize(aCollection); |
2264 | 4669 |
} else if (__isBytes(aCollection)) { |
4670 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
3221
21085ae8e1be
eliminated some single-underscore macro refs (stringSize, arraySize ...)
Claus Gittinger <cg@exept.de>
parents:
3200
diff
changeset
|
4671 |
len = __byteArraySize(aCollection) - offs; |
2264 | 4672 |
} else |
4673 |
goto out; |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4674 |
#ifdef BUGGY |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4675 |
__WRITEBYTES__(cnt, f, __ByteArrayInstPtr(aCollection)->ba_element+offs, len, _buffered); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4676 |
#else |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4677 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element) - (char *)__InstPtr(aCollection); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4678 |
o_offs += offs; |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4679 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4680 |
#endif |
2264 | 4681 |
} else |
4682 |
goto out; |
|
4683 |
} |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4684 |
|
2264 | 4685 |
if (cnt == len) { |
4686 |
pos = __INST(position); |
|
4687 |
if (pos != nil) { |
|
4688 |
__INST(position) = __MKSMALLINT(__intVal(pos) + len); |
|
4689 |
} |
|
4690 |
RETURN (self); |
|
4691 |
} |
|
4692 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
612 | 4693 |
} |
1478
aae07192b7d1
labels in empty statements need a semi
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
4694 |
out: ; |
612 | 4695 |
%}. |
2331 | 4696 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
4697 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4698 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
612 | 4699 |
|
4700 |
^ super nextPutAll:aCollection |
|
4701 |
! |
|
4702 |
||
4703 |
nextPutAll:aCollection startingAt:start to:stop |
|
4704 |
"write a range of elements of the argument, aCollection. |
|
4705 |
Reimplemented for speed when writing strings or byteArrays. |
|
4706 |
For others, falls back to general method in superclass." |
|
4707 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4708 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4709 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4710 |
int offs, len, cnt, iStart, iStop, _buffered; |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4711 |
OBJ fp, pos; |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4712 |
int o_offs; |
612 | 4713 |
|
1133 | 4714 |
__INST(lastErrorNumber) = nil; |
4715 |
if (((fp = __INST(filePointer)) != nil) |
|
2264 | 4716 |
&& (__INST(mode) != @symbol(readonly)) |
4717 |
&& __bothSmallInteger(start, stop) |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4718 |
) { |
2264 | 4719 |
f = __FILEVal(fp); |
4720 |
if (_buffered = (__INST(buffered) == true)) { |
|
4721 |
__WRITING__(f) |
|
4722 |
} |
|
4723 |
iStart = __intVal(start); |
|
4724 |
iStop = __intVal(stop); |
|
4725 |
if ((iStart < 1) || (iStop < iStart)) { |
|
4726 |
RETURN(self); |
|
4727 |
} |
|
4728 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
|
4729 |
len = __stringSize(aCollection); |
|
4730 |
if (iStop > len) { |
|
4731 |
RETURN(self); |
|
4732 |
} |
|
4733 |
if (iStop > len) |
|
4734 |
iStop = len; |
|
4735 |
len = iStop - iStart + 1; |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4736 |
#ifdef BUGGY |
2264 | 4737 |
__WRITEBYTES__(cnt, f, __stringVal(aCollection)+iStart-1, len, _buffered); |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4738 |
#else |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4739 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4740 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4741 |
#endif |
2264 | 4742 |
} else { |
4743 |
if (__INST(binary) == true) { |
|
4744 |
int offs; |
|
4745 |
||
4746 |
if (__isByteArray(aCollection)) { |
|
4747 |
offs = 0; |
|
3221
21085ae8e1be
eliminated some single-underscore macro refs (stringSize, arraySize ...)
Claus Gittinger <cg@exept.de>
parents:
3200
diff
changeset
|
4748 |
len = __byteArraySize(aCollection); |
2264 | 4749 |
} else if (__isBytes(aCollection)) { |
4750 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
3221
21085ae8e1be
eliminated some single-underscore macro refs (stringSize, arraySize ...)
Claus Gittinger <cg@exept.de>
parents:
3200
diff
changeset
|
4751 |
len = __byteArraySize(aCollection) - offs; |
2264 | 4752 |
} else |
4753 |
goto out; |
|
4754 |
||
4755 |
if (iStop > len) { |
|
4756 |
RETURN(self); |
|
4757 |
} |
|
4758 |
if (iStop > len) |
|
4759 |
iStop = len; |
|
4760 |
len = iStop - iStart + 1; |
|
4761 |
offs += iStart - 1; |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4762 |
#ifdef BUGGY |
2264 | 4763 |
__WRITEBYTES__(cnt, f, __ByteArrayInstPtr(aCollection)->ba_element+offs, len, _buffered); |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4764 |
#else |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4765 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4766 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+offs, len, _buffered); |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
4767 |
#endif |
2264 | 4768 |
} else |
4769 |
goto out; |
|
4770 |
} |
|
4771 |
if (cnt == len) { |
|
4772 |
pos = __INST(position); |
|
4773 |
if (pos != nil) { |
|
4774 |
__INST(position) = __MKSMALLINT(__intVal(pos) + len); |
|
4775 |
} |
|
4776 |
RETURN (self); |
|
4777 |
} |
|
4778 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
612 | 4779 |
} |
1478
aae07192b7d1
labels in empty statements need a semi
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
4780 |
out: ; |
612 | 4781 |
%}. |
4782 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
4783 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
4784 |
! ! |
|
4785 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
4786 |
!ExternalStream class methodsFor:'documentation'! |
730 | 4787 |
|
4788 |
version |
|
3949 | 4789 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.182 1998-12-21 20:32:56 cg Exp $' |
730 | 4790 |
! ! |
612 | 4791 |
ExternalStream initialize! |