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