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