author | Stefan Vogel <sv@exept.de> |
Wed, 25 Nov 2015 19:44:31 +0100 | |
changeset 18956 | 9ef063595474 |
parent 18929 | 14b2c3ab66e8 |
child 18958 | e35ab14763b6 |
child 19008 | e74b041ed377 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
18797 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5414 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
17418 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
ReadWriteStream subclass:#ExternalStream |
18929 | 17 |
instanceVariableNames:'handleType handle mode buffered binary eolMode hitEOF didWrite |
18 |
lastErrorNumber readAhead' |
|
19 |
classVariableNames:'Lobby LastErrorNumber InvalidReadSignal InvalidWriteSignal |
|
20 |
InvalidModeSignal OpenErrorSignal StreamNotOpenSignal |
|
21 |
InvalidOperationSignal DefaultEOLMode ReadMode ReadWriteMode |
|
22 |
WriteMode AppendMode CreateReadWriteMode StreamIOErrorSignal |
|
23 |
FileOpenTrace MaxNonTenurableExecutors' |
|
24 |
poolDictionaries:'' |
|
25 |
category:'Streams-External' |
|
1 | 26 |
! |
27 |
||
216 | 28 |
!ExternalStream primitiveDefinitions! |
1 | 29 |
%{ |
10328
ad731a7584a3
ensure that win32_fwrite is used for stdout and stderr
Claus Gittinger <cg@exept.de>
parents:
9053
diff
changeset
|
30 |
#undef fwrite |
ad731a7584a3
ensure that win32_fwrite is used for stdout and stderr
Claus Gittinger <cg@exept.de>
parents:
9053
diff
changeset
|
31 |
|
17078 | 32 |
#include "stxOSDefs.h" |
33 |
||
1 | 34 |
#include <stdio.h> |
437 | 35 |
#define _STDIO_H_INCLUDED_ |
36 |
||
2903 | 37 |
#ifndef NO_FCNTL_H |
38 |
# include <fcntl.h> |
|
39 |
# define _FCNTL_H_INCLUDED_ |
|
40 |
#endif |
|
437 | 41 |
|
10 | 42 |
#include <errno.h> |
437 | 43 |
#define _ERRNO_H_INCLUDED_ |
42 | 44 |
|
1141 | 45 |
#ifdef LINUX |
46 |
/* use inline string macros */ |
|
47 |
# define __STRINGDEFS__ |
|
48 |
# include <linuxIntern.h> |
|
49 |
#endif |
|
50 |
||
16508 | 51 |
#if !defined(MSDOS_LIKE) && !defined(__openVMS__) |
52 |
# include <termios.h> |
|
53 |
#endif |
|
54 |
||
42 | 55 |
#ifdef hpux |
56 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
|
57 |
#endif |
|
216 | 58 |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
59 |
#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
|
60 |
# 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
|
61 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
62 |
#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
|
63 |
# 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
|
64 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
65 |
#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
|
66 |
# 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
|
67 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
68 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
69 |
#if defined(__VMS__) || defined(__sparc__) || defined(WIN32) |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5101
diff
changeset
|
70 |
# define CLEAR_ERRNO __threadErrno = 0; |
2961 | 71 |
#else |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
72 |
# define CLEAR_ERRNO /* nothing */ |
2961 | 73 |
#endif |
74 |
||
3420 | 75 |
#ifdef LATER |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
76 |
# define __isFilePointer(x) (__Class(x) == ExternalStream__FilePointer) |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
77 |
# define __CHANGECLASS(o, x) (__qClass(o) = (x), o) |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
78 |
# define __MKFILEPOINTER(f) __CHANGECLASS(__MKEXTERNALADDRESS(f), ExternalStream__FilePointer) |
3420 | 79 |
#else |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
80 |
# define __MKFILEPOINTER(f) __MKEXTERNALADDRESS(f) |
3420 | 81 |
#endif |
82 |
||
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
83 |
#ifdef WIN32 |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
84 |
# define NO_STDIO |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
85 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
86 |
# undef __HANDLE_INTERRUPTS__ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
87 |
# define __HANDLE_INTERRUPTS__ /* nothing */ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
88 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
89 |
# undef __BEGIN_INTERRUPTABLE__ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
90 |
# define __BEGIN_INTERRUPTABLE__ /* nothing */ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
91 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
92 |
# undef __END_INTERRUPTABLE__ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
93 |
# define __END_INTERRUPTABLE__ /* nothing */ |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
94 |
#endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
95 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
96 |
# ifdef __i386__ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
97 |
# define _X86_ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
98 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
99 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
100 |
#ifdef WIN32 |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
101 |
# undef INT |
14644 | 102 |
# undef UINT |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
103 |
# undef Array |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
104 |
# undef Number |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
105 |
# undef Method |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
106 |
# undef Point |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
107 |
# undef Rectangle |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
108 |
# undef Block |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
109 |
# undef Time |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
110 |
# undef Date |
7832 | 111 |
# undef Delay |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
112 |
# undef Set |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
113 |
# undef Signal |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
114 |
# undef Context |
9023 | 115 |
# undef Message |
9024 | 116 |
# undef Process |
10339 | 117 |
# undef Processor |
14632 | 118 |
# undef String |
119 |
# undef Character |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
120 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
121 |
# define NOATOM |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
122 |
# define NOGDICAPMASKS |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
123 |
# define NOMETAFILE |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
124 |
# define NOMINMAX |
14068 | 125 |
//# define NOOPENFILE |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
126 |
# define NOSOUND |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
127 |
# define NOWH |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
128 |
//# define NOCOMM |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
129 |
# define NOKANJI |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
130 |
# define NOCRYPT |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
131 |
//# define NOMCX |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
132 |
# define WIN32_LEAN_AND_MEAN |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
133 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
134 |
# include <windows.h> |
15435
fc902ce16af0
typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents:
15361
diff
changeset
|
135 |
# include <winsock2.h> |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
136 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
137 |
# ifdef __DEF_Array |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
138 |
# define Array __DEF_Array |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
139 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
140 |
# ifdef __DEF_Number |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
141 |
# define Number __DEF_Number |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
142 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
143 |
# ifdef __DEF_Method |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
144 |
# define Method __DEF_Method |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
145 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
146 |
# ifdef __DEF_Point |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
147 |
# define Point __DEF_Point |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
148 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
149 |
# ifdef __DEF_Block |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
150 |
# define Block __DEF_Block |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
151 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
152 |
# ifdef __DEF_Time |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
153 |
# define Time __DEF_Time |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
154 |
# endif |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
155 |
# ifdef __DEF_Date |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
156 |
# define Date __DEF_Date |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
157 |
# endif |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
158 |
# ifdef __DEF_Set |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
159 |
# define Set __DEF_Set |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
160 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
161 |
# ifdef __DEF_Signal |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
162 |
# define Signal __DEF_Signal |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
163 |
# endif |
7832 | 164 |
# ifdef __DEF_Delay |
165 |
# define Delay __DEF_Delay |
|
166 |
# endif |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
167 |
# ifdef __DEF_Context |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
168 |
# define Context __DEF_Context |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
169 |
# endif |
9023 | 170 |
# ifdef __DEF_Message |
171 |
# define Message __DEF_Message |
|
172 |
# endif |
|
9024 | 173 |
# ifdef __DEF_Process |
174 |
# define Process __DEF_Process |
|
175 |
# endif |
|
10339 | 176 |
# ifdef __DEF_Processor |
177 |
# define Processor __DEF_Processor |
|
178 |
# endif |
|
14632 | 179 |
# ifdef __DEF_String |
180 |
# define String __DEF_String |
|
181 |
# endif |
|
182 |
# ifdef __DEF_Character |
|
183 |
# define Character __DEF_Character |
|
184 |
# endif |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
185 |
|
14644 | 186 |
# define INT STX_INT |
187 |
# define UINT STX_UINT |
|
188 |
# ifndef off_t |
|
189 |
# define off_t long |
|
190 |
# endif |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
191 |
#endif /* WIN32 */ |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
192 |
|
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
193 |
#ifndef NO_STDIO /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
194 |
# define STDIO_NEEDS_FSEEK |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
195 |
# define FILEPOINTER FILE * |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
196 |
# define READ(f, cp, n) read(f, cp, n) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
197 |
# define WRITE(f, cp, n) write(f, cp, n) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
198 |
# define FFLUSH(fp) fflush(fp) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
199 |
# define FILENO(f) fileno(f) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
200 |
#endif /* use STDIO */ |
3681 | 201 |
|
3682 | 202 |
#ifdef DEBUGGING |
4092 | 203 |
extern char *__survStartPtr, *__survEndPtr; |
3682 | 204 |
# define DEBUGBUFFER(buf) \ |
3681 | 205 |
if (((char *)(buf) >= __survStartPtr) \ |
206 |
&& ((char *)(buf) < __survEndPtr)) { \ |
|
18797 | 207 |
__fatal0("read into survivor\n"); \ |
3681 | 208 |
} |
209 |
||
3682 | 210 |
#else |
211 |
# define DEBUGBUFFER(buf) /* nothing */ |
|
212 |
#endif |
|
213 |
||
216 | 214 |
/* |
215 |
* stdio library requires an fseek before reading whenever a file |
|
216 |
* is open for read/write and the last operation was a write. |
|
217 |
* (also vice-versa). |
|
218 |
* All code should use the following macro before doing reads: |
|
219 |
*/ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
220 |
#ifdef STDIO_NEEDS_FSEEK |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
221 |
# 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
|
222 |
#else |
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) /* nothing */ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
224 |
#endif |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
225 |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
226 |
#ifdef WIN32 |
16912 | 227 |
// Win returns from ReadFile() with false and _threadErrno == 0 on end of pipe. |
228 |
// We don't know why |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
229 |
# define READ(ret, f, cp, n, handleType) { \ |
18797 | 230 |
if (handleType == @symbol(socketHandle)) { \ |
231 |
(ret) = __STX_WSA_NOINT_CALL4("recv", recv, (f), (cp), (n), 0); \ |
|
232 |
} else { \ |
|
233 |
HANDLE h = _get_osfhandle(fileno(f)); \ |
|
234 |
if (handleType == @symbol(socketFilePointer)) { \ |
|
235 |
(ret) = __STX_WSA_NOINT_CALL4("recv", recv, h, (cp), (n), 0);\ |
|
236 |
} else { \ |
|
237 |
int __res; \ |
|
238 |
(ret) = __STX_API_NOINT_CALL5("ReadFile", ReadFile, h, (cp), (n), &__res, 0);\ |
|
239 |
(ret) = (ret) ? __res : ((__threadErrno == EPIPE || __threadErrno == 0) ? 0 : -1); \ |
|
240 |
} \ |
|
241 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
242 |
} |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
243 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
244 |
# define WRITE(ret, f, cp, n, handleType) { \ |
18797 | 245 |
if (handleType == @symbol(socketHandle)) { \ |
246 |
(ret) = __STX_WSA_NOINT_CALL4("send", send, (f), (cp), (n), 0); \ |
|
247 |
} else {\ |
|
248 |
HANDLE h = _get_osfhandle(fileno(f)); \ |
|
249 |
if (handleType == @symbol(socketFilePointer)) { \ |
|
250 |
(ret) = __STX_WSA_NOINT_CALL4("send", send, h, (cp), (n), 0);\ |
|
251 |
} else {\ |
|
252 |
int __res; \ |
|
253 |
(ret) = __STX_API_NOINT_CALL5("WriteFile", WriteFile, h, (cp), (n), &__res, 0);\ |
|
254 |
(ret) = (ret) ? __res : -1; \ |
|
255 |
} \ |
|
256 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
257 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
258 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
259 |
# define FFLUSH(fp) fflush(fp) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
260 |
# undef STDIO_NEEDS_FSEEK |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
261 |
# define FILEPOINTER FILE * |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
262 |
# define FILENO(f) fileno(f) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
263 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
264 |
# define __READING__(f) \ |
1133 | 265 |
if ((__INST(didWrite) != false) \ |
266 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
18797 | 267 |
__INST(didWrite) = false; \ |
268 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
216 | 269 |
} |
270 |
||
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
271 |
# define __WRITING__(f) \ |
1133 | 272 |
if ((__INST(didWrite) != true) \ |
273 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
18797 | 274 |
__INST(didWrite) = true; \ |
275 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
216 | 276 |
} |
277 |
||
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
278 |
# define __UNGETC__(c, f, isBuffered) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
279 |
if (isBuffered) { \ |
18797 | 280 |
ungetc((c), (f)); \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
281 |
} else { \ |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
282 |
__INST(readAhead) = __mkSmallInteger((c)); \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
283 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
284 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
285 |
# define __READBYTE__(ret, f, buf, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
286 |
if (isBuffered) { \ |
18797 | 287 |
for (;;) { \ |
288 |
CLEAR_ERRNO; \ |
|
289 |
(ret) = getc(f); \ |
|
290 |
if ((ret) >= 0) { \ |
|
291 |
*(buf) = (ret); \ |
|
292 |
(ret) = 1; \ |
|
293 |
} else if (ferror(f)) { \ |
|
294 |
if (__threadErrno == EINTR) { \ |
|
295 |
clearerr(f); \ |
|
296 |
continue; \ |
|
297 |
} \ |
|
298 |
} else { \ |
|
299 |
(ret) = 0; \ |
|
300 |
} \ |
|
301 |
break; \ |
|
302 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
303 |
} else { \ |
18797 | 304 |
OBJ rA = __INST(readAhead); \ |
305 |
if (rA != nil) { \ |
|
306 |
*(buf) = (char)__intVal(rA); \ |
|
307 |
__INST(readAhead) = nil; \ |
|
308 |
(ret) = 1; \ |
|
309 |
} else { \ |
|
310 |
for (;;) { \ |
|
311 |
CLEAR_ERRNO; \ |
|
312 |
READ((ret), f, buf, 1, handleType); \ |
|
313 |
if ((ret) >= 0 || __threadErrno != EINTR) \ |
|
314 |
break; \ |
|
315 |
} \ |
|
316 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
317 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
318 |
|
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 |
* read_bytes into a c-buffer |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
321 |
* (which may NOT move) |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
322 |
*/ |
12548 | 323 |
# define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
324 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
325 |
if (isBuffered) { \ |
18797 | 326 |
int __offs = 0; \ |
327 |
while (__offs < (cnt)) { \ |
|
328 |
CLEAR_ERRNO; \ |
|
329 |
(ret) = getc(f); \ |
|
330 |
if ((ret) < 0) { \ |
|
331 |
if (ferror(f)) { \ |
|
332 |
if (__threadErrno == EINTR) { \ |
|
333 |
clearerr(f); \ |
|
334 |
continue; \ |
|
335 |
} \ |
|
336 |
} else { \ |
|
337 |
(ret) = 0; \ |
|
338 |
} \ |
|
339 |
break; \ |
|
340 |
} \ |
|
341 |
(buf)[__offs++] = (ret); \ |
|
342 |
} \ |
|
343 |
if (__offs > 0) \ |
|
344 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
345 |
} else { \ |
18797 | 346 |
int __offs = 0; \ |
347 |
\ |
|
348 |
while (__offs < (cnt)) { \ |
|
349 |
OBJ rA = __INST(readAhead); \ |
|
350 |
if (rA != nil) { \ |
|
351 |
(buf)[__offs] = __intVal(rA); \ |
|
352 |
__INST(readAhead) = nil; \ |
|
353 |
(ret) = 1; \ |
|
354 |
} else { \ |
|
355 |
CLEAR_ERRNO; \ |
|
356 |
READ((ret), f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
357 |
if ((ret) <= 0) { \ |
|
358 |
if ((ret) < 0 && __threadErrno == EINTR) { \ |
|
359 |
continue; \ |
|
360 |
} \ |
|
361 |
break; \ |
|
362 |
} \ |
|
363 |
} \ |
|
364 |
__offs += (ret); \ |
|
365 |
} \ |
|
366 |
if (__offs > 0) \ |
|
367 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
368 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
369 |
|
12548 | 370 |
# define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
371 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
372 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
373 |
int oldFlags; \ |
18797 | 374 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
375 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
376 |
if (isBuffered) { \ |
18797 | 377 |
while (__offs < (cnt)) { \ |
378 |
CLEAR_ERRNO; \ |
|
379 |
(ret) = getc(f); \ |
|
380 |
if ((ret) < 0) { \ |
|
381 |
if (ferror(f)) { \ |
|
382 |
if (__threadErrno == EINTR) { \ |
|
383 |
clearerr(f); \ |
|
384 |
continue; \ |
|
385 |
} \ |
|
386 |
} else { \ |
|
387 |
(ret) = 0; \ |
|
388 |
} \ |
|
389 |
break; \ |
|
390 |
} \ |
|
391 |
(buf)[__offs++] = (ret); \ |
|
392 |
} \ |
|
393 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
394 |
} else { \ |
18797 | 395 |
while (__offs < (cnt)) { \ |
396 |
OBJ rA = __INST(readAhead); \ |
|
397 |
if (rA != nil) { \ |
|
398 |
(buf)[__offs] = __intVal(rA); \ |
|
399 |
__INST(readAhead) = nil; \ |
|
400 |
(ret) = 1; \ |
|
401 |
__offs ++; \ |
|
402 |
continue; \ |
|
403 |
} \ |
|
404 |
CLEAR_ERRNO; \ |
|
405 |
{ \ |
|
406 |
int res = 0; \ |
|
407 |
if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res), 1)) \ |
|
408 |
|| (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \ |
|
409 |
|| (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0), 1))) { \ |
|
410 |
if (res > 0) { \ |
|
411 |
if (res > ((cnt)-__offs)) \ |
|
412 |
res = (cnt)-__offs; \ |
|
413 |
READ((ret), f, (buf)+__offs, res, handleType); \ |
|
414 |
} else { \ |
|
415 |
(ret) = 0; \ |
|
416 |
break; \ |
|
417 |
} \ |
|
418 |
} else { \ |
|
419 |
READ((ret), f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
420 |
} \ |
|
421 |
} \ |
|
422 |
if ((ret) <= 0) { \ |
|
423 |
if (ret < 0 && __threadErrno == EINTR) \ |
|
424 |
continue; \ |
|
425 |
break; \ |
|
426 |
} \ |
|
427 |
__offs += (ret); \ |
|
428 |
} \ |
|
429 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
430 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
431 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
432 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
433 |
# define IO_BUFFER_SIZE (8*1024) |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
434 |
|
12548 | 435 |
# 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
|
436 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
437 |
int __ooffs = obj_offs; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
438 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
439 |
char *buf = (char *)(obj); \ |
18797 | 440 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
441 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
442 |
if (isBuffered) { \ |
18797 | 443 |
while (__offs < (cnt)) { \ |
444 |
CLEAR_ERRNO; \ |
|
445 |
(ret) = getc(f); \ |
|
446 |
if ((ret) < 0) { \ |
|
447 |
if (ferror(f)) { \ |
|
448 |
if (__threadErrno == EINTR) { \ |
|
449 |
clearerr(f); \ |
|
450 |
/* refetch */ \ |
|
451 |
buf = (char *)(obj); \ |
|
452 |
continue; \ |
|
453 |
} \ |
|
454 |
} else { \ |
|
455 |
(ret) = 0; \ |
|
456 |
} \ |
|
457 |
break; \ |
|
458 |
} \ |
|
459 |
(buf)[__ooffs+__offs] = (ret); \ |
|
460 |
__offs++; \ |
|
461 |
} \ |
|
462 |
if (__offs > 0) \ |
|
463 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
464 |
} else { \ |
18797 | 465 |
while (__offs < (cnt)) { \ |
466 |
char __buf[IO_BUFFER_SIZE]; \ |
|
467 |
OBJ rA = __INST(readAhead); \ |
|
468 |
if (rA != nil) { \ |
|
469 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
470 |
__INST(readAhead) = nil; \ |
|
471 |
(ret) = 1; \ |
|
472 |
} else { \ |
|
473 |
int l; \ |
|
474 |
CLEAR_ERRNO; \ |
|
475 |
l = (cnt)-__offs; \ |
|
476 |
if ( l > IO_BUFFER_SIZE) \ |
|
477 |
l = IO_BUFFER_SIZE; \ |
|
478 |
READ((ret),f, __buf, l, handleType); \ |
|
479 |
if ((ret) <= 0) { \ |
|
480 |
if ((ret) < 0 && __threadErrno == EINTR) { \ |
|
481 |
continue; \ |
|
482 |
} \ |
|
483 |
break; \ |
|
484 |
} \ |
|
485 |
} \ |
|
486 |
if ((ret) > 0 ) { \ |
|
487 |
/* refetch */ \ |
|
488 |
buf = (char *)(obj); \ |
|
489 |
memcpy((buf)+__ooffs+__offs,__buf,(ret)); \ |
|
490 |
__offs += (ret); \ |
|
491 |
} else { \ |
|
492 |
(ret) = 0; \ |
|
493 |
} \ |
|
494 |
} \ |
|
495 |
if (__offs > 0) \ |
|
496 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
497 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
498 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
499 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
500 |
# 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
|
501 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
502 |
int __ooffs = obj_offs; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
503 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
504 |
char *buf = (char *)(obj); \ |
18797 | 505 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
506 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
507 |
if (isBuffered) { \ |
18797 | 508 |
while (__offs < (cnt)) { \ |
509 |
CLEAR_ERRNO; \ |
|
510 |
(ret) = getc(f); \ |
|
511 |
if ((ret) < 0) { \ |
|
512 |
if (ferror(f)) { \ |
|
513 |
if (__threadErrno == EINTR) { \ |
|
514 |
clearerr(f); \ |
|
515 |
/* refetch */ \ |
|
516 |
buf = (char *)(obj);\ |
|
517 |
continue; \ |
|
518 |
} \ |
|
519 |
} else { \ |
|
520 |
(ret) = 0; \ |
|
521 |
} \ |
|
522 |
break; \ |
|
523 |
} \ |
|
524 |
(buf)[__ooffs+__offs] = (ret); \ |
|
525 |
__offs++; \ |
|
526 |
} \ |
|
527 |
if (__offs > 0) \ |
|
528 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
529 |
} else { \ |
18797 | 530 |
while (__offs < (cnt)) { \ |
531 |
char __buf[IO_BUFFER_SIZE]; \ |
|
532 |
OBJ rA = __INST(readAhead); \ |
|
533 |
if (rA != nil) { \ |
|
534 |
(buf)[__ooffs+__offs] = __intVal(rA);\ |
|
535 |
__INST(readAhead) = nil; \ |
|
536 |
(ret) = 1; \ |
|
537 |
__offs++; \ |
|
538 |
continue; \ |
|
539 |
} \ |
|
540 |
{ \ |
|
541 |
int res = 0; \ |
|
542 |
int l = (cnt)-__offs; \ |
|
543 |
CLEAR_ERRNO; \ |
|
544 |
if (l > IO_BUFFER_SIZE) \ |
|
545 |
l = IO_BUFFER_SIZE; \ |
|
546 |
if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)), FIONREAD, &res), 1)) \ |
|
547 |
|| (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \ |
|
548 |
|| (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0,0,0,&res,0), 1))) { \ |
|
549 |
if (res > 0) { \ |
|
550 |
if (res > l) res = l; \ |
|
551 |
READ((ret), f, __buf, res, handleType); \ |
|
552 |
} else { \ |
|
553 |
(ret) = 0; \ |
|
554 |
break; \ |
|
555 |
} \ |
|
556 |
} else { \ |
|
557 |
READ((ret), f, __buf, l, handleType); \ |
|
558 |
} \ |
|
559 |
if ((ret) <= 0) { \ |
|
560 |
if (ret < 0 && __threadErrno == EINTR) \ |
|
561 |
continue; \ |
|
562 |
break; \ |
|
563 |
} \ |
|
564 |
} \ |
|
565 |
if ((ret) > 0) { \ |
|
566 |
buf = (char *)(obj); \ |
|
567 |
memcpy((buf)+__ooffs+__offs, __buf, (ret)); \ |
|
568 |
__offs += (ret); \ |
|
569 |
} else { \ |
|
570 |
(ret) = 0; \ |
|
571 |
} \ |
|
572 |
} \ |
|
573 |
if (__offs > 0) \ |
|
574 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
575 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
576 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
577 |
|
12548 | 578 |
# define __WRITEBYTE__(ret, f, buf, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
579 |
if (isBuffered) { \ |
18797 | 580 |
for (;;) { \ |
581 |
CLEAR_ERRNO; \ |
|
582 |
ret = putc(*(buf), f); \ |
|
583 |
if ((ret) >= 0) { \ |
|
584 |
(ret) = 1; \ |
|
585 |
} else if (ferror(f)) { \ |
|
586 |
if (__threadErrno == EINTR) { \ |
|
587 |
clearerr(f); \ |
|
588 |
continue; \ |
|
589 |
} \ |
|
590 |
} else \ |
|
591 |
(ret) = 0; \ |
|
592 |
break; \ |
|
593 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
594 |
} else { \ |
18797 | 595 |
for (;;) { \ |
596 |
CLEAR_ERRNO; \ |
|
597 |
WRITE(ret,f, buf, 1, handleType); \ |
|
598 |
if ((ret) >= 0 || __threadErrno != EINTR) \ |
|
599 |
break; \ |
|
600 |
} \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
601 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
602 |
|
12548 | 603 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
604 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
605 |
if (isBuffered) { \ |
18797 | 606 |
int __offs = 0; \ |
607 |
while (__offs < (cnt)) { \ |
|
608 |
CLEAR_ERRNO; \ |
|
609 |
(ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\ |
|
610 |
if ((ret) <= 0) { \ |
|
611 |
if (ferror(f)) { \ |
|
612 |
if (__threadErrno == EINTR) { \ |
|
613 |
clearerr(f); \ |
|
614 |
continue; \ |
|
615 |
} \ |
|
616 |
break; \ |
|
617 |
} else { \ |
|
618 |
(ret) = 0; \ |
|
619 |
} \ |
|
620 |
} \ |
|
621 |
__offs += (ret); \ |
|
622 |
} \ |
|
623 |
if (__offs > 0) \ |
|
624 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
625 |
} else { \ |
18797 | 626 |
int __offs = 0; \ |
627 |
while (__offs < (cnt)) { \ |
|
628 |
CLEAR_ERRNO; \ |
|
629 |
WRITE(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
630 |
if (ret <= 0) { \ |
|
631 |
if (ret < 0 && __threadErrno == EINTR) { \ |
|
632 |
continue; \ |
|
633 |
} \ |
|
634 |
break; \ |
|
635 |
} \ |
|
636 |
__offs += (ret); \ |
|
637 |
} \ |
|
638 |
if (__offs > 0) \ |
|
639 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
640 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
641 |
|
12548 | 642 |
# 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
|
643 |
{ \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
644 |
int __ooffs = obj_offs; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
645 |
int __offs = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
646 |
char *buf = (char *)(obj); \ |
18797 | 647 |
\ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
648 |
(ret) = 0; \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
649 |
if (isBuffered) { \ |
18797 | 650 |
while (__offs < (cnt)) { \ |
651 |
CLEAR_ERRNO; \ |
|
652 |
ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \ |
|
653 |
if ((ret) <= 0) { \ |
|
654 |
if (ferror(f)) { \ |
|
655 |
if (__threadErrno == EINTR) { \ |
|
656 |
/* refetch */ \ |
|
657 |
buf = (char *)(obj); \ |
|
658 |
clearerr(f); \ |
|
659 |
continue; \ |
|
660 |
} \ |
|
661 |
break; \ |
|
662 |
} else { \ |
|
663 |
(ret) = 0; \ |
|
664 |
} \ |
|
665 |
} \ |
|
666 |
__offs += (ret); \ |
|
667 |
} \ |
|
668 |
if (__offs > 0) \ |
|
669 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
670 |
} else { \ |
18797 | 671 |
while (__offs < (cnt)) { \ |
672 |
char __buf[IO_BUFFER_SIZE]; \ |
|
673 |
int l; \ |
|
674 |
CLEAR_ERRNO; \ |
|
675 |
l = (cnt)-__offs; \ |
|
676 |
if ( l > IO_BUFFER_SIZE) \ |
|
677 |
l = IO_BUFFER_SIZE; \ |
|
678 |
/* refetch */ \ |
|
679 |
buf = (char *)(obj); \ |
|
680 |
memcpy(__buf,(buf)+__ooffs+__offs,l); \ |
|
681 |
WRITE(ret,f, __buf, l, handleType); \ |
|
682 |
if (ret <= 0) { \ |
|
683 |
if (ret < 0 && __threadErrno == EINTR) { \ |
|
684 |
continue; \ |
|
685 |
} \ |
|
686 |
break; \ |
|
687 |
} \ |
|
688 |
__offs += (ret); \ |
|
689 |
} \ |
|
690 |
if (__offs > 0) \ |
|
691 |
(ret) = __offs; \ |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
692 |
} \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
693 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
694 |
|
16296 | 695 |
#else /* ! WIN32 */ |
696 |
/* ======================== UNIX / LINUX ====================================================== */ |
|
16373
9b9827530bf4
Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents:
16368
diff
changeset
|
697 |
typedef int SOCKET; |
9b9827530bf4
Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents:
16368
diff
changeset
|
698 |
|
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
699 |
# define __READING__(f) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
700 |
if ((__INST(didWrite) != false) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
701 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
18797 | 702 |
__INST(didWrite) = false; \ |
703 |
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
|
704 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
705 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
706 |
# define __WRITING__(f) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
707 |
if ((__INST(didWrite) != true) \ |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
708 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
18797 | 709 |
__INST(didWrite) = true; \ |
710 |
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
|
711 |
} |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
712 |
|
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 |
# ifdef NO_STDIO |
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
715 |
# define __UNGETC__(c, f, isBuffered) \ |
8913 | 716 |
__INST(readAhead) = __mkSmallInteger((c)); |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
717 |
# else /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
718 |
# define __UNGETC__(c, f, isBuffered) \ |
2264 | 719 |
if (isBuffered) { \ |
18797 | 720 |
ungetc((c), (f)); \ |
2264 | 721 |
} else { \ |
18797 | 722 |
__INST(readAhead) = __mkSmallInteger((c)); \ |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
723 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
724 |
# endif /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
725 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
726 |
# ifdef NO_STDIO |
12548 | 727 |
# 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
|
728 |
{ \ |
18797 | 729 |
OBJ rA = __INST(readAhead); \ |
730 |
if (rA != nil) { \ |
|
731 |
*(buf) = __intVal(rA); \ |
|
732 |
DEBUGBUFFER(buf); \ |
|
733 |
__INST(readAhead) = nil; \ |
|
734 |
(ret) = 1; \ |
|
735 |
} else { \ |
|
736 |
for (;;) { \ |
|
737 |
CLEAR_ERRNO; \ |
|
738 |
(ret) = READ(f, buf, 1, handleType); \ |
|
739 |
DEBUGBUFFER(buf); \ |
|
740 |
if ((ret) >= 0) break; \ |
|
741 |
if (errno != EINTR) { \ |
|
742 |
break; \ |
|
743 |
} \ |
|
744 |
__HANDLE_INTERRUPTS__; \ |
|
745 |
} \ |
|
746 |
} \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
747 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
748 |
# else /* use STDIO */ |
12548 | 749 |
# define __READBYTE__(ret, f, buf, isBuffered, handleType) \ |
2264 | 750 |
if (isBuffered) { \ |
18797 | 751 |
for (;;) { \ |
752 |
CLEAR_ERRNO; \ |
|
753 |
(ret) = getc(f); \ |
|
754 |
if ((ret) >= 0) { \ |
|
755 |
DEBUGBUFFER(buf); \ |
|
756 |
*(buf) = (ret); \ |
|
757 |
(ret) = 1; \ |
|
758 |
} else if (ferror(f)) { \ |
|
759 |
if (errno == EINTR) { \ |
|
760 |
__HANDLE_INTERRUPTS__; \ |
|
761 |
clearerr(f); \ |
|
762 |
continue; \ |
|
763 |
} \ |
|
764 |
} else \ |
|
765 |
(ret) = 0; \ |
|
766 |
break; \ |
|
767 |
} \ |
|
2264 | 768 |
} else { \ |
18797 | 769 |
OBJ rA = __INST(readAhead); \ |
770 |
if (rA != nil) { \ |
|
771 |
*(buf) = __intVal(rA); \ |
|
772 |
DEBUGBUFFER(buf); \ |
|
773 |
__INST(readAhead) = nil; \ |
|
774 |
(ret) = 1; \ |
|
775 |
} else { \ |
|
776 |
for (;;) { \ |
|
777 |
CLEAR_ERRNO; \ |
|
778 |
(ret) = read(fileno(f), buf, 1); \ |
|
779 |
DEBUGBUFFER(buf); \ |
|
780 |
if ((ret) >= 0) break; \ |
|
781 |
if (errno != EINTR) { \ |
|
782 |
break; \ |
|
783 |
} \ |
|
784 |
__HANDLE_INTERRUPTS__; \ |
|
785 |
} \ |
|
786 |
} \ |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
787 |
} |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
788 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
789 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
790 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
791 |
* 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
|
792 |
* (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
|
793 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
794 |
# ifdef NO_STDIO |
12548 | 795 |
# 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
|
796 |
{ \ |
18797 | 797 |
int __offs = 0, __cnt; \ |
798 |
\ |
|
799 |
while (__offs < (cnt)) { \ |
|
800 |
OBJ rA = __INST(readAhead); \ |
|
801 |
if (rA != nil) { \ |
|
802 |
(buf)[__offs] = __intVal(rA); \ |
|
803 |
DEBUGBUFFER(buf); \ |
|
804 |
__INST(readAhead) = nil; \ |
|
805 |
__offs++; \ |
|
806 |
} else { \ |
|
807 |
CLEAR_ERRNO; \ |
|
808 |
__cnt = READ(f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
809 |
DEBUGBUFFER(buf); \ |
|
810 |
if (__cnt <= 0) { \ |
|
811 |
if (__cnt < 0 && errno == EINTR) { \ |
|
812 |
__HANDLE_INTERRUPTS__; \ |
|
813 |
continue; \ |
|
814 |
} \ |
|
815 |
break; \ |
|
816 |
} \ |
|
817 |
__offs += __cnt; \ |
|
818 |
} \ |
|
819 |
} \ |
|
820 |
if (__offs > 0) \ |
|
821 |
(ret) = __offs; \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
822 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
823 |
# else /* use STDIO */ |
12548 | 824 |
# 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
|
825 |
(ret) = 0; \ |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
826 |
if (isBuffered) { \ |
18797 | 827 |
int __offs = 0; \ |
828 |
while (__offs < (cnt)) { \ |
|
829 |
CLEAR_ERRNO; \ |
|
830 |
(ret) = getc(f); \ |
|
831 |
if ((ret) < 0) { \ |
|
832 |
if (ferror(f)) { \ |
|
833 |
if (errno == EINTR) { \ |
|
834 |
__HANDLE_INTERRUPTS__; \ |
|
835 |
clearerr(f); \ |
|
836 |
continue; \ |
|
837 |
} \ |
|
838 |
} else { \ |
|
839 |
(ret) = 0; \ |
|
840 |
} \ |
|
841 |
break; \ |
|
842 |
} \ |
|
843 |
DEBUGBUFFER(buf); \ |
|
844 |
(buf)[__offs++] = (ret); \ |
|
845 |
} \ |
|
846 |
if (__offs > 0) \ |
|
847 |
(ret) = __offs; \ |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
848 |
} else { \ |
18797 | 849 |
int __offs = 0, __cnt; \ |
850 |
int fd = fileno(f); \ |
|
851 |
\ |
|
852 |
while (__offs < (cnt)) { \ |
|
853 |
OBJ rA = __INST(readAhead); \ |
|
854 |
if (rA != nil) { \ |
|
855 |
DEBUGBUFFER(buf); \ |
|
856 |
(buf)[__offs] = __intVal(rA); \ |
|
857 |
__INST(readAhead) = nil; \ |
|
858 |
__offs++; \ |
|
859 |
} else { \ |
|
860 |
CLEAR_ERRNO; \ |
|
861 |
__cnt = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
862 |
DEBUGBUFFER(buf); \ |
|
863 |
if (__cnt <= 0) { \ |
|
864 |
if (__cnt < 0 && errno == EINTR) { \ |
|
865 |
__HANDLE_INTERRUPTS__; \ |
|
866 |
continue; \ |
|
867 |
} \ |
|
868 |
break; \ |
|
869 |
} \ |
|
870 |
__offs += __cnt; \ |
|
871 |
} \ |
|
872 |
} \ |
|
873 |
if (__offs > 0) \ |
|
874 |
(ret) = __offs; \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
875 |
} |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
876 |
|
6563
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
877 |
|
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
878 |
/* |
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
879 |
* 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
|
880 |
*/ |
f786651428c8
Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents:
6487
diff
changeset
|
881 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
882 |
# 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
|
883 |
# define SETFLAGS(fd, flags) \ |
18797 | 884 |
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
|
885 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
886 |
# if defined(O_NONBLOCK) |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
887 |
# define __STX_NONBLOCK_FLAG O_NONBLOCK |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
888 |
# else |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
889 |
# if defined(O_NDELAY) |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
890 |
# define __STX_NONBLOCK_FLAG O_NDELAY |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
891 |
# else |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
892 |
# define __STX_NONBLOCK_FLAG FNDELAY |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
893 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
894 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
895 |
|
15342 | 896 |
# define SETNONBLOCKING(fd, oldFlags) \ |
18797 | 897 |
{ \ |
898 |
int flags = fcntl(fd, F_GETFL, 0); \ |
|
899 |
if (flags >= 0) { \ |
|
900 |
fcntl(fd, F_SETFL, flags | __STX_NONBLOCK_FLAG); \ |
|
901 |
} \ |
|
902 |
oldFlags = flags; \ |
|
903 |
} |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
904 |
# else |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
905 |
# define SETFLAGS(fd, flags) /* nothing */ |
15342 | 906 |
# define SETNONBLOCKING(fd, oldFlags) /* nothing */ |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
907 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
908 |
|
12548 | 909 |
# define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
910 |
{ \ |
15342 | 911 |
int __offs = 0, __cnt; \ |
3681 | 912 |
int oldFlags; \ |
18797 | 913 |
\ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
914 |
(ret) = 0; \ |
15342 | 915 |
SETNONBLOCKING(fileno(f), oldFlags); \ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
916 |
if (isBuffered) { \ |
18797 | 917 |
while (__offs < (cnt)) { \ |
918 |
CLEAR_ERRNO; \ |
|
919 |
(ret) = getc(f); \ |
|
920 |
if ((ret) < 0) { \ |
|
921 |
if (ferror(f)) { \ |
|
922 |
if (errno == EINTR) { \ |
|
923 |
(ret) = 0; \ |
|
924 |
clearerr(f); \ |
|
925 |
break; \ |
|
926 |
} \ |
|
927 |
} else { \ |
|
928 |
(ret) = 0; \ |
|
929 |
} \ |
|
930 |
break; \ |
|
931 |
} \ |
|
932 |
(buf)[__offs++] = (ret); \ |
|
933 |
DEBUGBUFFER(buf); \ |
|
934 |
} \ |
|
935 |
if (__offs > 0) \ |
|
936 |
(ret) = __offs; \ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
937 |
} else { \ |
18797 | 938 |
int fd = fileno(f); \ |
939 |
\ |
|
940 |
while (__offs < (cnt)) { \ |
|
941 |
OBJ rA = __INST(readAhead); \ |
|
942 |
if (rA != nil) { \ |
|
943 |
(buf)[__offs] = __intVal(rA); \ |
|
944 |
DEBUGBUFFER(buf); \ |
|
945 |
__INST(readAhead) = nil; \ |
|
946 |
__offs++; \ |
|
947 |
continue; \ |
|
948 |
} \ |
|
949 |
CLEAR_ERRNO; \ |
|
950 |
__cnt = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
951 |
DEBUGBUFFER(buf); \ |
|
952 |
if (__cnt > 0) { \ |
|
953 |
__offs += __cnt; \ |
|
954 |
} \ |
|
955 |
break; \ |
|
956 |
} \ |
|
957 |
if (__offs > 0) \ |
|
958 |
(ret) = __offs; \ |
|
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
959 |
} \ |
3681 | 960 |
SETFLAGS(fileno(f), oldFlags); \ |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
961 |
} |
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
962 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
963 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
964 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
965 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
966 |
* 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
|
967 |
* (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
|
968 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
969 |
# ifdef NO_STDIO |
12548 | 970 |
# 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
|
971 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
972 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
973 |
int __offs = 0; \ |
7540
7cd542f29024
Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents:
7503
diff
changeset
|
974 |
int __cnt; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
975 |
char *buf = (char *)(obj); \ |
18797 | 976 |
\ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
977 |
(ret) = 0; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
978 |
{ \ |
18797 | 979 |
while (__offs < (cnt)) { \ |
980 |
OBJ rA = __INST(readAhead); \ |
|
981 |
if (rA != nil) { \ |
|
982 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
983 |
DEBUGBUFFER(buf); \ |
|
984 |
__INST(readAhead) = nil; \ |
|
985 |
__offs++; \ |
|
986 |
} else { \ |
|
987 |
CLEAR_ERRNO; \ |
|
988 |
__cnt = READ(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \ |
|
989 |
DEBUGBUFFER(buf); \ |
|
990 |
if (__cnt <= 0) { \ |
|
991 |
if (__cnt < 0 && errno == EINTR) { \ |
|
992 |
__HANDLE_INTERRUPTS__; \ |
|
993 |
/* refetch */ \ |
|
994 |
buf = (char *)(obj); \ |
|
995 |
continue; \ |
|
996 |
} \ |
|
997 |
break; \ |
|
998 |
} \ |
|
999 |
__offs += __cnt; \ |
|
1000 |
} \ |
|
1001 |
} \ |
|
1002 |
if (__offs > 0) \ |
|
1003 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1004 |
} \ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1005 |
} |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
1006 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1007 |
# else /* use STDIO */ |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
1008 |
|
12548 | 1009 |
# 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
|
1010 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1011 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1012 |
int __offs = 0; \ |
7540
7cd542f29024
Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents:
7503
diff
changeset
|
1013 |
int __cnt; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1014 |
char *buf = (char *)(obj); \ |
18797 | 1015 |
\ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1016 |
(ret) = 0; \ |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1017 |
if (isBuffered) { \ |
18797 | 1018 |
while (__offs < (cnt)) { \ |
1019 |
CLEAR_ERRNO; \ |
|
1020 |
(ret) = getc(f); \ |
|
1021 |
if ((ret) < 0) { \ |
|
1022 |
if (ferror(f)) { \ |
|
1023 |
if (errno == EINTR) { \ |
|
1024 |
__HANDLE_INTERRUPTS__; \ |
|
1025 |
clearerr(f); \ |
|
1026 |
/* refetch */ \ |
|
1027 |
buf = (char *)(obj); \ |
|
1028 |
DEBUGBUFFER(buf); \ |
|
1029 |
continue; \ |
|
1030 |
} \ |
|
1031 |
} else { \ |
|
1032 |
(ret) = 0; \ |
|
1033 |
} \ |
|
1034 |
break; \ |
|
1035 |
} \ |
|
1036 |
(buf)[__ooffs+__offs] = (ret); \ |
|
1037 |
DEBUGBUFFER(buf); \ |
|
1038 |
__offs++; \ |
|
1039 |
} \ |
|
1040 |
if (__offs > 0) \ |
|
1041 |
(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
|
1042 |
} else { \ |
18797 | 1043 |
int fd = fileno(f); \ |
1044 |
\ |
|
1045 |
while (__offs < (cnt)) { \ |
|
1046 |
OBJ rA = __INST(readAhead); \ |
|
1047 |
if (rA != nil) { \ |
|
1048 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
1049 |
DEBUGBUFFER(buf); \ |
|
1050 |
__INST(readAhead) = nil; \ |
|
1051 |
__offs++; \ |
|
1052 |
} else { \ |
|
1053 |
CLEAR_ERRNO; \ |
|
1054 |
__cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
1055 |
DEBUGBUFFER(buf); \ |
|
1056 |
if (__cnt <= 0) { \ |
|
1057 |
if (__cnt < 0 && errno == EINTR) { \ |
|
1058 |
__HANDLE_INTERRUPTS__; \ |
|
1059 |
/* refetch */ \ |
|
1060 |
buf = (char *)(obj); \ |
|
1061 |
continue; \ |
|
1062 |
} \ |
|
1063 |
break; \ |
|
1064 |
} \ |
|
1065 |
__offs += __cnt; \ |
|
1066 |
} \ |
|
1067 |
} \ |
|
1068 |
if (__offs > 0) \ |
|
1069 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1070 |
} \ |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1071 |
} |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1072 |
|
12548 | 1073 |
# 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
|
1074 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1075 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1076 |
int __offs = 0; \ |
7540
7cd542f29024
Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents:
7503
diff
changeset
|
1077 |
int __cnt; \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1078 |
char *buf = (char *)(obj); \ |
15342 | 1079 |
int oldFlags; \ |
18797 | 1080 |
\ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1081 |
(ret) = 0; \ |
15342 | 1082 |
SETNONBLOCKING(fileno(f), oldFlags); \ |
18797 | 1083 |
\ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1084 |
if (isBuffered) { \ |
18797 | 1085 |
while (__offs < (cnt)) { \ |
1086 |
CLEAR_ERRNO; \ |
|
1087 |
(ret) = getc(f); \ |
|
1088 |
if ((ret) < 0) { \ |
|
1089 |
if (ferror(f)) { \ |
|
1090 |
if (errno == EINTR) { \ |
|
1091 |
clearerr(f); \ |
|
1092 |
/* refetch */ \ |
|
1093 |
buf = (char *)(obj); \ |
|
1094 |
(ret) = 0; \ |
|
1095 |
break; \ |
|
1096 |
} \ |
|
1097 |
} else { \ |
|
1098 |
(ret) = 0; \ |
|
1099 |
} \ |
|
1100 |
break; \ |
|
1101 |
} \ |
|
1102 |
(buf)[__ooffs+__offs] = (ret); \ |
|
1103 |
DEBUGBUFFER(buf); \ |
|
1104 |
__offs++; \ |
|
1105 |
} \ |
|
1106 |
if (__offs > 0) \ |
|
1107 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1108 |
} else { \ |
18797 | 1109 |
int fd = fileno(f); \ |
1110 |
\ |
|
1111 |
while (__offs < (cnt)) { \ |
|
1112 |
OBJ rA = __INST(readAhead); \ |
|
1113 |
if (rA != nil) { \ |
|
1114 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
1115 |
DEBUGBUFFER(buf); \ |
|
1116 |
__INST(readAhead) = nil; \ |
|
1117 |
__offs++; \ |
|
1118 |
continue; \ |
|
1119 |
} \ |
|
1120 |
CLEAR_ERRNO; \ |
|
1121 |
__cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
1122 |
DEBUGBUFFER(buf); \ |
|
1123 |
if (__cnt > 0) { \ |
|
1124 |
__offs += __cnt; \ |
|
1125 |
} \ |
|
1126 |
break; \ |
|
1127 |
} \ |
|
1128 |
if (__offs > 0) \ |
|
1129 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1130 |
} \ |
15342 | 1131 |
SETFLAGS(fileno(f), oldFlags); \ |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1132 |
} |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1133 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1134 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1135 |
# endif /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1136 |
|
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1137 |
# ifdef NO_STDIO |
12548 | 1138 |
# define __WRITEBYTE__(ret, f, buf, isBuffered, handleType) \ |
18797 | 1139 |
for (;;) { \ |
1140 |
CLEAR_ERRNO; \ |
|
1141 |
(ret) = WRITE(f, buf, 1, handleType); \ |
|
1142 |
if ((ret) >= 0) break; \ |
|
1143 |
if (errno != EINTR) { \ |
|
1144 |
break; \ |
|
1145 |
} \ |
|
1146 |
__HANDLE_INTERRUPTS__; \ |
|
1147 |
} |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1148 |
# else /* use STDIO */ |
12548 | 1149 |
# 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
|
1150 |
if (isBuffered) { \ |
18797 | 1151 |
for (;;) { \ |
1152 |
CLEAR_ERRNO; \ |
|
1153 |
ret = putc(*(buf), f); \ |
|
1154 |
if ((ret) >= 0) { \ |
|
1155 |
(ret) = 1; \ |
|
1156 |
} else if (ferror(f)) { \ |
|
1157 |
/* SOLARIS/SPARC (2.6) generates spurious errors with errno = 0 */ \ |
|
1158 |
if (errno == EINTR || errno == 0) { \ |
|
1159 |
__HANDLE_INTERRUPTS__; \ |
|
1160 |
clearerr(f); \ |
|
1161 |
continue; \ |
|
1162 |
} \ |
|
1163 |
} else \ |
|
1164 |
(ret) = 0; \ |
|
1165 |
break; \ |
|
1166 |
} \ |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
1167 |
} else { \ |
18797 | 1168 |
for (;;) { \ |
1169 |
CLEAR_ERRNO; \ |
|
1170 |
(ret) = write(fileno(f), buf, 1); \ |
|
1171 |
if ((ret) >= 0) break; \ |
|
1172 |
if (errno != EINTR) { \ |
|
1173 |
break; \ |
|
1174 |
} \ |
|
1175 |
__HANDLE_INTERRUPTS__; \ |
|
1176 |
} \ |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1177 |
} |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1178 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1179 |
|
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1180 |
/* |
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1181 |
* 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
|
1182 |
* (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
|
1183 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1184 |
# ifdef NO_STDIO |
12548 | 1185 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
2961 | 1186 |
(ret) = 0; \ |
1187 |
{ \ |
|
18797 | 1188 |
int __offs = 0; \ |
1189 |
while (__offs < (cnt)) { \ |
|
1190 |
CLEAR_ERRNO; \ |
|
1191 |
ret = WRITE(f, (buf)+__offs, (cnt)-__offs, handleType); \ |
|
1192 |
if (ret <= 0) { \ |
|
1193 |
if (ret < 0 && errno == EINTR) { \ |
|
1194 |
__HANDLE_INTERRUPTS__; \ |
|
1195 |
continue; \ |
|
1196 |
} \ |
|
1197 |
break; \ |
|
1198 |
} \ |
|
1199 |
__offs += (ret); \ |
|
1200 |
} \ |
|
1201 |
if (__offs > 0) \ |
|
1202 |
(ret) = __offs; \ |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1203 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1204 |
# else /* use STDIO */ |
12548 | 1205 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType) \ |
2961 | 1206 |
(ret) = 0; \ |
1207 |
if (isBuffered) { \ |
|
18797 | 1208 |
int __offs = 0; \ |
1209 |
while (__offs < (cnt)) { \ |
|
1210 |
CLEAR_ERRNO; \ |
|
1211 |
(ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\ |
|
1212 |
if ((ret) <= 0) { \ |
|
1213 |
if (ferror(f)) { \ |
|
1214 |
if (errno == EINTR) { \ |
|
1215 |
__HANDLE_INTERRUPTS__; \ |
|
1216 |
clearerr(f); \ |
|
1217 |
continue; \ |
|
1218 |
} \ |
|
1219 |
} else { \ |
|
1220 |
(ret) = 0; \ |
|
1221 |
} \ |
|
1222 |
break; \ |
|
1223 |
} \ |
|
1224 |
__offs += (ret); \ |
|
1225 |
} \ |
|
1226 |
if (__offs > 0) \ |
|
1227 |
(ret) = __offs; \ |
|
2961 | 1228 |
} else { \ |
18797 | 1229 |
int __offs = 0; \ |
1230 |
while (__offs < (cnt)) { \ |
|
1231 |
CLEAR_ERRNO; \ |
|
1232 |
(ret) = write(fileno(f), (buf)+__offs, (cnt)-__offs);\ |
|
1233 |
if ((ret) <= 0) { \ |
|
1234 |
if ((ret) < 0) { \ |
|
1235 |
if (errno == EINTR) { \ |
|
1236 |
__HANDLE_INTERRUPTS__; \ |
|
1237 |
continue; \ |
|
1238 |
} \ |
|
1239 |
} \ |
|
1240 |
break; \ |
|
1241 |
} \ |
|
1242 |
__offs += (ret); \ |
|
1243 |
} \ |
|
1244 |
if (__offs > 0) \ |
|
1245 |
(ret) = __offs; \ |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1246 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1247 |
# endif /* use STDIO */ |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1248 |
|
223 | 1249 |
/* |
2513
a589586a3828
big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents:
2331
diff
changeset
|
1250 |
* 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
|
1251 |
* (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
|
1252 |
*/ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1253 |
# ifdef NO_STDIO |
12548 | 1254 |
# 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
|
1255 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1256 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1257 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1258 |
char *buf = (char *)(obj); \ |
18797 | 1259 |
\ |
2961 | 1260 |
(ret) = 0; \ |
1261 |
{ \ |
|
18797 | 1262 |
while (__offs < (cnt)) { \ |
1263 |
CLEAR_ERRNO; \ |
|
1264 |
ret = WRITE(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \ |
|
1265 |
if (ret <= 0) { \ |
|
1266 |
if (ret < 0 && errno == EINTR) { \ |
|
1267 |
__HANDLE_INTERRUPTS__; \ |
|
1268 |
/* refetch */ \ |
|
1269 |
buf = (char *)(obj); \ |
|
1270 |
continue; \ |
|
1271 |
} \ |
|
1272 |
break; \ |
|
1273 |
} \ |
|
1274 |
__offs += (ret); \ |
|
1275 |
} \ |
|
1276 |
if (__offs > 0) \ |
|
1277 |
(ret) = __offs; \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1278 |
} \ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
1279 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1280 |
# else /* use STDIO */ |
12548 | 1281 |
# 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
|
1282 |
{ \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1283 |
int __ooffs = obj_offs; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1284 |
int __offs = 0; \ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1285 |
char *buf = (char *)(obj); \ |
18797 | 1286 |
\ |
2961 | 1287 |
(ret) = 0; \ |
1288 |
if (isBuffered) { \ |
|
18797 | 1289 |
while (__offs < (cnt)) { \ |
1290 |
CLEAR_ERRNO; \ |
|
1291 |
(ret) = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \ |
|
1292 |
if ((ret) <= 0) { \ |
|
1293 |
if (ferror(f)) { \ |
|
1294 |
if (errno == EINTR) { \ |
|
1295 |
__HANDLE_INTERRUPTS__; \ |
|
1296 |
/* refetch */ \ |
|
1297 |
buf = (char *)(obj); \ |
|
1298 |
clearerr(f); \ |
|
1299 |
continue; \ |
|
1300 |
} \ |
|
1301 |
break; \ |
|
1302 |
} else { \ |
|
1303 |
(ret) = 0; \ |
|
1304 |
} \ |
|
1305 |
} \ |
|
1306 |
__offs += (ret); \ |
|
1307 |
} \ |
|
2961 | 1308 |
} else { \ |
18797 | 1309 |
while (__offs < (cnt)) { \ |
1310 |
CLEAR_ERRNO; \ |
|
1311 |
(ret) = write(fileno(f), (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
1312 |
if ((ret) <= 0) { \ |
|
1313 |
if ((ret) < 0) { \ |
|
1314 |
if (errno == EINTR){ \ |
|
1315 |
__HANDLE_INTERRUPTS__; \ |
|
1316 |
/* refetch */ \ |
|
1317 |
buf = (char *)(obj); \ |
|
1318 |
continue; \ |
|
1319 |
} \ |
|
1320 |
} \ |
|
1321 |
break; \ |
|
1322 |
} \ |
|
1323 |
__offs += (ret); \ |
|
1324 |
} \ |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
1325 |
} \ |
15845 | 1326 |
if (__offs > 0) \ |
18797 | 1327 |
(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
|
1328 |
} |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1329 |
# endif /* use STDIO */ |
4913
4964b2c71d31
remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents:
4640
diff
changeset
|
1330 |
#endif /* unix */ |
1 | 1331 |
%} |
188 | 1332 |
! ! |
1 | 1333 |
|
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1334 |
!ExternalStream class methodsFor:'documentation'! |
22 | 1335 |
|
88 | 1336 |
copyright |
1337 |
" |
|
1338 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
18797 | 1339 |
All Rights Reserved |
88 | 1340 |
|
1341 |
This software is furnished under a license and may be used |
|
1342 |
only in accordance with the terms of that license and with the |
|
1343 |
inclusion of the above copyright notice. This software may not |
|
1344 |
be provided or otherwise made available to, or used by, any |
|
1345 |
other person. No title to or ownership of the software is |
|
1346 |
hereby transferred. |
|
1347 |
" |
|
1348 |
! |
|
1349 |
||
22 | 1350 |
documentation |
1351 |
" |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1352 |
ExternalStream defines protocol common to Streams which have a file-descriptor and |
68 | 1353 |
represent some file or communicationChannel of the underlying OperatingSystem. |
1354 |
ExternalStream is abstract; concrete classes are FileStream, PipeStream etc. |
|
77 | 1355 |
|
159 | 1356 |
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
|
1357 |
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
|
1358 |
while in binary-mode the basic elements are bytes which read/write as SmallIntegers |
68 | 1359 |
in the range 0..255. |
22 | 1360 |
|
68 | 1361 |
Also, the stream can be either in buffered or unbuffered mode. In buffered mode, |
1362 |
data is not written until either a cr is written (in text mode) or a synchronizeOutput |
|
1363 |
is sent (in both modes). |
|
22 | 1364 |
|
77 | 1365 |
The underlying OperatingSystem streams may either be closed explicitely (sending a close) |
68 | 1366 |
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
|
1367 |
object AND a close will be performed automatically (but you will NOT know when this |
77 | 1368 |
happens - so it is recommended, that you close your files when no longer needed). |
159 | 1369 |
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
|
1370 |
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
|
1371 |
All streams understand the close message, so it never hurts to use it (it is defined as |
77 | 1372 |
a noop in one of the superclasses). |
22 | 1373 |
|
68 | 1374 |
Most of the methods found here redefine inherited methods for better performance, |
1375 |
since I/O from/to files should be fast. |
|
22 | 1376 |
|
68 | 1377 |
Recovering a snapshot: |
1295 | 1378 |
not all streams can be restored to the state they had before - see the implementation of |
1379 |
reOpen in subclasses for more information. |
|
1380 |
For streams sitting on some communication channel (i.e. Pipes and Sockets) you should |
|
1381 |
reestablish the stream upon image restart (make someone dependent on ObjectMemory). |
|
1382 |
FileStreams are reopened and positioned to their offset they had at snapshot time. |
|
1383 |
This may fail, if the file was removed or renamed - or lead to confusion |
|
1384 |
if the contents changed in the meantime. |
|
1385 |
Therefore, it is a good idea to reopen files and check for these things at restart time. |
|
1386 |
||
1387 |
[Instance variables:] |
|
1388 |
||
18797 | 1389 |
handleType <Symbol> desribes what handle is: |
1390 |
win32: #fileHandle, #socketHandle, |
|
1391 |
#filePointer, #socketFilePointer, #pipeFilePointer |
|
1392 |
unix: #filePointer, #socketFilePointer, #pipeFilePointer |
|
1393 |
needed for win32, which uses different APIs for the different handles (sigh) |
|
1394 |
handle <Integer> used to be always a filePointer somehow mapped to an integer (FILE* - not the fd); |
|
1395 |
now, either a filePointer or a handle (win32) |
|
1396 |
mode <Symbol> #readwrite, #readonly or #writeonly |
|
1397 |
buffered <Boolean> true, if buffered (i.e. collects characters - does |
|
1398 |
not output immediately) |
|
1399 |
binary <Boolean> true if in binary mode (reads bytes instead of chars) |
|
1400 |
eolMode <Symbol> one of nil, #nl, #cr or #crlf. |
|
1401 |
determines how lines should be terminated. |
|
1402 |
nil -> newLine (as in Unix); |
|
1403 |
#crlf -> with cr-lf (as in MSDOS) |
|
1404 |
#cr -> with cr (as in VMS) |
|
1405 |
hitEOF <Boolean> true, if EOF was reached |
|
1406 |
||
1407 |
lastErrorNumber <Integer> the value of errno (only valid right after the error - |
|
1408 |
updated with next i/o operation) |
|
1295 | 1409 |
|
1410 |
[Class variables:] |
|
18797 | 1411 |
Lobby <Registry> keeps track of used ext-streams (to free up FILE*'s) |
1412 |
||
1413 |
StreamErrorSignal <Signal> parent of all stream errors (see Stream class) |
|
1414 |
InvalidReadSignal <Signal> raised on read from writeonly stream |
|
1415 |
InvalidWriteSignal <Signal> raised on write to readonly stream |
|
1416 |
InvalidModeSignal <Signal> raised on text I/O with binary-stream |
|
1417 |
or binary I/O with text-stream |
|
1418 |
OpenErrorSignal <Signal> raised if open fails |
|
1419 |
StreamNotOpenSignal <Signal> raised on I/O with non-open stream |
|
159 | 1420 |
|
1421 |
Additional notes: |
|
1422 |
This class is implemented using the underlying stdio-c library package, which |
|
1423 |
has both advantages and disadvantages: since it is portable (posix defined), porting |
|
1424 |
ST/X to non-Unix machines is simplified. The disadvantage is that the stdio library |
|
1425 |
has big problems handling unbounded Streams, since the EOF handling in stdio is |
|
1426 |
not prepared for data to arrive after EOF has been reached - time will show, if we need |
|
1427 |
a complete rewrite for UnboundedStream ... |
|
325 | 1428 |
|
188 | 1429 |
Also, depending on the system, the stdio library behaves infriendly when signals |
1430 |
occur while reading (for example, timer interrupts) - on real unixes (i.e. BSD) the signal |
|
1431 |
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
|
1432 |
an error and errno is set to EINTR. |
1295 | 1433 |
Thats what the ugly code around all getc-calls is for ... |
1434 |
Since things get more and more ugly - we will rewrite ExternalStream |
|
1435 |
completely, to NOT use any stdio stuff (and do its buffering itself). |
|
325 | 1436 |
|
1437 |
Notice that typical stdio's use a single errno global variable to return an error code, |
|
16508 | 1438 |
this was bad design in the stdio lib (right from the very beginning), since it's much |
325 | 1439 |
harder to deal with this in the presence of lightweight processes, where errno gets |
1440 |
overwritten by an I/O operation done in another thread. (stdio should have been written |
|
1441 |
to return errno as a negative number ...). |
|
1442 |
To deal with this, the scheduler treats errno like a per-thread private variable, |
|
1443 |
and saves/restores the errno setting when switching to another thread. |
|
1444 |
(Notice that some thread packages do this also, but ST/X's thread implementation |
|
1445 |
does not depend on those, but instead uses a portable private package). |
|
1446 |
||
216 | 1447 |
Finally, if an stdio-stream is open for both reading and writing, we have to call |
1448 |
fseek whenever we are about to read after write and vice versa. |
|
1449 |
Two macros (__READING__ and __WRITING__) have been defined to be used before every |
|
1450 |
fread/fgetc and fwrite/putc respectively. |
|
1295 | 1451 |
|
1452 |
[author:] |
|
18797 | 1453 |
Claus Gittinger |
1454 |
Stefan Vogel (many, many fixes ...) |
|
1295 | 1455 |
|
1456 |
[see also:] |
|
18797 | 1457 |
FileStream Socket PipeStream |
1458 |
Filename OperatingSystem |
|
22 | 1459 |
" |
3255 | 1460 |
! |
1461 |
||
1462 |
examples |
|
1463 |
" |
|
1464 |
open a file, read the contents and display it in a textView: |
|
18797 | 1465 |
[exBegin] |
1466 |
|topView scrollPane textView fileStream text| |
|
1467 |
||
1468 |
topView := StandardSystemView new. |
|
1469 |
topView label:'contents of Makefile'. |
|
1470 |
||
1471 |
scrollPane := HVScrollableView in:topView. |
|
1472 |
scrollPane origin:0.0@0.0 corner:1.0@1.0. |
|
1473 |
||
1474 |
textView := EditTextView new. |
|
1475 |
scrollPane scrolledView:textView. |
|
1476 |
||
1477 |
fileStream := 'Makefile' asFilename readStream. |
|
1478 |
text := fileStream upToEnd. |
|
1479 |
fileStream close. |
|
1480 |
||
1481 |
textView contents:text. |
|
1482 |
||
1483 |
topView open. |
|
1484 |
[exEnd] |
|
3255 | 1485 |
|
1486 |
||
1487 |
Notice, all of the above can also be done (simply) as: |
|
18797 | 1488 |
[exBegin] |
1489 |
EditTextView openOn:'Makefile' |
|
1490 |
[exEnd] |
|
3255 | 1491 |
" |
22 | 1492 |
! ! |
1493 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1494 |
!ExternalStream class methodsFor:'initialization'! |
1 | 1495 |
|
18636 | 1496 |
closeFiles |
1497 |
"close all files. |
|
1498 |
To be called on exit of Smalltalk." |
|
1499 |
||
1500 |
Lobby do:[:eachFileStream | |
|
18797 | 1501 |
eachFileStream close |
18636 | 1502 |
]. |
1503 |
! |
|
1504 |
||
2920 | 1505 |
initDefaultEOLMode |
1506 |
OperatingSystem isUNIXlike ifTrue:[ |
|
18797 | 1507 |
"/ unix EOL conventions |
1508 |
DefaultEOLMode := #nl |
|
2920 | 1509 |
] ifFalse:[ |
18797 | 1510 |
OperatingSystem isVMSlike ifTrue:[ |
1511 |
"/ vms EOL conventions |
|
1512 |
DefaultEOLMode := #cr |
|
1513 |
] ifFalse:[ |
|
1514 |
"/ msdos EOL conventions |
|
1515 |
"/ msdos uses #crlf. but we keep #nl here, |
|
1516 |
"/ otherwise sending HTTP request over sockets fails |
|
17420 | 1517 |
"/ DefaultEOLMode := #crlf |
18797 | 1518 |
DefaultEOLMode := #nl |
1519 |
] |
|
2920 | 1520 |
] |
1521 |
! |
|
1522 |
||
1523 |
initModeStrings |
|
1524 |
"initialize modeStrings which are passed down to the underlying |
|
1525 |
fopen/fdopen functions." |
|
1526 |
||
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1527 |
OperatingSystem isMSDOSlike ifTrue:[ |
18797 | 1528 |
ReadMode := 'rb'. |
1529 |
ReadWriteMode := 'rb+'. |
|
1530 |
WriteMode := 'wb'. |
|
1531 |
AppendMode := 'ab+'. |
|
1532 |
CreateReadWriteMode := 'wb+'. |
|
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1533 |
] ifFalse:[ |
18797 | 1534 |
ReadMode := 'r'. |
1535 |
ReadWriteMode := 'r+'. |
|
1536 |
WriteMode := 'w'. |
|
1537 |
AppendMode := 'a+'. |
|
1538 |
CreateReadWriteMode := 'w+'. |
|
3132
037027ae20fd
msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1539 |
] |
2920 | 1540 |
! |
1541 |
||
1 | 1542 |
initialize |
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
1543 |
OpenErrorSignal isNil ifTrue:[ |
18790 | 1544 |
OpenErrorSignal := OpenError. |
1545 |
OpenErrorSignal notifierString:'open error'. |
|
1546 |
||
1547 |
InvalidReadSignal := InvalidReadError. |
|
1548 |
InvalidReadSignal notifierString:'stream does not support reading'. |
|
1549 |
||
1550 |
InvalidWriteSignal := InvalidWriteError. |
|
1551 |
InvalidWriteSignal notifierString:'stream does not support writing'. |
|
1552 |
||
1553 |
InvalidModeSignal := InvalidModeError. |
|
1554 |
InvalidModeSignal notifierString:'binary/text mode mismatch'. |
|
1555 |
||
1556 |
InvalidOperationSignal := InvalidOperationError. |
|
1557 |
InvalidOperationSignal notifierString:'unsupported file operation'. |
|
1558 |
||
1559 |
StreamNotOpenSignal := StreamNotOpenError. |
|
1560 |
StreamNotOpenSignal notifierString:'stream is not open'. |
|
1561 |
||
1562 |
StreamIOErrorSignal := StreamIOError. |
|
1563 |
StreamIOErrorSignal notifierString:'I/O error'. |
|
159 | 1564 |
]. |
1565 |
||
31 | 1566 |
Lobby isNil ifTrue:[ |
18790 | 1567 |
Lobby := Registry new. |
1568 |
||
1569 |
"want to get informed when returning from snapshot" |
|
1570 |
ObjectMemory addDependent:self |
|
2920 | 1571 |
]. |
5442 | 1572 |
DefaultEOLMode isNil ifTrue:[ |
18790 | 1573 |
self initDefaultEOLMode. |
5442 | 1574 |
]. |
1575 |
ReadMode isNil ifTrue:[ |
|
18790 | 1576 |
self initModeStrings. |
5442 | 1577 |
]. |
18790 | 1578 |
|
1579 |
"limit the amount of newspace to be used for non-tenurable executors to 5%" |
|
18929 | 1580 |
"/ MaxNonTenurableExecutors := ObjectMemory newSpaceSize // (Socket sizeOfInst:0) // 20. |
1581 |
"/ cg: changed because Socket is not in libbasic. Thus, standalone (libbasic only) |
|
1582 |
"/ programs would fail to start. |
|
1583 |
MaxNonTenurableExecutors := (ObjectMemory newSpaceSize // 20 min:2000). |
|
18790 | 1584 |
|
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1585 |
"Modified: / 21.5.1998 / 16:33:53 / cg" |
1 | 1586 |
! |
1587 |
||
1588 |
reOpenFiles |
|
4108
8c4a45bc70eb
perform reOpen of streams earlier (before doing the view-reinit)
Claus Gittinger <cg@exept.de>
parents:
4093
diff
changeset
|
1589 |
"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
|
1590 |
This is invoked via the #earlyRestart change notification." |
1 | 1591 |
|
18636 | 1592 |
Lobby do:[:eachFileStream | |
18797 | 1593 |
eachFileStream reOpen |
2920 | 1594 |
]. |
1 | 1595 |
! |
1596 |
||
1469 | 1597 |
update:something with:aParameter from:changedObject |
1 | 1598 |
"have to reopen files when returning from snapshot" |
1599 |
||
4108
8c4a45bc70eb
perform reOpen of streams earlier (before doing the view-reinit)
Claus Gittinger <cg@exept.de>
parents:
4093
diff
changeset
|
1600 |
something == #earlyRestart ifTrue:[ |
18797 | 1601 |
self reOpenFiles. |
1602 |
self initDefaultEOLMode |
|
1 | 1603 |
] |
1469 | 1604 |
|
1605 |
"Created: 15.6.1996 / 15:19:59 / cg" |
|
1 | 1606 |
! ! |
1607 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1608 |
!ExternalStream class methodsFor:'instance creation'! |
612 | 1609 |
|
12706 | 1610 |
forFileDescriptor:aFileDescriptor mode:modeSymbol |
2920 | 1611 |
"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
|
1612 |
to operate on this fd. |
12706 | 1613 |
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
|
1614 |
This may be used to wrap fd's as returned by user |
2920 | 1615 |
primitive code, or to wrap pipe-fds into externalStreams." |
1616 |
||
12706 | 1617 |
^ self forFileDescriptor:aFileDescriptor mode:modeSymbol buffered:true handleType:nil |
7002 | 1618 |
|
1619 |
" |
|
1620 |
the example below will probably fail (15 is a random FD): |
|
1621 |
||
1622 |
|s| |
|
1623 |
||
1624 |
s := ExternalStream forFileDescriptor:15 mode:'r'. |
|
1625 |
s next. |
|
1626 |
" |
|
1627 |
||
1628 |
"Created: 29.2.1996 / 18:05:00 / cg" |
|
1629 |
"Modified: 29.2.1996 / 18:17:07 / cg" |
|
1630 |
! |
|
1631 |
||
12706 | 1632 |
forFileDescriptor:aFileDescriptor mode:modeSymbol buffered:buffered handleType:handleTypeSymbol |
7002 | 1633 |
"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
|
1634 |
to operate on this fd. |
12706 | 1635 |
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
|
1636 |
This may be used to wrap fd's as returned by user |
7002 | 1637 |
primitive code, or to wrap pipe-fds into externalStreams." |
1638 |
||
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1639 |
|newStream| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1640 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1641 |
newStream := self basicNew. |
7002 | 1642 |
newStream text; buffered:buffered; eolMode:DefaultEOLMode; clearEOF. |
12706 | 1643 |
^ newStream connectTo:aFileDescriptor withMode:modeSymbol handleType:handleTypeSymbol |
2920 | 1644 |
|
1645 |
" |
|
1646 |
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
|
1647 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1648 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1649 |
|
12706 | 1650 |
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
|
1651 |
s next. |
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 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1654 |
"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
|
1655 |
"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
|
1656 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1657 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1658 |
forReadWriteToFileDescriptor:aFileDescriptor |
2920 | 1659 |
"given a fileDescriptor, create an ExternalStream object |
1660 |
to read/write from/to this fd. This may be used to wrap fd's |
|
1661 |
as returned by user primitive code, or to wrap pipe- |
|
1662 |
filedescriptors into externalStreams." |
|
1663 |
||
12706 | 1664 |
^ self forFileDescriptor:aFileDescriptor mode:#readWrite |
2920 | 1665 |
|
1666 |
" |
|
1667 |
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
|
1668 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1669 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1670 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1671 |
s := ExternalStream forReadWriteToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1672 |
s next. |
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 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1675 |
"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
|
1676 |
"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
|
1677 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1678 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1679 |
forReadingFromFileDescriptor:aFileDescriptor |
2920 | 1680 |
"given a fileDescriptor, create an ExternalStream object |
1681 |
to read from this fd. This may be used to wrap fd's |
|
1682 |
as returned by user primitive code, or to wrap pipe- |
|
1683 |
filedescriptors into externalStreams." |
|
1684 |
||
12706 | 1685 |
^ self forFileDescriptor:aFileDescriptor mode:#readonly |
2920 | 1686 |
|
1687 |
" |
|
1688 |
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
|
1689 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1690 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1691 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1692 |
s := ExternalStream forReadingFromFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1693 |
s next. |
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 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1697 |
|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
|
1698 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1699 |
'create OS pipe ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1700 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1701 |
pipe := OperatingSystem makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1702 |
readFd := pipe at:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1703 |
writeFd := pipe at:2. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1704 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1705 |
'connect Smalltalk streams ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1706 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1707 |
rs := ExternalStream forReadingFromFileDescriptor:readFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1708 |
ws := ExternalStream forWritingToFileDescriptor:writeFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1709 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1710 |
'read ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1711 |
[ |
18797 | 1712 |
1 to:10 do:[:i | |
1713 |
Transcript showCR:rs nextLine |
|
1714 |
]. |
|
1715 |
rs close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1716 |
] forkAt:7. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1717 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1718 |
'write ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1719 |
[ |
18797 | 1720 |
1 to:10 do:[:i | |
1721 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1722 |
]. |
|
1723 |
ws close. |
|
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1724 |
] fork. |
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 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1728 |
"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
|
1729 |
"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
|
1730 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1731 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1732 |
forWritingToFileDescriptor:aFileDescriptor |
2920 | 1733 |
"given a fileDescriptor, create an ExternalStream object |
1734 |
to write to this fd. This may be used to wrap fd's |
|
1735 |
as returned by user primitive code, or to wrap pipe- |
|
1736 |
filedescriptors into externalStreams." |
|
1737 |
||
12706 | 1738 |
^ self forFileDescriptor:aFileDescriptor mode:#writeonly |
2920 | 1739 |
|
1740 |
" |
|
1741 |
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
|
1742 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1743 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1744 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1745 |
s := ExternalStream forWritingToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1746 |
s binary. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1747 |
s nextPut:1. |
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 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1750 |
"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
|
1751 |
"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
|
1752 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
1753 |
|
612 | 1754 |
new |
15918
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
1755 |
"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
|
1756 |
|
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
1757 |
^ self basicNew initialize. |
612 | 1758 |
! ! |
1759 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1760 |
!ExternalStream class methodsFor:'Signal constants'! |
159 | 1761 |
|
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1762 |
inaccessibleSignal |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1763 |
"ST-80 compatibility: return openErrorSignal" |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1764 |
|
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1765 |
^ self openErrorSignal |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1766 |
|
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1767 |
"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
|
1768 |
! |
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
1769 |
|
612 | 1770 |
invalidModeSignal |
1771 |
"return the signal raised when doing text-I/O with a binary stream |
|
1772 |
or binary-I/O with a text stream" |
|
1773 |
||
8074 | 1774 |
^ InvalidModeError |
159 | 1775 |
! |
1776 |
||
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1777 |
invalidOperationSignal |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1778 |
"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
|
1779 |
I/O operation is attempted" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1780 |
|
8074 | 1781 |
^ InvalidOperationError |
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1782 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1783 |
|
159 | 1784 |
invalidReadSignal |
1785 |
"return the signal raised when reading from writeonly streams" |
|
1786 |
||
8074 | 1787 |
^ InvalidReadError |
159 | 1788 |
! |
1789 |
||
1790 |
invalidWriteSignal |
|
1791 |
"return the signal raised when writing to readonly streams" |
|
1792 |
||
8074 | 1793 |
^ InvalidWriteError |
159 | 1794 |
! |
1795 |
||
612 | 1796 |
openErrorSignal |
1797 |
"return the signal raised when a file open failed" |
|
1798 |
||
1799 |
^ OpenErrorSignal |
|
1800 |
! |
|
1801 |
||
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1802 |
streamIOErrorSignal |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1803 |
"return the signal raised when an I/O error occurs. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1804 |
(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
|
1805 |
which is no longer available and has been mounted soft)" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1806 |
|
8074 | 1807 |
^ StreamIOError |
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1808 |
|
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1809 |
"Created: / 21.5.1998 / 16:32:55 / cg" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1810 |
! |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
1811 |
|
159 | 1812 |
streamNotOpenSignal |
1813 |
"return the signal raised on I/O with closed streams" |
|
1814 |
||
8074 | 1815 |
^ StreamNotOpenError |
2 | 1816 |
! ! |
1817 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
1818 |
!ExternalStream class methodsFor:'error handling'! |
92 | 1819 |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1820 |
errorReporter |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1821 |
"I know about error codes" |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1822 |
|
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1823 |
^ self |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1824 |
! |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1825 |
|
92 | 1826 |
lastErrorNumber |
159 | 1827 |
"return the errno of the last error" |
92 | 1828 |
|
1829 |
^ LastErrorNumber |
|
159 | 1830 |
|
1831 |
" |
|
1832 |
ExternalStream lastErrorNumber |
|
1833 |
" |
|
92 | 1834 |
! |
1835 |
||
1836 |
lastErrorString |
|
1837 |
"return a message string describing the last error" |
|
1838 |
||
1839 |
^ OperatingSystem errorTextForNumber:LastErrorNumber |
|
1840 |
||
1841 |
" |
|
1842 |
ExternalStream lastErrorString |
|
1843 |
" |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1844 |
! |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1845 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
1846 |
reportOn:anErrorSymbolOrNumber |
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1847 |
"an error occured. |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1848 |
Report it via an Exception" |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1849 |
|
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1850 |
anErrorSymbolOrNumber isInteger ifTrue:[ |
18797 | 1851 |
StreamError |
1852 |
raiseRequestWith:anErrorSymbolOrNumber |
|
1853 |
errorString:(' - os error:' , (OperatingSystem errorTextForNumber:anErrorSymbolOrNumber)) |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1854 |
]. |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1855 |
(self respondsTo:anErrorSymbolOrNumber) ifTrue:[ |
18797 | 1856 |
self perform:anErrorSymbolOrNumber |
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1857 |
] ifFalse:[ |
18797 | 1858 |
StreamError |
1859 |
raiseRequestWith:anErrorSymbolOrNumber |
|
1860 |
errorString:(' - ' , anErrorSymbolOrNumber printString) |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1861 |
]. |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
1862 |
^ false |
92 | 1863 |
! ! |
1864 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1865 |
!ExternalStream class methodsFor:'obsolete'! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1866 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1867 |
makePTYPair |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1868 |
"obsolete since 12-07-2003" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1869 |
<resource:#obsolete> |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1870 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1871 |
self obsoleteMethodWarning:'use NonPositionableExternalStream makePTYPair'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1872 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1873 |
^ NonPositionableExternalStream makePTYPair. |
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 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1876 |
makePipe |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1877 |
"obsolete since 12-07-2003" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1878 |
<resource:#obsolete> |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1879 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1880 |
self obsoleteMethodWarning:'use NonPositionableExternalStream makePipe'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1881 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1882 |
^ NonPositionableExternalStream makePipe. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1883 |
! ! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
1884 |
|
7261 | 1885 |
!ExternalStream methodsFor:'Compatibility-Dolphin'! |
6474 | 1886 |
|
1887 |
beText |
|
1888 |
self text |
|
1889 |
! ! |
|
1890 |
||
7261 | 1891 |
!ExternalStream methodsFor:'Compatibility-Squeak'! |
3044 | 1892 |
|
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1893 |
nextInto:aByteArrayOrString |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1894 |
"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
|
1895 |
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
|
1896 |
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
|
1897 |
|
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1898 |
|n nWanted| |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1899 |
|
4368 | 1900 |
nWanted := aByteArrayOrString byteSize. |
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1901 |
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
|
1902 |
n == nWanted ifTrue:[^ aByteArrayOrString]. |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1903 |
^ aByteArrayOrString copyTo:n |
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 |
! |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1906 |
|
4368 | 1907 |
readInto:aContainer startingAt:index count:nElements |
1908 |
"same as #nextBytes:into:startingAt: for ByteArrays; |
|
1909 |
for LongArrays, nelements longs are read. |
|
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1910 |
Squeak compatibility." |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1911 |
|
4368 | 1912 |
|elementSize n| |
1913 |
||
1914 |
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
|
1915 |
n := self nextBytes:nElements*elementSize into:aContainer startingAt:(index-1)*elementSize+1. |
4368 | 1916 |
^ n // elementSize |
3872
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1917 |
|
13940
12eb1c056d5e
fixed: #readInto:startingAt:count:; offset was wrong for non-byte elements
Claus Gittinger <cg@exept.de>
parents:
13927
diff
changeset
|
1918 |
"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
|
1919 |
! |
cab901645d2a
added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
3863
diff
changeset
|
1920 |
|
3044 | 1921 |
readOnly |
1922 |
"Squeak compatibility: make the stream readOnly" |
|
1923 |
||
1924 |
mode := #readonly |
|
1925 |
||
1926 |
"Modified: 20.10.1997 / 19:23:04 / cg" |
|
1927 |
"Created: 20.10.1997 / 19:23:19 / cg" |
|
1928 |
! ! |
|
1929 |
||
5187 | 1930 |
!ExternalStream methodsFor:'Signal constants'! |
1931 |
||
1932 |
invalidReadSignal |
|
1933 |
^ self class invalidReadSignal |
|
1934 |
||
1935 |
"Created: / 3.12.1998 / 15:12:06 / cg" |
|
1936 |
! |
|
1937 |
||
1938 |
invalidWriteSignal |
|
1939 |
^ self class invalidWriteSignal |
|
1940 |
||
1941 |
"Created: / 3.12.1998 / 15:12:10 / cg" |
|
1942 |
! ! |
|
1943 |
||
1 | 1944 |
!ExternalStream methodsFor:'accessing'! |
1945 |
||
612 | 1946 |
binary |
1947 |
"switch to binary mode - default is text" |
|
1948 |
||
1949 |
binary := true |
|
1 | 1950 |
! |
1951 |
||
1952 |
buffered:aBoolean |
|
10 | 1953 |
"turn buffering on or off - default is on" |
1 | 1954 |
|
10 | 1955 |
buffered := aBoolean |
1 | 1956 |
! |
1957 |
||
1958 |
contents |
|
2 | 1959 |
"return the contents of the file from the current position up-to |
1960 |
the end. If the stream is in binary mode, a ByteArray containing |
|
1961 |
the byte values is returned. |
|
1962 |
In text-mode, a collection of strings, each representing one line, |
|
1963 |
is returned." |
|
1964 |
||
1965 |
|text l chunks sizes chunk byteCount cnt bytes offset| |
|
1966 |
||
1967 |
binary ifTrue:[ |
|
18797 | 1968 |
"adding to a ByteArray produces quadratic time-space |
1969 |
behavior - therefore we allocate chunks, and concatenate them |
|
1970 |
at the end." |
|
1971 |
||
1972 |
chunks := OrderedCollection new. |
|
1973 |
sizes := OrderedCollection new. |
|
1974 |
byteCount := 0. |
|
1975 |
[self atEnd] whileFalse:[ |
|
1976 |
chunk := ByteArray uninitializedNew:4096. |
|
1977 |
cnt := self nextBytes:(chunk size) into:chunk. |
|
1978 |
cnt notNil ifTrue:[ |
|
1979 |
chunks add:chunk. |
|
1980 |
sizes add:cnt. |
|
1981 |
byteCount := byteCount + cnt |
|
1982 |
] |
|
1983 |
]. |
|
1984 |
||
1985 |
"now, create one big ByteArray" |
|
1986 |
bytes := ByteArray uninitializedNew:byteCount. |
|
1987 |
offset := 1. |
|
1988 |
1 to:chunks size do:[:index | |
|
1989 |
chunk := chunks at:index. |
|
1990 |
cnt := sizes at:index. |
|
1991 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk. |
|
1992 |
offset := offset + cnt |
|
1993 |
]. |
|
1994 |
^ bytes |
|
2 | 1995 |
]. |
1 | 1996 |
|
244 | 1997 |
text := StringCollection new. |
1 | 1998 |
[self atEnd] whileFalse:[ |
18797 | 1999 |
l := self nextLine. |
2000 |
l isNil ifTrue:[ |
|
2001 |
^ text |
|
2002 |
]. |
|
2003 |
text add:l |
|
1 | 2004 |
]. |
2005 |
^ text |
|
2006 |
! |
|
2007 |
||
11751
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2008 |
contentsAsString |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2009 |
"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
|
2010 |
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
|
2011 |
|
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2012 |
^ self contentsOfEntireFile |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2013 |
! |
1e75c54748ef
added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents:
11651
diff
changeset
|
2014 |
|
612 | 2015 |
contentsOfEntireFile |
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
2016 |
"ST-80 compatibility: return contents as a String (or byteArray, if in binary mode). |
3876 | 2017 |
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
|
2018 |
|
6305 | 2019 |
binary ifTrue:[ |
18797 | 2020 |
^ self contents. |
3938
4e7d1880336b
oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents:
3906
diff
changeset
|
2021 |
]. |
6305 | 2022 |
^ self upToEnd |
1522
1d0b4bc2e563
ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents:
1515
diff
changeset
|
2023 |
|
3876 | 2024 |
"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
|
2025 |
"Modified: / 27.11.1998 / 16:29:43 / cg" |
2 | 2026 |
! |
2027 |
||
612 | 2028 |
contentsSpecies |
2029 |
"return the kind of object to be returned by sub-collection builders |
|
2030 |
(such as upTo)" |
|
159 | 2031 |
|
5441 | 2032 |
binary == true ifTrue:[ |
18797 | 2033 |
^ ByteArray |
159 | 2034 |
]. |
612 | 2035 |
^ String |
2 | 2036 |
! |
2037 |
||
16776 | 2038 |
eolMode |
2039 |
"return how end-of-line (EOL) is to be marked. |
|
2040 |
Returns one one of: |
|
18797 | 2041 |
#crlf -> add a CR-NL, as in MSDOS |
2042 |
#cr -> add a CR, as in VMS |
|
2043 |
#nl -> add a NL, as in Unix |
|
2044 |
nil -> transparent |
|
16776 | 2045 |
" |
2046 |
||
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
|
2047 |
^ eolMode |
16776 | 2048 |
! |
2049 |
||
2920 | 2050 |
eolMode:aSymbolOrNil |
2051 |
"specify how end-of-line (EOL) is to be marked. |
|
2052 |
The argument may be one of: |
|
18797 | 2053 |
#crlf -> add a CR-NL, as in MSDOS |
2054 |
#cr -> add a CR, as in VMS |
|
2055 |
#nl -> add a NL, as in Unix |
|
2056 |
anyOther -> like #nl |
|
2920 | 2057 |
" |
2058 |
||
17462 | 2059 |
aSymbolOrNil == #crnl ifTrue:[ |
18797 | 2060 |
eolMode := #crlf |
17462 | 2061 |
] ifFalse:[ |
18797 | 2062 |
aSymbolOrNil == #lf ifTrue:[ |
2063 |
eolMode := #nl |
|
2064 |
] ifFalse:[ |
|
2065 |
eolMode := aSymbolOrNil |
|
2066 |
] |
|
17462 | 2067 |
]. |
2920 | 2068 |
! |
2069 |
||
612 | 2070 |
fileDescriptor |
2071 |
"return the fileDescriptor of the receiver - |
|
2072 |
notice: this one returns the underlying OSs fileDescriptor - |
|
2073 |
this may not be available on all platforms (i.e. non unix systems)." |
|
1 | 2074 |
|
3971 | 2075 |
%{ |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2076 |
OBJ _handle = __INST(handle); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2077 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2078 |
if (_handle != nil) { |
18797 | 2079 |
if (__INST(handleType) == @symbol(socketHandle)) { |
2080 |
RETURN (_handle); |
|
2081 |
} else if ((__INST(handleType) == nil) |
|
2082 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2083 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
2084 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
2085 |
RETURN ( __MKINT(fileno(__FILEVal(_handle)))); |
|
2086 |
} |
|
1 | 2087 |
} |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2088 |
%}. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2089 |
handle isNil ifTrue:[^ self errorNotOpen]. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2090 |
^ self fileDescriptorOfFile:handle |
5852 | 2091 |
! |
2092 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2093 |
fileDescriptorOfFile:handle |
5852 | 2094 |
"for migration to rel5 only: |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2095 |
return the fileDescriptor of the argument handle - |
5852 | 2096 |
notice: this one returns the underlying OSs fileDescriptor - |
2097 |
this may not be available on all platforms (i.e. non unix systems)." |
|
2098 |
||
18312 | 2099 |
%{ |
2100 |
#ifdef __SCHTEAM__ |
|
2101 |
return context._RETURN(handle); |
|
2102 |
#endif |
|
2103 |
%}. |
|
5852 | 2104 |
self primitiveFailed |
612 | 2105 |
! |
2106 |
||
10455 | 2107 |
fileHandle |
2108 |
"return the fileHandle of the receiver. |
|
2109 |
Under unix, this is the fileDescriptor; under windows, this is the Handle." |
|
2110 |
||
2111 |
%{ |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2112 |
OBJ _handle = __INST(handle); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2113 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2114 |
if (_handle != nil) { |
18797 | 2115 |
if ((__INST(handleType) == @symbol(fileHandle)) |
2116 |
|| (__INST(handleType) == @symbol(socketHandle))) { |
|
2117 |
RETURN (_handle); |
|
2118 |
} |
|
2119 |
if ((__INST(handleType) == nil) |
|
2120 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2121 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
2122 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
10455 | 2123 |
#ifdef WIN32 |
18797 | 2124 |
RETURN(__MKEXTERNALADDRESS(_get_osfhandle(fileno(__FILEVal(_handle))))); |
10455 | 2125 |
#else |
18797 | 2126 |
RETURN (__MKINT(fileno(__FILEVal(_handle)))); |
10455 | 2127 |
#endif |
18797 | 2128 |
} |
10455 | 2129 |
} |
2130 |
%}. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2131 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2132 |
^ handle |
10455 | 2133 |
! |
2134 |
||
612 | 2135 |
filePointer |
2136 |
"return the filePointer of the receiver - |
|
2137 |
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
|
2138 |
a FILE * - not a fileDescriptor. |
612 | 2139 |
(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
|
2140 |
You cannot do much with the returned value |
612 | 2141 |
- except passing it to a primitive, for example." |
2142 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2143 |
(handleType isNil or:[handleType == #filePointer]) ifTrue:[ |
18797 | 2144 |
^ handle |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2145 |
]. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2146 |
^ self error:'not a FILE*' |
612 | 2147 |
! |
2148 |
||
16368
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 |
^ handleType |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2151 |
! |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2152 |
|
5323 | 2153 |
lineEndCRLF |
2154 |
eolMode := #crlf |
|
2155 |
! |
|
2156 |
||
17672 | 2157 |
lineEndLF |
2158 |
eolMode := #nl |
|
2159 |
! |
|
2160 |
||
3016 | 2161 |
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
|
2162 |
eolMode := #nl |
3016 | 2163 |
! |
2164 |
||
8069
fdf460457c96
acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents:
8066
diff
changeset
|
2165 |
pathName |
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2166 |
"answer the pathName of the stream. |
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2167 |
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
|
2168 |
|
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2169 |
^ '' |
8069
fdf460457c96
acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents:
8066
diff
changeset
|
2170 |
! |
fdf460457c96
acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents:
8066
diff
changeset
|
2171 |
|
612 | 2172 |
readonly |
2173 |
"set access mode to readonly" |
|
2174 |
||
2175 |
mode := #readonly |
|
2176 |
! |
|
2177 |
||
2178 |
readwrite |
|
2179 |
"set access mode to readwrite" |
|
2180 |
||
2181 |
mode := #readwrite |
|
2182 |
! |
|
2183 |
||
2184 |
text |
|
2185 |
"switch to text mode - default is text" |
|
2186 |
||
2187 |
binary := false |
|
2188 |
! |
|
2189 |
||
2190 |
useCRLF:aBoolean |
|
2920 | 2191 |
"turn on or off CRLF sending (instead of LF only) - default is off. |
2192 |
This method is provided for backward compatibility - see #eolMode: |
|
2193 |
which offers another choice." |
|
2194 |
||
2195 |
aBoolean ifTrue:[ |
|
18797 | 2196 |
eolMode := #crlf |
2920 | 2197 |
] ifFalse:[ |
18797 | 2198 |
eolMode := #nl |
2920 | 2199 |
]. |
1 | 2200 |
! |
2201 |
||
612 | 2202 |
writeonly |
2203 |
"set access mode to writeonly" |
|
2204 |
||
2205 |
mode := #writeonly |
|
2206 |
! ! |
|
2207 |
||
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2208 |
!ExternalStream methodsFor:'closing'! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2209 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2210 |
close |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2211 |
"close the stream. |
16300 | 2212 |
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
|
2213 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2214 |
self isOpen ifTrue:[ |
18779 | 2215 |
self unregisterForFinalization. |
2216 |
self closeFile. |
|
16300 | 2217 |
]. |
8966
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 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2220 |
shutDown |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2221 |
"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
|
2222 |
see comment there" |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2223 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2224 |
self isOpen ifTrue:[ |
18779 | 2225 |
self unregisterForFinalization. |
2226 |
self closeFile |
|
8966
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 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2229 |
"Modified: 30.8.1996 / 00:39:21 / cg" |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2230 |
! ! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2231 |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2232 |
!ExternalStream methodsFor:'copying'! |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2233 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2234 |
copy |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2235 |
"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
|
2236 |
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
|
2237 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2238 |
|copy| |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2239 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2240 |
copy := super copy. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2241 |
copy dupFd. |
18779 | 2242 |
copy registerForFinalization. |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2243 |
^ copy |
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 |
" |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2246 |
|stream1 stream2| |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2247 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2248 |
stream1 := Filename newTemporary writeStream. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2249 |
stream2 := stream1 copy. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2250 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2251 |
stream1 inspect. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2252 |
stream2 inspect. |
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 |
! ! |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
2255 |
|
612 | 2256 |
!ExternalStream methodsFor:'error handling'! |
2257 |
||
2258 |
argumentMustBeCharacter |
|
14440 | 2259 |
"report an error, that the argument must be a character in 0..FF" |
2260 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2261 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2262 |
|
14440 | 2263 |
ArgumentError raiseErrorString:' - argument must be a single byte character' |
612 | 2264 |
! |
2265 |
||
2266 |
argumentMustBeInteger |
|
14440 | 2267 |
"report an error, that the argument must be an integer" |
2268 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2269 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2270 |
|
14440 | 2271 |
ArgumentError raiseErrorString:' - argument must be an integer' |
77 | 2272 |
! |
2273 |
||
612 | 2274 |
argumentMustBeString |
14440 | 2275 |
"report an error, that the argument must be a string" |
2276 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2277 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2278 |
|
14440 | 2279 |
ArgumentError raiseErrorString:' - argument must be a string' |
612 | 2280 |
! |
2281 |
||
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2282 |
errorAlreadyOpen |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2283 |
"{ Pragma: +optSpace }" |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2284 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2285 |
"report an error, that the stream is already opened" |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2286 |
|
8074 | 2287 |
^ OpenError |
18797 | 2288 |
raiseRequestWith:self |
2289 |
errorString:' - stream is already open' |
|
8511
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 |
" |
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2292 |
self new errorAlreadyOpen |
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2293 |
" |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2294 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2295 |
"Modified: / 8.5.1999 / 20:12:30 / cg" |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2296 |
! |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4468
diff
changeset
|
2297 |
|
612 | 2298 |
errorBinary |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2299 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2300 |
|
612 | 2301 |
"report an error, that the stream is in binary mode" |
2302 |
||
8074 | 2303 |
^ InvalidModeError |
18797 | 2304 |
raiseRequestWith:self |
2305 |
errorString:(self class name , ' is in binary mode') |
|
2306 |
"/ in:thisContext sender |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2307 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2308 |
"Modified: / 8.5.1999 / 20:12:43 / cg" |
612 | 2309 |
! |
2310 |
||
2311 |
errorNotBinary |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2312 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2313 |
|
612 | 2314 |
"report an error, that the stream is not in binary mode" |
2315 |
||
8074 | 2316 |
^ InvalidModeError |
18797 | 2317 |
raiseRequestWith:self |
2318 |
errorString:(self class name , ' is not in binary mode') |
|
2319 |
"/ in:thisContext sender |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2320 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2321 |
"Modified: / 8.5.1999 / 20:12:40 / cg" |
1 | 2322 |
! |
2323 |
||
612 | 2324 |
errorNotBuffered |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2325 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2326 |
|
612 | 2327 |
"report an error, that the stream is not in buffered mode" |
2328 |
||
8074 | 2329 |
^ StreamError |
18797 | 2330 |
raiseRequestWith:self |
2331 |
errorString:(self class name , ' is unbuffered - operation not allowed') |
|
2332 |
"/ in:thisContext sender |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2333 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2334 |
"Modified: / 8.5.1999 / 20:12:36 / cg" |
612 | 2335 |
! |
2336 |
||
2337 |
errorNotOpen |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2338 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2339 |
|
612 | 2340 |
"report an error, that the stream has not been opened" |
2341 |
||
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2342 |
^ 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
|
2343 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2344 |
"Modified: / 8.5.1999 / 20:12:33 / cg" |
612 | 2345 |
! |
2346 |
||
2347 |
errorReadOnly |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2348 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2349 |
|
612 | 2350 |
"report an error, that the stream is a readOnly stream" |
2351 |
||
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2352 |
^ 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
|
2353 |
|
4468 | 2354 |
"Modified: / 30.7.1999 / 17:08:19 / cg" |
612 | 2355 |
! |
2356 |
||
6255
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2357 |
errorReporter |
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2358 |
"ST-80 mimicry." |
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2359 |
|
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
2360 |
^ self class |
6255
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2361 |
! |
d68343dc8288
Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents:
6012
diff
changeset
|
2362 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2363 |
errorUnsupportedOperation |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2364 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2365 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2366 |
"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
|
2367 |
|
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2368 |
^ 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
|
2369 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2370 |
"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
|
2371 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
2372 |
|
612 | 2373 |
errorWriteOnly |
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2374 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2375 |
|
612 | 2376 |
"report an error, that the stream is a writeOnly stream" |
2377 |
||
8511
eb02df149f42
Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
2378 |
^ 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
|
2379 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2380 |
"Modified: / 8.5.1999 / 20:12:20 / cg" |
2 | 2381 |
! |
2382 |
||
612 | 2383 |
ioError |
3474
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
2384 |
"report an error, that some I/O error occured. |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
2385 |
(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
|
2386 |
which is no longer available and has been mounted soft)" |
1af4d150dab4
added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents:
3467
diff
changeset
|
2387 |
|
16296 | 2388 |
"{ Pragma: +optSpace }" |
2389 |
||
2390 |
self ioError:lastErrorNumber |
|
2391 |
||
2392 |
"Modified: / 8.5.1999 / 20:12:16 / cg" |
|
2393 |
! |
|
2394 |
||
2395 |
ioError:errorNumber |
|
2396 |
"report an error, that some I/O error occured. |
|
2397 |
(for example, a device-IO-error, or reading an NFS-dir, |
|
2398 |
which is no longer available and has been mounted soft)" |
|
2399 |
||
2400 |
"{ Pragma: +optSpace }" |
|
2401 |
||
2402 |
^ StreamIOError newException |
|
18797 | 2403 |
errorCode:errorNumber; |
2404 |
osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber); |
|
2405 |
parameter:self; |
|
2406 |
raiseRequest |
|
4161
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2407 |
|
3f2758cfeff5
raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents:
4152
diff
changeset
|
2408 |
"Modified: / 8.5.1999 / 20:12:16 / cg" |
612 | 2409 |
! |
2410 |
||
2411 |
lastErrorNumber |
|
2412 |
"return the last error" |
|
2413 |
||
2414 |
^ lastErrorNumber |
|
2415 |
! |
|
2416 |
||
2417 |
lastErrorString |
|
2418 |
"return a message string describing the last error" |
|
2419 |
||
2420 |
(lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[ |
|
18797 | 2421 |
^ 'I/O error' |
612 | 2422 |
]. |
2423 |
^ OperatingSystem errorTextForNumber:lastErrorNumber |
|
2 | 2424 |
! |
2425 |
||
10932 | 2426 |
lastErrorSymbol |
2427 |
"return an error symbol describing the last error" |
|
2428 |
||
2429 |
^ OperatingSystem errorSymbolForNumber:lastErrorNumber |
|
2430 |
! |
|
2431 |
||
612 | 2432 |
openError |
12688 | 2433 |
"report an error, that the open failed" |
2434 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2435 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2436 |
|
14438 | 2437 |
^ self openError:lastErrorNumber. |
612 | 2438 |
! |
2439 |
||
12688 | 2440 |
openError:errorNumber |
2441 |
"report an error, that the open failed" |
|
2442 |
||
2443 |
"{ Pragma: +optSpace }" |
|
2444 |
||
15496 | 2445 |
|exClass errorHolder| |
2446 |
||
2447 |
errorHolder := OperatingSystem errorHolderForNumber:errorNumber. |
|
2448 |
||
2449 |
exClass := (errorHolder errorCategory == #nonexistentSignal) |
|
18797 | 2450 |
ifTrue:[ FileDoesNotExistException ] |
2451 |
ifFalse:[ OpenError ]. |
|
13673 | 2452 |
|
2453 |
^ exClass newException |
|
18797 | 2454 |
errorCode:errorNumber; |
2455 |
osErrorHolder:errorHolder; |
|
2456 |
"/ cg: initialized lazyly - see OpenError>>#description |
|
2457 |
"/ errorString:(' : ' , errorHolder errorString); |
|
2458 |
parameter:self; |
|
2459 |
raiseRequest |
|
2460 |
"/ in:thisContext sender |
|
13673 | 2461 |
|
2462 |
"Modified: / 09-09-2011 / 07:22:49 / cg" |
|
12688 | 2463 |
! |
2464 |
||
612 | 2465 |
readError |
15845 | 2466 |
"report an error, that some read error occured" |
2467 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2468 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2469 |
|
15845 | 2470 |
^ self readError:lastErrorNumber |
2471 |
! |
|
2472 |
||
2473 |
readError:errorNumber |
|
612 | 2474 |
"report an error, that some read error occured" |
2475 |
||
15845 | 2476 |
"{ Pragma: +optSpace }" |
2477 |
||
2478 |
^ ReadError newException |
|
18797 | 2479 |
errorCode:errorNumber; |
2480 |
osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber); |
|
2481 |
parameter:self; |
|
2482 |
raiseRequest |
|
612 | 2483 |
! |
2484 |
||
2485 |
writeError |
|
15845 | 2486 |
"report an error, that some write error occured" |
2487 |
||
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2488 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
2489 |
|
15845 | 2490 |
^ self writeError:lastErrorNumber |
2491 |
! |
|
2492 |
||
2493 |
writeError:errorNumber |
|
612 | 2494 |
"report an error, that some write error occured" |
2495 |
||
15845 | 2496 |
"{ Pragma: +optSpace }" |
2497 |
||
2498 |
^ WriteError newException |
|
18797 | 2499 |
errorCode:errorNumber; |
2500 |
osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber); |
|
2501 |
parameter:self; |
|
2502 |
raiseRequest |
|
612 | 2503 |
! ! |
2504 |
||
6461
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2505 |
!ExternalStream methodsFor:'finalization'! |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2506 |
|
15468 | 2507 |
executor |
2508 |
"return a copy for finalization-registration; |
|
2509 |
since all we need at finalization time is the fileDescriptor, |
|
2510 |
a cheaper copy is possible." |
|
2511 |
||
2512 |
^ self class basicNew setAccessor:handleType to:handle |
|
2513 |
! |
|
2514 |
||
6461
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2515 |
finalizationLobby |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2516 |
"answer the registry used for finalization. |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2517 |
ExternalStreams have their own Registry" |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2518 |
|
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2519 |
^ Lobby |
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 |
|
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2522 |
finalize |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2523 |
"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
|
2524 |
|
16551 | 2525 |
"/ with timeout to avoid blocking in a bad pty/socket |
2526 |
[ |
|
18797 | 2527 |
self closeFile |
16551 | 2528 |
] valueWithTimeout:30 seconds. |
18779 | 2529 |
! |
2530 |
||
2531 |
reRegisterForFinalization |
|
2532 |
Lobby registerChange:self. |
|
2533 |
! |
|
2534 |
||
2535 |
registerForFinalization |
|
2536 |
||
2537 |
"keep myself in newSpace, so it will be finalized early" |
|
18790 | 2538 |
Lobby size < MaxNonTenurableExecutors ifTrue:[ |
2539 |
ObjectMemory preventTenureOf:self. |
|
2540 |
]. |
|
18779 | 2541 |
Lobby register:self. |
2542 |
! |
|
2543 |
||
2544 |
unregisterForFinalization |
|
2545 |
Lobby unregister:self. |
|
2546 |
"probably the object will be collected soon..." |
|
18793 | 2547 |
ObjectMemory allowTenureOf:self |
6461
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2548 |
! ! |
f5efaff3457b
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6441
diff
changeset
|
2549 |
|
15918
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2550 |
!ExternalStream methodsFor:'initialization'! |
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2551 |
|
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2552 |
initialize |
15921 | 2553 |
position := 0. |
2554 |
binary := false. |
|
2555 |
buffered := true. |
|
2556 |
eolMode := DefaultEOLMode. |
|
2557 |
hitEOF := false. |
|
15918
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2558 |
! ! |
d5fe9b9e87d3
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15896
diff
changeset
|
2559 |
|
2 | 2560 |
!ExternalStream methodsFor:'line reading/writing'! |
1 | 2561 |
|
2562 |
nextLine |
|
2563 |
"read the next line (characters up to newline). |
|
2564 |
Return a string containing those characters excluding the newline. |
|
2565 |
If the previous-to-last character is a cr, this is also removed, |
|
16508 | 2566 |
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
|
2567 |
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
|
2568 |
|
16296 | 2569 |
|line error| |
971
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
2570 |
|
12873 | 2571 |
%{ /* STACK:100000 */ |
1 | 2572 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2573 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
2574 |
int len, ret; |
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
2575 |
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
|
2576 |
char *rslt, *nextPtr, *limit; |
22 | 2577 |
int fd, ch; |
1068
92fdcccf12a3
alles MIST - zurueck zu 1.89 und NOCONTEXT entfernt
Claus Gittinger <cg@exept.de>
parents:
1067
diff
changeset
|
2578 |
int _buffered; |
216 | 2579 |
OBJ fp; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2580 |
int lineTooLong = 0; |
2920 | 2581 |
int cutOff = 0; |
1 | 2582 |
|
1133 | 2583 |
__INST(lastErrorNumber) = nil; |
12548 | 2584 |
if ((__INST(handleType) == nil) |
2585 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2586 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2587 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 2588 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 2589 |
if (((fp = __INST(handle)) != nil) |
2590 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2591 |
&& (__INST(binary) != true) |
|
2592 |
) { |
|
2593 |
f = __FILEVal(fp); |
|
2594 |
buffer[0] = '\0'; |
|
2595 |
||
2596 |
_buffered = (__INST(buffered) == true); |
|
2597 |
if (_buffered) { |
|
2598 |
__READING__(f); |
|
2599 |
} |
|
2600 |
||
2601 |
rslt = nextPtr = buffer; |
|
2602 |
limit = buffer + sizeof(buffer) - 2; |
|
2603 |
||
2604 |
for (;;) { |
|
2605 |
__READBYTE__(ret, f, nextPtr, _buffered, __INST(handleType)); |
|
2606 |
if (ret <= 0) { |
|
2607 |
if (nextPtr == buffer) |
|
2608 |
rslt = NULL; |
|
2609 |
if (ret == 0) { |
|
2610 |
__INST(hitEOF) = true; |
|
2611 |
break; |
|
2612 |
} else { |
|
2613 |
error = __mkSmallInteger(__threadErrno); |
|
2614 |
goto err; |
|
2615 |
} |
|
2616 |
} |
|
2617 |
||
2618 |
if (*nextPtr == '\n') { |
|
2619 |
cutOff = 1; |
|
2620 |
*nextPtr = '\0'; |
|
2621 |
break; |
|
2622 |
} |
|
2623 |
if (*nextPtr == '\r') { |
|
2624 |
char peekChar; |
|
2625 |
||
2626 |
/* |
|
2627 |
* peek ahead for a newLine ... |
|
2628 |
*/ |
|
2629 |
__READBYTE__(ret, f, &peekChar, _buffered, __INST(handleType)); |
|
2630 |
if (ret <= 0) { |
|
2631 |
cutOff = 1; |
|
2632 |
*nextPtr = '\0'; |
|
2633 |
if (ret == 0) { |
|
2634 |
__INST(hitEOF) = true; |
|
2635 |
break; |
|
2636 |
} |
|
2637 |
error = __mkSmallInteger(__threadErrno); |
|
2638 |
goto err; |
|
2639 |
} |
|
2640 |
||
2641 |
if (peekChar == '\n') { |
|
2642 |
cutOff = 2; |
|
2643 |
*nextPtr = '\0'; |
|
2644 |
break; |
|
2645 |
} |
|
2646 |
||
2647 |
__UNGETC__(peekChar, f, _buffered); |
|
2648 |
||
2649 |
cutOff = 1; |
|
2650 |
*nextPtr = '\0'; |
|
2651 |
break; |
|
2652 |
} |
|
2653 |
||
2654 |
nextPtr++; |
|
2655 |
if (nextPtr >= limit) { |
|
2656 |
*nextPtr = '\0'; |
|
2657 |
lineTooLong = 1; |
|
2658 |
if (@global(InfoPrinting) == true) { |
|
2659 |
fprintf(stderr, "ExtStream [warning]: line truncated in nextLine\n"); |
|
2660 |
} |
|
2661 |
break; |
|
2662 |
} |
|
2663 |
} |
|
2664 |
||
2665 |
if (rslt != NULL) { |
|
2666 |
len = nextPtr-buffer; |
|
2667 |
||
2668 |
if (__isSmallInteger(__INST(position))) { |
|
2669 |
INT np = __intVal(__INST(position)) + len + cutOff; |
|
2670 |
OBJ t; |
|
2671 |
||
2672 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
2673 |
} else { |
|
2674 |
__INST(position) = nil; /* i.e. do not know */ |
|
2675 |
} |
|
2676 |
/* remove any EOL character */ |
|
2677 |
if (len != 0) { |
|
2678 |
if (buffer[len-1] == '\n') { |
|
2679 |
buffer[--len] = '\0'; |
|
2680 |
} |
|
2681 |
if ((len != 0) && (buffer[len-1] == '\r')) { |
|
2682 |
buffer[--len] = '\0'; |
|
2683 |
} |
|
2684 |
} |
|
2685 |
line = __MKSTRING_L(buffer, len); |
|
2686 |
if (! lineTooLong) { |
|
2687 |
RETURN ( line ); |
|
2688 |
} |
|
2689 |
} |
|
2690 |
} |
|
1 | 2691 |
} |
1478
aae07192b7d1
labels in empty statements need a semi
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
2692 |
err: ; |
159 | 2693 |
%}. |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2694 |
line notNil ifTrue:[ |
18797 | 2695 |
"/ the line as read is longer than 32k characters (boy - what a line) |
2696 |
"/ The exception could be handled by reading more and returning the |
|
2697 |
"/ concatenation in your exception handler (the receiver and the partial |
|
2698 |
"/ line are passed as parameter) |
|
2699 |
||
2700 |
LineTooLongErrorSignal isHandled ifTrue:[ |
|
2701 |
^ LineTooLongErrorSignal |
|
2702 |
raiseRequestWith:(Array with:self with:line) |
|
2703 |
errorString:('line too long read error') |
|
2704 |
]. |
|
2705 |
^ line , self nextLine |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2706 |
]. |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
2707 |
|
8614 | 2708 |
(hitEOF == true) ifTrue:[^ self pastEndRead]. |
16296 | 2709 |
error notNil ifTrue:[ |
18797 | 2710 |
lastErrorNumber := error. |
2711 |
^ self readError:error |
|
16296 | 2712 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2713 |
handle isNil ifTrue:[^ self errorNotOpen]. |
1 | 2714 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
5431 | 2715 |
(binary == true) ifTrue:[^ self errorBinary]. |
2716 |
^ super nextLine |
|
1 | 2717 |
! |
2718 |
||
2719 |
nextPutLine:aString |
|
2920 | 2720 |
"write the characters in aString and append an end-of-Line marker |
2721 |
(LF, CR or CRLF - depending in the setting of eolMode)" |
|
1 | 2722 |
|
16296 | 2723 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
2724 |
%{ |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
2725 |
OBJ fp; |
1 | 2726 |
|
1133 | 2727 |
__INST(lastErrorNumber) = nil; |
12548 | 2728 |
if ((__INST(handleType) == nil) |
2729 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
2730 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
2731 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 2732 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 2733 |
if (((fp = __INST(handle)) != nil) |
2734 |
&& (__INST(mode) != @symbol(readonly)) |
|
2735 |
&& (__INST(binary) != true) |
|
2736 |
&& __isStringLike(aString) |
|
2737 |
) { |
|
2738 |
int _buffered = (__INST(buffered) == true); |
|
2739 |
int len = __stringSize(aString); |
|
2740 |
int cnt, len1; |
|
2741 |
FILEPOINTER f = __FILEVal(fp); |
|
2742 |
char *cp; |
|
2743 |
int o_offs; |
|
2744 |
||
2745 |
if (_buffered) { |
|
2746 |
__WRITING__(f) |
|
2747 |
} |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
2748 |
#ifdef WIN32 |
18797 | 2749 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
2750 |
cnt = __win32_fwrite(__stringVal(aString), 1, len, f); |
|
2751 |
} else |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2752 |
#endif |
18797 | 2753 |
{ |
2754 |
o_offs = (char *)__stringVal(aString)-(char *)aString; |
|
2755 |
__WRITEBYTES_OBJ__(cnt, f, aString, o_offs, len, _buffered, __INST(handleType)); |
|
2756 |
} |
|
2757 |
if (cnt == len) { |
|
2758 |
OBJ mode = __INST(eolMode); |
|
2759 |
||
2760 |
len1 = len; |
|
2761 |
||
2762 |
if (mode == @symbol(cr)) { |
|
2763 |
cp = "\r"; len = 1; |
|
2764 |
} else if (mode == @symbol(crlf)) { |
|
2765 |
cp = "\r\n"; len = 2; |
|
2766 |
} else if (mode == @symbol(eot)) { |
|
2767 |
cp = "\004"; len = 1; |
|
2768 |
} else if (mode == @symbol(etx)) { |
|
2769 |
cp = "\003"; len = 1; |
|
2770 |
} else { |
|
2771 |
cp = "\n"; len = 1; |
|
2772 |
} |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2773 |
#ifdef WIN32 |
18797 | 2774 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
2775 |
cnt = __win32_fwrite(cp, 1, len, f); |
|
2776 |
} else |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2777 |
#endif |
18797 | 2778 |
{ |
2779 |
__WRITEBYTES__(cnt, f, cp, len, _buffered, __INST(handleType)); |
|
2780 |
} |
|
2781 |
if (cnt > 0) { |
|
2782 |
if (__isSmallInteger(__INST(position))) { |
|
2783 |
INT np = __intVal(__INST(position)) + cnt; |
|
2784 |
OBJ t; |
|
2785 |
||
2786 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
2787 |
} else { |
|
2788 |
__INST(position) = nil; /* i.e. do not know */ |
|
2789 |
} |
|
2790 |
RETURN ( self ); |
|
2791 |
} |
|
2792 |
} |
|
2793 |
error = __mkSmallInteger(__threadErrno); |
|
2794 |
} |
|
1 | 2795 |
} |
159 | 2796 |
%}. |
16296 | 2797 |
error notNil ifTrue:[ |
18797 | 2798 |
lastErrorNumber := error. |
2799 |
self writeError:error. |
|
2800 |
^ self |
|
16296 | 2801 |
]. |
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
2802 |
super nextPutLine:aString. |
1 | 2803 |
! |
2804 |
||
2805 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil |
|
68 | 2806 |
"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
|
2807 |
and append all lines to self. |
159 | 2808 |
Can be used to copy/create large files or copy from a pipe/socket. |
68 | 2809 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2810 |
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
|
2811 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2812 |
|line| |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2813 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2814 |
[aStream atEnd] whileFalse:[ |
18797 | 2815 |
line := aStream nextLine. |
2816 |
line isNil ifTrue:[ |
|
2817 |
^ self. |
|
2818 |
]. |
|
2819 |
self nextPutLine:line. |
|
2820 |
(aStringOrNil notNil and:[line startsWith:aStringOrNil]) ifTrue:[ |
|
2821 |
^ self |
|
2822 |
] |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2823 |
]. |
1 | 2824 |
! |
2825 |
||
2826 |
peekForLineStartingWith:aString |
|
2827 |
"read ahead for next line starting with aString; |
|
159 | 2828 |
return the line-string if found, or nil if EOF is encountered. |
7118 | 2829 |
If matched, do not advance position beyond that line |
159 | 2830 |
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
|
2831 |
If not matched, reposition to original position for further reading." |
1 | 2832 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2833 |
|firstPos lastPos line| |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2834 |
|
1 | 2835 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2836 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2 | 2837 |
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
|
2838 |
buffered ifFalse:[^ self errorNotBuffered]. |
12 | 2839 |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2840 |
firstPos := self position. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2841 |
[self atEnd] whileFalse:[ |
18797 | 2842 |
lastPos := self position. |
2843 |
line := self nextLine. |
|
2844 |
line isNil ifTrue:[ |
|
2845 |
self position:firstPos. |
|
2846 |
^ nil |
|
2847 |
]. |
|
2848 |
(line startsWith:aString) ifTrue:[ |
|
2849 |
self position:lastPos. |
|
2850 |
^ line |
|
2851 |
] |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2852 |
]. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2853 |
self position:firstPos. |
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
2854 |
^ nil |
1 | 2855 |
! |
2856 |
||
2857 |
peekForLineStartingWithAny:aCollectionOfStrings |
|
2858 |
"read ahead for next line starting with any of aCollectionOfStrings; |
|
2859 |
return the index in aCollection if found, nil otherwise.. |
|
2860 |
If no match, do not change position; otherwise advance right before the |
|
2861 |
matched line so that nextLine will return this line." |
|
2862 |
||
2863 |
|line startPos linePos index| |
|
2864 |
||
2865 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2866 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2 | 2867 |
binary ifTrue:[^ self errorBinary]. |
2868 |
||
1 | 2869 |
startPos := self position. |
2870 |
[self atEnd] whileFalse:[ |
|
18797 | 2871 |
linePos := self position. |
2872 |
line := self nextLine. |
|
2873 |
line notNil ifTrue:[ |
|
2874 |
index := 1. |
|
2875 |
aCollectionOfStrings do:[:prefix | |
|
2876 |
(line startsWith:prefix) ifTrue:[ |
|
2877 |
self position:linePos. |
|
2878 |
^ index |
|
2879 |
]. |
|
2880 |
index := index + 1 |
|
2881 |
] |
|
2882 |
] |
|
1 | 2883 |
]. |
2884 |
self position:startPos. |
|
2885 |
^ nil |
|
2886 |
! ! |
|
2887 |
||
612 | 2888 |
!ExternalStream methodsFor:'misc functions'! |
2889 |
||
2890 |
async:aBoolean |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
2891 |
"set/clear the async attribute - if set, the availability of data on |
612 | 2892 |
the receiver will trigger an ioInterrupt. |
779 | 2893 |
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
|
2894 |
Notice: |
18797 | 2895 |
not every OS supports this |
2896 |
- check with OS>>supportsIOInterrupts before using" |
|
92 | 2897 |
|
2898 |
|fd| |
|
2899 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2900 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2901 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2902 |
OperatingSystem supportsIOInterrupts ifFalse:[ |
18797 | 2903 |
^ self errorUnsupportedOperation |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2904 |
]. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2905 |
|
92 | 2906 |
fd := self fileDescriptor. |
612 | 2907 |
aBoolean ifTrue:[ |
18797 | 2908 |
^ OperatingSystem enableIOInterruptsOn:fd |
612 | 2909 |
]. |
2910 |
^ OperatingSystem disableIOInterruptsOn:fd |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2911 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2912 |
"Modified: 11.1.1997 / 17:50:21 / cg" |
92 | 2913 |
! |
2914 |
||
612 | 2915 |
blocking:aBoolean |
2916 |
"set/clear the blocking attribute - if set (which is the default) |
|
2917 |
a read (using next) on the receiver will block until data is available. |
|
2918 |
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
|
2919 |
nil. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
2920 |
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
|
2921 |
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
|
2922 |
in advance. However, the data must then be read using #nextBytes: |
8616 | 2923 |
methods, in order to avoid other (pastEndRead) exceptions." |
92 | 2924 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
2925 |
handle isNil ifTrue:[^ self errorNotOpen]. |
7499
53b7a86e1f0b
ExternalStream>>blocking: returns previous blocking status
Stefan Vogel <sv@exept.de>
parents:
7497
diff
changeset
|
2926 |
^ 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
|
2927 |
|
3174 | 2928 |
"Modified: / 11.1.1997 / 17:48:01 / cg" |
2929 |
"Modified: / 15.1.1998 / 11:49:48 / stefan" |
|
612 | 2930 |
! |
2931 |
||
17618 | 2932 |
copyToEndFrom:inStream |
2933 |
"read from inStream, and write all data up to the end to the receiver. |
|
2934 |
Return the number of bytes which have been transferred. |
|
2935 |
Same functionality as copyToEnd:, but reversed arg and receiver |
|
2936 |
(useful in a cascade message of the writeStream)" |
|
2937 |
||
2938 |
|bufferSize| |
|
2939 |
||
2940 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
18797 | 2941 |
"/ mhmh - NT hangs, when copying bigger blocks to a network drive - why ? |
2942 |
bufferSize := 1 * 1024. |
|
17618 | 2943 |
] ifFalse:[ |
18797 | 2944 |
bufferSize := 8 * 1024. |
17618 | 2945 |
]. |
2946 |
||
2947 |
^ inStream copyToEndInto:self bufferSize:bufferSize |
|
2948 |
||
2949 |
" |
|
2950 |
|in out| |
|
2951 |
||
2952 |
in := 'Makefile' asFilename readStream. |
|
2953 |
out := Stdout. |
|
2954 |
in copyToEndInto:out. |
|
2955 |
in close. |
|
2956 |
" |
|
2957 |
! |
|
2958 |
||
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2959 |
copyToEndInto:outStream |
7017
03f1bd57c001
no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
7013
diff
changeset
|
2960 |
"copy the data into another stream." |
03f1bd57c001
no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
7013
diff
changeset
|
2961 |
|
8362 | 2962 |
|bufferSize| |
7700
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2963 |
|
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2964 |
OperatingSystem isMSDOSlike ifTrue:[ |
18797 | 2965 |
"/ mhmh - NT hangs, when copying bigger blocks to a network drive - why ? |
2966 |
bufferSize := 1 * 1024. |
|
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2967 |
] ifFalse:[ |
18797 | 2968 |
bufferSize := 8 * 1024. |
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2969 |
]. |
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2970 |
|
7017
03f1bd57c001
no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
7013
diff
changeset
|
2971 |
^ self copyToEndInto:outStream bufferSize:bufferSize |
7700
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 |
" |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2974 |
|in out| |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2975 |
|
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2976 |
in := 'Makefile' asFilename readStream. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2977 |
out := Stdout. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2978 |
in copyToEndInto:out. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2979 |
in close. |
767e11efc326
use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents:
7685
diff
changeset
|
2980 |
" |
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2981 |
! |
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
2982 |
|
612 | 2983 |
create |
2984 |
"create the stream |
|
2985 |
- this must be redefined in subclass" |
|
2986 |
||
2987 |
^ self subclassResponsibility |
|
2988 |
! |
|
2989 |
||
2990 |
ioctl:ioctlNumber |
|
2991 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2992 |
on the underlying file; no arguments are passed." |
|
2993 |
||
2920 | 2994 |
^ self ioctl:ioctlNumber with:nil |
2995 |
! |
|
2996 |
||
2997 |
ioctl:ioctlNumber with:arg |
|
2998 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2999 |
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
|
3000 |
This is not used by ST/X, but provided for special situations |
2920 | 3001 |
- for example, to control proprietrary I/O devices. |
3002 |
||
3003 |
Since the type of the argument depends on the ioctl being |
|
3004 |
performed, different arg types are allowed here. |
|
3005 |
If the argument is nil, an ioctl without argument is performed. |
|
16508 | 3006 |
If the argument is an integral number, it's directly passed; |
3007 |
if it's a kind of ByteArray (ByteArray, String or Structure), |
|
2920 | 3008 |
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
|
3009 |
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
|
3010 |
This allows performing most ioctls |
2920 | 3011 |
- however, it might be tricky to setup the buffer. |
3012 |
Be careful in what you pass - ST/X cannot validate its correctness." |
|
3013 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3014 |
|error| |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3015 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3016 |
%{ |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3017 |
#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
|
3018 |
int fd; |
10552
d52aa8e066e5
#ioctl:with: accept 32-bit integers as arguments
Stefan Vogel <sv@exept.de>
parents:
10469
diff
changeset
|
3019 |
int ret; |
d52aa8e066e5
#ioctl:with: accept 32-bit integers as arguments
Stefan Vogel <sv@exept.de>
parents:
10469
diff
changeset
|
3020 |
unsigned int ioNum; |
2920 | 3021 |
INT ioArg; |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3022 |
OBJ fp = __INST(handle); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3023 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3024 |
if (fp == nil) |
18797 | 3025 |
goto out; |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3026 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3027 |
if (!__isInteger(ioctlNumber) |
18797 | 3028 |
|| (!__isInteger(arg) |
3029 |
&& (arg != nil) |
|
3030 |
&& !__isBytes(arg) |
|
3031 |
&& !__isExternalBytesLike(arg) |
|
3032 |
&& !__isExternalAddress(arg))) { |
|
3033 |
error = @symbol(badArgument); |
|
3034 |
goto out; |
|
16368
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 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3037 |
if (__INST(handleType) == @symbol(socketHandle)) { |
18797 | 3038 |
fd = __FILEVal(fp); |
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3039 |
} else |
18797 | 3040 |
if ((__INST(handleType) == nil) |
3041 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3042 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3043 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3044 |
fd = fileno(__FILEVal(fp)); |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3045 |
} else { |
18797 | 3046 |
error = @symbol(badHandleType); |
3047 |
goto out; |
|
612 | 3048 |
} |
16508 | 3049 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3050 |
ioNum = __unsignedLongIntVal(ioctlNumber); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3051 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3052 |
__BEGIN_INTERRUPTABLE__ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3053 |
do { |
18797 | 3054 |
__threadErrno = 0; |
3055 |
if (arg == nil) { |
|
3056 |
ioArg = 0; |
|
3057 |
} else if (__isSmallInteger(arg)) { |
|
3058 |
ioArg = __intVal(arg); |
|
3059 |
} else if (__isInteger(arg)) { |
|
3060 |
ioArg = __unsignedLongIntVal(arg); |
|
3061 |
} else if (__isExternalBytesLike(arg)) { |
|
3062 |
ioArg = (INT)(__externalBytesAddress(arg)); |
|
3063 |
} else if (__isExternalAddress(arg)) { |
|
3064 |
ioArg = (INT)(__externalAddressVal(arg)); |
|
3065 |
} else { |
|
3066 |
ioArg = (INT)(__ByteArrayInstPtr(arg)->ba_element); |
|
3067 |
} |
|
3068 |
ret = ioctl(fd, ioNum, ioArg); |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3069 |
} while (ret < 0 && __threadErrno == EINTR); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3070 |
__END_INTERRUPTABLE__ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3071 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3072 |
if (ret >= 0) { |
18797 | 3073 |
RETURN ( __mkSmallInteger(ret) ); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3074 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3075 |
error = __mkSmallInteger(__threadErrno); |
791 | 3076 |
#endif |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3077 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3078 |
out:; |
612 | 3079 |
%}. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3080 |
error notNil ifTrue:[ |
18797 | 3081 |
lastErrorNumber := error. |
3082 |
^ self ioError:error. |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3083 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3084 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3085 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3086 |
"/ the system does not support ioctl (MSDOS or VMS) |
2920 | 3087 |
^ self errorUnsupportedOperation |
612 | 3088 |
! |
3089 |
||
16551 | 3090 |
reset |
3091 |
"set the read position to the beginning of the collection" |
|
3092 |
||
3093 |
self position:0 |
|
3094 |
! |
|
3095 |
||
3096 |
setToEnd |
|
3097 |
"redefined since it must be implemented differently" |
|
3098 |
||
3099 |
^ self subclassResponsibility |
|
3100 |
! |
|
3101 |
||
3102 |
sync |
|
3103 |
"make sure, that the OS writes cached data to the disk" |
|
3104 |
||
3105 |
|error| |
|
3106 |
||
3107 |
self flush. |
|
3108 |
||
3109 |
%{ |
|
3110 |
#if !defined(__openVMS__) |
|
3111 |
int fd; |
|
3112 |
int ret; |
|
3113 |
OBJ fp = __INST(handle); |
|
3114 |
||
3115 |
if (fp == nil) |
|
18797 | 3116 |
goto out; |
16551 | 3117 |
|
3118 |
if (__INST(handleType) == @symbol(socketHandle)) { |
|
18797 | 3119 |
// syncing a socket to disk ? |
3120 |
error = @symbol(badHandleType); |
|
3121 |
goto out; |
|
3122 |
// fd = __FILEVal(fp); |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3123 |
} else |
18797 | 3124 |
if ((__INST(handleType) == nil) |
3125 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3126 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3127 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3128 |
fd = fileno(__FILEVal(fp)); |
|
16551 | 3129 |
} else { |
18797 | 3130 |
error = @symbol(badHandleType); |
3131 |
goto out; |
|
16551 | 3132 |
} |
3133 |
||
3134 |
#ifdef WIN32 |
|
3135 |
__threadErrno = 0; |
|
3136 |
ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fd)); |
|
3137 |
if (ret) { |
|
18797 | 3138 |
RETURN (self); |
16551 | 3139 |
} |
3140 |
#else |
|
3141 |
__BEGIN_INTERRUPTABLE__ |
|
3142 |
do { |
|
18797 | 3143 |
ret = fsync(fd); |
16551 | 3144 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3145 |
__END_INTERRUPTABLE__ |
|
3146 |
||
3147 |
if (ret >= 0) { |
|
18797 | 3148 |
RETURN (self); |
16551 | 3149 |
} |
3150 |
#endif /* ! WIN32 */ |
|
3151 |
error = __mkSmallInteger(__threadErrno); |
|
3152 |
#endif /* ! __openVMS__ */ |
|
3153 |
out:; |
|
3154 |
%}. |
|
3155 |
error notNil ifTrue:[ |
|
18797 | 3156 |
lastErrorNumber := error. |
3157 |
self ioError:error. |
|
3158 |
^ self. |
|
16551 | 3159 |
]. |
3160 |
handle isNil ifTrue:[self errorNotOpen]. |
|
3161 |
||
3162 |
" |
|
18797 | 3163 |
|f| |
3164 |
f := 'x' asFilename writeStream. |
|
3165 |
f nextPutAll:'hallo'; sync; syncData; close |
|
16551 | 3166 |
" |
3167 |
! |
|
3168 |
||
3169 |
syncData |
|
3170 |
"make sure, that the OS writes cached data to the disk. |
|
3171 |
In this case, metadata is only written, if it is |
|
3172 |
required to read the file's data (so metadata will not be written, |
|
3173 |
if only access/modification time has changed)." |
|
3174 |
|error| |
|
3175 |
||
3176 |
self flush. |
|
3177 |
||
3178 |
%{ |
|
3179 |
#if !defined(__openVMS__) |
|
3180 |
int fd; |
|
3181 |
int ret; |
|
3182 |
OBJ fp = __INST(handle); |
|
3183 |
||
3184 |
if (fp == nil) |
|
18797 | 3185 |
goto out; |
16551 | 3186 |
|
3187 |
if (__INST(handleType) == @symbol(socketHandle)) { |
|
18797 | 3188 |
// syncing a socket to disk ? |
3189 |
error = @symbol(badHandleType); |
|
3190 |
goto out; |
|
3191 |
// fd = __FILEVal(fp); |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3192 |
} else |
18797 | 3193 |
if ((__INST(handleType) == nil) |
3194 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3195 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3196 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3197 |
fd = fileno(__FILEVal(fp)); |
|
16551 | 3198 |
} else { |
18797 | 3199 |
error = @symbol(badHandleType); |
3200 |
goto out; |
|
16551 | 3201 |
} |
3202 |
||
3203 |
#ifdef WIN32 |
|
3204 |
__threadErrno = 0; |
|
3205 |
ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fd)); |
|
3206 |
if (ret) { |
|
18797 | 3207 |
RETURN (self); |
16551 | 3208 |
} |
3209 |
#else |
|
3210 |
__BEGIN_INTERRUPTABLE__ |
|
3211 |
do { |
|
18797 | 3212 |
ret = fdatasync(fd); |
16551 | 3213 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3214 |
__END_INTERRUPTABLE__ |
|
3215 |
||
3216 |
if (ret >= 0) { |
|
18797 | 3217 |
RETURN (self); |
16551 | 3218 |
} |
3219 |
#endif /* ! WIN32 */ |
|
3220 |
error = __mkSmallInteger(__threadErrno); |
|
3221 |
#endif /* ! __openVMS__ */ |
|
3222 |
out:; |
|
3223 |
%}. |
|
3224 |
||
3225 |
error notNil ifTrue:[ |
|
18797 | 3226 |
lastErrorNumber := error. |
3227 |
self ioError:error. |
|
3228 |
^ self. |
|
16551 | 3229 |
]. |
3230 |
handle isNil ifTrue:[^ self errorNotOpen]. |
|
3231 |
||
3232 |
"if notdef HAS_DATASYNC, fall back" |
|
3233 |
self sync. |
|
3234 |
||
3235 |
" |
|
18797 | 3236 |
|f| |
3237 |
f := 'x' asFilename writeStream. |
|
3238 |
f nextPutAll:'hallo'; sync; syncData; close |
|
16551 | 3239 |
" |
3240 |
! |
|
3241 |
||
16508 | 3242 |
tcgetattr |
3243 |
"unix only: |
|
3244 |
provide the information of a tcgetattr call as a dictionary. |
|
3245 |
This provides terminal setting values of a tty or pty. |
|
3246 |
Used by terminal emulators. |
|
3247 |
On non-unix systems, an unsupported operation error is raised" |
|
3248 |
||
3249 |
|error ret |
|
3250 |
iflags "/ input flags |
|
3251 |
ignbrk brkint ignpar parmrk inpck istrip inlcr igncr |
|
16509
2cf139885242
Fixed ExternalStream>>tcgetattr
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16508
diff
changeset
|
3252 |
icrnl iuclc ixon ixoff ixany imaxbel iutf8 |
16508 | 3253 |
oflags "/ output flags |
3254 |
opost onlcr ocrnl olcuc onocr onlret oxtabs onoteot |
|
3255 |
cflags "/ control flags |
|
3256 |
csize cstopb cread parenb parodd hupcl clocal |
|
3257 |
lflags "/ local flags |
|
3258 |
echoke echoe echok echo echonl echoprt echoctl |
|
3259 |
isig icanon altwerase iexten extproc tostop |
|
3260 |
| |
|
3261 |
||
3262 |
%{ |
|
3263 |
#if !defined(MSDOS_LIKE) && !defined(__openVMS__) |
|
3264 |
int fd; |
|
3265 |
int _ret; |
|
3266 |
OBJ fp = __INST(handle); |
|
3267 |
struct termios t; |
|
3268 |
int sz; |
|
3269 |
||
3270 |
if (fp == nil) |
|
18797 | 3271 |
goto out; |
16508 | 3272 |
|
3273 |
if (__INST(handleType) == @symbol(socketHandle)) { |
|
18797 | 3274 |
// cg: termina attributes of a socket? |
3275 |
error = @symbol(badHandleType); |
|
3276 |
goto out; |
|
3277 |
// fd = __FILEVal(fp); |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3278 |
} else |
18797 | 3279 |
if ((__INST(handleType) == nil) |
3280 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3281 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
3282 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
3283 |
fd = fileno(__FILEVal(fp)); |
|
16508 | 3284 |
} else { |
18797 | 3285 |
error = @symbol(badHandleType); |
3286 |
goto out; |
|
16508 | 3287 |
} |
3288 |
||
3289 |
__BEGIN_INTERRUPTABLE__ |
|
3290 |
do { |
|
18797 | 3291 |
__threadErrno = 0; |
3292 |
_ret = tcgetattr(fd, &t); |
|
16508 | 3293 |
} while (_ret < 0 && __threadErrno == EINTR); |
3294 |
__END_INTERRUPTABLE__ |
|
3295 |
||
3296 |
if (_ret < 0) { |
|
18797 | 3297 |
error = __mkSmallInteger(__threadErrno); |
16508 | 3298 |
} |
3299 |
ignbrk = t.c_iflag & IGNBRK ? true : false; |
|
3300 |
brkint = t.c_iflag & BRKINT ? true : false; |
|
3301 |
ignpar = t.c_iflag & IGNPAR ? true : false; |
|
3302 |
parmrk = t.c_iflag & PARMRK ? true : false; |
|
3303 |
inpck = t.c_iflag & INPCK ? true : false; |
|
3304 |
istrip = t.c_iflag & ISTRIP ? true : false; |
|
3305 |
inlcr = t.c_iflag & INLCR ? true : false; |
|
3306 |
ixon = t.c_iflag & IXON ? true : false; |
|
3307 |
ixoff = t.c_iflag & IXOFF ? true : false; |
|
3308 |
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
|
3309 |
# ifdef IMAXBEL |
16508 | 3310 |
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
|
3311 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3312 |
# ifdef IUCLC |
16508 | 3313 |
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
|
3314 |
# endif |
16508 | 3315 |
|
3316 |
opost = t.c_oflag & OPOST ? true : false; |
|
3317 |
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
|
3318 |
# ifdef OXTABS |
16508 | 3319 |
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
|
3320 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3321 |
# ifdef ONOTEOT |
16508 | 3322 |
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
|
3323 |
# endif |
16508 | 3324 |
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
|
3325 |
# ifdef OLCUC |
16508 | 3326 |
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
|
3327 |
# endif |
16508 | 3328 |
onocr = t.c_oflag & ONOCR ? true : false; |
3329 |
onlret = t.c_oflag & ONLRET ? true : false; |
|
3330 |
||
3331 |
sz = t.c_cflag & CSIZE; |
|
3332 |
switch (sz) { |
|
18797 | 3333 |
case CS5: |
3334 |
csize = @symbol(cs5); |
|
3335 |
break; |
|
3336 |
case CS6: |
|
3337 |
csize = @symbol(cs6); |
|
3338 |
break; |
|
3339 |
case CS7: |
|
3340 |
csize = @symbol(cs7); |
|
3341 |
break; |
|
3342 |
case CS8: |
|
3343 |
csize = @symbol(cs8); |
|
3344 |
break; |
|
16508 | 3345 |
} |
3346 |
cstopb = t.c_cflag & CSTOPB ? true : false; |
|
3347 |
cread = t.c_cflag & CREAD ? true : false; |
|
3348 |
parenb = t.c_cflag & PARENB ? true : false; |
|
3349 |
parodd = t.c_cflag & PARODD ? true : false; |
|
3350 |
hupcl = t.c_cflag & HUPCL ? true : false; |
|
3351 |
clocal = t.c_cflag & CLOCAL ? true : false; |
|
3352 |
||
3353 |
echoke = t.c_lflag & ECHOKE ? true : false; |
|
3354 |
echoe = t.c_lflag & ECHOE ? true : false; |
|
3355 |
echo = t.c_lflag & ECHO ? true : false; |
|
3356 |
echonl = t.c_lflag & ECHONL ? true : false; |
|
3357 |
echoctl = t.c_lflag & ECHOCTL ? true : false; |
|
3358 |
isig = t.c_lflag & ISIG ? true : false; |
|
3359 |
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
|
3360 |
# ifdef ALTWERASE |
16508 | 3361 |
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
|
3362 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3363 |
# ifdef IEXTEN |
16508 | 3364 |
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
|
3365 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3366 |
# ifdef EXTPROC |
16508 | 3367 |
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
|
3368 |
# endif |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
3369 |
# ifdef TOSTOP |
16508 | 3370 |
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
|
3371 |
# endif |
16508 | 3372 |
|
3373 |
#else |
|
3374 |
error = @symbol(unsupportedOperation); |
|
3375 |
#endif |
|
3376 |
||
3377 |
out:; |
|
3378 |
%}. |
|
3379 |
error notNil ifTrue:[ |
|
18797 | 3380 |
error == #unsupportedOperation ifTrue:[ |
3381 |
"/ the system does not support tcgetattr |
|
3382 |
^ self errorUnsupportedOperation |
|
3383 |
]. |
|
3384 |
lastErrorNumber := error. |
|
3385 |
^ self ioError:error. |
|
16508 | 3386 |
]. |
3387 |
handle isNil ifTrue:[^ self errorNotOpen]. |
|
3388 |
||
3389 |
"/ construct a dictionary... |
|
3390 |
ret := Dictionary new. |
|
3391 |
ret at:#iflags put:(iflags := Dictionary new). |
|
3392 |
ret at:#oflags put:(oflags := Dictionary new). |
|
3393 |
ret at:#cflags put:(cflags := Dictionary new). |
|
3394 |
ret at:#lflags put:(lflags := Dictionary new). |
|
3395 |
iflags at:#ignbrk put:ignbrk. |
|
3396 |
iflags at:#brkint put:brkint. |
|
3397 |
iflags at:#ignpar put:ignpar. |
|
3398 |
iflags at:#parmrk put:parmrk. |
|
3399 |
iflags at:#inpck put:inpck. |
|
3400 |
iflags at:#istrip put:istrip. |
|
3401 |
iflags at:#inlcr put:inlcr. |
|
3402 |
iflags at:#igncr put:igncr. |
|
3403 |
iflags at:#icrnl put:icrnl. |
|
16509
2cf139885242
Fixed ExternalStream>>tcgetattr
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16508
diff
changeset
|
3404 |
iflags at:#uiclc put:iuclc. |
16508 | 3405 |
iflags at:#ixon put:ixon. |
3406 |
iflags at:#ixoff put:ixoff. |
|
3407 |
iflags at:#ixany put:ixany. |
|
3408 |
iflags at:#imaxbel put:imaxbel. |
|
3409 |
iflags at:#iutf8 put:iutf8. |
|
3410 |
||
3411 |
oflags at:#opost put:opost. |
|
3412 |
oflags at:#onlcr put:onlcr. |
|
3413 |
oflags at:#oxtabs put:oxtabs. |
|
3414 |
oflags at:#onoteot put:onoteot. |
|
3415 |
||
3416 |
cflags at:#csize put:csize. |
|
3417 |
cflags at:#cstopb put:cstopb. |
|
3418 |
cflags at:#cread put:cread. |
|
3419 |
cflags at:#parenb put:parenb. |
|
3420 |
cflags at:#parodd put:parodd. |
|
3421 |
cflags at:#hupcl put:hupcl. |
|
3422 |
cflags at:#clocal put:clocal. |
|
3423 |
||
3424 |
lflags at:#echoke put:echoke. |
|
3425 |
lflags at:#echoe put:echoe. |
|
3426 |
lflags at:#echok put:echok. |
|
3427 |
lflags at:#echo put:echo. |
|
3428 |
lflags at:#echonl put:echonl. |
|
3429 |
lflags at:#echoprt put:echoprt. |
|
3430 |
lflags at:#echoctl put:echoctl. |
|
3431 |
lflags at:#isig put:isig. |
|
3432 |
lflags at:#icanon put:icanon. |
|
3433 |
lflags at:#altwerase put:altwerase. |
|
3434 |
lflags at:#iexten put:iexten. |
|
3435 |
lflags at:#extproc put:extproc. |
|
3436 |
lflags at:#tostop put:tostop. |
|
3437 |
^ ret |
|
612 | 3438 |
! ! |
3439 |
||
3440 |
!ExternalStream methodsFor:'non homogenous reading'! |
|
3441 |
||
17609 | 3442 |
next:count into:aCollection startingAt:start |
3443 |
|readCount| |
|
3444 |
||
3445 |
readCount := self nextBytes:count into:aCollection startingAt:start. |
|
3446 |
readCount = count ifTrue:[ |
|
18797 | 3447 |
^ aCollection. |
17609 | 3448 |
]. |
3449 |
^ aCollection copyFrom:1 to:start+readCount-1. |
|
15254 | 3450 |
! |
3451 |
||
3674 | 3452 |
nextAvailable:count |
3453 |
"return the next count elements of the stream as aCollection. |
|
3454 |
If the stream reaches the end before count elements have been read, |
|
3455 |
return what is available. (i.e. a shorter collection). |
|
3456 |
The type of collection is specified in #contentsSpecies." |
|
3457 |
||
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3949
diff
changeset
|
3458 |
|buffer n| |
3674 | 3459 |
|
3460 |
binary ifTrue:[ |
|
18797 | 3461 |
buffer := ByteArray uninitializedNew:count |
3674 | 3462 |
] ifFalse:[ |
18797 | 3463 |
buffer := String uninitializedNew:count |
3674 | 3464 |
]. |
3465 |
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
|
3466 |
n == 0 ifTrue:[ |
18797 | 3467 |
binary ifTrue:[ |
3468 |
^ #[] |
|
3469 |
]. |
|
3470 |
^ '' |
|
5202
e8130049b18f
#nextAvailableBytes:into:startingAt: may NOT set EOF flag,
Claus Gittinger <cg@exept.de>
parents:
5187
diff
changeset
|
3471 |
]. |
3674 | 3472 |
|
3473 |
n ~~ count ifTrue:[ |
|
18797 | 3474 |
^ buffer copyTo:n |
3674 | 3475 |
]. |
3476 |
^ buffer. |
|
3477 |
||
3478 |
" |
|
8818
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:3 |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3480 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:10 |
3674 | 3481 |
(ReadStream on:'hello') nextAvailable:3 |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3482 |
(ReadStream on:'hello') nextAvailable:10 |
3674 | 3483 |
" |
3484 |
||
3485 |
"Modified: / 16.6.1998 / 15:52:41 / cg" |
|
3486 |
! |
|
3487 |
||
17608 | 3488 |
nextAvailable:count into:aCollection startingAt:pos |
3489 |
"return the next count elements of the stream into aCollection at position pos." |
|
3490 |
||
3491 |
^ self nextAvailableBytes:count into:aCollection startingAt:pos. |
|
3492 |
! |
|
3493 |
||
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3494 |
nextAvailableBytes:count into:anObject startingAt:start |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3495 |
"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
|
3496 |
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
|
3497 |
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
|
3498 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3499 |
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
|
3500 |
is raised if the connection is broken. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3501 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3502 |
Notice, that in contrast to other methods, |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3503 |
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
|
3504 |
Thus allowing read of partial blocks. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3505 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3506 |
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
|
3507 |
(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
|
3508 |
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
|
3509 |
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
|
3510 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3511 |
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
|
3512 |
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
|
3513 |
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
|
3514 |
|
16296 | 3515 |
|error| |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3516 |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3517 |
%{ |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3518 |
FILEPOINTER f; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3519 |
int cnt, offs, ret, _buffered; |
15987 | 3520 |
int objSize, nInstBytes; |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3521 |
char *cp; |
3445
2639c1e3fd0b
nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents:
3432
diff
changeset
|
3522 |
char *extPtr; |
7195
755c46f9b774
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3523 |
OBJ fp; |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3524 |
int o_offs; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3525 |
|
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3526 |
__INST(lastErrorNumber) = nil; |
12548 | 3527 |
if ((__INST(handleType) == nil) |
3528 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3529 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3530 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3531 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3532 |
if (((fp = __INST(handle)) != nil) |
3533 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3534 |
&& __bothSmallInteger(count, start) |
|
3535 |
) { |
|
3536 |
f = __FILEVal(fp); |
|
3537 |
||
3538 |
cnt = __intVal(count); |
|
3539 |
offs = __intVal(start) - 1; |
|
3540 |
||
3541 |
if (__isExternalBytesLike(anObject)) { |
|
3542 |
OBJ sz; |
|
3543 |
||
3544 |
nInstBytes = 0; |
|
3545 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
3546 |
if (extPtr == NULL) goto bad; |
|
3547 |
sz = __externalBytesSize(anObject); |
|
3548 |
if (__isSmallInteger(sz)) { |
|
3549 |
objSize = __intVal(sz); |
|
3550 |
} else { |
|
3551 |
objSize = 0; /* unknown */ |
|
3552 |
} |
|
3553 |
} else { |
|
3554 |
OBJ oClass = __Class(anObject); |
|
3555 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
3556 |
||
3557 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
3558 |
||
3559 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
3560 |
case BYTEARRAY: |
|
3561 |
case WORDARRAY: |
|
3562 |
case LONGARRAY: |
|
3563 |
case SWORDARRAY: |
|
3564 |
case SLONGARRAY: |
|
3565 |
case FLOATARRAY: |
|
3566 |
break; |
|
3567 |
case DOUBLEARRAY: |
|
15987 | 3568 |
#ifdef __NEED_DOUBLE_ALIGN |
18797 | 3569 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
15987 | 3570 |
#endif |
18797 | 3571 |
break; |
3572 |
case LONGLONGARRAY: |
|
3573 |
case SLONGLONGARRAY: |
|
16296 | 3574 |
#ifdef __NEED_LONGLONG_ALIGN |
18797 | 3575 |
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
|
3576 |
#endif |
18797 | 3577 |
break; |
3578 |
default: |
|
3579 |
goto bad; |
|
3580 |
} |
|
3581 |
extPtr = (char *)0; |
|
3582 |
objSize = __Size(anObject) - nInstBytes; |
|
3583 |
} |
|
3584 |
||
3585 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
3586 |
_buffered = (__INST(buffered) == true); |
|
3587 |
if (_buffered) { |
|
3588 |
__READING__(f); |
|
3589 |
} |
|
3590 |
||
3591 |
if (extPtr) { |
|
3592 |
__READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType)); |
|
3593 |
} else { |
|
3594 |
/* |
|
3595 |
* on interrupt, anObject may be moved to another location. |
|
3596 |
* So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__, |
|
3597 |
* to get a new address. |
|
3598 |
*/ |
|
3599 |
offs += nInstBytes; |
|
3600 |
__READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType)); |
|
3601 |
} |
|
3602 |
/* 0 is NOT an EOF condition here ... */ |
|
3603 |
if (ret >= 0) { |
|
3604 |
if (__isSmallInteger(__INST(position))) { |
|
3605 |
INT np = __intVal(__INST(position)) + ret; |
|
3606 |
OBJ t; |
|
3607 |
||
3608 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3609 |
} else { |
|
3610 |
__INST(position) = nil; /* i.e. do not know */ |
|
3611 |
} |
|
3612 |
RETURN (__mkSmallInteger(ret)); |
|
3613 |
} |
|
3614 |
__INST(position) = nil; |
|
3615 |
error = __mkSmallInteger(__threadErrno); |
|
3616 |
} |
|
3617 |
} |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3618 |
} |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3619 |
bad: ; |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3620 |
%}. |
8614 | 3621 |
(hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0]. |
16296 | 3622 |
error notNil ifTrue:[ |
18797 | 3623 |
lastErrorNumber := error. |
3624 |
^ self readError:error |
|
16296 | 3625 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3626 |
handle isNil ifTrue:[^ self errorNotOpen]. |
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3627 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3628 |
" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3629 |
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
|
3630 |
" |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3631 |
^ self primitiveFailed |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3632 |
! |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3633 |
|
612 | 3634 |
nextByte |
3635 |
"read the next byte and return it as an Integer; return nil on error. |
|
3636 |
This is allowed in both text and binary modes, always returning the |
|
3637 |
bytes binary value as an integer in 0..255." |
|
3638 |
||
16296 | 3639 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3640 |
%{ |
18364 | 3641 |
#ifdef __SCHTEAM__ |
3642 |
STObject handle = self.instVarAt(I_handle); |
|
3643 |
||
3644 |
if (handle != STObject.Nil) { |
|
18797 | 3645 |
STObject next; |
3646 |
||
3647 |
next = handle.nextByte(); |
|
3648 |
if (next != STObject.EOF) { |
|
3649 |
self.instVarAt_put(I_position, STObject.Nil); |
|
3650 |
return __c__._RETURN( next ); |
|
3651 |
} |
|
3652 |
self.instVarAt_put(I_hitEOF, STObject.True); |
|
18364 | 3653 |
} |
3654 |
#else |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3655 |
FILEPOINTER f; |
612 | 3656 |
unsigned char byte; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3657 |
int ret, _buffered; |
612 | 3658 |
OBJ fp; |
3659 |
||
1133 | 3660 |
__INST(lastErrorNumber) = nil; |
12548 | 3661 |
if ((__INST(handleType) == nil) |
3662 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3663 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3664 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3665 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3666 |
if (((fp = __INST(handle)) != nil) |
3667 |
&& (__INST(mode) != @symbol(writeonly))) { |
|
3668 |
f = __FILEVal(fp); |
|
3669 |
||
3670 |
_buffered = (__INST(buffered) == true); |
|
3671 |
if (_buffered) { |
|
3672 |
__READING__(f) |
|
3673 |
} |
|
3674 |
__READBYTE__(ret, f, &byte, _buffered, __INST(handleType)); |
|
3675 |
if (ret > 0) { |
|
3676 |
if (__isSmallInteger(__INST(position))) { |
|
3677 |
INT np = __intVal(__INST(position)) + 1; |
|
3678 |
OBJ t; |
|
3679 |
||
3680 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3681 |
} else { |
|
3682 |
__INST(position) = nil; /* i.e. do not know */ |
|
3683 |
} |
|
3684 |
RETURN (__mkSmallInteger(byte)); |
|
3685 |
} |
|
3686 |
||
3687 |
if (ret == 0) { |
|
3688 |
__INST(hitEOF) = true; |
|
3689 |
} else /* ret < 0 */ { |
|
3690 |
__INST(position) = nil; |
|
3691 |
error = __mkSmallInteger(__threadErrno); |
|
3692 |
} |
|
3693 |
} |
|
612 | 3694 |
} |
18364 | 3695 |
#endif /* not SCHTEAM */ |
612 | 3696 |
%}. |
8614 | 3697 |
hitEOF ifTrue:[^ self pastEndRead]. |
16296 | 3698 |
error notNil ifTrue:[ |
18797 | 3699 |
lastErrorNumber := error. |
3700 |
^ self readError:error |
|
16296 | 3701 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3702 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 3703 |
^ self errorWriteOnly |
3704 |
! |
|
3705 |
||
3706 |
nextBytes:count into:anObject startingAt:start |
|
3707 |
"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
|
3708 |
bytes read or the number of bytes read, if EOF is encountered before. |
612 | 3709 |
If the receiver is some socket/pipe-like stream, an exception |
3710 |
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
|
3711 |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3712 |
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
|
3713 |
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
|
3714 |
to only read whats there. |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
3715 |
|
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
3716 |
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
|
3717 |
this does NOT return nil on EOF, but the actual count. |
612 | 3718 |
Thus allowing read of partial blocks. |
3719 |
||
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3720 |
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
|
3721 |
(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
|
3722 |
or an externalBytes object (with known size). |
612 | 3723 |
If anObject is a string or byteArray and reused, this provides the |
3724 |
fastest possible physical I/O (since no new objects are allocated). |
|
3725 |
||
3726 |
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
|
3727 |
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
|
3728 |
architectures (unless you prepared the buffer with care), |
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3729 |
since it does not care for byte order or float representation." |
612 | 3730 |
|
16296 | 3731 |
|error| |
612 | 3732 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
3733 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
3734 |
int cnt, offs, ret, _buffered; |
15987 | 3735 |
int objSize, nInstBytes; |
3432
1228d4ba9518
allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents:
3420
diff
changeset
|
3736 |
char *extPtr; |
7195
755c46f9b774
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3737 |
OBJ fp; |
612 | 3738 |
|
1133 | 3739 |
__INST(lastErrorNumber) = nil; |
12548 | 3740 |
if ((__INST(handleType) == nil) |
3741 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3742 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3743 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3744 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3745 |
if (((fp = __INST(handle)) != nil) |
3746 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3747 |
&& __bothSmallInteger(count, start) |
|
3748 |
) { |
|
3749 |
f = __FILEVal(fp); |
|
3750 |
||
3751 |
cnt = __intVal(count); |
|
3752 |
offs = __intVal(start) - 1; |
|
3753 |
||
3754 |
if (__isExternalBytesLike(anObject)) { |
|
3755 |
OBJ sz; |
|
3756 |
||
3757 |
nInstBytes = 0; |
|
3758 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
3759 |
if (extPtr == NULL) goto bad; |
|
3760 |
sz = __externalBytesSize(anObject); |
|
3761 |
if (__isSmallInteger(sz)) { |
|
3762 |
objSize = __intVal(sz); |
|
3763 |
} else { |
|
3764 |
objSize = 0; /* unknown */ |
|
3765 |
} |
|
3766 |
} else { |
|
3767 |
OBJ oClass = __Class(anObject); |
|
3768 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
3769 |
||
3770 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
3771 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
3772 |
case BYTEARRAY: |
|
3773 |
case WORDARRAY: |
|
3774 |
case LONGARRAY: |
|
3775 |
case SWORDARRAY: |
|
3776 |
case SLONGARRAY: |
|
3777 |
case FLOATARRAY: |
|
3778 |
break; |
|
3779 |
case DOUBLEARRAY: |
|
15987 | 3780 |
#ifdef __NEED_DOUBLE_ALIGN |
18797 | 3781 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
15987 | 3782 |
#endif |
18797 | 3783 |
break; |
3784 |
case LONGLONGARRAY: |
|
3785 |
case SLONGLONGARRAY: |
|
16296 | 3786 |
#ifdef __NEED_LONGLONG_ALIGN |
18797 | 3787 |
nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1); |
16296 | 3788 |
#endif |
18797 | 3789 |
break; |
3790 |
default: |
|
3791 |
goto bad; |
|
3792 |
} |
|
3793 |
extPtr = (char *)0; |
|
3794 |
objSize = __Size(anObject) - nInstBytes; |
|
3795 |
} |
|
3796 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
3797 |
_buffered = (__INST(buffered) == true); |
|
3798 |
if (_buffered) { |
|
3799 |
__READING__(f); |
|
3800 |
} |
|
3801 |
||
3802 |
if (extPtr) { |
|
3803 |
__READBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType)); |
|
3804 |
} else { |
|
3805 |
/* |
|
3806 |
* on interrupt, anObject may be moved to another location. |
|
3807 |
* So we pass anObject, and the offset to the __READBYTES_OBJ__ macro. |
|
3808 |
*/ |
|
3809 |
offs += nInstBytes; |
|
3810 |
__READBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType)); |
|
3811 |
} |
|
3812 |
||
3813 |
if (ret > 0) { |
|
3814 |
if (__isSmallInteger(__INST(position))) { |
|
3815 |
INT np = __intVal(__INST(position)) + ret; |
|
3816 |
OBJ t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3817 |
} else { |
|
3818 |
__INST(position) = nil; /* i.e. do not know */ |
|
3819 |
} |
|
3820 |
RETURN (__mkSmallInteger(ret)); |
|
3821 |
} |
|
3822 |
if (ret == 0) { |
|
3823 |
__INST(hitEOF) = true; |
|
3824 |
} else /* ret < 0 */ { |
|
3825 |
__INST(position) = nil; |
|
3826 |
error = __mkSmallInteger(__threadErrno); |
|
3827 |
} |
|
3828 |
} |
|
3829 |
} |
|
612 | 3830 |
} |
3831 |
bad: ; |
|
3832 |
%}. |
|
8614 | 3833 |
(hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0]. |
16296 | 3834 |
error notNil ifTrue:[ |
18797 | 3835 |
lastErrorNumber := error. |
3836 |
^ self readError:error |
|
16296 | 3837 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3838 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 3839 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
3840 |
" |
|
3841 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
3842 |
" |
|
3843 |
^ self primitiveFailed |
|
92 | 3844 |
! |
3845 |
||
612 | 3846 |
nextLong |
3847 |
"Read four bytes (msb-first) and return the value as a 32-bit signed Integer. |
|
3848 |
The returned value may be a LargeInteger. |
|
3849 |
(msb-first for compatibility with other smalltalks)" |
|
3850 |
||
3851 |
^ self nextUnsignedLongMSB:true |
|
3852 |
! |
|
3853 |
||
3854 |
nextLongMSB:msbFlag |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3855 |
"Read four bytes and return the value as a 32-bit signed Integer, |
612 | 3856 |
which may be a LargeInteger. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
3857 |
If msbFlag is true, value is read with most-significant byte first, |
612 | 3858 |
otherwise least-significant byte comes first. |
3859 |
A nil is returned, if EOF is hit before all 4 bytes have been read. |
|
3860 |
Works in both binary and text modes." |
|
3861 |
||
16296 | 3862 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3863 |
%{ |
612 | 3864 |
OBJ fp; |
3865 |
||
1133 | 3866 |
__INST(lastErrorNumber) = nil; |
12548 | 3867 |
if ((__INST(handleType) == nil) |
3868 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3869 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3870 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3871 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3872 |
if (((fp = __INST(handle)) != nil) |
3873 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3874 |
) { |
|
3875 |
FILEPOINTER f; |
|
3876 |
int ret, _buffered; |
|
3877 |
int value; |
|
3878 |
union { |
|
3879 |
unsigned char buffer[4]; |
|
3880 |
int intVal; |
|
3881 |
} u; |
|
3882 |
||
3883 |
f = __FILEVal(fp); |
|
3884 |
_buffered = (__INST(buffered) == true); |
|
3885 |
if (_buffered) { |
|
3886 |
__READING__(f) |
|
3887 |
} |
|
3888 |
__READBYTES__(ret, f, u.buffer, 4, _buffered, __INST(handleType)); |
|
3889 |
||
3890 |
if (ret == 4) { |
|
3891 |
if (__isSmallInteger(__INST(position))) { |
|
3892 |
INT np = __intVal(__INST(position)) + 4; |
|
3893 |
OBJ t; |
|
3894 |
||
3895 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3896 |
} else { |
|
3897 |
__INST(position) = nil; /* i.e. do not know */ |
|
3898 |
} |
|
3899 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
3900 |
#if defined(__MSBFIRST__) |
18797 | 3901 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3902 |
#else |
18797 | 3903 |
value = (u.buffer[0] & 0xFF); |
3904 |
value = (value << 8) | (u.buffer[1] & 0xFF); |
|
3905 |
value = (value << 8) | (u.buffer[2] & 0xFF); |
|
3906 |
value = (value << 8) | (u.buffer[3] & 0xFF); |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3907 |
#endif |
18797 | 3908 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
3909 |
#if defined(__LSBFIRST__) |
18797 | 3910 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3911 |
#else |
18797 | 3912 |
value = (u.buffer[3] & 0xFF); |
3913 |
value = (value << 8) | (u.buffer[2] & 0xFF); |
|
3914 |
value = (value << 8) | (u.buffer[1] & 0xFF); |
|
3915 |
value = (value << 8) | (u.buffer[0] & 0xFF); |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3916 |
#endif |
18797 | 3917 |
} |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3918 |
#if __POINTER_SIZE__ == 8 |
18797 | 3919 |
RETURN ( __mkSmallInteger(value)); |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3920 |
#else |
18797 | 3921 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) { |
3922 |
RETURN ( __mkSmallInteger(value)); |
|
3923 |
} |
|
3924 |
RETURN ( __MKLARGEINT(value) ); |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
3925 |
#endif |
18797 | 3926 |
} |
3927 |
||
3928 |
if (ret < 0) { |
|
3929 |
__INST(position) = nil; |
|
3930 |
error = __mkSmallInteger(__threadErrno); |
|
3931 |
} else /* ret == 0 */ { |
|
3932 |
__INST(hitEOF) = true; |
|
3933 |
} |
|
3934 |
} |
|
612 | 3935 |
} |
3936 |
%}. |
|
8614 | 3937 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3938 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 3939 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 3940 |
lastErrorNumber := error. |
3941 |
^ self readError:error. |
|
362 | 3942 |
! |
3943 |
||
612 | 3944 |
nextShortMSB:msbFlag |
3945 |
"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
|
3946 |
If msbFlag is true, value is read with most-significant byte first, |
612 | 3947 |
otherwise least-significant byte comes first. |
3948 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
3949 |
Works in both binary and text modes." |
|
3950 |
||
16296 | 3951 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
3952 |
%{ |
612 | 3953 |
OBJ fp; |
3954 |
||
1133 | 3955 |
__INST(lastErrorNumber) = nil; |
12548 | 3956 |
if ((__INST(handleType) == nil) |
3957 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
3958 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
3959 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 3960 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 3961 |
if (((fp = __INST(handle)) != nil) |
3962 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3963 |
) { |
|
3964 |
FILEPOINTER f; |
|
3965 |
int ret, _buffered; |
|
3966 |
short value; |
|
3967 |
union { |
|
3968 |
unsigned char buffer[2]; |
|
3969 |
short shortVal; |
|
3970 |
} u; |
|
3971 |
||
3972 |
f = __FILEVal(fp); |
|
3973 |
_buffered = (__INST(buffered) == true); |
|
3974 |
if (_buffered) { |
|
3975 |
__READING__(f) |
|
3976 |
} |
|
3977 |
__READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType)); |
|
3978 |
||
3979 |
if (ret == 2) { |
|
3980 |
if (__isSmallInteger(__INST(position))) { |
|
3981 |
INT np = __intVal(__INST(position)) + 2; |
|
3982 |
OBJ t; |
|
3983 |
||
3984 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
3985 |
} else { |
|
3986 |
__INST(position) = nil; /* i.e. do not know */ |
|
3987 |
} |
|
3988 |
if (msbFlag == true) { |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3989 |
#if defined(__MSBFIRST__) |
18797 | 3990 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3991 |
#else |
18797 | 3992 |
value = ((u.buffer[0] & 0xFF) << 8) | (u.buffer[1] & 0xFF); |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3993 |
#endif |
18797 | 3994 |
} else { |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3995 |
#if defined(__LSBFIRST__) |
18797 | 3996 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3997 |
#else |
18797 | 3998 |
value = ((u.buffer[1] & 0xFF) << 8) | (u.buffer[0] & 0xFF); |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
3999 |
#endif |
18797 | 4000 |
} |
4001 |
RETURN (__mkSmallInteger(value)); |
|
4002 |
} |
|
4003 |
||
4004 |
if (ret < 0) { |
|
4005 |
__INST(position) = nil; /* i.e. do not know */ |
|
4006 |
error = __mkSmallInteger(__threadErrno); |
|
4007 |
} else /* ret == 0 */ { |
|
4008 |
__INST(hitEOF) = true; |
|
4009 |
} |
|
4010 |
} |
|
612 | 4011 |
} |
4012 |
%}. |
|
8614 | 4013 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4014 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 4015 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 4016 |
lastErrorNumber := error. |
4017 |
^ self readError:error. |
|
612 | 4018 |
! |
4019 |
||
4020 |
nextUnsignedLongMSB:msbFlag |
|
4021 |
"Read four bytes and return the value as a 32-bit unsigned Integer, which may be |
|
4022 |
a LargeInteger. |
|
4023 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
4024 |
least-significant byte comes first. |
|
4025 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read. |
|
4026 |
Works in both binary and text modes." |
|
4027 |
||
16296 | 4028 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4029 |
%{ |
612 | 4030 |
OBJ fp; |
4031 |
||
1133 | 4032 |
__INST(lastErrorNumber) = nil; |
12548 | 4033 |
if ((__INST(handleType) == nil) |
4034 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4035 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4036 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4037 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4038 |
if (((fp = __INST(handle)) != nil) |
4039 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4040 |
) { |
|
4041 |
FILEPOINTER f; |
|
4042 |
int ret, _buffered; |
|
4043 |
unsigned INT value; |
|
4044 |
union { |
|
4045 |
unsigned char buffer[4]; |
|
4046 |
unsigned int intVal; |
|
4047 |
} u; |
|
4048 |
||
4049 |
f = __FILEVal(fp); |
|
4050 |
_buffered = (__INST(buffered) == true); |
|
4051 |
if (_buffered) { |
|
4052 |
__READING__(f) |
|
4053 |
} |
|
4054 |
__READBYTES__(ret, f, u.buffer, 4, _buffered, __INST(handleType)); |
|
4055 |
||
4056 |
if (ret == 4) { |
|
4057 |
if (__isSmallInteger(__INST(position))) { |
|
4058 |
INT np = __intVal(__INST(position)) + 4; |
|
4059 |
OBJ t; |
|
4060 |
||
4061 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4062 |
} else { |
|
4063 |
__INST(position) = nil; /* i.e. do not know */ |
|
4064 |
} |
|
4065 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4066 |
#if defined(__MSBFIRST__) |
18797 | 4067 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4068 |
#else |
18797 | 4069 |
value = u.buffer[0]; |
4070 |
value = (value << 8) | u.buffer[1]; |
|
4071 |
value = (value << 8) | u.buffer[2]; |
|
4072 |
value = (value << 8) | u.buffer[3]; |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4073 |
#endif |
18797 | 4074 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4075 |
#if defined(__LSBFIRST__) |
18797 | 4076 |
value = u.intVal; |
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4077 |
#else |
18797 | 4078 |
value = u.buffer[3]; |
4079 |
value = (value << 8) | u.buffer[2]; |
|
4080 |
value = (value << 8) | u.buffer[1]; |
|
4081 |
value = (value << 8) | u.buffer[0]; |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4082 |
#endif |
18797 | 4083 |
} |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4084 |
#if __POINTER_SIZE__ == 8 |
18797 | 4085 |
value &= 0xFFFFFFFF; |
4086 |
RETURN (__mkSmallInteger(value)); |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4087 |
#else |
18797 | 4088 |
if (value <= _MAX_INT) { |
4089 |
RETURN (__mkSmallInteger(value)); |
|
4090 |
} |
|
4091 |
RETURN (__MKULARGEINT(value) ); |
|
4383
7a5abc09fb39
slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents:
4368
diff
changeset
|
4092 |
#endif |
18797 | 4093 |
} |
4094 |
||
4095 |
if (ret < 0) { |
|
4096 |
__INST(position) = nil; /* i.e. do not know */ |
|
4097 |
error = __mkSmallInteger(__threadErrno); |
|
4098 |
} else /* ret == 0 */ { |
|
4099 |
__INST(hitEOF) = true; |
|
4100 |
} |
|
4101 |
} |
|
612 | 4102 |
} |
4103 |
%}. |
|
8614 | 4104 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4105 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 4106 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 4107 |
lastErrorNumber := error. |
4108 |
^ self readError:error. |
|
612 | 4109 |
! |
4110 |
||
4111 |
nextUnsignedShortMSB:msbFlag |
|
4112 |
"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
|
4113 |
If msbFlag is true, value is read with most-significant byte first, |
1079 | 4114 |
otherwise least-significant byte comes first. |
612 | 4115 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
4116 |
Works in both binary and text modes." |
|
4117 |
||
16296 | 4118 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4119 |
%{ |
612 | 4120 |
OBJ fp; |
4121 |
||
1133 | 4122 |
__INST(lastErrorNumber) = nil; |
12548 | 4123 |
if ((__INST(handleType) == nil) |
4124 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4125 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4126 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4127 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4128 |
if (((fp = __INST(handle)) != nil) |
4129 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4130 |
) { |
|
4131 |
FILEPOINTER f; |
|
4132 |
int ret, _buffered; |
|
4133 |
unsigned int value; |
|
4134 |
union { |
|
4135 |
unsigned char buffer[2]; |
|
4136 |
unsigned short shortVal; |
|
4137 |
} u; |
|
4138 |
||
4139 |
f = __FILEVal(fp); |
|
4140 |
_buffered = (__INST(buffered) == true); |
|
4141 |
if (_buffered) { |
|
4142 |
__READING__(f) |
|
4143 |
} |
|
4144 |
__READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType)); |
|
4145 |
||
4146 |
if (ret == 2) { |
|
4147 |
if (__isSmallInteger(__INST(position))) { |
|
4148 |
INT np = __intVal(__INST(position)) + 2; |
|
4149 |
OBJ t; |
|
4150 |
||
4151 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4152 |
} else { |
|
4153 |
__INST(position) = nil; /* i.e. do not know */ |
|
4154 |
} |
|
4155 |
if (msbFlag == true) { |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4156 |
#if defined(__MSBFIRST__) |
18797 | 4157 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4158 |
#else |
18797 | 4159 |
value = (u.buffer[0] << 8) | u.buffer[1]; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4160 |
#endif |
18797 | 4161 |
} else { |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4162 |
#if defined(__LSBFIRST__) |
18797 | 4163 |
value = u.shortVal; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4164 |
#else |
18797 | 4165 |
value = (u.buffer[1] << 8) | u.buffer[0]; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4166 |
#endif |
18797 | 4167 |
} |
4168 |
RETURN (__mkSmallInteger(value)); |
|
4169 |
} |
|
4170 |
||
4171 |
if (ret < 0) { |
|
4172 |
__INST(position) = nil; /* i.e. do not know */ |
|
4173 |
error = __mkSmallInteger(__threadErrno); |
|
4174 |
} else /* ret == 0 */ { |
|
4175 |
__INST(hitEOF) = true; |
|
4176 |
} |
|
4177 |
} |
|
612 | 4178 |
} |
4179 |
%}. |
|
8614 | 4180 |
hitEOF ifTrue:[^ self pastEndRead]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4181 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 4182 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
16296 | 4183 |
lastErrorNumber := error. |
4184 |
^ self readError:error. |
|
612 | 4185 |
! |
4186 |
||
4187 |
nextWord |
|
15452 | 4188 |
<resource: #obsolete> |
612 | 4189 |
"in text-mode: |
18797 | 4190 |
read the alphaNumeric next word (i.e. up to non letter-or-digit). |
4191 |
return a string containing those characters. |
|
612 | 4192 |
in binary-mode: |
18797 | 4193 |
read two bytes (msb-first) and return the value as a 16-bit |
4194 |
unsigned Integer (for compatibility with other smalltalks)" |
|
612 | 4195 |
|
4196 |
binary ifTrue:[ |
|
18797 | 4197 |
self obsoleteMethodWarning:'use #nextUnsignedShortMSB:true'. |
4198 |
^ self nextUnsignedShortMSB:true |
|
92 | 4199 |
]. |
14518 | 4200 |
self obsoleteMethodWarning:'use #nextAlphaNumericWord'. |
612 | 4201 |
^ self nextAlphaNumericWord |
4202 |
! ! |
|
4203 |
||
4204 |
!ExternalStream methodsFor:'non homogenous writing'! |
|
4205 |
||
4206 |
nextPutByte:aByteValue |
|
4207 |
"write a byte. |
|
4208 |
Works in both binary and text modes." |
|
4209 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4210 |
|error| |
16296 | 4211 |
|
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4212 |
%{ |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
4213 |
OBJ fp; |
612 | 4214 |
|
1133 | 4215 |
__INST(lastErrorNumber) = nil; |
12548 | 4216 |
if ((__INST(handleType) == nil) |
4217 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4218 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4219 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4220 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4221 |
if (((fp = __INST(handle)) != nil) |
4222 |
&& (__INST(mode) != @symbol(readonly)) |
|
4223 |
&& __isSmallInteger(aByteValue) |
|
4224 |
||
4225 |
) { |
|
4226 |
int _buffered = (__INST(buffered) == true); |
|
4227 |
FILEPOINTER f = __FILEVal(fp); |
|
4228 |
char c = __intVal(aByteValue); |
|
4229 |
int cnt; |
|
4230 |
||
4231 |
if (_buffered) { |
|
4232 |
__WRITING__(f) |
|
4233 |
} |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
4234 |
#ifdef WIN32 |
18797 | 4235 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
4236 |
cnt = __win32_fwrite(&c, 1, 1, f); |
|
4237 |
} else |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
4238 |
#endif |
18797 | 4239 |
{ |
4240 |
__WRITEBYTE__(cnt, f, &c, _buffered, __INST(handleType)); |
|
4241 |
} |
|
4242 |
if (cnt == 1) { |
|
4243 |
if (__isSmallInteger(__INST(position))) { |
|
4244 |
INT np = __intVal(__INST(position)) + 1; |
|
4245 |
OBJ t; |
|
4246 |
||
4247 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4248 |
} else { |
|
4249 |
__INST(position) = nil; /* i.e. do not know */ |
|
4250 |
} |
|
4251 |
RETURN (self); |
|
4252 |
} |
|
4253 |
if (cnt < 0) { |
|
4254 |
__INST(position) = nil; /* i.e. do not know */ |
|
4255 |
} |
|
4256 |
error = __mkSmallInteger(__threadErrno); |
|
4257 |
} |
|
612 | 4258 |
} |
4259 |
%}. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4260 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
7801
d930c353588d
writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents:
7737
diff
changeset
|
4261 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
16296 | 4262 |
lastErrorNumber := error. |
4263 |
self writeError:error. |
|
612 | 4264 |
! |
4265 |
||
4266 |
nextPutLong:aNumber MSB:msbFlag |
|
4267 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
4268 |
true, data is written most-significant byte first; otherwise least |
|
4269 |
first. |
|
4270 |
Works in both binary and text modes." |
|
4271 |
||
16296 | 4272 |
|error| |
4273 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4274 |
%{ |
612 | 4275 |
int num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4276 |
union { |
18797 | 4277 |
char bytes[4]; |
4278 |
int intVal; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4279 |
} u; |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4280 |
OBJ fp; |
612 | 4281 |
|
1133 | 4282 |
__INST(lastErrorNumber) = nil; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4283 |
if (__isSmallInteger(aNumber)) { |
18797 | 4284 |
num = __intVal(aNumber); |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4285 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4286 |
#if __POINTER_SIZE__ == 8 |
18797 | 4287 |
// always more than 4-bytes |
4288 |
goto badArg; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4289 |
#else |
18797 | 4290 |
num = __longIntVal(aNumber); |
4291 |
if (num == 0) { |
|
4292 |
num = __signedLongIntVal(aNumber); |
|
4293 |
if (num == 0) { |
|
4294 |
/* bad arg or out-of-range integer |
|
4295 |
* (handled by the fallBack code) |
|
4296 |
*/ |
|
4297 |
goto badArg; |
|
4298 |
} |
|
4299 |
} |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4300 |
#endif |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4301 |
} |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4302 |
|
12548 | 4303 |
if ((__INST(handleType) == nil) |
4304 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4305 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4306 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4307 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4308 |
if (((fp = __INST(handle)) != nil) |
4309 |
&& (__INST(mode) != @symbol(readonly)) |
|
4310 |
) { |
|
4311 |
int _buffered = (__INST(buffered) == true); |
|
4312 |
FILEPOINTER f = __FILEVal(fp); |
|
4313 |
int cnt; |
|
4314 |
||
4315 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4316 |
#if defined(__MSBFIRST__) |
18797 | 4317 |
u.intVal = num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4318 |
#else |
18797 | 4319 |
u.bytes[0] = (num >> 24) & 0xFF; |
4320 |
u.bytes[1] = (num >> 16) & 0xFF; |
|
4321 |
u.bytes[2] = (num >> 8) & 0xFF; |
|
4322 |
u.bytes[3] = num & 0xFF; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4323 |
#endif |
18797 | 4324 |
} else { |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4325 |
#if defined(__LSBFIRST__) |
18797 | 4326 |
u.intVal = num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4327 |
#else |
18797 | 4328 |
u.bytes[3] = (num >> 24) & 0xFF; |
4329 |
u.bytes[2] = (num >> 16) & 0xFF; |
|
4330 |
u.bytes[1] = (num >> 8) & 0xFF; |
|
4331 |
u.bytes[0] = num & 0xFF; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4332 |
#endif |
18797 | 4333 |
} |
4334 |
||
4335 |
if (_buffered) { |
|
4336 |
__WRITING__(f) |
|
4337 |
} |
|
4338 |
__WRITEBYTES__(cnt, f, u.bytes, 4, _buffered, __INST(handleType)); |
|
4339 |
||
4340 |
if (cnt == 4) { |
|
4341 |
if (__isSmallInteger(__INST(position))) { |
|
4342 |
INT np = __intVal(__INST(position)) + 4; |
|
4343 |
OBJ t; |
|
4344 |
||
4345 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4346 |
} else { |
|
4347 |
__INST(position) = nil; /* i.e. do not know */ |
|
4348 |
} |
|
4349 |
RETURN ( self ); |
|
4350 |
} |
|
4351 |
__INST(position) = nil; /* i.e. do not know */ |
|
4352 |
error = __mkSmallInteger(__threadErrno); |
|
4353 |
} |
|
612 | 4354 |
} |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4355 |
badArg: ; |
612 | 4356 |
%}. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4357 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
7801
d930c353588d
writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents:
7737
diff
changeset
|
4358 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
16296 | 4359 |
error notNil ifTrue:[ |
18797 | 4360 |
lastErrorNumber := error. |
4361 |
self writeError:error. |
|
4362 |
^ self |
|
16296 | 4363 |
]. |
612 | 4364 |
|
4365 |
aNumber isInteger ifTrue:[ |
|
18797 | 4366 |
^ super nextPutLong:aNumber MSB:msbFlag |
92 | 4367 |
]. |
612 | 4368 |
self argumentMustBeInteger |
4369 |
! |
|
4370 |
||
15655 | 4371 |
nextPutShort:anIntegerOrCharacter MSB:msbFlag |
4372 |
"Write the argument, anIntegerOrCharacter as a short (two bytes). If msbFlag is |
|
612 | 4373 |
true, data is written most-significant byte first; otherwise least |
4374 |
first. |
|
4375 |
Works in both binary and text modes." |
|
4376 |
||
16296 | 4377 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4378 |
%{ |
612 | 4379 |
int num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4380 |
union { |
18797 | 4381 |
char bytes[2]; |
4382 |
short shortVal; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4383 |
} u; |
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4384 |
OBJ fp; |
612 | 4385 |
|
1133 | 4386 |
__INST(lastErrorNumber) = nil; |
12548 | 4387 |
if ((__INST(handleType) == nil) |
4388 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4389 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4390 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4391 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4392 |
if (((fp = __INST(handle)) != nil) |
4393 |
&& (__INST(mode) != @symbol(readonly)) |
|
4394 |
) { |
|
4395 |
FILEPOINTER f = __FILEVal(fp); |
|
4396 |
int _buffered = (__INST(buffered) == true); |
|
4397 |
int cnt; |
|
4398 |
||
4399 |
if (__isSmallInteger(anIntegerOrCharacter)) { |
|
4400 |
num = __intVal(anIntegerOrCharacter); |
|
4401 |
} else if (__isCharacter(anIntegerOrCharacter)) { |
|
4402 |
num = __smallIntegerVal(__characterVal(anIntegerOrCharacter)); |
|
4403 |
} else |
|
4404 |
goto out; |
|
4405 |
||
4406 |
if (msbFlag == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
4407 |
#if defined(__MSBFIRST__) |
18797 | 4408 |
u.shortVal = num; |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4409 |
#else |
18797 | 4410 |
u.bytes[0] = (num >> 8) & 0xFF; |
4411 |
u.bytes[1] = num & 0xFF; |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4412 |
#endif |
18797 | 4413 |
} else { |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4414 |
#if defined(__LSBFIRST__) |
18797 | 4415 |
u.shortVal = num; |
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4416 |
#else |
18797 | 4417 |
u.bytes[1] = (num >> 8) & 0xFF; |
4418 |
u.bytes[0] = num & 0xFF; |
|
4384
f8e779b99b14
slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents:
4383
diff
changeset
|
4419 |
#endif |
18797 | 4420 |
} |
4421 |
||
4422 |
if (_buffered) { |
|
4423 |
__WRITING__(f) |
|
4424 |
} |
|
4425 |
__WRITEBYTES__(cnt, f, u.bytes, 2, _buffered, __INST(handleType)); |
|
4426 |
||
4427 |
if (cnt == 2) { |
|
4428 |
if (__isSmallInteger(__INST(position))) { |
|
4429 |
INT np = __intVal(__INST(position)) + 2; |
|
4430 |
OBJ t; |
|
4431 |
||
4432 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
4433 |
} else { |
|
4434 |
__INST(position) = nil; /* i.e. do not know */ |
|
4435 |
} |
|
4436 |
RETURN ( self ); |
|
4437 |
} |
|
4438 |
__INST(position) = nil; /* i.e. do not know */ |
|
4439 |
error = __mkSmallInteger(__threadErrno); |
|
4440 |
} |
|
612 | 4441 |
} |
15655 | 4442 |
out:; |
612 | 4443 |
%}. |
16296 | 4444 |
error notNil ifTrue:[ |
18797 | 4445 |
lastErrorNumber := error. |
4446 |
self writeError:error. |
|
4447 |
^ self |
|
16296 | 4448 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4449 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
7801
d930c353588d
writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents:
7737
diff
changeset
|
4450 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
612 | 4451 |
self argumentMustBeInteger |
10 | 4452 |
! ! |
612 | 4453 |
|
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4454 |
!ExternalStream methodsFor:'positioning'! |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4455 |
|
15619 | 4456 |
position |
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4457 |
self subclassResponsibility |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4458 |
! |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4459 |
|
15619 | 4460 |
position:index0Based |
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4461 |
self subclassResponsibility |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4462 |
! ! |
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
4463 |
|
12631 | 4464 |
!ExternalStream methodsFor:'printing & storing'! |
4465 |
||
4466 |
printOn:aStream |
|
14068 | 4467 |
aStream |
18797 | 4468 |
nextPutAll:self className; |
4469 |
nextPutAll:'(handle:('. |
|
12631 | 4470 |
handle printOn:aStream. |
4471 |
aStream nextPutAll:'))' |
|
4472 |
! ! |
|
4473 |
||
612 | 4474 |
!ExternalStream methodsFor:'private'! |
4475 |
||
4476 |
clearEOF |
|
4477 |
hitEOF := false |
|
18783 | 4478 |
! ! |
4479 |
||
4480 |
!ExternalStream protectedMethodsFor:'private'! |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4481 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4482 |
closeFile |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4483 |
"low level close - may be redefined in subclasses |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4484 |
Don't send this message, send #close instead" |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4485 |
|
12674 | 4486 |
|fp error| |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4487 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4488 |
fp := handle. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4489 |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4490 |
%{ |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4491 |
int rslt; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4492 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4493 |
if (fp == nil) { |
18797 | 4494 |
error = @symbol(errorNotOpen); |
4495 |
goto out; |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4496 |
} |
16508 | 4497 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4498 |
if (__INST(handleType) == @symbol(socketHandle)) { |
18797 | 4499 |
SOCKET sock = __FILEVal(fp); |
4500 |
||
4501 |
if (@global(FileOpenTrace) == true) { |
|
4502 |
fprintf(stderr, "close socket [ExternalStream] %"_lx_"\n", (INT)sock); |
|
4503 |
} |
|
4504 |
||
4505 |
// whether the close() will be successful or not - the handle is invalid now! |
|
4506 |
__INST(handle) = nil; |
|
4507 |
do { |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4508 |
#ifdef WIN32 |
18797 | 4509 |
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
|
4510 |
#else |
18797 | 4511 |
rslt = close(sock); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4512 |
#endif |
18797 | 4513 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4514 |
} else if ((__INST(handleType) == nil) |
18797 | 4515 |
|| (__INST(handleType) == @symbol(filePointer)) |
4516 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
4517 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4518 |
{ |
18797 | 4519 |
FILEPOINTER f = __FILEVal(fp); |
4520 |
||
4521 |
if (@global(FileOpenTrace) == true) { |
|
4522 |
fprintf(stderr, "fclose [ExternalStream] %"_lx_"\n", (INT)f); |
|
4523 |
} |
|
4524 |
// whether the close() will be successful or not - the handle is invalid now! |
|
4525 |
__INST(handle) = nil; |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4526 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4527 |
#ifdef WIN32 |
18797 | 4528 |
if (__INST(mode) != @symbol(readonly) && __INST(buffered) != false) { |
4529 |
// do a fflush() first, so that fclose() doesn't block |
|
4530 |
// we suspect, that EINTR causes problems in fclose() |
|
4531 |
do { |
|
4532 |
__threadErrno = 0; |
|
4533 |
rslt = __STX_C_CALL1("fflush", fflush, f); |
|
4534 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
|
4535 |
} |
|
4536 |
do { |
|
4537 |
__threadErrno = 0; |
|
4538 |
rslt = __STX_C_NOINT_CALL1("fclose", fclose, f); |
|
4539 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4540 |
#else |
18797 | 4541 |
// cg: the pre Nov2014 code always did the fclose interruptable; |
4542 |
// I am not sure, if fclose is actually prepared to do this; |
|
4543 |
// at least when only reading, this should not block, and we |
|
4544 |
// should be ableto do it without being interruptable. |
|
4545 |
// Must watch this - if it leads to blockings, change and think about it. |
|
4546 |
if (__INST(mode) != @symbol(readonly)) { |
|
4547 |
__BEGIN_INTERRUPTABLE__ |
|
4548 |
rslt = fclose(f); |
|
4549 |
__END_INTERRUPTABLE__ |
|
4550 |
} else { |
|
4551 |
rslt = fclose(f); |
|
4552 |
} |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4553 |
#endif |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4554 |
} else { |
18797 | 4555 |
error = @symbol(badHandleType); |
4556 |
goto out; |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4557 |
} |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4558 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4559 |
if (rslt < 0) { |
18797 | 4560 |
error = __mkSmallInteger(__threadErrno); |
4561 |
goto out; |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4562 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4563 |
RETURN (self); |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4564 |
|
12674 | 4565 |
out:; |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4566 |
%}. |
12674 | 4567 |
|
4568 |
error notNil ifTrue:[ |
|
18797 | 4569 |
error == #errorNotOpen ifTrue:[ |
4570 |
self errorNotOpen. |
|
4571 |
]. |
|
4572 |
error isInteger ifTrue:[ |
|
4573 |
lastErrorNumber := error. |
|
4574 |
mode == #readonly ifTrue:[ |
|
4575 |
self ioError:error. |
|
4576 |
] ifFalse:[ |
|
4577 |
self writeError:error. |
|
4578 |
]. |
|
4579 |
^ self. |
|
4580 |
]. |
|
4581 |
self primitiveFailed:error. |
|
4582 |
^ self. |
|
12674 | 4583 |
]. |
4584 |
||
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4585 |
"/ fallback for rel5 |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4586 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4587 |
fp := handle. |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4588 |
fp notNil ifTrue:[ |
18797 | 4589 |
handle := nil. |
4590 |
self closeFile:fp |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4591 |
] |
18783 | 4592 |
! ! |
4593 |
||
4594 |
!ExternalStream methodsFor:'private'! |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4595 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4596 |
closeFile:handle |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4597 |
"for rel5 only" |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4598 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4599 |
self primitiveFailed |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4600 |
! |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4601 |
|
12706 | 4602 |
connectTo:aFileDescriptor withMode:modeSymbol handleType:handleTypeSymbol |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4603 |
"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
|
4604 |
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
|
4605 |
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
|
4606 |
a Stream object. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4607 |
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
|
4608 |
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
|
4609 |
|
12706 | 4610 |
|retVal error openmode| |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4611 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4612 |
handle notNil ifTrue:[^ self errorAlreadyOpen]. |
12706 | 4613 |
mode := modeSymbol. |
4614 |
openmode := self fopenMode. |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4615 |
%{ |
12683 | 4616 |
FILEPOINTER f = 0; |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4617 |
OBJ fp; |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4618 |
FILE *fdopen(); |
12683 | 4619 |
int fd = -2; |
4620 |
||
4621 |
if (__isStringLike(openmode)) { |
|
12688 | 4622 |
#ifdef WIN32 |
18779 | 4623 |
__stxWrapApiEnterCritical(); |
12688 | 4624 |
#endif |
18779 | 4625 |
if (__isSmallInteger(aFileDescriptor)) { |
4626 |
fd = __intVal(aFileDescriptor); |
|
4627 |
} |
|
12683 | 4628 |
#ifdef WIN32 |
18779 | 4629 |
else if (__isExternalAddressLike(aFileDescriptor)) { |
4630 |
fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY); |
|
4631 |
if (fd < 0) { |
|
4632 |
if (__threadErrno == 0) { |
|
4633 |
// no more file descriptors |
|
4634 |
__threadErrno = EMFILE; |
|
4635 |
} |
|
4636 |
error = __mkSmallInteger(__threadErrno); |
|
4637 |
__stxWrapApiLeaveCritical(); |
|
4638 |
goto out; |
|
4639 |
} |
|
4640 |
} |
|
12683 | 4641 |
#endif |
18779 | 4642 |
f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode)); |
12688 | 4643 |
#ifdef WIN32 |
18779 | 4644 |
__stxWrapApiLeaveCritical(); |
12688 | 4645 |
#endif |
18779 | 4646 |
if (f == NULL) { |
4647 |
error =__mkSmallInteger(__threadErrno); |
|
4648 |
} else { |
|
4649 |
if (@global(FileOpenTrace) == true) { |
|
4650 |
fprintf(stderr, "fdopen [ExternalStream] %"_ld_" (%"_lx_") -> %"_lx_"\n", (INT)fd, (INT)fd, (INT)f); |
|
4651 |
} |
|
4652 |
||
4653 |
fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp); |
|
4654 |
retVal = self; |
|
4655 |
} |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4656 |
} |
12688 | 4657 |
out:; |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4658 |
%}. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4659 |
retVal notNil ifTrue:[ |
18779 | 4660 |
position := 0. |
4661 |
buffered isNil ifTrue:[ |
|
4662 |
buffered := true. "default is buffered" |
|
4663 |
]. |
|
4664 |
handleType := handleTypeSymbol. |
|
4665 |
self registerForFinalization. |
|
4666 |
^ retVal |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4667 |
]. |
12688 | 4668 |
error notNil ifTrue:[ |
18779 | 4669 |
OperatingSystem closeFd:aFileDescriptor. |
4670 |
lastErrorNumber := error. |
|
4671 |
position := nil. |
|
4672 |
" |
|
4673 |
the open failed for some reason ... |
|
4674 |
" |
|
4675 |
^ self openError:error |
|
8966
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 |
! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4678 |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4679 |
dupFd |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4680 |
"duplicate my file descriptor" |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4681 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4682 |
|fd dupFd| |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4683 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4684 |
fd := self fileHandle. |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4685 |
dupFd := OperatingSystem dup:fd. |
10989 | 4686 |
self setFileHandle:dupFd mode:self fopenMode. |
10995 | 4687 |
! |
10989 | 4688 |
|
4689 |
fopenMode |
|
4690 |
"answer the mode for fopen. |
|
4691 |
Only used internally" |
|
4692 |
||
4693 |
mode == #readonly ifTrue:[ |
|
18797 | 4694 |
^ ReadMode |
10989 | 4695 |
]. |
4696 |
mode == #writeonly ifTrue:[ |
|
18797 | 4697 |
^ WriteMode |
10989 | 4698 |
]. |
4699 |
mode == #readWrite ifTrue:[ |
|
18797 | 4700 |
^ ReadWriteMode |
10989 | 4701 |
]. |
4702 |
mode == #append ifTrue:[ |
|
18797 | 4703 |
^ AppendMode |
10989 | 4704 |
]. |
4705 |
^ ReadWriteMode |
|
10995 | 4706 |
! |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4707 |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4708 |
open:aPath withMode:openModeString |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4709 |
"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
|
4710 |
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
|
4711 |
|
16296 | 4712 |
|ok error| |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4713 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4714 |
ok := false. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4715 |
%{ |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4716 |
FILE *f; |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4717 |
OBJ fp; |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4718 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4719 |
if (__isStringLike(aPath) && __isStringLike(openModeString)) { |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4720 |
#ifdef WIN32 |
10339 | 4721 |
# if 1 |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4722 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString)); |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4723 |
# else |
18779 | 4724 |
__BEGIN_INTERRUPTABLE__ |
4725 |
do { |
|
4726 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString)); |
|
4727 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
|
4728 |
__END_INTERRUPTABLE__ |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4729 |
# endif |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4730 |
#else /* UNIX */ |
18779 | 4731 |
__BEGIN_INTERRUPTABLE__ |
4732 |
do { |
|
4733 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString)); |
|
4734 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
|
4735 |
__END_INTERRUPTABLE__ |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4736 |
#endif /* UNIX */ |
18779 | 4737 |
if (f == NULL) { |
4738 |
error = __mkSmallInteger(__threadErrno); |
|
4739 |
} else { |
|
4740 |
if (@global(FileOpenTrace) == true) { |
|
4741 |
fprintf(stderr, "fopen %s [ExternalStream] -> %"_lx_"\n", __stringVal(aPath), (INT)f); |
|
4742 |
} |
|
4743 |
fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp); |
|
4744 |
__INST(handleType) = @symbol(filePointer); |
|
4745 |
ok = true; |
|
4746 |
} |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4747 |
} |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4748 |
%}. |
16296 | 4749 |
ok ifTrue:[ |
18779 | 4750 |
position := 0. |
4751 |
self registerForFinalization. |
|
4752 |
^ self. |
|
16296 | 4753 |
]. |
4754 |
error notNil ifTrue:[ |
|
18779 | 4755 |
" |
4756 |
the open failed for some reason ... |
|
4757 |
" |
|
4758 |
lastErrorNumber := error. |
|
4759 |
self openError:error. |
|
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4760 |
]. |
16296 | 4761 |
self primitiveFailed. |
8966
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 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4764 |
reOpen |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4765 |
"USERS WILL NEVER INVOKE THIS METHOD |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4766 |
sent after snapin to reopen streams. |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4767 |
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
|
4768 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4769 |
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
|
4770 |
handle := nil. |
18779 | 4771 |
self unregisterForFinalization. |
8966
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4772 |
|
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4773 |
"Modified: 10.1.1997 / 17:50:44 / cg" |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4774 |
! |
0b299dd8759c
Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
4775 |
|
12398 | 4776 |
setAccessor:what to:something |
4777 |
"set the filePointer/fileHandle to the given one; |
|
4778 |
low level private & friend interface; may also be used to connect to some |
|
4779 |
externally provided handle." |
|
4780 |
||
4781 |
handle := something. |
|
4782 |
handleType := what. |
|
4783 |
! |
|
4784 |
||
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4785 |
setFileDescriptor:anInteger mode:openMode |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4786 |
"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
|
4787 |
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
|
4788 |
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
|
4789 |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4790 |
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
|
4791 |
! |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4792 |
|
12398 | 4793 |
setFileHandle:something |
4794 |
"set the fileHandle to the given one; |
|
4795 |
low level private & friend interface; may also be used to connect to some |
|
4796 |
externally provided file handle." |
|
4797 |
||
4798 |
self setAccessor:#fileHandle to:something |
|
4799 |
! |
|
4800 |
||
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4801 |
setFileHandle:anIntegerOrExternalAddress mode:openMode |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4802 |
"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
|
4803 |
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
|
4804 |
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
|
4805 |
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
|
4806 |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4807 |
%{ |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4808 |
FILEPOINTER f; |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4809 |
OBJ fp; |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4810 |
FILE *fdopen(); |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4811 |
int fd; |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4812 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4813 |
if (!__isStringLike(openMode)) |
18797 | 4814 |
goto err; |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4815 |
|
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4816 |
#ifdef WIN32 |
12688 | 4817 |
__stxWrapApiEnterCritical(); |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4818 |
if (__isExternalAddressLike(anIntegerOrExternalAddress) ) { |
18797 | 4819 |
HANDLE __fileHandle = (HANDLE)__externalAddressVal(anIntegerOrExternalAddress); |
4820 |
fd = _open_osfhandle((long)__fileHandle, O_BINARY); /* should we handle readonly, append or text mode? */ |
|
4821 |
if (fd < 0) { |
|
4822 |
__stxWrapApiLeaveCritical(); |
|
4823 |
CloseHandle(__fileHandle); |
|
4824 |
goto err; |
|
4825 |
} |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4826 |
} else |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4827 |
#endif |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4828 |
if (__isSmallInteger(anIntegerOrExternalAddress)) { |
18797 | 4829 |
fd = __smallIntegerVal(anIntegerOrExternalAddress); |
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4830 |
} else { |
12688 | 4831 |
#ifdef WIN32 |
18797 | 4832 |
__stxWrapApiLeaveCritical(); |
12688 | 4833 |
#endif |
18797 | 4834 |
goto err; |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4835 |
} |
10997 | 4836 |
f = fdopen(fd, __stringVal(openMode)); |
12688 | 4837 |
#ifdef WIN32 |
4838 |
__stxWrapApiLeaveCritical(); |
|
4839 |
#endif |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4840 |
if (f != NULL) { |
18797 | 4841 |
if (@global(FileOpenTrace) == true) { |
4842 |
fprintf(stderr, "fdopen [ExternalStream] %d -> %"_lx_"\n", fd, (INT)f); |
|
4843 |
} |
|
4844 |
fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp); |
|
4845 |
__INST(handleType) = @symbol(filePointer); |
|
4846 |
RETURN (self); |
|
10982
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4847 |
} |
11fd48c1276d
New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents:
10932
diff
changeset
|
4848 |
err:; |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4849 |
%}. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4850 |
^ self primitiveFailed |
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 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4853 |
setFilePointer:something |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4854 |
"set the filePointer to the given one; |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4855 |
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
|
4856 |
externally provided file." |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4857 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4858 |
self setAccessor:#filePointer to:something |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4859 |
! |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4860 |
|
12398 | 4861 |
setLastError:aNumber |
4862 |
lastErrorNumber := aNumber |
|
12395
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 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4865 |
setSocketHandle:something |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4866 |
"set the socketHandle to the given one; |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4867 |
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
|
4868 |
externally provided socket handle." |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4869 |
|
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4870 |
self setAccessor:#socketHandle to:something |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4871 |
! ! |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
4872 |
|
612 | 4873 |
!ExternalStream methodsFor:'queries'! |
4874 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4875 |
nextError |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4876 |
"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
|
4877 |
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
|
4878 |
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
|
4879 |
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
|
4880 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4881 |
%{ /*NOCONTEXT*/ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4882 |
FILEPOINTER f; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4883 |
int ret, _buffered; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4884 |
OBJ fp; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4885 |
unsigned char ch; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4886 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4887 |
if ((__INST(handleType) == nil) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4888 |
|| (__INST(handleType) == @symbol(filePointer)) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4889 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4890 |
|| (__INST(handleType) == @symbol(socketHandle)) |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4891 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4892 |
if (((fp = __INST(handle)) != nil) |
4893 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4894 |
) { |
|
4895 |
f = __FILEVal(fp); |
|
4896 |
||
4897 |
_buffered = (__INST(buffered) == true); |
|
4898 |
if (_buffered) { |
|
4899 |
__READING__(f) |
|
4900 |
} |
|
4901 |
__READBYTE__(ret, f, &ch, _buffered, __INST(handleType)); |
|
4902 |
||
4903 |
if (ret > 0) { |
|
4904 |
RETURN(nil) |
|
4905 |
} |
|
4906 |
if (ret < 0) { |
|
4907 |
RETURN(__mkSmallInteger(__threadErrno)); |
|
4908 |
} else /* ret == 0 */ { |
|
4909 |
RETURN(__mkSmallInteger(0)); /* EOF */ |
|
4910 |
} |
|
4911 |
} |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4912 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4913 |
%}. |
612 | 4914 |
! |
4915 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4916 |
numAvailable |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4917 |
"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
|
4918 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4919 |
|fd| |
7804 | 4920 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
4921 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4922 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4923 |
fd := self fileDescriptor. |
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 |
%{ |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4926 |
#ifdef WIN32 |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4927 |
int res = 1; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4928 |
int success = 0; |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4929 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4930 |
if (__INST(handleType) == @symbol(socketHandle)) { |
18797 | 4931 |
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
|
4932 |
} else if (__INST(handleType) == @symbol(socketFilePointer)) { |
18797 | 4933 |
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
|
4934 |
} else if (__INST(handleType) == @symbol(pipeFilePointer)) { |
18797 | 4935 |
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
|
4936 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4937 |
if (success) { |
18797 | 4938 |
if (__INST(readAhead) != nil) res++; |
4939 |
RETURN(__mkSmallInteger(res)); |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4940 |
} |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4941 |
#endif |
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 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4944 |
^ (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
|
4945 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4946 |
"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
|
4947 |
"Modified: / 4.2.1998 / 17:31:11 / cg" |
612 | 4948 |
! ! |
4949 |
||
4950 |
!ExternalStream methodsFor:'reading'! |
|
4951 |
||
4952 |
next |
|
4953 |
"return the next element; advance read position. |
|
4954 |
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
|
4955 |
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
|
4956 |
- see #pastEndRead." |
1 | 4957 |
|
16296 | 4958 |
|c error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
4959 |
%{ |
18364 | 4960 |
#ifdef __SCHTEAM__ |
4961 |
STObject handle = self.instVarAt(I_handle); |
|
4962 |
||
4963 |
if (handle != STObject.Nil) { |
|
18797 | 4964 |
STObject next; |
4965 |
||
4966 |
if (self.instVarAt(I_binary) == STObject.True) { |
|
4967 |
next = handle.nextByte(); |
|
4968 |
} else { |
|
4969 |
next = handle.nextChar(); |
|
4970 |
} |
|
4971 |
if (next != STObject.EOF) { |
|
4972 |
self.instVarAt_put(I_position, STObject.Nil); |
|
4973 |
return __c__._RETURN( next ); |
|
4974 |
} |
|
4975 |
self.instVarAt_put(I_hitEOF, STObject.True); |
|
18364 | 4976 |
} |
4977 |
#else |
|
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
4978 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
4979 |
int ret, _buffered; |
612 | 4980 |
OBJ pos, fp; |
4981 |
unsigned char ch; |
|
4982 |
||
1133 | 4983 |
__INST(lastErrorNumber) = nil; |
12548 | 4984 |
if ((__INST(handleType) == nil) |
4985 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
4986 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
4987 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 4988 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 4989 |
if (((fp = __INST(handle)) != nil) |
4990 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4991 |
) { |
|
4992 |
f = __FILEVal(fp); |
|
4993 |
||
4994 |
_buffered = (__INST(buffered) == true); |
|
4995 |
if (_buffered) { |
|
4996 |
__READING__(f) |
|
4997 |
} |
|
4998 |
__READBYTE__(ret, f, &ch, _buffered, __INST(handleType)); |
|
4999 |
||
5000 |
if (ret > 0) { |
|
5001 |
pos = __INST(position); |
|
5002 |
if (__isSmallInteger(pos)) { |
|
5003 |
OBJ t; |
|
5004 |
||
5005 |
t = __MKINT(__intVal(pos) + 1); __INST(position) = t; __STORE(self, t); |
|
5006 |
} else { |
|
5007 |
__INST(position) = nil; /* i.e. do not know */ |
|
5008 |
} |
|
5009 |
if (__INST(binary) == true) { |
|
5010 |
RETURN ( __mkSmallInteger(ch) ); |
|
5011 |
} |
|
5012 |
RETURN ( __MKCHARACTER(ch) ); |
|
5013 |
} |
|
5014 |
||
5015 |
__INST(position) = nil; |
|
5016 |
if ((ret < 0) |
|
18364 | 5017 |
# ifdef ECONNRESET |
18797 | 5018 |
&& (__threadErrno != ECONNRESET) |
18364 | 5019 |
# endif |
18797 | 5020 |
){ |
5021 |
error = __mkSmallInteger(__threadErrno); |
|
5022 |
} else /* ret == 0 */ { |
|
5023 |
__INST(hitEOF) = true; |
|
5024 |
} |
|
5025 |
} |
|
612 | 5026 |
} |
18364 | 5027 |
#endif /* not SCHTEAM */ |
612 | 5028 |
%}. |
8614 | 5029 |
hitEOF == true ifTrue:[^ self pastEndRead]. |
16296 | 5030 |
error notNil ifTrue:[ |
18797 | 5031 |
lastErrorNumber := error. |
5032 |
^ self readError:error |
|
16296 | 5033 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5034 |
handle isNil ifTrue:[^ self errorNotOpen]. |
5420 | 5035 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
5036 |
||
5037 |
readAhead notNil ifTrue:[ |
|
18797 | 5038 |
c := readAhead. |
5039 |
readAhead := nil. |
|
5040 |
^ c. |
|
5420 | 5041 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5042 |
c := self nextByteFromFile:handle. |
5430 | 5043 |
c isNil ifTrue:[ |
18797 | 5044 |
^ self pastEndRead. |
5430 | 5045 |
]. |
5420 | 5046 |
binary == true ifTrue:[ |
18797 | 5047 |
^ c |
5420 | 5048 |
]. |
5049 |
^ Character value:c |
|
612 | 5050 |
! |
5051 |
||
5052 |
next:count |
|
5053 |
"return the next count elements of the stream as a collection. |
|
5054 |
Redefined to return a String or ByteArray instead of the default: Array." |
|
5055 |
||
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5056 |
|coll nRead| |
612 | 5057 |
|
5058 |
binary ifTrue:[ |
|
18797 | 5059 |
coll := ByteArray uninitializedNew:count |
612 | 5060 |
] ifFalse:[ |
18797 | 5061 |
coll := String new:count |
612 | 5062 |
]. |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5063 |
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
|
5064 |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5065 |
"/ for readStream protocol compatibility, |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5066 |
"/ we must raise an exception here. |
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5067 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5068 |
nRead < count ifTrue:[ |
18797 | 5069 |
^ self pastEndRead |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5070 |
]. |
612 | 5071 |
^ coll |
2151
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5072 |
|
5f1fd65f8c3b
next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents:
2134
diff
changeset
|
5073 |
"Modified: 11.1.1997 / 17:44:17 / cg" |
612 | 5074 |
! |
5075 |
||
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5076 |
nextOrNil |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5077 |
"return the next element; advance read position. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5078 |
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
|
5079 |
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
|
5080 |
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
|
5081 |
|
16296 | 5082 |
|c error| |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5083 |
%{ |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5084 |
FILEPOINTER f; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5085 |
int ret, _buffered; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5086 |
OBJ pos, fp; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5087 |
unsigned char ch; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5088 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5089 |
__INST(lastErrorNumber) = nil; |
12548 | 5090 |
if ((__INST(handleType) == nil) |
5091 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5092 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5093 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5094 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5095 |
if (((fp = __INST(handle)) != nil) |
5096 |
&& (__INST(mode) != @symbol(writeonly)) |
|
5097 |
) { |
|
5098 |
f = __FILEVal(fp); |
|
5099 |
||
5100 |
_buffered = (__INST(buffered) == true); |
|
5101 |
if (_buffered) { |
|
5102 |
__READING__(f) |
|
5103 |
} |
|
5104 |
__READBYTE__(ret, f, &ch, _buffered, __INST(handleType)); |
|
5105 |
||
5106 |
if (ret > 0) { |
|
5107 |
pos = __INST(position); |
|
5108 |
if (__isSmallInteger(pos)) { |
|
5109 |
OBJ t; |
|
5110 |
||
5111 |
t = __MKINT(__intVal(pos) + 1); __INST(position) = t; __STORE(self, t); |
|
5112 |
} else { |
|
5113 |
__INST(position) = nil; /* i.e. do not know */ |
|
5114 |
} |
|
5115 |
if (__INST(binary) == true) { |
|
5116 |
RETURN ( __mkSmallInteger(ch) ); |
|
5117 |
} |
|
5118 |
RETURN ( __MKCHARACTER(ch) ); |
|
5119 |
} |
|
5120 |
||
5121 |
__INST(position) = nil; |
|
5122 |
if ((ret < 0) |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5123 |
#ifdef ECONNRESET |
18797 | 5124 |
&& (__threadErrno != ECONNRESET) |
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5125 |
#endif |
18797 | 5126 |
){ |
5127 |
error = __mkSmallInteger(__threadErrno); |
|
5128 |
} else /* ret == 0 */ { |
|
5129 |
__INST(hitEOF) = true; |
|
5130 |
} |
|
5131 |
} |
|
10348
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 |
%}. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5134 |
hitEOF == true ifTrue:[^ nil]. |
16296 | 5135 |
error notNil ifTrue:[ |
18797 | 5136 |
lastErrorNumber := error. |
5137 |
^ self readError:error. |
|
16296 | 5138 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5139 |
handle isNil ifTrue:[^ self errorNotOpen]. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5140 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5141 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5142 |
readAhead notNil ifTrue:[ |
18797 | 5143 |
c := readAhead. |
5144 |
readAhead := nil. |
|
5145 |
^ c. |
|
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5146 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5147 |
c := self nextByteFromFile:handle. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5148 |
c isNil ifTrue:[ |
18797 | 5149 |
^ nil. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5150 |
]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5151 |
binary == true ifTrue:[ |
18797 | 5152 |
^ c |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5153 |
]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5154 |
^ Character value:c |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5155 |
! |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5156 |
|
612 | 5157 |
peek |
5158 |
"return the element to be read next without advancing read position. |
|
5159 |
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
|
5160 |
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
|
5161 |
- see #pastEndRead." |
612 | 5162 |
|
16296 | 5163 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5164 |
%{ |
2593
d678527ef34e
changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents:
2591
diff
changeset
|
5165 |
FILEPOINTER f; |
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5166 |
unsigned char c; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5167 |
int ret, _buffered; |
216 | 5168 |
OBJ fp; |
3672
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
5169 |
OBJ ra; |
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
5170 |
|
49dff0652a2c
fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents:
3569
diff
changeset
|
5171 |
if ((ra = __INST(readAhead)) != nil) { |
18797 | 5172 |
if (__INST(binary) == true) { |
5173 |
RETURN ( ra ); |
|
5174 |
} |
|
5175 |
c = __intVal(ra); |
|
5176 |
RETURN ( __MKCHARACTER(c) ); |
|
3029
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
5177 |
} |
aa14768e0e6a
larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents:
3016
diff
changeset
|
5178 |
|
1133 | 5179 |
__INST(lastErrorNumber) = nil; |
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5180 |
|
12548 | 5181 |
if ((__INST(handleType) == nil) |
5182 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5183 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5184 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5185 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5186 |
if (((fp = __INST(handle)) != nil) |
5187 |
&& (__INST(mode) != @symbol(writeonly)) |
|
5188 |
) { |
|
5189 |
f = __FILEVal(fp); |
|
5190 |
_buffered = (__INST(buffered) == true); |
|
5191 |
if (_buffered) { |
|
5192 |
__READING__(f) |
|
5193 |
} |
|
5194 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5195 |
||
5196 |
if (ret > 0) { |
|
5197 |
__UNGETC__(c, f, _buffered); |
|
5198 |
||
5199 |
if (__INST(binary) == true) { |
|
5200 |
RETURN ( __mkSmallInteger(c) ); |
|
5201 |
} |
|
5202 |
RETURN ( __MKCHARACTER(c) ); |
|
5203 |
} |
|
5204 |
if ((ret < 0) |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5205 |
#ifdef ECONNRESET |
18797 | 5206 |
&& (__threadErrno != ECONNRESET) |
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5207 |
#endif |
18797 | 5208 |
){ |
5209 |
error = __mkSmallInteger(__threadErrno); |
|
5210 |
} else /* ret == 0 */ { |
|
5211 |
__INST(hitEOF) = true; |
|
5212 |
} |
|
5213 |
} |
|
1 | 5214 |
} |
159 | 5215 |
%}. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5216 |
hitEOF == true ifTrue:[^ self pastEndRead]. |
16296 | 5217 |
error notNil ifTrue:[ |
18797 | 5218 |
lastErrorNumber := error. |
5219 |
^ self readError:error. |
|
16296 | 5220 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5221 |
handle isNil ifTrue:[^ self errorNotOpen]. |
5420 | 5222 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
5223 |
||
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5224 |
readAhead isNil ifTrue:[ |
18797 | 5225 |
readAhead := self nextOrNil. |
5226 |
readAhead isNil ifTrue:[ |
|
5227 |
^ self pastEndRead. |
|
5228 |
]. |
|
5420 | 5229 |
]. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5230 |
^ readAhead |
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 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5233 |
peekOrNil |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5234 |
"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
|
5235 |
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
|
5236 |
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
|
5237 |
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
|
5238 |
|
16296 | 5239 |
|error| |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5240 |
%{ |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5241 |
FILEPOINTER f; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5242 |
unsigned char c; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5243 |
int ret, _buffered; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5244 |
OBJ fp; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5245 |
OBJ ra; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5246 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5247 |
if ((ra = __INST(readAhead)) != nil) { |
18797 | 5248 |
if (__INST(binary) == true) { |
5249 |
RETURN ( ra ); |
|
5250 |
} |
|
5251 |
c = __intVal(ra); |
|
5252 |
RETURN ( __MKCHARACTER(c) ); |
|
10348
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 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5255 |
__INST(lastErrorNumber) = nil; |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5256 |
|
12548 | 5257 |
if ((__INST(handleType) == nil) |
5258 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5259 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5260 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5261 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5262 |
if (((fp = __INST(handle)) != nil) |
5263 |
&& (__INST(mode) != @symbol(writeonly)) |
|
5264 |
) { |
|
5265 |
f = __FILEVal(fp); |
|
5266 |
_buffered = (__INST(buffered) == true); |
|
5267 |
if (_buffered) { |
|
5268 |
__READING__(f) |
|
5269 |
} |
|
5270 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5271 |
||
5272 |
if (ret > 0) { |
|
5273 |
__UNGETC__(c, f, _buffered); |
|
5274 |
||
5275 |
if (__INST(binary) == true) { |
|
5276 |
RETURN ( __mkSmallInteger(c) ); |
|
5277 |
} |
|
5278 |
RETURN ( __MKCHARACTER(c) ); |
|
5279 |
} |
|
5280 |
if ((ret < 0) |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5281 |
#ifdef ECONNRESET |
18797 | 5282 |
&& (__threadErrno != ECONNRESET) |
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5283 |
#endif |
18797 | 5284 |
){ |
5285 |
error = __mkSmallInteger(__threadErrno); |
|
5286 |
} else /* ret == 0 */ { |
|
5287 |
__INST(hitEOF) = true; |
|
5288 |
} |
|
5289 |
} |
|
10348
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 |
%}. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5292 |
hitEOF == true ifTrue:[^ nil]. |
16296 | 5293 |
error notNil ifTrue:[ |
18797 | 5294 |
lastErrorNumber := error. |
5295 |
^ self readError:error. |
|
16296 | 5296 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5297 |
handle isNil ifTrue:[^ self errorNotOpen]. |
10348
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5298 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5299 |
|
7ed317f7f2be
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10339
diff
changeset
|
5300 |
readAhead isNil ifTrue:[ |
18797 | 5301 |
readAhead := self nextOrNil. |
5420 | 5302 |
]. |
5303 |
^ readAhead |
|
1295 | 5304 |
! |
5305 |
||
5306 |
upToEnd |
|
5307 |
"return a collection of the elements up-to the end. |
|
5308 |
Return nil if the stream-end is reached before." |
|
5309 |
||
6569 | 5310 |
|answerStream buffer count| |
5311 |
||
5312 |
buffer := self contentsSpecies new:4096. |
|
1295 | 5313 |
[self atEnd] whileFalse:[ |
18797 | 5314 |
count := self nextAvailableBytes:4096 into:buffer startingAt:1. |
5315 |
count ~~ 0 ifTrue:[ |
|
5316 |
answerStream isNil ifTrue:[ |
|
5317 |
answerStream := WriteStream with:buffer. |
|
5318 |
answerStream position:count. |
|
5319 |
buffer := self contentsSpecies new:4096. |
|
5320 |
] ifFalse:[ |
|
5321 |
answerStream nextPutAll:buffer startingAt:1 to:count. |
|
5322 |
]. |
|
5323 |
]. |
|
1295 | 5324 |
]. |
6758
bc933037a1dc
oops - upToEnd failed if already at the end
Claus Gittinger <cg@exept.de>
parents:
6661
diff
changeset
|
5325 |
answerStream isNil ifTrue:[^ self contentsSpecies new]. |
1295 | 5326 |
^ answerStream contents |
5327 |
||
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5328 |
" |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5329 |
('smalltalk.rc' asFilename readStream upToEnd) |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5330 |
= |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5331 |
('smalltalk.rc' asFilename readStream contentsOfEntireFile) |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
5332 |
" |
612 | 5333 |
! ! |
5334 |
||
5420 | 5335 |
!ExternalStream methodsFor:'rel5 protocol'! |
5336 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5337 |
atEndFile:handle |
5420 | 5338 |
"for migration to rel5 only" |
5339 |
||
5340 |
self primitiveFailed |
|
5341 |
! |
|
5342 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5343 |
nextByteFromFile:handle |
5420 | 5344 |
"for migration to rel5 only" |
5345 |
||
5346 |
self primitiveFailed |
|
5347 |
! |
|
5348 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5349 |
nextPutByte:aByte toFile:handle |
5420 | 5350 |
"for migration to rel5 only" |
5351 |
||
5352 |
self primitiveFailed |
|
5353 |
! ! |
|
5354 |
||
612 | 5355 |
!ExternalStream methodsFor:'testing'! |
5356 |
||
5357 |
atEnd |
|
5358 |
"return true, if position is at end" |
|
5359 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5360 |
%{ |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
5361 |
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
|
5362 |
char c; |
612 | 5363 |
|
1133 | 5364 |
if (__INST(hitEOF) == true) { |
18797 | 5365 |
RETURN (true); |
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
5366 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
7017
diff
changeset
|
5367 |
pos = __INST(position); |
7875
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
5368 |
lim = __INST(readLimit); |
eb7d48b0425d
made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents:
7832
diff
changeset
|
5369 |
if (lim != nil) { |
18797 | 5370 |
off_t _pos, _readLimit; |
5371 |
||
5372 |
_pos = __signedLongIntVal(pos); |
|
5373 |
_pos = _pos - __intVal( @global(PositionableStream:ZeroPosition)) + 1; |
|
5374 |
_readLimit = __signedLongIntVal(lim); |
|
5375 |
if (_pos > _readLimit) { |
|
5376 |
RETURN (true); |
|
5377 |
} |
|
612 | 5378 |
} |
5379 |
||
1133 | 5380 |
__INST(lastErrorNumber) = nil; |
5381 |
||
12548 | 5382 |
if ((__INST(handleType) == nil) |
5383 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5384 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5385 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5386 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 5387 |
if ((fp = __INST(handle)) != nil) { |
5388 |
int _buffered = (__INST(buffered) == true); |
|
5389 |
int ret; |
|
5390 |
FILEPOINTER f = __FILEVal(fp); |
|
5391 |
||
5392 |
if (_buffered) { |
|
5393 |
__READING__(f); |
|
5394 |
} else { |
|
5395 |
if (__INST(readAhead) != nil) { |
|
5396 |
RETURN (false); |
|
5397 |
} |
|
5398 |
} |
|
5399 |
||
5400 |
/* |
|
5401 |
* read ahead ... |
|
5402 |
*/ |
|
5403 |
do { |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5404 |
#ifdef WIN32 |
10339 | 5405 |
# if 1 |
18797 | 5406 |
__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
|
5407 |
|
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5408 |
# else |
18797 | 5409 |
__BEGIN_INTERRUPTABLE__ |
5410 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5411 |
__END_INTERRUPTABLE__ |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5412 |
# endif |
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5413 |
#else /* not WIN32 */ |
18797 | 5414 |
__BEGIN_INTERRUPTABLE__ |
5415 |
__READBYTE__(ret, f, &c, _buffered, __INST(handleType)); |
|
5416 |
__END_INTERRUPTABLE__ |
|
4914 | 5417 |
#endif |
18797 | 5418 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
5419 |
||
5420 |
if (ret > 0) { |
|
5421 |
__UNGETC__(c&0xff, f, _buffered); |
|
5422 |
RETURN (false); |
|
5423 |
} |
|
5424 |
if (ret == 0) { |
|
5425 |
__INST(hitEOF) = true; |
|
5426 |
RETURN (true); |
|
5427 |
} |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5428 |
#ifdef ECONNRESET |
18797 | 5429 |
// connection reset by peer is also an EOF |
5430 |
if (__threadErrno == ECONNRESET) { |
|
5431 |
__INST(hitEOF) = true; |
|
5432 |
RETURN (true); |
|
5433 |
} |
|
17360
24b114ab935a
ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents:
17332
diff
changeset
|
5434 |
#endif |
18797 | 5435 |
/* ret < 0 */ |
5436 |
__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno); |
|
5437 |
} |
|
612 | 5438 |
} |
5439 |
%}. |
|
5440 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5441 |
handle isNil ifTrue:[^ self errorNotOpen]. |
5420 | 5442 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
5443 |
readAhead notNil ifTrue:[^ false]. |
|
5444 |
||
5445 |
"/ migration support |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5446 |
^ self |
18797 | 5447 |
atEndFile:handle |
3906
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
5448 |
|
7f2d4a3b63e2
oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents:
3878
diff
changeset
|
5449 |
"Modified: / 30.10.1998 / 20:16:06 / cg" |
612 | 5450 |
! |
5451 |
||
5452 |
canReadWithoutBlocking |
|
5453 |
"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
|
5454 |
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
|
5455 |
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
|
5456 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5457 |
^ readAhead notNil |
18797 | 5458 |
or:[handle isNil] |
5459 |
or:[mode == #writeonly |
|
5460 |
or:[OperatingSystem readCheck:self fileDescriptor]] |
|
612 | 5461 |
|
5462 |
" |
|
5463 |
|pipe| |
|
5464 |
||
5465 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
5466 |
pipe canReadWithoutBlocking ifTrue:[ |
|
18797 | 5467 |
Transcript showCR:'data available' |
612 | 5468 |
] ifFalse:[ |
18797 | 5469 |
Transcript showCR:'no data available' |
612 | 5470 |
]. |
5471 |
pipe close |
|
5472 |
" |
|
2986
c6957cf6128f
Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents:
2963
diff
changeset
|
5473 |
|
c6957cf6128f
Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents:
2963
diff
changeset
|
5474 |
"Modified: 25.9.1997 / 13:08:45 / stefan" |
612 | 5475 |
! |
5476 |
||
5477 |
canWriteWithoutBlocking |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5478 |
"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
|
5479 |
(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
|
5480 |
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
|
5481 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5482 |
^ handle isNil |
18797 | 5483 |
or:[mode == #readonly |
5484 |
or:[OperatingSystem writeCheck:self fileDescriptor]] |
|
3274
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
5485 |
! |
92a24829f14a
added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents:
3255
diff
changeset
|
5486 |
|
10995 | 5487 |
gotErrorOrEOF |
5488 |
"answerv true, if amn error or eof has been occured on the stream" |
|
5489 |
||
5490 |
^ hitEOF == true or:[lastErrorNumber notNil] |
|
5491 |
! |
|
5492 |
||
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5493 |
isBinary |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5494 |
"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
|
5495 |
The default when created is false." |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5496 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5497 |
^ binary |
6269
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
5498 |
! |
3a888523e3a6
#errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents:
6255
diff
changeset
|
5499 |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5500 |
isBlocking |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5501 |
"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
|
5502 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5503 |
handle isNil ifTrue:[^ self errorNotOpen]. |
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5504 |
^ OperatingSystem isBlockingOn:self fileDescriptor |
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 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5507 |
isExternalStream |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5508 |
"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
|
5509 |
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
|
5510 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5511 |
^ true |
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 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5514 |
isOpen |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5515 |
"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
|
5516 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5517 |
^ handle notNil |
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 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5520 |
isReadable |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5521 |
"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
|
5522 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5523 |
^ (mode ~~ #writeonly) |
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 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5526 |
isWritable |
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5527 |
"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
|
5528 |
|
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5529 |
^ (mode ~~ #readonly) |
612 | 5530 |
! ! |
5531 |
||
5532 |
!ExternalStream methodsFor:'waiting for I/O'! |
|
5533 |
||
5101 | 5534 |
canBeSelected |
5535 |
"return true, if this stream can be selected upon" |
|
5536 |
||
5537 |
^ OperatingSystem supportsSelect |
|
5538 |
! |
|
5539 |
||
18327 | 5540 |
readWaitWithTimeoutMs:timeoutOrNil |
612 | 5541 |
"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
|
5542 |
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
|
5543 |
If data is already available, return immediate. |
612 | 5544 |
Return true if a timeout occured (i.e. false, if data is available). |
5545 |
The other threads are not affected by the wait." |
|
5546 |
||
16480 | 5547 |
|fd inputSema hasTimedout wasBlocked| |
612 | 5548 |
|
2120
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5549 |
readAhead notNil ifTrue:[^ false]. |
71c1870df7ba
implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents:
2045
diff
changeset
|
5550 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5551 |
handle isNil ifTrue:[^ self errorNotOpen]. |
612 | 5552 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
5553 |
||
5554 |
fd := self fileDescriptor. |
|
5555 |
(OperatingSystem readCheck:fd) ifTrue:[^ false]. |
|
5556 |
||
5557 |
wasBlocked := OperatingSystem blockInterrupts. |
|
16463 | 5558 |
inputSema := Semaphore new name:'readWait'. |
5559 |
[ |
|
18797 | 5560 |
timeoutOrNil notNil ifTrue:[ |
5561 |
Processor signal:inputSema afterMilliseconds:timeoutOrNil. |
|
5562 |
]. |
|
5563 |
Processor signal:inputSema onInput:fd. |
|
5564 |
Processor activeProcess state:#ioWait. |
|
5565 |
inputSema wait. |
|
5566 |
hasTimedout := timeoutOrNil notNil and:[(OperatingSystem readCheck:fd) not]. |
|
16463 | 5567 |
] ifCurtailed:[ |
18797 | 5568 |
Processor disableSemaphore:inputSema. |
5569 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
612 | 5570 |
]. |
18327 | 5571 |
timeoutOrNil notNil ifTrue:[ |
18797 | 5572 |
Processor disableSemaphore:inputSema. |
16480 | 5573 |
]. |
612 | 5574 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
16480 | 5575 |
^ hasTimedout |
612 | 5576 |
! |
5577 |
||
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5578 |
readWriteWaitWithTimeoutMs:timeout |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5579 |
"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
|
5580 |
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
|
5581 |
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
|
5582 |
Return immediate if the receiver is already ready. |
5506 | 5583 |
The other threads are not affected by the wait." |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5584 |
|
16480 | 5585 |
|fd sema hasTimedout wasBlocked| |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5586 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5587 |
handle isNil ifTrue:[ |
18797 | 5588 |
^ self errorNotOpen |
3490
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 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5591 |
fd := self fileDescriptor. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5592 |
(OperatingSystem readWriteCheck:fd) ifTrue:[^ false]. |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5593 |
|
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5594 |
wasBlocked := OperatingSystem blockInterrupts. |
16463 | 5595 |
sema := Semaphore new name:'readWriteWait'. |
5596 |
[ |
|
18797 | 5597 |
timeout notNil ifTrue:[ |
5598 |
Processor signal:sema afterMilliseconds:timeout |
|
5599 |
]. |
|
5600 |
Processor signal:sema onOutput:fd. |
|
5601 |
Processor signal:sema onInput:fd. |
|
5602 |
Processor activeProcess state:#ioWait. |
|
5603 |
sema wait. |
|
5604 |
hasTimedout := timeout notNil and:[(OperatingSystem readWriteCheck:fd) not]. |
|
16463 | 5605 |
] ifCurtailed:[ |
18797 | 5606 |
Processor disableSemaphore:sema. |
5607 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5608 |
]. |
16480 | 5609 |
timeout notNil ifTrue:[ |
18797 | 5610 |
Processor disableSemaphore:sema. |
16480 | 5611 |
]. |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5612 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
16480 | 5613 |
^ hasTimedout |
3490
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5614 |
! |
6a87c2b37fd4
added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents:
3474
diff
changeset
|
5615 |
|
18956 | 5616 |
writeExceptionWaitWithTimeoutMs:timeout |
5617 |
"suspend the current process, until the receiver |
|
5618 |
becomes ready for reading or writing or a timeout (in milliseconds) expired. |
|
5619 |
Return true if a timeout occured (i.e. false, if data is available). |
|
5620 |
Return immediate if the receiver is already ready. |
|
5621 |
The other threads are not affected by the wait." |
|
5622 |
||
5623 |
|fd sema hasTimedout wasBlocked| |
|
5624 |
||
5625 |
handle isNil ifTrue:[ |
|
5626 |
^ self errorNotOpen |
|
5627 |
]. |
|
5628 |
||
5629 |
fd := self fileDescriptor. |
|
5630 |
(OperatingSystem writeExceptionCheck:fd) ifTrue:[^ false]. |
|
5631 |
||
5632 |
wasBlocked := OperatingSystem blockInterrupts. |
|
5633 |
sema := Semaphore new name:'writeExceptionWait'. |
|
5634 |
[ |
|
5635 |
timeout notNil ifTrue:[ |
|
5636 |
Processor signal:sema afterMilliseconds:timeout |
|
5637 |
]. |
|
5638 |
Processor signal:sema onOutput:fd. |
|
5639 |
Processor signal:sema onException:fd. |
|
5640 |
Processor activeProcess state:#ioWait. |
|
5641 |
sema wait. |
|
5642 |
hasTimedout := timeout notNil and:[(OperatingSystem readWriteCheck:fd) not]. |
|
5643 |
] ifCurtailed:[ |
|
5644 |
Processor disableSemaphore:sema. |
|
5645 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
5646 |
]. |
|
5647 |
timeout notNil ifTrue:[ |
|
5648 |
Processor disableSemaphore:sema. |
|
5649 |
]. |
|
5650 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
5651 |
^ hasTimedout |
|
5652 |
! |
|
5653 |
||
612 | 5654 |
writeWaitWithTimeoutMs:timeout |
5655 |
"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
|
5656 |
becomes ready for writing or a timeout (in milliseconds) expired. |
612 | 5657 |
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
|
5658 |
Return immediate if the receiver is already ready. |
612 | 5659 |
The other threads are not affected by the wait." |
5660 |
||
16480 | 5661 |
|fd outputSema hasTimedout wasBlocked| |
612 | 5662 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5663 |
handle isNil ifTrue:[ |
18797 | 5664 |
^ self errorNotOpen |
612 | 5665 |
]. |
5666 |
mode == #readonly ifTrue:[ |
|
18797 | 5667 |
^ self errorReadOnly |
612 | 5668 |
]. |
5669 |
||
5670 |
fd := self fileDescriptor. |
|
5671 |
(OperatingSystem writeCheck:fd) ifTrue:[^ false]. |
|
5672 |
||
5673 |
wasBlocked := OperatingSystem blockInterrupts. |
|
16463 | 5674 |
outputSema := Semaphore new name:'writeWait'. |
5675 |
[ |
|
18797 | 5676 |
timeout notNil ifTrue:[ |
5677 |
Processor signal:outputSema afterMilliseconds:timeout |
|
5678 |
]. |
|
5679 |
Processor signal:outputSema onOutput:fd. |
|
5680 |
Processor activeProcess state:#ioWait. |
|
5681 |
outputSema wait. |
|
5682 |
hasTimedout := timeout notNil and:[(OperatingSystem writeCheck:fd) not]. |
|
16463 | 5683 |
] ifCurtailed:[ |
18797 | 5684 |
Processor disableSemaphore:outputSema. |
5685 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
612 | 5686 |
]. |
16480 | 5687 |
timeout notNil ifTrue:[ |
18797 | 5688 |
Processor disableSemaphore:outputSema. |
16480 | 5689 |
]. |
612 | 5690 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
16480 | 5691 |
^ hasTimedout |
612 | 5692 |
! ! |
5693 |
||
5694 |
!ExternalStream methodsFor:'writing'! |
|
5695 |
||
5696 |
cr |
|
5697 |
"append an end-of-line character (or CRLF if in crlf mode). |
|
5698 |
reimplemented for speed" |
|
5699 |
||
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5700 |
%{ |
12548 | 5701 |
if ((__INST(handleType) == nil) |
5702 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
5703 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 5704 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
5705 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5706 |
OBJ fp; |
5707 |
||
5708 |
__INST(lastErrorNumber) = nil; |
|
5709 |
if (((fp = __INST(handle)) != nil) |
|
5710 |
&& (__INST(mode) != @symbol(readonly)) |
|
5711 |
&& (__INST(binary) != true) |
|
5712 |
) { |
|
5713 |
FILEPOINTER f = __FILEVal(fp); |
|
5714 |
int _buffered = (__INST(buffered) == true); |
|
5715 |
int len, cnt; |
|
5716 |
char *cp; |
|
5717 |
||
5718 |
if (_buffered) { |
|
5719 |
__WRITING__(f) |
|
5720 |
} |
|
5721 |
{ |
|
5722 |
OBJ mode = __INST(eolMode); |
|
5723 |
||
5724 |
if (mode == @symbol(cr)) { |
|
5725 |
cp = "\r"; len = 1; |
|
5726 |
} else if (mode == @symbol(crlf)) { |
|
5727 |
cp = "\r\n"; len = 2; |
|
5728 |
} else if (mode == @symbol(eot)) { |
|
5729 |
cp = "\004"; len = 1; |
|
5730 |
} else if (mode == @symbol(etx)) { |
|
5731 |
cp = "\003"; len = 1; |
|
5732 |
} else { |
|
5733 |
cp = "\n"; len = 1; |
|
5734 |
} |
|
5735 |
} |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5736 |
#ifdef WIN32 |
18797 | 5737 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
5738 |
cnt = __win32_fwrite(cp, 1, len, f); |
|
5739 |
} else |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5740 |
#endif |
18797 | 5741 |
{ |
5742 |
__WRITEBYTES__(cnt, f, cp, len, _buffered, __INST(handleType)); |
|
5743 |
} |
|
5744 |
if (cnt == len) { |
|
5745 |
if (__isSmallInteger(__INST(position))) { |
|
5746 |
INT np = __intVal(__INST(position)) + len; |
|
5747 |
OBJ t; |
|
5748 |
||
5749 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
5750 |
} else { |
|
5751 |
__INST(position) = nil; /* i.e: dont know */ |
|
5752 |
} |
|
5753 |
RETURN ( self ); |
|
5754 |
} |
|
5755 |
__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno); |
|
5756 |
} |
|
612 | 5757 |
} |
5758 |
%}. |
|
2326 | 5759 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
5760 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
2326 | 5761 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
5414 | 5762 |
(binary==true) ifTrue:[self errorBinary. ^ self]. |
5763 |
||
17426
6e49243ce4e0
fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents:
17420
diff
changeset
|
5764 |
(eolMode == #cr) ifTrue:[ |
18797 | 5765 |
self nextPut:(Character return). |
5766 |
^ 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
|
5767 |
]. |
6e49243ce4e0
fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents:
17420
diff
changeset
|
5768 |
(eolMode == #crlf) ifTrue:[ |
18797 | 5769 |
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
|
5770 |
]. |
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5771 |
self nextPut:(Character nl). |
5414 | 5772 |
^ self |
612 | 5773 |
! |
5774 |
||
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5775 |
flush |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5776 |
"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
|
5777 |
|
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5778 |
%{ |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5779 |
__INST(lastErrorNumber) = nil; |
12548 | 5780 |
if ((__INST(handleType) == nil) |
5781 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5782 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
5783 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5784 |
OBJ fp; |
5785 |
int _buffered = (__INST(buffered) == true); |
|
5786 |
||
5787 |
if ((fp = __INST(handle)) != nil) { |
|
5788 |
if (__INST(mode) != @symbol(readonly)) { |
|
5789 |
if (_buffered) { |
|
5790 |
FILEPOINTER f = __FILEVal(fp); |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
5791 |
#ifdef WIN32 |
18797 | 5792 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
5793 |
__win32_fflush(f); |
|
5794 |
} else { |
|
5795 |
int rslt; |
|
5796 |
||
5797 |
do { |
|
5798 |
__threadErrno = 0; |
|
5799 |
rslt = __STX_C_CALL1("fflush", fflush, f); |
|
5800 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
|
5801 |
} |
|
12678 | 5802 |
#else /* ! WIN32 */ |
18797 | 5803 |
__BEGIN_INTERRUPTABLE__ |
5804 |
FFLUSH(f); |
|
5805 |
__END_INTERRUPTABLE__ |
|
12678 | 5806 |
#endif /* ! WIN32 */ |
18797 | 5807 |
} |
5808 |
} |
|
5809 |
} |
|
1463
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5810 |
} |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5811 |
%} |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5812 |
! |
ace6b72b6f4a
Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
5813 |
|
612 | 5814 |
nextPut:aCharacter |
5815 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
5816 |
Only single-byte characters are currently supported" |
|
5817 |
||
16296 | 5818 |
|error| |
1067
1e7959af0821
removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents:
1066
diff
changeset
|
5819 |
%{ |
18364 | 5820 |
#ifdef __SCHTEAM__ |
5821 |
STObject handle = self.instVarAt(I_handle); |
|
5822 |
||
5823 |
if ((handle != STObject.Nil) |
|
5824 |
&& (aCharacter.isSTCharacter())) { |
|
18797 | 5825 |
handle.writeChar( aCharacter ); |
5826 |
self.instVarAt_put(I_position, STObject.Nil); |
|
5827 |
return __c__._RETURN_self(); |
|
18364 | 5828 |
} |
5829 |
#else |
|
17332 | 5830 |
__INST(lastErrorNumber) = nil; |
5831 |
if ((__INST(handleType) == nil) |
|
5832 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5833 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
5834 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
5835 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5836 |
OBJ fp; |
5837 |
||
5838 |
if (((fp = __INST(handle)) != nil) |
|
5839 |
&& (__INST(mode) != @symbol(readonly)) |
|
5840 |
) { |
|
5841 |
FILEPOINTER f = __FILEVal(fp); |
|
5842 |
int _buffered = (__INST(buffered) == true); |
|
5843 |
unsigned codePoint; |
|
5844 |
unsigned char c; |
|
5845 |
int cnt; |
|
5846 |
char buff[2]; |
|
5847 |
int nBytes = 1; |
|
5848 |
||
5849 |
if (__INST(binary) != true) { |
|
5850 |
if (__isCharacter(aCharacter)) { |
|
5851 |
||
5852 |
codePoint = __intVal(__characterVal(aCharacter)); |
|
5853 |
if (codePoint <= 0xFF) { |
|
5854 |
c = codePoint; |
|
5855 |
buff[0] = c; nBytes = 1; |
|
5856 |
||
5857 |
if (c == '\n') { |
|
5858 |
OBJ mode = __INST(eolMode); |
|
5859 |
if (mode == @symbol(nl)) { |
|
5860 |
// no EOL translation |
|
5861 |
} else if (mode == nil) { |
|
5862 |
// no EOL translation |
|
5863 |
} else if (mode == @symbol(cr)) { |
|
5864 |
buff[0] = '\r'; |
|
5865 |
} else if (mode == @symbol(eot)) { |
|
5866 |
buff[0] = '\004'; |
|
5867 |
} else if (mode == @symbol(etx)) { |
|
5868 |
buff[0] = '\003'; |
|
5869 |
} else if (mode == @symbol(crlf)) { |
|
5870 |
buff[0] = '\r'; |
|
5871 |
buff[1] = '\n'; |
|
5872 |
nBytes = 2; |
|
5873 |
} |
|
5874 |
} |
|
17332 | 5875 |
doWrite: |
18797 | 5876 |
if (! f) { |
5877 |
fprintf(stderr, "oops - fileHandle is NULL in nextPut:\n"); |
|
5878 |
__INST(handle) = nil; |
|
5879 |
goto out; |
|
5880 |
} |
|
5881 |
||
5882 |
if (_buffered) { |
|
5883 |
__WRITING__(f) |
|
5884 |
} |
|
18364 | 5885 |
# ifdef WIN32 |
18797 | 5886 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
5887 |
cnt = __win32_fwrite(buff, 1, nBytes, f); |
|
5888 |
} else |
|
18364 | 5889 |
# endif |
18797 | 5890 |
{ |
5891 |
__WRITEBYTES__(cnt, f, buff, nBytes, _buffered, __INST(handleType)); |
|
5892 |
} |
|
5893 |
if (cnt == nBytes) { |
|
5894 |
if (__isSmallInteger(__INST(position))) { |
|
5895 |
INT np = __intVal(__INST(position)) + nBytes; |
|
5896 |
OBJ t; |
|
5897 |
||
5898 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
5899 |
} else { |
|
5900 |
__INST(position) = nil; /* i.e. do not know */ |
|
5901 |
} |
|
5902 |
RETURN ( self ); |
|
5903 |
} |
|
5904 |
error = __mkSmallInteger(__threadErrno); |
|
5905 |
} |
|
5906 |
} |
|
5907 |
} else { |
|
5908 |
if (__isSmallInteger(aCharacter)) { |
|
5909 |
c = __intVal(aCharacter); |
|
5910 |
buff[0] = c; nBytes = 1; |
|
5911 |
goto doWrite; |
|
5912 |
} |
|
5913 |
} |
|
5914 |
} |
|
17332 | 5915 |
} |
5916 |
out: ; |
|
18364 | 5917 |
#endif /* not SCHTEAM */ |
17332 | 5918 |
%}. |
5919 |
error notNil ifTrue:[ |
|
18797 | 5920 |
lastErrorNumber := error. |
5921 |
self writeError:error. |
|
5922 |
^ self |
|
17332 | 5923 |
]. |
5924 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
|
5925 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
5926 |
binary == true ifFalse:[ |
|
18797 | 5927 |
(aCharacter isCharacter not |
5928 |
or:[aCharacter codePoint > 16rFF]) ifTrue:[ |
|
5929 |
self argumentMustBeCharacter. |
|
5930 |
^ self. |
|
5931 |
]. |
|
17332 | 5932 |
] ifTrue:[ |
18797 | 5933 |
aCharacter isInteger ifFalse:[ |
5934 |
self argumentMustBeInteger. |
|
5935 |
^ self. |
|
5936 |
]. |
|
17332 | 5937 |
]. |
5938 |
"/ migration support |
|
5939 |
self |
|
18797 | 5940 |
nextPutByte:aCharacter asInteger |
5941 |
toFile:handle |
|
17332 | 5942 |
! |
5943 |
||
17418 | 5944 |
nextPutAll:aCollection |
5945 |
"write all elements of the argument, aCollection. |
|
5946 |
Reimplemented for speed when writing strings or byteArrays. |
|
5947 |
For others, falls back to general method in superclass." |
|
5948 |
||
5949 |
|error| |
|
5950 |
%{ |
|
18364 | 5951 |
#ifdef __SCHTEAM__ |
5952 |
STObject handle = self.instVarAt(I_handle); |
|
5953 |
||
5954 |
if ((handle != STObject.Nil) |
|
5955 |
&& (aCollection.isSTString())) { |
|
18797 | 5956 |
handle.writeCharacters( aCollection.asSTString().characters ); |
5957 |
self.instVarAt_put(I_position, STObject.Nil); |
|
5958 |
return __c__._RETURN_self(); |
|
18364 | 5959 |
} |
5960 |
#else |
|
17418 | 5961 |
|
5962 |
__INST(lastErrorNumber) = nil; |
|
5963 |
||
5964 |
if ((__INST(handleType) == nil) |
|
5965 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
5966 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
5967 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
5968 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 5969 |
OBJ fp; |
5970 |
||
5971 |
if (((fp = __INST(handle)) != nil) |
|
5972 |
&& (__INST(mode) != @symbol(readonly)) |
|
5973 |
) { |
|
5974 |
INT len, cnt; |
|
5975 |
INT o_offs; |
|
5976 |
FILEPOINTER f = __FILEVal(fp); |
|
5977 |
int _buffered = (__INST(buffered) == true); |
|
5978 |
||
5979 |
if (! f) { |
|
5980 |
fprintf(stderr, "oops - fileHandle is NULL in nextPutAll:\n"); |
|
5981 |
__INST(handle) = nil; |
|
5982 |
goto out; |
|
5983 |
} |
|
5984 |
if (_buffered) { |
|
5985 |
__WRITING__(f) |
|
5986 |
} |
|
5987 |
||
5988 |
if (__isStringLike(aCollection)) { |
|
5989 |
OBJ mode = __INST(eolMode); |
|
5990 |
char *stringP = __stringVal(aCollection); |
|
5991 |
len = __stringSize(aCollection); |
|
5992 |
||
5993 |
if (__INST(binary) != true |
|
5994 |
&& ((mode == @symbol(cr)) |
|
5995 |
|| (mode == @symbol(etx)) |
|
5996 |
|| (mode == @symbol(eot)) |
|
5997 |
|| (mode == @symbol(crlf))) |
|
5998 |
&& memchr(stringP, '\n', len) != NULL) |
|
5999 |
{ |
|
6000 |
// there is a '\n' to be translated, replace it into a buffer |
|
6001 |
||
6002 |
char *end; |
|
6003 |
char sep[2]; |
|
6004 |
int sepLen = 1; |
|
6005 |
int bufLen; |
|
6006 |
char *buf, *endBuf, *sp, *dp; |
|
6007 |
||
6008 |
sep[0] = '\n'; |
|
6009 |
if (mode == @symbol(crlf)) { |
|
6010 |
sep[0] = '\r'; sep[1] = '\n'; sepLen = 2; |
|
6011 |
} else if (mode == @symbol(cr)) { |
|
6012 |
sep[0] = '\r'; |
|
6013 |
} else if (mode == @symbol(eot)) { |
|
6014 |
sep[0] = '\004'; |
|
6015 |
} else if (mode == @symbol(etx)) { |
|
6016 |
sep[0] = '\003'; |
|
6017 |
} |
|
6018 |
||
6019 |
// estimate size of buffer - assume every 4th char is a separator |
|
6020 |
bufLen = (sepLen == 1) ? len : (len + ((len/4) + 1) * sepLen); |
|
6021 |
buf = (char *)malloc(bufLen); |
|
6022 |
if (buf == NULL) { |
|
6023 |
error = __mkSmallInteger(ENOMEM); |
|
6024 |
goto out; |
|
6025 |
} |
|
6026 |
||
6027 |
endBuf = buf + bufLen; |
|
6028 |
end = stringP + len; |
|
6029 |
for (sp = stringP, dp = buf; sp < end; sp++) { |
|
6030 |
char c; |
|
6031 |
||
6032 |
if ((dp+sepLen) >= endBuf) { |
|
6033 |
char *newBuf; |
|
6034 |
||
6035 |
bufLen = bufLen * 2; |
|
6036 |
newBuf = (char *)realloc(buf, bufLen); |
|
6037 |
if (newBuf == NULL) { |
|
6038 |
free(buf); |
|
6039 |
error = __mkSmallInteger(ENOMEM); |
|
6040 |
goto out; |
|
6041 |
} |
|
6042 |
endBuf = newBuf + bufLen; |
|
6043 |
dp = newBuf + (dp-buf); |
|
6044 |
buf = newBuf; |
|
6045 |
} |
|
6046 |
||
6047 |
if ((c = *sp) != '\n') { |
|
6048 |
*dp++ = c; |
|
6049 |
} else { |
|
6050 |
*dp++ = sep[0]; |
|
6051 |
if (sepLen == 2) { |
|
6052 |
*dp++ = sep[1]; |
|
6053 |
}; |
|
6054 |
} |
|
6055 |
} |
|
6056 |
||
6057 |
len = dp - buf; |
|
18364 | 6058 |
# ifdef WIN32 |
18797 | 6059 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6060 |
cnt = __win32_fwrite(buf, 1, len, f); |
|
6061 |
} else |
|
18364 | 6062 |
# endif |
18797 | 6063 |
{ |
6064 |
__WRITEBYTES__(cnt, f, buf, len, _buffered, __INST(handleType)); |
|
6065 |
} |
|
6066 |
free(buf); |
|
6067 |
} else { // No EOL conversion needed |
|
18364 | 6068 |
# ifdef WIN32 |
18797 | 6069 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6070 |
cnt = __win32_fwrite(stringP, 1, len, f); |
|
6071 |
} else |
|
18364 | 6072 |
# endif |
18797 | 6073 |
{ |
6074 |
o_offs = stringP - (char *)__InstPtr(aCollection); |
|
6075 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered, __INST(handleType)); |
|
6076 |
} |
|
6077 |
} |
|
6078 |
} else { // Not a String |
|
6079 |
if (__INST(binary) == true) { |
|
6080 |
INT offs; |
|
6081 |
||
6082 |
if (__isByteArrayLike(aCollection)) { |
|
6083 |
offs = 0; |
|
6084 |
len = __byteArraySize(aCollection); |
|
6085 |
} else if (__isBytes(aCollection)) { |
|
6086 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
6087 |
len = __byteArraySize(aCollection) - offs; |
|
6088 |
} else |
|
6089 |
goto out; |
|
18364 | 6090 |
# ifdef WIN32 |
18797 | 6091 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6092 |
cnt = __win32_fwrite(__stringVal(aCollection), 1, len, f); |
|
6093 |
} else |
|
18364 | 6094 |
# endif |
18797 | 6095 |
{ |
6096 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element) - (char *)__InstPtr(aCollection); |
|
6097 |
o_offs += offs; |
|
6098 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered, __INST(handleType)); |
|
6099 |
} |
|
6100 |
} else // Not binary mode |
|
6101 |
goto out; |
|
6102 |
} |
|
6103 |
||
6104 |
// Now check for errors |
|
6105 |
if (cnt == len) { |
|
6106 |
if (__isSmallInteger(__INST(position))) { |
|
6107 |
INT np = __intVal(__INST(position)) + len; |
|
6108 |
OBJ t; |
|
6109 |
||
6110 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6111 |
} else { |
|
6112 |
__INST(position) = nil; /* i.e. do not know */ |
|
6113 |
} |
|
6114 |
RETURN (self); |
|
6115 |
} |
|
6116 |
fprintf(stderr, "cnt=%d len=%d\n", cnt, len); |
|
6117 |
error = __mkSmallInteger(__threadErrno); |
|
6118 |
} |
|
17418 | 6119 |
} |
6120 |
out: ; |
|
18364 | 6121 |
#endif /* not SCHTEAM */ |
17418 | 6122 |
%}. |
6123 |
error notNil ifTrue:[ |
|
18797 | 6124 |
lastErrorNumber := error. |
6125 |
self writeError:error. |
|
6126 |
^ self |
|
17418 | 6127 |
]. |
6128 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
|
6129 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
6130 |
||
6131 |
^ super nextPutAll:aCollection |
|
6132 |
! |
|
6133 |
||
17608 | 6134 |
nextPutAll:initialWriteCount from:buffer startingAt:initialOffset |
6135 |
"redefined, to wait until stream is writable, to avoid blocking in a write. |
|
6136 |
I don't know how to write non-bytes to a external stream, but let superclass handle this." |
|
6137 |
||
6138 |
buffer isByteCollection ifTrue:[ |
|
18797 | 6139 |
^ self nextPutBytes:initialWriteCount from:buffer startingAt:initialOffset. |
17608 | 6140 |
]. |
6141 |
||
6142 |
^ super nextPutAll:initialWriteCount from:buffer startingAt:initialOffset |
|
6143 |
! |
|
6144 |
||
17418 | 6145 |
nextPutAll:aCollection startingAt:start to:stop |
6146 |
"write a range of elements of the argument, aCollection. |
|
6147 |
Reimplemented for speed when writing strings or byteArrays. |
|
6148 |
For others, falls back to general method in superclass." |
|
6149 |
||
6150 |
|error| |
|
6151 |
||
6152 |
%{ |
|
6153 |
__INST(lastErrorNumber) = nil; |
|
6154 |
if ((__INST(handleType) == nil) |
|
6155 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
6156 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
6157 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
6158 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 6159 |
OBJ fp = __INST(handle); |
6160 |
||
6161 |
if ((fp != nil) |
|
6162 |
&& (__INST(mode) != @symbol(readonly)) |
|
6163 |
&& __bothSmallInteger(start, stop) |
|
6164 |
) { |
|
6165 |
int _buffered = (__INST(buffered) == true); |
|
6166 |
FILEPOINTER f = __FILEVal(fp); |
|
6167 |
int offs, len, cnt; |
|
6168 |
int iStart = __intVal(start); |
|
6169 |
int iStop = __intVal(stop); |
|
6170 |
int o_offs; |
|
6171 |
||
6172 |
if (_buffered ) { |
|
6173 |
__WRITING__(f) |
|
6174 |
} |
|
6175 |
||
6176 |
if ((iStart < 1) || (iStop < iStart)) { |
|
6177 |
RETURN(self); |
|
6178 |
} |
|
6179 |
if (__isStringLike(aCollection)) { |
|
6180 |
char *stringP; |
|
6181 |
OBJ mode = __INST(eolMode); |
|
6182 |
||
6183 |
len = __stringSize(aCollection); |
|
6184 |
if (iStop > len) { |
|
6185 |
RETURN(self); |
|
6186 |
} |
|
6187 |
if (iStop > len) |
|
6188 |
iStop = len; |
|
6189 |
len = iStop - iStart + 1; |
|
6190 |
stringP = __stringVal(aCollection) + iStart - 1; |
|
6191 |
||
6192 |
if (__INST(binary) != true |
|
6193 |
&& ((mode == @symbol(cr)) |
|
6194 |
|| (mode == @symbol(etx)) |
|
6195 |
|| (mode == @symbol(eot)) |
|
6196 |
|| (mode == @symbol(crlf))) |
|
6197 |
&& memchr(stringP, '\n', len) != NULL) |
|
6198 |
{ |
|
6199 |
// see if there is a \n which needs to be translated, replace it |
|
6200 |
||
6201 |
char *end = stringP + len; |
|
6202 |
char sep[2]; |
|
6203 |
int sepLen = 1; |
|
6204 |
int bufLen; |
|
6205 |
char *buf, *endBuf, *sp, *dp; |
|
6206 |
||
6207 |
sep[0] = '\n'; |
|
6208 |
if (mode == @symbol(crlf)) { |
|
6209 |
sep[0] = '\r'; sep[1] = '\n'; sepLen = 2; |
|
6210 |
} else if (mode == @symbol(cr)) { |
|
6211 |
sep[0] = '\r'; |
|
6212 |
} else if (mode == @symbol(eot)) { |
|
6213 |
sep[0] = '\004'; |
|
6214 |
} else if (mode == @symbol(etx)) { |
|
6215 |
sep[0] = '\003'; |
|
6216 |
} |
|
6217 |
||
6218 |
// estimate size of buffer - assume every 4th char is a separator |
|
6219 |
bufLen = (sepLen == 1) ? len : (len + ((len/4) + 1) * sepLen); |
|
6220 |
buf = (char *)malloc(bufLen); |
|
6221 |
if (buf == NULL) { |
|
6222 |
error = __mkSmallInteger(ENOMEM); |
|
6223 |
goto out; |
|
6224 |
} |
|
6225 |
||
6226 |
endBuf = buf + bufLen; |
|
6227 |
||
6228 |
for (sp = stringP, dp = buf; sp < end; sp++) { |
|
6229 |
char c; |
|
6230 |
||
6231 |
if ((dp+sepLen) >= endBuf) { |
|
6232 |
char *newBuf; |
|
6233 |
||
6234 |
bufLen = bufLen * 2; |
|
6235 |
newBuf = (char *)realloc(buf, bufLen); |
|
6236 |
if (newBuf == NULL) { |
|
6237 |
free(buf); |
|
6238 |
error = __mkSmallInteger(ENOMEM); |
|
6239 |
goto out; |
|
6240 |
} |
|
6241 |
endBuf = newBuf + bufLen; |
|
6242 |
dp = newBuf + (dp-buf); |
|
6243 |
buf = newBuf; |
|
6244 |
} |
|
6245 |
||
6246 |
if ((c = *sp) == '\n') { |
|
6247 |
*dp++ = sep[0]; |
|
6248 |
if (sepLen == 2) { |
|
6249 |
*dp++ = sep[1]; |
|
6250 |
}; |
|
6251 |
} else { |
|
6252 |
*dp++ = c; |
|
6253 |
} |
|
6254 |
} |
|
6255 |
||
6256 |
len = dp - buf; |
|
17420 | 6257 |
#ifdef WIN32 |
18797 | 6258 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6259 |
cnt = __win32_fwrite(buf, 1, len, f); |
|
6260 |
} else |
|
17420 | 6261 |
#endif |
18797 | 6262 |
{ |
6263 |
__WRITEBYTES__(cnt, f, buf, len, _buffered, __INST(handleType)); |
|
6264 |
} |
|
6265 |
free(buf); |
|
6266 |
} else { // No EOL conversion needed |
|
17420 | 6267 |
#ifdef WIN32 |
18797 | 6268 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6269 |
cnt = __win32_fwrite(stringP, 1, len, f); |
|
6270 |
} else |
|
17420 | 6271 |
#endif |
18797 | 6272 |
{ |
6273 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
|
6274 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered, __INST(handleType)); |
|
6275 |
} |
|
6276 |
} |
|
6277 |
} else { // Not a string |
|
6278 |
if (__INST(binary) == true) { |
|
6279 |
int offs; |
|
6280 |
||
6281 |
if (__isByteArrayLike(aCollection)) { |
|
6282 |
offs = 0; |
|
6283 |
len = __byteArraySize(aCollection); |
|
6284 |
} else if (__isBytes(aCollection)) { |
|
6285 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
6286 |
len = __byteArraySize(aCollection) - offs; |
|
6287 |
} else |
|
6288 |
goto out; |
|
6289 |
||
6290 |
if (iStop > len) { |
|
6291 |
RETURN(self); |
|
6292 |
} |
|
6293 |
if (iStop > len) |
|
6294 |
iStop = len; |
|
6295 |
len = iStop - iStart + 1; |
|
6296 |
offs += iStart - 1; |
|
17418 | 6297 |
#ifdef WIN32 |
18797 | 6298 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6299 |
cnt = __win32_fwrite(__stringVal(aCollection)+iStart-1, 1, len, f); |
|
6300 |
} else |
|
17418 | 6301 |
#endif |
18797 | 6302 |
{ |
6303 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection); |
|
6304 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+offs, len, _buffered, __INST(handleType)); |
|
6305 |
} |
|
6306 |
} else |
|
6307 |
goto out; |
|
6308 |
} |
|
6309 |
if (cnt == len) { |
|
6310 |
if (__isSmallInteger(__INST(position))) { |
|
6311 |
INT np = __intVal(__INST(position)) + len; |
|
6312 |
OBJ t; |
|
6313 |
||
6314 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6315 |
} else { |
|
6316 |
__INST(position) = nil; /* i.e. do not know */ |
|
6317 |
} |
|
6318 |
RETURN (self); |
|
6319 |
} |
|
6320 |
error = __mkSmallInteger(__threadErrno); |
|
6321 |
} |
|
17418 | 6322 |
} |
6323 |
out: ; |
|
6324 |
%}. |
|
6325 |
error notNil ifTrue:[ |
|
18797 | 6326 |
lastErrorNumber := error. |
6327 |
self writeError:error. |
|
6328 |
^ self |
|
17418 | 6329 |
]. |
6330 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
6331 |
! |
|
6332 |
||
6333 |
nextPutAllUnicode:aString |
|
6334 |
"normal streams can not handle multi-byte characters, so convert them to utf8. |
|
6335 |
This is needed, so that you can do ('something' asUnicode16String errorPrintCR)" |
|
6336 |
||
6337 |
aString do:[:eachCharacter| |
|
18797 | 6338 |
self nextPutUtf8:eachCharacter. |
17418 | 6339 |
]. |
6340 |
||
6341 |
" |
|
18929 | 6342 |
'Bönnigheim' asUnicode16String errorPrintCR |
17418 | 6343 |
" |
6344 |
! |
|
6345 |
||
17608 | 6346 |
nextPutBytes:count from:anObject startingAt:start |
6347 |
"write count bytes from an object starting at index start. |
|
6348 |
return the number of bytes written - which could be 0. |
|
6349 |
The object must have non-pointer indexed instvars |
|
6350 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
|
6351 |
or an externalBytes object (with known size). |
|
6352 |
||
6353 |
Use with care - non object oriented i/o. |
|
6354 |
Warning: |
|
18797 | 6355 |
in general, you cannot use this method to pass non-byte data to other |
6356 |
architectures (unless you prepared the buffer with care), |
|
6357 |
since it does not care for byte order or float representation." |
|
17608 | 6358 |
|
6359 |
|error| |
|
6360 |
||
6361 |
%{ |
|
18312 | 6362 |
#ifdef __SCHTEAM__ |
6363 |
byte[] bytes; |
|
6364 |
STObject handle = self.instVarAt(I_handle); |
|
6365 |
||
6366 |
if (anObject.isSTString()) { |
|
18797 | 6367 |
char[] chars = anObject.asSTString().characters; |
6368 |
handle.writeCharacters(chars, start.intValue()-1, count.intValue()); |
|
6369 |
self.instVarAt_put(I_position, STObject.Nil); |
|
6370 |
return context._RETURN(count); |
|
18364 | 6371 |
} |
6372 |
if (anObject.isSymbol()) { |
|
18797 | 6373 |
java.lang.String chars = anObject.asSTSymbol().characters; |
6374 |
handle.writeString(chars, start.intValue()-1, count.intValue()); |
|
6375 |
self.instVarAt_put(I_position, STObject.Nil); |
|
6376 |
return context._RETURN(count); |
|
18312 | 6377 |
} |
6378 |
#else |
|
18652
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
6379 |
int ret; |
ee5e81c67497
moved local C variables down into their blcoks
Claus Gittinger <cg@exept.de>
parents:
18636
diff
changeset
|
6380 |
int objSize, nInstBytes; |
17608 | 6381 |
char *extPtr; |
6382 |
OBJ fp; |
|
6383 |
||
6384 |
__INST(lastErrorNumber) = nil; |
|
6385 |
if ((__INST(handleType) == nil) |
|
6386 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
6387 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
6388 |
|| (__INST(handleType) == @symbol(socketHandle)) |
|
6389 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
|
18797 | 6390 |
if (((fp = __INST(handle)) != nil) |
6391 |
&& (__INST(mode) != @symbol(readonly)) |
|
6392 |
&& __bothSmallInteger(count, start) |
|
6393 |
) { |
|
6394 |
int _buffered = (__INST(buffered) == true); |
|
6395 |
FILEPOINTER f = __FILEVal(fp); |
|
6396 |
int len = __intVal(count); |
|
6397 |
int offs = __intVal(start) - 1; |
|
6398 |
||
6399 |
if (__isExternalBytesLike(anObject)) { |
|
6400 |
OBJ sz; |
|
6401 |
||
6402 |
nInstBytes = 0; |
|
6403 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
6404 |
if (extPtr == NULL) goto bad; |
|
6405 |
sz = __externalBytesSize(anObject); |
|
6406 |
if (__isSmallInteger(sz)) { |
|
6407 |
objSize = __intVal(sz); |
|
6408 |
} else { |
|
6409 |
objSize = 0; /* unknown */ |
|
6410 |
} |
|
6411 |
} else { |
|
6412 |
OBJ oClass = __Class(anObject); |
|
6413 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
6414 |
||
6415 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
6416 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
6417 |
case BYTEARRAY: |
|
6418 |
case WORDARRAY: |
|
6419 |
case LONGARRAY: |
|
6420 |
case SWORDARRAY: |
|
6421 |
case SLONGARRAY: |
|
6422 |
case FLOATARRAY: |
|
6423 |
break; |
|
6424 |
case DOUBLEARRAY: |
|
18312 | 6425 |
# ifdef __NEED_DOUBLE_ALIGN |
18797 | 6426 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
18312 | 6427 |
# endif |
18797 | 6428 |
break; |
6429 |
case LONGLONGARRAY: |
|
6430 |
case SLONGLONGARRAY: |
|
18312 | 6431 |
# ifdef __NEED_LONGLONG_ALIGN |
18797 | 6432 |
nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1); |
18312 | 6433 |
# endif |
18797 | 6434 |
break; |
6435 |
default: |
|
6436 |
goto bad; |
|
6437 |
} |
|
6438 |
extPtr = (char *)0; |
|
6439 |
objSize = __Size(anObject) - nInstBytes; |
|
6440 |
} |
|
6441 |
if ( (offs >= 0) && (len >= 0) && (objSize >= (len + offs)) ) { |
|
6442 |
int cnt; |
|
6443 |
||
6444 |
if (_buffered) { |
|
6445 |
__WRITING__(f) |
|
6446 |
} |
|
6447 |
||
6448 |
if (extPtr) { |
|
18312 | 6449 |
# ifdef WIN32 |
18797 | 6450 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6451 |
cnt = __win32_fwrite(extPtr+offs, 1, len, f); |
|
6452 |
} else |
|
18312 | 6453 |
# endif |
18797 | 6454 |
{ |
6455 |
__WRITEBYTES__(cnt, f, extPtr+offs, len, _buffered, __INST(handleType)); |
|
6456 |
} |
|
6457 |
} else { |
|
6458 |
/* |
|
6459 |
* on interrupt, anObject may be moved to another location. |
|
6460 |
* So we pass anObject, and the offset to the __WRITEBYTES_OBJ__ macro. |
|
6461 |
*/ |
|
6462 |
offs += nInstBytes; |
|
18312 | 6463 |
# ifdef WIN32 |
18797 | 6464 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6465 |
cnt = __win32_fwrite((char *)anObject+offs, 1, len, f); |
|
6466 |
} else |
|
18312 | 6467 |
# endif |
18797 | 6468 |
{ |
6469 |
__WRITEBYTES_OBJ__(cnt, f, anObject, offs, len, _buffered, __INST(handleType)); |
|
6470 |
} |
|
6471 |
} |
|
6472 |
||
6473 |
if (cnt >= 0) { |
|
6474 |
if (__isSmallInteger(__INST(position))) { |
|
6475 |
INT np = __intVal(__INST(position)) + cnt; |
|
6476 |
OBJ t; |
|
6477 |
||
6478 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6479 |
} else { |
|
6480 |
__INST(position) = nil; /* i.e. do not know */ |
|
6481 |
} |
|
6482 |
RETURN ( __mkSmallInteger(cnt) ); |
|
6483 |
} else /* cnt < 0 */ { |
|
6484 |
if ( |
|
18312 | 6485 |
# ifdef EWOULDBLOCK |
18797 | 6486 |
(__threadErrno == EWOULDBLOCK) || |
18312 | 6487 |
# endif |
18797 | 6488 |
(__threadErrno == EAGAIN) |
6489 |
) { |
|
6490 |
RETURN ( __mkSmallInteger(0) ); |
|
6491 |
} |
|
6492 |
__INST(position) = nil; /* i.e. do not know */ |
|
6493 |
error = __mkSmallInteger(__threadErrno); |
|
6494 |
} |
|
6495 |
} |
|
6496 |
} |
|
17608 | 6497 |
} |
6498 |
bad: ; |
|
18312 | 6499 |
#endif /* not SCHTEAM */ |
17608 | 6500 |
%}. |
6501 |
error notNil ifTrue:[ |
|
18797 | 6502 |
lastErrorNumber := error. |
6503 |
self writeError:error. |
|
6504 |
^ 0 |
|
17608 | 6505 |
]. |
17634 | 6506 |
handle isNil ifTrue:[self errorNotOpen. ^ 0]. |
6507 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ 0]. |
|
6508 |
self primitiveFailed. |
|
6509 |
^ 0. |
|
17608 | 6510 |
! |
6511 |
||
17418 | 6512 |
nextPutUnicode:aCharacter |
6513 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
6514 |
||
6515 |
self nextPutUtf8:aCharacter. |
|
6516 |
! |
|
6517 |
||
6518 |
nextPutUtf16:aCharacter |
|
6519 |
"append my UTF-16 representation to the argument, aStream. |
|
6520 |
UTF-16 can encode only characters with code points between 0 to 16r10FFFF." |
|
6521 |
||
6522 |
|codePoint| |
|
6523 |
||
6524 |
codePoint := aCharacter codePoint. |
|
6525 |
(codePoint <= 16rD7FF |
|
6526 |
or:[codePoint >= 16rE000 and:[codePoint <= 16rFFFF]]) ifTrue:[ |
|
18797 | 6527 |
self nextPutShort:codePoint MSB:true. |
17418 | 6528 |
] ifFalse:[codePoint <= 16r10FFFF ifTrue:[ |
18797 | 6529 |
|highBits lowBits| |
6530 |
||
6531 |
codePoint := codePoint - 16r100000. |
|
6532 |
highBits := codePoint bitShift:-10. |
|
6533 |
lowBits := codePoint bitAnd:16r3FF. |
|
6534 |
self nextPutShort:highBits+16rD800 MSB:true. |
|
6535 |
self nextPutShort:lowBits+16rDC00 MSB:true. |
|
17418 | 6536 |
] ifFalse:[ |
18797 | 6537 |
EncodingError raiseWith:aCharacter errorString:'Character cannot be encoded as UTF-16'. |
17418 | 6538 |
]]. |
6539 |
||
6540 |
" |
|
18797 | 6541 |
(FileStream newTemporary |
6542 |
nextPutUtf16:$B; |
|
18929 | 6543 |
nextPutUtf16:$Ä; |
18797 | 6544 |
nextPutUtf16:(Character codePoint:16r10CCCC); |
6545 |
reset; |
|
6546 |
binary; |
|
6547 |
contents) |
|
17418 | 6548 |
" |
6549 |
! |
|
6550 |
||
17332 | 6551 |
old_nextPut:aCharacter |
6552 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
6553 |
Only single-byte characters are currently supported" |
|
6554 |
||
6555 |
|error| |
|
6556 |
%{ |
|
6557 |
OBJ fp; |
|
612 | 6558 |
|
1133 | 6559 |
__INST(lastErrorNumber) = nil; |
12548 | 6560 |
if ((__INST(handleType) == nil) |
6561 |
|| (__INST(handleType) == @symbol(filePointer)) |
|
6562 |
|| (__INST(handleType) == @symbol(socketFilePointer)) |
|
16368
1efe4d36fd3f
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16308
diff
changeset
|
6563 |
|| (__INST(handleType) == @symbol(socketHandle)) |
12548 | 6564 |
|| (__INST(handleType) == @symbol(pipeFilePointer))) { |
18797 | 6565 |
if (((fp = __INST(handle)) != nil) |
6566 |
&& (__INST(mode) != @symbol(readonly)) |
|
6567 |
) { |
|
6568 |
unsigned char c; |
|
6569 |
int _buffered = (__INST(buffered) == true); |
|
6570 |
FILEPOINTER f = __FILEVal(fp); |
|
6571 |
||
6572 |
if (__INST(binary) != true) { |
|
6573 |
if (__isCharacter(aCharacter)) { |
|
6574 |
unsigned codePoint = __intVal(__characterVal(aCharacter)); |
|
6575 |
||
6576 |
if (codePoint <= 0xFF) { |
|
6577 |
int cnt; |
|
6578 |
||
6579 |
c = codePoint; |
|
612 | 6580 |
doWrite: |
18797 | 6581 |
if (! f) { |
6582 |
fprintf(stderr, "oops - fileHandle is NULL in nextPut:\n"); |
|
6583 |
__INST(handle) = nil; |
|
6584 |
goto out; |
|
6585 |
} |
|
6586 |
||
6587 |
if (_buffered) { |
|
6588 |
__WRITING__(f) |
|
6589 |
} |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
6590 |
#ifdef WIN32 |
18797 | 6591 |
if ((f == __win32_stdout()) || (f == __win32_stderr())) { |
6592 |
cnt = __win32_fwrite(&c, 1, 1, f); |
|
6593 |
} else |
|
9053
f3f3d48d6f44
fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents:
9024
diff
changeset
|
6594 |
#endif |
18797 | 6595 |
__WRITEBYTE__(cnt, f, &c, _buffered, __INST(handleType)); |
6596 |
if (cnt == 1) { |
|
6597 |
if (__isSmallInteger(__INST(position))) { |
|
6598 |
INT np = __intVal(__INST(position)) + 1; |
|
6599 |
OBJ t; |
|
6600 |
||
6601 |
t = __MKINT(np); __INST(position) = t; __STORE(self, t); |
|
6602 |
} else { |
|
6603 |
__INST(position) = nil; /* i.e. do not know */ |
|
6604 |
} |
|
6605 |
RETURN ( self ); |
|
6606 |
} |
|
6607 |
error = __mkSmallInteger(__threadErrno); |
|
6608 |
} |
|
6609 |
} |
|
6610 |
} else { |
|
6611 |
if (__isSmallInteger(aCharacter)) { |
|
6612 |
c = __intVal(aCharacter); |
|
6613 |
goto doWrite; |
|
6614 |
} |
|
6615 |
} |
|
6616 |
} |
|
612 | 6617 |
} |
7737 | 6618 |
out: ; |
612 | 6619 |
%}. |
16296 | 6620 |
error notNil ifTrue:[ |
18797 | 6621 |
lastErrorNumber := error. |
6622 |
self writeError:error. |
|
6623 |
^ self |
|
16296 | 6624 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11751
diff
changeset
|
6625 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
2331 | 6626 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
5417 | 6627 |
binary == true ifFalse:[ |
18797 | 6628 |
(aCharacter isCharacter not |
6629 |
or:[aCharacter codePoint > 16rFF]) ifTrue:[ |
|
6630 |
self argumentMustBeCharacter. |
|
6631 |
^ self. |
|
6632 |
]. |
|
5417 | 6633 |
] ifTrue:[ |
18797 | 6634 |
aCharacter isInteger ifFalse:[ |
6635 |
self argumentMustBeInteger. |
|
6636 |
^ self. |
|
6637 |
]. |
|
5417 | 6638 |
]. |
6639 |
"/ migration support |
|
8818
4b1ffc19e3ce
remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents:
8616
diff
changeset
|
6640 |
self |
18797 | 6641 |
nextPutByte:aCharacter asInteger |
6642 |
toFile:handle |
|
612 | 6643 |
! ! |
6644 |
||
2045
1ad17ca6520f
fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents:
2044
diff
changeset
|
6645 |
!ExternalStream class methodsFor:'documentation'! |
730 | 6646 |
|
6647 |
version |
|
18636 | 6648 |
^ '$Header$' |
12398 | 6649 |
! |
6650 |
||
6651 |
version_CVS |
|
18636 | 6652 |
^ '$Header$' |
730 | 6653 |
! ! |
6804
caf6a4521d4c
added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents:
6758
diff
changeset
|
6654 |
|
14603 | 6655 |
|
612 | 6656 |
ExternalStream initialize! |