author | Stefan Vogel <sv@exept.de> |
Thu, 01 Oct 2015 19:06:51 +0200 | |
changeset 18797 | 0c9e79945694 |
parent 18793 | 19f35dc98859 |
child 18800 | 02724cc719b6 |
child 18929 | 14b2c3ab66e8 |
permissions | -rw-r--r-- |
18790 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1988 by Claus Gittinger |
18797 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5414 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17418 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
ReadWriteStream subclass:#ExternalStream |
18797 | 19 |
instanceVariableNames:'handleType handle mode buffered binary eolMode hitEOF didWrite |
20 |
lastErrorNumber readAhead' |
|
21 |
classVariableNames:'Lobby LastErrorNumber InvalidReadSignal InvalidWriteSignal |
|
22 |
InvalidModeSignal OpenErrorSignal StreamNotOpenSignal |
|
23 |
InvalidOperationSignal DefaultEOLMode ReadMode ReadWriteMode |
|
24 |
WriteMode AppendMode CreateReadWriteMode StreamIOErrorSignal |
|
25 |
FileOpenTrace MaxNonTenurableExecutors' |
|
26 |
poolDictionaries:'' |
|
27 |
category:'Streams-External' |
|
1 | 28 |
! |
29 |
||
216 | 30 |
!ExternalStream primitiveDefinitions! |
1 | 31 |
%{ |
10328
ad731a7584a3
ensure that win32_fwrite is used for stdout and stderr
Claus Gittinger <cg@exept.de>
parents:
9053
diff
changeset
|
32 |
#undef fwrite |
ad731a7584a3
ensure that win32_fwrite is used for stdout and stderr
Claus Gittinger <cg@exept.de>
parents:
9053
diff
changeset
|
33 |
|
17078 | 34 |
#include "stxOSDefs.h" |
35 |
||
1 | 36 |
#include <stdio.h> |
437 | 37 |
#define _STDIO_H_INCLUDED_ |
38 |
||
2903 | 39 |
#ifndef NO_FCNTL_H |
40 |
# include <fcntl.h> |
|
41 |
# define _FCNTL_H_INCLUDED_ |
|
42 |
#endif |
|
437 | 43 |
|
10 | 44 |
#include <errno.h> |
437 | 45 |
#define _ERRNO_H_INCLUDED_ |
42 | 46 |
|
1141 | 47 |
#ifdef LINUX |
48 |
/* use inline string macros */ |
|
49 |
# define __STRINGDEFS__ |
|
50 |
# include <linuxIntern.h> |
|
51 |
#endif |
|
52 |
||
16508 | 53 |
#if !defined(MSDOS_LIKE) && !defined(__openVMS__) |
54 |
# include <termios.h> |
|
55 |
#endif |
|
56 |
||
42 | 57 |
#ifdef hpux |
58 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
|
59 |
#endif |
|
216 | 60 |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
61 |
#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
|
62 |
# 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
|
63 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
64 |
#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
|
65 |
# 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
|
66 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
67 |
#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
|
68 |
# 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
|
69 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
70 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
71 |
#if defined(__VMS__) || defined(__sparc__) || defined(WIN32) |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5101
diff
changeset
|
72 |
# define CLEAR_ERRNO __threadErrno = 0; |
2961 | 73 |
#else |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
74 |
# define CLEAR_ERRNO /* nothing */ |
2961 | 75 |
#endif |
76 |
||
3420 | 77 |
#ifdef LATER |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
78 |
# define __isFilePointer(x) (__Class(x) == ExternalStream__FilePointer) |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
79 |
# define __CHANGECLASS(o, x) (__qClass(o) = (x), o) |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
80 |
# define __MKFILEPOINTER(f) __CHANGECLASS(__MKEXTERNALADDRESS(f), ExternalStream__FilePointer) |
3420 | 81 |
#else |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
82 |
# define __MKFILEPOINTER(f) __MKEXTERNALADDRESS(f) |
3420 | 83 |
#endif |
84 |
||
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
85 |
#ifdef WIN32 |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
86 |
# define NO_STDIO |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
87 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
88 |
# undef __HANDLE_INTERRUPTS__ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
89 |
# define __HANDLE_INTERRUPTS__ /* nothing */ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
90 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
91 |
# undef __BEGIN_INTERRUPTABLE__ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
92 |
# define __BEGIN_INTERRUPTABLE__ /* nothing */ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
93 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
94 |
# undef __END_INTERRUPTABLE__ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
95 |
# define __END_INTERRUPTABLE__ /* nothing */ |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
96 |
#endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
97 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
98 |
# ifdef __i386__ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
99 |
# define _X86_ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
100 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
101 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
102 |
#ifdef WIN32 |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
103 |
# undef INT |
14644 | 104 |
# undef UINT |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
105 |
# undef Array |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
106 |
# undef Number |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
107 |
# undef Method |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
108 |
# undef Point |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
109 |
# undef Rectangle |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
110 |
# undef Block |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
111 |
# undef Time |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
112 |
# undef Date |
7832 | 113 |
# undef Delay |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
114 |
# undef Set |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
115 |
# undef Signal |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
116 |
# undef Context |
9023 | 117 |
# undef Message |
9024 | 118 |
# undef Process |
10339 | 119 |
# undef Processor |
14632 | 120 |
# undef String |
121 |
# undef Character |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
122 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
123 |
# define NOATOM |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
124 |
# define NOGDICAPMASKS |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
125 |
# define NOMETAFILE |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
126 |
# define NOMINMAX |
14068 | 127 |
//# define NOOPENFILE |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
128 |
# define NOSOUND |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
129 |
# define NOWH |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
130 |
//# define NOCOMM |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
131 |
# define NOKANJI |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
132 |
# define NOCRYPT |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
133 |
//# define NOMCX |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
134 |
# define WIN32_LEAN_AND_MEAN |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
135 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
136 |
# include <windows.h> |
15435
fc902ce16af0
typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents:
15361
diff
changeset
|
137 |
# include <winsock2.h> |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
138 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
139 |
# ifdef __DEF_Array |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
140 |
# define Array __DEF_Array |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
141 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
142 |
# ifdef __DEF_Number |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
143 |
# define Number __DEF_Number |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
144 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
145 |
# ifdef __DEF_Method |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
146 |
# define Method __DEF_Method |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
147 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
148 |
# ifdef __DEF_Point |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
149 |
# define Point __DEF_Point |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
150 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
151 |
# ifdef __DEF_Block |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
152 |
# define Block __DEF_Block |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
153 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
154 |
# ifdef __DEF_Time |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
155 |
# define Time __DEF_Time |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
156 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
157 |
# ifdef __DEF_Date |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
158 |
# define Date __DEF_Date |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
159 |
# endif |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
160 |
# ifdef __DEF_Set |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
161 |
# define Set __DEF_Set |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
162 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
163 |
# ifdef __DEF_Signal |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
164 |
# define Signal __DEF_Signal |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
165 |
# endif |
7832 | 166 |
# ifdef __DEF_Delay |
167 |
# define Delay __DEF_Delay |
|
168 |
# endif |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
169 |
# ifdef __DEF_Context |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
170 |
# define Context __DEF_Context |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
171 |
# endif |
9023 | 172 |
# ifdef __DEF_Message |
173 |
# define Message __DEF_Message |
|
174 |
# endif |
|
9024 | 175 |
# ifdef __DEF_Process |
176 |
# define Process __DEF_Process |
|
177 |
# endif |
|
10339 | 178 |
# ifdef __DEF_Processor |
179 |
# define Processor __DEF_Processor |
|
180 |
# endif |
|
14632 | 181 |
# ifdef __DEF_String |
182 |
# define String __DEF_String |
|
183 |
# endif |
|
184 |
# ifdef __DEF_Character |
|
185 |
# define Character __DEF_Character |
|
186 |
# endif |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
187 |
|
14644 | 188 |
# define INT STX_INT |
189 |
# define UINT STX_UINT |
|
190 |
# ifndef off_t |
|
191 |
# define off_t long |
|
192 |
# endif |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
193 |
#endif /* WIN32 */ |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
194 |
|
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
195 |
#ifndef NO_STDIO /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
196 |
# define STDIO_NEEDS_FSEEK |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
197 |
# define FILEPOINTER FILE * |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
198 |
# define READ(f, cp, n) read(f, cp, n) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
199 |
# define WRITE(f, cp, n) write(f, cp, n) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
200 |
# define FFLUSH(fp) fflush(fp) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
201 |
# define FILENO(f) fileno(f) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
202 |
#endif /* use STDIO */ |
3681 | 203 |
|
3682 | 204 |
#ifdef DEBUGGING |
4092 | 205 |
extern char *__survStartPtr, *__survEndPtr; |
3682 | 206 |
# define DEBUGBUFFER(buf) \ |
3681 | 207 |
if (((char *)(buf) >= __survStartPtr) \ |
208 |
&& ((char *)(buf) < __survEndPtr)) { \ |
|
18797 | 209 |
__fatal0("read into survivor\n"); \ |
3681 | 210 |
} |
211 |
||
3682 | 212 |
#else |
213 |
# define DEBUGBUFFER(buf) /* nothing */ |
|
214 |
#endif |
|
215 |
||
216 | 216 |
/* |
217 |
* stdio library requires an fseek before reading whenever a file |
|
218 |
* is open for read/write and the last operation was a write. |
|
219 |
* (also vice-versa). |
|
220 |
* All code should use the following macro before doing reads: |
|
221 |
*/ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
222 |
#ifdef STDIO_NEEDS_FSEEK |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
223 |
# define OPT_FSEEK(f, pos, whence) fseek(f, pos, whence) |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
224 |
#else |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
225 |
# define OPT_FSEEK(f, pos, whence) /* nothing */ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
226 |
#endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
227 |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
228 |
#ifdef WIN32 |
16912 | 229 |
// Win returns from ReadFile() with false and _threadErrno == 0 on end of pipe. |
230 |
// We don't know why |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
231 |
# define READ(ret, f, cp, n, handleType) { \ |
18797 | 232 |
if (handleType == @symbol(socketHandle)) { \ |
233 |
(ret) = __STX_WSA_NOINT_CALL4("recv", recv, (f), (cp), (n), 0); \ |
|
234 |
} else { \ |
|
235 |
HANDLE h = _get_osfhandle(fileno(f)); \ |
|
236 |
if (handleType == @symbol(socketFilePointer)) { \ |
|
237 |
(ret) = __STX_WSA_NOINT_CALL4("recv", recv, h, (cp), (n), 0);\ |
|
238 |
} else { \ |
|
239 |
int __res; \ |
|
240 |
(ret) = __STX_API_NOINT_CALL5("ReadFile", ReadFile, h, (cp), (n), &__res, 0);\ |
|
241 |
(ret) = (ret) ? __res : ((__threadErrno == EPIPE || __threadErrno == 0) ? 0 : -1); \ |
|
242 |
} \ |
|
243 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
244 |
} |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
245 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
246 |
# define WRITE(ret, f, cp, n, handleType) { \ |
18797 | 247 |
if (handleType == @symbol(socketHandle)) { \ |
248 |
(ret) = __STX_WSA_NOINT_CALL4("send", send, (f), (cp), (n), 0); \ |
|
249 |
} else {\ |
|
250 |
HANDLE h = _get_osfhandle(fileno(f)); \ |
|
251 |
if (handleType == @symbol(socketFilePointer)) { \ |
|
252 |
(ret) = __STX_WSA_NOINT_CALL4("send", send, h, (cp), (n), 0);\ |
|
253 |
} else {\ |
|
254 |
int __res; \ |
|
255 |
(ret) = __STX_API_NOINT_CALL5("WriteFile", WriteFile, h, (cp), (n), &__res, 0);\ |
|
256 |
(ret) = (ret) ? __res : -1; \ |
|
257 |
} \ |
|
258 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
259 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
260 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
261 |
# define FFLUSH(fp) fflush(fp) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
262 |
# undef STDIO_NEEDS_FSEEK |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
263 |
# define FILEPOINTER FILE * |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
264 |
# define FILENO(f) fileno(f) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
265 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
266 |
# define __READING__(f) \ |
1133 | 267 |
if ((__INST(didWrite) != false) \ |
268 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
18797 | 269 |
__INST(didWrite) = false; \ |
270 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
216 | 271 |
} |
272 |
||
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
273 |
# define __WRITING__(f) \ |
1133 | 274 |
if ((__INST(didWrite) != true) \ |
275 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
18797 | 276 |
__INST(didWrite) = true; \ |
277 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
216 | 278 |
} |
279 |
||
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
280 |
# define __UNGETC__(c, f, isBuffered) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
281 |
if (isBuffered) { \ |
18797 | 282 |
ungetc((c), (f)); \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
283 |
} else { \ |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
284 |
__INST(readAhead) = __mkSmallInteger((c)); \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
285 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
286 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
287 |
# define __READBYTE__(ret, f, buf, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
288 |
if (isBuffered) { \ |
18797 | 289 |
for (;;) { \ |
290 |
CLEAR_ERRNO; \ |
|
291 |
(ret) = getc(f); \ |
|
292 |
if ((ret) >= 0) { \ |
|
293 |
*(buf) = (ret); \ |
|
294 |
(ret) = 1; \ |
|
295 |
} else if (ferror(f)) { \ |
|
296 |
if (__threadErrno == EINTR) { \ |
|
297 |
clearerr(f); \ |
|
298 |
continue; \ |
|
299 |
} \ |
|
300 |
} else { \ |
|
301 |
(ret) = 0; \ |
|
302 |
} \ |
|
303 |
break; \ |
|
304 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
305 |
} else { \ |
18797 | 306 |
OBJ rA = __INST(readAhead); \ |
307 |
if (rA != nil) { \ |
|
308 |
*(buf) = (char)__intVal(rA); \ |
|
309 |
__INST(readAhead) = nil; \ |
|
310 |
(ret) = 1; \ |
|
311 |
} else { \ |
|
312 |
for (;;) { \ |
|
313 |
CLEAR_ERRNO; \ |
|
314 |
READ((ret), f, buf, 1, handleType); \ |
|
315 |
if ((ret) >= 0 || __threadErrno != EINTR) \ |
|
316 |
break; \ |
|
317 |
} \ |
|
318 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
319 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
320 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
321 |
/* |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
322 |
* read_bytes into a c-buffer |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
323 |
* (which may NOT move) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
324 |
*/ |
12548 | 325 |
# define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
326 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
327 |
if (isBuffered) { \ |
18797 | 328 |
int __offs = 0; \ |
329 |
while (__offs < (cnt)) { \ |
|
330 |
CLEAR_ERRNO; \ |
|
331 |
(ret) = getc(f); \ |
|
332 |
if ((ret) < 0) { \ |
|
333 |
if (ferror(f)) { \ |
|
334 |
if (__threadErrno == EINTR) { \ |
|
335 |
clearerr(f); \ |
|
336 |
continue; \ |
|
337 |
} \ |
|
338 |
} else { \ |
|
339 |
(ret) = 0; \ |
|
340 |
} \ |
|
341 |
break; \ |
|
342 |
} \ |
|
343 |
(buf)[__offs++] = (ret); \ |
|
344 |
} \ |
|
345 |
if (__offs > 0) \ |
|
346 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
347 |
} else { \ |
18797 | 348 |
int __offs = 0; \ |
349 |
\ |
|
350 |
while (__offs < (cnt)) { \ |
|
351 |
OBJ rA = __INST(readAhead); \ |
|
352 |
if (rA != nil) { \ |
|
353 |
(buf)[__offs] = __intVal(rA); \ |
|
354 |
__INST(readAhead) = nil; \ |
|
355 |
(ret) = 1; \ |
|
356 |
} else { \ |
|
357 |
CLEAR_ERRNO; \ |
|
358 |
READ((ret), f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
359 |
if ((ret) <= 0) { \ |
|
360 |
if ((ret) < 0 && __threadErrno == EINTR) { \ |
|
361 |
continue; \ |
|
362 |
} \ |
|
363 |
break; \ |
|
364 |
} \ |
|
365 |
} \ |
|
366 |
__offs += (ret); \ |
|
367 |
} \ |
|
368 |
if (__offs > 0) \ |
|
369 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
370 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
371 |
|
12548 | 372 |
# define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
373 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
374 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
375 |
int oldFlags; \ |
18797 | 376 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
377 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
378 |
if (isBuffered) { \ |
18797 | 379 |
while (__offs < (cnt)) { \ |
380 |
CLEAR_ERRNO; \ |
|
381 |
(ret) = getc(f); \ |
|
382 |
if ((ret) < 0) { \ |
|
383 |
if (ferror(f)) { \ |
|
384 |
if (__threadErrno == EINTR) { \ |
|
385 |
clearerr(f); \ |
|
386 |
continue; \ |
|
387 |
} \ |
|
388 |
} else { \ |
|
389 |
(ret) = 0; \ |
|
390 |
} \ |
|
391 |
break; \ |
|
392 |
} \ |
|
393 |
(buf)[__offs++] = (ret); \ |
|
394 |
} \ |
|
395 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
396 |
} else { \ |
18797 | 397 |
while (__offs < (cnt)) { \ |
398 |
OBJ rA = __INST(readAhead); \ |
|
399 |
if (rA != nil) { \ |
|
400 |
(buf)[__offs] = __intVal(rA); \ |
|
401 |
__INST(readAhead) = nil; \ |
|
402 |
(ret) = 1; \ |
|
403 |
__offs ++; \ |
|
404 |
continue; \ |
|
405 |
} \ |
|
406 |
CLEAR_ERRNO; \ |
|
407 |
{ \ |
|
408 |
int res = 0; \ |
|
409 |
if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res), 1)) \ |
|
410 |
|| (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \ |
|
411 |
|| (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0), 1))) { \ |
|
412 |
if (res > 0) { \ |
|
413 |
if (res > ((cnt)-__offs)) \ |
|
414 |
res = (cnt)-__offs; \ |
|
415 |
READ((ret), f, (buf)+__offs, res, handleType); \ |
|
416 |
} else { \ |
|
417 |
(ret) = 0; \ |
|
418 |
break; \ |
|
419 |
} \ |
|
420 |
} else { \ |
|
421 |
READ((ret), f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
422 |
} \ |
|
423 |
} \ |
|
424 |
if ((ret) <= 0) { \ |
|
425 |
if (ret < 0 && __threadErrno == EINTR) \ |
|
426 |
continue; \ |
|
427 |
break; \ |
|
428 |
} \ |
|
429 |
__offs += (ret); \ |
|
430 |
} \ |
|
431 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
432 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
433 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
434 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
435 |
# define IO_BUFFER_SIZE (8*1024) |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
436 |
|
12548 | 437 |
# define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
438 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
439 |
int __ooffs = obj_offs; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
440 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
441 |
char *buf = (char *)(obj); \ |
18797 | 442 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
443 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
444 |
if (isBuffered) { \ |
18797 | 445 |
while (__offs < (cnt)) { \ |
446 |
CLEAR_ERRNO; \ |
|
447 |
(ret) = getc(f); \ |
|
448 |
if ((ret) < 0) { \ |
|
449 |
if (ferror(f)) { \ |
|
450 |
if (__threadErrno == EINTR) { \ |
|
451 |
clearerr(f); \ |
|
452 |
/* refetch */ \ |
|
453 |
buf = (char *)(obj); \ |
|
454 |
continue; \ |
|
455 |
} \ |
|
456 |
} else { \ |
|
457 |
(ret) = 0; \ |
|
458 |
} \ |
|
459 |
break; \ |
|
460 |
} \ |
|
461 |
(buf)[__ooffs+__offs] = (ret); \ |
|
462 |
__offs++; \ |
|
463 |
} \ |
|
464 |
if (__offs > 0) \ |
|
465 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
466 |
} else { \ |
18797 | 467 |
while (__offs < (cnt)) { \ |
468 |
char __buf[IO_BUFFER_SIZE]; \ |
|
469 |
OBJ rA = __INST(readAhead); \ |
|
470 |
if (rA != nil) { \ |
|
471 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
472 |
__INST(readAhead) = nil; \ |
|
473 |
(ret) = 1; \ |
|
474 |
} else { \ |
|
475 |
int l; \ |
|
476 |
CLEAR_ERRNO; \ |
|
477 |
l = (cnt)-__offs; \ |
|
478 |
if ( l > IO_BUFFER_SIZE) \ |
|
479 |
l = IO_BUFFER_SIZE; \ |
|
480 |
READ((ret),f, __buf, l, handleType); \ |
|
481 |
if ((ret) <= 0) { \ |
|
482 |
if ((ret) < 0 && __threadErrno == EINTR) { \ |
|
483 |
continue; \ |
|
484 |
} \ |
|
485 |
break; \ |
|
486 |
} \ |
|
487 |
} \ |
|
488 |
if ((ret) > 0 ) { \ |
|
489 |
/* refetch */ \ |
|
490 |
buf = (char *)(obj); \ |
|
491 |
memcpy((buf)+__ooffs+__offs,__buf,(ret)); \ |
|
492 |
__offs += (ret); \ |
|
493 |
} else { \ |
|
494 |
(ret) = 0; \ |
|
495 |
} \ |
|
496 |
} \ |
|
497 |
if (__offs > 0) \ |
|
498 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
499 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
500 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
501 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
502 |
# define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
503 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
504 |
int __ooffs = obj_offs; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
505 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
506 |
char *buf = (char *)(obj); \ |
18797 | 507 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
508 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
509 |
if (isBuffered) { \ |
18797 | 510 |
while (__offs < (cnt)) { \ |
511 |
CLEAR_ERRNO; \ |
|
512 |
(ret) = getc(f); \ |
|
513 |
if ((ret) < 0) { \ |
|
514 |
if (ferror(f)) { \ |
|
515 |
if (__threadErrno == EINTR) { \ |
|
516 |
clearerr(f); \ |
|
517 |
/* refetch */ \ |
|
518 |
buf = (char *)(obj);\ |
|
519 |
continue; \ |
|
520 |
} \ |
|
521 |
} else { \ |
|
522 |
(ret) = 0; \ |
|
523 |
} \ |
|
524 |
break; \ |
|
525 |
} \ |
|
526 |
(buf)[__ooffs+__offs] = (ret); \ |
|
527 |
__offs++; \ |
|
528 |
} \ |
|
529 |
if (__offs > 0) \ |
|
530 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
531 |
} else { \ |
18797 | 532 |
while (__offs < (cnt)) { \ |
533 |
char __buf[IO_BUFFER_SIZE]; \ |
|
534 |
OBJ rA = __INST(readAhead); \ |
|
535 |
if (rA != nil) { \ |
|
536 |
(buf)[__ooffs+__offs] = __intVal(rA);\ |
|
537 |
__INST(readAhead) = nil; \ |
|
538 |
(ret) = 1; \ |
|
539 |
__offs++; \ |
|
540 |
continue; \ |
|
541 |
} \ |
|
542 |
{ \ |
|
543 |
int res = 0; \ |
|
544 |
int l = (cnt)-__offs; \ |
|
545 |
CLEAR_ERRNO; \ |
|
546 |
if (l > IO_BUFFER_SIZE) \ |
|
547 |
l = IO_BUFFER_SIZE; \ |
|
548 |
if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)), FIONREAD, &res), 1)) \ |
|
549 |
|| (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \ |
|
550 |
|| (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0,0,0,&res,0), 1))) { \ |
|
551 |
if (res > 0) { \ |
|
552 |
if (res > l) res = l; \ |
|
553 |
READ((ret), f, __buf, res, handleType); \ |
|
554 |
} else { \ |
|
555 |
(ret) = 0; \ |
|
556 |
break; \ |
|
557 |
} \ |
|
558 |
} else { \ |
|
559 |
READ((ret), f, __buf, l, handleType); \ |
|
560 |
} \ |
|
561 |
if ((ret) <= 0) { \ |
|
562 |
if (ret < 0 && __threadErrno == EINTR) \ |
|
563 |
continue; \ |
|
564 |
break; \ |
|
565 |
} \ |
|
566 |
} \ |
|
567 |
if ((ret) > 0) { \ |
|
568 |
buf = (char *)(obj); \ |
|
569 |
memcpy((buf)+__ooffs+__offs, __buf, (ret)); \ |
|
570 |
__offs += (ret); \ |
|
571 |
} else { \ |
|
572 |
(ret) = 0; \ |
|
573 |
} \ |
|
574 |
} \ |
|
575 |
if (__offs > 0) \ |
|
576 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
577 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
578 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
579 |
|
12548 | 580 |
# define __WRITEBYTE__(ret, f, buf, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
581 |
if (isBuffered) { \ |
18797 | 582 |
for (;;) { \ |
583 |
CLEAR_ERRNO; \ |
|
584 |
ret = putc(*(buf), f); \ |
|
585 |
if ((ret) >= 0) { \ |
|
586 |
(ret) = 1; \ |
|
587 |
} else if (ferror(f)) { \ |
|
588 |
if (__threadErrno == EINTR) { \ |
|
589 |
clearerr(f); \ |
|
590 |
continue; \ |
|
591 |
} \ |
|
592 |
} else \ |
|
593 |
(ret) = 0; \ |
|
594 |
break; \ |
|
595 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
596 |
} else { \ |
18797 | 597 |
for (;;) { \ |
598 |
CLEAR_ERRNO; \ |
|
599 |
WRITE(ret,f, buf, 1, handleType); \ |
|
600 |
if ((ret) >= 0 || __threadErrno != EINTR) \ |
|
601 |
break; \ |
|
602 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
603 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
604 |
|
12548 | 605 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
606 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
607 |
if (isBuffered) { \ |
18797 | 608 |
int __offs = 0; \ |
609 |
while (__offs < (cnt)) { \ |
|
610 |
CLEAR_ERRNO; \ |
|
611 |
(ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\ |
|
612 |
if ((ret) <= 0) { \ |
|
613 |
if (ferror(f)) { \ |
|
614 |
if (__threadErrno == EINTR) { \ |
|
615 |
clearerr(f); \ |
|
616 |
continue; \ |
|
617 |
} \ |
|
618 |
break; \ |
|
619 |
} else { \ |
|
620 |
(ret) = 0; \ |
|
621 |
} \ |
|
622 |
} \ |
|
623 |
__offs += (ret); \ |
|
624 |
} \ |
|
625 |
if (__offs > 0) \ |
|
626 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
627 |
} else { \ |
18797 | 628 |
int __offs = 0; \ |
629 |
while (__offs < (cnt)) { \ |
|
630 |
CLEAR_ERRNO; \ |
|
631 |
WRITE(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
632 |
if (ret <= 0) { \ |
|
633 |
if (ret < 0 && __threadErrno == EINTR) { \ |
|
634 |
continue; \ |
|
635 |
} \ |
|
636 |
break; \ |
|
637 |
} \ |
|
638 |
__offs += (ret); \ |
|
639 |
} \ |
|
640 |
if (__offs > 0) \ |
|
641 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
642 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
643 |
|
12548 | 644 |
# define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
645 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
646 |
int __ooffs = obj_offs; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
647 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
648 |
char *buf = (char *)(obj); \ |
18797 | 649 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
650 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
651 |
if (isBuffered) { \ |
18797 | 652 |
while (__offs < (cnt)) { \ |
653 |
CLEAR_ERRNO; \ |
|
654 |
ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \ |
|
655 |
if ((ret) <= 0) { \ |
|
656 |
if (ferror(f)) { \ |
|
657 |
if (__threadErrno == EINTR) { \ |
|
658 |
/* refetch */ \ |
|
659 |
buf = (char *)(obj); \ |
|
660 |
clearerr(f); \ |
|
661 |
continue; \ |
|
662 |
} \ |
|
663 |
break; \ |
|
664 |
} else { \ |
|
665 |
(ret) = 0; \ |
|
666 |
} \ |
|
667 |
} \ |
|
668 |
__offs += (ret); \ |
|
669 |
} \ |
|
670 |
if (__offs > 0) \ |
|
671 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
672 |
} else { \ |
18797 | 673 |
while (__offs < (cnt)) { \ |
674 |
char __buf[IO_BUFFER_SIZE]; \ |
|
675 |
int l; \ |
|
676 |
CLEAR_ERRNO; \ |
|
677 |
l = (cnt)-__offs; \ |
|
678 |
if ( l > IO_BUFFER_SIZE) \ |
|
679 |
l = IO_BUFFER_SIZE; \ |
|
680 |
/* refetch */ \ |
|
681 |
buf = (char *)(obj); \ |
|
682 |
memcpy(__buf,(buf)+__ooffs+__offs,l); \ |
|
683 |
WRITE(ret,f, __buf, l, handleType); \ |
|
684 |
if (ret <= 0) { \ |
|
685 |
if (ret < 0 && __threadErrno == EINTR) { \ |
|
686 |
continue; \ |
|
687 |
} \ |
|
688 |
break; \ |
|
689 |
} \ |
|
690 |
__offs += (ret); \ |
|
691 |
} \ |
|
692 |
if (__offs > 0) \ |
|
693 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
694 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
695 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
696 |
|
16296 | 697 |
#else /* ! WIN32 */ |
698 |
/* ======================== UNIX / LINUX ====================================================== */ |
|
16373
9b9827530bf4
Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents:
16368
diff
changeset
|
699 |
typedef int SOCKET; |
9b9827530bf4
Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents:
16368
diff
changeset
|
700 |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
701 |
# define __READING__(f) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
702 |
if ((__INST(didWrite) != false) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
703 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
18797 | 704 |
__INST(didWrite) = false; \ |
705 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
706 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
707 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
708 |
# define __WRITING__(f) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
709 |
if ((__INST(didWrite) != true) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
710 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
18797 | 711 |
__INST(didWrite) = true; \ |
712 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
713 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
714 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
715 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
716 |
# ifdef NO_STDIO |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
717 |
# define __UNGETC__(c, f, isBuffered) \ |
8913 | 718 |
__INST(readAhead) = __mkSmallInteger((c)); |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
719 |
# else /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
720 |
# define __UNGETC__(c, f, isBuffered) \ |
2264 | 721 |
if (isBuffered) { \ |
18797 | 722 |
ungetc((c), (f)); \ |
2264 | 723 |
} else { \ |
18797 | 724 |
__INST(readAhead) = __mkSmallInteger((c)); \ |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
725 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
726 |
# endif /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
727 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
728 |
# ifdef NO_STDIO |
12548 | 729 |
# define __READBYTE__(ret, f, buf, isBuffered, handleType) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
730 |
{ \ |
18797 | 731 |
OBJ rA = __INST(readAhead); \ |
732 |
if (rA != nil) { \ |
|
733 |
*(buf) = __intVal(rA); \ |
|
734 |
DEBUGBUFFER(buf); \ |
|
735 |
__INST(readAhead) = nil; \ |
|
736 |
(ret) = 1; \ |
|
737 |
} else { \ |
|
738 |
for (;;) { \ |
|
739 |
CLEAR_ERRNO; \ |
|
740 |
(ret) = READ(f, buf, 1, handleType); \ |
|
741 |
DEBUGBUFFER(buf); \ |
|
742 |
if ((ret) >= 0) break; \ |
|
743 |
if (errno != EINTR) { \ |
|
744 |
break; \ |
|
745 |
} \ |
|
746 |
__HANDLE_INTERRUPTS__; \ |
|
747 |
} \ |
|
748 |
} \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
749 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
750 |
# else /* use STDIO */ |
12548 | 751 |
# define __READBYTE__(ret, f, buf, isBuffered, handleType) \ |
2264 | 752 |
if (isBuffered) { \ |
18797 | 753 |
for (;;) { \ |
754 |
CLEAR_ERRNO; \ |
|
755 |
(ret) = getc(f); \ |
|
756 |
if ((ret) >= 0) { \ |
|
757 |
DEBUGBUFFER(buf); \ |
|
758 |
*(buf) = (ret); \ |
|
759 |
(ret) = 1; \ |
|
760 |
} else if (ferror(f)) { \ |
|
761 |
if (errno == EINTR) { \ |
|
762 |
__HANDLE_INTERRUPTS__; \ |
|
763 |
clearerr(f); \ |
|
764 |
continue; \ |
|
765 |
} \ |
|
766 |
} else \ |
|
767 |
(ret) = 0; \ |
|
768 |
break; \ |
|
769 |
} \ |
|
2264 | 770 |
} else { \ |
18797 | 771 |
OBJ rA = __INST(readAhead); \ |
772 |
if (rA != nil) { \ |
|
773 |
*(buf) = __intVal(rA); \ |
|
774 |
DEBUGBUFFER(buf); \ |
|
775 |
__INST(readAhead) = nil; \ |
|
776 |
(ret) = 1; \ |
|
777 |
} else { \ |
|
778 |
for (;;) { \ |
|
779 |
CLEAR_ERRNO; \ |
|
780 |
(ret) = read(fileno(f), buf, 1); \ |
|
781 |
DEBUGBUFFER(buf); \ |
|
782 |
if ((ret) >= 0) break; \ |
|
783 |
if (errno != EINTR) { \ |
|
784 |
break; \ |
|
785 |
} \ |
|
786 |
__HANDLE_INTERRUPTS__; \ |
|
787 |
} \ |
|
788 |
} \ |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
789 |
} |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
790 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
791 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
792 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
793 |
* 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
|
794 |
* (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
|
795 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
796 |
# ifdef NO_STDIO |
12548 | 797 |
# define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
798 |
{ \ |
18797 | 799 |
int __offs = 0, __cnt; \ |
800 |
\ |
|
801 |
while (__offs < (cnt)) { \ |
|
802 |
OBJ rA = __INST(readAhead); \ |
|
803 |
if (rA != nil) { \ |
|
804 |
(buf)[__offs] = __intVal(rA); \ |
|
805 |
DEBUGBUFFER(buf); \ |
|
806 |
__INST(readAhead) = nil; \ |
|
807 |
__offs++; \ |
|
808 |
} else { \ |
|
809 |
CLEAR_ERRNO; \ |
|
810 |
__cnt = READ(f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
811 |
DEBUGBUFFER(buf); \ |
|
812 |
if (__cnt <= 0) { \ |
|
813 |
if (__cnt < 0 && errno == EINTR) { \ |
|
814 |
__HANDLE_INTERRUPTS__; \ |
|
815 |
continue; \ |
|
816 |
} \ |
|
817 |
break; \ |
|
818 |
} \ |
|
819 |
__offs += __cnt; \ |
|
820 |
} \ |
|
821 |
} \ |
|
822 |
if (__offs > 0) \ |
|
823 |
(ret) = __offs; \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
824 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
825 |
# else /* use STDIO */ |
12548 | 826 |
# define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
827 |
(ret) = 0; \ |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
828 |
if (isBuffered) { \ |
18797 | 829 |
int __offs = 0; \ |
830 |
while (__offs < (cnt)) { \ |
|
831 |
CLEAR_ERRNO; \ |
|
832 |
(ret) = getc(f); \ |
|
833 |
if ((ret) < 0) { \ |
|
834 |
if (ferror(f)) { \ |
|
835 |
if (errno == EINTR) { \ |
|
836 |
__HANDLE_INTERRUPTS__; \ |
|
837 |
clearerr(f); \ |
|
838 |
continue; \ |
|
839 |
} \ |
|
840 |
} else { \ |
|
841 |
(ret) = 0; \ |
|
842 |
} \ |
|
843 |
break; \ |
|
844 |
} \ |
|
845 |
DEBUGBUFFER(buf); \ |
|
846 |
(buf)[__offs++] = (ret); \ |
|
847 |
} \ |
|
848 |
if (__offs > 0) \ |
|
849 |
(ret) = __offs; \ |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
850 |
} else { \ |
18797 | 851 |
int __offs = 0, __cnt; \ |
852 |
int fd = fileno(f); \ |
|
853 |
\ |
|
854 |
while (__offs < (cnt)) { \ |
|
855 |
OBJ rA = __INST(readAhead); \ |
|
856 |
if (rA != nil) { \ |
|
857 |
DEBUGBUFFER(buf); \ |
|
858 |
(buf)[__offs] = __intVal(rA); \ |
|
859 |
__INST(readAhead) = nil; \ |
|
860 |
__offs++; \ |
|
861 |
} else { \ |
|
862 |
CLEAR_ERRNO; \ |
|
863 |
__cnt = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
864 |
DEBUGBUFFER(buf); \ |
|
865 |
if (__cnt <= 0) { \ |
|
866 |
if (__cnt < 0 && errno == EINTR) { \ |
|
867 |
__HANDLE_INTERRUPTS__; \ |
|
868 |
continue; \ |
|
869 |
} \ |
|
870 |
break; \ |
|
871 |
} \ |
|
872 |
__offs += __cnt; \ |
|
873 |
} \ |
|
874 |
} \ |
|
875 |
if (__offs > 0) \ |
|
876 |
(ret) = __offs; \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
877 |
} |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
878 |
|
6563
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
879 |
|
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
880 |
/* |
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
881 |
* FNDELAY and O_NDELAY is deprecated, O_NONBLOCK is used in POSIX, XPG, etc... |
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
882 |
*/ |
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
883 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
884 |
# if defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(O_NDELAY) || defined(FNDELAY)) |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
885 |
# define SETFLAGS(fd, flags) \ |
18797 | 886 |
fcntl(fd, F_SETFL, flags) |
6563
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
887 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
888 |
# if defined(O_NONBLOCK) |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
889 |
# define __STX_NONBLOCK_FLAG O_NONBLOCK |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
890 |
# else |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
891 |
# if defined(O_NDELAY) |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
892 |
# define __STX_NONBLOCK_FLAG O_NDELAY |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
893 |
# else |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
894 |
# define __STX_NONBLOCK_FLAG FNDELAY |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
895 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
896 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
897 |
|
15342 | 898 |
# define SETNONBLOCKING(fd, oldFlags) \ |
18797 | 899 |
{ \ |
900 |
int flags = fcntl(fd, F_GETFL, 0); \ |
|
901 |
if (flags >= 0) { \ |
|
902 |
fcntl(fd, F_SETFL, flags | __STX_NONBLOCK_FLAG); \ |
|
903 |
} \ |
|
904 |
oldFlags = flags; \ |
|
905 |
} |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
906 |
# else |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
907 |
# define SETFLAGS(fd, flags) /* nothing */ |
15342 | 908 |
# define SETNONBLOCKING(fd, oldFlags) /* nothing */ |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
909 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
910 |
|
12548 | 911 |
# define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
912 |
{ \ |
15342 | 913 |
int __offs = 0, __cnt; \ |
3681 | 914 |
int oldFlags; \ |
18797 | 915 |
\ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
916 |
(ret) = 0; \ |
15342 | 917 |
SETNONBLOCKING(fileno(f), oldFlags); \ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
918 |
if (isBuffered) { \ |
18797 | 919 |
while (__offs < (cnt)) { \ |
920 |
CLEAR_ERRNO; \ |
|
921 |
(ret) = getc(f); \ |
|
922 |
if ((ret) < 0) { \ |
|
923 |
if (ferror(f)) { \ |
|
924 |
if (errno == EINTR) { \ |
|
925 |
(ret) = 0; \ |
|
926 |
clearerr(f); \ |
|
927 |
break; \ |
|
928 |
} \ |
|
929 |
} else { \ |
|
930 |
(ret) = 0; \ |
|
931 |
} \ |
|
932 |
break; \ |
|
933 |
} \ |
|
934 |
(buf)[__offs++] = (ret); \ |
|
935 |
DEBUGBUFFER(buf); \ |
|
936 |
} \ |
|
937 |
if (__offs > 0) \ |
|
938 |
(ret) = __offs; \ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
939 |
} else { \ |
18797 | 940 |
int fd = fileno(f); \ |
941 |
\ |
|
942 |
while (__offs < (cnt)) { \ |
|
943 |
OBJ rA = __INST(readAhead); \ |
|
944 |
if (rA != nil) { \ |
|
945 |
(buf)[__offs] = __intVal(rA); \ |
|
946 |
DEBUGBUFFER(buf); \ |
|
947 |
__INST(readAhead) = nil; \ |
|
948 |
__offs++; \ |
|
949 |
continue; \ |
|
950 |
} \ |
|
951 |
CLEAR_ERRNO; \ |
|
952 |
__cnt = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
953 |
DEBUGBUFFER(buf); \ |
|
954 |
if (__cnt > 0) { \ |
|
955 |
__offs += __cnt; \ |
|
956 |
} \ |
|
957 |
break; \ |
|
958 |
} \ |
|
959 |
if (__offs > 0) \ |
|
960 |
(ret) = __offs; \ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
961 |
} \ |
3681 | 962 |
SETFLAGS(fileno(f), oldFlags); \ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
963 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
964 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
965 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
966 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
967 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
968 |
* 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
|
969 |
* (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
|
970 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
971 |
# ifdef NO_STDIO |
12548 | 972 |
# define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
973 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
974 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
975 |
int __offs = 0; \ |
7540
7cd542f29024
Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents:
7503
diff
changeset
|
976 |
int __cnt; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
977 |
char *buf = (char *)(obj); \ |
18797 | 978 |
\ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
979 |
(ret) = 0; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
980 |
{ \ |
18797 | 981 |
while (__offs < (cnt)) { \ |
982 |
OBJ rA = __INST(readAhead); \ |
|
983 |
if (rA != nil) { \ |
|
984 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
985 |
DEBUGBUFFER(buf); \ |
|
986 |
__INST(readAhead) = nil; \ |
|
987 |
__offs++; \ |
|
988 |
} else { \ |
|
989 |
CLEAR_ERRNO; \ |
|
990 |
__cnt = READ(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \ |
|
991 |
DEBUGBUFFER(buf); \ |
|
992 |
if (__cnt <= 0) { \ |
|
993 |
if (__cnt < 0 && errno == EINTR) { \ |
|
994 |
__HANDLE_INTERRUPTS__; \ |
|
995 |
/* refetch */ \ |
|
996 |
buf = (char *)(obj); \ |
|
997 |
continue; \ |
|
998 |
} \ |
|
999 |
break; \ |
|
1000 |
} \ |
|
1001 |
__offs += __cnt; \ |
|
1002 |
} \ |
|
1003 |
} \ |
|
1004 |
if (__offs > 0) \ |
|
1005 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1006 |
} \ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1007 |
} |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
1008 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1009 |
# else /* use STDIO */ |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
1010 |
|
12548 | 1011 |
# define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1012 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1013 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1014 |
int __offs = 0; \ |
7540
7cd542f29024
Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents:
7503
diff
changeset
|
1015 |
int __cnt; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1016 |
char *buf = (char *)(obj); \ |
18797 | 1017 |
\ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1018 |
(ret) = 0; \ |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1019 |
if (isBuffered) { \ |
18797 | 1020 |
while (__offs < (cnt)) { \ |
1021 |
CLEAR_ERRNO; \ |
|
1022 |
(ret) = getc(f); \ |
|
1023 |
if ((ret) < 0) { \ |
|
1024 |
if (ferror(f)) { \ |
|
1025 |
if (errno == EINTR) { \ |
|
1026 |
__HANDLE_INTERRUPTS__; \ |
|
1027 |
clearerr(f); \ |
|
1028 |
/* refetch */ \ |
|
1029 |
buf = (char *)(obj); \ |
|
1030 |
DEBUGBUFFER(buf); \ |
|
1031 |
continue; \ |
|
1032 |
} \ |
|
1033 |
} else { \ |
|
1034 |
(ret) = 0; \ |
|
1035 |
} \ |
|
1036 |
break; \ |
|
1037 |
} \ |
|
1038 |
(buf)[__ooffs+__offs] = (ret); \ |
|
1039 |
DEBUGBUFFER(buf); \ |
|
1040 |
__offs++; \ |
|
1041 |
} \ |
|
1042 |
if (__offs > 0) \ |
|
1043 |
(ret) = __offs; \ |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1044 |
} else { \ |
18797 | 1045 |
int fd = fileno(f); \ |
1046 |
\ |
|
1047 |
while (__offs < (cnt)) { \ |
|
1048 |
OBJ rA = __INST(readAhead); \ |
|
1049 |
if (rA != nil) { \ |
|
1050 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
1051 |
DEBUGBUFFER(buf); \ |
|
1052 |
__INST(readAhead) = nil; \ |
|
1053 |
__offs++; \ |
|
1054 |
} else { \ |
|
1055 |
CLEAR_ERRNO; \ |
|
1056 |
__cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
1057 |
DEBUGBUFFER(buf); \ |
|
1058 |
if (__cnt <= 0) { \ |
|
1059 |
if (__cnt < 0 && errno == EINTR) { \ |
|
1060 |
__HANDLE_INTERRUPTS__; \ |
|
1061 |
/* refetch */ \ |
|
1062 |
buf = (char *)(obj); \ |
|
1063 |
continue; \ |
|
1064 |
} \ |
|
1065 |
break; \ |
|
1066 |
} \ |
|
1067 |
__offs += __cnt; \ |
|
1068 |
} \ |
|
1069 |
} \ |
|
1070 |
if (__offs > 0) \ |
|
1071 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1072 |
} \ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1073 |
} |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1074 |
|
12548 | 1075 |
# define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1076 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1077 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1078 |
int __offs = 0; \ |
7540
7cd542f29024
Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents:
7503
diff
changeset
|
1079 |
int __cnt; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1080 |
char *buf = (char *)(obj); \ |
15342 | 1081 |
int oldFlags; \ |
18797 | 1082 |
\ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1083 |
(ret) = 0; \ |
15342 | 1084 |
SETNONBLOCKING(fileno(f), oldFlags); \ |
18797 | 1085 |
\ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1086 |
if (isBuffered) { \ |
18797 | 1087 |
while (__offs < (cnt)) { \ |
1088 |
CLEAR_ERRNO; \ |
|
1089 |
(ret) = getc(f); \ |
|
1090 |
if ((ret) < 0) { \ |
|
1091 |
if (ferror(f)) { \ |
|
1092 |
if (errno == EINTR) { \ |
|
1093 |
clearerr(f); \ |
|
1094 |
/* refetch */ \ |
|
1095 |
buf = (char *)(obj); \ |
|
1096 |
(ret) = 0; \ |
|
1097 |
break; \ |
|
1098 |
} \ |
|
1099 |
} else { \ |
|
1100 |
(ret) = 0; \ |
|
1101 |
} \ |
|
1102 |
break; \ |
|
1103 |
} \ |
|
1104 |
(buf)[__ooffs+__offs] = (ret); \ |
|
1105 |
DEBUGBUFFER(buf); \ |
|
1106 |
__offs++; \ |
|
1107 |
} \ |
|
1108 |
if (__offs > 0) \ |
|
1109 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1110 |
} else { \ |
18797 | 1111 |
int fd = fileno(f); \ |
1112 |
\ |
|
1113 |
while (__offs < (cnt)) { \ |
|
1114 |
OBJ rA = __INST(readAhead); \ |
|
1115 |
if (rA != nil) { \ |
|
1116 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
1117 |
DEBUGBUFFER(buf); \ |
|
1118 |
__INST(readAhead) = nil; \ |
|
1119 |
__offs++; \ |
|
1120 |
continue; \ |
|
1121 |
} \ |
|
1122 |
CLEAR_ERRNO; \ |
|
1123 |
__cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
1124 |
DEBUGBUFFER(buf); \ |
|
1125 |
if (__cnt > 0) { \ |
|
1126 |
__offs += __cnt; \ |
|
1127 |
} \ |
|
1128 |
break; \ |
|
1129 |
} \ |
|
1130 |
if (__offs > 0) \ |
|
1131 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1132 |
} \ |
15342 | 1133 |
SETFLAGS(fileno(f), oldFlags); \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1134 |
} |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1135 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1136 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1137 |
# endif /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1138 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1139 |
# ifdef NO_STDIO |
12548 | 1140 |
# define __WRITEBYTE__(ret, f, buf, isBuffered, handleType) \ |
18797 | 1141 |
for (;;) { \ |
1142 |
CLEAR_ERRNO; \ |
|
1143 |
(ret) = WRITE(f, buf, 1, handleType); \ |
|
1144 |
if ((ret) >= 0) break; \ |
|
1145 |
if (errno != EINTR) { \ |
|
1146 |
break; \ |
|
1147 |
} \ |
|
1148 |
__HANDLE_INTERRUPTS__; \ |
|
1149 |
} |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1150 |
# else /* use STDIO */ |
12548 | 1151 |
# define __WRITEBYTE__(ret, f, buf, isBuffered, handleType) \ |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1152 |
if (isBuffered) { \ |
18797 | 1153 |
for (;;) { \ |
1154 |
CLEAR_ERRNO; \ |
|
1155 |
ret = putc(*(buf), f); \ |
|
1156 |
if ((ret) >= 0) { \ |
|
1157 |
(ret) = 1; \ |
|
1158 |
} else if (ferror(f)) { \ |
|
1159 |
/* SOLARIS/SPARC (2.6) generates spurious errors with errno = 0 */ \ |
|
1160 |
if (errno == EINTR || errno == 0) { \ |
|
1161 |
__HANDLE_INTERRUPTS__; \ |
|
1162 |
clearerr(f); \ |
|
1163 |
continue; \ |
|
1164 |
} \ |
|
1165 |
} else \ |
|
1166 |
(ret) = 0; \ |
|
1167 |
break; \ |
|
1168 |
} \ |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1169 |
} else { \ |
18797 | 1170 |
for (;;) { \ |
1171 |
CLEAR_ERRNO; \ |
|
1172 |
(ret) = write(fileno(f), buf, 1); \ |
|
1173 |
if ((ret) >= 0) break; \ |
|
1174 |
if (errno != EINTR) { \ |
|
1175 |
break; \ |
|
1176 |
} \ |
|
1177 |
__HANDLE_INTERRUPTS__; \ |
|
1178 |
} \ |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1179 |
} |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1180 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1181 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1182 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1183 |
* 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
|
1184 |
* (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
|
1185 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1186 |
# ifdef NO_STDIO |
12548 | 1187 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
2961 | 1188 |
(ret) = 0; \ |
1189 |
{ \ |
|
18797 | 1190 |
int __offs = 0; \ |
1191 |
while (__offs < (cnt)) { \ |
|
1192 |
CLEAR_ERRNO; \ |
|
1193 |
ret = WRITE(f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
1194 |
if (ret <= 0) { \ |
|
1195 |
if (ret < 0 && errno == EINTR) { \ |
|
1196 |
__HANDLE_INTERRUPTS__; \ |
|
1197 |
continue; \ |
|
1198 |
} \ |
|
1199 |
break; \ |
|
1200 |
} \ |
|
1201 |
__offs += (ret); \ |
|
1202 |
} \ |
|
1203 |
if (__offs > 0) \ |
|
1204 |
(ret) = __offs; \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1205 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1206 |
# else /* use STDIO */ |
12548 | 1207 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
2961 | 1208 |
(ret) = 0; \ |
1209 |
if (isBuffered) { \ |
|
18797 | 1210 |
int __offs = 0; \ |
1211 |
while (__offs < (cnt)) { \ |
|
1212 |
CLEAR_ERRNO; \ |
|
1213 |
(ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\ |
|
1214 |
if ((ret) <= 0) { \ |
|
1215 |
if (ferror(f)) { \ |
|
1216 |
if (errno == EINTR) { \ |
|
1217 |
__HANDLE_INTERRUPTS__; \ |
|
1218 |
clearerr(f); \ |
|
1219 |
continue; \ |
|
1220 |
} \ |
|
1221 |
} else { \ |
|
1222 |
(ret) = 0; \ |
|
1223 |
} \ |
|
1224 |
break; \ |
|
1225 |
} \ |
|
1226 |
__offs += (ret); \ |
|
1227 |
} \ |
|
1228 |
if (__offs > 0) \ |
|
1229 |
(ret) = __offs; \ |
|
2961 | 1230 |
} else { \ |
18797 | 1231 |
int __offs = 0; \ |
1232 |
while (__offs < (cnt)) { \ |
|
1233 |
CLEAR_ERRNO; \ |
|
1234 |
(ret) = write(fileno(f), (buf)+__offs, (cnt)-__offs);\ |
|
1235 |
if ((ret) <= 0) { \ |
|
1236 |
if ((ret) < 0) { \ |
|
1237 |
if (errno == EINTR) { \ |
|
1238 |
__HANDLE_INTERRUPTS__; \ |
|
1239 |
continue; \ |
|
1240 |
} \ |
|
1241 |
} \ |
|
1242 |
break; \ |
|
1243 |
} \ |
|
1244 |
__offs += (ret); \ |
|
1245 |
} \ |
|
1246 |
if (__offs > 0) \ |
|
1247 |
(ret) = __offs; \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1248 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1249 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1250 |
|
223 | 1251 |
/* |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1252 |
* 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
|
1253 |
* (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
|
1254 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1255 |
# ifdef NO_STDIO |
12548 | 1256 |
# define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1257 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1258 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1259 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1260 |
char *buf = (char *)(obj); \ |
18797 | 1261 |
\ |
2961 | 1262 |
(ret) = 0; \ |
1263 |
{ \ |
|
18797 | 1264 |
while (__offs < (cnt)) { \ |
1265 |
CLEAR_ERRNO; \ |
|
1266 |
ret = WRITE(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \ |
|
1267 |
if (ret <= 0) { \ |
|
1268 |
if (ret < 0 && errno == EINTR) { \ |
|
1269 |
__HANDLE_INTERRUPTS__; \ |
|
1270 |
/* refetch */ \ |
|
1271 |
buf = (char *)(obj); \ |
|
1272 |
continue; \ |
|
1273 |
} \ |
|
1274 |
break; \ |
|
1275 |
} \ |
|
1276 |
__offs += (ret); \ |
|
1277 |
} \ |
|
1278 |
if (__offs > 0) \ |
|
1279 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1280 |
} \ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1281 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1282 |
# else /* use STDIO */ |
12548 | 1283 |
# define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1284 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1285 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1286 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1287 |
char *buf = (char *)(obj); \ |
18797 | 1288 |
\ |
2961 | 1289 |
(ret) = 0; \ |
1290 |
if (isBuffered) { \ |
|
18797 | 1291 |
while (__offs < (cnt)) { \ |
1292 |
CLEAR_ERRNO; \ |
|
1293 |
(ret) = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \ |
|
1294 |
if ((ret) <= 0) { \ |
|
1295 |
if (ferror(f)) { \ |
|
1296 |
if (errno == EINTR) { \ |
|
1297 |
__HANDLE_INTERRUPTS__; \ |
|
1298 |
/* refetch */ \ |
|
1299 |
buf = (char *)(obj); \ |
|
1300 |
clearerr(f); \ |
|
1301 |
continue; \ |
|
1302 |
} \ |
|
1303 |
break; \ |
|
1304 |
} else { \ |
|
1305 |
(ret) = 0; \ |
|
1306 |
} \ |
|
1307 |
} \ |
|
1308 |
__offs += (ret); \ |
|
1309 |
} \ |
|
2961 | 1310 |
} else { \ |
18797 | 1311 |
while (__offs < (cnt)) { \ |
1312 |
CLEAR_ERRNO; \ |
|
1313 |
(ret) = write(fileno(f), (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
1314 |
if ((ret) <= 0) { \ |
|
1315 |
if ((ret) < 0) { \ |
|
1316 |
if (errno == EINTR){ \ |
|
1317 |
__HANDLE_INTERRUPTS__; \ |
|
1318 |
/* refetch */ \ |
|
1319 |
buf = (char *)(obj); \ |
|
1320 |
continue; \ |
|
1321 |
} \ |
|
1322 |
} \ |
|
1323 |
break; \ |
|
1324 |
} \ |
|
1325 |
__offs += (ret); \ |
|
1326 |
} \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1327 |
} \ |
15845 | 1328 |
if (__offs > 0) \ |
18797 | 1329 |
(ret) = __offs; \ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1330 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1331 |
# endif /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1332 |
#endif /* unix */ |
1 | 1333 |
%} |
188 | 1334 |
! ! |
1 | 1335 |
|
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1336 |
!ExternalStream class methodsFor:'documentation'! |
22 | 1337 |
|
88 | 1338 |
copyright |
1339 |
" |
|
1340 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
18797 | 1341 |
All Rights Reserved |
88 | 1342 |
|
1343 |
This software is furnished under a license and may be used |
|
1344 |
only in accordance with the terms of that license and with the |
|
1345 |
inclusion of the above copyright notice. This software may not |
|
1346 |
be provided or otherwise made available to, or used by, any |
|
1347 |
other person. No title to or ownership of the software is |
|
1348 |
hereby transferred. |
|
1349 |
" |
|
1350 |
! |
|
1351 |
||
22 | 1352 |
documentation |
1353 |
" |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1354 |
ExternalStream defines protocol common to Streams which have a file-descriptor and |
68 | 1355 |
represent some file or communicationChannel of the underlying OperatingSystem. |
1356 |
ExternalStream is abstract; concrete classes are FileStream, PipeStream etc. |
|
77 | 1357 |
|
159 | 1358 |
ExternalStreams can be in two modes: text- (the default) and binary-mode. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1359 |
In text-mode, the elements read/written are characters; |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1360 |
while in binary-mode the basic elements are bytes which read/write as SmallIntegers |
68 | 1361 |
in the range 0..255. |
22 | 1362 |
|
68 | 1363 |
Also, the stream can be either in buffered or unbuffered mode. In buffered mode, |
1364 |
data is not written until either a cr is written (in text mode) or a synchronizeOutput |
|
1365 |
is sent (in both modes). |
|
22 | 1366 |
|
77 | 1367 |
The underlying OperatingSystem streams may either be closed explicitely (sending a close) |
68 | 1368 |
or just forgotten - in this case, the garbage collector will eventually collect the |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1369 |
object AND a close will be performed automatically (but you will NOT know when this |
77 | 1370 |
happens - so it is recommended, that you close your files when no longer needed). |
159 | 1371 |
Closing is also suggested, since if smalltalk is finished (be it by purpose, or due to |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1372 |
some crash) the data will not be in the file, if unclosed. |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1373 |
All streams understand the close message, so it never hurts to use it (it is defined as |
77 | 1374 |
a noop in one of the superclasses). |
22 | 1375 |
|
68 | 1376 |
Most of the methods found here redefine inherited methods for better performance, |
1377 |
since I/O from/to files should be fast. |
|
22 | 1378 |
|
68 | 1379 |
Recovering a snapshot: |
1295 | 1380 |
not all streams can be restored to the state they had before - see the implementation of |
1381 |
reOpen in subclasses for more information. |
|
1382 |
For streams sitting on some communication channel (i.e. Pipes and Sockets) you should |
|
1383 |
reestablish the stream upon image restart (make someone dependent on ObjectMemory). |
|
1384 |
FileStreams are reopened and positioned to their offset they had at snapshot time. |
|
1385 |
This may fail, if the file was removed or renamed - or lead to confusion |
|
1386 |
if the contents changed in the meantime. |
|
1387 |
Therefore, it is a good idea to reopen files and check for these things at restart time. |
|
1388 |
||
1389 |
[Instance variables:] |
|
1390 |
||
18797 | 1391 |
handleType <Symbol> desribes what handle is: |
1392 |
win32: #fileHandle, #socketHandle, |
|
1393 |
#filePointer, #socketFilePointer, #pipeFilePointer |
|
1394 |
unix: #filePointer, #socketFilePointer, #pipeFilePointer |
|
1395 |
needed for win32, which uses different APIs for the different handles (sigh) |
|
1396 |
handle <Integer> used to be always a filePointer somehow mapped to an integer (FILE* - not the fd); |
|
1397 |
now, either a filePointer or a handle (win32) |
|
1398 |
mode <Symbol> #readwrite, #readonly or #writeonly |
|
1399 |
buffered <Boolean> true, if buffered (i.e. collects characters - does |
|
1400 |
not output immediately) |
|
1401 |
binary <Boolean> true if in binary mode (reads bytes instead of chars) |
|
1402 |
eolMode <Symbol> one of nil, #nl, #cr or #crlf. |
|
1403 |
determines how lines should be terminated. |
|
1404 |
nil -> newLine (as in Unix); |
|
1405 |
#crlf -> with cr-lf (as in MSDOS) |
|
1406 |
#cr -> with cr (as in VMS) |
|
1407 |
hitEOF <Boolean> true, if EOF was reached |
|
1408 |
||
1409 |
lastErrorNumber <Integer> the value of errno (only valid right after the error - |
|
1410 |
updated with next i/o operation) |
|
1295 | 1411 |
|
1412 |
[Class variables:] |
|
18797 | 1413 |
Lobby <Registry> keeps track of used ext-streams (to free up FILE*'s) |
1414 |
||
1415 |
StreamErrorSignal <Signal> parent of all stream errors (see Stream class) |
|
1416 |
InvalidReadSignal <Signal> raised on read from writeonly stream |
|
1417 |
InvalidWriteSignal <Signal> raised on write to readonly stream |
|
1418 |
InvalidModeSignal <Signal> raised on text I/O with binary-stream |
|
1419 |
or binary I/O with text-stream |
|
1420 |
OpenErrorSignal <Signal> raised if open fails |
|
1421 |
StreamNotOpenSignal <Signal> raised on I/O with non-open stream |
|
159 | 1422 |
|
1423 |
Additional notes: |
|
1424 |
This class is implemented using the underlying stdio-c library package, which |
|
1425 |
has both advantages and disadvantages: since it is portable (posix defined), porting |
|
1426 |
ST/X to non-Unix machines is simplified. The disadvantage is that the stdio library |
|
1427 |
has big problems handling unbounded Streams, since the EOF handling in stdio is |
|
1428 |
not prepared for data to arrive after EOF has been reached - time will show, if we need |
|
1429 |
a complete rewrite for UnboundedStream ... |
|
325 | 1430 |
|
188 | 1431 |
Also, depending on the system, the stdio library behaves infriendly when signals |
1432 |
occur while reading (for example, timer interrupts) - on real unixes (i.e. BSD) the signal |
|
1433 |
is handled transparently - on SYS5.3 (i.e. non unixes :-) the read operation returns |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1434 |
an error and errno is set to EINTR. |
1295 | 1435 |
Thats what the ugly code around all getc-calls is for ... |
1436 |
Since things get more and more ugly - we will rewrite ExternalStream |
|
1437 |
completely, to NOT use any stdio stuff (and do its buffering itself). |
|
325 | 1438 |
|
1439 |
Notice that typical stdio's use a single errno global variable to return an error code, |
|
16508 | 1440 |
this was bad design in the stdio lib (right from the very beginning), since it's much |
325 | 1441 |
harder to deal with this in the presence of lightweight processes, where errno gets |
1442 |
overwritten by an I/O operation done in another thread. (stdio should have been written |
|
1443 |
to return errno as a negative number ...). |
|
1444 |
To deal with this, the scheduler treats errno like a per-thread private variable, |
|
1445 |
and saves/restores the errno setting when switching to another thread. |
|
1446 |
(Notice that some thread packages do this also, but ST/X's thread implementation |
|
1447 |
does not depend on those, but instead uses a portable private package). |
|
1448 |
||
216 | 1449 |
Finally, if an stdio-stream is open for both reading and writing, we have to call |
1450 |
fseek whenever we are about to read after write and vice versa. |
|
1451 |
Two macros (__READING__ and __WRITING__) have been defined to be used before every |
|
1452 |
fread/fgetc and fwrite/putc respectively. |
|
1295 | 1453 |
|
1454 |
[author:] |
|
18797 | 1455 |
Claus Gittinger |
1456 |
Stefan Vogel (many, many fixes ...) |
|
1295 | 1457 |
|
1458 |
[see also:] |
|
18797 | 1459 |
FileStream Socket PipeStream |
1460 |
Filename OperatingSystem |
|
22 | 1461 |
" |
3255 | 1462 |
! |
1463 |
||
1464 |
examples |
|
1465 |
" |
|
1466 |
open a file, read the contents and display it in a textView: |
|
18797 | 1467 |
[exBegin] |
1468 |
|topView scrollPane textView fileStream text| |
|
1469 |
||
1470 |
topView := StandardSystemView new. |
|
1471 |
topView label:'contents of Makefile'. |
|
1472 |
||
1473 |
scrollPane := HVScrollableView in:topView. |
|
1474 |
scrollPane origin:0.0@0.0 corner:1.0@1.0. |
|
1475 |
||
1476 |
textView := EditTextView new. |
|
1477 |
scrollPane scrolledView:textView. |
|
1478 |
||
1479 |
fileStream := 'Makefile' asFilename readStream. |
|
1480 |
text := fileStream upToEnd. |
|
1481 |
fileStream close. |
|
1482 |
||
1483 |
textView contents:text. |
|
1484 |
||
1485 |
topView open. |
|
1486 |
[exEnd] |
|
3255 | 1487 |
|
1488 |
||
1489 |
Notice, all of the above can also be done (simply) as: |
|
18797 | 1490 |
[exBegin] |
1491 |
EditTextView openOn:'Makefile' |
|
1492 |
[exEnd] |
|
3255 | 1493 |
" |
22 | 1494 |
! ! |
1495 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1496 |
!ExternalStream class methodsFor:'initialization'! |
1 | 1497 |
|
18636 | 1498 |
closeFiles |
1499 |
"close all files. |
|
1500 |
To be called on exit of Smalltalk." |
|
1501 |
||
1502 |
Lobby do:[:eachFileStream | |
|
18797 | 1503 |
eachFileStream close |
18636 | 1504 |
]. |
1505 |
! |
|
1506 |
||
2920 | 1507 |
initDefaultEOLMode |
1508 |
OperatingSystem isUNIXlike ifTrue:[ |
|
18797 | 1509 |
"/ unix EOL conventions |
1510 |
DefaultEOLMode := #nl |
|
2920 | 1511 |
] ifFalse:[ |
18797 | 1512 |
OperatingSystem isVMSlike ifTrue:[ |
1513 |
"/ vms EOL conventions |
|
1514 |
DefaultEOLMode := #cr |
|
1515 |
] ifFalse:[ |
|
1516 |
"/ msdos EOL conventions |
|
1517 |
"/ msdos uses #crlf. but we keep #nl here, |
|
1518 |
"/ otherwise sending HTTP request over sockets fails |
|
17420 | 1519 |
"/ DefaultEOLMode := #crlf |
18797 | 1520 |
DefaultEOLMode := #nl |
1521 |
] |
|
2920 | 1522 |
] |
1523 |
! |
|
1524 |
||
1525 |
initModeStrings |
|
1526 |
"initialize modeStrings which are passed down to the underlying |
|
1527 |
fopen/fdopen functions." |
|
1528 |
||
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1529 |
OperatingSystem isMSDOSlike ifTrue:[ |
18797 | 1530 |
ReadMode := 'rb'. |
1531 |
ReadWriteMode := 'rb+'. |
|
1532 |
WriteMode := 'wb'. |
|
1533 |
AppendMode := 'ab+'. |
|
1534 |
CreateReadWriteMode := 'wb+'. |
|
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1535 |
] ifFalse:[ |
18797 | 1536 |
ReadMode := 'r'. |
1537 |
ReadWriteMode := 'r+'. |
|
1538 |
WriteMode := 'w'. |
|
1539 |
AppendMode := 'a+'. |
|
1540 |
CreateReadWriteMode := 'w+'. |
|
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1541 |
] |
2920 | 1542 |
! |
1543 |
||
1 | 1544 |
initialize |
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
1545 |
OpenErrorSignal isNil ifTrue:[ |
18790 | 1546 |
OpenErrorSignal := OpenError. |
1547 |
OpenErrorSignal notifierString:'open error'. |
|
1548 |
||
1549 |
InvalidReadSignal := InvalidReadError. |
|
1550 |
InvalidReadSignal notifierString:'stream does not support reading'. |
|
1551 |
||
1552 |
InvalidWriteSignal := InvalidWriteError. |
|
1553 |
InvalidWriteSignal notifierString:'stream does not support writing'. |
|
1554 |
||
1555 |
InvalidModeSignal := InvalidModeError. |
|
1556 |
InvalidModeSignal notifierString:'binary/text mode mismatch'. |
|
1557 |
||
1558 |
InvalidOperationSignal := InvalidOperationError. |
|
1559 |
InvalidOperationSignal notifierString:'unsupported file operation'. |
|
1560 |
||
1561 |
StreamNotOpenSignal := StreamNotOpenError. |
|
1562 |
StreamNotOpenSignal notifierString:'stream is not open'. |
|
1563 |
||
1564 |
StreamIOErrorSignal := StreamIOError. |
|
1565 |
StreamIOErrorSignal notifierString:'I/O error'. |
|
159 | 1566 |
]. |
1567 |
||
31 | 1568 |
Lobby isNil ifTrue:[ |
18790 | 1569 |
Lobby := Registry new. |
1570 |
||
1571 |
"want to get informed when returning from snapshot" |
|
1572 |
ObjectMemory addDependent:self |
|
2920 | 1573 |
]. |
5442 | 1574 |
DefaultEOLMode isNil ifTrue:[ |
18790 | 1575 |
self initDefaultEOLMode. |
5442 | 1576 |
]. |
1577 |
ReadMode isNil ifTrue:[ |
|
18790 | 1578 |
self initModeStrings. |
5442 | 1579 |
]. |
18790 | 1580 |
|
1581 |
"limit the amount of newspace to be used for non-tenurable executors to 5%" |
|
1582 |
MaxNonTenurableExecutors := ObjectMemory newSpaceSize // (Socket sizeOfInst:0) // 20. |
|
1583 |
||
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1584 |
"Modified: / 21.5.1998 / 16:33:53 / cg" |
1 | 1585 |
! |
1586 |
||
1587 |
reOpenFiles |
|
4108
8c4a45bc70eb
perform reOpen of streams earlier (before doing the view-reinit)
Claus Gittinger <cg@exept.de>
parents:
4093
diff
changeset
|
1588 |
"reopen all files (if possible) after a snapShot load. |
8c4a45bc70eb
perform reOpen of streams earlier (before doing the view-reinit)
Claus Gittinger <cg@exept.de>
parents:
4093
diff
changeset
|
1589 |
This is invoked via the #earlyRestart change notification." |
1 | 1590 |
|
18636 | 1591 |
Lobby do:[:eachFileStream | |
18797 | 1592 |
eachFileStream reOpen |
2920 | 1593 |
]. |
1 | 1594 |
! |
1595 |
||
1469 | 1596 |
update:something with:aParameter from:changedObject |
1 | 1597 |
"have to reopen files when returning from snapshot" |
1598 |
||
4108
8c4a45bc70eb
perform reOpen of streams earlier (before doing the view-reinit)
Claus Gittinger <cg@exept.de>
parents:
4093
diff
changeset
|
1599 |
something == #earlyRestart ifTrue:[ |
18797 | 1600 |
self reOpenFiles. |
1601 |
self initDefaultEOLMode |
|
1 | 1602 |
] |
1469 | 1603 |
|
1604 |
"Created: 15.6.1996 / 15:19:59 / cg" |
|
1 | 1605 |
! ! |
1606 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1607 |
!ExternalStream class methodsFor:'instance creation'! |
612 | 1608 |
|
12706 | 1609 |
forFileDescriptor:aFileDescriptor mode:modeSymbol |
2920 | 1610 |
"given a fileDescriptor, create an ExternalStream object |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1611 |
to operate on this fd. |
12706 | 1612 |
The modeSymbol-argument is #readonly, #readwrite, .... |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1613 |
This may be used to wrap fd's as returned by user |
2920 | 1614 |
primitive code, or to wrap pipe-fds into externalStreams." |
1615 |
||
12706 | 1616 |
^ self forFileDescriptor:aFileDescriptor mode:modeSymbol buffered:true handleType:nil |
7002 | 1617 |
|
1618 |
" |
|
1619 |
the example below will probably fail (15 is a random FD): |
|
1620 |
||
1621 |
|s| |
|
1622 |
||
1623 |
s := ExternalStream forFileDescriptor:15 mode:'r'. |
|
1624 |
s next. |
|
1625 |
" |
|
1626 |
||
1627 |
"Created: 29.2.1996 / 18:05:00 / cg" |
|
1628 |
"Modified: 29.2.1996 / 18:17:07 / cg" |
|
1629 |
! |
|
1630 |
||
12706 | 1631 |
forFileDescriptor:aFileDescriptor mode:modeSymbol buffered:buffered handleType:handleTypeSymbol |
7002 | 1632 |
"given a fileDescriptor, create an ExternalStream object |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1633 |
to operate on this fd. |
12706 | 1634 |
The modeSymbol-argument is #readonly, #readwrite, .... |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1635 |
This may be used to wrap fd's as returned by user |
7002 | 1636 |
primitive code, or to wrap pipe-fds into externalStreams." |
1637 |
||
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1638 |
|newStream| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1639 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1640 |
newStream := self basicNew. |
7002 | 1641 |
newStream text; buffered:buffered; eolMode:DefaultEOLMode; clearEOF. |
12706 | 1642 |
^ newStream connectTo:aFileDescriptor withMode:modeSymbol handleType:handleTypeSymbol |
2920 | 1643 |
|
1644 |
" |
|
1645 |
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
|
1646 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1647 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1648 |
|
12706 | 1649 |
s := ExternalStream forFileDescriptor:15 mode:#radonly handleType:#filePointer. |
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1650 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1651 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1652 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1653 |
"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
|
1654 |
"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
|
1655 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1656 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1657 |
forReadWriteToFileDescriptor:aFileDescriptor |
2920 | 1658 |
"given a fileDescriptor, create an ExternalStream object |
1659 |
to read/write from/to this fd. This may be used to wrap fd's |
|
1660 |
as returned by user primitive code, or to wrap pipe- |
|
1661 |
filedescriptors into externalStreams." |
|
1662 |
||
12706 | 1663 |
^ self forFileDescriptor:aFileDescriptor mode:#readWrite |
2920 | 1664 |
|
1665 |
" |
|
1666 |
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
|
1667 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1668 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1669 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1670 |
s := ExternalStream forReadWriteToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1671 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1672 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1673 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1674 |
"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
|
1675 |
"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
|
1676 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1677 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1678 |
forReadingFromFileDescriptor:aFileDescriptor |
2920 | 1679 |
"given a fileDescriptor, create an ExternalStream object |
1680 |
to read from this fd. This may be used to wrap fd's |
|
1681 |
as returned by user primitive code, or to wrap pipe- |
|
1682 |
filedescriptors into externalStreams." |
|
1683 |
||
12706 | 1684 |
^ self forFileDescriptor:aFileDescriptor mode:#readonly |
2920 | 1685 |
|
1686 |
" |
|
1687 |
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
|
1688 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1689 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1690 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1691 |
s := ExternalStream forReadingFromFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1692 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1693 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1694 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1695 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1696 |
|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
|
1697 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1698 |
'create OS pipe ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1699 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1700 |
pipe := OperatingSystem makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1701 |
readFd := pipe at:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1702 |
writeFd := pipe at:2. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1703 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1704 |
'connect Smalltalk streams ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1705 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1706 |
rs := ExternalStream forReadingFromFileDescriptor:readFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1707 |
ws := ExternalStream forWritingToFileDescriptor:writeFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1708 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1709 |
'read ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1710 |
[ |
18797 | 1711 |
1 to:10 do:[:i | |
1712 |
Transcript showCR:rs nextLine |
|
1713 |
]. |
|
1714 |
rs close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1715 |
] forkAt:7. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1716 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1717 |
'write ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1718 |
[ |
18797 | 1719 |
1 to:10 do:[:i | |
1720 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1721 |
]. |
|
1722 |
ws close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1723 |
] fork. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1724 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1725 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1726 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1727 |
"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
|
1728 |
"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
|
1729 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1730 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1731 |
forWritingToFileDescriptor:aFileDescriptor |
2920 | 1732 |
"given a fileDescriptor, create an ExternalStream object |
1733 |
to write to this fd. This may be used to wrap fd's |
|
1734 |
as returned by user primitive code, or to wrap pipe- |
|
1735 |
filedescriptors into externalStreams." |
|
1736 |
||
12706 | 1737 |
^ self forFileDescriptor:aFileDescriptor mode:#writeonly |
2920 | 1738 |
|
1739 |
" |
|
1740 |
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
|
1741 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1742 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1743 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1744 |
s := ExternalStream forWritingToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1745 |
s binary. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1746 |
s nextPut:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1747 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1748 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1749 |
"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
|
1750 |
"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
|
1751 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1752 |
|
612 | 1753 |
new |
15918
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
1754 |
"re-enable new - disabled in Stream superclass" |
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
1755 |
|
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
1756 |
^ self basicNew initialize. |
612 | 1757 |
! ! |
1758 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1759 |
!ExternalStream class methodsFor:'Signal constants'! |
159 | 1760 |
|
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1761 |
inaccessibleSignal |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1762 |
"ST-80 compatibility: return openErrorSignal" |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1763 |
|
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1764 |
^ self openErrorSignal |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1765 |
|
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1766 |
"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
|
1767 |
! |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1768 |
|
612 | 1769 |
invalidModeSignal |
1770 |
"return the signal raised when doing text-I/O with a binary stream |
|
1771 |
or binary-I/O with a text stream" |
|
1772 |
||
8074 | 1773 |
^ InvalidModeError |
159 | 1774 |
! |
1775 |
||
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1776 |
invalidOperationSignal |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1777 |
"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
|
1778 |
I/O operation is attempted" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1779 |
|
8074 | 1780 |
^ InvalidOperationError |
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1781 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1782 |
|
159 | 1783 |
invalidReadSignal |
1784 |
"return the signal raised when reading from writeonly streams" |
|
1785 |
||
8074 | 1786 |
^ InvalidReadError |
159 | 1787 |
! |
1788 |
||
1789 |
invalidWriteSignal |
|
1790 |
"return the signal raised when writing to readonly streams" |
|
1791 |
||
8074 | 1792 |
^ InvalidWriteError |
159 | 1793 |
! |
1794 |
||
612 | 1795 |
openErrorSignal |
1796 |
"return the signal raised when a file open failed" |
|
1797 |
||
1798 |
^ OpenErrorSignal |
|
1799 |
! |
|
1800 |
||
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1801 |
streamIOErrorSignal |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1802 |
"return the signal raised when an I/O error occurs. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1803 |
(for example, a device-IO-error, or reading an NFS-dir, |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1804 |
which is no longer available and has been mounted soft)" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1805 |
|
8074 | 1806 |
^ StreamIOError |
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1807 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1808 |
"Created: / 21.5.1998 / 16:32:55 / cg" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1809 |
! |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1810 |
|
159 | 1811 |
streamNotOpenSignal |
1812 |
"return the signal raised on I/O with closed streams" |
|
1813 |
||
8074 | 1814 |
^ StreamNotOpenError |
2 | 1815 |
! ! |
1816 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1817 |
!ExternalStream class methodsFor:'error handling'! |
92 | 1818 |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1819 |
errorReporter |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1820 |
"I know about error codes" |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1821 |
|
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1822 |
^ self |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1823 |
! |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1824 |
|
92 | 1825 |
lastErrorNumber |
159 | 1826 |
"return the errno of the last error" |
92 | 1827 |
|
1828 |
^ LastErrorNumber |
|
159 | 1829 |
|
1830 |
" |
|
1831 |
ExternalStream lastErrorNumber |
|
1832 |
" |
|
92 | 1833 |
! |
1834 |
||
1835 |
lastErrorString |
|
1836 |
"return a message string describing the last error" |
|
1837 |
||
1838 |
^ OperatingSystem errorTextForNumber:LastErrorNumber |
|
1839 |
||
1840 |
" |
|
1841 |
ExternalStream lastErrorString |
|
1842 |
" |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1843 |
! |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1844 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1845 |
reportOn:anErrorSymbolOrNumber |
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1846 |
"an error occured. |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1847 |
Report it via an Exception" |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1848 |
|
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1849 |
anErrorSymbolOrNumber isInteger ifTrue:[ |
18797 | 1850 |
StreamError |
1851 |
raiseRequestWith:anErrorSymbolOrNumber |
|
1852 |
errorString:(' - os error:' , (OperatingSystem errorTextForNumber:anErrorSymbolOrNumber)) |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1853 |
]. |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1854 |
(self respondsTo:anErrorSymbolOrNumber) ifTrue:[ |
18797 | 1855 |
self perform:anErrorSymbolOrNumber |
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1856 |
] ifFalse:[ |
18797 | 1857 |
StreamError |
1858 |
raiseRequestWith:anErrorSymbolOrNumber |
|
1859 |
errorString:(' - ' , anErrorSymbolOrNumber printString) |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1860 |
]. |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1861 |
^ false |
92 | 1862 |
! ! |
1863 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1864 |
!ExternalStream class methodsFor:'obsolete'! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1865 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1866 |
makePTYPair |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1867 |
"obsolete since 12-07-2003" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1868 |
<resource:#obsolete> |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1869 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1870 |
self obsoleteMethodWarning:'use NonPositionableExternalStream makePTYPair'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1871 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1872 |
^ NonPositionableExternalStream makePTYPair. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1873 |
! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1874 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1875 |
makePipe |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1876 |
"obsolete since 12-07-2003" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1877 |
<resource:#obsolete> |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1878 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1879 |
self obsoleteMethodWarning:'use NonPositionableExternalStream makePipe'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1880 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1881 |
^ NonPositionableExternalStream makePipe. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1882 |
! ! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1883 |
|
7261 | 1884 |
!ExternalStream methodsFor:'Compatibility-Dolphin'! |
6474 | 1885 |
|
1886 |
beText |
|
1887 |
self text |
|
1888 |
! ! |
|
1889 |
||
7261 | 1890 |
!ExternalStream methodsFor:'Compatibility-Squeak'! |
3044 | 1891 |
|
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1892 |
nextInto:aByteArrayOrString |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1893 |
"read elements into the argument, whose size determines the amount |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1894 |
of bytes to read. If not enough elements could be read, return |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1895 |
a copy of the argument; otherwise, return the filled argument." |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1896 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1897 |
|n nWanted| |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1898 |
|
4368 | 1899 |
nWanted := aByteArrayOrString byteSize. |
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1900 |
n := self nextAvailableBytes:nWanted into:aByteArrayOrString startingAt:1. |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1901 |
n == nWanted ifTrue:[^ aByteArrayOrString]. |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1902 |
^ aByteArrayOrString copyTo:n |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1903 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1904 |
! |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1905 |
|
4368 | 1906 |
readInto:aContainer startingAt:index count:nElements |
1907 |
"same as #nextBytes:into:startingAt: for ByteArrays; |
|
1908 |
for LongArrays, nelements longs are read. |
|
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1909 |
Squeak compatibility." |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1910 |
|
4368 | 1911 |
|elementSize n| |
1912 |
||
1913 |
elementSize := aContainer class elementByteSize. |
|
13940
12eb1c056d5e
fixed: #readInto:startingAt:count:; offset was wrong for non-byte elements
Claus Gittinger <cg@exept.de>
parents:
13927
diff
changeset
|
1914 |
n := self nextBytes:nElements*elementSize into:aContainer startingAt:(index-1)*elementSize+1. |
4368 | 1915 |
^ n // elementSize |
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1916 |
|
13940
12eb1c056d5e
fixed: #readInto:startingAt:count:; offset was wrong for non-byte elements
Claus Gittinger <cg@exept.de>
parents:
13927
diff
changeset
|
1917 |
"Modified: / 14-01-2012 / 19:00:35 / cg" |
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1918 |
! |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1919 |
|
3044 | 1920 |
readOnly |
1921 |
"Squeak compatibility: make the stream readOnly" |
|
1922 |
||
1923 |
mode := #readonly |
|
1924 |
||
1925 |
"Modified: 20.10.1997 / 19:23:04 / cg" |
|
1926 |
"Created: 20.10.1997 / 19:23:19 / cg" |
|
1927 |
! ! |
|
1928 |
||
5187 | 1929 |
!ExternalStream methodsFor:'Signal constants'! |
1930 |
||
1931 |
invalidReadSignal |
|
1932 |
^ self class invalidReadSignal |
|
1933 |
||
1934 |
"Created: / 3.12.1998 / 15:12:06 / cg" |
|
1935 |
! |
|
1936 |
||
1937 |
invalidWriteSignal |
|
1938 |
^ self class invalidWriteSignal |
|
1939 |
||
1940 |
"Created: / 3.12.1998 / 15:12:10 / cg" |
|
1941 |
! ! |
|
1942 |
||
1 | 1943 |
!ExternalStream methodsFor:'accessing'! |
1944 |
||
612 | 1945 |
binary |
1946 |
"switch to binary mode - default is text" |
|
1947 |
||
1948 |
binary := true |
|
1 | 1949 |
! |
1950 |
||
1951 |
buffered:aBoolean |
|
10 | 1952 |
"turn buffering on or off - default is on" |
1 | 1953 |
|
10 | 1954 |
buffered := aBoolean |
1 | 1955 |
! |
1956 |
||
1957 |
contents |
|
2 | 1958 |
"return the contents of the file from the current position up-to |
1959 |
the end. If the stream is in binary mode, a ByteArray containing |
|
1960 |
the byte values is returned. |
|
1961 |
In text-mode, a collection of strings, each representing one line, |
|
1962 |
is returned." |
|
1963 |
||
1964 |
|text l chunks sizes chunk byteCount cnt bytes offset| |
|
1965 |
||
1966 |
binary ifTrue:[ |
|
18797 | 1967 |
"adding to a ByteArray produces quadratic time-space |
1968 |
behavior - therefore we allocate chunks, and concatenate them |
|
1969 |
at the end." |
|
1970 |
||
1971 |
chunks := OrderedCollection new. |
|
1972 |
sizes := OrderedCollection new. |
|
1973 |
byteCount := 0. |
|
1974 |
[self atEnd] whileFalse:[ |
|
1975 |
chunk := ByteArray uninitializedNew:4096. |
|
1976 |
cnt := self nextBytes:(chunk size) into:chunk. |
|
1977 |
cnt notNil ifTrue:[ |
|
1978 |
chunks add:chunk. |
|
1979 |
sizes add:cnt. |
|
1980 |
byteCount := byteCount + cnt |
|
1981 |
] |
|
1982 |
]. |
|
1983 |
||
1984 |
"now, create one big ByteArray" |
|
1985 |
bytes := ByteArray uninitializedNew:byteCount. |
|
1986 |
offset := 1. |
|
1987 |
1 to:chunks size do:[:index | |
|
1988 |
chunk := chunks at:index. |
|
1989 |
cnt := sizes at:index. |
|
1990 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk. |
|
1991 |
offset := offset + cnt |
|
1992 |
]. |
|
1993 |
^ bytes |
|
2 | 1994 |
]. |
1 | 1995 |
|
244 | 1996 |
text := StringCollection new. |
1 | 1997 |
[self atEnd] whileFalse:[ |
18797 | 1998 |
l := self nextLine. |
1999 |
l isNil ifTrue:[ |
|
2000 |
^ text |
|
2001 |
]. |
|
2002 |
text add:l |
|
1 | 2003 |
]. |
2004 |
^ text |
|
2005 |
! |
|
2006 |
||
11751
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2007 |
contentsAsString |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2008 |
"to compensate for the bad naming, use this to make things explicit. |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2009 |
See also #contents, which returns the lines as stringCollection for textFiles." |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2010 |
|
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2011 |
^ self contentsOfEntireFile |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2012 |
! |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2013 |
|
612 | 2014 |
contentsOfEntireFile |
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
2015 |
"ST-80 compatibility: return contents as a String (or byteArray, if in binary mode). |
3876 | 2016 |
See also #contents, which returns the lines as stringCollection for text files." |
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
2017 |
|
6305 | 2018 |
binary ifTrue:[ |
18797 | 2019 |
^ self contents. |
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
2020 |
]. |
6305 | 2021 |
^ self upToEnd |
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
2022 |
|
3876 | 2023 |
"Modified: / 3.7.1996 / 13:22:16 / stefan" |
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
2024 |
"Modified: / 27.11.1998 / 16:29:43 / cg" |
2 | 2025 |
! |
2026 |
||
612 | 2027 |
contentsSpecies |
2028 |
"return the kind of object to be returned by sub-collection builders |
|
2029 |
(such as upTo)" |
|
159 | 2030 |
|
5441 | 2031 |
binary == true ifTrue:[ |
18797 | 2032 |
^ ByteArray |
159 | 2033 |
]. |
612 | 2034 |
^ String |
2 | 2035 |
! |
2036 |
||
16776 | 2037 |
eolMode |
2038 |
"return how end-of-line (EOL) is to be marked. |
|
2039 |
Returns one one of: |
|
18797 | 2040 |
#crlf -> add a CR-NL, as in MSDOS |
2041 |
#cr -> add a CR, as in VMS |
|
2042 |
#nl -> add a NL, as in Unix |
|
2043 |
nil -> transparent |
|
16776 | 2044 |
" |
2045 |
||
17115
2ef08a902c34
did report error in close always as writeError - although it could also happen on a readonly stream
Claus Gittinger <cg@exept.de>
parents:
17078
diff
changeset
|
2046 |
^ eolMode |
16776 | 2047 |
! |
2048 |
||
2920 | 2049 |
eolMode:aSymbolOrNil |
2050 |
"specify how end-of-line (EOL) is to be marked. |
|
2051 |
The argument may be one of: |
|
18797 | 2052 |
#crlf -> add a CR-NL, as in MSDOS |
2053 |
#cr -> add a CR, as in VMS |
|
2054 |
#nl -> add a NL, as in Unix |
|
2055 |
anyOther -> like #nl |
|
2920 | 2056 |
" |
2057 |
||
17462 | 2058 |
aSymbolOrNil == #crnl ifTrue:[ |
18797 | 2059 |
eolMode := #crlf |
17462 | 2060 |
] ifFalse:[ |
18797 | 2061 |
aSymbolOrNil == #lf ifTrue:[ |
2062 |
eolMode := #nl |
|
2063 |
] ifFalse:[ |
|
2064 |
eolMode := aSymbolOrNil |
|
2065 |
] |
|
17462 | 2066 |
]. |
2920 | 2067 |
! |
2068 |
||
612 | 2069 |
fileDescriptor |
2070 |
"return the fileDescriptor of the receiver - |
|
2071 |
notice: this one returns the underlying OSs fileDescriptor - |
|
2072 |
this may not be available on all platforms (i.e. non unix systems)." |
|
1 | 2073 |
|
3971 | 2074 |
%{ |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2075 |
OBJ _handle = __INST(handle); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2076 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2077 |
if (_handle != nil) { |
18797 | 2078 |
if (__INST(handleType) == @symbol(socketHandle)) { |
2079 |
RETURN (_handle); |
|
2080 |
} else if ((__INST(handleType) == nil) |
|
2081 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2082 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
2083 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
2084 |
RETURN ( __MKINT(fileno(__FILEVal(_handle)))); |
|
2085 |
} |
|
1 | 2086 |
} |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2087 |
%}. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2088 |
handle isNil ifTrue:[^ self errorNotOpen]. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2089 |
^ self fileDescriptorOfFile:handle |
5852 | 2090 |
! |
2091 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2092 |
fileDescriptorOfFile:handle |
5852 | 2093 |
"for migration to rel5 only: |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2094 |
return the fileDescriptor of the argument handle - |
5852 | 2095 |
notice: this one returns the underlying OSs fileDescriptor - |
2096 |
this may not be available on all platforms (i.e. non unix systems)." |
|
2097 |
||
18312 | 2098 |
%{ |
2099 |
#ifdef __SCHTEAM__ |
|
2100 |
return context._RETURN(handle); |
|
2101 |
#endif |
|
2102 |
%}. |
|
5852 | 2103 |
self primitiveFailed |
612 | 2104 |
! |
2105 |
||
10455 | 2106 |
fileHandle |
2107 |
"return the fileHandle of the receiver. |
|
2108 |
Under unix, this is the fileDescriptor; under windows, this is the Handle." |
|
2109 |
||
2110 |
%{ |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2111 |
OBJ _handle = __INST(handle); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2112 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2113 |
if (_handle != nil) { |
18797 | 2114 |
if ((__INST(handleType) == @symbol(fileHandle)) |
2115 |
|| (__INST(handleType) == @symbol(socketHandle))) { |
|
2116 |
RETURN (_handle); |
|
2117 |
} |
|
2118 |
if ((__INST(handleType) == nil) |
|
2119 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2120 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
2121 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
10455 | 2122 |
#ifdef WIN32 |
18797 | 2123 |
RETURN(__MKEXTERNALADDRESS(_get_osfhandle(fileno(__FILEVal(_handle))))); |
10455 | 2124 |
#else |
18797 | 2125 |
RETURN (__MKINT(fileno(__FILEVal(_handle)))); |
10455 | 2126 |
#endif |
18797 | 2127 |
} |
10455 | 2128 |
} |
2129 |
%}. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2130 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2131 |
^ handle |
10455 | 2132 |
! |
2133 |
||
612 | 2134 |
filePointer |
2135 |
"return the filePointer of the receiver - |
|
2136 |
notice: for portability stdio is used; this means you will get |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
2137 |
a FILE * - not a fileDescriptor. |
612 | 2138 |
(what you really get is a corresponding integer). |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
2139 |
You cannot do much with the returned value |
612 | 2140 |
- except passing it to a primitive, for example." |
2141 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2142 |
(handleType isNil or:[handleType == #filePointer]) ifTrue:[ |
18797 | 2143 |
^ handle |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2144 |
]. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2145 |
^ self error:'not a FILE*' |
612 | 2146 |
! |
2147 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2148 |
handleType |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2149 |
^ handleType |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2150 |
! |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2151 |
|
5323 | 2152 |
lineEndCRLF |
2153 |
eolMode := #crlf |
|
2154 |
! |
|
2155 |
||
17672 | 2156 |
lineEndLF |
2157 |
eolMode := #nl |
|
2158 |
! |
|
2159 |
||
3016 | 2160 |
lineEndTransparent |
17426
6e49243ce4e0
fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents:
17420
diff
changeset
|
2161 |
eolMode := #nl |
3016 | 2162 |
! |
2163 |
||
8069
fdf460457c96
acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents:
8066
diff
changeset
|
2164 |
pathName |
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2165 |
"answer the pathName of the stream. |
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2166 |
Only FileStreams know the pathName, so we return an empty string here" |
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2167 |
|
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2168 |
^ '' |
8069
fdf460457c96
acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents:
8066
diff
changeset
|
2169 |
! |
fdf460457c96
acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents:
8066
diff
changeset
|
2170 |
|
612 | 2171 |
readonly |
2172 |
"set access mode to readonly" |
|
2173 |
||
2174 |
mode := #readonly |
|
2175 |
! |
|
2176 |
||
2177 |
readwrite |
|
2178 |
"set access mode to readwrite" |
|
2179 |
||
2180 |
mode := #readwrite |
|
2181 |
! |
|
2182 |
||
2183 |
text |
|
2184 |
"switch to text mode - default is text" |
|
2185 |
||
2186 |
binary := false |
|
2187 |
! |
|
2188 |
||
2189 |
useCRLF:aBoolean |
|
2920 | 2190 |
"turn on or off CRLF sending (instead of LF only) - default is off. |
2191 |
This method is provided for backward compatibility - see #eolMode: |
|
2192 |
which offers another choice." |
|
2193 |
||
2194 |
aBoolean ifTrue:[ |
|
18797 | 2195 |
eolMode := #crlf |
2920 | 2196 |
] ifFalse:[ |
18797 | 2197 |
eolMode := #nl |
2920 | 2198 |
]. |
1 | 2199 |
! |
2200 |
||
612 | 2201 |
writeonly |
2202 |
"set access mode to writeonly" |
|
2203 |
||
2204 |
mode := #writeonly |
|
2205 |
! ! |
|
2206 |
||
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2207 |
!ExternalStream methodsFor:'closing'! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2208 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2209 |
close |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2210 |
"close the stream. |
16300 | 2211 |
No error of the stream is not open." |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2212 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2213 |
self isOpen ifTrue:[ |
18779 | 2214 |
self unregisterForFinalization. |
2215 |
self closeFile. |
|
16300 | 2216 |
]. |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2217 |
! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2218 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2219 |
shutDown |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2220 |
"close the stream - added for protocol compatibility with PipeStream. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2221 |
see comment there" |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2222 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2223 |
self isOpen ifTrue:[ |
18779 | 2224 |
self unregisterForFinalization. |
2225 |
self closeFile |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2226 |
] |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2227 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2228 |
"Modified: 30.8.1996 / 00:39:21 / cg" |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2229 |
! ! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2230 |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2231 |
!ExternalStream methodsFor:'copying'! |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2232 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2233 |
copy |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2234 |
"answer a copy of myself. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2235 |
Have to dup the filedescriptor" |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2236 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2237 |
|copy| |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2238 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2239 |
copy := super copy. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2240 |
copy dupFd. |
18779 | 2241 |
copy registerForFinalization. |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2242 |
^ copy |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2243 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2244 |
" |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2245 |
|stream1 stream2| |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2246 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2247 |
stream1 := Filename newTemporary writeStream. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2248 |
stream2 := stream1 copy. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2249 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2250 |
stream1 inspect. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2251 |
stream2 inspect. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2252 |
" |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2253 |
! ! |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2254 |
|
612 | 2255 |
!ExternalStream methodsFor:'error handling'! |
2256 |
||
2257 |
argumentMustBeCharacter |
|
14440 | 2258 |
"report an error, that the argument must be a character in 0..FF" |
2259 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2260 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2261 |
|
14440 | 2262 |
ArgumentError raiseErrorString:' - argument must be a single byte character' |
612 | 2263 |
! |
2264 |
||
2265 |
argumentMustBeInteger |
|
14440 | 2266 |
"report an error, that the argument must be an integer" |
2267 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2268 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2269 |
|
14440 | 2270 |
ArgumentError raiseErrorString:' - argument must be an integer' |
77 | 2271 |
! |
2272 |
||
612 | 2273 |
argumentMustBeString |
14440 | 2274 |
"report an error, that the argument must be a string" |
2275 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2276 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2277 |
|
14440 | 2278 |
ArgumentError raiseErrorString:' - argument must be a string' |
612 | 2279 |
! |
2280 |
||
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2281 |
errorAlreadyOpen |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2282 |
"{ Pragma: +optSpace }" |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2283 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2284 |
"report an error, that the stream is already opened" |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2285 |
|
8074 | 2286 |
^ OpenError |
18797 | 2287 |
raiseRequestWith:self |
2288 |
errorString:' - stream is already open' |
|
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2289 |
|
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2290 |
" |
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2291 |
self new errorAlreadyOpen |
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2292 |
" |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2293 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2294 |
"Modified: / 8.5.1999 / 20:12:30 / cg" |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2295 |
! |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2296 |
|
612 | 2297 |
errorBinary |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2298 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2299 |
|
612 | 2300 |
"report an error, that the stream is in binary mode" |
2301 |
||
8074 | 2302 |
^ InvalidModeError |
18797 | 2303 |
raiseRequestWith:self |
2304 |
errorString:(self class name , ' is in binary mode') |
|
2305 |
"/ in:thisContext sender |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2306 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2307 |
"Modified: / 8.5.1999 / 20:12:43 / cg" |
612 | 2308 |
! |
2309 |
||
2310 |
errorNotBinary |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2311 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2312 |
|
612 | 2313 |
"report an error, that the stream is not in binary mode" |
2314 |
||
8074 | 2315 |
^ InvalidModeError |
18797 | 2316 |
raiseRequestWith:self |
2317 |
errorString:(self class name , ' is not in binary mode') |
|
2318 |
"/ in:thisContext sender |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2319 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2320 |
"Modified: / 8.5.1999 / 20:12:40 / cg" |
1 | 2321 |
! |
2322 |
||
612 | 2323 |
errorNotBuffered |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2324 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2325 |
|
612 | 2326 |
"report an error, that the stream is not in buffered mode" |
2327 |
||
8074 | 2328 |
^ StreamError |
18797 | 2329 |
raiseRequestWith:self |
2330 |
errorString:(self class name , ' is unbuffered - operation not allowed') |
|
2331 |
"/ in:thisContext sender |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2332 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2333 |
"Modified: / 8.5.1999 / 20:12:36 / cg" |
612 | 2334 |
! |
2335 |
||
2336 |
errorNotOpen |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2337 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2338 |
|
612 | 2339 |
"report an error, that the stream has not been opened" |
2340 |
||
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2341 |
^ StreamNotOpenError raiseRequestWith:self "/ in:thisContext sender |
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2342 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2343 |
"Modified: / 8.5.1999 / 20:12:33 / cg" |
612 | 2344 |
! |
2345 |
||
2346 |
errorReadOnly |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2347 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2348 |
|
612 | 2349 |
"report an error, that the stream is a readOnly stream" |
2350 |
||
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2351 |
^ InvalidWriteSignal raiseRequestWith:self "/ in:thisContext sender |
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2352 |
|
4468 | 2353 |
"Modified: / 30.7.1999 / 17:08:19 / cg" |
612 | 2354 |
! |
2355 |
||
6255
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2356 |
errorReporter |
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2357 |
"ST-80 mimicry." |
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2358 |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
2359 |
^ self class |
6255
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2360 |
! |
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2361 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2362 |
errorUnsupportedOperation |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2363 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2364 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2365 |
"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
|
2366 |
|
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2367 |
^ InvalidOperationError raiseRequestWith:self "/ in:thisContext sender |
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2368 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2369 |
"Modified: / 8.5.1999 / 20:12:24 / cg" |
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2370 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2371 |
|
612 | 2372 |
errorWriteOnly |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2373 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2374 |
|
612 | 2375 |
"report an error, that the stream is a writeOnly stream" |
2376 |
||
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2377 |
^ InvalidReadError raiseRequestWith:self "/ in:thisContext sender |
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2378 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2379 |
"Modified: / 8.5.1999 / 20:12:20 / cg" |
2 | 2380 |
! |
2381 |
||
612 | 2382 |
ioError |
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
2383 |
"report an error, that some I/O error occured. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
2384 |
(for example, a device-IO-error, or reading an NFS-dir, |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
2385 |
which is no longer available and has been mounted soft)" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
2386 |
|
16296 | 2387 |
"{ Pragma: +optSpace }" |
2388 |
||
2389 |
self ioError:lastErrorNumber |
|
2390 |
||
2391 |
"Modified: / 8.5.1999 / 20:12:16 / cg" |
|
2392 |
! |
|
2393 |
||
2394 |
ioError:errorNumber |
|
2395 |
"report an error, that some I/O error occured. |
|
2396 |
(for example, a device-IO-error, or reading an NFS-dir, |
|
2397 |
which is no longer available and has been mounted soft)" |
|
2398 |
||
2399 |
"{ Pragma: +optSpace }" |
|
2400 |
||
2401 |
^ StreamIOError newException |
|
18797 | 2402 |
errorCode:errorNumber; |
2403 |
osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber); |
|
2404 |
parameter:self; |
|
2405 |
raiseRequest |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2406 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2407 |
"Modified: / 8.5.1999 / 20:12:16 / cg" |
612 | 2408 |
! |
2409 |
||
2410 |
lastErrorNumber |
|
2411 |
"return the last error" |
|
2412 |
||
2413 |
^ lastErrorNumber |
|
2414 |
! |
|
2415 |
||
2416 |
lastErrorString |
|
2417 |
"return a message string describing the last error" |
|
2418 |
||
2419 |
(lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[ |
|
18797 | 2420 |
^ 'I/O error' |
612 | 2421 |
]. |
2422 |
^ OperatingSystem errorTextForNumber:lastErrorNumber |
|
2 | 2423 |
! |
2424 |
||
10932 | 2425 |
lastErrorSymbol |
2426 |
"return an error symbol describing the last error" |
|
2427 |
||
2428 |
^ OperatingSystem errorSymbolForNumber:lastErrorNumber |
|
2429 |
! |
|
2430 |
||
612 | 2431 |
openError |
12688 | 2432 |
"report an error, that the open failed" |
2433 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2434 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2435 |
|
14438 | 2436 |
^ self openError:lastErrorNumber. |
612 | 2437 |
! |
2438 |
||
12688 | 2439 |
openError:errorNumber |
2440 |
"report an error, that the open failed" |
|
2441 |
||
2442 |
"{ Pragma: +optSpace }" |
|
2443 |
||
15496 | 2444 |
|exClass errorHolder| |
2445 |
||
2446 |
errorHolder := OperatingSystem errorHolderForNumber:errorNumber. |
|
2447 |
||
2448 |
exClass := (errorHolder errorCategory == #nonexistentSignal) |
|
18797 | 2449 |
ifTrue:[ FileDoesNotExistException ] |
2450 |
ifFalse:[ OpenError ]. |
|
13673 | 2451 |
|
2452 |
^ exClass newException |
|
18797 | 2453 |
errorCode:errorNumber; |
2454 |
osErrorHolder:errorHolder; |
|
2455 |
"/ cg: initialized lazyly - see OpenError>>#description |
|
2456 |
"/ errorString:(' : ' , errorHolder errorString); |
|
2457 |
parameter:self; |
|
2458 |
raiseRequest |
|
2459 |
"/ in:thisContext sender |
|
13673 | 2460 |
|
2461 |
"Modified: / 09-09-2011 / 07:22:49 / cg" |
|
12688 | 2462 |
! |
2463 |
||
612 | 2464 |
readError |
15845 | 2465 |
"report an error, that some read error occured" |
2466 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2467 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2468 |
|
15845 | 2469 |
^ self readError:lastErrorNumber |
2470 |
! |
|
2471 |
||
2472 |
readError:errorNumber |
|
612 | 2473 |
"report an error, that some read error occured" |
2474 |
||
15845 | 2475 |
"{ Pragma: +optSpace }" |
2476 |
||
2477 |
^ ReadError newException |
|
18797 | 2478 |
errorCode:errorNumber; |
2479 |
osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber); |
|
2480 |
parameter:self; |
|
2481 |
raiseRequest |
|
612 | 2482 |
! |
2483 |
||
2484 |
writeError |
|
15845 | 2485 |
"report an error, that some write error occured" |
2486 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2487 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2488 |
|
15845 | 2489 |
^ self writeError:lastErrorNumber |
2490 |
! |
|
2491 |
||
2492 |
writeError:errorNumber |
|
612 | 2493 |
"report an error, that some write error occured" |
2494 |
||
15845 | 2495 |
"{ Pragma: +optSpace }" |
2496 |
||
2497 |
^ WriteError newException |
|
18797 | 2498 |
errorCode:errorNumber; |
2499 |
osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber); |
|
2500 |
parameter:self; |
|
2501 |
raiseRequest |
|
612 | 2502 |
! ! |
2503 |
||
6461
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2504 |
!ExternalStream methodsFor:'finalization'! |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2505 |
|
15468 | 2506 |
executor |
2507 |
"return a copy for finalization-registration; |
|
2508 |
since all we need at finalization time is the fileDescriptor, |
|
2509 |
a cheaper copy is possible." |
|
2510 |
||
2511 |
^ self class basicNew setAccessor:handleType to:handle |
|
2512 |
! |
|
2513 |
||
6461
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2514 |
finalizationLobby |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2515 |
"answer the registry used for finalization. |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2516 |
ExternalStreams have their own Registry" |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2517 |
|
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2518 |
^ Lobby |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2519 |
! |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2520 |
|
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2521 |
finalize |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2522 |
"some Stream has been collected - close the file if not already done" |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2523 |
|
16551 | 2524 |
"/ with timeout to avoid blocking in a bad pty/socket |
2525 |
[ |
|
18797 | 2526 |
self closeFile |
16551 | 2527 |
] valueWithTimeout:30 seconds. |
18779 | 2528 |
! |
2529 |
||
2530 |
reRegisterForFinalization |
|
2531 |
Lobby registerChange:self. |
|
2532 |
! |
|
2533 |
||
2534 |
registerForFinalization |
|
2535 |
||
2536 |
"keep myself in newSpace, so it will be finalized early" |
|
18790 | 2537 |
Lobby size < MaxNonTenurableExecutors ifTrue:[ |
2538 |
ObjectMemory preventTenureOf:self. |
|
2539 |
]. |
|
18779 | 2540 |
Lobby register:self. |
2541 |
! |
|
2542 |
||
2543 |
unregisterForFinalization |
|
2544 |
Lobby unregister:self. |
|
2545 |
"probably the object will be collected soon..." |
|
18793 | 2546 |
ObjectMemory allowTenureOf:self |
6461
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2547 |
! ! |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2548 |
|
15918
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2549 |
!ExternalStream methodsFor:'initialization'! |
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2550 |
|
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2551 |
initialize |
15921 | 2552 |
position := 0. |
2553 |
binary := false. |
|
2554 |
buffered := true. |
|
2555 |
eolMode := DefaultEOLMode. |
|
2556 |
hitEOF := false. |
|
15918
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2557 |
! ! |
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2558 |
|
2 | 2559 |
!ExternalStream methodsFor:'line reading/writing'! |
1 | 2560 |
|
2561 |
nextLine |
|
2562 |
"read the next line (characters up to newline). |
|
2563 |
Return a string containing those characters excluding the newline. |
|
2564 |
If the previous-to-last character is a cr, this is also removed, |
|
16508 | 2565 |
so it's possible to read alien (i.e. ms-dos) text as well. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2566 |
The line must be shorter than 32K characters - otherwise an error is signalled." |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2567 |
|
16296 | 2568 |
|line error| |
971
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
2569 |
|
12873 | 2570 |
%{ /* STACK:100000 */ |
1 | 2571 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2572 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2573 |
int len, ret; |
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
2574 |
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
|
2575 |
char *rslt, *nextPtr, *limit; |
22 | 2576 |
int fd, ch; |
1068
92fdcccf12a3
alles MIST - zurueck zu 1.89 und NOCONTEXT entfernt
Claus Gittinger <cg@exept.de>
parents:
1067
diff
changeset
|
2577 |
int _buffered; |
216 | 2578 |
OBJ fp; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2579 |
int lineTooLong = 0; |
2920 | 2580 |
int cutOff = 0; |
1 | 2581 |
|
1133 | 2582 |
__INST(lastErrorNumber) = nil; |
12548 | 2583 |
if ((__INST(handleType) == nil) |
2584 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2585 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2586 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 2587 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 2588 |
if (((fp = __INST(handle)) != nil) |
2589 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2590 |
&& (__INST(binary) != true) |
|
2591 |
) { |
|
2592 |
f = __FILEVal(fp); |
|
2593 |
buffer[0] = '\0'; |
|
2594 |
||
2595 |
_buffered = (__INST(buffered) == true); |
|
2596 |
if (_buffered) { |
|
2597 |
__READING__(f); |
|
2598 |
} |
|
2599 |
||
2600 |
rslt = nextPtr = buffer; |
|
2601 |
limit = buffer + sizeof(buffer) - 2; |
|
2602 |
||
2603 |
for (;;) { |
|
2604 |
__READBYTE__(ret, f, nextPtr, _buffered, __INST(handleType)); |
|
2605 |
if (ret <= 0) { |
|
2606 |
if (nextPtr == buffer) |
|
2607 |
rslt = NULL; |
|
2608 |
if (ret == 0) { |
|
2609 |
__INST(hitEOF) = true; |
|
2610 |
break; |
|
2611 |
} else { |
|
2612 |
error = __mkSmallInteger(__threadErrno); |
|
2613 |
goto err; |
|
2614 |
} |
|
2615 |
} |
|
2616 |
||
2617 |
if (*nextPtr == '\n') { |
|
2618 |
cutOff = 1; |
|
2619 |
*nextPtr = '\0'; |
|
2620 |
break; |
|
2621 |
} |
|
2622 |
if (*nextPtr == '\r') { |
|
2623 |
char peekChar; |
|
2624 |
||
2625 |
/* |
|
2626 |
* peek ahead for a newLine ... |
|
2627 |
*/ |
|
2628 |
__READBYTE__(ret, f, &peekChar, _buffered, __INST(handleType)); |
|
2629 |
if (ret <= 0) { |
|
2630 |
cutOff = 1; |
|
2631 |
*nextPtr = '\0'; |
|
2632 |
if (ret == 0) { |
|
2633 |
__INST(hitEOF) = true; |
|
2634 |
break; |
|
2635 |
} |
|
2636 |
error = __mkSmallInteger(__threadErrno); |
|
2637 |
goto err; |
|
2638 |
} |
|
2639 |
||
2640 |
if (peekChar == '\n') { |
|
2641 |
cutOff = 2; |
|
2642 |
*nextPtr = '\0'; |
|
2643 |
break; |
|
2644 |
} |
|
2645 |
||
2646 |
__UNGETC__(peekChar, f, _buffered); |
|
2647 |
||
2648 |
cutOff = 1; |
|
2649 |
*nextPtr = '\0'; |
|
2650 |
break; |
|
2651 |
} |
|
2652 |
||
2653 |
nextPtr++; |
|
2654 |
if (nextPtr >= limit) { |
|
2655 |
*nextPtr = '\0'; |
|
2656 |
lineTooLong = 1; |
|
2657 |
if (@global(InfoPrinting) == true) { |
|
2658 |
fprintf(stderr, "ExtStream [warning]: line truncated in nextLine\n"); |
|
2659 |
} |
|
2660 |
break; |
|
2661 |
} |
|
2662 |
} |
|
2663 |
||
2664 |
if (rslt != NULL) { |
|
2665 |
len = nextPtr-buffer; |
|
2666 |
||
2667 |
if (__isSmallInteger(__INST(position))) { |
|
2668 |
INT np = __intVal(__INST(position)) + len + cutOff; |
|
2669 |
OBJ t; |
|
2670 |
||
2671 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
2672 |
} else { |
|
2673 |
__INST(position) = nil; /* i.e. do not know */ |
|
2674 |
} |
|
2675 |
/* remove any EOL character */ |
|
2676 |
if (len != 0) { |
|
2677 |
if (buffer[len-1] == '\n') { |
|
2678 |
buffer[--len] = '\0'; |
|
2679 |
} |
|
2680 |
if ((len != 0) && (buffer[len-1] == '\r')) { |
|
2681 |
buffer[--len] = '\0'; |
|
2682 |
} |
|
2683 |
} |
|
2684 |
line = __MKSTRING_L(buffer, len); |
|
2685 |
if (! lineTooLong) { |
|
2686 |
RETURN ( line ); |
|
2687 |
} |
|
2688 |
} |
|
2689 |
} |
|
1 | 2690 |
} |
1478
aae07192b7d1
labels in empty statements need a semi
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
2691 |
err: ; |
159 | 2692 |
%}. |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2693 |
line notNil ifTrue:[ |
18797 | 2694 |
"/ the line as read is longer than 32k characters (boy - what a line) |
2695 |
"/ The exception could be handled by reading more and returning the |
|
2696 |
"/ concatenation in your exception handler (the receiver and the partial |
|
2697 |
"/ line are passed as parameter) |
|
2698 |
||
2699 |
LineTooLongErrorSignal isHandled ifTrue:[ |
|
2700 |
^ LineTooLongErrorSignal |
|
2701 |
raiseRequestWith:(Array with:self with:line) |
|
2702 |
errorString:('line too long read error') |
|
2703 |
]. |
|
2704 |
^ line , self nextLine |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2705 |
]. |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2706 |
|
8614 | 2707 |
(hitEOF == true) ifTrue:[^ self pastEndRead]. |
16296 | 2708 |
error notNil ifTrue:[ |
18797 | 2709 |
lastErrorNumber := error. |
2710 |
^ self readError:error |
|
16296 | 2711 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2712 |
handle isNil ifTrue:[^ self errorNotOpen]. |
1 | 2713 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
5431 | 2714 |
(binary == true) ifTrue:[^ self errorBinary]. |
2715 |
^ super nextLine |
|
1 | 2716 |
! |
2717 |
||
2718 |
nextPutLine:aString |
|
2920 | 2719 |
"write the characters in aString and append an end-of-Line marker |
2720 |
(LF, CR or CRLF - depending in the setting of eolMode)" |
|
1 | 2721 |
|
16296 | 2722 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2723 |
%{ |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
2724 |
OBJ fp; |
1 | 2725 |
|
1133 | 2726 |
__INST(lastErrorNumber) = nil; |
12548 | 2727 |
if ((__INST(handleType) == nil) |
2728 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2729 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2730 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 2731 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 2732 |
if (((fp = __INST(handle)) != nil) |
2733 |
&& (__INST(mode) != @symbol(readonly)) |
|
2734 |
&& (__INST(binary) != true) |
|
2735 |
&& __isStringLike(aString) |
|
2736 |
) { |
|
2737 |
int _buffered = (__INST(buffered) == true); |
|
2738 |
int len = __stringSize(aString); |
|
2739 |
int cnt, len1; |
|
2740 |
FILEPOINTER f = __FILEVal(fp); |
|
2741 |
char *cp; |
|
2742 |
int o_offs; |
|
2743 |
||
2744 |
if (_buffered) { |
|
2745 |
__WRITING__(f) |
|
2746 |
} |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
2747 |
#ifdef WIN32 |
18797 | 2748 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
2749 |
cnt = __win32_fwrite(__stringVal(aString), 1, len, f); |
|
2750 |
} else |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2751 |
#endif |
18797 | 2752 |
{ |
2753 |
o_offs = (char *)__stringVal(aString)-(char *)aString; |
|
2754 |
__WRITEBYTES_OBJ__(cnt, f, aString, o_offs, len, _buffered, __INST(handleType)); |
|
2755 |
} |
|
2756 |
if (cnt == len) { |
|
2757 |
OBJ mode = __INST(eolMode); |
|
2758 |
||
2759 |
len1 = len; |
|
2760 |
||
2761 |
if (mode == @symbol(cr)) { |
|
2762 |
cp = "\r"; len = 1; |
|
2763 |
} else if (mode == @symbol(crlf)) { |
|
2764 |
cp = "\r\n"; len = 2; |
|
2765 |
} else if (mode == @symbol(eot)) { |
|
2766 |
cp = "\004"; len = 1; |
|
2767 |
} else if (mode == @symbol(etx)) { |
|
2768 |
cp = "\003"; len = 1; |
|
2769 |
} else { |
|
2770 |
cp = "\n"; len = 1; |
|
2771 |
} |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2772 |
#ifdef WIN32 |
18797 | 2773 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
2774 |
cnt = __win32_fwrite(cp, 1, len, f); |
|
2775 |
} else |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2776 |
#endif |
18797 | 2777 |
{ |
2778 |
__WRITEBYTES__(cnt, f, cp, len, _buffered, __INST(handleType)); |
|
2779 |
} |
|
2780 |
if (cnt > 0) { |
|
2781 |
if (__isSmallInteger(__INST(position))) { |
|
2782 |
INT np = __intVal(__INST(position)) + cnt; |
|
2783 |
OBJ t; |
|
2784 |
||
2785 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
2786 |
} else { |
|
2787 |
__INST(position) = nil; /* i.e. do not know */ |
|
2788 |
} |
|
2789 |
RETURN ( self ); |
|
2790 |
} |
|
2791 |
} |
|
2792 |
error = __mkSmallInteger(__threadErrno); |
|
2793 |
} |
|
1 | 2794 |
} |
159 | 2795 |
%}. |
16296 | 2796 |
error notNil ifTrue:[ |
18797 | 2797 |
lastErrorNumber := error. |
2798 |
self writeError:error. |
|
2799 |
^ self |
|
16296 | 2800 |
]. |
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
2801 |
super nextPutLine:aString. |
1 | 2802 |
! |
2803 |
||
2804 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil |
|
68 | 2805 |
"read from aStream up to and including a line starting with aStringOrNil |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
2806 |
and append all lines to self. |
159 | 2807 |
Can be used to copy/create large files or copy from a pipe/socket. |
68 | 2808 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2809 |
If aStringOrNil is nil or not matched, copy proceeds to the end." |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2810 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2811 |
|line| |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2812 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2813 |
[aStream atEnd] whileFalse:[ |
18797 | 2814 |
line := aStream nextLine. |
2815 |
line isNil ifTrue:[ |
|
2816 |
^ self. |
|
2817 |
]. |
|
2818 |
self nextPutLine:line. |
|
2819 |
(aStringOrNil notNil and:[line startsWith:aStringOrNil]) ifTrue:[ |
|
2820 |
^ self |
|
2821 |
] |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2822 |
]. |
1 | 2823 |
! |
2824 |
||
2825 |
peekForLineStartingWith:aString |
|
2826 |
"read ahead for next line starting with aString; |
|
159 | 2827 |
return the line-string if found, or nil if EOF is encountered. |
7118 | 2828 |
If matched, do not advance position beyond that line |
159 | 2829 |
i.e. nextLine will read the matched line. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
7017
diff
changeset
|
2830 |
If not matched, reposition to original position for further reading." |
1 | 2831 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2832 |
|firstPos lastPos line| |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2833 |
|
1 | 2834 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2835 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2 | 2836 |
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
|
2837 |
buffered ifFalse:[^ self errorNotBuffered]. |
12 | 2838 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2839 |
firstPos := self position. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2840 |
[self atEnd] whileFalse:[ |
18797 | 2841 |
lastPos := self position. |
2842 |
line := self nextLine. |
|
2843 |
line isNil ifTrue:[ |
|
2844 |
self position:firstPos. |
|
2845 |
^ nil |
|
2846 |
]. |
|
2847 |
(line startsWith:aString) ifTrue:[ |
|
2848 |
self position:lastPos. |
|
2849 |
^ line |
|
2850 |
] |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2851 |
]. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2852 |
self position:firstPos. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2853 |
^ nil |
1 | 2854 |
! |
2855 |
||
2856 |
peekForLineStartingWithAny:aCollectionOfStrings |
|
2857 |
"read ahead for next line starting with any of aCollectionOfStrings; |
|
2858 |
return the index in aCollection if found, nil otherwise.. |
|
2859 |
If no match, do not change position; otherwise advance right before the |
|
2860 |
matched line so that nextLine will return this line." |
|
2861 |
||
2862 |
|line startPos linePos index| |
|
2863 |
||
2864 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2865 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2 | 2866 |
binary ifTrue:[^ self errorBinary]. |
2867 |
||
1 | 2868 |
startPos := self position. |
2869 |
[self atEnd] whileFalse:[ |
|
18797 | 2870 |
linePos := self position. |
2871 |
line := self nextLine. |
|
2872 |
line notNil ifTrue:[ |
|
2873 |
index := 1. |
|
2874 |
aCollectionOfStrings do:[:prefix | |
|
2875 |
(line startsWith:prefix) ifTrue:[ |
|
2876 |
self position:linePos. |
|
2877 |
^ index |
|
2878 |
]. |
|
2879 |
index := index + 1 |
|
2880 |
] |
|
2881 |
] |
|
1 | 2882 |
]. |
2883 |
self position:startPos. |
|
2884 |
^ nil |
|
2885 |
! ! |
|
2886 |
||
612 | 2887 |
!ExternalStream methodsFor:'misc functions'! |
2888 |
||
2889 |
async:aBoolean |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
2890 |
"set/clear the async attribute - if set, the availability of data on |
612 | 2891 |
the receiver will trigger an ioInterrupt. |
779 | 2892 |
If cleared (which is the default) no special notification is made. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
2893 |
Notice: |
18797 | 2894 |
not every OS supports this |
2895 |
- check with OS>>supportsIOInterrupts before using" |
|
92 | 2896 |
|
2897 |
|fd| |
|
2898 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2899 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2900 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2901 |
OperatingSystem supportsIOInterrupts ifFalse:[ |
18797 | 2902 |
^ self errorUnsupportedOperation |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2903 |
]. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2904 |
|
92 | 2905 |
fd := self fileDescriptor. |
612 | 2906 |
aBoolean ifTrue:[ |
18797 | 2907 |
^ OperatingSystem enableIOInterruptsOn:fd |
612 | 2908 |
]. |
2909 |
^ OperatingSystem disableIOInterruptsOn:fd |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2910 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2911 |
"Modified: 11.1.1997 / 17:50:21 / cg" |
92 | 2912 |
! |
2913 |
||
612 | 2914 |
blocking:aBoolean |
2915 |
"set/clear the blocking attribute - if set (which is the default) |
|
2916 |
a read (using next) on the receiver will block until data is available. |
|
2917 |
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
|
2918 |
nil. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2919 |
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
|
2920 |
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
|
2921 |
in advance. However, the data must then be read using #nextBytes: |
8616 | 2922 |
methods, in order to avoid other (pastEndRead) exceptions." |
92 | 2923 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2924 |
handle isNil ifTrue:[^ self errorNotOpen]. |
7499
53b7a86e1f0b
ExternalStream>>blocking: returns previous blocking status
Stefan Vogel <sv@exept.de>
parents:
7497
diff
changeset
|
2925 |
^ 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
|
2926 |
|
3174 | 2927 |
"Modified: / 11.1.1997 / 17:48:01 / cg" |
2928 |
"Modified: / 15.1.1998 / 11:49:48 / stefan" |
|
612 | 2929 |
! |
2930 |
||
17618 | 2931 |
copyToEndFrom:inStream |
2932 |
"read from inStream, and write all data up to the end to the receiver. |
|
2933 |
Return the number of bytes which have been transferred. |
|
2934 |
Same functionality as copyToEnd:, but reversed arg and receiver |
|
2935 |
(useful in a cascade message of the writeStream)" |
|
2936 |
||
2937 |
|bufferSize| |
|
2938 |
||
2939 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
18797 | 2940 |
"/ mhmh - NT hangs, when copying bigger blocks to a network drive - why ? |
2941 |
bufferSize := 1 * 1024. |
|
17618 | 2942 |
] ifFalse:[ |
18797 | 2943 |
bufferSize := 8 * 1024. |
17618 | 2944 |
]. |
2945 |
||
2946 |
^ inStream copyToEndInto:self bufferSize:bufferSize |
|
2947 |
||
2948 |
" |
|
2949 |
|in out| |
|
2950 |
||
2951 |
in := 'Makefile' asFilename readStream. |
|
2952 |
out := Stdout. |
|
2953 |
in copyToEndInto:out. |
|
2954 |
in close. |
|
2955 |
" |
|
2956 |
! |
|
2957 |
||
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2958 |
copyToEndInto:outStream |
7017
03f1bd57c001
no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
7013
diff
changeset
|
2959 |
"copy the data into another stream." |
03f1bd57c001
no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
7013
diff
changeset
|
2960 |
|
8362 | 2961 |
|bufferSize| |
7700
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2962 |
|
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2963 |
OperatingSystem isMSDOSlike ifTrue:[ |
18797 | 2964 |
"/ mhmh - NT hangs, when copying bigger blocks to a network drive - why ? |
2965 |
bufferSize := 1 * 1024. |
|
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2966 |
] ifFalse:[ |
18797 | 2967 |
bufferSize := 8 * 1024. |
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2968 |
]. |
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2969 |
|
7017
03f1bd57c001
no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
7013
diff
changeset
|
2970 |
^ self copyToEndInto:outStream bufferSize:bufferSize |
7700
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2971 |
|
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2972 |
" |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2973 |
|in out| |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2974 |
|
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2975 |
in := 'Makefile' asFilename readStream. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2976 |
out := Stdout. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2977 |
in copyToEndInto:out. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2978 |
in close. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2979 |
" |
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2980 |
! |
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2981 |
|
612 | 2982 |
create |
2983 |
"create the stream |
|
2984 |
- this must be redefined in subclass" |
|
2985 |
||
2986 |
^ self subclassResponsibility |
|
2987 |
! |
|
2988 |
||
2989 |
ioctl:ioctlNumber |
|
2990 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2991 |
on the underlying file; no arguments are passed." |
|
2992 |
||
2920 | 2993 |
^ self ioctl:ioctlNumber with:nil |
2994 |
! |
|
2995 |
||
2996 |
ioctl:ioctlNumber with:arg |
|
2997 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2998 |
on the underlying file; the argument is passed as argument. |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
2999 |
This is not used by ST/X, but provided for special situations |
2920 | 3000 |
- for example, to control proprietrary I/O devices. |
3001 |
||
3002 |
Since the type of the argument depends on the ioctl being |
|
3003 |
performed, different arg types are allowed here. |
|
3004 |
If the argument is nil, an ioctl without argument is performed. |
|
16508 | 3005 |
If the argument is an integral number, it's directly passed; |
3006 |
if it's a kind of ByteArray (ByteArray, String or Structure), |
|
2920 | 3007 |
or external data (ExternalBytes or ExternalAddress), |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3008 |
a pointer to the data is passed. |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3009 |
This allows performing most ioctls |
2920 | 3010 |
- however, it might be tricky to setup the buffer. |
3011 |
Be careful in what you pass - ST/X cannot validate its correctness." |
|
3012 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3013 |
|error| |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3014 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3015 |
%{ |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3016 |
#if !defined(MSDOS_LIKE) && !defined(__openVMS__) |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3017 |
int fd; |
10552
d52aa8e066e5
#ioctl:with: accept 32-bit integers as arguments
Stefan Vogel <sv@exept.de>
parents:
10469
diff
changeset
|
3018 |
int ret; |
d52aa8e066e5
#ioctl:with: accept 32-bit integers as arguments
Stefan Vogel <sv@exept.de>
parents:
10469
diff
changeset
|
3019 |
unsigned int ioNum; |
2920 | 3020 |
INT ioArg; |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3021 |
OBJ fp = __INST(handle); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3022 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3023 |
if (fp == nil) |
18797 | 3024 |
goto out; |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3025 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3026 |
if (!__isInteger(ioctlNumber) |
18797 | 3027 |
|| (!__isInteger(arg) |
3028 |
&& (arg != nil) |
|
3029 |
&& !__isBytes(arg) |
|
3030 |
&& !__isExternalBytesLike(arg) |
|
3031 |
&& !__isExternalAddress(arg))) { |
|
3032 |
error = @symbol(badArgument); |
|
3033 |
goto out; |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3034 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3035 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3036 |
if (__INST(handleType) == @symbol(socketHandle)) { |
18797 | 3037 |
fd = __FILEVal(fp); |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3038 |
} else |
18797 | 3039 |
if ((__INST(handleType) == nil) |
3040 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3041 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3042 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3043 |
fd = fileno(__FILEVal(fp)); |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3044 |
} else { |
18797 | 3045 |
error = @symbol(badHandleType); |
3046 |
goto out; |
|
612 | 3047 |
} |
16508 | 3048 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3049 |
ioNum = __unsignedLongIntVal(ioctlNumber); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3050 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3051 |
__BEGIN_INTERRUPTABLE__ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3052 |
do { |
18797 | 3053 |
__threadErrno = 0; |
3054 |
if (arg == nil) { |
|
3055 |
ioArg = 0; |
|
3056 |
} else if (__isSmallInteger(arg)) { |
|
3057 |
ioArg = __intVal(arg); |
|
3058 |
} else if (__isInteger(arg)) { |
|
3059 |
ioArg = __unsignedLongIntVal(arg); |
|
3060 |
} else if (__isExternalBytesLike(arg)) { |
|
3061 |
ioArg = (INT)(__externalBytesAddress(arg)); |
|
3062 |
} else if (__isExternalAddress(arg)) { |
|
3063 |
ioArg = (INT)(__externalAddressVal(arg)); |
|
3064 |
} else { |
|
3065 |
ioArg = (INT)(__ByteArrayInstPtr(arg)->ba_element); |
|
3066 |
} |
|
3067 |
ret = ioctl(fd, ioNum, ioArg); |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3068 |
} while (ret < 0 && __threadErrno == EINTR); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3069 |
__END_INTERRUPTABLE__ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3070 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3071 |
if (ret >= 0) { |
18797 | 3072 |
RETURN ( __mkSmallInteger(ret) ); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3073 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3074 |
error = __mkSmallInteger(__threadErrno); |
791 | 3075 |
#endif |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3076 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3077 |
out:; |
612 | 3078 |
%}. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3079 |
error notNil ifTrue:[ |
18797 | 3080 |
lastErrorNumber := error. |
3081 |
^ self ioError:error. |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3082 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3083 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3084 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3085 |
"/ the system does not support ioctl (MSDOS or VMS) |
2920 | 3086 |
^ self errorUnsupportedOperation |
612 | 3087 |
! |
3088 |
||
16551 | 3089 |
reset |
3090 |
"set the read position to the beginning of the collection" |
|
3091 |
||
3092 |
self position:0 |
|
3093 |
! |
|
3094 |
||
3095 |
setToEnd |
|
3096 |
"redefined since it must be implemented differently" |
|
3097 |
||
3098 |
^ self subclassResponsibility |
|
3099 |
! |
|
3100 |
||
3101 |
sync |
|
3102 |
"make sure, that the OS writes cached data to the disk" |
|
3103 |
||
3104 |
|error| |
|
3105 |
||
3106 |
self flush. |
|
3107 |
||
3108 |
%{ |
|
3109 |
#if !defined(__openVMS__) |
|
3110 |
int fd; |
|
3111 |
int ret; |
|
3112 |
OBJ fp = __INST(handle); |
|
3113 |
||
3114 |
if (fp == nil) |
|
18797 | 3115 |
goto out; |
16551 | 3116 |
|
3117 |
if (__INST(handleType) == @symbol(socketHandle)) { |
|
18797 | 3118 |
// syncing a socket to disk ? |
3119 |
error = @symbol(badHandleType); |
|
3120 |
goto out; |
|
3121 |
// fd = __FILEVal(fp); |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3122 |
} else |
18797 | 3123 |
if ((__INST(handleType) == nil) |
3124 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3125 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3126 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3127 |
fd = fileno(__FILEVal(fp)); |
|
16551 | 3128 |
} else { |
18797 | 3129 |
error = @symbol(badHandleType); |
3130 |
goto out; |
|
16551 | 3131 |
} |
3132 |
||
3133 |
#ifdef WIN32 |
|
3134 |
__threadErrno = 0; |
|
3135 |
ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fd)); |
|
3136 |
if (ret) { |
|
18797 | 3137 |
RETURN (self); |
16551 | 3138 |
} |
3139 |
#else |
|
3140 |
__BEGIN_INTERRUPTABLE__ |
|
3141 |
do { |
|
18797 | 3142 |
ret = fsync(fd); |
16551 | 3143 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3144 |
__END_INTERRUPTABLE__ |
|
3145 |
||
3146 |
if (ret >= 0) { |
|
18797 | 3147 |
RETURN (self); |
16551 | 3148 |
} |
3149 |
#endif /* ! WIN32 */ |
|
3150 |
error = __mkSmallInteger(__threadErrno); |
|
3151 |
#endif /* ! __openVMS__ */ |
|
3152 |
out:; |
|
3153 |
%}. |
|
3154 |
error notNil ifTrue:[ |
|
18797 | 3155 |
lastErrorNumber := error. |
3156 |
self ioError:error. |
|
3157 |
^ self. |
|
16551 | 3158 |
]. |
3159 |
handle isNil ifTrue:[self errorNotOpen]. |
|
3160 |
||
3161 |
" |
|
18797 | 3162 |
|f| |
3163 |
f := 'x' asFilename writeStream. |
|
3164 |
f nextPutAll:'hallo'; sync; syncData; close |
|
16551 | 3165 |
" |
3166 |
! |
|
3167 |
||
3168 |
syncData |
|
3169 |
"make sure, that the OS writes cached data to the disk. |
|
3170 |
In this case, metadata is only written, if it is |
|
3171 |
required to read the file's data (so metadata will not be written, |
|
3172 |
if only access/modification time has changed)." |
|
3173 |
|error| |
|
3174 |
||
3175 |
self flush. |
|
3176 |
||
3177 |
%{ |
|
3178 |
#if !defined(__openVMS__) |
|
3179 |
int fd; |
|
3180 |
int ret; |
|
3181 |
OBJ fp = __INST(handle); |
|
3182 |
||
3183 |
if (fp == nil) |
|
18797 | 3184 |
goto out; |
16551 | 3185 |
|
3186 |
if (__INST(handleType) == @symbol(socketHandle)) { |
|
18797 | 3187 |
// syncing a socket to disk ? |
3188 |
error = @symbol(badHandleType); |
|
3189 |
goto out; |
|
3190 |
// fd = __FILEVal(fp); |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3191 |
} else |
18797 | 3192 |
if ((__INST(handleType) == nil) |
3193 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3194 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3195 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3196 |
fd = fileno(__FILEVal(fp)); |
|
16551 | 3197 |
} else { |
18797 | 3198 |
error = @symbol(badHandleType); |
3199 |
goto out; |
|
16551 | 3200 |
} |
3201 |
||
3202 |
#ifdef WIN32 |
|
3203 |
__threadErrno = 0; |
|
3204 |
ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fd)); |
|
3205 |
if (ret) { |
|
18797 | 3206 |
RETURN (self); |
16551 | 3207 |
} |
3208 |
#else |
|
3209 |
__BEGIN_INTERRUPTABLE__ |
|
3210 |
do { |
|
18797 | 3211 |
ret = fdatasync(fd); |
16551 | 3212 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3213 |
__END_INTERRUPTABLE__ |
|
3214 |
||
3215 |
if (ret >= 0) { |
|
18797 | 3216 |
RETURN (self); |
16551 | 3217 |
} |
3218 |
#endif /* ! WIN32 */ |
|
3219 |
error = __mkSmallInteger(__threadErrno); |
|
3220 |
#endif /* ! __openVMS__ */ |
|
3221 |
out:; |
|
3222 |
%}. |
|
3223 |
||
3224 |
error notNil ifTrue:[ |
|
18797 | 3225 |
lastErrorNumber := error. |
3226 |
self ioError:error. |
|
3227 |
^ self. |
|
16551 | 3228 |
]. |
3229 |
handle isNil ifTrue:[^ self errorNotOpen]. |
|
3230 |
||
3231 |
"if notdef HAS_DATASYNC, fall back" |
|
3232 |
self sync. |
|
3233 |
||
3234 |
" |
|
18797 | 3235 |
|f| |
3236 |
f := 'x' asFilename writeStream. |
|
3237 |
f nextPutAll:'hallo'; sync; syncData; close |
|
16551 | 3238 |
" |
3239 |
! |
|
3240 |
||
16508 | 3241 |
tcgetattr |
3242 |
"unix only: |
|
3243 |
provide the information of a tcgetattr call as a dictionary. |
|
3244 |
This provides terminal setting values of a tty or pty. |
|
3245 |
Used by terminal emulators. |
|
3246 |
On non-unix systems, an unsupported operation error is raised" |
|
3247 |
||
3248 |
|error ret |
|
3249 |
iflags "/ input flags |
|
3250 |
ignbrk brkint ignpar parmrk inpck istrip inlcr igncr |
|
16509
2cf139885242
Fixed ExternalStream>>tcgetattr
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16508
diff
changeset
|
3251 |
icrnl iuclc ixon ixoff ixany imaxbel iutf8 |
16508 | 3252 |
oflags "/ output flags |
3253 |
opost onlcr ocrnl olcuc onocr onlret oxtabs onoteot |
|
3254 |
cflags "/ control flags |
|
3255 |
csize cstopb cread parenb parodd hupcl clocal |
|
3256 |
lflags "/ local flags |
|
3257 |
echoke echoe echok echo echonl echoprt echoctl |
|
3258 |
isig icanon altwerase iexten extproc tostop |
|
3259 |
| |
|
3260 |
||
3261 |
%{ |
|
3262 |
#if !defined(MSDOS_LIKE) && !defined(__openVMS__) |
|
3263 |
int fd; |
|
3264 |
int _ret; |
|
3265 |
OBJ fp = __INST(handle); |
|
3266 |
struct termios t; |
|
3267 |
int sz; |
|
3268 |
||
3269 |
if (fp == nil) |
|
18797 | 3270 |
goto out; |
16508 | 3271 |
|
3272 |
if (__INST(handleType) == @symbol(socketHandle)) { |
|
18797 | 3273 |
// cg: termina attributes of a socket? |
3274 |
error = @symbol(badHandleType); |
|
3275 |
goto out; |
|
3276 |
// fd = __FILEVal(fp); |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3277 |
} else |
18797 | 3278 |
if ((__INST(handleType) == nil) |
3279 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3280 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3281 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3282 |
fd = fileno(__FILEVal(fp)); |
|
16508 | 3283 |
} else { |
18797 | 3284 |
error = @symbol(badHandleType); |
3285 |
goto out; |
|
16508 | 3286 |
} |
3287 |
||
3288 |
__BEGIN_INTERRUPTABLE__ |
|
3289 |
do { |
|
18797 | 3290 |
__threadErrno = 0; |
3291 |
_ret = tcgetattr(fd, &t); |
|
16508 | 3292 |
} while (_ret < 0 && __threadErrno == EINTR); |
3293 |
__END_INTERRUPTABLE__ |
|
3294 |
||
3295 |
if (_ret < 0) { |
|
18797 | 3296 |
error = __mkSmallInteger(__threadErrno); |
16508 | 3297 |
} |
3298 |
ignbrk = t.c_iflag & IGNBRK ? true : false; |
|
3299 |
brkint = t.c_iflag & BRKINT ? true : false; |
|
3300 |
ignpar = t.c_iflag & IGNPAR ? true : false; |
|
3301 |
parmrk = t.c_iflag & PARMRK ? true : false; |
|
3302 |
inpck = t.c_iflag & INPCK ? true : false; |
|
3303 |
istrip = t.c_iflag & ISTRIP ? true : false; |
|
3304 |
inlcr = t.c_iflag & INLCR ? true : false; |
|
3305 |
ixon = t.c_iflag & IXON ? true : false; |
|
3306 |
ixoff = t.c_iflag & IXOFF ? true : false; |
|
3307 |
ixany = t.c_iflag & IXANY ? true : false; |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3308 |
# ifdef IMAXBEL |
16508 | 3309 |
imaxbel = t.c_iflag & IMAXBEL ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3310 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3311 |
# ifdef IUCLC |
16508 | 3312 |
iuclc = t.c_iflag & IUCLC ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3313 |
# endif |
16508 | 3314 |
|
3315 |
opost = t.c_oflag & OPOST ? true : false; |
|
3316 |
onlcr = t.c_oflag & ONLCR ? true : false; |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3317 |
# ifdef OXTABS |
16508 | 3318 |
oxtabs = t.c_oflag & OXTABS ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3319 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3320 |
# ifdef ONOTEOT |
16508 | 3321 |
onoteot = t.c_oflag & ONOTEOT ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3322 |
# endif |
16508 | 3323 |
ocrnl = t.c_oflag & OCRNL ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3324 |
# ifdef OLCUC |
16508 | 3325 |
olcuc = t.c_oflag & OLCUC ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3326 |
# endif |
16508 | 3327 |
onocr = t.c_oflag & ONOCR ? true : false; |
3328 |
onlret = t.c_oflag & ONLRET ? true : false; |
|
3329 |
||
3330 |
sz = t.c_cflag & CSIZE; |
|
3331 |
switch (sz) { |
|
18797 | 3332 |
case CS5: |
3333 |
csize = @symbol(cs5); |
|
3334 |
break; |
|
3335 |
case CS6: |
|
3336 |
csize = @symbol(cs6); |
|
3337 |
break; |
|
3338 |
case CS7: |
|
3339 |
csize = @symbol(cs7); |
|
3340 |
break; |
|
3341 |
case CS8: |
|
3342 |
csize = @symbol(cs8); |
|
3343 |
break; |
|
16508 | 3344 |
} |
3345 |
cstopb = t.c_cflag & CSTOPB ? true : false; |
|
3346 |
cread = t.c_cflag & CREAD ? true : false; |
|
3347 |
parenb = t.c_cflag & PARENB ? true : false; |
|
3348 |
parodd = t.c_cflag & PARODD ? true : false; |
|
3349 |
hupcl = t.c_cflag & HUPCL ? true : false; |
|
3350 |
clocal = t.c_cflag & CLOCAL ? true : false; |
|
3351 |
||
3352 |
echoke = t.c_lflag & ECHOKE ? true : false; |
|
3353 |
echoe = t.c_lflag & ECHOE ? true : false; |
|
3354 |
echo = t.c_lflag & ECHO ? true : false; |
|
3355 |
echonl = t.c_lflag & ECHONL ? true : false; |
|
3356 |
echoctl = t.c_lflag & ECHOCTL ? true : false; |
|
3357 |
isig = t.c_lflag & ISIG ? true : false; |
|
3358 |
icanon = t.c_lflag & ICANON ? true : false; |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3359 |
# ifdef ALTWERASE |
16508 | 3360 |
altwerase = t.c_lflag & ALTWERASE ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3361 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3362 |
# ifdef IEXTEN |
16508 | 3363 |
iexten = t.c_lflag & IEXTEN ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3364 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3365 |
# ifdef EXTPROC |
16508 | 3366 |
extproc = t.c_lflag & EXTPROC ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3367 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3368 |
# ifdef TOSTOP |
16508 | 3369 |
tostop = t.c_lflag & TOSTOP ? true : false; |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3370 |
# endif |
16508 | 3371 |
|
3372 |
#else |
|
3373 |
error = @symbol(unsupportedOperation); |
|
3374 |
#endif |
|
3375 |
||
3376 |
out:; |
|
3377 |
%}. |
|
3378 |
error notNil ifTrue:[ |
|
18797 | 3379 |
error == #unsupportedOperation ifTrue:[ |
3380 |
"/ the system does not support tcgetattr |
|
3381 |
^ self errorUnsupportedOperation |
|
3382 |
]. |
|
3383 |
lastErrorNumber := error. |
|
3384 |
^ self ioError:error. |
|
16508 | 3385 |
]. |
3386 |
handle isNil ifTrue:[^ self errorNotOpen]. |
|
3387 |
||
3388 |
"/ construct a dictionary... |
|
3389 |
ret := Dictionary new. |
|
3390 |
ret at:#iflags put:(iflags := Dictionary new). |
|
3391 |
ret at:#oflags put:(oflags := Dictionary new). |
|
3392 |
ret at:#cflags put:(cflags := Dictionary new). |
|
3393 |
ret at:#lflags put:(lflags := Dictionary new). |
|
3394 |
iflags at:#ignbrk put:ignbrk. |
|
3395 |
iflags at:#brkint put:brkint. |
|
3396 |
iflags at:#ignpar put:ignpar. |
|
3397 |
iflags at:#parmrk put:parmrk. |
|
3398 |
iflags at:#inpck put:inpck. |
|
3399 |
iflags at:#istrip put:istrip. |
|
3400 |
iflags at:#inlcr put:inlcr. |
|
3401 |
iflags at:#igncr put:igncr. |
|
3402 |
iflags at:#icrnl put:icrnl. |
|
16509
2cf139885242
Fixed ExternalStream>>tcgetattr
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16508
diff
changeset
|
3403 |
iflags at:#uiclc put:iuclc. |
16508 | 3404 |
iflags at:#ixon put:ixon. |
3405 |
iflags at:#ixoff put:ixoff. |
|
3406 |
iflags at:#ixany put:ixany. |
|
3407 |
iflags at:#imaxbel put:imaxbel. |
|
3408 |
iflags at:#iutf8 put:iutf8. |
|
3409 |
||
3410 |
oflags at:#opost put:opost. |
|
3411 |
oflags at:#onlcr put:onlcr. |
|
3412 |
oflags at:#oxtabs put:oxtabs. |
|
3413 |
oflags at:#onoteot put:onoteot. |
|
3414 |
||
3415 |
cflags at:#csize put:csize. |
|
3416 |
cflags at:#cstopb put:cstopb. |
|
3417 |
cflags at:#cread put:cread. |
|
3418 |
cflags at:#parenb put:parenb. |
|
3419 |
cflags at:#parodd put:parodd. |
|
3420 |
cflags at:#hupcl put:hupcl. |
|
3421 |
cflags at:#clocal put:clocal. |
|
3422 |
||
3423 |
lflags at:#echoke put:echoke. |
|
3424 |
lflags at:#echoe put:echoe. |
|
3425 |
lflags at:#echok put:echok. |
|
3426 |
lflags at:#echo put:echo. |
|
3427 |
lflags at:#echonl put:echonl. |
|
3428 |
lflags at:#echoprt put:echoprt. |
|
3429 |
lflags at:#echoctl put:echoctl. |
|
3430 |
lflags at:#isig put:isig. |
|
3431 |
lflags at:#icanon put:icanon. |
|
3432 |
lflags at:#altwerase put:altwerase. |
|
3433 |
lflags at:#iexten put:iexten. |
|
3434 |
lflags at:#extproc put:extproc. |
|
3435 |
lflags at:#tostop put:tostop. |
|
3436 |
^ ret |
|
612 | 3437 |
! ! |
3438 |
||
3439 |
!ExternalStream methodsFor:'non homogenous reading'! |
|
3440 |
||
17609 | 3441 |
next:count into:aCollection startingAt:start |
3442 |
|readCount| |
|
3443 |
||
3444 |
readCount := self nextBytes:count into:aCollection startingAt:start. |
|
3445 |
readCount = count ifTrue:[ |
|
18797 | 3446 |
^ aCollection. |
17609 | 3447 |
]. |
3448 |
^ aCollection copyFrom:1 to:start+readCount-1. |
|
15254 | 3449 |
! |
3450 |
||
3674 | 3451 |
nextAvailable:count |
3452 |
"return the next count elements of the stream as aCollection. |
|
3453 |
If the stream reaches the end before count elements have been read, |
|
3454 |
return what is available. (i.e. a shorter collection). |
|
3455 |
The type of collection is specified in #contentsSpecies." |
|
3456 |
||
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3949
diff
changeset
|
3457 |
|buffer n| |
3674 | 3458 |
|
3459 |
binary ifTrue:[ |
|
18797 | 3460 |
buffer := ByteArray uninitializedNew:count |
3674 | 3461 |
] ifFalse:[ |
18797 | 3462 |
buffer := String uninitializedNew:count |
3674 | 3463 |
]. |
3464 |
n := self nextAvailableBytes:count into:buffer startingAt:1. |
|
5202
e8130049b18f
#nextAvailableBytes:into:startingAt: may NOT set EOF flag,
Claus Gittinger <cg@exept.de>
parents:
5187
diff
changeset
|
3465 |
n == 0 ifTrue:[ |
18797 | 3466 |
binary ifTrue:[ |
3467 |
^ #[] |
|
3468 |
]. |
|
3469 |
^ '' |
|
5202
e8130049b18f
#nextAvailableBytes:into:startingAt: may NOT set EOF flag,
Claus Gittinger <cg@exept.de>
parents:
5187
diff
changeset
|
3470 |
]. |
3674 | 3471 |
|
3472 |
n ~~ count ifTrue:[ |
|
18797 | 3473 |
^ buffer copyTo:n |
3674 | 3474 |
]. |
3475 |
^ buffer. |
|
3476 |
||
3477 |
" |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3478 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:3 |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3479 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:10 |
3674 | 3480 |
(ReadStream on:'hello') nextAvailable:3 |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3481 |
(ReadStream on:'hello') nextAvailable:10 |
3674 | 3482 |
" |
3483 |
||
3484 |
"Modified: / 16.6.1998 / 15:52:41 / cg" |
|
3485 |
! |
|
3486 |
||
17608 | 3487 |
nextAvailable:count into:aCollection startingAt:pos |
3488 |
"return the next count elements of the stream into aCollection at position pos." |
|
3489 |
||
3490 |
^ self nextAvailableBytes:count into:aCollection startingAt:pos. |
|
3491 |
! |
|
3492 |
||
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3493 |
nextAvailableBytes:count into:anObject startingAt:start |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3494 |
"read the next count bytes into an object and return the number of |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3495 |
bytes read or the number of bytes read, if EOF is encountered before, |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3496 |
or no more bytes are available for reading (from the pipe/socket). |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3497 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3498 |
If the receiver is some socket/pipe-like stream, an exception |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3499 |
is raised if the connection is broken. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3500 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3501 |
Notice, that in contrast to other methods, |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3502 |
this does NOT return nil on EOF, but the actual count. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3503 |
Thus allowing read of partial blocks. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3504 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3505 |
The object must have non-pointer indexed instvars |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3506 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3507 |
If anObject is a string or byteArray and reused, this provides the |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3508 |
fastest possible physical I/O (since no new objects are allocated). |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3509 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3510 |
Use with care - non object oriented I/O. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3511 |
Warning: in general, you cannot use this method to pass data from other |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3512 |
architectures since it does not care for byte order or float representation." |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3513 |
|
16296 | 3514 |
|error| |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3515 |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3516 |
%{ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3517 |
FILEPOINTER f; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3518 |
int cnt, offs, ret, _buffered; |
15987 | 3519 |
int objSize, nInstBytes; |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3520 |
char *cp; |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3521 |
char *extPtr; |
7195
755c46f9b774
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3522 |
OBJ fp; |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3523 |
int o_offs; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3524 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3525 |
__INST(lastErrorNumber) = nil; |
12548 | 3526 |
if ((__INST(handleType) == nil) |
3527 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3528 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3529 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3530 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3531 |
if (((fp = __INST(handle)) != nil) |
3532 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3533 |
&& __bothSmallInteger(count, start) |
|
3534 |
) { |
|
3535 |
f = __FILEVal(fp); |
|
3536 |
||
3537 |
cnt = __intVal(count); |
|
3538 |
offs = __intVal(start) - 1; |
|
3539 |
||
3540 |
if (__isExternalBytesLike(anObject)) { |
|
3541 |
OBJ sz; |
|
3542 |
||
3543 |
nInstBytes = 0; |
|
3544 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
3545 |
if (extPtr == NULL) goto bad; |
|
3546 |
sz = __externalBytesSize(anObject); |
|
3547 |
if (__isSmallInteger(sz)) { |
|
3548 |
objSize = __intVal(sz); |
|
3549 |
} else { |
|
3550 |
objSize = 0; /* unknown */ |
|
3551 |
} |
|
3552 |
} else { |
|
3553 |
OBJ oClass = __Class(anObject); |
|
3554 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
3555 |
||
3556 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
3557 |
||
3558 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
3559 |
case BYTEARRAY: |
|
3560 |
case WORDARRAY: |
|
3561 |
case LONGARRAY: |
|
3562 |
case SWORDARRAY: |
|
3563 |
case SLONGARRAY: |
|
3564 |
case FLOATARRAY: |
|
3565 |
break; |
|
3566 |
case DOUBLEARRAY: |
|
15987 | 3567 |
#ifdef __NEED_DOUBLE_ALIGN |
18797 | 3568 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
15987 | 3569 |
#endif |
18797 | 3570 |
break; |
3571 |
case LONGLONGARRAY: |
|
3572 |
case SLONGLONGARRAY: |
|
16296 | 3573 |
#ifdef __NEED_LONGLONG_ALIGN |
18797 | 3574 |
nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1); |
5202
e8130049b18f
#nextAvailableBytes:into:startingAt: may NOT set EOF flag,
Claus Gittinger <cg@exept.de>
parents:
5187
diff
changeset
|
3575 |
#endif |
18797 | 3576 |
break; |
3577 |
default: |
|
3578 |
goto bad; |
|
3579 |
} |
|
3580 |
extPtr = (char *)0; |
|
3581 |
objSize = __Size(anObject) - nInstBytes; |
|
3582 |
} |
|
3583 |
||
3584 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
3585 |
_buffered = (__INST(buffered) == true); |
|
3586 |
if (_buffered) { |
|
3587 |
__READING__(f); |
|
3588 |
} |
|
3589 |
||
3590 |
if (extPtr) { |
|
3591 |
__READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType)); |
|
3592 |
} else { |
|
3593 |
/* |
|
3594 |
* on interrupt, anObject may be moved to another location. |
|
3595 |
* So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__, |
|
3596 |
* to get a new address. |
|
3597 |
*/ |
|
3598 |
offs += nInstBytes; |
|
3599 |
__READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType)); |
|
3600 |
} |
|
3601 |
/* 0 is NOT an EOF condition here ... */ |
|
3602 |
if (ret >= 0) { |
|
3603 |
if (__isSmallInteger(__INST(position))) { |
|
3604 |
INT np = __intVal(__INST(position)) + ret; |
|
3605 |
OBJ t; |
|
3606 |
||
3607 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3608 |
} else { |
|
3609 |
__INST(position) = nil; /* i.e. do not know */ |
|
3610 |
} |
|
3611 |
RETURN (__mkSmallInteger(ret)); |
|
3612 |
} |
|
3613 |
__INST(position) = nil; |
|
3614 |
error = __mkSmallInteger(__threadErrno); |
|
3615 |
} |
|
3616 |
} |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3617 |
} |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3618 |
bad: ; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3619 |
%}. |
8614 | 3620 |
(hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0]. |
16296 | 3621 |
error notNil ifTrue:[ |
18797 | 3622 |
lastErrorNumber := error. |
3623 |
^ self readError:error |
|
16296 | 3624 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3625 |
handle isNil ifTrue:[^ self errorNotOpen]. |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3626 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3627 |
" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3628 |
count not integer or arg not bit-like (String, ByteArray etc) |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3629 |
" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3630 |
^ self primitiveFailed |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3631 |
! |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3632 |
|
612 | 3633 |
nextByte |
3634 |
"read the next byte and return it as an Integer; return nil on error. |
|
3635 |
This is allowed in both text and binary modes, always returning the |
|
3636 |
bytes binary value as an integer in 0..255." |
|
3637 |
||
16296 | 3638 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3639 |
%{ |
18364 | 3640 |
#ifdef __SCHTEAM__ |
3641 |
STObject handle = self.instVarAt(I_handle); |
|
3642 |
||
3643 |
if (handle != STObject.Nil) { |
|
18797 | 3644 |
STObject next; |
3645 |
||
3646 |
next = handle.nextByte(); |
|
3647 |
if (next != STObject.EOF) { |
|
3648 |
self.instVarAt_put(I_position, STObject.Nil); |
|
3649 |
return __c__._RETURN( next ); |
|
3650 |
} |
|
3651 |
self.instVarAt_put(I_hitEOF, STObject.True); |
|
18364 | 3652 |
} |
3653 |
#else |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3654 |
FILEPOINTER f; |
612 | 3655 |
unsigned char byte; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3656 |
int ret, _buffered; |
612 | 3657 |
OBJ fp; |
3658 |
||
1133 | 3659 |
__INST(lastErrorNumber) = nil; |
12548 | 3660 |
if ((__INST(handleType) == nil) |
3661 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3662 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3663 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3664 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3665 |
if (((fp = __INST(handle)) != nil) |
3666 |
&& (__INST(mode) != @symbol(writeonly))) { |
|
3667 |
f = __FILEVal(fp); |
|
3668 |
||
3669 |
_buffered = (__INST(buffered) == true); |
|
3670 |
if (_buffered) { |
|
3671 |
__READING__(f) |
|
3672 |
} |
|
3673 |
__READBYTE__(ret, f, &byte, _buffered, __INST(handleType)); |
|
3674 |
if (ret > 0) { |
|
3675 |
if (__isSmallInteger(__INST(position))) { |
|
3676 |
INT np = __intVal(__INST(position)) + 1; |
|
3677 |
OBJ t; |
|
3678 |
||
3679 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3680 |
} else { |
|
3681 |
__INST(position) = nil; /* i.e. do not know */ |
|
3682 |
} |
|
3683 |
RETURN (__mkSmallInteger(byte)); |
|
3684 |
} |
|
3685 |
||
3686 |
if (ret == 0) { |
|
3687 |
__INST(hitEOF) = true; |
|
3688 |
} else /* ret < 0 */ { |
|
3689 |
__INST(position) = nil; |
|
3690 |
error = __mkSmallInteger(__threadErrno); |
|
3691 |
} |
|
3692 |
} |
|
612 | 3693 |
} |
18364 | 3694 |
#endif /* not SCHTEAM */ |
612 | 3695 |
%}. |
8614 | 3696 |
hitEOF ifTrue:[^ self pastEndRead]. |
16296 | 3697 |
error notNil ifTrue:[ |
18797 | 3698 |
lastErrorNumber := error. |
3699 |
^ self readError:error |
|
16296 | 3700 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3701 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 3702 |
^ self errorWriteOnly |
3703 |
! |
|
3704 |
||
3705 |
nextBytes:count into:anObject startingAt:start |
|
3706 |
"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
|
3707 |
bytes read or the number of bytes read, if EOF is encountered before. |
612 | 3708 |
If the receiver is some socket/pipe-like stream, an exception |
3709 |
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
|
3710 |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3711 |
Warning: if used with a pipe/socket, this blocks until the requested number |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3712 |
of bytes have been read. See #nextAvailableBytes:into:startingAt: |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3713 |
to only read whats there. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3714 |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3715 |
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
|
3716 |
this does NOT return nil on EOF, but the actual count. |
612 | 3717 |
Thus allowing read of partial blocks. |
3718 |
||
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3719 |
The object must have non-pointer indexed instvars |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3720 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray), |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3721 |
or an externalBytes object (with known size). |
612 | 3722 |
If anObject is a string or byteArray and reused, this provides the |
3723 |
fastest possible physical I/O (since no new objects are allocated). |
|
3724 |
||
3725 |
Use with care - non object oriented I/O. |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3726 |
Warning: in general, you cannot use this method to pass data from other |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3727 |
architectures (unless you prepared the buffer with care), |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3728 |
since it does not care for byte order or float representation." |
612 | 3729 |
|
16296 | 3730 |
|error| |
612 | 3731 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3732 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3733 |
int cnt, offs, ret, _buffered; |
15987 | 3734 |
int objSize, nInstBytes; |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3735 |
char *extPtr; |
7195
755c46f9b774
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3736 |
OBJ fp; |
612 | 3737 |
|
1133 | 3738 |
__INST(lastErrorNumber) = nil; |
12548 | 3739 |
if ((__INST(handleType) == nil) |
3740 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3741 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3742 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3743 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3744 |
if (((fp = __INST(handle)) != nil) |
3745 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3746 |
&& __bothSmallInteger(count, start) |
|
3747 |
) { |
|
3748 |
f = __FILEVal(fp); |
|
3749 |
||
3750 |
cnt = __intVal(count); |
|
3751 |
offs = __intVal(start) - 1; |
|
3752 |
||
3753 |
if (__isExternalBytesLike(anObject)) { |
|
3754 |
OBJ sz; |
|
3755 |
||
3756 |
nInstBytes = 0; |
|
3757 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
3758 |
if (extPtr == NULL) goto bad; |
|
3759 |
sz = __externalBytesSize(anObject); |
|
3760 |
if (__isSmallInteger(sz)) { |
|
3761 |
objSize = __intVal(sz); |
|
3762 |
} else { |
|
3763 |
objSize = 0; /* unknown */ |
|
3764 |
} |
|
3765 |
} else { |
|
3766 |
OBJ oClass = __Class(anObject); |
|
3767 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
3768 |
||
3769 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
3770 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
3771 |
case BYTEARRAY: |
|
3772 |
case WORDARRAY: |
|
3773 |
case LONGARRAY: |
|
3774 |
case SWORDARRAY: |
|
3775 |
case SLONGARRAY: |
|
3776 |
case FLOATARRAY: |
|
3777 |
break; |
|
3778 |
case DOUBLEARRAY: |
|
15987 | 3779 |
#ifdef __NEED_DOUBLE_ALIGN |
18797 | 3780 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
15987 | 3781 |
#endif |
18797 | 3782 |
break; |
3783 |
case LONGLONGARRAY: |
|
3784 |
case SLONGLONGARRAY: |
|
16296 | 3785 |
#ifdef __NEED_LONGLONG_ALIGN |
18797 | 3786 |
nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1); |
16296 | 3787 |
#endif |
18797 | 3788 |
break; |
3789 |
default: |
|
3790 |
goto bad; |
|
3791 |
} |
|
3792 |
extPtr = (char *)0; |
|
3793 |
objSize = __Size(anObject) - nInstBytes; |
|
3794 |
} |
|
3795 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
3796 |
_buffered = (__INST(buffered) == true); |
|
3797 |
if (_buffered) { |
|
3798 |
__READING__(f); |
|
3799 |
} |
|
3800 |
||
3801 |
if (extPtr) { |
|
3802 |
__READBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType)); |
|
3803 |
} else { |
|
3804 |
/* |
|
3805 |
* on interrupt, anObject may be moved to another location. |
|
3806 |
* So we pass anObject, and the offset to the __READBYTES_OBJ__ macro. |
|
3807 |
*/ |
|
3808 |
offs += nInstBytes; |
|
3809 |
__READBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType)); |
|
3810 |
} |
|
3811 |
||
3812 |
if (ret > 0) { |
|
3813 |
if (__isSmallInteger(__INST(position))) { |
|
3814 |
INT np = __intVal(__INST(position)) + ret; |
|
3815 |
OBJ t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3816 |
} else { |
|
3817 |
__INST(position) = nil; /* i.e. do not know */ |
|
3818 |
} |
|
3819 |
RETURN (__mkSmallInteger(ret)); |
|
3820 |
} |
|
3821 |
if (ret == 0) { |
|
3822 |
__INST(hitEOF) = true; |
|
3823 |
} else /* ret < 0 */ { |
|
3824 |
__INST(position) = nil; |
|
3825 |
error = __mkSmallInteger(__threadErrno); |
|
3826 |
} |
|
3827 |
} |
|
3828 |
} |
|
612 | 3829 |
} |
3830 |
bad: ; |
|
3831 |
%}. |
|
8614 | 3832 |
(hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0]. |
16296 | 3833 |
error notNil ifTrue:[ |
18797 | 3834 |
lastErrorNumber := error. |
3835 |
^ self readError:error |
|
16296 | 3836 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3837 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 3838 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
3839 |
" |
|
3840 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
3841 |
" |
|
3842 |
^ self primitiveFailed |
|
92 | 3843 |
! |
3844 |
||
612 | 3845 |
nextLong |
3846 |
"Read four bytes (msb-first) and return the value as a 32-bit signed Integer. |
|
3847 |
The returned value may be a LargeInteger. |
|
3848 |
(msb-first for compatibility with other smalltalks)" |
|
3849 |
||
3850 |
^ self nextUnsignedLongMSB:true |
|
3851 |
! |
|
3852 |
||
3853 |
nextLongMSB:msbFlag |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3854 |
"Read four bytes and return the value as a 32-bit signed Integer, |
612 | 3855 |
which may be a LargeInteger. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3856 |
If msbFlag is true, value is read with most-significant byte first, |
612 | 3857 |
otherwise least-significant byte comes first. |
3858 |
A nil is returned, if EOF is hit before all 4 bytes have been read. |
|
3859 |
Works in both binary and text modes." |
|
3860 |
||
16296 | 3861 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3862 |
%{ |
612 | 3863 |
OBJ fp; |
3864 |
||
1133 | 3865 |
__INST(lastErrorNumber) = nil; |
12548 | 3866 |
if ((__INST(handleType) == nil) |
3867 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3868 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3869 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3870 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3871 |
if (((fp = __INST(handle)) != nil) |
3872 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3873 |
) { |
|
3874 |
FILEPOINTER f; |
|
3875 |
int ret, _buffered; |
|
3876 |
int value; |
|
3877 |
union { |
|
3878 |
unsigned char buffer[4]; |
|
3879 |
int intVal; |
|
3880 |
} u; |
|
3881 |
||
3882 |
f = __FILEVal(fp); |
|
3883 |
_buffered = (__INST(buffered) == true); |
|
3884 |
if (_buffered) { |
|
3885 |
__READING__(f) |
|
3886 |
} |
|
3887 |
__READBYTES__(ret, f, u.buffer, 4, _buffered, __INST(handleType)); |
|
3888 |
||
3889 |
if (ret == 4) { |
|
3890 |
if (__isSmallInteger(__INST(position))) { |
|
3891 |
INT np = __intVal(__INST(position)) + 4; |
|
3892 |
OBJ t; |
|
3893 |
||
3894 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3895 |
} else { |
|
3896 |
__INST(position) = nil; /* i.e. do not know */ |
|
3897 |
} |
|
3898 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
3899 |
#if defined(__MSBFIRST__) |
18797 | 3900 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3901 |
#else |
18797 | 3902 |
value = (u.buffer[0] & 0xFF); |
3903 |
value = (value << 8) | (u.buffer[1] & 0xFF); |
|
3904 |
value = (value << 8) | (u.buffer[2] & 0xFF); |
|
3905 |
value = (value << 8) | (u.buffer[3] & 0xFF); |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3906 |
#endif |
18797 | 3907 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
3908 |
#if defined(__LSBFIRST__) |
18797 | 3909 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3910 |
#else |
18797 | 3911 |
value = (u.buffer[3] & 0xFF); |
3912 |
value = (value << 8) | (u.buffer[2] & 0xFF); |
|
3913 |
value = (value << 8) | (u.buffer[1] & 0xFF); |
|
3914 |
value = (value << 8) | (u.buffer[0] & 0xFF); |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3915 |
#endif |
18797 | 3916 |
} |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3917 |
#if __POINTER_SIZE__ == 8 |
18797 | 3918 |
RETURN ( __mkSmallInteger(value)); |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3919 |
#else |
18797 | 3920 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) { |
3921 |
RETURN ( __mkSmallInteger(value)); |
|
3922 |
} |
|
3923 |
RETURN ( __MKLARGEINT(value) ); |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3924 |
#endif |
18797 | 3925 |
} |
3926 |
||
3927 |
if (ret < 0) { |
|
3928 |
__INST(position) = nil; |
|
3929 |
error = __mkSmallInteger(__threadErrno); |
|
3930 |
} else /* ret == 0 */ { |
|
3931 |
__INST(hitEOF) = true; |
|
3932 |
} |
|
3933 |
} |
|
612 | 3934 |
} |
3935 |
%}. |
|
8614 | 3936 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3937 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 3938 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 3939 |
lastErrorNumber := error. |
3940 |
^ self readError:error. |
|
362 | 3941 |
! |
3942 |
||
612 | 3943 |
nextShortMSB:msbFlag |
3944 |
"Read two bytes and return the value as a 16-bit signed Integer. |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3945 |
If msbFlag is true, value is read with most-significant byte first, |
612 | 3946 |
otherwise least-significant byte comes first. |
3947 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
3948 |
Works in both binary and text modes." |
|
3949 |
||
16296 | 3950 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3951 |
%{ |
612 | 3952 |
OBJ fp; |
3953 |
||
1133 | 3954 |
__INST(lastErrorNumber) = nil; |
12548 | 3955 |
if ((__INST(handleType) == nil) |
3956 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3957 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3958 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3959 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3960 |
if (((fp = __INST(handle)) != nil) |
3961 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3962 |
) { |
|
3963 |
FILEPOINTER f; |
|
3964 |
int ret, _buffered; |
|
3965 |
short value; |
|
3966 |
union { |
|
3967 |
unsigned char buffer[2]; |
|
3968 |
short shortVal; |
|
3969 |
} u; |
|
3970 |
||
3971 |
f = __FILEVal(fp); |
|
3972 |
_buffered = (__INST(buffered) == true); |
|
3973 |
if (_buffered) { |
|
3974 |
__READING__(f) |
|
3975 |
} |
|
3976 |
__READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType)); |
|
3977 |
||
3978 |
if (ret == 2) { |
|
3979 |
if (__isSmallInteger(__INST(position))) { |
|
3980 |
INT np = __intVal(__INST(position)) + 2; |
|
3981 |
OBJ t; |
|
3982 |
||
3983 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3984 |
} else { |
|
3985 |
__INST(position) = nil; /* i.e. do not know */ |
|
3986 |
} |
|
3987 |
if (msbFlag == true) { |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3988 |
#if defined(__MSBFIRST__) |
18797 | 3989 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3990 |
#else |
18797 | 3991 |
value = ((u.buffer[0] & 0xFF) << 8) | (u.buffer[1] & 0xFF); |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3992 |
#endif |
18797 | 3993 |
} else { |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3994 |
#if defined(__LSBFIRST__) |
18797 | 3995 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3996 |
#else |
18797 | 3997 |
value = ((u.buffer[1] & 0xFF) << 8) | (u.buffer[0] & 0xFF); |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3998 |
#endif |
18797 | 3999 |
} |
4000 |
RETURN (__mkSmallInteger(value)); |
|
4001 |
} |
|
4002 |
||
4003 |
if (ret < 0) { |
|
4004 |
__INST(position) = nil; /* i.e. do not know */ |
|
4005 |
error = __mkSmallInteger(__threadErrno); |
|
4006 |
} else /* ret == 0 */ { |
|
4007 |
__INST(hitEOF) = true; |
|
4008 |
} |
|
4009 |
} |
|
612 | 4010 |
} |
4011 |
%}. |
|
8614 | 4012 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4013 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 4014 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 4015 |
lastErrorNumber := error. |
4016 |
^ self readError:error. |
|
612 | 4017 |
! |
4018 |
||
4019 |
nextUnsignedLongMSB:msbFlag |
|
4020 |
"Read four bytes and return the value as a 32-bit unsigned Integer, which may be |
|
4021 |
a LargeInteger. |
|
4022 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
4023 |
least-significant byte comes first. |
|
4024 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read. |
|
4025 |
Works in both binary and text modes." |
|
4026 |
||
16296 | 4027 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4028 |
%{ |
612 | 4029 |
OBJ fp; |
4030 |
||
1133 | 4031 |
__INST(lastErrorNumber) = nil; |
12548 | 4032 |
if ((__INST(handleType) == nil) |
4033 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4034 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4035 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4036 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4037 |
if (((fp = __INST(handle)) != nil) |
4038 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4039 |
) { |
|
4040 |
FILEPOINTER f; |
|
4041 |
int ret, _buffered; |
|
4042 |
unsigned INT value; |
|
4043 |
union { |
|
4044 |
unsigned char buffer[4]; |
|
4045 |
unsigned int intVal; |
|
4046 |
} u; |
|
4047 |
||
4048 |
f = __FILEVal(fp); |
|
4049 |
_buffered = (__INST(buffered) == true); |
|
4050 |
if (_buffered) { |
|
4051 |
__READING__(f) |
|
4052 |
} |
|
4053 |
__READBYTES__(ret, f, u.buffer, 4, _buffered, __INST(handleType)); |
|
4054 |
||
4055 |
if (ret == 4) { |
|
4056 |
if (__isSmallInteger(__INST(position))) { |
|
4057 |
INT np = __intVal(__INST(position)) + 4; |
|
4058 |
OBJ t; |
|
4059 |
||
4060 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4061 |
} else { |
|
4062 |
__INST(position) = nil; /* i.e. do not know */ |
|
4063 |
} |
|
4064 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4065 |
#if defined(__MSBFIRST__) |
18797 | 4066 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4067 |
#else |
18797 | 4068 |
value = u.buffer[0]; |
4069 |
value = (value << 8) | u.buffer[1]; |
|
4070 |
value = (value << 8) | u.buffer[2]; |
|
4071 |
value = (value << 8) | u.buffer[3]; |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4072 |
#endif |
18797 | 4073 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4074 |
#if defined(__LSBFIRST__) |
18797 | 4075 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4076 |
#else |
18797 | 4077 |
value = u.buffer[3]; |
4078 |
value = (value << 8) | u.buffer[2]; |
|
4079 |
value = (value << 8) | u.buffer[1]; |
|
4080 |
value = (value << 8) | u.buffer[0]; |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4081 |
#endif |
18797 | 4082 |
} |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4083 |
#if __POINTER_SIZE__ == 8 |
18797 | 4084 |
value &= 0xFFFFFFFF; |
4085 |
RETURN (__mkSmallInteger(value)); |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4086 |
#else |
18797 | 4087 |
if (value <= _MAX_INT) { |
4088 |
RETURN (__mkSmallInteger(value)); |
|
4089 |
} |
|
4090 |
RETURN (__MKULARGEINT(value) ); |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4091 |
#endif |
18797 | 4092 |
} |
4093 |
||
4094 |
if (ret < 0) { |
|
4095 |
__INST(position) = nil; /* i.e. do not know */ |
|
4096 |
error = __mkSmallInteger(__threadErrno); |
|
4097 |
} else /* ret == 0 */ { |
|
4098 |
__INST(hitEOF) = true; |
|
4099 |
} |
|
4100 |
} |
|
612 | 4101 |
} |
4102 |
%}. |
|
8614 | 4103 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4104 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 4105 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 4106 |
lastErrorNumber := error. |
4107 |
^ self readError:error. |
|
612 | 4108 |
! |
4109 |
||
4110 |
nextUnsignedShortMSB:msbFlag |
|
4111 |
"Read two bytes and return the value as a 16-bit unsigned Integer. |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
4112 |
If msbFlag is true, value is read with most-significant byte first, |
1079 | 4113 |
otherwise least-significant byte comes first. |
612 | 4114 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
4115 |
Works in both binary and text modes." |
|
4116 |
||
16296 | 4117 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4118 |
%{ |
612 | 4119 |
OBJ fp; |
4120 |
||
1133 | 4121 |
__INST(lastErrorNumber) = nil; |
12548 | 4122 |
if ((__INST(handleType) == nil) |
4123 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4124 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4125 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4126 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4127 |
if (((fp = __INST(handle)) != nil) |
4128 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4129 |
) { |
|
4130 |
FILEPOINTER f; |
|
4131 |
int ret, _buffered; |
|
4132 |
unsigned int value; |
|
4133 |
union { |
|
4134 |
unsigned char buffer[2]; |
|
4135 |
unsigned short shortVal; |
|
4136 |
} u; |
|
4137 |
||
4138 |
f = __FILEVal(fp); |
|
4139 |
_buffered = (__INST(buffered) == true); |
|
4140 |
if (_buffered) { |
|
4141 |
__READING__(f) |
|
4142 |
} |
|
4143 |
__READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType)); |
|
4144 |
||
4145 |
if (ret == 2) { |
|
4146 |
if (__isSmallInteger(__INST(position))) { |
|
4147 |
INT np = __intVal(__INST(position)) + 2; |
|
4148 |
OBJ t; |
|
4149 |
||
4150 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4151 |
} else { |
|
4152 |
__INST(position) = nil; /* i.e. do not know */ |
|
4153 |
} |
|
4154 |
if (msbFlag == true) { |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4155 |
#if defined(__MSBFIRST__) |
18797 | 4156 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4157 |
#else |
18797 | 4158 |
value = (u.buffer[0] << 8) | u.buffer[1]; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4159 |
#endif |
18797 | 4160 |
} else { |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4161 |
#if defined(__LSBFIRST__) |
18797 | 4162 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4163 |
#else |
18797 | 4164 |
value = (u.buffer[1] << 8) | u.buffer[0]; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4165 |
#endif |
18797 | 4166 |
} |
4167 |
RETURN (__mkSmallInteger(value)); |
|
4168 |
} |
|
4169 |
||
4170 |
if (ret < 0) { |
|
4171 |
__INST(position) = nil; /* i.e. do not know */ |
|
4172 |
error = __mkSmallInteger(__threadErrno); |
|
4173 |
} else /* ret == 0 */ { |
|
4174 |
__INST(hitEOF) = true; |
|
4175 |
} |
|
4176 |
} |
|
612 | 4177 |
} |
4178 |
%}. |
|
8614 | 4179 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4180 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 4181 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 4182 |
lastErrorNumber := error. |
4183 |
^ self readError:error. |
|
612 | 4184 |
! |
4185 |
||
4186 |
nextWord |
|
15452 | 4187 |
<resource: #obsolete> |
612 | 4188 |
"in text-mode: |
18797 | 4189 |
read the alphaNumeric next word (i.e. up to non letter-or-digit). |
4190 |
return a string containing those characters. |
|
612 | 4191 |
in binary-mode: |
18797 | 4192 |
read two bytes (msb-first) and return the value as a 16-bit |
4193 |
unsigned Integer (for compatibility with other smalltalks)" |
|
612 | 4194 |
|
4195 |
binary ifTrue:[ |
|
18797 | 4196 |
self obsoleteMethodWarning:'use #nextUnsignedShortMSB:true'. |
4197 |
^ self nextUnsignedShortMSB:true |
|
92 | 4198 |
]. |
14518 | 4199 |
self obsoleteMethodWarning:'use #nextAlphaNumericWord'. |
612 | 4200 |
^ self nextAlphaNumericWord |
4201 |
! ! |
|
4202 |
||
4203 |
!ExternalStream methodsFor:'non homogenous writing'! |
|
4204 |
||
4205 |
nextPutByte:aByteValue |
|
4206 |
"write a byte. |
|
4207 |
Works in both binary and text modes." |
|
4208 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4209 |
|error| |
16296 | 4210 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4211 |
%{ |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
4212 |
OBJ fp; |
612 | 4213 |
|
1133 | 4214 |
__INST(lastErrorNumber) = nil; |
12548 | 4215 |
if ((__INST(handleType) == nil) |
4216 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4217 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4218 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4219 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4220 |
if (((fp = __INST(handle)) != nil) |
4221 |
&& (__INST(mode) != @symbol(readonly)) |
|
4222 |
&& __isSmallInteger(aByteValue) |
|
4223 |
||
4224 |
) { |
|
4225 |
int _buffered = (__INST(buffered) == true); |
|
4226 |
FILEPOINTER f = __FILEVal(fp); |
|
4227 |
char c = __intVal(aByteValue); |
|
4228 |
int cnt; |
|
4229 |
||
4230 |
if (_buffered) { |
|
4231 |
__WRITING__(f) |
|
4232 |
} |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
4233 |
#ifdef WIN32 |
18797 | 4234 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
4235 |
cnt = __win32_fwrite(&c, 1, 1, f); |
|
4236 |
} else |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
4237 |
#endif |
18797 | 4238 |
{ |
4239 |
__WRITEBYTE__(cnt, f, &c, _buffered, __INST(handleType)); |
|
4240 |
} |
|
4241 |
if (cnt == 1) { |
|
4242 |
if (__isSmallInteger(__INST(position))) { |
|
4243 |
INT np = __intVal(__INST(position)) + 1; |
|
4244 |
OBJ t; |
|
4245 |
||
4246 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4247 |
} else { |
|
4248 |
__INST(position) = nil; /* i.e. do not know */ |
|
4249 |
} |
|
4250 |
RETURN (self); |
|
4251 |
} |
|
4252 |
if (cnt < 0) { |
|
4253 |
__INST(position) = nil; /* i.e. do not know */ |
|
4254 |
} |
|
4255 |
error = __mkSmallInteger(__threadErrno); |
|
4256 |
} |
|
612 | 4257 |
} |
4258 |
%}. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4259 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
7801
d930c353588d
writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents:
7737
diff
changeset
|
4260 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
16296 | 4261 |
lastErrorNumber := error. |
4262 |
self writeError:error. |
|
612 | 4263 |
! |
4264 |
||
4265 |
nextPutLong:aNumber MSB:msbFlag |
|
4266 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
4267 |
true, data is written most-significant byte first; otherwise least |
|
4268 |
first. |
|
4269 |
Works in both binary and text modes." |
|
4270 |
||
16296 | 4271 |
|error| |
4272 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4273 |
%{ |
612 | 4274 |
int num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4275 |
union { |
18797 | 4276 |
char bytes[4]; |
4277 |
int intVal; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4278 |
} u; |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4279 |
OBJ fp; |
612 | 4280 |
|
1133 | 4281 |
__INST(lastErrorNumber) = nil; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4282 |
if (__isSmallInteger(aNumber)) { |
18797 | 4283 |
num = __intVal(aNumber); |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4284 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4285 |
#if __POINTER_SIZE__ == 8 |
18797 | 4286 |
// always more than 4-bytes |
4287 |
goto badArg; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4288 |
#else |
18797 | 4289 |
num = __longIntVal(aNumber); |
4290 |
if (num == 0) { |
|
4291 |
num = __signedLongIntVal(aNumber); |
|
4292 |
if (num == 0) { |
|
4293 |
/* bad arg or out-of-range integer |
|
4294 |
* (handled by the fallBack code) |
|
4295 |
*/ |
|
4296 |
goto badArg; |
|
4297 |
} |
|
4298 |
} |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4299 |
#endif |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4300 |
} |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4301 |
|
12548 | 4302 |
if ((__INST(handleType) == nil) |
4303 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4304 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4305 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4306 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4307 |
if (((fp = __INST(handle)) != nil) |
4308 |
&& (__INST(mode) != @symbol(readonly)) |
|
4309 |
) { |
|
4310 |
int _buffered = (__INST(buffered) == true); |
|
4311 |
FILEPOINTER f = __FILEVal(fp); |
|
4312 |
int cnt; |
|
4313 |
||
4314 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4315 |
#if defined(__MSBFIRST__) |
18797 | 4316 |
u.intVal = num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4317 |
#else |
18797 | 4318 |
u.bytes[0] = (num >> 24) & 0xFF; |
4319 |
u.bytes[1] = (num >> 16) & 0xFF; |
|
4320 |
u.bytes[2] = (num >> 8) & 0xFF; |
|
4321 |
u.bytes[3] = num & 0xFF; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4322 |
#endif |
18797 | 4323 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4324 |
#if defined(__LSBFIRST__) |
18797 | 4325 |
u.intVal = num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4326 |
#else |
18797 | 4327 |
u.bytes[3] = (num >> 24) & 0xFF; |
4328 |
u.bytes[2] = (num >> 16) & 0xFF; |
|
4329 |
u.bytes[1] = (num >> 8) & 0xFF; |
|
4330 |
u.bytes[0] = num & 0xFF; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4331 |
#endif |
18797 | 4332 |
} |
4333 |
||
4334 |
if (_buffered) { |
|
4335 |
__WRITING__(f) |
|
4336 |
} |
|
4337 |
__WRITEBYTES__(cnt, f, u.bytes, 4, _buffered, __INST(handleType)); |
|
4338 |
||
4339 |
if (cnt == 4) { |
|
4340 |
if (__isSmallInteger(__INST(position))) { |
|
4341 |
INT np = __intVal(__INST(position)) + 4; |
|
4342 |
OBJ t; |
|
4343 |
||
4344 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4345 |
} else { |
|
4346 |
__INST(position) = nil; /* i.e. do not know */ |
|
4347 |
} |
|
4348 |
RETURN ( self ); |
|
4349 |
} |
|
4350 |
__INST(position) = nil; /* i.e. do not know */ |
|
4351 |
error = __mkSmallInteger(__threadErrno); |
|
4352 |
} |
|
612 | 4353 |
} |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4354 |
badArg: ; |
612 | 4355 |
%}. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4356 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
7801
d930c353588d
writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents:
7737
diff
changeset
|
4357 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
16296 | 4358 |
error notNil ifTrue:[ |
18797 | 4359 |
lastErrorNumber := error. |
4360 |
self writeError:error. |
|
4361 |
^ self |
|
16296 | 4362 |
]. |
612 | 4363 |
|
4364 |
aNumber isInteger ifTrue:[ |
|
18797 | 4365 |
^ super nextPutLong:aNumber MSB:msbFlag |
92 | 4366 |
]. |
612 | 4367 |
self argumentMustBeInteger |
4368 |
! |
|
4369 |
||
15655 | 4370 |
nextPutShort:anIntegerOrCharacter MSB:msbFlag |
4371 |
"Write the argument, anIntegerOrCharacter as a short (two bytes). If msbFlag is |
|
612 | 4372 |
true, data is written most-significant byte first; otherwise least |
4373 |
first. |
|
4374 |
Works in both binary and text modes." |
|
4375 |
||
16296 | 4376 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4377 |
%{ |
612 | 4378 |
int num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4379 |
union { |
18797 | 4380 |
char bytes[2]; |
4381 |
short shortVal; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4382 |
} u; |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4383 |
OBJ fp; |
612 | 4384 |
|
1133 | 4385 |
__INST(lastErrorNumber) = nil; |
12548 | 4386 |
if ((__INST(handleType) == nil) |
4387 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4388 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4389 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4390 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4391 |
if (((fp = __INST(handle)) != nil) |
4392 |
&& (__INST(mode) != @symbol(readonly)) |
|
4393 |
) { |
|
4394 |
FILEPOINTER f = __FILEVal(fp); |
|
4395 |
int _buffered = (__INST(buffered) == true); |
|
4396 |
int cnt; |
|
4397 |
||
4398 |
if (__isSmallInteger(anIntegerOrCharacter)) { |
|
4399 |
num = __intVal(anIntegerOrCharacter); |
|
4400 |
} else if (__isCharacter(anIntegerOrCharacter)) { |
|
4401 |
num = __smallIntegerVal(__characterVal(anIntegerOrCharacter)); |
|
4402 |
} else |
|
4403 |
goto out; |
|
4404 |
||
4405 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4406 |
#if defined(__MSBFIRST__) |
18797 | 4407 |
u.shortVal = num; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4408 |
#else |
18797 | 4409 |
u.bytes[0] = (num >> 8) & 0xFF; |
4410 |
u.bytes[1] = num & 0xFF; |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4411 |
#endif |
18797 | 4412 |
} else { |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4413 |
#if defined(__LSBFIRST__) |
18797 | 4414 |
u.shortVal = num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4415 |
#else |
18797 | 4416 |
u.bytes[1] = (num >> 8) & 0xFF; |
4417 |
u.bytes[0] = num & 0xFF; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4418 |
#endif |
18797 | 4419 |
} |
4420 |
||
4421 |
if (_buffered) { |
|
4422 |
__WRITING__(f) |
|
4423 |
} |
|
4424 |
__WRITEBYTES__(cnt, f, u.bytes, 2, _buffered, __INST(handleType)); |
|
4425 |
||
4426 |
if (cnt == 2) { |
|
4427 |
if (__isSmallInteger(__INST(position))) { |
|
4428 |
INT np = __intVal(__INST(position)) + 2; |
|
4429 |
OBJ t; |
|
4430 |
||
4431 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4432 |
} else { |
|
4433 |
__INST(position) = nil; /* i.e. do not know */ |
|
4434 |
} |
|
4435 |
RETURN ( self ); |
|
4436 |
} |
|
4437 |
__INST(position) = nil; /* i.e. do not know */ |
|
4438 |
error = __mkSmallInteger(__threadErrno); |
|
4439 |
} |
|
612 | 4440 |
} |
15655 | 4441 |
out:; |
612 | 4442 |
%}. |
16296 | 4443 |
error notNil ifTrue:[ |
18797 | 4444 |
lastErrorNumber := error. |
4445 |
self writeError:error. |
|
4446 |
^ self |
|
16296 | 4447 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4448 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
7801
d930c353588d
writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents:
7737
diff
changeset
|
4449 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
612 | 4450 |
self argumentMustBeInteger |
10 | 4451 |
! ! |
612 | 4452 |
|
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4453 |
!ExternalStream methodsFor:'positioning'! |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4454 |
|
15619 | 4455 |
position |
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4456 |
self subclassResponsibility |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4457 |
! |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4458 |
|
15619 | 4459 |
position:index0Based |
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4460 |
self subclassResponsibility |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4461 |
! ! |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4462 |
|
12631 | 4463 |
!ExternalStream methodsFor:'printing & storing'! |
4464 |
||
4465 |
printOn:aStream |
|
14068 | 4466 |
aStream |
18797 | 4467 |
nextPutAll:self className; |
4468 |
nextPutAll:'(handle:('. |
|
12631 | 4469 |
handle printOn:aStream. |
4470 |
aStream nextPutAll:'))' |
|
4471 |
! ! |
|
4472 |
||
612 | 4473 |
!ExternalStream methodsFor:'private'! |
4474 |
||
4475 |
clearEOF |
|
4476 |
hitEOF := false |
|
18783 | 4477 |
! ! |
4478 |
||
4479 |
!ExternalStream protectedMethodsFor:'private'! |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4480 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4481 |
closeFile |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4482 |
"low level close - may be redefined in subclasses |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4483 |
Don't send this message, send #close instead" |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4484 |
|
12674 | 4485 |
|fp error| |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4486 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4487 |
fp := handle. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4488 |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4489 |
%{ |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4490 |
int rslt; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4491 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4492 |
if (fp == nil) { |
18797 | 4493 |
error = @symbol(errorNotOpen); |
4494 |
goto out; |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4495 |
} |
16508 | 4496 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4497 |
if (__INST(handleType) == @symbol(socketHandle)) { |
18797 | 4498 |
SOCKET sock = __FILEVal(fp); |
4499 |
||
4500 |
if (@global(FileOpenTrace) == true) { |
|
4501 |
fprintf(stderr, "close socket [ExternalStream] %"_lx_"\n", (INT)sock); |
|
4502 |
} |
|
4503 |
||
4504 |
// whether the close() will be successful or not - the handle is invalid now! |
|
4505 |
__INST(handle) = nil; |
|
4506 |
do { |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4507 |
#ifdef WIN32 |
18797 | 4508 |
rslt = __STX_WSA_NOINT_CALL1("closesocket", closesocket, sock); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4509 |
#else |
18797 | 4510 |
rslt = close(sock); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4511 |
#endif |
18797 | 4512 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4513 |
} else if ((__INST(handleType) == nil) |
18797 | 4514 |
|| (__INST(handleType) == @symbol(filePointer)) |
4515 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
4516 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4517 |
{ |
18797 | 4518 |
FILEPOINTER f = __FILEVal(fp); |
4519 |
||
4520 |
if (@global(FileOpenTrace) == true) { |
|
4521 |
fprintf(stderr, "fclose [ExternalStream] %"_lx_"\n", (INT)f); |
|
4522 |
} |
|
4523 |
// whether the close() will be successful or not - the handle is invalid now! |
|
4524 |
__INST(handle) = nil; |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4525 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4526 |
#ifdef WIN32 |
18797 | 4527 |
if (__INST(mode) != @symbol(readonly) && __INST(buffered) != false) { |
4528 |
// do a fflush() first, so that fclose() doesn't block |
|
4529 |
// we suspect, that EINTR causes problems in fclose() |
|
4530 |
do { |
|
4531 |
__threadErrno = 0; |
|
4532 |
rslt = __STX_C_CALL1("fflush", fflush, f); |
|
4533 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
|
4534 |
} |
|
4535 |
do { |
|
4536 |
__threadErrno = 0; |
|
4537 |
rslt = __STX_C_NOINT_CALL1("fclose", fclose, f); |
|
4538 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4539 |
#else |
18797 | 4540 |
// cg: the pre Nov2014 code always did the fclose interruptable; |
4541 |
// I am not sure, if fclose is actually prepared to do this; |
|
4542 |
// at least when only reading, this should not block, and we |
|
4543 |
// should be ableto do it without being interruptable. |
|
4544 |
// Must watch this - if it leads to blockings, change and think about it. |
|
4545 |
if (__INST(mode) != @symbol(readonly)) { |
|
4546 |
__BEGIN_INTERRUPTABLE__ |
|
4547 |
rslt = fclose(f); |
|
4548 |
__END_INTERRUPTABLE__ |
|
4549 |
} else { |
|
4550 |
rslt = fclose(f); |
|
4551 |
} |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4552 |
#endif |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4553 |
} else { |
18797 | 4554 |
error = @symbol(badHandleType); |
4555 |
goto out; |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4556 |
} |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4557 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4558 |
if (rslt < 0) { |
18797 | 4559 |
error = __mkSmallInteger(__threadErrno); |
4560 |
goto out; |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4561 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4562 |
RETURN (self); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4563 |
|
12674 | 4564 |
out:; |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4565 |
%}. |
12674 | 4566 |
|
4567 |
error notNil ifTrue:[ |
|
18797 | 4568 |
error == #errorNotOpen ifTrue:[ |
4569 |
self errorNotOpen. |
|
4570 |
]. |
|
4571 |
error isInteger ifTrue:[ |
|
4572 |
lastErrorNumber := error. |
|
4573 |
mode == #readonly ifTrue:[ |
|
4574 |
self ioError:error. |
|
4575 |
] ifFalse:[ |
|
4576 |
self writeError:error. |
|
4577 |
]. |
|
4578 |
^ self. |
|
4579 |
]. |
|
4580 |
self primitiveFailed:error. |
|
4581 |
^ self. |
|
12674 | 4582 |
]. |
4583 |
||
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4584 |
"/ fallback for rel5 |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4585 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4586 |
fp := handle. |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4587 |
fp notNil ifTrue:[ |
18797 | 4588 |
handle := nil. |
4589 |
self closeFile:fp |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4590 |
] |
18783 | 4591 |
! ! |
4592 |
||
4593 |
!ExternalStream methodsFor:'private'! |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4594 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4595 |
closeFile:handle |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4596 |
"for rel5 only" |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4597 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4598 |
self primitiveFailed |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4599 |
! |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4600 |
|
12706 | 4601 |
connectTo:aFileDescriptor withMode:modeSymbol handleType:handleTypeSymbol |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4602 |
"connect a fileDescriptor; openmode is the string defining the way to open. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4603 |
This can be used to connect an externally provided fileDescriptor (from |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4604 |
primitive code) or a pipeFileDescriptor (as returned by makePipe) to |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4605 |
a Stream object. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4606 |
The openMode ('r', 'w' etc.) must match the mode in which |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4607 |
the fileDescriptor was originally opened (otherwise i/o errors will be reported later)." |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4608 |
|
12706 | 4609 |
|retVal error openmode| |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4610 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4611 |
handle notNil ifTrue:[^ self errorAlreadyOpen]. |
12706 | 4612 |
mode := modeSymbol. |
4613 |
openmode := self fopenMode. |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4614 |
%{ |
12683 | 4615 |
FILEPOINTER f = 0; |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4616 |
OBJ fp; |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4617 |
FILE *fdopen(); |
12683 | 4618 |
int fd = -2; |
4619 |
||
4620 |
if (__isStringLike(openmode)) { |
|
12688 | 4621 |
#ifdef WIN32 |
18779 | 4622 |
__stxWrapApiEnterCritical(); |
12688 | 4623 |
#endif |
18779 | 4624 |
if (__isSmallInteger(aFileDescriptor)) { |
4625 |
fd = __intVal(aFileDescriptor); |
|
4626 |
} |
|
12683 | 4627 |
#ifdef WIN32 |
18779 | 4628 |
else if (__isExternalAddressLike(aFileDescriptor)) { |
4629 |
fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY); |
|
4630 |
if (fd < 0) { |
|
4631 |
if (__threadErrno == 0) { |
|
4632 |
// no more file descriptors |
|
4633 |
__threadErrno = EMFILE; |
|
4634 |
} |
|
4635 |
error = __mkSmallInteger(__threadErrno); |
|
4636 |
__stxWrapApiLeaveCritical(); |
|
4637 |
goto out; |
|
4638 |
} |
|
4639 |
} |
|
12683 | 4640 |
#endif |
18779 | 4641 |
f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode)); |
12688 | 4642 |
#ifdef WIN32 |
18779 | 4643 |
__stxWrapApiLeaveCritical(); |
12688 | 4644 |
#endif |
18779 | 4645 |
if (f == NULL) { |
4646 |
error =__mkSmallInteger(__threadErrno); |
|
4647 |
} else { |
|
4648 |
if (@global(FileOpenTrace) == true) { |
|
4649 |
fprintf(stderr, "fdopen [ExternalStream] %"_ld_" (%"_lx_") -> %"_lx_"\n", (INT)fd, (INT)fd, (INT)f); |
|
4650 |
} |
|
4651 |
||
4652 |
fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp); |
|
4653 |
retVal = self; |
|
4654 |
} |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4655 |
} |
12688 | 4656 |
out:; |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4657 |
%}. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4658 |
retVal notNil ifTrue:[ |
18779 | 4659 |
position := 0. |
4660 |
buffered isNil ifTrue:[ |
|
4661 |
buffered := true. "default is buffered" |
|
4662 |
]. |
|
4663 |
handleType := handleTypeSymbol. |
|
4664 |
self registerForFinalization. |
|
4665 |
^ retVal |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4666 |
]. |
12688 | 4667 |
error notNil ifTrue:[ |
18779 | 4668 |
OperatingSystem closeFd:aFileDescriptor. |
4669 |
lastErrorNumber := error. |
|
4670 |
position := nil. |
|
4671 |
" |
|
4672 |
the open failed for some reason ... |
|
4673 |
" |
|
4674 |
^ self openError:error |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4675 |
]. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4676 |
! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4677 |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4678 |
dupFd |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4679 |
"duplicate my file descriptor" |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4680 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4681 |
|fd dupFd| |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4682 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4683 |
fd := self fileHandle. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4684 |
dupFd := OperatingSystem dup:fd. |
10989 | 4685 |
self setFileHandle:dupFd mode:self fopenMode. |
10995 | 4686 |
! |
10989 | 4687 |
|
4688 |
fopenMode |
|
4689 |
"answer the mode for fopen. |
|
4690 |
Only used internally" |
|
4691 |
||
4692 |
mode == #readonly ifTrue:[ |
|
18797 | 4693 |
^ ReadMode |
10989 | 4694 |
]. |
4695 |
mode == #writeonly ifTrue:[ |
|
18797 | 4696 |
^ WriteMode |
10989 | 4697 |
]. |
4698 |
mode == #readWrite ifTrue:[ |
|
18797 | 4699 |
^ ReadWriteMode |
10989 | 4700 |
]. |
4701 |
mode == #append ifTrue:[ |
|
18797 | 4702 |
^ AppendMode |
10989 | 4703 |
]. |
4704 |
^ ReadWriteMode |
|
10995 | 4705 |
! |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4706 |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4707 |
open:aPath withMode:openModeString |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4708 |
"low level open; opens the file/device and sets the handle instance |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4709 |
variable. Careful: this does not care for any other state." |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4710 |
|
16296 | 4711 |
|ok error| |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4712 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4713 |
ok := false. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4714 |
%{ |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4715 |
FILE *f; |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4716 |
OBJ fp; |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4717 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4718 |
if (__isStringLike(aPath) && __isStringLike(openModeString)) { |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4719 |
#ifdef WIN32 |
10339 | 4720 |
# if 1 |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4721 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString)); |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4722 |
# else |
18779 | 4723 |
__BEGIN_INTERRUPTABLE__ |
4724 |
do { |
|
4725 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString)); |
|
4726 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
|
4727 |
__END_INTERRUPTABLE__ |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4728 |
# endif |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4729 |
#else /* UNIX */ |
18779 | 4730 |
__BEGIN_INTERRUPTABLE__ |
4731 |
do { |
|
4732 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString)); |
|
4733 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
|
4734 |
__END_INTERRUPTABLE__ |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4735 |
#endif /* UNIX */ |
18779 | 4736 |
if (f == NULL) { |
4737 |
error = __mkSmallInteger(__threadErrno); |
|
4738 |
} else { |
|
4739 |
if (@global(FileOpenTrace) == true) { |
|
4740 |
fprintf(stderr, "fopen %s [ExternalStream] -> %"_lx_"\n", __stringVal(aPath), (INT)f); |
|
4741 |
} |
|
4742 |
fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp); |
|
4743 |
__INST(handleType) = @symbol(filePointer); |
|
4744 |
ok = true; |
|
4745 |
} |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4746 |
} |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4747 |
%}. |
16296 | 4748 |
ok ifTrue:[ |
18779 | 4749 |
position := 0. |
4750 |
self registerForFinalization. |
|
4751 |
^ self. |
|
16296 | 4752 |
]. |
4753 |
error notNil ifTrue:[ |
|
18779 | 4754 |
" |
4755 |
the open failed for some reason ... |
|
4756 |
" |
|
4757 |
lastErrorNumber := error. |
|
4758 |
self openError:error. |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4759 |
]. |
16296 | 4760 |
self primitiveFailed. |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4761 |
! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4762 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4763 |
reOpen |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4764 |
"USERS WILL NEVER INVOKE THIS METHOD |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4765 |
sent after snapin to reopen streams. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4766 |
cannot reopen here since I am abstract and have no device knowledge" |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4767 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4768 |
self class name errorPrint. ' [warning]: automatic reOpen not supported - stream closed' errorPrintCR. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4769 |
handle := nil. |
18779 | 4770 |
self unregisterForFinalization. |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4771 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4772 |
"Modified: 10.1.1997 / 17:50:44 / cg" |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4773 |
! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4774 |
|
12398 | 4775 |
setAccessor:what to:something |
4776 |
"set the filePointer/fileHandle to the given one; |
|
4777 |
low level private & friend interface; may also be used to connect to some |
|
4778 |
externally provided handle." |
|
4779 |
||
4780 |
handle := something. |
|
4781 |
handleType := what. |
|
4782 |
! |
|
4783 |
||
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4784 |
setFileDescriptor:anInteger mode:openMode |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4785 |
"set the handle based upon a given fileDescriptor - |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4786 |
notice: this one is based on the underlying OSs fileDescriptor - |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4787 |
this may not be available on all platforms (i.e. non unix systems)." |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4788 |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4789 |
self setFileHandle:anInteger mode:openMode |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4790 |
! |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4791 |
|
12398 | 4792 |
setFileHandle:something |
4793 |
"set the fileHandle to the given one; |
|
4794 |
low level private & friend interface; may also be used to connect to some |
|
4795 |
externally provided file handle." |
|
4796 |
||
4797 |
self setAccessor:#fileHandle to:something |
|
4798 |
! |
|
4799 |
||
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4800 |
setFileHandle:anIntegerOrExternalAddress mode:openMode |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4801 |
"set the handle based upon a given fileHandle - |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4802 |
notice: this one is based on the underlying OSs fileDescriptor - |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4803 |
this is a fileDescriptor (Integer) on Unix and Windows, or an Handle (ExternalAddres) on Windows only. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4804 |
It may not be available on all platforms." |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4805 |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4806 |
%{ |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4807 |
FILEPOINTER f; |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4808 |
OBJ fp; |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4809 |
FILE *fdopen(); |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4810 |
int fd; |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4811 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4812 |
if (!__isStringLike(openMode)) |
18797 | 4813 |
goto err; |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4814 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4815 |
#ifdef WIN32 |
12688 | 4816 |
__stxWrapApiEnterCritical(); |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4817 |
if (__isExternalAddressLike(anIntegerOrExternalAddress) ) { |
18797 | 4818 |
HANDLE __fileHandle = (HANDLE)__externalAddressVal(anIntegerOrExternalAddress); |
4819 |
fd = _open_osfhandle((long)__fileHandle, O_BINARY); /* should we handle readonly, append or text mode? */ |
|
4820 |
if (fd < 0) { |
|
4821 |
__stxWrapApiLeaveCritical(); |
|
4822 |
CloseHandle(__fileHandle); |
|
4823 |
goto err; |
|
4824 |
} |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4825 |
} else |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4826 |
#endif |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4827 |
if (__isSmallInteger(anIntegerOrExternalAddress)) { |
18797 | 4828 |
fd = __smallIntegerVal(anIntegerOrExternalAddress); |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4829 |
} else { |
12688 | 4830 |
#ifdef WIN32 |
18797 | 4831 |
__stxWrapApiLeaveCritical(); |
12688 | 4832 |
#endif |
18797 | 4833 |
goto err; |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4834 |
} |
10997 | 4835 |
f = fdopen(fd, __stringVal(openMode)); |
12688 | 4836 |
#ifdef WIN32 |
4837 |
__stxWrapApiLeaveCritical(); |
|
4838 |
#endif |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4839 |
if (f != NULL) { |
18797 | 4840 |
if (@global(FileOpenTrace) == true) { |
4841 |
fprintf(stderr, "fdopen [ExternalStream] %d -> %"_lx_"\n", fd, (INT)f); |
|
4842 |
} |
|
4843 |
fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp); |
|
4844 |
__INST(handleType) = @symbol(filePointer); |
|
4845 |
RETURN (self); |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4846 |
} |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4847 |
err:; |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4848 |
%}. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4849 |
^ self primitiveFailed |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4850 |
! |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4851 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4852 |
setFilePointer:something |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4853 |
"set the filePointer to the given one; |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4854 |
low level private & friend interface; may also be used to connect to some |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4855 |
externally provided file." |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4856 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4857 |
self setAccessor:#filePointer to:something |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4858 |
! |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4859 |
|
12398 | 4860 |
setLastError:aNumber |
4861 |
lastErrorNumber := aNumber |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4862 |
! |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4863 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4864 |
setSocketHandle:something |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4865 |
"set the socketHandle to the given one; |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4866 |
low level private & friend interface; may also be used to connect to some |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4867 |
externally provided socket handle." |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4868 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4869 |
self setAccessor:#socketHandle to:something |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4870 |
! ! |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4871 |
|
612 | 4872 |
!ExternalStream methodsFor:'queries'! |
4873 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4874 |
nextError |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4875 |
"return the error by trying to read something. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4876 |
Should only be used, when we know, that a read operation |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4877 |
will return an error (otherwise a character may be lost). |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4878 |
Return an integer (error number), 0 (EOF) or nil (no error)" |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4879 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4880 |
%{ /*NOCONTEXT*/ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4881 |
FILEPOINTER f; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4882 |
int ret, _buffered; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4883 |
OBJ fp; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4884 |
unsigned char ch; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4885 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4886 |
if ((__INST(handleType) == nil) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4887 |
|| (__INST(handleType) == @symbol(filePointer)) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4888 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4889 |
|| (__INST(handleType) == @symbol(socketHandle)) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4890 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4891 |
if (((fp = __INST(handle)) != nil) |
4892 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4893 |
) { |
|
4894 |
f = __FILEVal(fp); |
|
4895 |
||
4896 |
_buffered = (__INST(buffered) == true); |
|
4897 |
if (_buffered) { |
|
4898 |
__READING__(f) |
|
4899 |
} |
|
4900 |
__READBYTE__(ret, f, &ch, _buffered, __INST(handleType)); |
|
4901 |
||
4902 |
if (ret > 0) { |
|
4903 |
RETURN(nil) |
|
4904 |
} |
|
4905 |
if (ret < 0) { |
|
4906 |
RETURN(__mkSmallInteger(__threadErrno)); |
|
4907 |
} else /* ret == 0 */ { |
|
4908 |
RETURN(__mkSmallInteger(0)); /* EOF */ |
|
4909 |
} |
|
4910 |
} |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4911 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4912 |
%}. |
612 | 4913 |
! |
4914 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4915 |
numAvailable |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4916 |
"return the number of bytes available for reading" |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4917 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4918 |
|fd| |
7804 | 4919 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4920 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4921 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4922 |
fd := self fileDescriptor. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4923 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4924 |
%{ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4925 |
#ifdef WIN32 |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4926 |
int res = 1; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4927 |
int success = 0; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4928 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4929 |
if (__INST(handleType) == @symbol(socketHandle)) { |
18797 | 4930 |
success = ioctlsocket((SOCKET)__externalAddressVal(fd), FIONREAD, &res) == 0; |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4931 |
} else if (__INST(handleType) == @symbol(socketFilePointer)) { |
18797 | 4932 |
success = ioctlsocket((SOCKET)_get_osfhandle(__intVal(fd)), FIONREAD, &res) == 0; |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4933 |
} else if (__INST(handleType) == @symbol(pipeFilePointer)) { |
18797 | 4934 |
success = PeekNamedPipe((HANDLE)_get_osfhandle(__intVal(fd)),0,0,0,&res,0); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4935 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4936 |
if (success) { |
18797 | 4937 |
if (__INST(readAhead) != nil) res++; |
4938 |
RETURN(__mkSmallInteger(res)); |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4939 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4940 |
#endif |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4941 |
%}. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4942 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4943 |
^ (readAhead notNil or:[OperatingSystem readCheck:fd]) ifTrue:[1] ifFalse:[0]. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4944 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4945 |
"Created: / 4.2.1998 / 16:56:01 / cg" |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4946 |
"Modified: / 4.2.1998 / 17:31:11 / cg" |
612 | 4947 |
! ! |
4948 |
||
4949 |
!ExternalStream methodsFor:'reading'! |
|
4950 |
||
4951 |
next |
|
4952 |
"return the next element; advance read position. |
|
4953 |
In binary mode, an integer is returned, otherwise a character. |
|
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
4954 |
If there are no more elements, either an exception is thrown or nil is returned |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
4955 |
- see #pastEndRead." |
1 | 4956 |
|
16296 | 4957 |
|c error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4958 |
%{ |
18364 | 4959 |
#ifdef __SCHTEAM__ |
4960 |
STObject handle = self.instVarAt(I_handle); |
|
4961 |
||
4962 |
if (handle != STObject.Nil) { |
|
18797 | 4963 |
STObject next; |
4964 |
||
4965 |
if (self.instVarAt(I_binary) == STObject.True) { |
|
4966 |
next = handle.nextByte(); |
|
4967 |
} else { |
|
4968 |
next = handle.nextChar(); |
|
4969 |
} |
|
4970 |
if (next != STObject.EOF) { |
|
4971 |
self.instVarAt_put(I_position, STObject.Nil); |
|
4972 |
return __c__._RETURN( next ); |
|
4973 |
} |
|
4974 |
self.instVarAt_put(I_hitEOF, STObject.True); |
|
18364 | 4975 |
} |
4976 |
#else |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4977 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4978 |
int ret, _buffered; |
612 | 4979 |
OBJ pos, fp; |
4980 |
unsigned char ch; |
|
4981 |
||
1133 | 4982 |
__INST(lastErrorNumber) = nil; |
12548 | 4983 |
if ((__INST(handleType) == nil) |
4984 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4985 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4986 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4987 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4988 |
if (((fp = __INST(handle)) != nil) |
4989 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4990 |
) { |
|
4991 |
f = __FILEVal(fp); |
|
4992 |
||
4993 |
_buffered = (__INST(buffered) == true); |
|
4994 |
if (_buffered) { |
|
4995 |
__READING__(f) |
|
4996 |
} |
|
4997 |
__READBYTE__(ret, f, &ch, _buffered, __INST(handleType)); |
|
4998 |
||
4999 |
if (ret > 0) { |
|
5000 |
pos = __INST(position); |
|
5001 |
if (__isSmallInteger(pos)) { |
|
5002 |
OBJ t; |
|
5003 |
||
5004 |
t = __MKINT(__intVal(pos) + 1); __INST(position) = t; __STORE(self, t); |
|
5005 |
} else { |
|
5006 |
__INST(position) = nil; /* i.e. do not know */ |
|
5007 |
} |
|
5008 |
if (__INST(binary) == true) { |
|
5009 |
RETURN ( __mkSmallInteger(ch) ); |
|
5010 |
} |
|
5011 |
RETURN ( __MKCHARACTER(ch) ); |
|
5012 |
} |
|
5013 |
||
5014 |
__INST(position) = nil; |
|
5015 |
if ((ret < 0) |
|
18364 | 5016 |
# ifdef ECONNRESET |
18797 | 5017 |
&& (__threadErrno != ECONNRESET) |
18364 | 5018 |
# endif |
18797 | 5019 |
){ |
5020 |
error = __mkSmallInteger(__threadErrno); |
|
5021 |
} else /* ret == 0 */ { |
|
5022 |
__INST(hitEOF) = true; |
|
5023 |
} |
|
5024 |
} |
|
612 | 5025 |
} |
18364 | 5026 |
#endif /* not SCHTEAM */ |
612 | 5027 |
%}. |
8614 | 5028 |
hitEOF == true ifTrue:[^ self pastEndRead]. |
16296 | 5029 |
error notNil ifTrue:[ |
18797 | 5030 |
lastErrorNumber := error. |
5031 |
^ self readError:error |
|
16296 | 5032 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5033 |
handle isNil ifTrue:[^ self errorNotOpen]. |
5420 | 5034 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
5035 |
||
5036 |
readAhead notNil ifTrue:[ |
|
18797 | 5037 |
c := readAhead. |
5038 |
readAhead := nil. |
|
5039 |
^ c. |
|
5420 | 5040 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5041 |
c := self nextByteFromFile:handle. |
5430 | 5042 |
c isNil ifTrue:[ |
18797 | 5043 |
^ self pastEndRead. |
5430 | 5044 |
]. |
5420 | 5045 |
binary == true ifTrue:[ |
18797 | 5046 |
^ c |
5420 | 5047 |
]. |
5048 |
^ Character value:c |
|
612 | 5049 |
! |
5050 |
||
5051 |
next:count |
|
5052 |
"return the next count elements of the stream as a collection. |
|
5053 |
Redefined to return a String or ByteArray instead of the default: Array." |
|
5054 |
||
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5055 |
|coll nRead| |
612 | 5056 |
|
5057 |
binary ifTrue:[ |
|
18797 | 5058 |
coll := ByteArray uninitializedNew:count |
612 | 5059 |
] ifFalse:[ |
18797 | 5060 |
coll := String new:count |
612 | 5061 |
]. |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5062 |
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
|
5063 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5064 |
"/ for readStream protocol compatibility, |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5065 |
"/ we must raise an exception here. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5066 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5067 |
nRead < count ifTrue:[ |
18797 | 5068 |
^ self pastEndRead |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5069 |
]. |
612 | 5070 |
^ coll |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5071 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5072 |
"Modified: 11.1.1997 / 17:44:17 / cg" |
612 | 5073 |
! |
5074 |
||
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5075 |
nextOrNil |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5076 |
"return the next element; advance read position. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5077 |
In binary mode, an integer is returned, otherwise a character. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5078 |
If there are no more elements, nil is returned. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5079 |
This is #atEnd and #next in a single operation - to speed up some code" |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5080 |
|
16296 | 5081 |
|c error| |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5082 |
%{ |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5083 |
FILEPOINTER f; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5084 |
int ret, _buffered; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5085 |
OBJ pos, fp; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5086 |
unsigned char ch; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5087 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5088 |
__INST(lastErrorNumber) = nil; |
12548 | 5089 |
if ((__INST(handleType) == nil) |
5090 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5091 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5092 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5093 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5094 |
if (((fp = __INST(handle)) != nil) |
5095 |
&& (__INST(mode) != @symbol(writeonly)) |
|
5096 |
) { |
|
5097 |
f = __FILEVal(fp); |
|
5098 |
||
5099 |
_buffered = (__INST(buffered) == true); |
|
5100 |
if (_buffered) { |
|
5101 |
__READING__(f) |
|
5102 |
} |
|
5103 |
__READBYTE__(ret, f, &ch, _buffered, __INST(handleType)); |
|
5104 |
||
5105 |
if (ret > 0) { |
|
5106 |
pos = __INST(position); |
|
5107 |
if (__isSmallInteger(pos)) { |
|
5108 |
OBJ t; |
|
5109 |
||
5110 |
t = __MKINT(__intVal(pos) + 1); __INST(position) = t; __STORE(self, t); |
|
5111 |
} else { |
|
5112 |
__INST(position) = nil; /* i.e. do not know */ |
|
5113 |
} |
|
5114 |
if (__INST(binary) == true) { |
|
5115 |
RETURN ( __mkSmallInteger(ch) ); |
|
5116 |
} |
|
5117 |
RETURN ( __MKCHARACTER(ch) ); |
|
5118 |
} |
|
5119 |
||
5120 |
__INST(position) = nil; |
|
5121 |
if ((ret < 0) |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5122 |
#ifdef ECONNRESET |
18797 | 5123 |
&& (__threadErrno != ECONNRESET) |
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5124 |
#endif |
18797 | 5125 |
){ |
5126 |
error = __mkSmallInteger(__threadErrno); |
|
5127 |
} else /* ret == 0 */ { |
|
5128 |
__INST(hitEOF) = true; |
|
5129 |
} |
|
5130 |
} |
|
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5131 |
} |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5132 |
%}. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5133 |
hitEOF == true ifTrue:[^ nil]. |
16296 | 5134 |
error notNil ifTrue:[ |
18797 | 5135 |
lastErrorNumber := error. |
5136 |
^ self readError:error. |
|
16296 | 5137 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5138 |
handle isNil ifTrue:[^ self errorNotOpen]. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5139 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5140 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5141 |
readAhead notNil ifTrue:[ |
18797 | 5142 |
c := readAhead. |
5143 |
readAhead := nil. |
|
5144 |
^ c. |
|
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5145 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5146 |
c := self nextByteFromFile:handle. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5147 |
c isNil ifTrue:[ |
18797 | 5148 |
^ nil. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5149 |
]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5150 |
binary == true ifTrue:[ |
18797 | 5151 |
^ c |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5152 |
]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5153 |
^ Character value:c |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5154 |
! |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5155 |
|
612 | 5156 |
peek |
5157 |
"return the element to be read next without advancing read position. |
|
5158 |
In binary mode, an integer is returned, otherwise a character. |
|
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5159 |
If there are no more elements, either an exception is thrown or nil is returned |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5160 |
- see #pastEndRead." |
612 | 5161 |
|
16296 | 5162 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5163 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
5164 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5165 |
unsigned char c; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5166 |
int ret, _buffered; |
216 | 5167 |
OBJ fp; |
3672
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
5168 |
OBJ ra; |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
5169 |
|
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
5170 |
if ((ra = __INST(readAhead)) != nil) { |
18797 | 5171 |
if (__INST(binary) == true) { |
5172 |
RETURN ( ra ); |
|
5173 |
} |
|
5174 |
c = __intVal(ra); |
|
5175 |
RETURN ( __MKCHARACTER(c) ); |
|
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
5176 |
} |
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
5177 |
|
1133 | 5178 |
__INST(lastErrorNumber) = nil; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5179 |
|
12548 | 5180 |
if ((__INST(handleType) == nil) |
5181 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5182 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5183 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5184 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5185 |
if (((fp = __INST(handle)) != nil) |
5186 |
&& (__INST(mode) != @symbol(writeonly)) |
|
5187 |
) { |
|
5188 |
f = __FILEVal(fp); |
|
5189 |
_buffered = (__INST(buffered) == true); |
|
5190 |
if (_buffered) { |
|
5191 |
__READING__(f) |
|
5192 |
} |
|
5193 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5194 |
||
5195 |
if (ret > 0) { |
|
5196 |
__UNGETC__(c, f, _buffered); |
|
5197 |
||
5198 |
if (__INST(binary) == true) { |
|
5199 |
RETURN ( __mkSmallInteger(c) ); |
|
5200 |
} |
|
5201 |
RETURN ( __MKCHARACTER(c) ); |
|
5202 |
} |
|
5203 |
if ((ret < 0) |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5204 |
#ifdef ECONNRESET |
18797 | 5205 |
&& (__threadErrno != ECONNRESET) |
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5206 |
#endif |
18797 | 5207 |
){ |
5208 |
error = __mkSmallInteger(__threadErrno); |
|
5209 |
} else /* ret == 0 */ { |
|
5210 |
__INST(hitEOF) = true; |
|
5211 |
} |
|
5212 |
} |
|
1 | 5213 |
} |
159 | 5214 |
%}. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5215 |
hitEOF == true ifTrue:[^ self pastEndRead]. |
16296 | 5216 |
error notNil ifTrue:[ |
18797 | 5217 |
lastErrorNumber := error. |
5218 |
^ self readError:error. |
|
16296 | 5219 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5220 |
handle isNil ifTrue:[^ self errorNotOpen]. |
5420 | 5221 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
5222 |
||
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5223 |
readAhead isNil ifTrue:[ |
18797 | 5224 |
readAhead := self nextOrNil. |
5225 |
readAhead isNil ifTrue:[ |
|
5226 |
^ self pastEndRead. |
|
5227 |
]. |
|
5420 | 5228 |
]. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5229 |
^ readAhead |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5230 |
! |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5231 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5232 |
peekOrNil |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5233 |
"return the element to be read next without advancing read position. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5234 |
In binary mode, an integer is returned, otherwise a character. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5235 |
If there are no more elements, nil is returned. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5236 |
Same as #atEnd and #peek in a single operation - speeding up some code" |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5237 |
|
16296 | 5238 |
|error| |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5239 |
%{ |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5240 |
FILEPOINTER f; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5241 |
unsigned char c; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5242 |
int ret, _buffered; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5243 |
OBJ fp; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5244 |
OBJ ra; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5245 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5246 |
if ((ra = __INST(readAhead)) != nil) { |
18797 | 5247 |
if (__INST(binary) == true) { |
5248 |
RETURN ( ra ); |
|
5249 |
} |
|
5250 |
c = __intVal(ra); |
|
5251 |
RETURN ( __MKCHARACTER(c) ); |
|
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5252 |
} |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5253 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5254 |
__INST(lastErrorNumber) = nil; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5255 |
|
12548 | 5256 |
if ((__INST(handleType) == nil) |
5257 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5258 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5259 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5260 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5261 |
if (((fp = __INST(handle)) != nil) |
5262 |
&& (__INST(mode) != @symbol(writeonly)) |
|
5263 |
) { |
|
5264 |
f = __FILEVal(fp); |
|
5265 |
_buffered = (__INST(buffered) == true); |
|
5266 |
if (_buffered) { |
|
5267 |
__READING__(f) |
|
5268 |
} |
|
5269 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5270 |
||
5271 |
if (ret > 0) { |
|
5272 |
__UNGETC__(c, f, _buffered); |
|
5273 |
||
5274 |
if (__INST(binary) == true) { |
|
5275 |
RETURN ( __mkSmallInteger(c) ); |
|
5276 |
} |
|
5277 |
RETURN ( __MKCHARACTER(c) ); |
|
5278 |
} |
|
5279 |
if ((ret < 0) |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5280 |
#ifdef ECONNRESET |
18797 | 5281 |
&& (__threadErrno != ECONNRESET) |
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5282 |
#endif |
18797 | 5283 |
){ |
5284 |
error = __mkSmallInteger(__threadErrno); |
|
5285 |
} else /* ret == 0 */ { |
|
5286 |
__INST(hitEOF) = true; |
|
5287 |
} |
|
5288 |
} |
|
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5289 |
} |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5290 |
%}. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5291 |
hitEOF == true ifTrue:[^ nil]. |
16296 | 5292 |
error notNil ifTrue:[ |
18797 | 5293 |
lastErrorNumber := error. |
5294 |
^ self readError:error. |
|
16296 | 5295 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5296 |
handle isNil ifTrue:[^ self errorNotOpen]. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5297 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5298 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5299 |
readAhead isNil ifTrue:[ |
18797 | 5300 |
readAhead := self nextOrNil. |
5420 | 5301 |
]. |
5302 |
^ readAhead |
|
1295 | 5303 |
! |
5304 |
||
5305 |
upToEnd |
|
5306 |
"return a collection of the elements up-to the end. |
|
5307 |
Return nil if the stream-end is reached before." |
|
5308 |
||
6569 | 5309 |
|answerStream buffer count| |
5310 |
||
5311 |
buffer := self contentsSpecies new:4096. |
|
1295 | 5312 |
[self atEnd] whileFalse:[ |
18797 | 5313 |
count := self nextAvailableBytes:4096 into:buffer startingAt:1. |
5314 |
count ~~ 0 ifTrue:[ |
|
5315 |
answerStream isNil ifTrue:[ |
|
5316 |
answerStream := WriteStream with:buffer. |
|
5317 |
answerStream position:count. |
|
5318 |
buffer := self contentsSpecies new:4096. |
|
5319 |
] ifFalse:[ |
|
5320 |
answerStream nextPutAll:buffer startingAt:1 to:count. |
|
5321 |
]. |
|
5322 |
]. |
|
1295 | 5323 |
]. |
6758
bc933037a1dc
oops - upToEnd failed if already at the end
Claus Gittinger <cg@exept.de>
parents:
6661
diff
changeset
|
5324 |
answerStream isNil ifTrue:[^ self contentsSpecies new]. |
1295 | 5325 |
^ answerStream contents |
5326 |
||
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5327 |
" |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5328 |
('smalltalk.rc' asFilename readStream upToEnd) |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5329 |
= |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5330 |
('smalltalk.rc' asFilename readStream contentsOfEntireFile) |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5331 |
" |
612 | 5332 |
! ! |
5333 |
||
5420 | 5334 |
!ExternalStream methodsFor:'rel5 protocol'! |
5335 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5336 |
atEndFile:handle |
5420 | 5337 |
"for migration to rel5 only" |
5338 |
||
5339 |
self primitiveFailed |
|
5340 |
! |
|
5341 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5342 |
nextByteFromFile:handle |
5420 | 5343 |
"for migration to rel5 only" |
5344 |
||
5345 |
self primitiveFailed |
|
5346 |
! |
|
5347 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5348 |
nextPutByte:aByte toFile:handle |
5420 | 5349 |
"for migration to rel5 only" |
5350 |
||
5351 |
self primitiveFailed |
|
5352 |
! ! |
|
5353 |
||
612 | 5354 |
!ExternalStream methodsFor:'testing'! |
5355 |
||
5356 |
atEnd |
|
5357 |
"return true, if position is at end" |
|
5358 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5359 |
%{ |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
5360 |
OBJ fp, pos, lim; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5361 |
char c; |
612 | 5362 |
|
1133 | 5363 |
if (__INST(hitEOF) == true) { |
18797 | 5364 |
RETURN (true); |
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
5365 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
7017
diff
changeset
|
5366 |
pos = __INST(position); |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
5367 |
lim = __INST(readLimit); |
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
5368 |
if (lim != nil) { |
18797 | 5369 |
off_t _pos, _readLimit; |
5370 |
||
5371 |
_pos = __signedLongIntVal(pos); |
|
5372 |
_pos = _pos - __intVal( @global(PositionableStream:ZeroPosition)) + 1; |
|
5373 |
_readLimit = __signedLongIntVal(lim); |
|
5374 |
if (_pos > _readLimit) { |
|
5375 |
RETURN (true); |
|
5376 |
} |
|
612 | 5377 |
} |
5378 |
||
1133 | 5379 |
__INST(lastErrorNumber) = nil; |
5380 |
||
12548 | 5381 |
if ((__INST(handleType) == nil) |
5382 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5383 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5384 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5385 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5386 |
if ((fp = __INST(handle)) != nil) { |
5387 |
int _buffered = (__INST(buffered) == true); |
|
5388 |
int ret; |
|
5389 |
FILEPOINTER f = __FILEVal(fp); |
|
5390 |
||
5391 |
if (_buffered) { |
|
5392 |
__READING__(f); |
|
5393 |
} else { |
|
5394 |
if (__INST(readAhead) != nil) { |
|
5395 |
RETURN (false); |
|
5396 |
} |
|
5397 |
} |
|
5398 |
||
5399 |
/* |
|
5400 |
* read ahead ... |
|
5401 |
*/ |
|
5402 |
do { |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5403 |
#ifdef WIN32 |
10339 | 5404 |
# if 1 |
18797 | 5405 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5406 |
|
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5407 |
# else |
18797 | 5408 |
__BEGIN_INTERRUPTABLE__ |
5409 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5410 |
__END_INTERRUPTABLE__ |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5411 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5412 |
#else /* not WIN32 */ |
18797 | 5413 |
__BEGIN_INTERRUPTABLE__ |
5414 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5415 |
__END_INTERRUPTABLE__ |
|
4914 | 5416 |
#endif |
18797 | 5417 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
5418 |
||
5419 |
if (ret > 0) { |
|
5420 |
__UNGETC__(c&0xff, f, _buffered); |
|
5421 |
RETURN (false); |
|
5422 |
} |
|
5423 |
if (ret == 0) { |
|
5424 |
__INST(hitEOF) = true; |
|
5425 |
RETURN (true); |
|
5426 |
} |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5427 |
#ifdef ECONNRESET |
18797 | 5428 |
// connection reset by peer is also an EOF |
5429 |
if (__threadErrno == ECONNRESET) { |
|
5430 |
__INST(hitEOF) = true; |
|
5431 |
RETURN (true); |
|
5432 |
} |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5433 |
#endif |
18797 | 5434 |
/* ret < 0 */ |
5435 |
__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno); |
|
5436 |
} |
|
612 | 5437 |
} |
5438 |
%}. |
|
5439 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5440 |
handle isNil ifTrue:[^ self errorNotOpen]. |
5420 | 5441 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
5442 |
readAhead notNil ifTrue:[^ false]. |
|
5443 |
||
5444 |
"/ migration support |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5445 |
^ self |
18797 | 5446 |
atEndFile:handle |
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
5447 |
|
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
5448 |
"Modified: / 30.10.1998 / 20:16:06 / cg" |
612 | 5449 |
! |
5450 |
||
5451 |
canReadWithoutBlocking |
|
5452 |
"return true, if any data is available for reading (i.e. |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5453 |
a read operation will not block the smalltalk process), false otherwise. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5454 |
We know, that error conditions do not block, so return true for errors." |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5455 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5456 |
^ readAhead notNil |
18797 | 5457 |
or:[handle isNil] |
5458 |
or:[mode == #writeonly |
|
5459 |
or:[OperatingSystem readCheck:self fileDescriptor]] |
|
612 | 5460 |
|
5461 |
" |
|
5462 |
|pipe| |
|
5463 |
||
5464 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
5465 |
pipe canReadWithoutBlocking ifTrue:[ |
|
18797 | 5466 |
Transcript showCR:'data available' |
612 | 5467 |
] ifFalse:[ |
18797 | 5468 |
Transcript showCR:'no data available' |
612 | 5469 |
]. |
5470 |
pipe close |
|
5471 |
" |
|
2986
c6957cf6128f
Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents:
2963
diff
changeset
|
5472 |
|
c6957cf6128f
Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents:
2963
diff
changeset
|
5473 |
"Modified: 25.9.1997 / 13:08:45 / stefan" |
612 | 5474 |
! |
5475 |
||
5476 |
canWriteWithoutBlocking |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5477 |
"return true, if data can be written into the stream |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5478 |
(i.e. a write operation will not block the smalltalk process). |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5479 |
We know, that error conditions do not block, so return true for errors." |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5480 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5481 |
^ handle isNil |
18797 | 5482 |
or:[mode == #readonly |
5483 |
or:[OperatingSystem writeCheck:self fileDescriptor]] |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
5484 |
! |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
5485 |
|
10995 | 5486 |
gotErrorOrEOF |
5487 |
"answerv true, if amn error or eof has been occured on the stream" |
|
5488 |
||
5489 |
^ hitEOF == true or:[lastErrorNumber notNil] |
|
5490 |
! |
|
5491 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5492 |
isBinary |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5493 |
"return true, if the stream is in binary (as opposed to text-) mode. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5494 |
The default when created is false." |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5495 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5496 |
^ binary |
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
5497 |
! |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
5498 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5499 |
isBlocking |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5500 |
"return true, if O_NONBLOCK is NOT set in the fileDescriptor (probably UNIX specific)" |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
5501 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5502 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5503 |
^ OperatingSystem isBlockingOn:self fileDescriptor |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5504 |
! |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5505 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5506 |
isExternalStream |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5507 |
"return true, if the receiver is some kind of externalStream; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5508 |
true is returned here - the method redefined from Object." |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5509 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5510 |
^ true |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5511 |
! |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5512 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5513 |
isOpen |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5514 |
"return true, if this stream is open" |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5515 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5516 |
^ handle notNil |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5517 |
! |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5518 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5519 |
isReadable |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5520 |
"return true, if this stream can be read from" |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5521 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5522 |
^ (mode ~~ #writeonly) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5523 |
! |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5524 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5525 |
isWritable |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5526 |
"return true, if this stream can be written to" |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5527 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5528 |
^ (mode ~~ #readonly) |
612 | 5529 |
! ! |
5530 |
||
5531 |
!ExternalStream methodsFor:'waiting for I/O'! |
|
5532 |
||
5101 | 5533 |
canBeSelected |
5534 |
"return true, if this stream can be selected upon" |
|
5535 |
||
5536 |
^ OperatingSystem supportsSelect |
|
5537 |
! |
|
5538 |
||
18327 | 5539 |
readWaitWithTimeoutMs:timeoutOrNil |
612 | 5540 |
"suspend the current process, until the receiver |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5541 |
becomes ready for reading or a timeout (in milliseconds) expired. |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5542 |
If data is already available, return immediate. |
612 | 5543 |
Return true if a timeout occured (i.e. false, if data is available). |
5544 |
The other threads are not affected by the wait." |
|
5545 |
||
16480 | 5546 |
|fd inputSema hasTimedout wasBlocked| |
612 | 5547 |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5548 |
readAhead notNil ifTrue:[^ false]. |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5549 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5550 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 5551 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
5552 |
||
5553 |
fd := self fileDescriptor. |
|
5554 |
(OperatingSystem readCheck:fd) ifTrue:[^ false]. |
|
5555 |
||
5556 |
wasBlocked := OperatingSystem blockInterrupts. |
|
16463 | 5557 |
inputSema := Semaphore new name:'readWait'. |
5558 |
[ |
|
18797 | 5559 |
timeoutOrNil notNil ifTrue:[ |
5560 |
Processor signal:inputSema afterMilliseconds:timeoutOrNil. |
|
5561 |
]. |
|
5562 |
Processor signal:inputSema onInput:fd. |
|
5563 |
Processor activeProcess state:#ioWait. |
|
5564 |
inputSema wait. |
|
5565 |
hasTimedout := timeoutOrNil notNil and:[(OperatingSystem readCheck:fd) not]. |
|
16463 | 5566 |
] ifCurtailed:[ |
18797 | 5567 |
Processor disableSemaphore:inputSema. |
5568 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
612 | 5569 |
]. |
18327 | 5570 |
timeoutOrNil notNil ifTrue:[ |
18797 | 5571 |
Processor disableSemaphore:inputSema. |
16480 | 5572 |
]. |
612 | 5573 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
16480 | 5574 |
^ hasTimedout |
612 | 5575 |
! |
5576 |
||
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5577 |
readWriteWaitWithTimeoutMs:timeout |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5578 |
"suspend the current process, until the receiver |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5579 |
becomes ready for reading or writing or a timeout (in milliseconds) expired. |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5580 |
Return true if a timeout occured (i.e. false, if data is available). |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5581 |
Return immediate if the receiver is already ready. |
5506 | 5582 |
The other threads are not affected by the wait." |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5583 |
|
16480 | 5584 |
|fd sema hasTimedout wasBlocked| |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5585 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5586 |
handle isNil ifTrue:[ |
18797 | 5587 |
^ self errorNotOpen |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5588 |
]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5589 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5590 |
fd := self fileDescriptor. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5591 |
(OperatingSystem readWriteCheck:fd) ifTrue:[^ false]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5592 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5593 |
wasBlocked := OperatingSystem blockInterrupts. |
16463 | 5594 |
sema := Semaphore new name:'readWriteWait'. |
5595 |
[ |
|
18797 | 5596 |
timeout notNil ifTrue:[ |
5597 |
Processor signal:sema afterMilliseconds:timeout |
|
5598 |
]. |
|
5599 |
Processor signal:sema onOutput:fd. |
|
5600 |
Processor signal:sema onInput:fd. |
|
5601 |
Processor activeProcess state:#ioWait. |
|
5602 |
sema wait. |
|
5603 |
hasTimedout := timeout notNil and:[(OperatingSystem readWriteCheck:fd) not]. |
|
16463 | 5604 |
] ifCurtailed:[ |
18797 | 5605 |
Processor disableSemaphore:sema. |
5606 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5607 |
]. |
16480 | 5608 |
timeout notNil ifTrue:[ |
18797 | 5609 |
Processor disableSemaphore:sema. |
16480 | 5610 |
]. |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5611 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
16480 | 5612 |
^ hasTimedout |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5613 |
! |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5614 |
|
612 | 5615 |
writeWaitWithTimeoutMs:timeout |
5616 |
"suspend the current process, until the receiver |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5617 |
becomes ready for writing or a timeout (in milliseconds) expired. |
612 | 5618 |
Return true if a timeout occured (i.e. false, if data is available). |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5619 |
Return immediate if the receiver is already ready. |
612 | 5620 |
The other threads are not affected by the wait." |
5621 |
||
16480 | 5622 |
|fd outputSema hasTimedout wasBlocked| |
612 | 5623 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5624 |
handle isNil ifTrue:[ |
18797 | 5625 |
^ self errorNotOpen |
612 | 5626 |
]. |
5627 |
mode == #readonly ifTrue:[ |
|
18797 | 5628 |
^ self errorReadOnly |
612 | 5629 |
]. |
5630 |
||
5631 |
fd := self fileDescriptor. |
|
5632 |
(OperatingSystem writeCheck:fd) ifTrue:[^ false]. |
|
5633 |
||
5634 |
wasBlocked := OperatingSystem blockInterrupts. |
|
16463 | 5635 |
outputSema := Semaphore new name:'writeWait'. |
5636 |
[ |
|
18797 | 5637 |
timeout notNil ifTrue:[ |
5638 |
Processor signal:outputSema afterMilliseconds:timeout |
|
5639 |
]. |
|
5640 |
Processor signal:outputSema onOutput:fd. |
|
5641 |
Processor activeProcess state:#ioWait. |
|
5642 |
outputSema wait. |
|
5643 |
hasTimedout := timeout notNil and:[(OperatingSystem writeCheck:fd) not]. |
|
16463 | 5644 |
] ifCurtailed:[ |
18797 | 5645 |
Processor disableSemaphore:outputSema. |
5646 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
612 | 5647 |
]. |
16480 | 5648 |
timeout notNil ifTrue:[ |
18797 | 5649 |
Processor disableSemaphore:outputSema. |
16480 | 5650 |
]. |
612 | 5651 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
16480 | 5652 |
^ hasTimedout |
612 | 5653 |
! ! |
5654 |
||
5655 |
!ExternalStream methodsFor:'writing'! |
|
5656 |
||
5657 |
cr |
|
5658 |
"append an end-of-line character (or CRLF if in crlf mode). |
|
5659 |
reimplemented for speed" |
|
5660 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5661 |
%{ |
12548 | 5662 |
if ((__INST(handleType) == nil) |
5663 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5664 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5665 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
5666 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5667 |
OBJ fp; |
5668 |
||
5669 |
__INST(lastErrorNumber) = nil; |
|
5670 |
if (((fp = __INST(handle)) != nil) |
|
5671 |
&& (__INST(mode) != @symbol(readonly)) |
|
5672 |
&& (__INST(binary) != true) |
|
5673 |
) { |
|
5674 |
FILEPOINTER f = __FILEVal(fp); |
|
5675 |
int _buffered = (__INST(buffered) == true); |
|
5676 |
int len, cnt; |
|
5677 |
char *cp; |
|
5678 |
||
5679 |
if (_buffered) { |
|
5680 |
__WRITING__(f) |
|
5681 |
} |
|
5682 |
{ |
|
5683 |
OBJ mode = __INST(eolMode); |
|
5684 |
||
5685 |
if (mode == @symbol(cr)) { |
|
5686 |
cp = "\r"; len = 1; |
|
5687 |
} else if (mode == @symbol(crlf)) { |
|
5688 |
cp = "\r\n"; len = 2; |
|
5689 |
} else if (mode == @symbol(eot)) { |
|
5690 |
cp = "\004"; len = 1; |
|
5691 |
} else if (mode == @symbol(etx)) { |
|
5692 |
cp = "\003"; len = 1; |
|
5693 |
} else { |
|
5694 |
cp = "\n"; len = 1; |
|
5695 |
} |
|
5696 |
} |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5697 |
#ifdef WIN32 |
18797 | 5698 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
5699 |
cnt = __win32_fwrite(cp, 1, len, f); |
|
5700 |
} else |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5701 |
#endif |
18797 | 5702 |
{ |
5703 |
__WRITEBYTES__(cnt, f, cp, len, _buffered, __INST(handleType)); |
|
5704 |
} |
|
5705 |
if (cnt == len) { |
|
5706 |
if (__isSmallInteger(__INST(position))) { |
|
5707 |
INT np = __intVal(__INST(position)) + len; |
|
5708 |
OBJ t; |
|
5709 |
||
5710 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
5711 |
} else { |
|
5712 |
__INST(position) = nil; /* i.e: dont know */ |
|
5713 |
} |
|
5714 |
RETURN ( self ); |
|
5715 |
} |
|
5716 |
__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno); |
|
5717 |
} |
|
612 | 5718 |
} |
5719 |
%}. |
|
2326 | 5720 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5721 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
2326 | 5722 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
5414 | 5723 |
(binary==true) ifTrue:[self errorBinary. ^ self]. |
5724 |
||
17426
6e49243ce4e0
fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents:
17420
diff
changeset
|
5725 |
(eolMode == #cr) ifTrue:[ |
18797 | 5726 |
self nextPut:(Character return). |
5727 |
^ self |
|
17426
6e49243ce4e0
fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents:
17420
diff
changeset
|
5728 |
]. |
6e49243ce4e0
fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents:
17420
diff
changeset
|
5729 |
(eolMode == #crlf) ifTrue:[ |
18797 | 5730 |
self nextPut:(Character return). |
17426
6e49243ce4e0
fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents:
17420
diff
changeset
|
5731 |
]. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5732 |
self nextPut:(Character nl). |
5414 | 5733 |
^ self |
612 | 5734 |
! |
5735 |
||
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5736 |
flush |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5737 |
"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
|
5738 |
|
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5739 |
%{ |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5740 |
__INST(lastErrorNumber) = nil; |
12548 | 5741 |
if ((__INST(handleType) == nil) |
5742 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5743 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
5744 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5745 |
OBJ fp; |
5746 |
int _buffered = (__INST(buffered) == true); |
|
5747 |
||
5748 |
if ((fp = __INST(handle)) != nil) { |
|
5749 |
if (__INST(mode) != @symbol(readonly)) { |
|
5750 |
if (_buffered) { |
|
5751 |
FILEPOINTER f = __FILEVal(fp); |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5752 |
#ifdef WIN32 |
18797 | 5753 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
5754 |
__win32_fflush(f); |
|
5755 |
} else { |
|
5756 |
int rslt; |
|
5757 |
||
5758 |
do { |
|
5759 |
__threadErrno = 0; |
|
5760 |
rslt = __STX_C_CALL1("fflush", fflush, f); |
|
5761 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
|
5762 |
} |
|
12678 | 5763 |
#else /* ! WIN32 */ |
18797 | 5764 |
__BEGIN_INTERRUPTABLE__ |
5765 |
FFLUSH(f); |
|
5766 |
__END_INTERRUPTABLE__ |
|
12678 | 5767 |
#endif /* ! WIN32 */ |
18797 | 5768 |
} |
5769 |
} |
|
5770 |
} |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5771 |
} |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5772 |
%} |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5773 |
! |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5774 |
|
612 | 5775 |
nextPut:aCharacter |
5776 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
5777 |
Only single-byte characters are currently supported" |
|
5778 |
||
16296 | 5779 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5780 |
%{ |
18364 | 5781 |
#ifdef __SCHTEAM__ |
5782 |
STObject handle = self.instVarAt(I_handle); |
|
5783 |
||
5784 |
if ((handle != STObject.Nil) |
|
5785 |
&& (aCharacter.isSTCharacter())) { |
|
18797 | 5786 |
handle.writeChar( aCharacter ); |
5787 |
self.instVarAt_put(I_position, STObject.Nil); |
|
5788 |
return __c__._RETURN_self(); |
|
18364 | 5789 |
} |
5790 |
#else |
|
17332 | 5791 |
__INST(lastErrorNumber) = nil; |
5792 |
if ((__INST(handleType) == nil) |
|
5793 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5794 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
5795 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
5796 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5797 |
OBJ fp; |
5798 |
||
5799 |
if (((fp = __INST(handle)) != nil) |
|
5800 |
&& (__INST(mode) != @symbol(readonly)) |
|
5801 |
) { |
|
5802 |
FILEPOINTER f = __FILEVal(fp); |
|
5803 |
int _buffered = (__INST(buffered) == true); |
|
5804 |
unsigned codePoint; |
|
5805 |
unsigned char c; |
|
5806 |
int cnt; |
|
5807 |
char buff[2]; |
|
5808 |
int nBytes = 1; |
|
5809 |
||
5810 |
if (__INST(binary) != true) { |
|
5811 |
if (__isCharacter(aCharacter)) { |
|
5812 |
||
5813 |
codePoint = __intVal(__characterVal(aCharacter)); |
|
5814 |
if (codePoint <= 0xFF) { |
|
5815 |
c = codePoint; |
|
5816 |
buff[0] = c; nBytes = 1; |
|
5817 |
||
5818 |
if (c == '\n') { |
|
5819 |
OBJ mode = __INST(eolMode); |
|
5820 |
if (mode == @symbol(nl)) { |
|
5821 |
// no EOL translation |
|
5822 |
} else if (mode == nil) { |
|
5823 |
// no EOL translation |
|
5824 |
} else if (mode == @symbol(cr)) { |
|
5825 |
buff[0] = '\r'; |
|
5826 |
} else if (mode == @symbol(eot)) { |
|
5827 |
buff[0] = '\004'; |
|
5828 |
} else if (mode == @symbol(etx)) { |
|
5829 |
buff[0] = '\003'; |
|
5830 |
} else if (mode == @symbol(crlf)) { |
|
5831 |
buff[0] = '\r'; |
|
5832 |
buff[1] = '\n'; |
|
5833 |
nBytes = 2; |
|
5834 |
} |
|
5835 |
} |
|
17332 | 5836 |
doWrite: |
18797 | 5837 |
if (! f) { |
5838 |
fprintf(stderr, "oops - fileHandle is NULL in nextPut:\n"); |
|
5839 |
__INST(handle) = nil; |
|
5840 |
goto out; |
|
5841 |
} |
|
5842 |
||
5843 |
if (_buffered) { |
|
5844 |
__WRITING__(f) |
|
5845 |
} |
|
18364 | 5846 |
# ifdef WIN32 |
18797 | 5847 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
5848 |
cnt = __win32_fwrite(buff, 1, nBytes, f); |
|
5849 |
} else |
|
18364 | 5850 |
# endif |
18797 | 5851 |
{ |
5852 |
__WRITEBYTES__(cnt, f, buff, nBytes, _buffered, __INST(handleType)); |
|
5853 |
} |
|
5854 |
if (cnt == nBytes) { |
|
5855 |
if (__isSmallInteger(__INST(position))) { |
|
5856 |
INT np = __intVal(__INST(position)) + nBytes; |
|
5857 |
OBJ t; |
|
5858 |
||
5859 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
5860 |
} else { |
|
5861 |
__INST(position) = nil; /* i.e. do not know */ |
|
5862 |
} |
|
5863 |
RETURN ( self ); |
|
5864 |
} |
|
5865 |
error = __mkSmallInteger(__threadErrno); |
|
5866 |
} |
|
5867 |
} |
|
5868 |
} else { |
|
5869 |
if (__isSmallInteger(aCharacter)) { |
|
5870 |
c = __intVal(aCharacter); |
|
5871 |
buff[0] = c; nBytes = 1; |
|
5872 |
goto doWrite; |
|
5873 |
} |
|
5874 |
} |
|
5875 |
} |
|
17332 | 5876 |
} |
5877 |
out: ; |
|
18364 | 5878 |
#endif /* not SCHTEAM */ |
17332 | 5879 |
%}. |
5880 |
error notNil ifTrue:[ |
|
18797 | 5881 |
lastErrorNumber := error. |
5882 |
self writeError:error. |
|
5883 |
^ self |
|
17332 | 5884 |
]. |
5885 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
|
5886 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
5887 |
binary == true ifFalse:[ |
|
18797 | 5888 |
(aCharacter isCharacter not |
5889 |
or:[aCharacter codePoint > 16rFF]) ifTrue:[ |
|
5890 |
self argumentMustBeCharacter. |
|
5891 |
^ self. |
|
5892 |
]. |
|
17332 | 5893 |
] ifTrue:[ |
18797 | 5894 |
aCharacter isInteger ifFalse:[ |
5895 |
self argumentMustBeInteger. |
|
5896 |
^ self. |
|
5897 |
]. |
|
17332 | 5898 |
]. |
5899 |
"/ migration support |
|
5900 |
self |
|
18797 | 5901 |
nextPutByte:aCharacter asInteger |
5902 |
toFile:handle |
|
17332 | 5903 |
! |
5904 |
||
17418 | 5905 |
nextPutAll:aCollection |
5906 |
"write all elements of the argument, aCollection. |
|
5907 |
Reimplemented for speed when writing strings or byteArrays. |
|
5908 |
For others, falls back to general method in superclass." |
|
5909 |
||
5910 |
|error| |
|
5911 |
%{ |
|
18364 | 5912 |
#ifdef __SCHTEAM__ |
5913 |
STObject handle = self.instVarAt(I_handle); |
|
5914 |
||
5915 |
if ((handle != STObject.Nil) |
|
5916 |
&& (aCollection.isSTString())) { |
|
18797 | 5917 |
handle.writeCharacters( aCollection.asSTString().characters ); |
5918 |
self.instVarAt_put(I_position, STObject.Nil); |
|
5919 |
return __c__._RETURN_self(); |
|
18364 | 5920 |
} |
5921 |
#else |
|
17418 | 5922 |
|
5923 |
__INST(lastErrorNumber) = nil; |
|
5924 |
||
5925 |
if ((__INST(handleType) == nil) |
|
5926 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5927 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
5928 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
5929 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5930 |
OBJ fp; |
5931 |
||
5932 |
if (((fp = __INST(handle)) != nil) |
|
5933 |
&& (__INST(mode) != @symbol(readonly)) |
|
5934 |
) { |
|
5935 |
INT len, cnt; |
|
5936 |
INT o_offs; |
|
5937 |
FILEPOINTER f = __FILEVal(fp); |
|
5938 |
int _buffered = (__INST(buffered) == true); |
|
5939 |
||
5940 |
if (! f) { |
|
5941 |
fprintf(stderr, "oops - fileHandle is NULL in nextPutAll:\n"); |
|
5942 |
__INST(handle) = nil; |
|
5943 |
goto out; |
|
5944 |
} |
|
5945 |
if (_buffered) { |
|
5946 |
__WRITING__(f) |
|
5947 |
} |
|
5948 |
||
5949 |
if (__isStringLike(aCollection)) { |
|
5950 |
OBJ mode = __INST(eolMode); |
|
5951 |
char *stringP = __stringVal(aCollection); |
|
5952 |
len = __stringSize(aCollection); |
|
5953 |
||
5954 |
if (__INST(binary) != true |
|
5955 |
&& ((mode == @symbol(cr)) |
|
5956 |
|| (mode == @symbol(etx)) |
|
5957 |
|| (mode == @symbol(eot)) |
|
5958 |
|| (mode == @symbol(crlf))) |
|
5959 |
&& memchr(stringP, '\n', len) != NULL) |
|
5960 |
{ |
|
5961 |
// there is a '\n' to be translated, replace it into a buffer |
|
5962 |
||
5963 |
char *end; |
|
5964 |
char sep[2]; |
|
5965 |
int sepLen = 1; |
|
5966 |
int bufLen; |
|
5967 |
char *buf, *endBuf, *sp, *dp; |
|
5968 |
||
5969 |
sep[0] = '\n'; |
|
5970 |
if (mode == @symbol(crlf)) { |
|
5971 |
sep[0] = '\r'; sep[1] = '\n'; sepLen = 2; |
|
5972 |
} else if (mode == @symbol(cr)) { |
|
5973 |
sep[0] = '\r'; |
|
5974 |
} else if (mode == @symbol(eot)) { |
|
5975 |
sep[0] = '\004'; |
|
5976 |
} else if (mode == @symbol(etx)) { |
|
5977 |
sep[0] = '\003'; |
|
5978 |
} |
|
5979 |
||
5980 |
// estimate size of buffer - assume every 4th char is a separator |
|
5981 |
bufLen = (sepLen == 1) ? len : (len + ((len/4) + 1) * sepLen); |
|
5982 |
buf = (char *)malloc(bufLen); |
|
5983 |
if (buf == NULL) { |
|
5984 |
error = __mkSmallInteger(ENOMEM); |
|
5985 |
goto out; |
|
5986 |
} |
|
5987 |
||
5988 |
endBuf = buf + bufLen; |
|
5989 |
end = stringP + len; |
|
5990 |
for (sp = stringP, dp = buf; sp < end; sp++) { |
|
5991 |
char c; |
|
5992 |
||
5993 |
if ((dp+sepLen) >= endBuf) { |
|
5994 |
char *newBuf; |
|
5995 |
||
5996 |
bufLen = bufLen * 2; |
|
5997 |
newBuf = (char *)realloc(buf, bufLen); |
|
5998 |
if (newBuf == NULL) { |
|
5999 |
free(buf); |
|
6000 |
error = __mkSmallInteger(ENOMEM); |
|
6001 |
goto out; |
|
6002 |
} |
|
6003 |
endBuf = newBuf + bufLen; |
|
6004 |
dp = newBuf + (dp-buf); |
|
6005 |
buf = newBuf; |
|
6006 |
} |
|
6007 |
||
6008 |
if ((c = *sp) != '\n') { |
|
6009 |
*dp++ = c; |
|
6010 |
} else { |
|
6011 |
*dp++ = sep[0]; |
|
6012 |
if (sepLen == 2) { |
|
6013 |
*dp++ = sep[1]; |
|
6014 |
}; |
|
6015 |
} |
|
6016 |
} |
|
6017 |
||
6018 |
len = dp - buf; |
|
18364 | 6019 |
# ifdef WIN32 |
18797 | 6020 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6021 |
cnt = __win32_fwrite(buf, 1, len, f); |
|
6022 |
} else |
|
18364 | 6023 |
# endif |
18797 | 6024 |
{ |
6025 |
__WRITEBYTES__(cnt, f, buf, len, _buffered, __INST(handleType)); |
|
6026 |
} |
|
6027 |
free(buf); |
|
6028 |
} else { // No EOL conversion needed |
|
18364 | 6029 |
# ifdef WIN32 |
18797 | 6030 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6031 |
cnt = __win32_fwrite(stringP, 1, len, f); |
|
6032 |
} else |
|
18364 | 6033 |
# endif |
18797 | 6034 |
{ |
6035 |
o_offs = stringP - (char *)__InstPtr(aCollection); |
|
6036 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered, __INST(handleType)); |
|
6037 |
} |
|
6038 |
} |
|
6039 |
} else { // Not a String |
|
6040 |
if (__INST(binary) == true) { |
|
6041 |
INT offs; |
|
6042 |
||
6043 |
if (__isByteArrayLike(aCollection)) { |
|
6044 |
offs = 0; |
|
6045 |
len = __byteArraySize(aCollection); |
|
6046 |
} else if (__isBytes(aCollection)) { |
|
6047 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
6048 |
len = __byteArraySize(aCollection) - offs; |
|
6049 |
} else |
|
6050 |
goto out; |
|
18364 | 6051 |
# ifdef WIN32 |
18797 | 6052 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6053 |
cnt = __win32_fwrite(__stringVal(aCollection), 1, len, f); |
|
6054 |
} else |
|
18364 | 6055 |
# endif |
18797 | 6056 |
{ |
6057 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element) - (char *)__InstPtr(aCollection); |
|
6058 |
o_offs += offs; |
|
6059 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered, __INST(handleType)); |
|
6060 |
} |
|
6061 |
} else // Not binary mode |
|
6062 |
goto out; |
|
6063 |
} |
|
6064 |
||
6065 |
// Now check for errors |
|
6066 |
if (cnt == len) { |
|
6067 |
if (__isSmallInteger(__INST(position))) { |
|
6068 |
INT np = __intVal(__INST(position)) + len; |
|
6069 |
OBJ t; |
|
6070 |
||
6071 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6072 |
} else { |
|
6073 |
__INST(position) = nil; /* i.e. do not know */ |
|
6074 |
} |
|
6075 |
RETURN (self); |
|
6076 |
} |
|
6077 |
fprintf(stderr, "cnt=%d len=%d\n", cnt, len); |
|
6078 |
error = __mkSmallInteger(__threadErrno); |
|
6079 |
} |
|
17418 | 6080 |
} |
6081 |
out: ; |
|
18364 | 6082 |
#endif /* not SCHTEAM */ |
17418 | 6083 |
%}. |
6084 |
error notNil ifTrue:[ |
|
18797 | 6085 |
lastErrorNumber := error. |
6086 |
self writeError:error. |
|
6087 |
^ self |
|
17418 | 6088 |
]. |
6089 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
|
6090 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
6091 |
||
6092 |
^ super nextPutAll:aCollection |
|
6093 |
! |
|
6094 |
||
17608 | 6095 |
nextPutAll:initialWriteCount from:buffer startingAt:initialOffset |
6096 |
"redefined, to wait until stream is writable, to avoid blocking in a write. |
|
6097 |
I don't know how to write non-bytes to a external stream, but let superclass handle this." |
|
6098 |
||
6099 |
buffer isByteCollection ifTrue:[ |
|
18797 | 6100 |
^ self nextPutBytes:initialWriteCount from:buffer startingAt:initialOffset. |
17608 | 6101 |
]. |
6102 |
||
6103 |
^ super nextPutAll:initialWriteCount from:buffer startingAt:initialOffset |
|
6104 |
! |
|
6105 |
||
17418 | 6106 |
nextPutAll:aCollection startingAt:start to:stop |
6107 |
"write a range of elements of the argument, aCollection. |
|
6108 |
Reimplemented for speed when writing strings or byteArrays. |
|
6109 |
For others, falls back to general method in superclass." |
|
6110 |
||
6111 |
|error| |
|
6112 |
||
6113 |
%{ |
|
6114 |
__INST(lastErrorNumber) = nil; |
|
6115 |
if ((__INST(handleType) == nil) |
|
6116 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
6117 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
6118 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
6119 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 6120 |
OBJ fp = __INST(handle); |
6121 |
||
6122 |
if ((fp != nil) |
|
6123 |
&& (__INST(mode) != @symbol(readonly)) |
|
6124 |
&& __bothSmallInteger(start, stop) |
|
6125 |
) { |
|
6126 |
int _buffered = (__INST(buffered) == true); |
|
6127 |
FILEPOINTER f = __FILEVal(fp); |
|
6128 |
int offs, len, cnt; |
|
6129 |
int iStart = __intVal(start); |
|
6130 |
int iStop = __intVal(stop); |
|
6131 |
int o_offs; |
|
6132 |
||
6133 |
if (_buffered ) { |
|
6134 |
__WRITING__(f) |
|
6135 |
} |
|
6136 |
||
6137 |
if ((iStart < 1) || (iStop < iStart)) { |
|
6138 |
RETURN(self); |
|
6139 |
} |
|
6140 |
if (__isStringLike(aCollection)) { |
|
6141 |
char *stringP; |
|
6142 |
OBJ mode = __INST(eolMode); |
|
6143 |
||
6144 |
len = __stringSize(aCollection); |
|
6145 |
if (iStop > len) { |
|
6146 |
RETURN(self); |
|
6147 |
} |
|
6148 |
if (iStop > len) |
|
6149 |
iStop = len; |
|
6150 |
len = iStop - iStart + 1; |
|
6151 |
stringP = __stringVal(aCollection) + iStart - 1; |
|
6152 |
||
6153 |
if (__INST(binary) != true |
|
6154 |
&& ((mode == @symbol(cr)) |
|
6155 |
|| (mode == @symbol(etx)) |
|
6156 |
|| (mode == @symbol(eot)) |
|
6157 |
|| (mode == @symbol(crlf))) |
|
6158 |
&& memchr(stringP, '\n', len) != NULL) |
|
6159 |
{ |
|
6160 |
// see if there is a \n which needs to be translated, replace it |
|
6161 |
||
6162 |
char *end = stringP + len; |
|
6163 |
char sep[2]; |
|
6164 |
int sepLen = 1; |
|
6165 |
int bufLen; |
|
6166 |
char *buf, *endBuf, *sp, *dp; |
|
6167 |
||
6168 |
sep[0] = '\n'; |
|
6169 |
if (mode == @symbol(crlf)) { |
|
6170 |
sep[0] = '\r'; sep[1] = '\n'; sepLen = 2; |
|
6171 |
} else if (mode == @symbol(cr)) { |
|
6172 |
sep[0] = '\r'; |
|
6173 |
} else if (mode == @symbol(eot)) { |
|
6174 |
sep[0] = '\004'; |
|
6175 |
} else if (mode == @symbol(etx)) { |
|
6176 |
sep[0] = '\003'; |
|
6177 |
} |
|
6178 |
||
6179 |
// estimate size of buffer - assume every 4th char is a separator |
|
6180 |
bufLen = (sepLen == 1) ? len : (len + ((len/4) + 1) * sepLen); |
|
6181 |
buf = (char *)malloc(bufLen); |
|
6182 |
if (buf == NULL) { |
|
6183 |
error = __mkSmallInteger(ENOMEM); |
|
6184 |
goto out; |
|
6185 |
} |
|
6186 |
||
6187 |
endBuf = buf + bufLen; |
|
6188 |
||
6189 |
for (sp = stringP, dp = buf; sp < end; sp++) { |
|
6190 |
char c; |
|
6191 |
||
6192 |
if ((dp+sepLen) >= endBuf) { |
|
6193 |
char *newBuf; |
|
6194 |
||
6195 |
bufLen = bufLen * 2; |
|
6196 |
newBuf = (char *)realloc(buf, bufLen); |
|
6197 |
if (newBuf == NULL) { |
|
6198 |
free(buf); |
|
6199 |
error = __mkSmallInteger(ENOMEM); |
|
6200 |
goto out; |
|
6201 |
} |
|
6202 |
endBuf = newBuf + bufLen; |
|
6203 |
dp = newBuf + (dp-buf); |
|
6204 |
buf = newBuf; |
|
6205 |
} |
|
6206 |
||
6207 |
if ((c = *sp) == '\n') { |
|
6208 |
*dp++ = sep[0]; |
|
6209 |
if (sepLen == 2) { |
|
6210 |
*dp++ = sep[1]; |
|
6211 |
}; |
|
6212 |
} else { |
|
6213 |
*dp++ = c; |
|
6214 |
} |
|
6215 |
} |
|
6216 |
||
6217 |
len = dp - buf; |
|
17420 | 6218 |
#ifdef WIN32 |
18797 | 6219 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6220 |
cnt = __win32_fwrite(buf, 1, len, f); |
|
6221 |
} else |
|
17420 | 6222 |
#endif |
18797 | 6223 |
{ |
6224 |
__WRITEBYTES__(cnt, f, buf, len, _buffered, __INST(handleType)); |
|
6225 |
} |
|
6226 |
free(buf); |
|
6227 |
} else { // No EOL conversion needed |
|
17420 | 6228 |
#ifdef WIN32 |
18797 | 6229 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6230 |
cnt = __win32_fwrite(stringP, 1, len, f); |
|
6231 |
} else |
|
17420 | 6232 |
#endif |
18797 | 6233 |
{ |
6234 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
|
6235 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered, __INST(handleType)); |
|
6236 |
} |
|
6237 |
} |
|
6238 |
} else { // Not a string |
|
6239 |
if (__INST(binary) == true) { |
|
6240 |
int offs; |
|
6241 |
||
6242 |
if (__isByteArrayLike(aCollection)) { |
|
6243 |
offs = 0; |
|
6244 |
len = __byteArraySize(aCollection); |
|
6245 |
} else if (__isBytes(aCollection)) { |
|
6246 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
6247 |
len = __byteArraySize(aCollection) - offs; |
|
6248 |
} else |
|
6249 |
goto out; |
|
6250 |
||
6251 |
if (iStop > len) { |
|
6252 |
RETURN(self); |
|
6253 |
} |
|
6254 |
if (iStop > len) |
|
6255 |
iStop = len; |
|
6256 |
len = iStop - iStart + 1; |
|
6257 |
offs += iStart - 1; |
|
17418 | 6258 |
#ifdef WIN32 |
18797 | 6259 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6260 |
cnt = __win32_fwrite(__stringVal(aCollection)+iStart-1, 1, len, f); |
|
6261 |
} else |
|
17418 | 6262 |
#endif |
18797 | 6263 |
{ |
6264 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection); |
|
6265 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+offs, len, _buffered, __INST(handleType)); |
|
6266 |
} |
|
6267 |
} else |
|
6268 |
goto out; |
|
6269 |
} |
|
6270 |
if (cnt == len) { |
|
6271 |
if (__isSmallInteger(__INST(position))) { |
|
6272 |
INT np = __intVal(__INST(position)) + len; |
|
6273 |
OBJ t; |
|
6274 |
||
6275 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6276 |
} else { |
|
6277 |
__INST(position) = nil; /* i.e. do not know */ |
|
6278 |
} |
|
6279 |
RETURN (self); |
|
6280 |
} |
|
6281 |
error = __mkSmallInteger(__threadErrno); |
|
6282 |
} |
|
17418 | 6283 |
} |
6284 |
out: ; |
|
6285 |
%}. |
|
6286 |
error notNil ifTrue:[ |
|
18797 | 6287 |
lastErrorNumber := error. |
6288 |
self writeError:error. |
|
6289 |
^ self |
|
17418 | 6290 |
]. |
6291 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
6292 |
! |
|
6293 |
||
6294 |
nextPutAllUnicode:aString |
|
6295 |
"normal streams can not handle multi-byte characters, so convert them to utf8. |
|
6296 |
This is needed, so that you can do ('something' asUnicode16String errorPrintCR)" |
|
6297 |
||
6298 |
aString do:[:eachCharacter| |
|
18797 | 6299 |
self nextPutUtf8:eachCharacter. |
17418 | 6300 |
]. |
6301 |
||
6302 |
" |
|
18797 | 6303 |
'Bönnigheim' asUnicode16String errorPrintCR |
17418 | 6304 |
" |
6305 |
! |
|
6306 |
||
17608 | 6307 |
nextPutBytes:count from:anObject startingAt:start |
6308 |
"write count bytes from an object starting at index start. |
|
6309 |
return the number of bytes written - which could be 0. |
|
6310 |
The object must have non-pointer indexed instvars |
|
6311 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
|
6312 |
or an externalBytes object (with known size). |
|
6313 |
||
6314 |
Use with care - non object oriented i/o. |
|
6315 |
Warning: |
|
18797 | 6316 |
in general, you cannot use this method to pass non-byte data to other |
6317 |
architectures (unless you prepared the buffer with care), |
|
6318 |
since it does not care for byte order or float representation." |
|
17608 | 6319 |
|
6320 |
|error| |
|
6321 |
||
6322 |
%{ |
|
18312 | 6323 |
#ifdef __SCHTEAM__ |
6324 |
byte[] bytes; |
|
6325 |
STObject handle = self.instVarAt(I_handle); |
|
6326 |
||
6327 |
if (anObject.isSTString()) { |
|
18797 | 6328 |
char[] chars = anObject.asSTString().characters; |
6329 |
handle.writeCharacters(chars, start.intValue()-1, count.intValue()); |
|
6330 |
self.instVarAt_put(I_position, STObject.Nil); |
|
6331 |
return context._RETURN(count); |
|
18364 | 6332 |
} |
6333 |
if (anObject.isSymbol()) { |
|
18797 | 6334 |
java.lang.String chars = anObject.asSTSymbol().characters; |
6335 |
handle.writeString(chars, start.intValue()-1, count.intValue()); |
|
6336 |
self.instVarAt_put(I_position, STObject.Nil); |
|
6337 |
return context._RETURN(count); |
|
18312 | 6338 |
} |
6339 |
#else |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
6340 |
int ret; |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
6341 |
int objSize, nInstBytes; |
17608 | 6342 |
char *extPtr; |
6343 |
OBJ fp; |
|
6344 |
||
6345 |
__INST(lastErrorNumber) = nil; |
|
6346 |
if ((__INST(handleType) == nil) |
|
6347 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
6348 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
6349 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
6350 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 6351 |
if (((fp = __INST(handle)) != nil) |
6352 |
&& (__INST(mode) != @symbol(readonly)) |
|
6353 |
&& __bothSmallInteger(count, start) |
|
6354 |
) { |
|
6355 |
int _buffered = (__INST(buffered) == true); |
|
6356 |
FILEPOINTER f = __FILEVal(fp); |
|
6357 |
int len = __intVal(count); |
|
6358 |
int offs = __intVal(start) - 1; |
|
6359 |
||
6360 |
if (__isExternalBytesLike(anObject)) { |
|
6361 |
OBJ sz; |
|
6362 |
||
6363 |
nInstBytes = 0; |
|
6364 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
6365 |
if (extPtr == NULL) goto bad; |
|
6366 |
sz = __externalBytesSize(anObject); |
|
6367 |
if (__isSmallInteger(sz)) { |
|
6368 |
objSize = __intVal(sz); |
|
6369 |
} else { |
|
6370 |
objSize = 0; /* unknown */ |
|
6371 |
} |
|
6372 |
} else { |
|
6373 |
OBJ oClass = __Class(anObject); |
|
6374 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
6375 |
||
6376 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
6377 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
6378 |
case BYTEARRAY: |
|
6379 |
case WORDARRAY: |
|
6380 |
case LONGARRAY: |
|
6381 |
case SWORDARRAY: |
|
6382 |
case SLONGARRAY: |
|
6383 |
case FLOATARRAY: |
|
6384 |
break; |
|
6385 |
case DOUBLEARRAY: |
|
18312 | 6386 |
# ifdef __NEED_DOUBLE_ALIGN |
18797 | 6387 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
18312 | 6388 |
# endif |
18797 | 6389 |
break; |
6390 |
case LONGLONGARRAY: |
|
6391 |
case SLONGLONGARRAY: |
|
18312 | 6392 |
# ifdef __NEED_LONGLONG_ALIGN |
18797 | 6393 |
nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1); |
18312 | 6394 |
# endif |
18797 | 6395 |
break; |
6396 |
default: |
|
6397 |
goto bad; |
|
6398 |
} |
|
6399 |
extPtr = (char *)0; |
|
6400 |
objSize = __Size(anObject) - nInstBytes; |
|
6401 |
} |
|
6402 |
if ( (offs >= 0) && (len >= 0) && (objSize >= (len + offs)) ) { |
|
6403 |
int cnt; |
|
6404 |
||
6405 |
if (_buffered) { |
|
6406 |
__WRITING__(f) |
|
6407 |
} |
|
6408 |
||
6409 |
if (extPtr) { |
|
18312 | 6410 |
# ifdef WIN32 |
18797 | 6411 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6412 |
cnt = __win32_fwrite(extPtr+offs, 1, len, f); |
|
6413 |
} else |
|
18312 | 6414 |
# endif |
18797 | 6415 |
{ |
6416 |
__WRITEBYTES__(cnt, f, extPtr+offs, len, _buffered, __INST(handleType)); |
|
6417 |
} |
|
6418 |
} else { |
|
6419 |
/* |
|
6420 |
* on interrupt, anObject may be moved to another location. |
|
6421 |
* So we pass anObject, and the offset to the __WRITEBYTES_OBJ__ macro. |
|
6422 |
*/ |
|
6423 |
offs += nInstBytes; |
|
18312 | 6424 |
# ifdef WIN32 |
18797 | 6425 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6426 |
cnt = __win32_fwrite((char *)anObject+offs, 1, len, f); |
|
6427 |
} else |
|
18312 | 6428 |
# endif |
18797 | 6429 |
{ |
6430 |
__WRITEBYTES_OBJ__(cnt, f, anObject, offs, len, _buffered, __INST(handleType)); |
|
6431 |
} |
|
6432 |
} |
|
6433 |
||
6434 |
if (cnt >= 0) { |
|
6435 |
if (__isSmallInteger(__INST(position))) { |
|
6436 |
INT np = __intVal(__INST(position)) + cnt; |
|
6437 |
OBJ t; |
|
6438 |
||
6439 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6440 |
} else { |
|
6441 |
__INST(position) = nil; /* i.e. do not know */ |
|
6442 |
} |
|
6443 |
RETURN ( __mkSmallInteger(cnt) ); |
|
6444 |
} else /* cnt < 0 */ { |
|
6445 |
if ( |
|
18312 | 6446 |
# ifdef EWOULDBLOCK |
18797 | 6447 |
(__threadErrno == EWOULDBLOCK) || |
18312 | 6448 |
# endif |
18797 | 6449 |
(__threadErrno == EAGAIN) |
6450 |
) { |
|
6451 |
RETURN ( __mkSmallInteger(0) ); |
|
6452 |
} |
|
6453 |
__INST(position) = nil; /* i.e. do not know */ |
|
6454 |
error = __mkSmallInteger(__threadErrno); |
|
6455 |
} |
|
6456 |
} |
|
6457 |
} |
|
17608 | 6458 |
} |
6459 |
bad: ; |
|
18312 | 6460 |
#endif /* not SCHTEAM */ |
17608 | 6461 |
%}. |
6462 |
error notNil ifTrue:[ |
|
18797 | 6463 |
lastErrorNumber := error. |
6464 |
self writeError:error. |
|
6465 |
^ 0 |
|
17608 | 6466 |
]. |
17634 | 6467 |
handle isNil ifTrue:[self errorNotOpen. ^ 0]. |
6468 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ 0]. |
|
6469 |
self primitiveFailed. |
|
6470 |
^ 0. |
|
17608 | 6471 |
! |
6472 |
||
17418 | 6473 |
nextPutUnicode:aCharacter |
6474 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
6475 |
||
6476 |
self nextPutUtf8:aCharacter. |
|
6477 |
! |
|
6478 |
||
6479 |
nextPutUtf16:aCharacter |
|
6480 |
"append my UTF-16 representation to the argument, aStream. |
|
6481 |
UTF-16 can encode only characters with code points between 0 to 16r10FFFF." |
|
6482 |
||
6483 |
|codePoint| |
|
6484 |
||
6485 |
codePoint := aCharacter codePoint. |
|
6486 |
(codePoint <= 16rD7FF |
|
6487 |
or:[codePoint >= 16rE000 and:[codePoint <= 16rFFFF]]) ifTrue:[ |
|
18797 | 6488 |
self nextPutShort:codePoint MSB:true. |
17418 | 6489 |
] ifFalse:[codePoint <= 16r10FFFF ifTrue:[ |
18797 | 6490 |
|highBits lowBits| |
6491 |
||
6492 |
codePoint := codePoint - 16r100000. |
|
6493 |
highBits := codePoint bitShift:-10. |
|
6494 |
lowBits := codePoint bitAnd:16r3FF. |
|
6495 |
self nextPutShort:highBits+16rD800 MSB:true. |
|
6496 |
self nextPutShort:lowBits+16rDC00 MSB:true. |
|
17418 | 6497 |
] ifFalse:[ |
18797 | 6498 |
EncodingError raiseWith:aCharacter errorString:'Character cannot be encoded as UTF-16'. |
17418 | 6499 |
]]. |
6500 |
||
6501 |
" |
|
18797 | 6502 |
(FileStream newTemporary |
6503 |
nextPutUtf16:$B; |
|
6504 |
nextPutUtf16:$Ä; |
|
6505 |
nextPutUtf16:(Character codePoint:16r10CCCC); |
|
6506 |
reset; |
|
6507 |
binary; |
|
6508 |
contents) |
|
17418 | 6509 |
" |
6510 |
! |
|
6511 |
||
17332 | 6512 |
old_nextPut:aCharacter |
6513 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
6514 |
Only single-byte characters are currently supported" |
|
6515 |
||
6516 |
|error| |
|
6517 |
%{ |
|
6518 |
OBJ fp; |
|
612 | 6519 |
|
1133 | 6520 |
__INST(lastErrorNumber) = nil; |
12548 | 6521 |
if ((__INST(handleType) == nil) |
6522 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
6523 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
6524 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 6525 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 6526 |
if (((fp = __INST(handle)) != nil) |
6527 |
&& (__INST(mode) != @symbol(readonly)) |
|
6528 |
) { |
|
6529 |
unsigned char c; |
|
6530 |
int _buffered = (__INST(buffered) == true); |
|
6531 |
FILEPOINTER f = __FILEVal(fp); |
|
6532 |
||
6533 |
if (__INST(binary) != true) { |
|
6534 |
if (__isCharacter(aCharacter)) { |
|
6535 |
unsigned codePoint = __intVal(__characterVal(aCharacter)); |
|
6536 |
||
6537 |
if (codePoint <= 0xFF) { |
|
6538 |
int cnt; |
|
6539 |
||
6540 |
c = codePoint; |
|
612 | 6541 |
doWrite: |
18797 | 6542 |
if (! f) { |
6543 |
fprintf(stderr, "oops - fileHandle is NULL in nextPut:\n"); |
|
6544 |
__INST(handle) = nil; |
|
6545 |
goto out; |
|
6546 |
} |
|
6547 |
||
6548 |
if (_buffered) { |
|
6549 |
__WRITING__(f) |
|
6550 |
} |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
6551 |
#ifdef WIN32 |
18797 | 6552 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6553 |
cnt = __win32_fwrite(&c, 1, 1, f); |
|
6554 |
} else |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
6555 |
#endif |
18797 | 6556 |
__WRITEBYTE__(cnt, f, &c, _buffered, __INST(handleType)); |
6557 |
if (cnt == 1) { |
|
6558 |
if (__isSmallInteger(__INST(position))) { |
|
6559 |
INT np = __intVal(__INST(position)) + 1; |
|
6560 |
OBJ t; |
|
6561 |
||
6562 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6563 |
} else { |
|
6564 |
__INST(position) = nil; /* i.e. do not know */ |
|
6565 |
} |
|
6566 |
RETURN ( self ); |
|
6567 |
} |
|
6568 |
error = __mkSmallInteger(__threadErrno); |
|
6569 |
} |
|
6570 |
} |
|
6571 |
} else { |
|
6572 |
if (__isSmallInteger(aCharacter)) { |
|
6573 |
c = __intVal(aCharacter); |
|
6574 |
goto doWrite; |
|
6575 |
} |
|
6576 |
} |
|
6577 |
} |
|
612 | 6578 |
} |
7737 | 6579 |
out: ; |
612 | 6580 |
%}. |
16296 | 6581 |
error notNil ifTrue:[ |
18797 | 6582 |
lastErrorNumber := error. |
6583 |
self writeError:error. |
|
6584 |
^ self |
|
16296 | 6585 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
6586 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
2331 | 6587 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
5417 | 6588 |
binary == true ifFalse:[ |
18797 | 6589 |
(aCharacter isCharacter not |
6590 |
or:[aCharacter codePoint > 16rFF]) ifTrue:[ |
|
6591 |
self argumentMustBeCharacter. |
|
6592 |
^ self. |
|
6593 |
]. |
|
5417 | 6594 |
] ifTrue:[ |
18797 | 6595 |
aCharacter isInteger ifFalse:[ |
6596 |
self argumentMustBeInteger. |
|
6597 |
^ self. |
|
6598 |
]. |
|
5417 | 6599 |
]. |
6600 |
"/ migration support |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
6601 |
self |
18797 | 6602 |
nextPutByte:aCharacter asInteger |
6603 |
toFile:handle |
|
612 | 6604 |
! ! |
6605 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
6606 |
!ExternalStream class methodsFor:'documentation'! |
730 | 6607 |
|
6608 |
version |
|
18636 | 6609 |
^ '$Header$' |
12398 | 6610 |
! |
6611 |
||
6612 |
version_CVS |
|
18636 | 6613 |
^ '$Header$' |
730 | 6614 |
! ! |
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
6615 |
|
14603 | 6616 |
|
612 | 6617 |
ExternalStream initialize! |