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