ExternalStream.st
author Claus Gittinger <cg@exept.de>
Sun, 03 May 2015 14:38:52 +0200
changeset 18312 b414c7a96a1e
parent 18218 80b81f1f725a
child 18315 e678ab267382
child 18327 57a095b55d92
permissions -rw-r--r--
comment sonly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
     1
"{ Encoding: utf8 }"
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     4
 COPYRIGHT (c) 1988 by Claus Gittinger
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5414
8557e4e71e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5323
diff changeset
    14
"{ Package: 'stx:libbasic' }"
8557e4e71e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5323
diff changeset
    15
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
    16
"{ NameSpace: Smalltalk }"
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
    17
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
ReadWriteStream subclass:#ExternalStream
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
    19
	instanceVariableNames:'handleType handle mode buffered binary eolMode hitEOF didWrite
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    20
		lastErrorNumber readAhead'
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    21
	classVariableNames:'Lobby LastErrorNumber InvalidReadSignal InvalidWriteSignal
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    22
		InvalidModeSignal OpenErrorSignal StreamNotOpenSignal
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    23
		InvalidOperationSignal DefaultEOLMode ReadMode ReadWriteMode
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    24
		WriteMode AppendMode CreateReadWriteMode StreamIOErrorSignal
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    25
		FileOpenTrace'
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    26
	poolDictionaries:''
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
    27
	category:'Streams-External'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    28
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    29
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
    30
!ExternalStream primitiveDefinitions!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    31
%{
10328
ad731a7584a3 ensure that win32_fwrite is used for stdout and stderr
Claus Gittinger <cg@exept.de>
parents: 9053
diff changeset
    32
#undef fwrite
ad731a7584a3 ensure that win32_fwrite is used for stdout and stderr
Claus Gittinger <cg@exept.de>
parents: 9053
diff changeset
    33
17078
f3ded8a6f9d9 include os defss
Claus Gittinger <cg@exept.de>
parents: 16912
diff changeset
    34
#include "stxOSDefs.h"
f3ded8a6f9d9 include os defss
Claus Gittinger <cg@exept.de>
parents: 16912
diff changeset
    35
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    36
#include <stdio.h>
437
claus
parents: 436
diff changeset
    37
#define _STDIO_H_INCLUDED_
claus
parents: 436
diff changeset
    38
2903
e64704e86cce various VMS changes
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    39
#ifndef NO_FCNTL_H
e64704e86cce various VMS changes
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    40
# include <fcntl.h>
e64704e86cce various VMS changes
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    41
# define _FCNTL_H_INCLUDED_
e64704e86cce various VMS changes
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    42
#endif
437
claus
parents: 436
diff changeset
    43
10
claus
parents: 5
diff changeset
    44
#include <errno.h>
437
claus
parents: 436
diff changeset
    45
#define _ERRNO_H_INCLUDED_
42
e33491f6f260 *** empty log message ***
claus
parents: 38
diff changeset
    46
1141
8ea1b43f7034 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    47
#ifdef LINUX
8ea1b43f7034 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    48
  /* use inline string macros */
8ea1b43f7034 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    49
# define __STRINGDEFS__
8ea1b43f7034 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    50
# include <linuxIntern.h>
8ea1b43f7034 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    51
#endif
8ea1b43f7034 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    52
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
    53
#if !defined(MSDOS_LIKE) && !defined(__openVMS__)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
    54
# include <termios.h>
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
    55
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
    56
42
e33491f6f260 *** empty log message ***
claus
parents: 38
diff changeset
    57
#ifdef hpux
e33491f6f260 *** empty log message ***
claus
parents: 38
diff changeset
    58
# define fileno(f)      ((f->__fileH << 8) | (f->__fileL))
e33491f6f260 *** empty log message ***
claus
parents: 38
diff changeset
    59
#endif
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
    60
933
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    61
#ifndef SEEK_SET
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    62
# define SEEK_SET 0
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    63
#endif
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    64
#ifndef SEEK_CUR
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    65
# define SEEK_CUR 1
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    66
#endif
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    67
#ifndef SEEK_END
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    68
# define SEEK_END 2
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    69
#endif
2eebae059045 oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents: 932
diff changeset
    70
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
    71
#if defined(__VMS__) || defined(__sparc__) || defined(WIN32)
5120
13143fbcbdf8 introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents: 5101
diff changeset
    72
# define CLEAR_ERRNO            __threadErrno = 0;
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
    73
#else
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
    74
# define CLEAR_ERRNO            /* nothing */
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
    75
#endif
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
    76
3420
4dc6667afda9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3419
diff changeset
    77
#ifdef LATER
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
    78
# define __isFilePointer(x)      (__Class(x) == ExternalStream__FilePointer)
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
    79
# define __CHANGECLASS(o, x)     (__qClass(o) = (x), o)
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
    80
# define __MKFILEPOINTER(f)      __CHANGECLASS(__MKEXTERNALADDRESS(f), ExternalStream__FilePointer)
3420
4dc6667afda9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3419
diff changeset
    81
#else
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
    82
# define __MKFILEPOINTER(f)      __MKEXTERNALADDRESS(f)
3420
4dc6667afda9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3419
diff changeset
    83
#endif
4dc6667afda9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3419
diff changeset
    84
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    85
#ifdef WIN32
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    86
# define NO_STDIO
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    87
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    88
# undef __HANDLE_INTERRUPTS__
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    89
# define __HANDLE_INTERRUPTS__    /* nothing */
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    90
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    91
# undef __BEGIN_INTERRUPTABLE__
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    92
# define __BEGIN_INTERRUPTABLE__  /* nothing */
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    93
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    94
# undef __END_INTERRUPTABLE__
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    95
# define __END_INTERRUPTABLE__    /* nothing */
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
    96
#endif
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
    97
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
    98
# ifdef __i386__
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
    99
#  define _X86_
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   100
# endif
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   101
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   102
#ifdef WIN32
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   103
# undef INT
14644
e5f19ef20dcd mingw changes
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   104
# undef UINT
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   105
# undef Array
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   106
# undef Number
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   107
# undef Method
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   108
# undef Point
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   109
# undef Rectangle
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   110
# undef Block
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   111
# undef Time
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   112
# undef Date
7832
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
   113
# undef Delay
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   114
# undef Set
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   115
# undef Signal
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   116
# undef Context
9023
7919c66b8fb0 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9002
diff changeset
   117
# undef Message
9024
8066b17d9ccf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9023
diff changeset
   118
# undef Process
10339
972d0164125a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10328
diff changeset
   119
# undef Processor
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   120
# undef String
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   121
# undef Character
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   122
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   123
# define NOATOM
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   124
# define NOGDICAPMASKS
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   125
# define NOMETAFILE
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   126
# define NOMINMAX
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
   127
//# define NOOPENFILE
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   128
# define NOSOUND
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   129
# define NOWH
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   130
//# define NOCOMM
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   131
# define NOKANJI
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   132
# define NOCRYPT
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   133
//# define NOMCX
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   134
# define WIN32_LEAN_AND_MEAN
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   135
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   136
# include <windows.h>
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
   137
# include <winsock2.h>
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   138
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   139
# ifdef __DEF_Array
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   140
#  define Array __DEF_Array
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   141
# endif
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   142
# ifdef __DEF_Number
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   143
#  define Number __DEF_Number
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   144
# endif
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   145
# ifdef __DEF_Method
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   146
#  define Method __DEF_Method
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   147
# endif
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   148
# ifdef __DEF_Point
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   149
#  define Point __DEF_Point
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   150
# endif
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   151
# ifdef __DEF_Block
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   152
#  define Block __DEF_Block
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   153
# endif
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   154
# ifdef __DEF_Time
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   155
#  define Time __DEF_Time
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   156
# endif
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   157
# ifdef __DEF_Date
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   158
#  define Date __DEF_Date
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   159
# endif
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   160
# ifdef __DEF_Set
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   161
#  define Set __DEF_Set
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   162
# endif
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   163
# ifdef __DEF_Signal
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   164
#  define Signal __DEF_Signal
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   165
# endif
7832
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
   166
# ifdef __DEF_Delay
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
   167
#  define Delay __DEF_Delay
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
   168
# endif
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   169
# ifdef __DEF_Context
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   170
#  define Context __DEF_Context
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   171
# endif
9023
7919c66b8fb0 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9002
diff changeset
   172
# ifdef __DEF_Message
7919c66b8fb0 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9002
diff changeset
   173
#  define Message __DEF_Message
7919c66b8fb0 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9002
diff changeset
   174
# endif
9024
8066b17d9ccf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9023
diff changeset
   175
# ifdef __DEF_Process
8066b17d9ccf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9023
diff changeset
   176
#  define Process __DEF_Process
8066b17d9ccf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9023
diff changeset
   177
# endif
10339
972d0164125a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10328
diff changeset
   178
# ifdef __DEF_Processor
972d0164125a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10328
diff changeset
   179
#  define Processor __DEF_Processor
972d0164125a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10328
diff changeset
   180
# endif
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   181
# ifdef __DEF_String
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   182
#  define String __DEF_String
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   183
# endif
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   184
# ifdef __DEF_Character
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   185
#  define Character __DEF_Character
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
   186
# endif
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   187
14644
e5f19ef20dcd mingw changes
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   188
# define INT    STX_INT
e5f19ef20dcd mingw changes
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   189
# define UINT   STX_UINT
e5f19ef20dcd mingw changes
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   190
# ifndef off_t
e5f19ef20dcd mingw changes
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   191
#  define off_t  long
e5f19ef20dcd mingw changes
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   192
# endif
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   193
#endif /* WIN32 */
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   194
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   195
#ifndef NO_STDIO /* use STDIO */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   196
# define STDIO_NEEDS_FSEEK
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   197
# define FILEPOINTER            FILE *
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   198
# define READ(f, cp, n)         read(f, cp, n)
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   199
# define WRITE(f, cp, n)        write(f, cp, n)
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   200
# define FFLUSH(fp)             fflush(fp)
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   201
# define FILENO(f)              fileno(f)
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   202
#endif /* use STDIO */
3681
55dc17d40aab fixed nextAvail
Claus Gittinger <cg@exept.de>
parents: 3674
diff changeset
   203
3682
Claus Gittinger <cg@exept.de>
parents: 3681
diff changeset
   204
#ifdef DEBUGGING
4092
fee552eb3418 removed win32 stuff
Claus Gittinger <cg@exept.de>
parents: 3980
diff changeset
   205
  extern char *__survStartPtr, *__survEndPtr;
3682
Claus Gittinger <cg@exept.de>
parents: 3681
diff changeset
   206
# define DEBUGBUFFER(buf)  \
3681
55dc17d40aab fixed nextAvail
Claus Gittinger <cg@exept.de>
parents: 3674
diff changeset
   207
    if (((char *)(buf) >= __survStartPtr) \
55dc17d40aab fixed nextAvail
Claus Gittinger <cg@exept.de>
parents: 3674
diff changeset
   208
     && ((char *)(buf) < __survEndPtr)) { \
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
   209
	__fatal0("read into survivor\n"); \
3681
55dc17d40aab fixed nextAvail
Claus Gittinger <cg@exept.de>
parents: 3674
diff changeset
   210
    }
55dc17d40aab fixed nextAvail
Claus Gittinger <cg@exept.de>
parents: 3674
diff changeset
   211
3682
Claus Gittinger <cg@exept.de>
parents: 3681
diff changeset
   212
#else
Claus Gittinger <cg@exept.de>
parents: 3681
diff changeset
   213
# define DEBUGBUFFER(buf) /* nothing */
Claus Gittinger <cg@exept.de>
parents: 3681
diff changeset
   214
#endif
Claus Gittinger <cg@exept.de>
parents: 3681
diff changeset
   215
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   216
/*
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   217
 * stdio library requires an fseek before reading whenever a file
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   218
 * is open for read/write and the last operation was a write.
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   219
 * (also vice-versa).
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   220
 * All code should use the following macro before doing reads:
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   221
 */
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
   222
#ifdef STDIO_NEEDS_FSEEK
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   223
# define OPT_FSEEK(f, pos, whence)      fseek(f, pos, whence)
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
   224
#else
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   225
# define OPT_FSEEK(f, pos, whence)      /* nothing */
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
   226
#endif
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
   227
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   228
#ifdef WIN32
16912
2391fcb8b4a4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16909
diff changeset
   229
// Win returns from ReadFile() with false and _threadErrno == 0 on end of pipe.
2391fcb8b4a4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16909
diff changeset
   230
// We don't know why
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
   231
#  define READ(ret, f, cp, n, handleType) { \
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
   232
	if (handleType == @symbol(socketHandle)) { \
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
   233
	  (ret) = __STX_WSA_NOINT_CALL4("recv", recv, (f), (cp), (n), 0); \
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
   234
	} else { \
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
   235
	  HANDLE h = _get_osfhandle(fileno(f)); \
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
   236
	  if (handleType == @symbol(socketFilePointer)) { \
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
   237
	    (ret) = __STX_WSA_NOINT_CALL4("recv", recv, h, (cp), (n), 0);\
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
   238
	  } else { \
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
   239
	    int __res; \
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
   240
	    (ret) = __STX_API_NOINT_CALL5("ReadFile", ReadFile, h, (cp), (n), &__res, 0);\
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
   241
	    (ret) = (ret) ? __res : ((__threadErrno == EPIPE || __threadErrno == 0) ? 0 : -1); \
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
   242
	  } \
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
   243
	} \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   244
      }
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
   245
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
   246
#  define WRITE(ret, f, cp, n, handleType) { \
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
   247
	if (handleType == @symbol(socketHandle)) { \
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
   248
	  (ret) = __STX_WSA_NOINT_CALL4("send", send, (f), (cp), (n), 0); \
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
   249
	} else {\
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
   250
	  HANDLE h = _get_osfhandle(fileno(f)); \
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
   251
	  if (handleType == @symbol(socketFilePointer)) { \
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
   252
	    (ret) = __STX_WSA_NOINT_CALL4("send", send, h, (cp), (n), 0);\
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
   253
	  } else {\
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
   254
	    int __res; \
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
   255
	    (ret) = __STX_API_NOINT_CALL5("WriteFile", WriteFile, h, (cp), (n), &__res, 0);\
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
   256
	    (ret) = (ret) ? __res : -1; \
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
   257
	  } \
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
   258
	} \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   259
      }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   260
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   261
# define FFLUSH(fp)             fflush(fp)
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   262
# undef STDIO_NEEDS_FSEEK
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   263
# define FILEPOINTER            FILE *
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   264
# define FILENO(f)              fileno(f)
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   265
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   266
# define __READING__(f)                          \
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
   267
    if ((__INST(didWrite) != false)              \
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
   268
     && (__INST(mode) == @symbol(readwrite))) {  \
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
   269
	__INST(didWrite) = false;                \
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
   270
	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   271
    }
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   272
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   273
# define __WRITING__(f)                          \
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
   274
    if ((__INST(didWrite) != true)               \
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
   275
     && (__INST(mode) == @symbol(readwrite))) {  \
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
   276
	__INST(didWrite) = true;                 \
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
   277
	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   278
    }
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
   279
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   280
# define __UNGETC__(c, f, isBuffered)                   \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   281
    if (isBuffered) {                                   \
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
   282
	ungetc((c), (f));                               \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   283
    } else {                                            \
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
   284
      __INST(readAhead) = __mkSmallInteger((c));        \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   285
    }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   286
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
   287
# define __READBYTE__(ret, f, buf, isBuffered, handleType) \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   288
    if (isBuffered) {                                   \
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
   289
	for (;;) {                                      \
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
   290
	    CLEAR_ERRNO;                                \
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
   291
	    (ret) = getc(f);                            \
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
   292
	    if ((ret) >= 0) {                           \
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
   293
		*(buf) = (ret);                         \
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
   294
		(ret) = 1;                              \
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
   295
	    } else if (ferror(f)) {                     \
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
   296
		if (__threadErrno == EINTR) {           \
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
   297
		    clearerr(f);                        \
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
   298
		    continue;                           \
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
   299
		}                                       \
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
   300
	    } else {                                    \
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
   301
		(ret) = 0;                              \
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
   302
	    }                                           \
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
   303
	    break;                                      \
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
   304
	}                                               \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   305
    } else {                                            \
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
   306
	OBJ rA = __INST(readAhead);                     \
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
   307
	if (rA != nil) {                                \
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
   308
	    *(buf) = (char)__intVal(rA);                \
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
   309
	    __INST(readAhead) = nil;                    \
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
   310
	    (ret) = 1;                                  \
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
   311
	} else {                                        \
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
   312
	    for (;;) {                                  \
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
   313
		CLEAR_ERRNO;                            \
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
   314
		READ((ret), f, buf, 1, handleType);       \
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
   315
		if ((ret) >= 0 || __threadErrno != EINTR) \
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
   316
		    break;                              \
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
   317
	    }                                           \
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
   318
	}                                               \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   319
    }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   320
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   321
  /*
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   322
   * read_bytes into a c-buffer
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   323
   * (which may NOT move)
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   324
   */
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   325
# define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   326
    (ret) = 0;                                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   327
    if (isBuffered) {                                   \
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
   328
	int __offs = 0;                                 \
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
   329
	while (__offs < (cnt)) {                        \
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
   330
	    CLEAR_ERRNO;                                \
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
   331
	    (ret) = getc(f);                            \
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
   332
	    if ((ret) < 0) {                            \
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
   333
		if (ferror(f)) {                        \
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
   334
		    if (__threadErrno == EINTR) {       \
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
   335
			clearerr(f);                    \
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
   336
			continue;                       \
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
   337
		    }                                   \
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
   338
		} else {                                \
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
   339
		    (ret) = 0;                          \
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
   340
		}                                       \
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
   341
		break;                                  \
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
   342
	    }                                           \
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
   343
	    (buf)[__offs++] = (ret);                    \
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
   344
	}                                               \
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
   345
	if (__offs > 0)                                 \
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
   346
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   347
    } else {                                            \
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
   348
	int __offs = 0;                                 \
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
   349
							\
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
   350
	while (__offs < (cnt)) {                        \
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
   351
	    OBJ rA = __INST(readAhead);                 \
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
   352
	    if (rA != nil) {                            \
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
   353
		(buf)[__offs] = __intVal(rA);           \
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
   354
		__INST(readAhead) = nil;                \
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
   355
		(ret) = 1;                              \
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
   356
	    } else {                                    \
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
   357
		CLEAR_ERRNO;                            \
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
   358
		READ((ret), f, (buf)+__offs, (cnt)-__offs, handleType); \
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
   359
		if ((ret) <= 0) {                       \
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
   360
		    if ((ret) < 0 && __threadErrno == EINTR) {  \
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
   361
			continue;                       \
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
   362
		    }                                   \
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
   363
		    break;                              \
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
   364
		}                                       \
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
   365
	    }                                           \
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
   366
	    __offs += (ret);                            \
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
   367
	}                                               \
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
   368
	if (__offs > 0)                                 \
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
   369
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   370
   }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   371
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   372
# define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   373
  {                                                     \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   374
    int __offs = 0;                                     \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   375
    int oldFlags;                                       \
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
   376
							\
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   377
    (ret) = 0;                                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   378
    if (isBuffered) {                                   \
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
   379
	while (__offs < (cnt)) {                        \
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
   380
	    CLEAR_ERRNO;                                \
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
   381
	    (ret) = getc(f);                            \
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
   382
	    if ((ret) < 0) {                            \
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
   383
		if (ferror(f)) {                        \
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
   384
		    if (__threadErrno == EINTR) {       \
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
   385
			clearerr(f);                    \
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
   386
			continue;                       \
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
   387
		    }                                   \
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
   388
		} else {                                \
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
   389
		    (ret) = 0;                          \
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
   390
		}                                       \
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
   391
		break;                                  \
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
   392
	    }                                           \
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
   393
	    (buf)[__offs++] = (ret);                    \
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
   394
	}                                               \
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
   395
	(ret) = __offs;                                 \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   396
    } else {                                            \
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
   397
	while (__offs < (cnt)) {                        \
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
   398
	    OBJ rA = __INST(readAhead);                 \
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
   399
	    if (rA != nil) {                            \
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
   400
		(buf)[__offs] = __intVal(rA);           \
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
   401
		__INST(readAhead) = nil;                \
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
   402
		(ret) = 1;                              \
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
   403
		__offs ++;                              \
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
   404
		continue;                               \
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
   405
	    }                                           \
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
   406
	    CLEAR_ERRNO;                                \
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
   407
	    {                                           \
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
   408
	      int res = 0;                              \
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
   409
	      if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res), 1)) \
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
   410
		  || (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \
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
   411
		  || (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0), 1))) { \
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
   412
		  if (res > 0) {                        \
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
   413
		      if (res > ((cnt)-__offs))         \
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
   414
			  res = (cnt)-__offs;           \
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
   415
		      READ((ret), f, (buf)+__offs, res, handleType); \
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
   416
		  } else {                              \
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
   417
		      (ret) = 0;                        \
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
   418
		      break;                            \
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
   419
		  }                                     \
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
   420
	      } else {                                  \
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
   421
		  READ((ret), f, (buf)+__offs, (cnt)-__offs, handleType); \
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
   422
	      }                                         \
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
   423
	    }                                           \
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
   424
	    if ((ret) <= 0) {                           \
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
   425
		if (ret < 0 && __threadErrno == EINTR)  \
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
   426
		    continue;                           \
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
   427
		break;                                  \
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
   428
	    }                                           \
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
   429
	    __offs += (ret);                            \
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
   430
	}                                               \
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
   431
	(ret) = __offs;                                 \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   432
    }                                                   \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   433
  }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   434
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   435
# define IO_BUFFER_SIZE        (8*1024)
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   436
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   437
# define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   438
  {                                                     \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   439
    int __ooffs = obj_offs;                             \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   440
    int __offs = 0;                                     \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   441
    char *buf = (char *)(obj);                          \
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
   442
							\
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   443
    (ret) = 0;                                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   444
    if (isBuffered) {                                   \
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
   445
	while (__offs < (cnt)) {                        \
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
   446
	    CLEAR_ERRNO;                                \
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
   447
	    (ret) = getc(f);                            \
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
   448
	    if ((ret) < 0) {                            \
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
   449
		if (ferror(f)) {                        \
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
   450
		    if (__threadErrno == EINTR) {       \
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
   451
			clearerr(f);                    \
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
   452
			/* refetch */                   \
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
   453
			buf = (char *)(obj);   \
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
   454
			continue;                       \
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
   455
		    }                                   \
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
   456
		} else {                                \
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
   457
		    (ret) = 0;                          \
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
   458
		}                                       \
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
   459
		break;                                  \
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
   460
	    }                                           \
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
   461
	    (buf)[__ooffs+__offs] = (ret);              \
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
   462
	    __offs++;                                   \
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
   463
	}                                               \
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
   464
	if (__offs > 0)                                 \
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
   465
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   466
    } else {                                            \
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
   467
	while (__offs < (cnt)) {                        \
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
   468
	    char __buf[IO_BUFFER_SIZE];                 \
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
   469
	    OBJ rA = __INST(readAhead);                 \
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
   470
	    if (rA != nil) {                            \
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
   471
		(buf)[__ooffs+__offs] = __intVal(rA);   \
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
   472
		__INST(readAhead) = nil;                \
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
   473
		(ret) = 1;                              \
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
   474
	    } else {                                    \
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
   475
		int l;                                  \
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
   476
		CLEAR_ERRNO;                            \
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
   477
		l = (cnt)-__offs;                       \
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
   478
		if ( l > IO_BUFFER_SIZE)                \
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
   479
		  l = IO_BUFFER_SIZE;                   \
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
   480
		READ((ret),f, __buf, l, handleType);                  \
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
   481
		if ((ret) <= 0) {                       \
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
   482
		    if ((ret) < 0 && __threadErrno == EINTR) {  \
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
   483
			continue;                       \
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
   484
		    }                                   \
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
   485
		    break;                              \
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
   486
		}                                       \
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
   487
	    }                                           \
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
   488
	    if ((ret) > 0 ) {                           \
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
   489
		/* refetch */                               \
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
   490
		buf = (char *)(obj);               \
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
   491
		memcpy((buf)+__ooffs+__offs,__buf,(ret));   \
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
   492
		__offs += (ret);                            \
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
   493
	    } else {                                        \
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
   494
		(ret) = 0;                                  \
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
   495
	    }                                               \
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
   496
	}                                               \
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
   497
	if (__offs > 0)                                 \
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
   498
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   499
    }                                                   \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   500
  }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   501
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
   502
# define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   503
  {                                                  \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   504
    int __ooffs = obj_offs;                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   505
    int __offs = 0;                                  \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   506
    char *buf = (char *)(obj);                       \
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
   507
						     \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   508
    (ret) = 0;                                       \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   509
    if (isBuffered) {                                \
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
   510
	while (__offs < (cnt)) {                     \
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
   511
	    CLEAR_ERRNO;                             \
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
   512
	    (ret) = getc(f);                         \
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
   513
	    if ((ret) < 0) {                         \
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
   514
		if (ferror(f)) {                     \
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
   515
		    if (__threadErrno == EINTR) {    \
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
   516
			clearerr(f);                 \
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
   517
			/* refetch */                \
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
   518
			buf = (char *)(obj);\
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
   519
			continue;                    \
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
   520
		    }                                \
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
   521
		} else {                             \
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
   522
		    (ret) = 0;                       \
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
   523
		}                                    \
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
   524
		break;                               \
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
   525
	    }                                        \
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
   526
	    (buf)[__ooffs+__offs] = (ret);           \
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
   527
	    __offs++;                                \
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
   528
	}                                            \
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
   529
	if (__offs > 0)                              \
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
   530
	    (ret) = __offs;                          \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   531
    } else {                                         \
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
   532
	while (__offs < (cnt)) {                     \
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
   533
	    char __buf[IO_BUFFER_SIZE];              \
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
   534
	    OBJ rA = __INST(readAhead);              \
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
   535
	    if (rA != nil) {                         \
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
   536
		(buf)[__ooffs+__offs] = __intVal(rA);\
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
   537
		__INST(readAhead) = nil;             \
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
   538
		(ret) = 1;                           \
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
   539
		__offs++;                            \
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
   540
		continue;                            \
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
   541
	    }                                        \
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
   542
	    {                                        \
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
   543
		int res = 0;                         \
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
   544
		int l = (cnt)-__offs;                \
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
   545
		CLEAR_ERRNO;                         \
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
   546
		if (l > IO_BUFFER_SIZE)              \
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
   547
		    l = IO_BUFFER_SIZE;              \
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
   548
		if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)), FIONREAD, &res), 1)) \
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
   549
		    || (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \
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
   550
		    || (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0,0,0,&res,0), 1))) { \
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
   551
		    if (res > 0) {                   \
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
   552
			if (res > l) res = l;        \
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
   553
			READ((ret), f, __buf, res, handleType); \
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
   554
		    } else {                         \
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
   555
			(ret) = 0;                   \
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
   556
			break;                       \
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
   557
		    }                                \
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
   558
		} else {                             \
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
   559
		    READ((ret), f, __buf, l, handleType); \
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
   560
		}                                     \
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
   561
		if ((ret) <= 0) {                     \
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
   562
		    if (ret < 0 && __threadErrno == EINTR) \
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
   563
			continue;                       \
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
   564
		    break;                              \
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
   565
		}                                       \
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
   566
	    }                                           \
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
   567
	    if ((ret) > 0) {                            \
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
   568
		buf = (char *)(obj);                    \
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
   569
		memcpy((buf)+__ooffs+__offs, __buf, (ret)); \
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
   570
		__offs += (ret);                        \
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
   571
	    } else {                                    \
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
   572
		(ret) = 0;                              \
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
   573
	    }                                           \
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
   574
	}                                               \
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
   575
	if (__offs > 0)                                 \
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
   576
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   577
    }                                                   \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   578
  }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   579
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   580
# define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)         \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   581
    if (isBuffered) {                                   \
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
   582
	for (;;) {                                      \
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
   583
	    CLEAR_ERRNO;                                \
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
   584
	    ret = putc(*(buf), f);                      \
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
   585
	    if ((ret) >= 0) {                           \
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
   586
		(ret) = 1;                              \
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
   587
	    } else if (ferror(f)) {                     \
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
   588
		if (__threadErrno == EINTR) {                   \
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
   589
		    clearerr(f);                        \
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
   590
		    continue;                           \
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
   591
		}                                       \
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
   592
	    } else                                      \
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
   593
		(ret) = 0;                              \
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
   594
	    break;                                      \
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
   595
	}                                               \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   596
    } else {                                            \
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
   597
	for (;;) {                                      \
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
   598
	    CLEAR_ERRNO;                                \
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
   599
	    WRITE(ret,f, buf, 1, handleType);                       \
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
   600
	    if ((ret) >= 0 || __threadErrno != EINTR)           \
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
   601
		break;                                  \
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
   602
	}                                               \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   603
   }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   604
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   605
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)   \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   606
    (ret) = 0;                                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   607
    if (isBuffered) {                                   \
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
   608
	int __offs = 0;                                 \
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
   609
	while (__offs < (cnt)) {                        \
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
   610
	    CLEAR_ERRNO;                                \
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
   611
	    (ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
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
   612
	    if ((ret) <= 0) {                           \
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
   613
		if (ferror(f)) {                        \
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
   614
		    if (__threadErrno == EINTR) {               \
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
   615
			clearerr(f);                    \
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
   616
			continue;                       \
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
   617
		    }                                   \
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
   618
		    break;                              \
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
   619
		} else {                                \
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
   620
		    (ret) = 0;                          \
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
   621
		}                                       \
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
   622
	    }                                           \
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
   623
	    __offs += (ret);                            \
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
   624
	}                                               \
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
   625
	if (__offs > 0)                                 \
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
   626
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   627
    } else {                                            \
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
   628
	int __offs = 0;                                 \
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
   629
	while (__offs < (cnt)) {                        \
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
   630
	    CLEAR_ERRNO;                                \
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
   631
	    WRITE(ret,f, (buf)+__offs, (cnt)-__offs, handleType);   \
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
   632
	    if (ret <= 0) {                             \
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
   633
		if (ret < 0 && __threadErrno == EINTR) { \
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
   634
		    continue;                           \
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
   635
		}                                       \
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
   636
		break;                                  \
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
   637
	    }                                           \
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
   638
	    __offs += (ret);                            \
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
   639
	}                                               \
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
   640
	if (__offs > 0)                                 \
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
   641
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   642
   }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   643
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   644
# define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   645
  {                                                     \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   646
    int __ooffs = obj_offs;                             \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   647
    int __offs = 0;                                     \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   648
    char *buf = (char *)(obj);                          \
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
   649
							\
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   650
    (ret) = 0;                                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   651
    if (isBuffered) {                                   \
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
   652
	while (__offs < (cnt)) {                        \
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
   653
	    CLEAR_ERRNO;                                \
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
   654
	    ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \
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
   655
	    if ((ret) <= 0) {                           \
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
   656
		if (ferror(f)) {                        \
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
   657
		    if (__threadErrno == EINTR) {       \
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
   658
			/* refetch */                   \
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
   659
			buf = (char *)(obj);   \
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
   660
			clearerr(f);                    \
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
   661
			continue;                       \
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
   662
		    }                                   \
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
   663
		    break;                              \
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
   664
		} else {                                \
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
   665
		    (ret) = 0;                          \
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
   666
		}                                       \
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
   667
	    }                                           \
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
   668
	    __offs += (ret);                            \
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
   669
	}                                               \
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
   670
	if (__offs > 0)                                 \
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
   671
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   672
    } else {                                            \
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
   673
	while (__offs < (cnt)) {                        \
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
   674
	    char __buf[IO_BUFFER_SIZE];                 \
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
   675
	    int l;                                      \
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
   676
	    CLEAR_ERRNO;                                \
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
   677
	    l = (cnt)-__offs;                           \
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
   678
	    if ( l > IO_BUFFER_SIZE)                    \
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
   679
	      l = IO_BUFFER_SIZE;                       \
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
   680
	    /* refetch */                               \
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
   681
	    buf = (char *)(obj);               \
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
   682
	    memcpy(__buf,(buf)+__ooffs+__offs,l);       \
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
   683
	    WRITE(ret,f, __buf, l, handleType);                     \
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
   684
	    if (ret <= 0) {                             \
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
   685
		if (ret < 0 && __threadErrno == EINTR) {        \
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
   686
		    continue;                           \
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
   687
		}                                       \
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
   688
		break;                                  \
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
   689
	    }                                           \
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
   690
	    __offs += (ret);                            \
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
   691
	}                                               \
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
   692
	if (__offs > 0)                                 \
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
   693
	    (ret) = __offs;                             \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   694
    }                                                   \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   695
  }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   696
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
   697
#else /* ! WIN32 */
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
   698
/* ========================   UNIX / LINUX ====================================================== */
16373
9b9827530bf4 Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents: 16368
diff changeset
   699
typedef int SOCKET;
9b9827530bf4 Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents: 16368
diff changeset
   700
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   701
# define __READING__(f)                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   702
    if ((__INST(didWrite) != false)              \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   703
     && (__INST(mode) == @symbol(readwrite))) {  \
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
   704
	__INST(didWrite) = false;                \
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
   705
	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   706
    }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   707
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   708
# define __WRITING__(f)                          \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   709
    if ((__INST(didWrite) != true)               \
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   710
     && (__INST(mode) == @symbol(readwrite))) {  \
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
   711
	__INST(didWrite) = true;                 \
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
   712
	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   713
    }
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   714
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   715
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   716
# ifdef NO_STDIO
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   717
#  define __UNGETC__(c, f, isBuffered)                  \
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   718
    __INST(readAhead) = __mkSmallInteger((c));
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   719
# else /* use STDIO */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   720
#  define __UNGETC__(c, f, isBuffered)                  \
2264
ac5dfc03aa26 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   721
    if (isBuffered) {                                   \
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
   722
	ungetc((c), (f));                               \
2264
ac5dfc03aa26 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   723
    } else {                                            \
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
   724
	__INST(readAhead) = __mkSmallInteger((c));          \
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   725
    }
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   726
# endif /* use STDIO */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   727
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   728
# ifdef NO_STDIO
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   729
#  define __READBYTE__(ret, f, buf, isBuffered, handleType)         \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   730
    {                                                   \
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
   731
	OBJ rA = __INST(readAhead);                     \
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
   732
	if (rA != nil) {                                \
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
   733
	    *(buf) = __intVal(rA);                      \
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
   734
	    DEBUGBUFFER(buf);                           \
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
   735
	    __INST(readAhead) = nil;                    \
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
   736
	    (ret) = 1;                                  \
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
   737
	} else {                                        \
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
   738
	    for (;;) {                                  \
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
   739
		CLEAR_ERRNO;                            \
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
   740
		(ret) = READ(f, buf, 1, handleType);    \
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
   741
		DEBUGBUFFER(buf);                       \
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
   742
		if ((ret) >= 0) break;                  \
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
   743
		if (errno != EINTR) {                   \
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
   744
		    break;                              \
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
   745
		}                                       \
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
   746
		__HANDLE_INTERRUPTS__;                  \
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
   747
	    }                                           \
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
   748
	}                                               \
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
   749
    }
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   750
# else /* use STDIO */
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   751
#  define __READBYTE__(ret, f, buf, isBuffered, handleType)         \
2264
ac5dfc03aa26 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   752
    if (isBuffered) {                                   \
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
   753
	for (;;) {                                      \
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
   754
	    CLEAR_ERRNO;                                \
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
   755
	    (ret) = getc(f);                            \
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
   756
	    if ((ret) >= 0) {                           \
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
   757
		DEBUGBUFFER(buf);                       \
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
   758
		*(buf) = (ret);                         \
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
   759
		(ret) = 1;                              \
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
   760
	    } else if (ferror(f)) {                     \
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
   761
		if (errno == EINTR) {                   \
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
   762
		    __HANDLE_INTERRUPTS__;              \
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
   763
		    clearerr(f);                        \
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
   764
		    continue;                           \
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
   765
		}                                       \
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
   766
	    } else                                      \
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
   767
		(ret) = 0;                              \
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
   768
	    break;                                      \
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
   769
	}                                               \
2264
ac5dfc03aa26 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   770
    } else {                                            \
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
   771
	OBJ rA = __INST(readAhead);                     \
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
   772
	if (rA != nil) {                                \
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
   773
	    *(buf) = __intVal(rA);                      \
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
   774
	    DEBUGBUFFER(buf);                           \
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
   775
	    __INST(readAhead) = nil;                    \
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
   776
	    (ret) = 1;                                  \
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
   777
	} else {                                        \
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
   778
	    for (;;) {                                  \
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
   779
		CLEAR_ERRNO;                            \
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
   780
		(ret) = read(fileno(f), buf, 1);        \
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
   781
		DEBUGBUFFER(buf);                       \
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
   782
		if ((ret) >= 0) break;                  \
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
   783
		if (errno != EINTR) {                   \
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
   784
		    break;                              \
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
   785
		}                                       \
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
   786
		__HANDLE_INTERRUPTS__;                  \
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
   787
	    }                                           \
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
   788
	}                                               \
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   789
   }
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   790
# endif /* use STDIO */
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
   791
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   792
/*
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   793
 * read_bytes into a c-buffer
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   794
 * (which may NOT move)
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   795
 */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   796
# ifdef NO_STDIO
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   797
#  define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)   \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   798
    {                                                   \
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
   799
	int __offs = 0, __cnt;                          \
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
   800
							\
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
   801
	while (__offs < (cnt)) {                        \
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
   802
	    OBJ rA = __INST(readAhead);                 \
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
   803
	    if (rA != nil) {                            \
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
   804
		(buf)[__offs] = __intVal(rA);           \
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
   805
		DEBUGBUFFER(buf);                       \
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
   806
		__INST(readAhead) = nil;                \
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
   807
		__offs++;                               \
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
   808
	    } else {                                    \
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
   809
		CLEAR_ERRNO;                            \
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
   810
		__cnt = READ(f, (buf)+__offs, (cnt)-__offs, handleType); \
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
   811
		DEBUGBUFFER(buf);                       \
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
   812
		if (__cnt <= 0) {                       \
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
   813
		    if (__cnt < 0 && errno == EINTR) {  \
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
   814
			__HANDLE_INTERRUPTS__;          \
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
   815
			continue;                       \
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
   816
		    }                                   \
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
   817
		    break;                              \
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
   818
		}                                       \
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
   819
		__offs += __cnt;                        \
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
   820
	    }                                           \
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
   821
	}                                               \
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
   822
	if (__offs > 0)                                 \
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
   823
	    (ret) = __offs;                             \
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
   824
   }
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   825
# else /* use STDIO */
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   826
#  define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)     \
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   827
    (ret) = 0;                                          \
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   828
    if (isBuffered) {                                   \
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
   829
	int __offs = 0;                                 \
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
   830
	while (__offs < (cnt)) {                        \
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
   831
	    CLEAR_ERRNO;                                \
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
   832
	    (ret) = getc(f);                            \
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
   833
	    if ((ret) < 0) {                            \
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
   834
		if (ferror(f)) {                        \
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
   835
		    if (errno == EINTR) {               \
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
   836
			__HANDLE_INTERRUPTS__;          \
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
   837
			clearerr(f);                    \
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
   838
			continue;                       \
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
   839
		    }                                   \
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
   840
		} else {                                \
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
   841
		    (ret) = 0;                          \
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
   842
		}                                       \
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
   843
		break;                                  \
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
   844
	    }                                           \
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
   845
	    DEBUGBUFFER(buf);                           \
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
   846
	    (buf)[__offs++] = (ret);                    \
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
   847
	}                                               \
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
   848
	if (__offs > 0)                                 \
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
   849
	    (ret) = __offs;                             \
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   850
    } else {                                            \
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
   851
	int __offs = 0, __cnt;                          \
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
   852
	int fd = fileno(f);                             \
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
   853
							\
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
   854
	while (__offs < (cnt)) {                        \
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
   855
	    OBJ rA = __INST(readAhead);                 \
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
   856
	    if (rA != nil) {                            \
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
   857
		DEBUGBUFFER(buf);                       \
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
   858
		(buf)[__offs] = __intVal(rA);           \
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
   859
		__INST(readAhead) = nil;                \
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
   860
		__offs++;                               \
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
   861
	    } else {                                    \
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
   862
		CLEAR_ERRNO;                            \
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
   863
		__cnt = read(fd, (buf)+__offs, (cnt)-__offs);  \
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
   864
		DEBUGBUFFER(buf);                       \
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
   865
		if (__cnt <= 0) {                       \
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
   866
		    if (__cnt < 0 && errno == EINTR) {  \
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
   867
			__HANDLE_INTERRUPTS__;          \
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
   868
			continue;                       \
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
   869
		    }                                   \
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
   870
		    break;                              \
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
   871
		}                                       \
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
   872
		__offs += __cnt;                        \
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
   873
	    }                                           \
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
   874
	}                                               \
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
   875
	if (__offs > 0)                                 \
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
   876
	    (ret) = __offs;                             \
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
   877
   }
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   878
6563
f786651428c8 Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   879
f786651428c8 Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   880
/*
f786651428c8 Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   881
 * FNDELAY and O_NDELAY is deprecated, O_NONBLOCK is used in POSIX, XPG, etc...
f786651428c8 Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   882
 */
f786651428c8 Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   883
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   884
#  if defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(O_NDELAY) || defined(FNDELAY))
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   885
#   define SETFLAGS(fd, flags) \
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
   886
	fcntl(fd, F_SETFL, flags)
6563
f786651428c8 Support O_NONBLOCK which is the POSIX way of FNDELAY
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   887
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   888
#   if defined(O_NONBLOCK)
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   889
#    define __STX_NONBLOCK_FLAG O_NONBLOCK
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   890
#   else
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   891
#    if defined(O_NDELAY)
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   892
#     define __STX_NONBLOCK_FLAG O_NDELAY
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   893
#    else
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   894
#     define __STX_NONBLOCK_FLAG FNDELAY
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   895
#    endif
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   896
#   endif
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   897
15342
d89884677c76 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15304
diff changeset
   898
#   define SETNONBLOCKING(fd, oldFlags) \
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
   899
	{ \
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
   900
	    int flags = fcntl(fd, F_GETFL, 0); \
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
   901
	    if (flags >= 0) { \
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
   902
		fcntl(fd, F_SETFL, flags | __STX_NONBLOCK_FLAG); \
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
   903
	    } \
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
   904
	    oldFlags = flags; \
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
   905
	}
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   906
#  else
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   907
#   define SETFLAGS(fd, flags) /* nothing */
15342
d89884677c76 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15304
diff changeset
   908
#   define SETNONBLOCKING(fd, oldFlags) /* nothing */
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   909
#  endif
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   910
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   911
#  define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   912
  {                                                     \
15342
d89884677c76 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15304
diff changeset
   913
    int __offs = 0, __cnt;                              \
3681
55dc17d40aab fixed nextAvail
Claus Gittinger <cg@exept.de>
parents: 3674
diff changeset
   914
    int oldFlags;                                       \
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
   915
							\
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   916
    (ret) = 0;                                          \
15342
d89884677c76 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15304
diff changeset
   917
    SETNONBLOCKING(fileno(f), oldFlags);                \
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   918
    if (isBuffered) {                                   \
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
   919
	while (__offs < (cnt)) {                        \
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
   920
	    CLEAR_ERRNO;                                \
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
   921
	    (ret) = getc(f);                            \
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
   922
	    if ((ret) < 0) {                            \
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
   923
		if (ferror(f)) {                        \
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
   924
		    if (errno == EINTR) {               \
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
   925
			(ret) = 0;                      \
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
   926
			clearerr(f);                    \
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
   927
			break;                          \
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
   928
		    }                                   \
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
   929
		} else {                                \
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
   930
		    (ret) = 0;                          \
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
   931
		}                                       \
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
   932
		break;                                  \
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
   933
	    }                                           \
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
   934
	    (buf)[__offs++] = (ret);                    \
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
   935
	    DEBUGBUFFER(buf);                           \
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
   936
	}                                               \
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
   937
	if (__offs > 0)                                 \
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
   938
	    (ret) = __offs;                             \
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   939
    } else {                                            \
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
   940
	int fd = fileno(f);                             \
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
   941
							\
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
   942
	while (__offs < (cnt)) {                        \
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
   943
	    OBJ rA = __INST(readAhead);                 \
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
   944
	    if (rA != nil) {                            \
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
   945
		(buf)[__offs] = __intVal(rA);           \
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
   946
		DEBUGBUFFER(buf);                       \
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
   947
		__INST(readAhead) = nil;                \
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
   948
		__offs++;                               \
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
   949
		continue;                               \
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
   950
	    }                                           \
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
   951
	    CLEAR_ERRNO;                                \
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
   952
	    __cnt = read(fd, (buf)+__offs, (cnt)-__offs); \
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
   953
	    DEBUGBUFFER(buf);                           \
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
   954
	    if (__cnt > 0) {                            \
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
   955
		__offs += __cnt;                        \
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
   956
	    }                                           \
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
   957
	    break;                                      \
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
   958
	}                                               \
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
   959
	if (__offs > 0)                                 \
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
   960
	    (ret) = __offs;                             \
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   961
    }                                                   \
3681
55dc17d40aab fixed nextAvail
Claus Gittinger <cg@exept.de>
parents: 3674
diff changeset
   962
    SETFLAGS(fileno(f), oldFlags);                      \
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   963
  }
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
   964
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
   965
# endif /* use STDIO */
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
   966
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   967
/*
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   968
 * read_bytes into an object
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   969
 * (which may be moved by GC)
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
   970
 */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
   971
# ifdef NO_STDIO
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
   972
#  define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)     \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   973
  {                                                     \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   974
    int __ooffs = obj_offs;                             \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   975
    int __offs = 0;                                     \
7540
7cd542f29024 Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents: 7503
diff changeset
   976
    int __cnt;                                          \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   977
    char *buf = (char *)(obj);                          \
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
   978
							\
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
   979
    (ret) = 0;                                          \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
   980
    {                                                   \
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
   981
	while (__offs < (cnt)) {                        \
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
   982
	    OBJ rA = __INST(readAhead);                 \
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
   983
	    if (rA != nil) {                            \
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
   984
		(buf)[__ooffs+__offs] = __intVal(rA);   \
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
   985
		DEBUGBUFFER(buf);                       \
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
   986
		__INST(readAhead) = nil;                \
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
   987
		__offs++;                               \
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
   988
	    } else {                                    \
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
   989
		CLEAR_ERRNO;                            \
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
   990
		__cnt = READ(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \
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
   991
		DEBUGBUFFER(buf);                       \
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
   992
		if (__cnt <= 0) {                       \
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
   993
		    if (__cnt < 0 && errno == EINTR) {  \
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
   994
			__HANDLE_INTERRUPTS__;          \
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
   995
			/* refetch */                   \
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
   996
			buf = (char *)(obj);            \
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
   997
			continue;                       \
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
   998
		    }                                   \
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
   999
		    break;                              \
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
  1000
		}                                       \
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
  1001
		__offs += __cnt;                        \
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
  1002
	    }                                           \
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
  1003
	}                                               \
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
  1004
	if (__offs > 0)                                 \
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
  1005
	    (ret) = __offs;                             \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1006
    }                                                   \
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  1007
  }
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  1008
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1009
# else /* use STDIO */
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  1010
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1011
#  define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)     \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1012
  {                                                     \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1013
    int __ooffs = obj_offs;                             \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1014
    int __offs = 0;                                     \
7540
7cd542f29024 Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents: 7503
diff changeset
  1015
    int __cnt;                                          \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1016
    char *buf = (char *)(obj);                          \
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
  1017
							\
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1018
    (ret) = 0;                                          \
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1019
    if (isBuffered) {                                   \
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
  1020
	while (__offs < (cnt)) {                        \
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
  1021
	    CLEAR_ERRNO;                                \
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
  1022
	    (ret) = getc(f);                            \
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
  1023
	    if ((ret) < 0) {                            \
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
  1024
		if (ferror(f)) {                        \
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
  1025
		    if (errno == EINTR) {               \
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
  1026
			__HANDLE_INTERRUPTS__;          \
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
  1027
			clearerr(f);                    \
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
  1028
			/* refetch */                   \
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
  1029
			buf = (char *)(obj);            \
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
  1030
			DEBUGBUFFER(buf);               \
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
  1031
			continue;                       \
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
  1032
		    }                                   \
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
  1033
		} else {                                \
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
  1034
		    (ret) = 0;                          \
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
  1035
		}                                       \
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
  1036
		break;                                  \
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
  1037
	    }                                           \
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
  1038
	    (buf)[__ooffs+__offs] = (ret);              \
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
  1039
	    DEBUGBUFFER(buf);                           \
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
  1040
	    __offs++;                                   \
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
  1041
	}                                               \
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
  1042
	if (__offs > 0)                                 \
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
  1043
	    (ret) = __offs;                             \
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1044
    } else {                                            \
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
  1045
	int fd = fileno(f);                             \
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
  1046
							\
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
  1047
	while (__offs < (cnt)) {                        \
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
  1048
	    OBJ rA = __INST(readAhead);                 \
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
  1049
	    if (rA != nil) {                            \
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
  1050
		(buf)[__ooffs+__offs] = __intVal(rA);   \
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
  1051
		DEBUGBUFFER(buf);                       \
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
  1052
		__INST(readAhead) = nil;                \
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
  1053
		__offs++;                               \
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
  1054
	    } else {                                    \
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
  1055
		CLEAR_ERRNO;                            \
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
  1056
		__cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \
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
  1057
		DEBUGBUFFER(buf);                       \
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
  1058
		if (__cnt <= 0) {                       \
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
  1059
		    if (__cnt < 0 && errno == EINTR) {  \
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
  1060
			__HANDLE_INTERRUPTS__;          \
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
  1061
			/* refetch */                   \
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
  1062
			buf = (char *)(obj);            \
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
  1063
			continue;                       \
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
  1064
		    }                                   \
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
  1065
		    break;                              \
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
  1066
		}                                       \
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
  1067
		__offs += __cnt;                        \
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
  1068
	    }                                           \
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
  1069
	}                                               \
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
  1070
	if (__offs > 0)                                 \
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
  1071
	    (ret) = __offs;                             \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1072
    }                                                   \
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1073
  }
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1074
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1075
# define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)     \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1076
  {                                                     \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1077
    int __ooffs = obj_offs;                             \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1078
    int __offs = 0;                                     \
7540
7cd542f29024 Fix reading when readAheadBuffer is filled and following read fails
Stefan Vogel <sv@exept.de>
parents: 7503
diff changeset
  1079
    int __cnt;                                          \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1080
    char *buf = (char *)(obj);                          \
15342
d89884677c76 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15304
diff changeset
  1081
    int oldFlags;                                       \
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
  1082
							\
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1083
    (ret) = 0;                                          \
15342
d89884677c76 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15304
diff changeset
  1084
    SETNONBLOCKING(fileno(f), oldFlags);                \
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
  1085
							\
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1086
    if (isBuffered) {                                   \
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
  1087
	while (__offs < (cnt)) {                        \
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
  1088
	    CLEAR_ERRNO;                                \
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
  1089
	    (ret) = getc(f);                            \
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
  1090
	    if ((ret) < 0) {                            \
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
  1091
		if (ferror(f)) {                        \
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
  1092
		    if (errno == EINTR) {               \
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
  1093
			clearerr(f);                    \
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
  1094
			/* refetch */                   \
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
  1095
			buf = (char *)(obj);            \
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
  1096
			(ret) = 0;                      \
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
  1097
			break;                          \
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
  1098
		    }                                   \
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
  1099
		} else {                                \
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
  1100
		    (ret) = 0;                          \
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
  1101
		}                                       \
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
  1102
		break;                                  \
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
  1103
	    }                                           \
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
  1104
	    (buf)[__ooffs+__offs] = (ret);              \
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
  1105
	    DEBUGBUFFER(buf);                           \
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
  1106
	    __offs++;                                   \
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
  1107
	}                                               \
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
  1108
	if (__offs > 0)                                 \
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
  1109
	    (ret) = __offs;                             \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1110
    } else {                                            \
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
  1111
	int fd = fileno(f);                             \
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
  1112
							\
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
  1113
	while (__offs < (cnt)) {                        \
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
  1114
	    OBJ rA = __INST(readAhead);                 \
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
  1115
	    if (rA != nil) {                            \
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
  1116
		(buf)[__ooffs+__offs] = __intVal(rA);   \
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
  1117
		DEBUGBUFFER(buf);                       \
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
  1118
		__INST(readAhead) = nil;                \
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
  1119
		__offs++;                               \
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
  1120
		continue;                               \
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
  1121
	    }                                           \
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
  1122
	    CLEAR_ERRNO;                                \
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
  1123
	    __cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \
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
  1124
	    DEBUGBUFFER(buf);                           \
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
  1125
	    if (__cnt > 0) {                            \
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
  1126
		__offs += __cnt;                        \
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
  1127
	    }                                           \
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
  1128
	    break;                                      \
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
  1129
	}                                               \
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
  1130
	if (__offs > 0)                                 \
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
  1131
	    (ret) = __offs;                             \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1132
    }                                                   \
15342
d89884677c76 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15304
diff changeset
  1133
    SETFLAGS(fileno(f), oldFlags);                      \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1134
  }
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1135
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1136
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1137
# endif /* use STDIO */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
  1138
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
  1139
# ifdef NO_STDIO
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1140
#  define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)          \
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
  1141
	for (;;) {                                      \
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
  1142
	    CLEAR_ERRNO;                                \
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
  1143
	    (ret) = WRITE(f, buf, 1, handleType);       \
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
  1144
	    if ((ret) >= 0) break;                      \
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
  1145
	    if (errno != EINTR) {                       \
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
  1146
		break;                                  \
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
  1147
	    }                                           \
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
  1148
	    __HANDLE_INTERRUPTS__;                      \
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
  1149
	}
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1150
# else /* use STDIO */
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1151
#  define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)        \
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  1152
    if (isBuffered) {                                   \
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
  1153
	for (;;) {                                      \
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
  1154
	    CLEAR_ERRNO;                                \
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
  1155
	    ret = putc(*(buf), f);                      \
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
  1156
	    if ((ret) >= 0) {                           \
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
  1157
		(ret) = 1;                              \
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
  1158
	    } else if (ferror(f)) {                     \
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
  1159
		/* SOLARIS/SPARC (2.6) generates spurious errors with errno = 0 */ \
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
  1160
		if (errno == EINTR || errno == 0) {     \
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
  1161
		    __HANDLE_INTERRUPTS__;              \
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
  1162
		    clearerr(f);                        \
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
  1163
		    continue;                           \
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
  1164
		}                                       \
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
  1165
	    } else                                      \
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
  1166
		(ret) = 0;                              \
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
  1167
	    break;                                      \
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
  1168
	}                                               \
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  1169
    } else {                                            \
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
  1170
	for (;;) {                                      \
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
  1171
	    CLEAR_ERRNO;                                \
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
  1172
	    (ret) = write(fileno(f), buf, 1);           \
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
  1173
	    if ((ret) >= 0) break;                      \
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
  1174
	    if (errno != EINTR) {                       \
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
  1175
		break;                                  \
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
  1176
	    }                                           \
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
  1177
	    __HANDLE_INTERRUPTS__;                      \
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
  1178
	}                                               \
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1179
   }
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1180
# endif /* use STDIO */
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  1181
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1182
/*
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1183
 * write_bytes from a c-buffer
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1184
 * (which may NOT move)
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1185
 */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
  1186
# ifdef NO_STDIO
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1187
#  define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1188
    (ret) = 0;                                          \
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1189
    {                                                   \
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
  1190
	int __offs = 0;                                 \
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
  1191
	while (__offs < (cnt)) {                        \
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
  1192
	    CLEAR_ERRNO;                                \
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
  1193
	    ret = WRITE(f, (buf)+__offs, (cnt)-__offs, handleType); \
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
  1194
	    if (ret <= 0) {                             \
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
  1195
		if (ret < 0 && errno == EINTR) {        \
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
  1196
		    __HANDLE_INTERRUPTS__;              \
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
  1197
		    continue;                           \
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
  1198
		}                                       \
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
  1199
		break;                                  \
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
  1200
	    }                                           \
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
  1201
	    __offs += (ret);                            \
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
  1202
	}                                               \
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
  1203
	if (__offs > 0)                                 \
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
  1204
	    (ret) = __offs;                             \
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  1205
   }
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1206
# else /* use STDIO */
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1207
#  define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1208
    (ret) = 0;                                          \
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1209
    if (isBuffered) {                                   \
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
  1210
	int __offs = 0;                                 \
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
  1211
	while (__offs < (cnt)) {                        \
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
  1212
	    CLEAR_ERRNO;                                \
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
  1213
	    (ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
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
  1214
	    if ((ret) <= 0) {                            \
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
  1215
		if (ferror(f)) {                        \
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
  1216
		    if (errno == EINTR) {               \
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
  1217
			__HANDLE_INTERRUPTS__;          \
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
  1218
			clearerr(f);                    \
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
  1219
			continue;                       \
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
  1220
		    }                                   \
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
  1221
		} else {                                \
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
  1222
		    (ret) = 0;                          \
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
  1223
		}                                       \
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
  1224
		break;                                  \
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
  1225
	    }                                           \
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
  1226
	    __offs += (ret);                            \
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
  1227
	}                                               \
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
  1228
	if (__offs > 0)                                 \
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
  1229
	    (ret) = __offs;                             \
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1230
    } else {                                            \
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
  1231
	int __offs = 0;                                 \
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
  1232
	while (__offs < (cnt)) {                        \
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
  1233
	    CLEAR_ERRNO;                                \
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
  1234
	    (ret) = write(fileno(f), (buf)+__offs, (cnt)-__offs);\
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
  1235
	    if ((ret) <= 0) {                           \
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
  1236
		if ((ret) < 0) {                        \
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
  1237
		    if (errno == EINTR) {               \
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
  1238
			__HANDLE_INTERRUPTS__;          \
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
  1239
			continue;                       \
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
  1240
		    }                                   \
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
  1241
		}                                       \
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
  1242
		break;                                  \
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
  1243
	    }                                           \
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
  1244
	    __offs += (ret);                            \
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
  1245
	}                                               \
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
  1246
	if (__offs > 0)                                 \
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
  1247
	    (ret) = __offs;                             \
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  1248
   }
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1249
# endif /* use STDIO */
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  1250
223
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
  1251
/*
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1252
 * write_bytes from an object
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1253
 * (which may be moved around by GC)
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1254
 */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
  1255
# ifdef NO_STDIO
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1256
#  define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)            \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1257
  {                                                     \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1258
    int __ooffs = obj_offs;                             \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1259
    int __offs = 0;                                     \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1260
    char *buf = (char *)(obj);                          \
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
  1261
							\
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1262
    (ret) = 0;                                          \
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1263
    {                                                   \
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
  1264
	while (__offs < (cnt)) {                        \
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
  1265
	    CLEAR_ERRNO;                                \
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
  1266
	    ret = WRITE(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \
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
  1267
	    if (ret <= 0) {                             \
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
  1268
		if (ret < 0 && errno == EINTR) {        \
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
  1269
		    __HANDLE_INTERRUPTS__;              \
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
  1270
		    /* refetch */                       \
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
  1271
		    buf = (char *)(obj);                \
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
  1272
		    continue;                           \
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
  1273
		}                                       \
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
  1274
		break;                                  \
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
  1275
	    }                                           \
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
  1276
	    __offs += (ret);                            \
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
  1277
	}                                               \
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
  1278
	if (__offs > 0)                                 \
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
  1279
	    (ret) = __offs;                             \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1280
    }                                                   \
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  1281
  }
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1282
# else /* use STDIO */
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  1283
#  define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)            \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1284
  {                                                     \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1285
    int __ooffs = obj_offs;                             \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1286
    int __offs = 0;                                     \
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1287
    char *buf = (char *)(obj);                          \
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
  1288
							\
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1289
    (ret) = 0;                                          \
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1290
    if (isBuffered) {                                   \
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
  1291
	while (__offs < (cnt)) {                        \
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
  1292
	    CLEAR_ERRNO;                                \
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
  1293
	    (ret) = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f);  \
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
  1294
	    if ((ret) <= 0) {                           \
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
  1295
		if (ferror(f)) {                        \
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
  1296
		    if (errno == EINTR) {               \
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
  1297
			__HANDLE_INTERRUPTS__;          \
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
  1298
			/* refetch */                   \
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
  1299
			buf = (char *)(obj);            \
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
  1300
			clearerr(f);                    \
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
  1301
			continue;                       \
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
  1302
		    }                                   \
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
  1303
		    break;                              \
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
  1304
		} else {                                \
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
  1305
		    (ret) = 0;                          \
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
  1306
		}                                       \
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
  1307
	    }                                           \
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
  1308
	    __offs += (ret);                            \
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
  1309
	}                                               \
2961
a188727d0619 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2920
diff changeset
  1310
    } else {                                            \
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
  1311
	while (__offs < (cnt)) {                        \
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
  1312
	    CLEAR_ERRNO;                                \
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
  1313
	    (ret) = write(fileno(f), (buf)+__ooffs+__offs, (cnt)-__offs); \
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
  1314
	    if ((ret) <= 0) {                           \
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
  1315
		if ((ret) < 0) {                        \
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
  1316
		    if (errno == EINTR){                \
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
  1317
			__HANDLE_INTERRUPTS__;          \
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
  1318
			/* refetch */                   \
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
  1319
			buf = (char *)(obj);            \
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
  1320
			continue;                       \
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
  1321
		    }                                   \
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
  1322
		}                                       \
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
  1323
		break;                                  \
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
  1324
	    }                                           \
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
  1325
	    __offs += (ret);                            \
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
  1326
	}                                               \
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  1327
    }                                                   \
15845
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  1328
    if (__offs > 0)                                     \
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
  1329
	(ret) = __offs;                                 \
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1330
  }
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1331
# endif /* use STDIO */
4913
4964b2c71d31 remerged ExtSTream_win32 into ExtStream.
Claus Gittinger <cg@exept.de>
parents: 4640
diff changeset
  1332
#endif /* unix */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1333
%}
188
454ed0ee733e EINTR problems
claus
parents: 159
diff changeset
  1334
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1335
2045
1ad17ca6520f fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
  1336
!ExternalStream class methodsFor:'documentation'!
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1337
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1338
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1339
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1340
 COPYRIGHT (c) 1988 by Claus Gittinger
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1341
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1342
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1343
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1344
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1345
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1346
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1347
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1348
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1349
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1350
!
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
  1351
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1352
documentation
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1353
"
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1354
    ExternalStream defines protocol common to Streams which have a file-descriptor and
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1355
    represent some file or communicationChannel of the underlying OperatingSystem.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1356
    ExternalStream is abstract; concrete classes are FileStream, PipeStream etc.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1357
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1358
    ExternalStreams can be in two modes: text- (the default) and binary-mode.
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1359
    In text-mode, the elements read/written are characters;
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1360
    while in binary-mode the basic elements are bytes which read/write as SmallIntegers
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1361
    in the range 0..255.
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1362
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1363
    Also, the stream can be either in buffered or unbuffered mode. In buffered mode,
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1364
    data is not written until either a cr is written (in text mode) or a synchronizeOutput
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1365
    is sent (in both modes).
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1366
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1367
    The underlying OperatingSystem streams may either be closed explicitely (sending a close)
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1368
    or just forgotten - in this case, the garbage collector will eventually collect the
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1369
    object AND a close will be performed automatically (but you will NOT know when this
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1370
    happens - so it is recommended, that you close your files when no longer needed).
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1371
    Closing is also suggested, since if smalltalk is finished (be it by purpose, or due to
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1372
    some crash) the data will not be in the file, if unclosed.
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1373
    All streams understand the close message, so it never hurts to use it (it is defined as
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1374
    a noop in one of the superclasses).
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1375
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1376
    Most of the methods found here redefine inherited methods for better performance,
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1377
    since I/O from/to files should be fast.
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1378
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1379
    Recovering a snapshot:
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1380
      not all streams can be restored to the state they had before - see the implementation of
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1381
      reOpen in subclasses for more information.
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1382
      For streams sitting on some communication channel (i.e. Pipes and Sockets) you should
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1383
      reestablish the stream upon image restart (make someone dependent on ObjectMemory).
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1384
      FileStreams are reopened and positioned to their offset they had at snapshot time.
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1385
      This may fail, if the file was removed or renamed - or lead to confusion
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1386
      if the contents changed in the meantime.
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1387
      Therefore, it is a good idea to reopen files and check for these things at restart time.
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1388
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1389
    [Instance variables:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1390
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1391
	handleType      <Symbol>        desribes what handle is:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1392
					    win32: #fileHandle, #socketHandle,
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1393
						   #filePointer, #socketFilePointer, #pipeFilePointer
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1394
					    unix: #filePointer, #socketFilePointer, #pipeFilePointer
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1395
					needed for win32, which uses different APIs for the different handles (sigh)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1396
	handle          <Integer>       used to be always a filePointer somehow mapped to an integer (FILE* - not the fd);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1397
					now, either a filePointer or a handle (win32)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1398
	mode            <Symbol>        #readwrite, #readonly or #writeonly
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1399
	buffered        <Boolean>       true, if buffered (i.e. collects characters - does
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1400
					not output immediately)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1401
	binary          <Boolean>       true if in binary mode (reads bytes instead of chars)
17332
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  1402
	eolMode         <Symbol>        one of nil, #nl, #cr or #crlf.
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1403
					determines how lines should be terminated.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1404
					nil -> newLine (as in Unix);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1405
					#crlf -> with cr-lf (as in MSDOS)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1406
					#cr -> with cr (as in VMS)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1407
	hitEOF          <Boolean>       true, if EOF was reached
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1408
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1409
	lastErrorNumber <Integer>       the value of errno (only valid right after the error -
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1410
					updated with next i/o operation)
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1411
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1412
    [Class variables:]
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1413
	Lobby           <Registry>      keeps track of used ext-streams (to free up FILE*'s)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1414
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1415
	StreamErrorSignal       <Signal> parent of all stream errors (see Stream class)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1416
	InvalidReadSignal       <Signal> raised on read from writeonly stream
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1417
	InvalidWriteSignal      <Signal> raised on write to readonly stream
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1418
	InvalidModeSignal       <Signal> raised on text I/O with binary-stream
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1419
					 or binary I/O with text-stream
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1420
	OpenErrorSignal         <Signal> raised if open fails
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1421
	StreamNotOpenSignal     <Signal> raised on I/O with non-open stream
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1422
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1423
    Additional notes:
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1424
      This class is implemented using the underlying stdio-c library package, which
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1425
      has both advantages and disadvantages: since it is portable (posix defined), porting
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1426
      ST/X to non-Unix machines is simplified. The disadvantage is that the stdio library
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1427
      has big problems handling unbounded Streams, since the EOF handling in stdio is
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1428
      not prepared for data to arrive after EOF has been reached - time will show, if we need
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1429
      a complete rewrite for UnboundedStream ...
325
claus
parents: 308
diff changeset
  1430
188
454ed0ee733e EINTR problems
claus
parents: 159
diff changeset
  1431
      Also, depending on the system, the stdio library behaves infriendly when signals
454ed0ee733e EINTR problems
claus
parents: 159
diff changeset
  1432
      occur while reading (for example, timer interrupts) - on real unixes (i.e. BSD) the signal
454ed0ee733e EINTR problems
claus
parents: 159
diff changeset
  1433
      is handled transparently - on SYS5.3 (i.e. non unixes :-) the read operation returns
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1434
      an error and errno is set to EINTR.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1435
      Thats what the ugly code around all getc-calls is for ...
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1436
      Since things get more and more ugly - we will rewrite ExternalStream
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1437
      completely, to NOT use any stdio stuff (and do its buffering itself).
325
claus
parents: 308
diff changeset
  1438
claus
parents: 308
diff changeset
  1439
      Notice that typical stdio's use a single errno global variable to return an error code,
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1440
      this was bad design in the stdio lib (right from the very beginning), since it's much
325
claus
parents: 308
diff changeset
  1441
      harder to deal with this in the presence of lightweight processes, where errno gets
claus
parents: 308
diff changeset
  1442
      overwritten by an I/O operation done in another thread. (stdio should have been written
claus
parents: 308
diff changeset
  1443
      to return errno as a negative number ...).
claus
parents: 308
diff changeset
  1444
      To deal with this, the scheduler treats errno like a per-thread private variable,
claus
parents: 308
diff changeset
  1445
      and saves/restores the errno setting when switching to another thread.
claus
parents: 308
diff changeset
  1446
      (Notice that some thread packages do this also, but ST/X's thread implementation
claus
parents: 308
diff changeset
  1447
      does not depend on those, but instead uses a portable private package).
claus
parents: 308
diff changeset
  1448
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  1449
      Finally, if an stdio-stream is open for both reading and writing, we have to call
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  1450
      fseek whenever we are about to read after write and vice versa.
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  1451
      Two macros (__READING__ and __WRITING__) have been defined to be used before every
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  1452
      fread/fgetc and fwrite/putc respectively.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1453
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1454
    [author:]
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1455
	Claus Gittinger
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1456
	Stefan Vogel (many, many fixes ...)
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1457
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  1458
    [see also:]
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1459
	FileStream Socket PipeStream
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  1460
	Filename OperatingSystem
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1461
"
3255
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1462
!
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1463
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1464
examples
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1465
"
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1466
    open a file, read the contents and display it in a textView:
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1467
									[exBegin]
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1468
	|topView scrollPane textView fileStream text|
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1469
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1470
	topView := StandardSystemView new.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1471
	topView label:'contents of Makefile'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1472
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1473
	scrollPane := HVScrollableView in:topView.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1474
	scrollPane origin:0.0@0.0 corner:1.0@1.0.
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1475
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1476
	textView := EditTextView new.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1477
	scrollPane scrolledView:textView.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1478
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1479
	fileStream := 'Makefile' asFilename readStream.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1480
	text := fileStream upToEnd.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1481
	fileStream close.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1482
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1483
	textView contents:text.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1484
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1485
	topView open.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1486
									[exEnd]
3255
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1487
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1488
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1489
    Notice, all of the above can also be done (simply) as:
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1490
									[exBegin]
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1491
	EditTextView openOn:'Makefile'
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1492
									[exEnd]
3255
eb76cba071a1 example
Claus Gittinger <cg@exept.de>
parents: 3235
diff changeset
  1493
"
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1494
! !
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  1495
2045
1ad17ca6520f fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
  1496
!ExternalStream class methodsFor:'initialization'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1497
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1498
initDefaultEOLMode
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1499
    OperatingSystem isUNIXlike ifTrue:[
17426
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1500
	"/ unix EOL conventions
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1501
	DefaultEOLMode := #nl
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1502
    ] ifFalse:[
17426
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1503
	OperatingSystem isVMSlike ifTrue:[
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1504
	    "/ vms EOL conventions
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1505
	    DefaultEOLMode := #cr
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1506
	] ifFalse:[
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1507
	    "/ msdos EOL conventions
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1508
	    "/ msdos uses #crlf. but we keep #nl here,
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1509
	    "/ otherwise sending HTTP request over sockets fails
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  1510
"/            DefaultEOLMode := #crlf
17426
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1511
	    DefaultEOLMode := #nl
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  1512
	]
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1513
    ]
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1514
!
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1515
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1516
initModeStrings
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1517
    "initialize modeStrings which are passed down to the underlying
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1518
     fopen/fdopen functions."
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1519
3132
037027ae20fd msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1520
    OperatingSystem isMSDOSlike ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1521
	ReadMode := 'rb'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1522
	ReadWriteMode := 'rb+'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1523
	WriteMode := 'wb'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1524
	AppendMode := 'ab+'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1525
	CreateReadWriteMode := 'wb+'.
3132
037027ae20fd msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1526
    ] ifFalse:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1527
	ReadMode := 'r'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1528
	ReadWriteMode := 'r+'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1529
	WriteMode := 'w'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1530
	AppendMode := 'a+'.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1531
	CreateReadWriteMode := 'w+'.
3132
037027ae20fd msdos MUST use binary mode in I/O
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1532
    ]
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1533
!
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1534
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1535
initialize
593
19d568779cf7 moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents: 583
diff changeset
  1536
    OpenErrorSignal isNil ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1537
	OpenErrorSignal := OpenError.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1538
	OpenErrorSignal notifierString:'open error'.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1539
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1540
	InvalidReadSignal := InvalidReadError.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1541
	InvalidReadSignal notifierString:'stream does not support reading'.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1542
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1543
	InvalidWriteSignal := InvalidWriteError.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1544
	InvalidWriteSignal notifierString:'stream does not support writing'.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1545
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1546
	InvalidModeSignal := InvalidModeError.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1547
	InvalidModeSignal notifierString:'binary/text mode mismatch'.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1548
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1549
	InvalidOperationSignal := InvalidOperationError.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1550
	InvalidOperationSignal notifierString:'unsupported file operation'.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1551
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1552
	StreamNotOpenSignal := StreamNotOpenError.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1553
	StreamNotOpenSignal notifierString:'stream is not open'.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1554
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1555
	StreamIOErrorSignal := StreamIOError.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1556
	StreamIOErrorSignal notifierString:'I/O error'.
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1557
    ].
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1558
31
75f2b9f78be2 *** empty log message ***
claus
parents: 25
diff changeset
  1559
    Lobby isNil ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1560
	Lobby := Registry new.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1561
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1562
	"want to get informed when returning from snapshot"
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1563
	ObjectMemory addDependent:self
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1564
    ].
5442
24b88d75396d only init once
Claus Gittinger <cg@exept.de>
parents: 5441
diff changeset
  1565
    DefaultEOLMode isNil ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1566
	self initDefaultEOLMode.
5442
24b88d75396d only init once
Claus Gittinger <cg@exept.de>
parents: 5441
diff changeset
  1567
    ].
24b88d75396d only init once
Claus Gittinger <cg@exept.de>
parents: 5441
diff changeset
  1568
    ReadMode isNil ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  1569
	self initModeStrings.
5442
24b88d75396d only init once
Claus Gittinger <cg@exept.de>
parents: 5441
diff changeset
  1570
    ].
3474
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1571
    "Modified: / 21.5.1998 / 16:33:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1572
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1573
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1574
reOpenFiles
4108
8c4a45bc70eb perform reOpen of streams earlier (before doing the view-reinit)
Claus Gittinger <cg@exept.de>
parents: 4093
diff changeset
  1575
    "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
  1576
     This is invoked via the #earlyRestart change notification."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1577
301
35e40a6fc72b *** empty log message ***
claus
parents: 269
diff changeset
  1578
    Lobby do:[:aFileStream |
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1579
	aFileStream reOpen
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1580
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1581
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1582
1469
570ef7f8667b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1464
diff changeset
  1583
update:something with:aParameter from:changedObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1584
    "have to reopen files when returning from snapshot"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1585
4108
8c4a45bc70eb perform reOpen of streams earlier (before doing the view-reinit)
Claus Gittinger <cg@exept.de>
parents: 4093
diff changeset
  1586
    something == #earlyRestart ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1587
	self reOpenFiles.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1588
	self initDefaultEOLMode
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1589
    ]
1469
570ef7f8667b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1464
diff changeset
  1590
570ef7f8667b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1464
diff changeset
  1591
    "Created: 15.6.1996 / 15:19:59 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1592
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1593
2045
1ad17ca6520f fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
  1594
!ExternalStream class methodsFor:'instance creation'!
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1595
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1596
forFileDescriptor:aFileDescriptor mode:modeSymbol
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1597
    "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
  1598
     to operate on this fd.
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1599
     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
  1600
     This may be used to wrap fd's as returned by user
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1601
     primitive code, or to wrap pipe-fds into externalStreams."
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1602
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1603
    ^ self forFileDescriptor:aFileDescriptor mode:modeSymbol buffered:true handleType:nil
7002
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1604
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1605
    "
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1606
     the example below will probably fail (15 is a random FD):
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1607
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1608
     |s|
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1609
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1610
     s := ExternalStream forFileDescriptor:15 mode:'r'.
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1611
     s next.
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1612
    "
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1613
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1614
    "Created: 29.2.1996 / 18:05:00 / cg"
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1615
    "Modified: 29.2.1996 / 18:17:07 / cg"
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1616
!
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1617
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1618
forFileDescriptor:aFileDescriptor mode:modeSymbol buffered:buffered handleType:handleTypeSymbol
7002
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1619
    "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
  1620
     to operate on this fd.
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1621
     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
  1622
     This may be used to wrap fd's as returned by user
7002
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1623
     primitive code, or to wrap pipe-fds into externalStreams."
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1624
1048
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1625
    |newStream|
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1626
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1627
    newStream := self basicNew.
7002
7a5d55f87cd7 make pseudoTTYs unbuffered (req'd for UNIXWARE)
penk
parents: 6812
diff changeset
  1628
    newStream text; buffered:buffered; eolMode:DefaultEOLMode; clearEOF.
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1629
    ^ newStream connectTo:aFileDescriptor withMode:modeSymbol handleType:handleTypeSymbol
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1630
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1631
    "
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1632
     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
  1633
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1634
     |s|
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1635
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1636
     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
  1637
     s next.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1638
    "
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1639
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1640
    "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
  1641
    "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
  1642
!
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1643
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1644
forReadWriteToFileDescriptor:aFileDescriptor
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1645
    "given a fileDescriptor, create an ExternalStream object
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1646
     to read/write from/to this fd. This may be used to wrap fd's
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1647
     as returned by user primitive code, or to wrap pipe-
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1648
     filedescriptors into externalStreams."
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1649
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1650
    ^ self forFileDescriptor:aFileDescriptor mode:#readWrite
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1651
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1652
    "
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1653
     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
  1654
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1655
     |s|
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
     s := ExternalStream forReadWriteToFileDescriptor:15.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1658
     s next.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1659
    "
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1660
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1661
    "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
  1662
    "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
  1663
!
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1664
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1665
forReadingFromFileDescriptor:aFileDescriptor
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1666
    "given a fileDescriptor, create an ExternalStream object
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1667
     to read from this fd. This may be used to wrap fd's
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1668
     as returned by user primitive code, or to wrap pipe-
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1669
     filedescriptors into externalStreams."
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1670
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1671
    ^ self forFileDescriptor:aFileDescriptor mode:#readonly
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1672
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1673
    "
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1674
     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
  1675
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1676
     |s|
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
     s := ExternalStream forReadingFromFileDescriptor:15.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1679
     s next.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1680
    "
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1681
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1682
    "
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1683
     |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
  1684
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1685
     'create OS pipe ...'.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1686
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1687
     pipe := OperatingSystem makePipe.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1688
     readFd := pipe at:1.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1689
     writeFd := pipe at:2.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1690
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1691
     'connect Smalltalk streams ...'.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1692
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1693
     rs := ExternalStream forReadingFromFileDescriptor:readFd.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1694
     ws := ExternalStream forWritingToFileDescriptor:writeFd.
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
     'read ...'.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1697
     [
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1698
	 1 to:10 do:[:i |
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1699
	     Transcript showCR:rs nextLine
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1700
	 ].
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1701
	 rs close.
1048
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1702
     ] forkAt:7.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1703
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1704
     'write ...'.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1705
     [
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1706
	 1 to:10 do:[:i |
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1707
	     ws nextPutAll:'hello world '; nextPutAll:i printString; cr
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1708
	 ].
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  1709
	 ws close.
1048
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1710
     ] fork.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1711
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1712
    "
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1713
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1714
    "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
  1715
    "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
  1716
!
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
forWritingToFileDescriptor:aFileDescriptor
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1719
    "given a fileDescriptor, create an ExternalStream object
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1720
     to write to this fd. This may be used to wrap fd's
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1721
     as returned by user primitive code, or to wrap pipe-
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1722
     filedescriptors into externalStreams."
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1723
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  1724
    ^ self forFileDescriptor:aFileDescriptor mode:#writeonly
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1725
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1726
    "
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  1727
     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
  1728
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1729
     |s|
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
     s := ExternalStream forWritingToFileDescriptor:15.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1732
     s binary.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1733
     s nextPut:1.
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1734
    "
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1735
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1736
    "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
  1737
    "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
  1738
!
c535c5a73cba added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents: 1044
diff changeset
  1739
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1740
new
15918
d5fe9b9e87d3 Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 15896
diff changeset
  1741
    "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
  1742
d5fe9b9e87d3 Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 15896
diff changeset
  1743
    ^ self basicNew initialize.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1744
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1745
2045
1ad17ca6520f fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
  1746
!ExternalStream class methodsFor:'Signal constants'!
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1747
1522
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  1748
inaccessibleSignal
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  1749
    "ST-80 compatibility: return openErrorSignal"
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  1750
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  1751
    ^ self openErrorSignal
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  1752
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  1753
    "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
  1754
!
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  1755
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1756
invalidModeSignal
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1757
    "return the signal raised when doing text-I/O with a binary stream
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1758
     or binary-I/O with a text stream"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1759
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  1760
    ^ InvalidModeError
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1761
!
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1762
932
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  1763
invalidOperationSignal
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  1764
    "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
  1765
     I/O operation is attempted"
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  1766
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  1767
    ^ InvalidOperationError
932
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  1768
!
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  1769
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1770
invalidReadSignal
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1771
    "return the signal raised when reading from writeonly streams"
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1772
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  1773
    ^ InvalidReadError
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1774
!
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1775
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1776
invalidWriteSignal
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1777
    "return the signal raised when writing to readonly streams"
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1778
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  1779
    ^ InvalidWriteError
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1780
!
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1781
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1782
openErrorSignal
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1783
    "return the signal raised when a file open failed"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1784
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1785
    ^ OpenErrorSignal
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1786
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1787
3474
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1788
streamIOErrorSignal
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1789
    "return the signal raised when an I/O error occurs.
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1790
     (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
  1791
     which is no longer available and has been mounted soft)"
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1792
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  1793
    ^ StreamIOError
3474
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1794
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1795
    "Created: / 21.5.1998 / 16:32:55 / cg"
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1796
!
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  1797
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1798
streamNotOpenSignal
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1799
    "return the signal raised on I/O with closed streams"
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1800
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  1801
    ^ StreamNotOpenError
2
claus
parents: 1
diff changeset
  1802
! !
claus
parents: 1
diff changeset
  1803
2045
1ad17ca6520f fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
  1804
!ExternalStream class methodsFor:'error handling'!
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1805
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1806
errorReporter
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1807
    "I know about error codes"
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1808
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1809
    ^ self
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1810
!
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1811
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1812
lastErrorNumber
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1813
    "return the errno of the last error"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1814
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1815
    ^ LastErrorNumber
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1816
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1817
    "
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1818
     ExternalStream lastErrorNumber
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  1819
    "
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1820
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1821
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1822
lastErrorString
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1823
    "return a message string describing the last error"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1824
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1825
    ^ OperatingSystem errorTextForNumber:LastErrorNumber
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1826
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1827
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1828
     ExternalStream lastErrorString
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1829
    "
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1830
!
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1831
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1832
reportOn:anErrorSymbolOrNumber
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1833
    "an error occured.
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1834
     Report it via an Exception"
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1835
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1836
    anErrorSymbolOrNumber isInteger ifTrue:[
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1837
	StreamError
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1838
	    raiseRequestWith:anErrorSymbolOrNumber
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1839
	    errorString:(' - os error:' , (OperatingSystem errorTextForNumber:anErrorSymbolOrNumber))
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1840
    ].
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1841
    (self respondsTo:anErrorSymbolOrNumber) ifTrue:[
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1842
	self perform:anErrorSymbolOrNumber
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1843
    ] ifFalse:[
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1844
	StreamError
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1845
	    raiseRequestWith:anErrorSymbolOrNumber
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1846
	    errorString:(' - ' , anErrorSymbolOrNumber printString)
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1847
    ].
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  1848
    ^ false
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1849
! !
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1850
7497
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1851
!ExternalStream class methodsFor:'obsolete'!
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1852
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1853
makePTYPair
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1854
    "obsolete since 12-07-2003"
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1855
    <resource:#obsolete>
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1856
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1857
    self obsoleteMethodWarning:'use NonPositionableExternalStream makePTYPair'.
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1858
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1859
    ^ NonPositionableExternalStream makePTYPair.
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1860
!
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1861
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1862
makePipe
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1863
    "obsolete since 12-07-2003"
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1864
    <resource:#obsolete>
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1865
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1866
    self obsoleteMethodWarning:'use NonPositionableExternalStream makePipe'.
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1867
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1868
    ^ NonPositionableExternalStream makePipe.
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1869
! !
cc29afd51151 Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents: 7261
diff changeset
  1870
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7195
diff changeset
  1871
!ExternalStream methodsFor:'Compatibility-Dolphin'!
6474
92a666c5b104 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6461
diff changeset
  1872
92a666c5b104 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6461
diff changeset
  1873
beText
92a666c5b104 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6461
diff changeset
  1874
    self text
92a666c5b104 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6461
diff changeset
  1875
! !
92a666c5b104 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6461
diff changeset
  1876
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7195
diff changeset
  1877
!ExternalStream methodsFor:'Compatibility-Squeak'!
3044
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1878
3872
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1879
nextInto:aByteArrayOrString
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1880
    "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
  1881
     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
  1882
     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
  1883
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1884
    |n nWanted|
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1885
4368
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1886
    nWanted := aByteArrayOrString byteSize.
3872
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1887
    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
  1888
    n == nWanted ifTrue:[^ aByteArrayOrString].
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1889
    ^ aByteArrayOrString copyTo:n
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1890
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1891
!
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1892
4368
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1893
readInto:aContainer startingAt:index count:nElements
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1894
    "same as #nextBytes:into:startingAt: for ByteArrays;
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1895
     for LongArrays, nelements longs are read.
3872
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1896
     Squeak compatibility."
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1897
4368
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1898
    |elementSize n|
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1899
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1900
    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
  1901
    n := self nextBytes:nElements*elementSize into:aContainer startingAt:(index-1)*elementSize+1.
4368
d69568f9ad95 squeak compatibility fixes
Claus Gittinger <cg@exept.de>
parents: 4161
diff changeset
  1902
    ^ n // elementSize
3872
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1903
13940
12eb1c056d5e fixed: #readInto:startingAt:count:; offset was wrong for non-byte elements
Claus Gittinger <cg@exept.de>
parents: 13927
diff changeset
  1904
    "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
  1905
!
cab901645d2a added #nextInto: and #readInto: for Squeak compatibility.
Claus Gittinger <cg@exept.de>
parents: 3863
diff changeset
  1906
3044
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1907
readOnly
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1908
    "Squeak compatibility: make the stream readOnly"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1909
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1910
    mode := #readonly
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1911
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1912
    "Modified: 20.10.1997 / 19:23:04 / cg"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1913
    "Created: 20.10.1997 / 19:23:19 / cg"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1914
! !
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3029
diff changeset
  1915
5187
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1916
!ExternalStream methodsFor:'Signal constants'!
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1917
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1918
invalidReadSignal
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1919
    ^ self class invalidReadSignal
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1920
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1921
    "Created: / 3.12.1998 / 15:12:06 / cg"
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1922
!
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1923
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1924
invalidWriteSignal
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1925
    ^ self class invalidWriteSignal
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1926
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1927
    "Created: / 3.12.1998 / 15:12:10 / cg"
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1928
! !
4bf3787cc63a category renamining
Claus Gittinger <cg@exept.de>
parents: 5120
diff changeset
  1929
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1930
!ExternalStream methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1931
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1932
binary
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1933
    "switch to binary mode - default is text"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1934
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  1935
    binary := true
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1936
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1937
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1938
buffered:aBoolean
10
claus
parents: 5
diff changeset
  1939
    "turn buffering on or off - default is on"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1940
10
claus
parents: 5
diff changeset
  1941
    buffered := aBoolean
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1942
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1943
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1944
contents
2
claus
parents: 1
diff changeset
  1945
    "return the contents of the file from the current position up-to
claus
parents: 1
diff changeset
  1946
     the end. If the stream is in binary mode, a ByteArray containing
claus
parents: 1
diff changeset
  1947
     the byte values is returned.
claus
parents: 1
diff changeset
  1948
     In text-mode, a collection of strings, each representing one line,
claus
parents: 1
diff changeset
  1949
     is returned."
claus
parents: 1
diff changeset
  1950
claus
parents: 1
diff changeset
  1951
    |text l chunks sizes chunk byteCount cnt bytes offset|
claus
parents: 1
diff changeset
  1952
claus
parents: 1
diff changeset
  1953
    binary ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1954
	"adding to a ByteArray produces quadratic time-space
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1955
	 behavior - therefore we allocate chunks, and concatenate them
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1956
	 at the end."
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1957
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1958
	chunks := OrderedCollection new.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1959
	sizes := OrderedCollection new.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1960
	byteCount := 0.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1961
	[self atEnd] whileFalse:[
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1962
	    chunk := ByteArray uninitializedNew:4096.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1963
	    cnt := self nextBytes:(chunk size) into:chunk.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1964
	    cnt notNil ifTrue:[
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1965
		chunks add:chunk.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1966
		sizes add:cnt.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1967
		byteCount := byteCount + cnt
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1968
	    ]
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1969
	].
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1970
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1971
	"now, create one big ByteArray"
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1972
	bytes := ByteArray uninitializedNew:byteCount.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1973
	offset := 1.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1974
	1 to:chunks size do:[:index |
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1975
	    chunk := chunks at:index.
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  1976
	    cnt := sizes at:index.
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1977
	    bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1978
	    offset := offset + cnt
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1979
	].
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1980
	^ bytes
2
claus
parents: 1
diff changeset
  1981
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1982
244
9faa2da0650a Text <-> StringCollection
claus
parents: 223
diff changeset
  1983
    text := StringCollection new.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1984
    [self atEnd] whileFalse:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1985
	l := self nextLine.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1986
	l isNil ifTrue:[
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1987
	    ^ text
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1988
	].
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  1989
	text add:l
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1990
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1991
    ^ text
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1992
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1993
11751
1e75c54748ef added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents: 11651
diff changeset
  1994
contentsAsString
1e75c54748ef added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents: 11651
diff changeset
  1995
    "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
  1996
     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
  1997
1e75c54748ef added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents: 11651
diff changeset
  1998
    ^ self contentsOfEntireFile
1e75c54748ef added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents: 11651
diff changeset
  1999
!
1e75c54748ef added #contentsAsString for Filename compatibility
Stefan Vogel <sv@exept.de>
parents: 11651
diff changeset
  2000
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2001
contentsOfEntireFile
3938
4e7d1880336b oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents: 3906
diff changeset
  2002
    "ST-80 compatibility: return contents as a String (or byteArray, if in binary mode).
3876
a680b41a014c comment
Claus Gittinger <cg@exept.de>
parents: 3872
diff changeset
  2003
     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
  2004
6305
53b87502fc21 Fix #contentsOfEntireFile
Stefan Vogel <sv@exept.de>
parents: 6269
diff changeset
  2005
    binary ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2006
	^ self contents.
3938
4e7d1880336b oops - #contentsOfEntireFile must return a byteArray if in binaryMode.
Claus Gittinger <cg@exept.de>
parents: 3906
diff changeset
  2007
    ].
6305
53b87502fc21 Fix #contentsOfEntireFile
Stefan Vogel <sv@exept.de>
parents: 6269
diff changeset
  2008
    ^ self upToEnd
1522
1d0b4bc2e563 ST-80 compatibility: #contentsOfEntireFile must return a String.
Stefan Vogel <sv@exept.de>
parents: 1515
diff changeset
  2009
3876
a680b41a014c comment
Claus Gittinger <cg@exept.de>
parents: 3872
diff changeset
  2010
    "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
  2011
    "Modified: / 27.11.1998 / 16:29:43 / cg"
2
claus
parents: 1
diff changeset
  2012
!
claus
parents: 1
diff changeset
  2013
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2014
contentsSpecies
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2015
    "return the kind of object to be returned by sub-collection builders
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2016
     (such as upTo)"
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  2017
5441
e88302747bbd rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5431
diff changeset
  2018
    binary == true ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2019
	^ ByteArray
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  2020
    ].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2021
    ^ String
2
claus
parents: 1
diff changeset
  2022
!
claus
parents: 1
diff changeset
  2023
16776
fd62ae0ff6fa class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16551
diff changeset
  2024
eolMode
fd62ae0ff6fa class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16551
diff changeset
  2025
    "return how end-of-line (EOL) is to be marked.
fd62ae0ff6fa class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16551
diff changeset
  2026
     Returns one one of:
17432
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2027
	#crlf         -> add a CR-NL, as in MSDOS
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2028
	#cr           -> add a CR, as in VMS
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2029
	#nl           -> add a NL, as in Unix
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2030
	nil           -> transparent
16776
fd62ae0ff6fa class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16551
diff changeset
  2031
    "
fd62ae0ff6fa class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16551
diff changeset
  2032
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
  2033
    ^ eolMode
16776
fd62ae0ff6fa class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16551
diff changeset
  2034
!
fd62ae0ff6fa class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16551
diff changeset
  2035
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2036
eolMode:aSymbolOrNil
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2037
    "specify how end-of-line (EOL) is to be marked.
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2038
     The argument may be one of:
17432
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2039
	#crlf         -> add a CR-NL, as in MSDOS
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2040
	#cr           -> add a CR, as in VMS
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2041
	#nl           -> add a NL, as in Unix
c4deddc2545a fixed lineend handling; had segfaults here
Claus Gittinger <cg@exept.de>
parents: 17430
diff changeset
  2042
	anyOther      -> like #nl
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2043
    "
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2044
17462
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2045
    aSymbolOrNil == #crnl ifTrue:[
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2046
	eolMode := #crlf
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2047
    ] ifFalse:[
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2048
	aSymbolOrNil == #lf ifTrue:[
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2049
	    eolMode := #nl
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2050
	] ifFalse:[
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2051
	    eolMode := aSymbolOrNil
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2052
	]
6448b4559e12 ouch: eolMode
Claus Gittinger <cg@exept.de>
parents: 17432
diff changeset
  2053
    ].
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2054
!
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2055
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2056
fileDescriptor
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2057
    "return the fileDescriptor of the receiver -
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2058
     notice: this one returns the underlying OSs fileDescriptor -
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2059
     this may not be available on all platforms (i.e. non unix systems)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2060
3971
aad506cdc5d9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3956
diff changeset
  2061
%{
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2062
    OBJ _handle  = __INST(handle);
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2063
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2064
    if (_handle != nil) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2065
	if (__INST(handleType) == @symbol(socketHandle)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2066
	    RETURN (_handle);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2067
	} else if ((__INST(handleType) == nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2068
		     || (__INST(handleType) == @symbol(filePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2069
		     || (__INST(handleType) == @symbol(socketFilePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2070
		     || (__INST(handleType) == @symbol(pipeFilePointer))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2071
	    RETURN ( __MKINT(fileno(__FILEVal(_handle))));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2072
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2073
    }
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  2074
%}.
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2075
    handle isNil ifTrue:[^ self errorNotOpen].
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2076
    ^ self fileDescriptorOfFile:handle
5852
338c6e0cab8f rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5851
diff changeset
  2077
!
338c6e0cab8f rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5851
diff changeset
  2078
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2079
fileDescriptorOfFile:handle
5852
338c6e0cab8f rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5851
diff changeset
  2080
    "for migration to rel5 only:
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2081
     return the fileDescriptor of the argument handle -
5852
338c6e0cab8f rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5851
diff changeset
  2082
     notice: this one returns the underlying OSs fileDescriptor -
338c6e0cab8f rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5851
diff changeset
  2083
     this may not be available on all platforms (i.e. non unix systems)."
338c6e0cab8f rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5851
diff changeset
  2084
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2085
%{
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2086
#ifdef __SCHTEAM__
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2087
    return context._RETURN(handle);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2088
#endif
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2089
%}.
5852
338c6e0cab8f rel5 migration
Claus Gittinger <cg@exept.de>
parents: 5851
diff changeset
  2090
    self primitiveFailed
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2091
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2092
10455
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2093
fileHandle
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2094
    "return the fileHandle of the receiver.
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2095
     Under unix, this is the fileDescriptor; under windows, this is the Handle."
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2096
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2097
%{
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2098
    OBJ _handle  = __INST(handle);
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2099
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2100
    if (_handle != nil) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2101
	if ((__INST(handleType) == @symbol(fileHandle))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2102
	    || (__INST(handleType) == @symbol(socketHandle))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2103
	    RETURN (_handle);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2104
	} else if ((__INST(handleType) == nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2105
		     || (__INST(handleType) == @symbol(filePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2106
		     || (__INST(handleType) == @symbol(socketFilePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2107
		     || (__INST(handleType) == @symbol(pipeFilePointer))) {
10455
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2108
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2109
	    RETURN(__MKEXTERNALADDRESS(_get_osfhandle(fileno(__FILEVal(_handle)))));
10455
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2110
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2111
	    RETURN (__MKINT(fileno(__FILEVal(_handle))));
10455
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2112
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2113
	}
10455
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2114
    }
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2115
%}.
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2116
    handle isNil ifTrue:[^ self errorNotOpen].
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2117
    ^ handle
10455
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2118
!
8a434bc03256 +fileHandle
Claus Gittinger <cg@exept.de>
parents: 10450
diff changeset
  2119
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2120
filePointer
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2121
    "return the filePointer of the receiver -
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2122
     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
  2123
     a FILE * - not a fileDescriptor.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2124
     (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
  2125
     You cannot do much with the returned value
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2126
     - except passing it to a primitive, for example."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2127
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2128
    (handleType isNil or:[handleType == #filePointer]) ifTrue:[
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2129
	^ handle
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2130
    ].
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2131
    ^ self error:'not a FILE*'
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2132
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2133
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2134
handleType
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2135
    ^ handleType
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2136
!
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2137
5323
f4c3a230f42f more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5204
diff changeset
  2138
lineEndCRLF
f4c3a230f42f more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5204
diff changeset
  2139
    eolMode := #crlf
f4c3a230f42f more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5204
diff changeset
  2140
!
f4c3a230f42f more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5204
diff changeset
  2141
17672
758c3ff0c9e0 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17634
diff changeset
  2142
lineEndLF
758c3ff0c9e0 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17634
diff changeset
  2143
    eolMode := #nl
758c3ff0c9e0 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17634
diff changeset
  2144
!
758c3ff0c9e0 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17634
diff changeset
  2145
3016
42466b291fae more ST80 compatibility
ca
parents: 2999
diff changeset
  2146
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
  2147
    eolMode := #nl
3016
42466b291fae more ST80 compatibility
ca
parents: 2999
diff changeset
  2148
!
42466b291fae more ST80 compatibility
ca
parents: 2999
diff changeset
  2149
8069
fdf460457c96 acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents: 8066
diff changeset
  2150
pathName
8511
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2151
    "answer the pathName of the stream.
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2152
     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
  2153
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2154
    ^ ''
8069
fdf460457c96 acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents: 8066
diff changeset
  2155
!
fdf460457c96 acessing: pathName added (returns nil)
Michael Beyl <mb@exept.de>
parents: 8066
diff changeset
  2156
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2157
readonly
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2158
    "set access mode to readonly"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2159
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2160
    mode := #readonly
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2161
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2162
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2163
readwrite
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2164
    "set access mode to readwrite"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2165
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2166
    mode := #readwrite
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2167
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2168
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2169
text
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2170
    "switch to text mode - default is text"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2171
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2172
    binary := false
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2173
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2174
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2175
useCRLF:aBoolean
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2176
    "turn on or off CRLF sending (instead of LF only) - default is off.
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2177
     This method is provided for backward compatibility - see #eolMode:
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2178
     which offers another choice."
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2179
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2180
    aBoolean ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2181
	eolMode := #crlf
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2182
    ] ifFalse:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2183
	eolMode := #nl
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2184
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2185
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2186
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2187
writeonly
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2188
    "set access mode to writeonly"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2189
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2190
    mode := #writeonly
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2191
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2192
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2193
!ExternalStream methodsFor:'closing'!
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2194
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2195
close
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2196
    "close the stream.
16300
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  2197
     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
  2198
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2199
    self isOpen ifTrue:[
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2200
	Lobby unregister:self.
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2201
	self closeFile.
16300
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  2202
    ].
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2203
!
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2204
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2205
shutDown
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2206
    "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
  2207
     see comment there"
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2208
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2209
    self isOpen ifTrue:[
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2210
	Lobby unregister:self.
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2211
	self closeFile
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2212
    ]
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2213
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2214
    "Modified: 30.8.1996 / 00:39:21 / cg"
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2215
! !
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  2216
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2217
!ExternalStream methodsFor:'copying'!
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2218
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2219
copy
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2220
    "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
  2221
     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
  2222
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2223
    |copy|
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2224
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2225
    copy := super copy.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2226
    copy dupFd.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2227
    Lobby register:copy.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2228
    ^ copy
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2229
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2230
    "
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2231
       |stream1 stream2|
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2232
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2233
       stream1 := Filename newTemporary writeStream.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2234
       stream2 := stream1 copy.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2235
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2236
       stream1 inspect.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2237
       stream2 inspect.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2238
    "
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2239
! !
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  2240
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2241
!ExternalStream methodsFor:'error handling'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2242
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2243
argumentMustBeCharacter
14440
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2244
    "report an error, that the argument must be a character in 0..FF"
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2245
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2246
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2247
14440
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2248
    ArgumentError raiseErrorString:' - argument must be a single byte character'
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2249
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2250
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2251
argumentMustBeInteger
14440
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2252
    "report an error, that the argument must be an integer"
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2253
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2254
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2255
14440
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2256
    ArgumentError raiseErrorString:' - argument must be an integer'
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2257
!
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2258
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2259
argumentMustBeString
14440
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2260
    "report an error, that the argument must be a string"
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2261
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2262
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2263
14440
88e708da1ae1 changed:
Stefan Vogel <sv@exept.de>
parents: 14438
diff changeset
  2264
    ArgumentError raiseErrorString:' - argument must be a string'
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2265
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2266
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2267
errorAlreadyOpen
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2268
    "{ Pragma: +optSpace }"
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2269
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2270
    "report an error, that the stream is already opened"
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2271
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  2272
    ^ OpenError
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2273
	raiseRequestWith:self
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2274
	errorString:' - stream is already open'
8511
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2275
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2276
    "
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2277
      self new errorAlreadyOpen
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2278
    "
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2279
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2280
    "Modified: / 8.5.1999 / 20:12:30 / cg"
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2281
!
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4468
diff changeset
  2282
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2283
errorBinary
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2284
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2285
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2286
    "report an error, that the stream is in binary mode"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2287
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  2288
    ^ InvalidModeError
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2289
	raiseRequestWith:self
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2290
	errorString:(self class name , ' is in binary mode')
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2291
	"/ in:thisContext sender
4161
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2292
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2293
    "Modified: / 8.5.1999 / 20:12:43 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2294
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2295
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2296
errorNotBinary
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2297
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2298
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2299
    "report an error, that the stream is not in binary mode"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2300
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  2301
    ^ InvalidModeError
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2302
	raiseRequestWith:self
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2303
	errorString:(self class name , ' is not in binary mode')
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2304
	"/ in:thisContext sender
4161
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2305
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2306
    "Modified: / 8.5.1999 / 20:12:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2307
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2308
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2309
errorNotBuffered
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2310
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2311
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2312
    "report an error, that the stream is not in buffered mode"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2313
8074
4dce05d6d0a1 class based exceptions
Claus Gittinger <cg@exept.de>
parents: 8070
diff changeset
  2314
    ^ StreamError
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2315
	raiseRequestWith:self
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2316
	errorString:(self class name , ' is unbuffered - operation not allowed')
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2317
	"/ in:thisContext sender
4161
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2318
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2319
    "Modified: / 8.5.1999 / 20:12:36 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2320
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2321
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2322
errorNotOpen
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2323
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2324
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2325
    "report an error, that the stream has not been opened"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2326
8511
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2327
    ^ 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
  2328
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2329
    "Modified: / 8.5.1999 / 20:12:33 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2330
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2331
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2332
errorReadOnly
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2333
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2334
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2335
    "report an error, that the stream is a readOnly stream"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2336
8511
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2337
    ^ 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
  2338
4468
8c67b4f973d8 raise vs. raiseRequest
Claus Gittinger <cg@exept.de>
parents: 4456
diff changeset
  2339
    "Modified: / 30.7.1999 / 17:08:19 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2340
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2341
6255
d68343dc8288 Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents: 6012
diff changeset
  2342
errorReporter
d68343dc8288 Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents: 6012
diff changeset
  2343
    "ST-80 mimicry."
d68343dc8288 Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents: 6012
diff changeset
  2344
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  2345
    ^ self class
6255
d68343dc8288 Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents: 6012
diff changeset
  2346
!
d68343dc8288 Define #errorReporter and #reportError: (ST-80 protocol)
Stefan Vogel <sv@exept.de>
parents: 6012
diff changeset
  2347
932
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  2348
errorUnsupportedOperation
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2349
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2350
932
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  2351
    "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
  2352
8511
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2353
    ^ 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
  2354
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2355
    "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
  2356
!
f57ec42ceb44 added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents: 872
diff changeset
  2357
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2358
errorWriteOnly
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2359
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2360
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2361
    "report an error, that the stream is a writeOnly stream"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2362
8511
eb02df149f42 Raise proceedable exception proceedable.
Stefan Vogel <sv@exept.de>
parents: 8472
diff changeset
  2363
    ^ 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
  2364
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2365
    "Modified: / 8.5.1999 / 20:12:20 / cg"
2
claus
parents: 1
diff changeset
  2366
!
claus
parents: 1
diff changeset
  2367
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2368
ioError
3474
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  2369
    "report an error, that some I/O error occured.
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  2370
     (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
  2371
     which is no longer available and has been mounted soft)"
1af4d150dab4 added extra streamIOError signal.
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
  2372
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2373
    "{ Pragma: +optSpace }"
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2374
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2375
    self ioError:lastErrorNumber
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2376
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2377
    "Modified: / 8.5.1999 / 20:12:16 / cg"
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2378
!
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2379
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2380
ioError:errorNumber
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2381
    "report an error, that some I/O error occured.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2382
     (for example, a device-IO-error, or reading an NFS-dir,
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2383
     which is no longer available and has been mounted soft)"
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2384
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2385
    "{ Pragma: +optSpace }"
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2386
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2387
    ^ StreamIOError newException
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2388
	errorCode:errorNumber;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2389
	osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber);
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2390
	parameter:self;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2391
	raiseRequest
4161
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2392
3f2758cfeff5 raise errors in thisContext (not in sender context)
Claus Gittinger <cg@exept.de>
parents: 4152
diff changeset
  2393
    "Modified: / 8.5.1999 / 20:12:16 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2394
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2395
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2396
lastErrorNumber
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2397
    "return the last error"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2398
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2399
    ^ lastErrorNumber
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2400
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2401
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2402
lastErrorString
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2403
    "return a message string describing the last error"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2404
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2405
    (lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2406
	^ 'I/O error'
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2407
    ].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2408
    ^ OperatingSystem errorTextForNumber:lastErrorNumber
2
claus
parents: 1
diff changeset
  2409
!
claus
parents: 1
diff changeset
  2410
10932
c9148f19b2ea add #lastErrorSymbol
Stefan Vogel <sv@exept.de>
parents: 10852
diff changeset
  2411
lastErrorSymbol
c9148f19b2ea add #lastErrorSymbol
Stefan Vogel <sv@exept.de>
parents: 10852
diff changeset
  2412
    "return an error symbol describing the last error"
c9148f19b2ea add #lastErrorSymbol
Stefan Vogel <sv@exept.de>
parents: 10852
diff changeset
  2413
c9148f19b2ea add #lastErrorSymbol
Stefan Vogel <sv@exept.de>
parents: 10852
diff changeset
  2414
    ^ OperatingSystem errorSymbolForNumber:lastErrorNumber
c9148f19b2ea add #lastErrorSymbol
Stefan Vogel <sv@exept.de>
parents: 10852
diff changeset
  2415
!
c9148f19b2ea add #lastErrorSymbol
Stefan Vogel <sv@exept.de>
parents: 10852
diff changeset
  2416
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2417
openError
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2418
    "report an error, that the open failed"
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2419
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2420
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2421
14438
a4280b8038c0 changed:
Stefan Vogel <sv@exept.de>
parents: 14068
diff changeset
  2422
    ^ self openError:lastErrorNumber.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2423
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2424
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2425
openError:errorNumber
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2426
    "report an error, that the open failed"
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2427
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2428
    "{ Pragma: +optSpace }"
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2429
15496
21bb54fa8521 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15494
diff changeset
  2430
    |exClass errorHolder|
21bb54fa8521 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15494
diff changeset
  2431
21bb54fa8521 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15494
diff changeset
  2432
    errorHolder := OperatingSystem errorHolderForNumber:errorNumber.
21bb54fa8521 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15494
diff changeset
  2433
21bb54fa8521 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15494
diff changeset
  2434
    exClass := (errorHolder errorCategory == #nonexistentSignal)
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2435
	ifTrue:[ FileDoesNotExistException ]
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2436
	ifFalse:[ OpenError ].
13673
744194003e48 changed:
Claus Gittinger <cg@exept.de>
parents: 13306
diff changeset
  2437
744194003e48 changed:
Claus Gittinger <cg@exept.de>
parents: 13306
diff changeset
  2438
    ^ exClass newException
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2439
	errorCode:errorNumber;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2440
	osErrorHolder:errorHolder;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2441
	"/ cg: initialized lazyly - see OpenError>>#description
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2442
	"/ errorString:(' : ' , errorHolder errorString);
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2443
	parameter:self;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2444
	raiseRequest
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2445
	"/ in:thisContext sender
13673
744194003e48 changed:
Claus Gittinger <cg@exept.de>
parents: 13306
diff changeset
  2446
744194003e48 changed:
Claus Gittinger <cg@exept.de>
parents: 13306
diff changeset
  2447
    "Modified: / 09-09-2011 / 07:22:49 / cg"
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2448
!
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  2449
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2450
readError
15845
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2451
    "report an error, that some read error occured"
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2452
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2453
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2454
15845
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2455
    ^ self readError:lastErrorNumber
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2456
!
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2457
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2458
readError:errorNumber
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2459
    "report an error, that some read error occured"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2460
15845
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2461
    "{ Pragma: +optSpace }"
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2462
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2463
    ^ ReadError newException
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2464
	errorCode:errorNumber;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2465
	osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber);
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2466
	parameter:self;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2467
	raiseRequest
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2468
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2469
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2470
writeError
15845
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2471
    "report an error, that some write error occured"
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2472
1119
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2473
    "{ Pragma: +optSpace }"
956d62a5656c compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
  2474
15845
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2475
    ^ self writeError:lastErrorNumber
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2476
!
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2477
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2478
writeError:errorNumber
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2479
    "report an error, that some write error occured"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2480
15845
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2481
    "{ Pragma: +optSpace }"
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2482
2b732fc8a4f4 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15655
diff changeset
  2483
    ^ WriteError newException
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2484
	errorCode:errorNumber;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2485
	osErrorHolder:(OperatingSystem errorHolderForNumber:errorNumber);
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2486
	parameter:self;
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2487
	raiseRequest
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2488
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2489
6461
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2490
!ExternalStream methodsFor:'finalization'!
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2491
15468
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2492
executor
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2493
    "return a copy for finalization-registration;
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2494
     since all we need at finalization time is the fileDescriptor,
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2495
     a cheaper copy is possible."
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2496
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2497
    ^ self class basicNew setAccessor:handleType to:handle
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2498
!
816f9e81a546 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15452
diff changeset
  2499
6461
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2500
finalizationLobby
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2501
    "answer the registry used for finalization.
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2502
     ExternalStreams have their own Registry"
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2503
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2504
    ^ Lobby
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2505
!
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2506
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2507
finalize
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2508
    "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
  2509
16551
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  2510
    "/ with timeout to avoid blocking in a bad pty/socket
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  2511
    [
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
  2512
	self closeFile
16551
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  2513
    ] valueWithTimeout:30 seconds.
6461
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2514
! !
f5efaff3457b Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6441
diff changeset
  2515
15918
d5fe9b9e87d3 Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 15896
diff changeset
  2516
!ExternalStream methodsFor:'initialization'!
d5fe9b9e87d3 Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 15896
diff changeset
  2517
d5fe9b9e87d3 Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 15896
diff changeset
  2518
initialize
15921
9941c0e5132a initialize methods
Stefan Vogel <sv@exept.de>
parents: 15920
diff changeset
  2519
    position := 0.
9941c0e5132a initialize methods
Stefan Vogel <sv@exept.de>
parents: 15920
diff changeset
  2520
    binary := false.
9941c0e5132a initialize methods
Stefan Vogel <sv@exept.de>
parents: 15920
diff changeset
  2521
    buffered := true.
9941c0e5132a initialize methods
Stefan Vogel <sv@exept.de>
parents: 15920
diff changeset
  2522
    eolMode := DefaultEOLMode.
9941c0e5132a initialize methods
Stefan Vogel <sv@exept.de>
parents: 15920
diff changeset
  2523
    hitEOF := false.
15918
d5fe9b9e87d3 Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 15896
diff changeset
  2524
! !
d5fe9b9e87d3 Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 15896
diff changeset
  2525
2
claus
parents: 1
diff changeset
  2526
!ExternalStream methodsFor:'line reading/writing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2527
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2528
nextLine
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2529
    "read the next line (characters up to newline).
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2530
     Return a string containing those characters excluding the newline.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2531
     If the previous-to-last character is a cr, this is also removed,
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2532
     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
  2533
     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
  2534
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2535
    |line error|
971
eb70f5674303 max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents: 933
diff changeset
  2536
12873
f6b24aa46bb1 changed: #nextLine
Stefan Vogel <sv@exept.de>
parents: 12710
diff changeset
  2537
%{  /* STACK:100000 */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2538
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2539
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  2540
    int len, ret;
3029
aa14768e0e6a larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
  2541
    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
  2542
    char *rslt, *nextPtr, *limit;
22
847106305963 *** empty log message ***
claus
parents: 12
diff changeset
  2543
    int fd, ch;
1068
92fdcccf12a3 alles MIST - zurueck zu 1.89 und NOCONTEXT entfernt
Claus Gittinger <cg@exept.de>
parents: 1067
diff changeset
  2544
    int _buffered;
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  2545
    OBJ fp;
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  2546
    int lineTooLong = 0;
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2547
    int cutOff = 0;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2548
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  2549
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2550
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2551
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2552
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2553
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2554
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
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
  2555
	if (((fp = __INST(handle)) != nil)
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
  2556
	    && (__INST(mode) != @symbol(writeonly))
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
  2557
	    && (__INST(binary) != true)
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
  2558
	) {
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
  2559
	    f = __FILEVal(fp);
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
  2560
	    buffer[0] = '\0';
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
  2561
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
  2562
	    _buffered = (__INST(buffered) == true);
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
  2563
	    if (_buffered) {
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
  2564
		__READING__(f);
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
  2565
	    }
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
  2566
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
  2567
	    rslt = nextPtr = buffer;
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
  2568
	    limit = buffer + sizeof(buffer) - 2;
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
  2569
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
  2570
	    for (;;) {
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
  2571
		__READBYTE__(ret, f, nextPtr, _buffered, __INST(handleType));
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
  2572
		if (ret <= 0) {
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
  2573
		    if (nextPtr == buffer)
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
  2574
			rslt = NULL;
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
  2575
		    if (ret == 0) {
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
  2576
			__INST(hitEOF) = true;
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
  2577
			break;
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
  2578
		    } else {
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
  2579
			error = __mkSmallInteger(__threadErrno);
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
  2580
			goto err;
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
  2581
		    }
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
  2582
		}
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
  2583
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
  2584
		if (*nextPtr == '\n') {
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
  2585
		    cutOff = 1;
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
  2586
		    *nextPtr = '\0';
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
  2587
		    break;
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
  2588
		}
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
  2589
		if (*nextPtr == '\r') {
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
  2590
		    char peekChar;
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
  2591
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
  2592
		    /*
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
  2593
		     * peek ahead for a newLine ...
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
  2594
		     */
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
  2595
		    __READBYTE__(ret, f, &peekChar, _buffered, __INST(handleType));
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
  2596
		    if (ret <= 0) {
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
  2597
			cutOff = 1;
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
  2598
			*nextPtr = '\0';
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
  2599
			if (ret == 0) {
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
  2600
			    __INST(hitEOF) = true;
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
  2601
			    break;
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
  2602
			}
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
  2603
			error = __mkSmallInteger(__threadErrno);
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
  2604
			goto err;
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
  2605
		    }
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
  2606
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
  2607
		    if (peekChar == '\n') {
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
  2608
			cutOff = 2;
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
  2609
			*nextPtr = '\0';
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
  2610
			break;
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
  2611
		    }
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
  2612
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
  2613
		    __UNGETC__(peekChar, f, _buffered);
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
  2614
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
  2615
		    cutOff = 1;
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
  2616
		    *nextPtr = '\0';
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
  2617
		    break;
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
  2618
		}
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
  2619
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
  2620
		nextPtr++;
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
  2621
		if (nextPtr >= limit) {
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
  2622
		    *nextPtr = '\0';
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
  2623
		    lineTooLong = 1;
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
  2624
		    if (@global(InfoPrinting) == true) {
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
  2625
			fprintf(stderr, "ExtStream [warning]: line truncated in nextLine\n");
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
  2626
		    }
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
  2627
		    break;
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
  2628
		}
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
  2629
	    }
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
  2630
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
  2631
	    if (rslt != NULL) {
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
  2632
		len = nextPtr-buffer;
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
  2633
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
  2634
		if (__isSmallInteger(__INST(position))) {
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
  2635
		    INT np = __intVal(__INST(position)) + len + cutOff;
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
  2636
		    OBJ t;
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
  2637
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
  2638
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
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
  2639
		} else {
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
  2640
		    __INST(position) = nil; /* i.e. do not know */
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
  2641
		}
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
  2642
		/* remove any EOL character */
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
  2643
		if (len != 0) {
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
  2644
		    if (buffer[len-1] == '\n') {
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
  2645
			buffer[--len] = '\0';
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
  2646
		    }
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
  2647
		    if ((len != 0) && (buffer[len-1] == '\r')) {
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
  2648
			buffer[--len] = '\0';
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
  2649
		    }
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
  2650
		}
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
  2651
		line = __MKSTRING_L(buffer, len);
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
  2652
		if (! lineTooLong) {
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
  2653
		    RETURN ( line );
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
  2654
		}
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
  2655
	    }
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
  2656
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2657
    }
1478
aae07192b7d1 labels in empty statements need a semi
Claus Gittinger <cg@exept.de>
parents: 1469
diff changeset
  2658
err: ;
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  2659
%}.
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  2660
    line notNil ifTrue:[
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
  2661
	"/ the line as read is longer than 32k characters (boy - what a line)
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
  2662
	"/ The exception could be handled by reading more and returning the
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
  2663
	"/ concatenation in your exception handler (the receiver and the partial
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
  2664
	"/ line are passed as parameter)
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
  2665
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
  2666
	LineTooLongErrorSignal isHandled ifTrue:[
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
  2667
	    ^ LineTooLongErrorSignal
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
  2668
		raiseRequestWith:(Array with:self with:line)
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
  2669
		     errorString:('line too long read error')
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
  2670
	].
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
  2671
	^ line , self nextLine
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  2672
    ].
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  2673
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  2674
    (hitEOF == true) ifTrue:[^ self pastEndRead].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2675
    error notNil ifTrue:[
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
  2676
	lastErrorNumber := error.
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
  2677
	^ self readError:error
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2678
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2679
    handle isNil ifTrue:[^ self errorNotOpen].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2680
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
5431
24ce291c71f2 migration support
Claus Gittinger <cg@exept.de>
parents: 5430
diff changeset
  2681
    (binary == true) ifTrue:[^ self errorBinary].
24ce291c71f2 migration support
Claus Gittinger <cg@exept.de>
parents: 5430
diff changeset
  2682
    ^ super nextLine
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2683
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2684
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2685
nextPutLine:aString
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2686
    "write the characters in aString and append an end-of-Line marker
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2687
     (LF, CR or CRLF - depending in the setting of eolMode)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2688
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2689
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  2690
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2691
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  2692
    int len, cnt, len1, _buffered;
7875
eb7d48b0425d made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents: 7832
diff changeset
  2693
    OBJ fp;
537
dc4dad8a5ddd many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2694
    char *cp;
2513
a589586a3828 big bug: READBYTES & WRITEBYTES did not care for moving objects when interrupted.
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  2695
    int o_offs;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2696
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  2697
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2698
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2699
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2700
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2701
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  2702
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2703
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2704
	    && (__INST(mode) != @symbol(readonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2705
	    && (__INST(binary) != true)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2706
	    && __isStringLike(aString)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2707
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2708
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2709
	    len = __stringSize(aString);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2710
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2711
	    if (_buffered = (__INST(buffered) == true)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2712
		__WRITING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2713
	    }
9053
f3f3d48d6f44 fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents: 9024
diff changeset
  2714
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2715
	    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2716
		cnt = __win32_fwrite(__stringVal(aString), 1, len, f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2717
	    } else
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2718
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2719
	    {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2720
		o_offs = (char *)__stringVal(aString)-(char *)aString;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2721
		__WRITEBYTES_OBJ__(cnt, f, aString, o_offs, len, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2722
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2723
	    if (cnt == len) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2724
		OBJ mode = __INST(eolMode);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2725
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2726
		len1 = len;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2727
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2728
		if (mode == @symbol(cr)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2729
		    cp = "\r"; len = 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2730
		} else if (mode == @symbol(crlf)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2731
		    cp = "\r\n"; len = 2;
17430
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  2732
		} else if (mode == @symbol(eot)) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  2733
		    cp = "\004"; len = 1;
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  2734
		} else if (mode == @symbol(etx)) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  2735
		    cp = "\003"; len = 1;
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2736
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2737
		    cp = "\n"; len = 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2738
		}
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2739
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2740
		if ((f == __win32_stdout()) || (f == __win32_stderr())) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2741
		    cnt = __win32_fwrite(cp, 1, len, f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2742
		} else
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2743
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2744
		{
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2745
		    __WRITEBYTES__(cnt, f, cp, len, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2746
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2747
		if (cnt > 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2748
		    if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2749
			INT np = __intVal(__INST(position)) + cnt;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2750
			OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2751
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2752
			t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2753
		    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2754
			__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2755
		    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2756
		    RETURN ( self );
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2757
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2758
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2759
	    error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2760
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2761
    }
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  2762
%}.
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2763
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2764
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2765
	self writeError:error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2766
	^ self
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2767
    ].
2045
1ad17ca6520f fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
  2768
    super nextPutLine:aString.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2769
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2770
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2771
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2772
    "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
  2773
     and append all lines to self.
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  2774
     Can be used to copy/create large files or copy from a pipe/socket.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2775
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2776
     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
  2777
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2778
    |line|
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2779
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2780
    [aStream atEnd] whileFalse:[
8070
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2781
	line := aStream nextLine.
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2782
	line isNil ifTrue:[
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2783
	    ^ self.
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2784
	].
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2785
	self nextPutLine:line.
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2786
	(aStringOrNil notNil and:[line startsWith:aStringOrNil]) ifTrue:[
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2787
	    ^ self
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2788
	]
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2789
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2790
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2791
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2792
peekForLineStartingWith:aString
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2793
    "read ahead for next line starting with aString;
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  2794
     return the line-string if found, or nil if EOF is encountered.
7118
10b067555add *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7054
diff changeset
  2795
     If matched, do not advance position beyond that line
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  2796
     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
  2797
     If not matched, reposition to original position for further reading."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2798
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2799
    |firstPos lastPos line|
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2800
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2801
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2802
    handle isNil ifTrue:[^ self errorNotOpen].
2
claus
parents: 1
diff changeset
  2803
    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
  2804
    buffered ifFalse:[^ self errorNotBuffered].
12
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
  2805
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2806
    firstPos := self position.
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2807
    [self atEnd] whileFalse:[
8070
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2808
	lastPos := self position.
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2809
	line := self nextLine.
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2810
	line isNil ifTrue:[
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2811
	    self position:firstPos.
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2812
	    ^ nil
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2813
	].
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2814
	(line startsWith:aString) ifTrue:[
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2815
	    self position:lastPos.
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2816
	    ^ line
2fec8e67e119 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8069
diff changeset
  2817
	]
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2818
    ].
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2819
    self position:firstPos.
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  2820
    ^ nil
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2821
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2822
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2823
peekForLineStartingWithAny:aCollectionOfStrings
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2824
    "read ahead for next line starting with any of aCollectionOfStrings;
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2825
     return the index in aCollection if found, nil otherwise..
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2826
     If no match, do not change position; otherwise advance right before the
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2827
     matched line so that nextLine will return this line."
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2828
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2829
    |line startPos linePos index|
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2830
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2831
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2832
    handle isNil ifTrue:[^ self errorNotOpen].
2
claus
parents: 1
diff changeset
  2833
    binary ifTrue:[^ self errorBinary].
claus
parents: 1
diff changeset
  2834
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2835
    startPos := self position.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2836
    [self atEnd] whileFalse:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2837
	linePos := self position.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2838
	line := self nextLine.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2839
	line notNil ifTrue:[
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2840
	    index := 1.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2841
	    aCollectionOfStrings do:[:prefix |
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2842
		(line startsWith:prefix) ifTrue:[
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2843
		    self position:linePos.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2844
		    ^ index
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2845
		].
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2846
		index := index + 1
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2847
	    ]
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2848
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2849
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2850
    self position:startPos.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2851
    ^ nil
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2852
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2853
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2854
!ExternalStream methodsFor:'misc functions'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2855
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2856
async:aBoolean
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2857
    "set/clear the async attribute - if set, the availability of data on
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2858
     the receiver will trigger an ioInterrupt.
779
0e41a665038a commentary
Claus Gittinger <cg@exept.de>
parents: 730
diff changeset
  2859
     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
  2860
     Notice:
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  2861
	not every OS supports this
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2862
	- check with OS>>supportsIOInterrupts before using"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2863
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2864
    |fd|
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2865
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2866
    handle isNil ifTrue:[^ self errorNotOpen].
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  2867
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  2868
    OperatingSystem supportsIOInterrupts ifFalse:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2869
	^ self errorUnsupportedOperation
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  2870
    ].
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  2871
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2872
    fd := self fileDescriptor.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2873
    aBoolean ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  2874
	^ OperatingSystem enableIOInterruptsOn:fd
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2875
    ].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2876
    ^ OperatingSystem disableIOInterruptsOn:fd
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  2877
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  2878
    "Modified: 11.1.1997 / 17:50:21 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2879
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2880
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2881
blocking:aBoolean
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2882
    "set/clear the blocking attribute - if set (which is the default)
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2883
     a read (using next) on the receiver will block until data is available.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2884
     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
  2885
     nil.
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  2886
     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
  2887
     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
  2888
     in advance. However, the data must then be read using #nextBytes:
8616
6014ed5c67f9 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
  2889
     methods, in order to avoid other (pastEndRead) exceptions."
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2890
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2891
    handle isNil ifTrue:[^ self errorNotOpen].
7499
53b7a86e1f0b ExternalStream>>blocking: returns previous blocking status
Stefan Vogel <sv@exept.de>
parents: 7497
diff changeset
  2892
    ^ 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
  2893
3174
668414a73de9 Typo in comment
Stefan Vogel <sv@exept.de>
parents: 3132
diff changeset
  2894
    "Modified: / 11.1.1997 / 17:48:01 / cg"
668414a73de9 Typo in comment
Stefan Vogel <sv@exept.de>
parents: 3132
diff changeset
  2895
    "Modified: / 15.1.1998 / 11:49:48 / stefan"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2896
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2897
17618
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2898
copyToEndFrom:inStream
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2899
    "read from inStream, and write all data up to the end to the receiver.
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2900
     Return the number of bytes which have been transferred.
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2901
     Same functionality as copyToEnd:, but reversed arg and receiver
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2902
     (useful in a cascade message of the writeStream)"
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2903
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2904
    |bufferSize|
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2905
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2906
    OperatingSystem isMSDOSlike ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2907
	"/ mhmh - NT hangs, when copying bigger blocks to a network drive - why ?
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2908
	bufferSize := 1 * 1024.
17618
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2909
    ] ifFalse:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  2910
	bufferSize := 8 * 1024.
17618
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2911
    ].
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2912
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2913
    ^ inStream copyToEndInto:self bufferSize:bufferSize
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2914
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2915
    "
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2916
     |in out|
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2917
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2918
     in := 'Makefile' asFilename readStream.
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2919
     out := Stdout.
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2920
     in copyToEndInto:out.
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2921
     in close.
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2922
    "
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2923
!
c6ee18f841cc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17609
diff changeset
  2924
6804
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  2925
copyToEndInto:outStream
7017
03f1bd57c001 no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 7013
diff changeset
  2926
    "copy the data into another stream."
03f1bd57c001 no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 7013
diff changeset
  2927
8362
e17d9ffb6fa8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8339
diff changeset
  2928
    |bufferSize|
7700
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2929
6804
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  2930
    OperatingSystem isMSDOSlike ifTrue:[
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2931
	"/ mhmh - NT hangs, when copying bigger blocks to a network drive - why ?
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2932
	bufferSize := 1 * 1024.
6804
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  2933
    ] ifFalse:[
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  2934
	bufferSize := 8 * 1024.
6804
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  2935
    ].
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  2936
7017
03f1bd57c001 no one cares about the signal in copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 7013
diff changeset
  2937
    ^ self copyToEndInto:outStream bufferSize:bufferSize
7700
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2938
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2939
    "
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2940
     |in out|
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2941
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2942
     in := 'Makefile' asFilename readStream.
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2943
     out := Stdout.
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2944
     in copyToEndInto:out.
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2945
     in close.
767e11efc326 use sendfile if supported by OS
Claus Gittinger <cg@exept.de>
parents: 7685
diff changeset
  2946
    "
6804
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  2947
!
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  2948
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2949
create
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2950
    "create the stream
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2951
     - this must be redefined in subclass"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2952
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2953
    ^ self subclassResponsibility
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2954
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2955
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2956
ioctl:ioctlNumber
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2957
    "to provide a simple ioctl facility - an ioctl is performed
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2958
     on the underlying file; no arguments are passed."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  2959
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2960
    ^ self ioctl:ioctlNumber with:nil
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2961
!
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2962
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2963
ioctl:ioctlNumber with:arg
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2964
    "to provide a simple ioctl facility - an ioctl is performed
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2965
     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
  2966
     This is not used by ST/X, but provided for special situations
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2967
     - for example, to control proprietrary I/O devices.
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2968
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2969
     Since the type of the argument depends on the ioctl being
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2970
     performed, different arg types are allowed here.
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2971
     If the argument is nil, an ioctl without argument is performed.
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2972
     If the argument is an integral number, it's directly passed;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2973
     if it's a kind of ByteArray (ByteArray, String or Structure),
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2974
     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
  2975
     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
  2976
     This allows performing most ioctls
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2977
     - however, it might be tricky to setup the buffer.
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2978
     Be careful in what you pass - ST/X cannot validate its correctness."
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2979
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2980
    |error|
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2981
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  2982
%{
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  2983
#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
  2984
    int fd;
10552
d52aa8e066e5 #ioctl:with: accept 32-bit integers as arguments
Stefan Vogel <sv@exept.de>
parents: 10469
diff changeset
  2985
    int ret;
d52aa8e066e5 #ioctl:with: accept 32-bit integers as arguments
Stefan Vogel <sv@exept.de>
parents: 10469
diff changeset
  2986
    unsigned int ioNum;
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  2987
    INT ioArg;
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2988
    OBJ fp = __INST(handle);
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2989
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2990
    if (fp == nil)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2991
	goto out;
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2992
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  2993
    if (!__isInteger(ioctlNumber)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2994
	 || (!__isInteger(arg)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2995
	     && (arg != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2996
	     && !__isBytes(arg)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2997
	     && !__isExternalBytesLike(arg)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2998
	     && !__isExternalAddress(arg))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  2999
	error = @symbol(badArgument);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3000
	goto out;
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3001
    }
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3002
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3003
    if (__INST(handleType) == @symbol(socketHandle)) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3004
	fd = __FILEVal(fp);
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3005
    } else if ((__INST(handleType) == nil)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3006
	 || (__INST(handleType) == @symbol(filePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3007
	 || (__INST(handleType) == @symbol(socketFilePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3008
	 || (__INST(handleType) == @symbol(pipeFilePointer))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3009
	fd = fileno(__FILEVal(fp));
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3010
    } else {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3011
	error = @symbol(badHandleType);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3012
	goto out;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3013
    }
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3014
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3015
    ioNum = __unsignedLongIntVal(ioctlNumber);
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3016
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3017
    __BEGIN_INTERRUPTABLE__
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3018
    do {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3019
	__threadErrno = 0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3020
	if (arg == nil) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3021
	    ioArg = 0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3022
	} else if (__isSmallInteger(arg)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3023
	    ioArg = __intVal(arg);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3024
	} else if (__isInteger(arg)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3025
	    ioArg = __unsignedLongIntVal(arg);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3026
	} else if (__isExternalBytesLike(arg)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3027
	    ioArg = (INT)(__externalBytesAddress(arg));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3028
	} else if (__isExternalAddress(arg)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3029
	    ioArg = (INT)(__externalAddressVal(arg));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3030
	} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3031
	    ioArg = (INT)(__ByteArrayInstPtr(arg)->ba_element);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3032
	}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3033
	ret = ioctl(fd, ioNum, ioArg);
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3034
    } while (ret < 0 && __threadErrno == EINTR);
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3035
    __END_INTERRUPTABLE__
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 (ret >= 0) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3038
	RETURN ( __mkSmallInteger(ret) );
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3039
    }
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3040
    error = __mkSmallInteger(__threadErrno);
791
b1c153b1c719 WIN32 has no ioctl
Claus Gittinger <cg@exept.de>
parents: 779
diff changeset
  3041
#endif
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3042
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3043
out:;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3044
%}.
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3045
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3046
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3047
	^ self ioError:error.
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3048
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3049
    handle isNil ifTrue:[^ self errorNotOpen].
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3050
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3051
    "/ the system does not support ioctl (MSDOS or VMS)
2920
9c10b66e1ba8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2903
diff changeset
  3052
    ^ self errorUnsupportedOperation
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3053
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3054
16551
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3055
reset
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3056
    "set the read position to the beginning of the collection"
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3057
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3058
    self position:0
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3059
!
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3060
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3061
setToEnd
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3062
    "redefined since it must be implemented differently"
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3063
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3064
    ^ self subclassResponsibility
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3065
!
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3066
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3067
sync
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3068
    "make sure, that the OS writes cached data to the disk"
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3069
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3070
    |error|
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3071
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3072
    self flush.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3073
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3074
%{
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3075
#if !defined(__openVMS__)
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3076
    int fd;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3077
    int ret;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3078
    OBJ fp = __INST(handle);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3079
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3080
    if (fp == nil)
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3081
	goto out;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3082
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3083
    if (__INST(handleType) == @symbol(socketHandle)) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3084
	fd = __FILEVal(fp);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3085
    } else if ((__INST(handleType) == nil)
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3086
	       || (__INST(handleType) == @symbol(filePointer))
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3087
	       || (__INST(handleType) == @symbol(socketFilePointer))
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3088
	       || (__INST(handleType) == @symbol(pipeFilePointer))) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3089
	fd = fileno(__FILEVal(fp));
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3090
    } else {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3091
	error = @symbol(badHandleType);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3092
	goto out;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3093
    }
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3094
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3095
#ifdef WIN32
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3096
     __threadErrno = 0;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3097
     ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fd));
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3098
     if (ret) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3099
	 RETURN (self);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3100
     }
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3101
#else
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3102
     __BEGIN_INTERRUPTABLE__
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3103
     do {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3104
	 ret = fsync(fd);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3105
     } while ((ret < 0) && (__threadErrno == EINTR));
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3106
     __END_INTERRUPTABLE__
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3107
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3108
     if (ret >= 0) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3109
	 RETURN (self);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3110
     }
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3111
#endif /* ! WIN32 */
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3112
     error = __mkSmallInteger(__threadErrno);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3113
#endif /* ! __openVMS__ */
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3114
out:;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3115
%}.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3116
    error notNil ifTrue:[
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3117
	lastErrorNumber := error.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3118
	self ioError:error.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3119
	^ self.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3120
    ].
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3121
    handle isNil ifTrue:[self errorNotOpen].
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3122
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3123
    "
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3124
	|f|
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3125
	f := 'x' asFilename writeStream.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3126
	f nextPutAll:'hallo'; sync; syncData; close
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3127
    "
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3128
!
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3129
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3130
syncData
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3131
    "make sure, that the OS writes cached data to the disk.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3132
     In this case, metadata is only written, if it is
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3133
     required to read the file's data (so metadata will not be written,
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3134
     if only access/modification time has changed)."
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3135
    |error|
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3136
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3137
    self flush.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3138
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3139
%{
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3140
#if !defined(__openVMS__)
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3141
    int fd;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3142
    int ret;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3143
    OBJ fp = __INST(handle);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3144
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3145
    if (fp == nil)
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3146
	goto out;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3147
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3148
    if (__INST(handleType) == @symbol(socketHandle)) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3149
	fd = __FILEVal(fp);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3150
    } else if ((__INST(handleType) == nil)
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3151
	       || (__INST(handleType) == @symbol(filePointer))
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3152
	       || (__INST(handleType) == @symbol(socketFilePointer))
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3153
	       || (__INST(handleType) == @symbol(pipeFilePointer))) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3154
	fd = fileno(__FILEVal(fp));
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3155
    } else {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3156
	error = @symbol(badHandleType);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3157
	goto out;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3158
    }
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3159
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3160
#ifdef WIN32
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3161
     __threadErrno = 0;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3162
     ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fd));
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3163
     if (ret) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3164
	 RETURN (self);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3165
     }
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3166
#else
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3167
     __BEGIN_INTERRUPTABLE__
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3168
     do {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3169
	 ret = fdatasync(fd);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3170
     } while ((ret < 0) && (__threadErrno == EINTR));
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3171
     __END_INTERRUPTABLE__
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3172
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3173
     if (ret >= 0) {
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3174
	 RETURN (self);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3175
     }
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3176
#endif /* ! WIN32 */
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3177
     error = __mkSmallInteger(__threadErrno);
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3178
#endif /* ! __openVMS__ */
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3179
out:;
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3180
%}.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3181
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3182
    error notNil ifTrue:[
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3183
	lastErrorNumber := error.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3184
	self ioError:error.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3185
	^ self.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3186
    ].
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3187
    handle isNil ifTrue:[^ self errorNotOpen].
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3188
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3189
    "if notdef HAS_DATASYNC, fall back"
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3190
    self sync.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3191
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3192
    "
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3193
	|f|
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3194
	f := 'x' asFilename writeStream.
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3195
	f nextPutAll:'hallo'; sync; syncData; close
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3196
    "
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3197
!
352de37575d7 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 16509
diff changeset
  3198
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3199
tcgetattr
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3200
    "unix only:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3201
     provide the information of a tcgetattr call as a dictionary.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3202
     This provides terminal setting values of a tty or pty.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3203
     Used by terminal emulators.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3204
     On non-unix systems, an unsupported operation error is raised"
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3205
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3206
    |error ret
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3207
     iflags "/ input flags
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3208
     ignbrk brkint ignpar parmrk inpck istrip inlcr igncr
16509
2cf139885242 Fixed ExternalStream>>tcgetattr
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16508
diff changeset
  3209
     icrnl iuclc ixon ixoff ixany imaxbel iutf8
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3210
     oflags "/ output flags
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3211
     opost onlcr ocrnl olcuc onocr onlret oxtabs onoteot
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3212
     cflags "/ control flags
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3213
     csize cstopb cread parenb parodd hupcl clocal
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3214
     lflags "/ local flags
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3215
     echoke echoe echok echo echonl echoprt echoctl
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3216
     isig icanon altwerase iexten extproc tostop
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3217
     |
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3218
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3219
%{
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3220
#if !defined(MSDOS_LIKE) && !defined(__openVMS__)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3221
    int fd;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3222
    int _ret;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3223
    OBJ fp = __INST(handle);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3224
    struct termios t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3225
    int sz;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3226
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3227
    if (fp == nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3228
	goto out;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3229
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3230
    if (__INST(handleType) == @symbol(socketHandle)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3231
	fd = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3232
    } else if ((__INST(handleType) == nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3233
	 || (__INST(handleType) == @symbol(filePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3234
	 || (__INST(handleType) == @symbol(socketFilePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3235
	 || (__INST(handleType) == @symbol(pipeFilePointer))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3236
	fd = fileno(__FILEVal(fp));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3237
    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3238
	error = @symbol(badHandleType);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3239
	goto out;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3240
    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3241
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3242
    __BEGIN_INTERRUPTABLE__
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3243
    do {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3244
	__threadErrno = 0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3245
	_ret = tcgetattr(fd, &t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3246
    } while (_ret < 0 && __threadErrno == EINTR);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3247
    __END_INTERRUPTABLE__
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3248
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3249
    if (_ret < 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3250
	error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3251
    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3252
    ignbrk = t.c_iflag & IGNBRK ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3253
    brkint = t.c_iflag & BRKINT ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3254
    ignpar = t.c_iflag & IGNPAR ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3255
    parmrk = t.c_iflag & PARMRK ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3256
    inpck = t.c_iflag & INPCK ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3257
    istrip = t.c_iflag & ISTRIP ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3258
    inlcr = t.c_iflag & INLCR ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3259
    ixon = t.c_iflag & IXON ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3260
    ixoff = t.c_iflag & IXOFF ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3261
    ixany = t.c_iflag & IXANY ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3262
#ifdef IMAXBEL
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3263
    imaxbel = t.c_iflag & IMAXBEL ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3264
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3265
#ifdef IUCLC
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3266
    iuclc = t.c_iflag & IUCLC ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3267
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3268
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3269
    opost = t.c_oflag & OPOST ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3270
    onlcr = t.c_oflag & ONLCR ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3271
#ifdef OXTABS
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3272
    oxtabs = t.c_oflag & OXTABS ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3273
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3274
#ifdef ONOTEOT
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3275
    onoteot = t.c_oflag & ONOTEOT ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3276
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3277
    ocrnl = t.c_oflag & OCRNL ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3278
#ifdef OLCUC
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3279
    olcuc = t.c_oflag & OLCUC ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3280
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3281
    onocr = t.c_oflag & ONOCR ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3282
    onlret = t.c_oflag & ONLRET ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3283
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3284
    sz = t.c_cflag & CSIZE;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3285
    switch (sz) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3286
	case CS5:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3287
	    csize = @symbol(cs5);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3288
	    break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3289
	case CS6:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3290
	    csize = @symbol(cs6);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3291
	    break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3292
	case CS7:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3293
	    csize = @symbol(cs7);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3294
	    break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3295
	case CS8:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3296
	    csize = @symbol(cs8);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3297
	    break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3298
    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3299
    cstopb = t.c_cflag & CSTOPB ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3300
    cread = t.c_cflag & CREAD ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3301
    parenb = t.c_cflag & PARENB ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3302
    parodd = t.c_cflag & PARODD ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3303
    hupcl = t.c_cflag & HUPCL ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3304
    clocal = t.c_cflag & CLOCAL ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3305
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3306
    echoke = t.c_lflag & ECHOKE ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3307
    echoe = t.c_lflag & ECHOE ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3308
    echo = t.c_lflag & ECHO ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3309
    echonl = t.c_lflag & ECHONL ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3310
    echoctl = t.c_lflag & ECHOCTL ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3311
    isig = t.c_lflag & ISIG ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3312
    icanon = t.c_lflag & ICANON ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3313
#ifdef ALTWERASE
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3314
    altwerase = t.c_lflag & ALTWERASE ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3315
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3316
#ifdef IEXTEN
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3317
    iexten = t.c_lflag & IEXTEN ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3318
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3319
#ifdef EXTPROC
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3320
    extproc = t.c_lflag & EXTPROC ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3321
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3322
#ifdef TOSTOP
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3323
    tostop = t.c_lflag & TOSTOP ? true : false;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3324
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3325
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3326
#else
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3327
    error = @symbol(unsupportedOperation);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3328
#endif
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3329
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3330
out:;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3331
%}.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3332
    error notNil ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3333
	error == #unsupportedOperation ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3334
	    "/ the system does not support tcgetattr
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3335
	    ^ self errorUnsupportedOperation
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3336
	].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3337
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3338
	^ self ioError:error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3339
    ].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3340
    handle isNil ifTrue:[^ self errorNotOpen].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3341
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3342
    "/ construct a dictionary...
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3343
    ret := Dictionary new.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3344
    ret at:#iflags put:(iflags := Dictionary new).
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3345
    ret at:#oflags put:(oflags := Dictionary new).
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3346
    ret at:#cflags put:(cflags := Dictionary new).
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3347
    ret at:#lflags put:(lflags := Dictionary new).
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3348
    iflags at:#ignbrk put:ignbrk.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3349
    iflags at:#brkint put:brkint.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3350
    iflags at:#ignpar put:ignpar.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3351
    iflags at:#parmrk put:parmrk.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3352
    iflags at:#inpck put:inpck.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3353
    iflags at:#istrip put:istrip.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3354
    iflags at:#inlcr put:inlcr.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3355
    iflags at:#igncr put:igncr.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3356
    iflags at:#icrnl put:icrnl.
16509
2cf139885242 Fixed ExternalStream>>tcgetattr
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16508
diff changeset
  3357
    iflags at:#uiclc put:iuclc.
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3358
    iflags at:#ixon put:ixon.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3359
    iflags at:#ixoff put:ixoff.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3360
    iflags at:#ixany put:ixany.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3361
    iflags at:#imaxbel put:imaxbel.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3362
    iflags at:#iutf8 put:iutf8.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3363
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3364
    oflags at:#opost put:opost.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3365
    oflags at:#onlcr put:onlcr.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3366
    oflags at:#oxtabs put:oxtabs.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3367
    oflags at:#onoteot put:onoteot.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3368
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3369
    cflags at:#csize put:csize.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3370
    cflags at:#cstopb put:cstopb.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3371
    cflags at:#cread put:cread.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3372
    cflags at:#parenb put:parenb.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3373
    cflags at:#parodd put:parodd.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3374
    cflags at:#hupcl put:hupcl.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3375
    cflags at:#clocal put:clocal.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3376
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3377
    lflags at:#echoke put:echoke.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3378
    lflags at:#echoe put:echoe.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3379
    lflags at:#echok put:echok.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3380
    lflags at:#echo put:echo.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3381
    lflags at:#echonl put:echonl.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3382
    lflags at:#echoprt put:echoprt.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3383
    lflags at:#echoctl put:echoctl.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3384
    lflags at:#isig put:isig.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3385
    lflags at:#icanon put:icanon.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3386
    lflags at:#altwerase put:altwerase.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3387
    lflags at:#iexten put:iexten.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3388
    lflags at:#extproc put:extproc.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3389
    lflags at:#tostop put:tostop.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3390
    ^ ret
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3391
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3392
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3393
!ExternalStream methodsFor:'non homogenous reading'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3394
17609
b06cbfcc4a11 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17608
diff changeset
  3395
next:count into:aCollection startingAt:start
b06cbfcc4a11 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17608
diff changeset
  3396
    |readCount|
b06cbfcc4a11 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17608
diff changeset
  3397
b06cbfcc4a11 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17608
diff changeset
  3398
    readCount := self nextBytes:count into:aCollection startingAt:start.
b06cbfcc4a11 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17608
diff changeset
  3399
    readCount = count ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  3400
	^ aCollection.
17609
b06cbfcc4a11 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17608
diff changeset
  3401
    ].
b06cbfcc4a11 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17608
diff changeset
  3402
    ^ aCollection copyFrom:1 to:start+readCount-1.
15254
1d0a262c1743 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 14847
diff changeset
  3403
!
1d0a262c1743 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 14847
diff changeset
  3404
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3405
nextAvailable:count
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3406
    "return the next count elements of the stream as aCollection.
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3407
     If the stream reaches the end before count elements have been read,
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3408
     return what is available. (i.e. a shorter collection).
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3409
     The type of collection is specified in #contentsSpecies."
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3410
3956
51f1a9a4d63f changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents: 3949
diff changeset
  3411
    |buffer n|
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3412
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3413
    binary ifTrue:[
7832
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
  3414
	buffer := ByteArray uninitializedNew:count
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3415
    ] ifFalse:[
7832
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
  3416
	buffer := String uninitializedNew:count
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3417
    ].
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3418
    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
  3419
    n == 0 ifTrue:[
7832
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
  3420
	binary ifTrue:[
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  3421
	    ^ #[]
7832
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
  3422
	].
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
  3423
	^ ''
5202
e8130049b18f #nextAvailableBytes:into:startingAt: may NOT set EOF flag,
Claus Gittinger <cg@exept.de>
parents: 5187
diff changeset
  3424
    ].
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3425
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3426
    n ~~ count ifTrue:[
7832
f8a02752ca28 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7806
diff changeset
  3427
	^ buffer copyTo:n
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3428
    ].
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3429
    ^ buffer.
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3430
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3431
    "
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  3432
     (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
  3433
     (ReadStream on:#(1 2 3 4 5)) nextAvailable:10
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3434
     (ReadStream on:'hello') nextAvailable:3
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  3435
     (ReadStream on:'hello') nextAvailable:10
3674
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3436
    "
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3437
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3438
    "Modified: / 16.6.1998 / 15:52:41 / cg"
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3439
!
c51550d1df17 added #nextAvailable:
Claus Gittinger <cg@exept.de>
parents: 3672
diff changeset
  3440
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  3441
nextAvailable:count into:aCollection startingAt:pos
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  3442
    "return the next count elements of the stream into aCollection at position pos."
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  3443
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  3444
    ^ self nextAvailableBytes:count into:aCollection startingAt:pos.
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  3445
!
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  3446
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3447
nextAvailableBytes:count into:anObject startingAt:start
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3448
    "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
  3449
     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
  3450
     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
  3451
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3452
     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
  3453
     is raised if the connection is broken.
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3454
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3455
     Notice, that in contrast to other methods,
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3456
     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
  3457
     Thus allowing read of partial blocks.
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3458
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  3459
     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
  3460
     (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
  3461
     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
  3462
     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
  3463
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3464
     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
  3465
     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
  3466
     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
  3467
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3468
    |error|
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3469
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3470
%{
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3471
    FILEPOINTER f;
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3472
    int cnt, offs, ret, _buffered;
15987
0fbfbdf4a0a5 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15921
diff changeset
  3473
    int objSize, nInstBytes;
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3474
    char *cp;
3445
2639c1e3fd0b nextAvail also for externalBytes
Claus Gittinger <cg@exept.de>
parents: 3432
diff changeset
  3475
    char *extPtr;
7195
755c46f9b774 also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents: 7118
diff changeset
  3476
    OBJ fp;
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3477
    int o_offs;
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3478
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3479
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3480
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3481
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3482
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3483
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3484
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3485
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3486
	    && (__INST(mode) != @symbol(writeonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3487
	    && __bothSmallInteger(count, start)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3488
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3489
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3490
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3491
	    cnt = __intVal(count);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3492
	    offs = __intVal(start) - 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3493
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3494
	    if (__isExternalBytesLike(anObject)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3495
		OBJ sz;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3496
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3497
		nInstBytes = 0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3498
		extPtr = (char *)(__externalBytesAddress(anObject));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3499
		if (extPtr == NULL) goto bad;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3500
		sz = __externalBytesSize(anObject);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3501
		if (__isSmallInteger(sz)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3502
		    objSize = __intVal(sz);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3503
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3504
		    objSize = 0; /* unknown */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3505
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3506
	    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3507
		OBJ oClass = __Class(anObject);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3508
		int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3509
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3510
		nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3511
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3512
		switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3513
		    case BYTEARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3514
		    case WORDARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3515
		    case LONGARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3516
		    case SWORDARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3517
		    case SLONGARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3518
		    case FLOATARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3519
			break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3520
		    case DOUBLEARRAY:
15987
0fbfbdf4a0a5 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15921
diff changeset
  3521
#ifdef __NEED_DOUBLE_ALIGN
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3522
			nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1);
15987
0fbfbdf4a0a5 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15921
diff changeset
  3523
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3524
			break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3525
		    case LONGLONGARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3526
		    case SLONGLONGARRAY:
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3527
#ifdef __NEED_LONGLONG_ALIGN
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3528
			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
  3529
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3530
			break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3531
		    default:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3532
			goto bad;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3533
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3534
		extPtr = (char *)0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3535
		objSize = __Size(anObject) - nInstBytes;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3536
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3537
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3538
	    if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3539
		_buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3540
		if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3541
		    __READING__(f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3542
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3543
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3544
		if (extPtr) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3545
		    __READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3546
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3547
		    /*
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3548
		     * on interrupt, anObject may be moved to another location.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3549
		     * So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__,
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3550
		     * to get a new address.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3551
		     */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3552
		    offs += nInstBytes;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3553
		    __READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3554
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3555
		/* 0 is NOT an EOF condition here ... */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3556
		if (ret >= 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3557
		    if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3558
			INT np = __intVal(__INST(position)) + ret;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3559
			OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3560
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3561
			t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3562
		    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3563
			__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3564
		    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3565
		    RETURN (__mkSmallInteger(ret));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3566
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3567
		__INST(position) = nil;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3568
		error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3569
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3570
	}
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3571
    }
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3572
bad: ;
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3573
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  3574
    (hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3575
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3576
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3577
	^ self readError:error
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3578
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3579
    handle isNil ifTrue:[^ self errorNotOpen].
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3580
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3581
    "
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3582
     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
  3583
    "
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3584
    ^ self primitiveFailed
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3585
!
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3586
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3587
nextByte
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3588
    "read the next byte and return it as an Integer; return nil on error.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3589
     This is allowed in both text and binary modes, always returning the
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3590
     bytes binary value as an integer in 0..255."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3591
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3592
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  3593
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  3594
    FILEPOINTER f;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3595
    unsigned char byte;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  3596
    int ret, _buffered;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3597
    OBJ fp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3598
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  3599
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3600
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3601
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3602
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3603
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3604
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3605
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3606
	 && (__INST(mode) != @symbol(writeonly))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3607
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3608
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3609
	    _buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3610
	    if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3611
		__READING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3612
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3613
	    __READBYTE__(ret, f, &byte, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3614
	    if (ret > 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3615
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3616
		    INT np = __intVal(__INST(position)) + 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3617
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3618
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3619
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3620
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3621
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3622
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3623
		RETURN (__mkSmallInteger(byte));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3624
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3625
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3626
	    if (ret == 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3627
		__INST(hitEOF) = true;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3628
	    } else /* ret < 0 */ {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3629
		__INST(position) = nil;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3630
		error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3631
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3632
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3633
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3634
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  3635
    hitEOF ifTrue:[^ self pastEndRead].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3636
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3637
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3638
	^ self readError:error
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3639
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3640
    handle isNil ifTrue:[^ self errorNotOpen].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3641
    ^ self errorWriteOnly
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3642
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3643
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3644
nextBytes:count into:anObject startingAt:start
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3645
    "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
  3646
     bytes read or the number of bytes read, if EOF is encountered before.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3647
     If the receiver is some socket/pipe-like stream, an exception
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3648
     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
  3649
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3650
     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
  3651
     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
  3652
     to only read whats there.
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  3653
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  3654
     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
  3655
     this does NOT return nil on EOF, but the actual count.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3656
     Thus allowing read of partial blocks.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3657
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  3658
     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
  3659
     (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
  3660
     or an externalBytes object (with known size).
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3661
     If anObject is a string or byteArray and reused, this provides the
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3662
     fastest possible physical I/O (since no new objects are allocated).
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3663
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3664
     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
  3665
     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
  3666
     architectures (unless you prepared the buffer with care),
1228d4ba9518 allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 3420
diff changeset
  3667
     since it does not care for byte order or float representation."
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3668
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3669
    |error|
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3670
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  3671
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  3672
    int cnt, offs, ret, _buffered;
15987
0fbfbdf4a0a5 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15921
diff changeset
  3673
    int objSize, nInstBytes;
3432
1228d4ba9518 allow reading/writing from/to ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 3420
diff changeset
  3674
    char *extPtr;
7195
755c46f9b774 also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents: 7118
diff changeset
  3675
    OBJ fp;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3676
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  3677
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3678
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3679
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3680
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3681
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3682
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3683
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3684
	    && (__INST(mode) != @symbol(writeonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3685
	    && __bothSmallInteger(count, start)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3686
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3687
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3688
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3689
	    cnt = __intVal(count);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3690
	    offs = __intVal(start) - 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3691
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3692
	    if (__isExternalBytesLike(anObject)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3693
		OBJ sz;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3694
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3695
		nInstBytes = 0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3696
		extPtr = (char *)(__externalBytesAddress(anObject));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3697
		if (extPtr == NULL) goto bad;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3698
		sz = __externalBytesSize(anObject);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3699
		if (__isSmallInteger(sz)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3700
		    objSize = __intVal(sz);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3701
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3702
		    objSize = 0; /* unknown */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3703
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3704
	    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3705
		OBJ oClass = __Class(anObject);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3706
		int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3707
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3708
		nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3709
		switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3710
		    case BYTEARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3711
		    case WORDARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3712
		    case LONGARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3713
		    case SWORDARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3714
		    case SLONGARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3715
		    case FLOATARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3716
			break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3717
		    case DOUBLEARRAY:
15987
0fbfbdf4a0a5 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15921
diff changeset
  3718
#ifdef __NEED_DOUBLE_ALIGN
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3719
			nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1);
15987
0fbfbdf4a0a5 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15921
diff changeset
  3720
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3721
			break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3722
		    case LONGLONGARRAY:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3723
		    case SLONGLONGARRAY:
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3724
#ifdef __NEED_LONGLONG_ALIGN
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3725
			nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1);
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3726
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3727
			break;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3728
		    default:
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3729
			goto bad;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3730
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3731
		extPtr = (char *)0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3732
		objSize = __Size(anObject) - nInstBytes;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3733
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3734
	    if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3735
		_buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3736
		if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3737
		    __READING__(f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3738
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3739
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3740
		if (extPtr) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3741
		    __READBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3742
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3743
		    /*
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3744
		     * on interrupt, anObject may be moved to another location.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3745
		     * So we pass anObject, and the offset to the __READBYTES_OBJ__ macro.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3746
		     */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3747
		    offs += nInstBytes;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3748
		    __READBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3749
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3750
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3751
		if (ret > 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3752
		    if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3753
			INT np = __intVal(__INST(position)) + ret;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3754
			OBJ t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3755
		    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3756
			__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3757
		    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3758
		    RETURN (__mkSmallInteger(ret));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3759
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3760
		if (ret == 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3761
		    __INST(hitEOF) = true;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3762
		} else /* ret < 0 */ {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3763
		    __INST(position) = nil;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3764
		    error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3765
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3766
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3767
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3768
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3769
bad: ;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3770
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  3771
    (hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3772
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3773
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3774
	^ self readError:error
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3775
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3776
    handle isNil ifTrue:[^ self errorNotOpen].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3777
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3778
    "
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3779
     count not integer or arg not bit-like (String, ByteArray etc)
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3780
    "
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3781
    ^ self primitiveFailed
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  3782
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  3783
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3784
nextLong
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3785
    "Read four bytes (msb-first) and return the value as a 32-bit signed Integer.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3786
     The returned value may be a LargeInteger.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3787
     (msb-first for compatibility with other smalltalks)"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3788
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3789
    ^ self nextUnsignedLongMSB:true
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3790
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3791
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3792
nextLongMSB:msbFlag
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  3793
    "Read four bytes and return the value as a 32-bit signed Integer,
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3794
     which may be a LargeInteger.
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  3795
     If msbFlag is true, value is read with most-significant byte first,
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3796
     otherwise least-significant byte comes first.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3797
     A nil is returned, if EOF is hit before all 4 bytes have been read.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3798
     Works in both binary and text modes."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3799
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3800
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  3801
%{
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3802
    OBJ fp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3803
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  3804
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3805
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3806
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3807
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3808
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3809
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3810
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3811
	    && (__INST(mode) != @symbol(writeonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3812
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3813
	    FILEPOINTER f;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3814
	    int ret, _buffered;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3815
	    int value;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3816
	    union {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3817
		unsigned char buffer[4];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3818
		int intVal;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3819
	    } u;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3820
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3821
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3822
	    _buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3823
	    if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3824
		__READING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3825
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3826
	    __READBYTES__(ret, f, u.buffer, 4, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3827
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3828
	    if (ret == 4) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3829
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3830
		    INT np = __intVal(__INST(position)) + 4;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3831
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3832
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3833
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3834
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3835
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3836
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3837
		if (msbFlag == true) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  3838
#if defined(__MSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3839
		    value = u.intVal;
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  3840
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3841
		    value = (u.buffer[0] & 0xFF);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3842
		    value = (value << 8) | (u.buffer[1] & 0xFF);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3843
		    value = (value << 8) | (u.buffer[2] & 0xFF);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3844
		    value = (value << 8) | (u.buffer[3] & 0xFF);
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  3845
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3846
		} else {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  3847
#if defined(__LSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3848
		    value = u.intVal;
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  3849
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3850
		    value = (u.buffer[3] & 0xFF);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3851
		    value = (value << 8) | (u.buffer[2] & 0xFF);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3852
		    value = (value << 8) | (u.buffer[1] & 0xFF);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3853
		    value = (value << 8) | (u.buffer[0] & 0xFF);
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3854
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3855
		}
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3856
#if __POINTER_SIZE__ == 8
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3857
		RETURN ( __mkSmallInteger(value));
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3858
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3859
		if ((value >= _MIN_INT) && (value <= _MAX_INT)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3860
		    RETURN ( __mkSmallInteger(value));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3861
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3862
		RETURN ( __MKLARGEINT(value) );
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  3863
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3864
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3865
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3866
	    if (ret < 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3867
		__INST(position) = nil;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3868
		error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3869
	    } else /* ret == 0 */ {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3870
		__INST(hitEOF) = true;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3871
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3872
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3873
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3874
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  3875
    hitEOF ifTrue:[^ self pastEndRead].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3876
    handle isNil ifTrue:[^ self errorNotOpen].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3877
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3878
    lastErrorNumber := error.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3879
    ^ self readError:error.
362
claus
parents: 360
diff changeset
  3880
!
claus
parents: 360
diff changeset
  3881
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3882
nextShortMSB:msbFlag
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3883
    "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
  3884
     If msbFlag is true, value is read with most-significant byte first,
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3885
     otherwise least-significant byte comes first.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3886
     A nil is returned if EOF is reached (also when EOF is hit after the first byte).
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3887
     Works in both binary and text modes."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3888
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3889
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  3890
%{
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3891
    OBJ fp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3892
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  3893
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3894
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3895
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3896
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3897
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3898
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3899
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3900
	    && (__INST(mode) != @symbol(writeonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3901
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3902
	    FILEPOINTER f;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3903
	    int ret, _buffered;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3904
	    short value;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3905
	    union {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3906
		unsigned char buffer[2];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3907
		short shortVal;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3908
	    } u;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3909
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3910
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3911
	    _buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3912
	    if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3913
		__READING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3914
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3915
	    __READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3916
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3917
	    if (ret == 2) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3918
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3919
		    INT np = __intVal(__INST(position)) + 2;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3920
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3921
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3922
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3923
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3924
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3925
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3926
		if (msbFlag == true) {
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3927
#if defined(__MSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3928
		    value = u.shortVal;
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3929
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3930
		    value = ((u.buffer[0] & 0xFF) << 8) | (u.buffer[1] & 0xFF);
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3931
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3932
		} else {
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3933
#if defined(__LSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3934
		    value = u.shortVal;
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3935
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3936
		    value = ((u.buffer[1] & 0xFF) << 8) | (u.buffer[0] & 0xFF);
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3937
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3938
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3939
		RETURN (__mkSmallInteger(value));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3940
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3941
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3942
	    if (ret < 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3943
		__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3944
		error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3945
	    } else /* ret == 0 */ {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3946
		__INST(hitEOF) = true;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3947
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3948
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3949
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3950
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  3951
    hitEOF ifTrue:[^ self pastEndRead].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  3952
    handle isNil ifTrue:[^ self errorNotOpen].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3953
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3954
    lastErrorNumber := error.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3955
    ^ self readError:error.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3956
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3957
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3958
nextUnsignedLongMSB:msbFlag
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3959
    "Read four bytes and return the value as a 32-bit unsigned Integer, which may be
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3960
     a LargeInteger.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3961
     If msbFlag is true, value is read with most-significant byte first, otherwise
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3962
     least-significant byte comes first.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3963
     A nil is returned, if endOfFile occurs before all 4 bytes have been read.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3964
     Works in both binary and text modes."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3965
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  3966
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  3967
%{
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3968
    OBJ fp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  3969
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  3970
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3971
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3972
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3973
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  3974
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  3975
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3976
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3977
	    && (__INST(mode) != @symbol(writeonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3978
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3979
	    FILEPOINTER f;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3980
	    int ret, _buffered;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3981
	    unsigned INT value;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3982
	    union {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3983
		unsigned char buffer[4];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3984
		unsigned int intVal;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3985
	    } u;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3986
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3987
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3988
	    _buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3989
	    if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3990
		__READING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3991
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3992
	    __READBYTES__(ret, f, u.buffer, 4, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3993
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3994
	    if (ret == 4) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3995
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3996
		    INT np = __intVal(__INST(position)) + 4;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3997
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3998
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  3999
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4000
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4001
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4002
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4003
		if (msbFlag == true) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  4004
#if defined(__MSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4005
		    value = u.intVal;
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  4006
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4007
		    value = u.buffer[0];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4008
		    value = (value << 8) | u.buffer[1];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4009
		    value = (value << 8) | u.buffer[2];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4010
		    value = (value << 8) | u.buffer[3];
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  4011
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4012
		} else {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  4013
#if defined(__LSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4014
		    value = u.intVal;
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  4015
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4016
		    value = u.buffer[3];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4017
		    value = (value << 8) | u.buffer[2];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4018
		    value = (value << 8) | u.buffer[1];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4019
		    value = (value << 8) | u.buffer[0];
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4020
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4021
		}
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4022
#if __POINTER_SIZE__ == 8
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4023
		value &= 0xFFFFFFFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4024
		RETURN (__mkSmallInteger(value));
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4025
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4026
		if (value <= _MAX_INT) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4027
		    RETURN (__mkSmallInteger(value));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4028
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4029
		RETURN (__MKULARGEINT(value) );
4383
7a5abc09fb39 slightly tuned nextXXXLongMSB: and nextUnsignedXXXMSB:
Claus Gittinger <cg@exept.de>
parents: 4368
diff changeset
  4030
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4031
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4032
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4033
	    if (ret < 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4034
		__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4035
		error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4036
	    } else /* ret == 0 */ {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4037
		__INST(hitEOF) = true;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4038
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4039
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4040
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4041
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  4042
    hitEOF ifTrue:[^ self pastEndRead].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4043
    handle isNil ifTrue:[^ self errorNotOpen].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4044
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4045
    lastErrorNumber := error.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4046
    ^ self readError:error.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4047
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4048
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4049
nextUnsignedShortMSB:msbFlag
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4050
    "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
  4051
     If msbFlag is true, value is read with most-significant byte first,
1079
d47cd86b487b Make methods signal-proof.
Stefan Vogel <sv@exept.de>
parents: 1068
diff changeset
  4052
     otherwise least-significant byte comes first.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4053
     A nil is returned if EOF is reached (also when EOF is hit after the first byte).
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4054
     Works in both binary and text modes."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4055
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4056
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  4057
%{
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4058
    OBJ fp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4059
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  4060
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4061
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4062
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4063
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4064
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4065
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4066
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4067
	    && (__INST(mode) != @symbol(writeonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4068
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4069
	    FILEPOINTER f;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4070
	    int ret, _buffered;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4071
	    unsigned int value;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4072
	    union {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4073
		unsigned char buffer[2];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4074
		unsigned short shortVal;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4075
	    } u;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4076
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4077
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4078
	    _buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4079
	    if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4080
		__READING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4081
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4082
	    __READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4083
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4084
	    if (ret == 2) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4085
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4086
		    INT np = __intVal(__INST(position)) + 2;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4087
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4088
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4089
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4090
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4091
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4092
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4093
		if (msbFlag == true) {
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4094
#if defined(__MSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4095
		    value = u.shortVal;
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4096
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4097
		    value = (u.buffer[0] << 8) | u.buffer[1];
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4098
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4099
		} else {
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4100
#if defined(__LSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4101
		    value = u.shortVal;
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4102
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4103
		    value = (u.buffer[1] << 8) | u.buffer[0];
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4104
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4105
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4106
		RETURN (__mkSmallInteger(value));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4107
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4108
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4109
	    if (ret < 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4110
		__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4111
		error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4112
	    } else /* ret == 0 */ {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4113
		__INST(hitEOF) = true;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4114
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4115
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4116
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4117
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  4118
    hitEOF ifTrue:[^ self pastEndRead].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4119
    handle isNil ifTrue:[^ self errorNotOpen].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4120
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4121
    lastErrorNumber := error.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4122
    ^ self readError:error.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4123
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4124
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4125
nextWord
15452
e2a3b2dc064b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15435
diff changeset
  4126
    <resource: #obsolete>
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4127
    "in text-mode:
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
  4128
	 read the alphaNumeric next word (i.e. up to non letter-or-digit).
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
  4129
	 return a string containing those characters.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4130
     in binary-mode:
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
  4131
	 read two bytes (msb-first) and return the value as a 16-bit
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
  4132
	 unsigned Integer (for compatibility with other smalltalks)"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4133
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4134
    binary ifTrue:[
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14603
diff changeset
  4135
	^ self nextUnsignedShortMSB:true
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4136
    ].
14518
7dad78183779 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 14440
diff changeset
  4137
    self obsoleteMethodWarning:'use #nextAlphaNumericWord'.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4138
    ^ self nextAlphaNumericWord
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4139
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4140
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4141
!ExternalStream methodsFor:'non homogenous writing'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4142
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4143
nextPutByte:aByteValue
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4144
    "write a byte.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4145
     Works in both binary and text modes."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4146
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4147
    |error|
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4148
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  4149
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  4150
    FILEPOINTER f;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4151
    char c;
7875
eb7d48b0425d made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents: 7832
diff changeset
  4152
    OBJ fp;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  4153
    int cnt, _buffered;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4154
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  4155
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4156
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4157
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4158
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4159
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4160
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4161
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4162
	    && (__INST(mode) != @symbol(readonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4163
	    && __isSmallInteger(aByteValue)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4164
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4165
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4166
	    c = __intVal(aByteValue);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4167
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4168
	    if (_buffered = (__INST(buffered) == true)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4169
		__WRITING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4170
	    }
9053
f3f3d48d6f44 fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents: 9024
diff changeset
  4171
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4172
	    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4173
		cnt = __win32_fwrite(&c, 1, 1, f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4174
	    } else
9053
f3f3d48d6f44 fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents: 9024
diff changeset
  4175
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4176
	    {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4177
		__WRITEBYTE__(cnt, f, &c, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4178
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4179
	    if (cnt == 1) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4180
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4181
		    INT np = __intVal(__INST(position)) + 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4182
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4183
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4184
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4185
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4186
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4187
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4188
		RETURN (self);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4189
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4190
	    if (cnt < 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4191
		__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4192
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4193
	    error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4194
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4195
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4196
%}.
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4197
    handle isNil ifTrue:[self errorNotOpen. ^ self].
7801
d930c353588d writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents: 7737
diff changeset
  4198
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4199
    lastErrorNumber := error.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4200
    self writeError:error.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4201
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4202
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4203
nextPutLong:aNumber MSB:msbFlag
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4204
    "Write the argument, aNumber as a long (four bytes). If msbFlag is
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4205
     true, data is written most-significant byte first; otherwise least
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4206
     first.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4207
     Works in both binary and text modes."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4208
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4209
    |error|
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4210
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  4211
%{
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4212
    int num;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4213
    union {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4214
	char bytes[4];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4215
	int intVal;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4216
    } u;
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  4217
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  4218
    int cnt, _buffered;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4219
    OBJ fp;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4220
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  4221
    __INST(lastErrorNumber) = nil;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4222
    if (__isSmallInteger(aNumber)) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4223
	num = __intVal(aNumber);
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4224
    } else {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  4225
#if __POINTER_SIZE__ == 8
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4226
	goto badArg;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4227
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4228
	num = __longIntVal(aNumber);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4229
	if (num == 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4230
	    num = __signedLongIntVal(aNumber);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4231
	    if (num == 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4232
		/* bad arg or out-of-range integer
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4233
		 * (handled by the fallBack code)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4234
		 */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4235
		goto badArg;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4236
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4237
	}
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4238
#endif
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4239
    }
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4240
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4241
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4242
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4243
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4244
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4245
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4246
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4247
	 && (__INST(mode) != @symbol(readonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4248
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4249
	    if (msbFlag == true) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  4250
#if defined(__MSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4251
		u.intVal = num;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4252
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4253
		u.bytes[0] = (num >> 24) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4254
		u.bytes[1] = (num >> 16) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4255
		u.bytes[2] = (num >> 8) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4256
		u.bytes[3] = num & 0xFF;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4257
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4258
	    } else {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  4259
#if defined(__LSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4260
		u.intVal = num;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4261
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4262
		u.bytes[3] = (num >> 24) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4263
		u.bytes[2] = (num >> 16) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4264
		u.bytes[1] = (num >> 8) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4265
		u.bytes[0] = num & 0xFF;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4266
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4267
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4268
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4269
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4270
	    if (_buffered = (__INST(buffered) == true)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4271
		__WRITING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4272
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4273
	    __WRITEBYTES__(cnt, f, u.bytes, 4, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4274
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4275
	    if (cnt == 4) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4276
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4277
		    INT np = __intVal(__INST(position)) + 4;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4278
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4279
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4280
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4281
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4282
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4283
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4284
		RETURN ( self );
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4285
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4286
	    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4287
	    error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4288
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4289
    }
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4290
badArg: ;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4291
%}.
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4292
    handle isNil ifTrue:[self errorNotOpen. ^ self].
7801
d930c353588d writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents: 7737
diff changeset
  4293
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4294
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4295
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4296
	self writeError:error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4297
	^ self
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4298
    ].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4299
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4300
    aNumber isInteger ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4301
	^ super nextPutLong:aNumber MSB:msbFlag
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4302
    ].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4303
    self argumentMustBeInteger
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4304
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4305
15655
a044910b6f38 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15619
diff changeset
  4306
nextPutShort:anIntegerOrCharacter MSB:msbFlag
a044910b6f38 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15619
diff changeset
  4307
    "Write the argument, anIntegerOrCharacter as a short (two bytes). If msbFlag is
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4308
     true, data is written most-significant byte first; otherwise least
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4309
     first.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4310
     Works in both binary and text modes."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4311
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4312
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  4313
%{
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4314
    int num;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4315
    union {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4316
	char bytes[2];
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4317
	short shortVal;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4318
    } u;
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4319
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  4320
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  4321
    int cnt, _buffered;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4322
    OBJ fp;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4323
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  4324
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4325
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4326
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4327
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4328
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4329
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4330
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4331
	    && (__INST(mode) != @symbol(readonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4332
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4333
	    if (__isSmallInteger(anIntegerOrCharacter)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4334
		num = __intVal(anIntegerOrCharacter);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4335
	    } else if (__isCharacter(anIntegerOrCharacter)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4336
		num = __smallIntegerVal(__characterVal(anIntegerOrCharacter));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4337
	    } else
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4338
		goto out;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4339
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4340
	    if (msbFlag == true) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8881
diff changeset
  4341
#if defined(__MSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4342
		u.shortVal = num;
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4343
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4344
		u.bytes[0] = (num >> 8) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4345
		u.bytes[1] = num & 0xFF;
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4346
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4347
	    } else {
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4348
#if defined(__LSBFIRST__)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4349
		u.shortVal = num;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4350
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4351
		u.bytes[1] = (num >> 8) & 0xFF;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4352
		u.bytes[0] = num & 0xFF;
4384
f8e779b99b14 slightly tuned nextPutLongMSB: and nextPutShortMSB:
Claus Gittinger <cg@exept.de>
parents: 4383
diff changeset
  4353
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4354
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4355
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4356
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4357
	    if (_buffered = (__INST(buffered) == true)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4358
		__WRITING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4359
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4360
	    __WRITEBYTES__(cnt, f, u.bytes, 2, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4361
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4362
	    if (cnt == 2) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4363
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4364
		    INT np = __intVal(__INST(position)) + 2;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4365
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4366
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4367
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4368
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4369
		    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4370
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4371
		RETURN ( self );
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4372
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4373
	    __INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4374
	    error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4375
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4376
    }
15655
a044910b6f38 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15619
diff changeset
  4377
out:;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4378
%}.
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4379
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4380
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4381
	self writeError:error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4382
	^ self
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4383
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4384
    handle isNil ifTrue:[self errorNotOpen. ^ self].
7801
d930c353588d writeError: care for EWOULDBLOCK in nonBlocking case.
penk
parents: 7737
diff changeset
  4385
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4386
    self argumentMustBeInteger
10
claus
parents: 5
diff changeset
  4387
! !
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4388
7053
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4389
!ExternalStream methodsFor:'positioning'!
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4390
15619
3f2e6f5fab12 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15496
diff changeset
  4391
position
7053
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4392
    self subclassResponsibility
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4393
!
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4394
15619
3f2e6f5fab12 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 15496
diff changeset
  4395
position:index0Based
7053
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4396
    self subclassResponsibility
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4397
! !
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
  4398
12631
9e55d449f787 added: #printOn:
Stefan Vogel <sv@exept.de>
parents: 12553
diff changeset
  4399
!ExternalStream methodsFor:'printing & storing'!
9e55d449f787 added: #printOn:
Stefan Vogel <sv@exept.de>
parents: 12553
diff changeset
  4400
9e55d449f787 added: #printOn:
Stefan Vogel <sv@exept.de>
parents: 12553
diff changeset
  4401
printOn:aStream
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4402
    aStream
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4403
	nextPutAll:self className;
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4404
	nextPutAll:'(handle:('.
12631
9e55d449f787 added: #printOn:
Stefan Vogel <sv@exept.de>
parents: 12553
diff changeset
  4405
    handle printOn:aStream.
9e55d449f787 added: #printOn:
Stefan Vogel <sv@exept.de>
parents: 12553
diff changeset
  4406
    aStream nextPutAll:'))'
9e55d449f787 added: #printOn:
Stefan Vogel <sv@exept.de>
parents: 12553
diff changeset
  4407
! !
9e55d449f787 added: #printOn:
Stefan Vogel <sv@exept.de>
parents: 12553
diff changeset
  4408
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4409
!ExternalStream methodsFor:'private'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4410
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4411
clearEOF
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4412
    hitEOF := false
16300
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  4413
! !
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  4414
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  4415
!ExternalStream protectedMethodsFor:'private'!
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4416
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4417
closeFile
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4418
    "low level close - may be redefined in subclasses
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4419
     Don't send this message, send #close instead"
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4420
12674
80fbcc4be1d6 changed: #closeFile
Stefan Vogel <sv@exept.de>
parents: 12631
diff changeset
  4421
    |fp error|
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4422
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4423
    fp := handle.
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4424
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4425
%{
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4426
    int rslt;
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4427
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4428
    if (fp == nil) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4429
	error = @symbol(errorNotOpen);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4430
	goto out;
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4431
    }
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4432
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4433
    if (__INST(handleType) == @symbol(socketHandle)) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4434
	SOCKET sock = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4435
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4436
	if (@global(FileOpenTrace) == true) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4437
	    fprintf(stderr, "close socket [ExternalStream] %"_lx_"\n", (INT)sock);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4438
	}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4439
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4440
	// whether the close() will be successful or not - the handle is invalid now!
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4441
	__INST(handle) = nil;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4442
	do {
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4443
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4444
	    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
  4445
#else
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4446
	    rslt = close(sock);
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4447
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4448
	} while((rslt < 0) && (__threadErrno == EINTR));
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4449
    } else if ((__INST(handleType) == nil)
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4450
	       || (__INST(handleType) == @symbol(filePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4451
	       || (__INST(handleType) == @symbol(socketFilePointer))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4452
	       || (__INST(handleType) == @symbol(pipeFilePointer)))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4453
    {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4454
	FILEPOINTER f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4455
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4456
	if (@global(FileOpenTrace) == true) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4457
	    fprintf(stderr, "fclose [ExternalStream] %"_lx_"\n", (INT)f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4458
	}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4459
	// whether the close() will be successful or not - the handle is invalid now!
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4460
	__INST(handle) = nil;
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4461
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4462
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4463
	if (__INST(mode) != @symbol(readonly) && __INST(buffered) != false) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4464
	    // do a fflush() first, so that fclose() doesn't block
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4465
	    // we suspect, that EINTR causes problems in fclose()
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4466
	    do {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4467
		__threadErrno = 0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4468
		rslt = __STX_C_CALL1("fflush", fflush, f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4469
	    } while((rslt < 0) && (__threadErrno == EINTR));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4470
	}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4471
	do {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4472
	    __threadErrno = 0;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4473
	    rslt = __STX_C_NOINT_CALL1("fclose", fclose, f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4474
	} while((rslt < 0) && (__threadErrno == EINTR));
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4475
#else
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
  4476
	// cg: the pre Nov2014 code always did the fclose interruptable;
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
  4477
	// I am not sure, if fclose is actually prepared to do this;
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
  4478
	// at least when only reading, this should not block, and we
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
  4479
	// should be ableto do it without being interruptable.
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
  4480
	// Must watch this - if it leads to blockings, change and think about it.
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
  4481
	if (__INST(mode) != @symbol(readonly)) {
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
  4482
	    __BEGIN_INTERRUPTABLE__
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
  4483
	    rslt = fclose(f);
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
  4484
	    __END_INTERRUPTABLE__
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
  4485
	} else {
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
  4486
	    rslt = fclose(f);
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
  4487
	}
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4488
#endif
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4489
    } else {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4490
	error = @symbol(badHandleType);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4491
	goto out;
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4492
    }
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4493
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4494
    if (rslt < 0) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4495
	error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4496
	goto out;
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4497
    }
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4498
    RETURN (self);
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4499
12674
80fbcc4be1d6 changed: #closeFile
Stefan Vogel <sv@exept.de>
parents: 12631
diff changeset
  4500
out:;
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4501
%}.
12674
80fbcc4be1d6 changed: #closeFile
Stefan Vogel <sv@exept.de>
parents: 12631
diff changeset
  4502
80fbcc4be1d6 changed: #closeFile
Stefan Vogel <sv@exept.de>
parents: 12631
diff changeset
  4503
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4504
	error == #errorNotOpen ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4505
	    self errorNotOpen.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4506
	].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4507
	error isInteger ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4508
	    lastErrorNumber := error.
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
  4509
	    mode == #readonly ifTrue:[
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
  4510
		self ioError:error.
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
  4511
	    ] ifFalse:[
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
  4512
		self writeError:error.
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
  4513
	    ].
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4514
	    ^ self.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4515
	].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4516
	self primitiveFailed:error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4517
	^ self.
12674
80fbcc4be1d6 changed: #closeFile
Stefan Vogel <sv@exept.de>
parents: 12631
diff changeset
  4518
    ].
80fbcc4be1d6 changed: #closeFile
Stefan Vogel <sv@exept.de>
parents: 12631
diff changeset
  4519
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4520
    "/ fallback for rel5
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4521
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4522
    fp := handle.
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4523
    fp notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4524
	handle := nil.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4525
	self closeFile:fp
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4526
    ]
16300
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  4527
! !
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  4528
277c0cb1ecbc class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16296
diff changeset
  4529
!ExternalStream methodsFor:'private'!
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4530
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4531
closeFile:handle
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4532
    "for rel5 only"
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4533
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4534
    self primitiveFailed
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4535
!
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4536
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  4537
connectTo:aFileDescriptor withMode:modeSymbol handleType:handleTypeSymbol
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4538
    "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
  4539
     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
  4540
     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
  4541
     a Stream object.
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4542
     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
  4543
     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
  4544
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  4545
    |retVal error openmode|
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4546
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4547
    handle notNil ifTrue:[^ self errorAlreadyOpen].
12706
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  4548
    mode := modeSymbol.
Stefan Vogel <sv@exept.de>
parents: 12704
diff changeset
  4549
    openmode := self fopenMode.
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4550
%{
12683
40b0a5cd3fd3 changed:
Stefan Vogel <sv@exept.de>
parents: 12678
diff changeset
  4551
    FILEPOINTER f = 0;
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4552
    OBJ fp;
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4553
    FILE *fdopen();
12683
40b0a5cd3fd3 changed:
Stefan Vogel <sv@exept.de>
parents: 12678
diff changeset
  4554
    int fd = -2;
40b0a5cd3fd3 changed:
Stefan Vogel <sv@exept.de>
parents: 12678
diff changeset
  4555
40b0a5cd3fd3 changed:
Stefan Vogel <sv@exept.de>
parents: 12678
diff changeset
  4556
    if (__isStringLike(openmode)) {
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4557
#ifdef WIN32
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4558
	__stxWrapApiEnterCritical();
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4559
#endif
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4560
	if (__isSmallInteger(aFileDescriptor)) {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4561
	    fd = __intVal(aFileDescriptor);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4562
	}
12683
40b0a5cd3fd3 changed:
Stefan Vogel <sv@exept.de>
parents: 12678
diff changeset
  4563
#ifdef WIN32
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4564
	else if (__isExternalAddressLike(aFileDescriptor)) {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4565
	    fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4566
	    if (fd < 0) {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4567
		if (__threadErrno == 0) {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4568
		    // no more file descriptors
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4569
		    __threadErrno = EMFILE;
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4570
		}
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4571
		error = __mkSmallInteger(__threadErrno);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4572
		__stxWrapApiLeaveCritical();
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4573
		goto out;
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4574
	    }
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4575
	}
12683
40b0a5cd3fd3 changed:
Stefan Vogel <sv@exept.de>
parents: 12678
diff changeset
  4576
#endif
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4577
	f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode));
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4578
#ifdef WIN32
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4579
	__stxWrapApiLeaveCritical();
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4580
#endif
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4581
	if (f == NULL) {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4582
	    error =__mkSmallInteger(__threadErrno);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4583
	} else {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4584
	    if (@global(FileOpenTrace) == true) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4585
		fprintf(stderr, "fdopen [ExternalStream] %"_ld_" (%"_lx_") -> %"_lx_"\n", (INT)fd, (INT)fd, (INT)f);
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4586
	    }
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4587
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4588
	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4589
	    retVal = self;
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4590
	}
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4591
    }
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4592
out:;
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4593
%}.
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4594
    retVal notNil ifTrue:[
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4595
	position := 0.
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4596
	buffered isNil ifTrue:[
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4597
	    buffered := true.       "default is buffered"
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4598
	].
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4599
	handleType := handleTypeSymbol.
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4600
	Lobby register:self.
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4601
	^ retVal
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4602
    ].
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4603
    error notNil ifTrue:[
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4604
	OperatingSystem closeFd:aFileDescriptor.
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4605
	lastErrorNumber := error.
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4606
	position := nil.
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4607
	"
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4608
	 the open failed for some reason ...
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4609
	"
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4610
	^ self openError:error
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4611
    ].
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4612
!
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4613
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4614
dupFd
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4615
    "duplicate my file descriptor"
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4616
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4617
    |fd dupFd|
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4618
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4619
    fd := self fileHandle.
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4620
    dupFd := OperatingSystem dup:fd.
10989
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4621
    self setFileHandle:dupFd mode:self fopenMode.
10995
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  4622
!
10989
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4623
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4624
fopenMode
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4625
   "answer the mode for fopen.
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4626
    Only used internally"
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4627
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4628
   mode == #readonly ifTrue:[
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4629
	^ ReadMode
10989
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4630
   ].
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4631
   mode == #writeonly ifTrue:[
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4632
	^ WriteMode
10989
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4633
   ].
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4634
   mode == #readWrite ifTrue:[
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4635
	^ ReadWriteMode
10989
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4636
   ].
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4637
   mode == #append ifTrue:[
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4638
	^ AppendMode
10989
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4639
   ].
f7d1de91344b fix #setFileHandle:mode:
Stefan Vogel <sv@exept.de>
parents: 10988
diff changeset
  4640
   ^ ReadWriteMode
10995
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  4641
!
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4642
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4643
open:aPath withMode:openModeString
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4644
    "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
  4645
     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
  4646
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4647
    |ok error|
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4648
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4649
    ok := false.
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4650
%{
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4651
    FILE *f;
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4652
    OBJ fp;
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4653
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4654
    if (__isStringLike(aPath) && __isStringLike(openModeString)) {
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4655
#ifdef WIN32
10339
972d0164125a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10328
diff changeset
  4656
# if 1
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4657
       f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4658
# else
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4659
	__BEGIN_INTERRUPTABLE__
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4660
	do {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4661
	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4662
	} while ((f == NULL) && (__threadErrno == EINTR));
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4663
	__END_INTERRUPTABLE__
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4664
# endif
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4665
#else /* UNIX */
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4666
	__BEGIN_INTERRUPTABLE__
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4667
	do {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4668
	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4669
	} while ((f == NULL) && (__threadErrno == EINTR));
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4670
	__END_INTERRUPTABLE__
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4671
#endif /* UNIX */
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4672
	if (f == NULL) {
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4673
	    error = __mkSmallInteger(__threadErrno);
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4674
	} else {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4675
	    if (@global(FileOpenTrace) == true) {
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4676
		fprintf(stderr, "fopen %s [ExternalStream] -> %"_lx_"\n", __stringVal(aPath), (INT)f);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4677
	    }
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4678
	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4679
	    __INST(handleType) = @symbol(filePointer);
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4680
	    ok = true;
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4681
	}
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4682
    }
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4683
%}.
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4684
    ok ifTrue:[
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4685
	position := 0.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4686
	Lobby register:self.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4687
	^ self.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4688
    ].
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4689
    error notNil ifTrue:[
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4690
	"
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4691
	 the open failed for some reason ...
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4692
	"
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4693
	lastErrorNumber := error.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4694
	self openError:error.
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4695
    ].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4696
    self primitiveFailed.
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4697
!
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4698
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4699
reOpen
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4700
    "USERS WILL NEVER INVOKE THIS METHOD
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4701
     sent after snapin to reopen streams.
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4702
     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
  4703
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4704
    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
  4705
    handle := nil.
8966
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4706
    Lobby unregister:self.
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4707
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4708
    "Modified: 10.1.1997 / 17:50:44 / cg"
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4709
!
0b299dd8759c Allow Symbols as pathname and openMode
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  4710
12398
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4711
setAccessor:what to:something
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4712
    "set the filePointer/fileHandle to the given one;
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4713
     low level private & friend interface; may also be used to connect to some
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4714
     externally provided handle."
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4715
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4716
    handle := something.
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4717
    handleType := what.
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4718
!
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4719
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4720
setFileDescriptor:anInteger mode:openMode
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4721
    "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
  4722
     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
  4723
     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
  4724
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4725
    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
  4726
!
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4727
12398
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4728
setFileHandle:something
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4729
    "set the fileHandle to the given one;
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4730
     low level private & friend interface; may also be used to connect to some
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4731
     externally provided file handle."
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4732
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4733
    self setAccessor:#fileHandle to:something
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4734
!
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4735
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4736
setFileHandle:anIntegerOrExternalAddress mode:openMode
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4737
    "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
  4738
     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
  4739
     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
  4740
     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
  4741
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4742
%{
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4743
    FILEPOINTER f;
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4744
    OBJ fp;
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4745
    FILE *fdopen();
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4746
    int fd;
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4747
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4748
    if (!__isStringLike(openMode))
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4749
	goto err;
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4750
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4751
#ifdef WIN32
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4752
    __stxWrapApiEnterCritical();
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4753
    if (__isExternalAddressLike(anIntegerOrExternalAddress) ) {
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4754
	HANDLE __fileHandle = (HANDLE)__externalAddressVal(anIntegerOrExternalAddress);
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4755
	fd = _open_osfhandle((long)__fileHandle, O_BINARY);      /* should we handle readonly, append or text mode? */
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4756
	if (fd < 0) {
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4757
	    __stxWrapApiLeaveCritical();
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4758
	    CloseHandle(__fileHandle);
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4759
	    goto err;
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4760
	}
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4761
    } else
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4762
#endif
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4763
    if (__isSmallInteger(anIntegerOrExternalAddress)) {
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4764
	fd = __smallIntegerVal(anIntegerOrExternalAddress);
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4765
    } else {
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4766
#ifdef WIN32
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4767
	__stxWrapApiLeaveCritical();
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4768
#endif
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4769
	goto err;
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4770
    }
10997
8ec53332c725 _fdopen -> fdopen()
Stefan Vogel <sv@exept.de>
parents: 10995
diff changeset
  4771
    f = fdopen(fd, __stringVal(openMode));
12688
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4772
#ifdef WIN32
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4773
    __stxWrapApiLeaveCritical();
b429dc21d6b8 added: #openError:
Stefan Vogel <sv@exept.de>
parents: 12683
diff changeset
  4774
#endif
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4775
    if (f != NULL) {
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4776
	if (@global(FileOpenTrace) == true) {
15435
fc902ce16af0 typecasts in printf for 64bit
Claus Gittinger <cg@exept.de>
parents: 15361
diff changeset
  4777
	    fprintf(stderr, "fdopen [ExternalStream] %d -> %"_lx_"\n", fd, (INT)f);
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4778
	}
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4779
	fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4780
	__INST(handleType) = @symbol(filePointer);
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  4781
	RETURN (self);
10982
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4782
    }
11fd48c1276d New: #copy duplicates the underlying file descriptor resp. file handle
Stefan Vogel <sv@exept.de>
parents: 10932
diff changeset
  4783
err:;
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4784
%}.
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4785
    ^ self primitiveFailed
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4786
!
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4787
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4788
setFilePointer:something
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4789
    "set the filePointer to the given one;
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4790
     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
  4791
     externally provided file."
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4792
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4793
    self setAccessor:#filePointer to:something
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4794
!
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4795
12398
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4796
setLastError:aNumber
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  4797
    lastErrorNumber := aNumber
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4798
!
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4799
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4800
setSocketHandle:something
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4801
    "set the socketHandle to the given one;
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4802
     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
  4803
     externally provided socket handle."
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4804
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4805
    self setAccessor:#socketHandle to:something
8471
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4806
! !
e061e503bf03 Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents: 8362
diff changeset
  4807
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4808
!ExternalStream methodsFor:'queries'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4809
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4810
nextError
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4811
    "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
  4812
     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
  4813
     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
  4814
     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
  4815
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4816
%{ /*NOCONTEXT*/
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4817
    FILEPOINTER f;
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4818
    int ret, _buffered;
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4819
    OBJ fp;
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4820
    unsigned char ch;
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4821
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4822
    if ((__INST(handleType) == nil)
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4823
     || (__INST(handleType) == @symbol(filePointer))
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4824
     || (__INST(handleType) == @symbol(socketFilePointer))
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4825
     || (__INST(handleType) == @symbol(socketHandle))
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4826
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4827
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4828
	    && (__INST(mode) != @symbol(writeonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4829
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4830
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4831
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4832
	    _buffered = (__INST(buffered) == true);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4833
	    if (_buffered) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4834
		__READING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4835
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4836
	    __READBYTE__(ret, f, &ch, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4837
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4838
	    if (ret > 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4839
		RETURN(nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4840
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4841
	    if (ret < 0) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4842
		RETURN(__mkSmallInteger(__threadErrno));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4843
	    } else /* ret == 0 */ {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4844
		RETURN(__mkSmallInteger(0)); /* EOF */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4845
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4846
	}
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4847
    }
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4848
%}.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4849
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4850
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4851
numAvailable
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4852
    "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
  4853
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4854
    |fd|
7804
7128efd3b504 isNonBlocking -> isBlocking
Claus Gittinger <cg@exept.de>
parents: 7801
diff changeset
  4855
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4856
    handle isNil ifTrue:[^ self errorNotOpen].
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4857
    mode == #writeonly ifTrue:[^ self errorWriteOnly].
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4858
    fd := self fileDescriptor.
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4859
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4860
%{
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4861
#ifdef WIN32
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4862
    int res = 1;
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4863
    int success = 0;
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4864
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4865
    if (__INST(handleType) == @symbol(socketHandle)) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4866
	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
  4867
    } else if (__INST(handleType) == @symbol(socketFilePointer)) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4868
	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
  4869
    } else if (__INST(handleType) == @symbol(pipeFilePointer)) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4870
	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
  4871
    }
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4872
    if (success) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4873
	if (__INST(readAhead) != nil) res++;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  4874
	RETURN(__mkSmallInteger(res));
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4875
    }
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4876
#endif
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4877
%}.
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4878
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4879
    ^ (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
  4880
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4881
    "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
  4882
    "Modified: / 4.2.1998 / 17:31:11 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4883
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4884
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4885
!ExternalStream methodsFor:'reading'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4886
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4887
next
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4888
    "return the next element; advance read position.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4889
     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
  4890
     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
  4891
     - see #pastEndRead."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  4892
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4893
    |c error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  4894
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  4895
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  4896
    int ret, _buffered;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4897
    OBJ pos, fp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4898
    unsigned char ch;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4899
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  4900
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4901
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4902
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4903
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  4904
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  4905
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
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
  4906
	if (((fp = __INST(handle)) != nil)
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
  4907
	    && (__INST(mode) != @symbol(writeonly))
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
  4908
	) {
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
  4909
	    f = __FILEVal(fp);
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
  4910
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
  4911
	    _buffered = (__INST(buffered) == true);
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
  4912
	    if (_buffered) {
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
  4913
		__READING__(f)
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
  4914
	    }
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
  4915
	    __READBYTE__(ret, f, &ch, _buffered, __INST(handleType));
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
  4916
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
  4917
	    if (ret > 0) {
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
  4918
		pos = __INST(position);
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
  4919
		if (__isSmallInteger(pos)) {
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
  4920
		    OBJ t;
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
  4921
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
  4922
		    t = __MKINT(__intVal(pos) + 1); __INST(position) = t; __STORE(self, t);
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
  4923
		} else {
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
  4924
		    __INST(position) = nil; /* i.e. do not know */
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
  4925
		}
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
  4926
		if (__INST(binary) == true) {
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
  4927
		    RETURN ( __mkSmallInteger(ch) );
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
  4928
		}
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
  4929
		RETURN ( __MKCHARACTER(ch) );
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
  4930
	    }
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
  4931
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
  4932
	    __INST(position) = nil;
17360
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  4933
	    if ((ret < 0)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  4934
#ifdef ECONNRESET
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  4935
		&& (__threadErrno != ECONNRESET)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  4936
#endif
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  4937
	    ){
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
  4938
		error = __mkSmallInteger(__threadErrno);
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
  4939
	    } else /* ret == 0 */ {
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
  4940
		__INST(hitEOF) = true;
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
  4941
	    }
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
  4942
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4943
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4944
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8604
diff changeset
  4945
    hitEOF == true ifTrue:[^ self pastEndRead].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4946
    error notNil ifTrue:[
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
  4947
	lastErrorNumber := error.
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
  4948
	^ self readError:error
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4949
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4950
    handle isNil ifTrue:[^ self errorNotOpen].
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  4951
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  4952
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  4953
    readAhead notNil ifTrue:[
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
  4954
	c := readAhead.
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
  4955
	readAhead := nil.
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
  4956
	^ c.
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  4957
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  4958
    c := self nextByteFromFile:handle.
5430
7d0aa5fc6c22 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5420
diff changeset
  4959
    c isNil ifTrue:[
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
  4960
	^ self pastEndRead.
5430
7d0aa5fc6c22 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5420
diff changeset
  4961
    ].
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  4962
    binary == true ifTrue:[
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
  4963
	^ c
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  4964
    ].
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  4965
    ^ Character value:c
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4966
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4967
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4968
next:count
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4969
    "return the next count elements of the stream as a collection.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4970
     Redefined to return a String or ByteArray instead of the default: Array."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4971
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4972
    |coll nRead|
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4973
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4974
    binary ifTrue:[
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
  4975
	coll := ByteArray uninitializedNew:count
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4976
    ] ifFalse:[
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
  4977
	coll := String new:count
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4978
    ].
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4979
    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
  4980
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  4981
    "/ for readStream protocol compatibility,
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4982
    "/ we must raise an exception here.
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4983
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4984
    nRead < count ifTrue:[
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
  4985
	^ self pastEndRead
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4986
    ].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4987
    ^ coll
2151
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4988
5f1fd65f8c3b next: must raise pastEnd, if EOF is hit;
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
  4989
    "Modified: 11.1.1997 / 17:44:17 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4990
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  4991
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  4992
nextOrNil
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  4993
    "return the next element; advance read position.
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  4994
     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
  4995
     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
  4996
     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
  4997
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  4998
    |c error|
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  4999
%{
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5000
    FILEPOINTER f;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5001
    int ret, _buffered;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5002
    OBJ pos, fp;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5003
    unsigned char ch;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5004
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5005
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5006
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5007
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5008
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5009
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5010
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
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
  5011
	if (((fp = __INST(handle)) != nil)
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
  5012
	    && (__INST(mode) != @symbol(writeonly))
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
  5013
	) {
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
  5014
	    f = __FILEVal(fp);
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
  5015
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
  5016
	    _buffered = (__INST(buffered) == true);
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
  5017
	    if (_buffered) {
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
  5018
		__READING__(f)
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
  5019
	    }
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
  5020
	    __READBYTE__(ret, f, &ch, _buffered, __INST(handleType));
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
  5021
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
  5022
	    if (ret > 0) {
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
  5023
		pos = __INST(position);
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
  5024
		if (__isSmallInteger(pos)) {
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
  5025
		    OBJ t;
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
  5026
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
  5027
		    t = __MKINT(__intVal(pos) + 1); __INST(position) = t; __STORE(self, t);
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
  5028
		} else {
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
  5029
		    __INST(position) = nil; /* i.e. do not know */
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
  5030
		}
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
  5031
		if (__INST(binary) == true) {
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
  5032
		    RETURN ( __mkSmallInteger(ch) );
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
  5033
		}
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
  5034
		RETURN ( __MKCHARACTER(ch) );
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
  5035
	    }
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
  5036
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
  5037
	    __INST(position) = nil;
17360
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5038
	    if ((ret < 0)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5039
#ifdef ECONNRESET
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5040
		&& (__threadErrno != ECONNRESET)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5041
#endif
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5042
	    ){
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
  5043
		error = __mkSmallInteger(__threadErrno);
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
  5044
	    } else /* ret == 0 */ {
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
  5045
		__INST(hitEOF) = true;
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
  5046
	    }
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
  5047
	}
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5048
    }
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5049
%}.
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5050
    hitEOF == true ifTrue:[^ nil].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5051
    error notNil ifTrue:[
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
  5052
	lastErrorNumber := error.
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
  5053
	^ self readError:error.
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5054
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5055
    handle isNil ifTrue:[^ self errorNotOpen].
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5056
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5057
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5058
    readAhead notNil ifTrue:[
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
  5059
	c := readAhead.
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
  5060
	readAhead := nil.
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
  5061
	^ c.
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5062
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5063
    c := self nextByteFromFile:handle.
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5064
    c isNil ifTrue:[
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
  5065
	^ nil.
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5066
    ].
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5067
    binary == true ifTrue:[
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
  5068
	^ c
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5069
    ].
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5070
    ^ Character value:c
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5071
!
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5072
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5073
peek
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5074
    "return the element to be read next without advancing read position.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5075
     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
  5076
     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
  5077
     - see #pastEndRead."
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5078
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5079
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  5080
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  5081
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5082
    unsigned char c;
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  5083
    int ret, _buffered;
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  5084
    OBJ fp;
3672
49dff0652a2c fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents: 3569
diff changeset
  5085
    OBJ ra;
49dff0652a2c fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents: 3569
diff changeset
  5086
49dff0652a2c fixed readAhead in peek with non-binary mode
Claus Gittinger <cg@exept.de>
parents: 3569
diff changeset
  5087
    if ((ra = __INST(readAhead)) != nil) {
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
  5088
	if (__INST(binary) == true) {
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
  5089
	    RETURN ( ra );
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
  5090
	}
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
  5091
	c = __intVal(ra);
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
  5092
	RETURN ( __MKCHARACTER(c) );
3029
aa14768e0e6a larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
  5093
    }
aa14768e0e6a larger lineBuffer in #nextLine
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
  5094
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  5095
    __INST(lastErrorNumber) = nil;
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  5096
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5097
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5098
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5099
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5100
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5101
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
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
  5102
	if (((fp = __INST(handle)) != nil)
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
  5103
	    && (__INST(mode) != @symbol(writeonly))
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
  5104
	) {
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
  5105
	    f = __FILEVal(fp);
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
  5106
	    _buffered = (__INST(buffered) == true);
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
  5107
	    if (_buffered) {
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
  5108
		__READING__(f)
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
  5109
	    }
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
  5110
	    __READBYTE__(ret, f, &c, _buffered, __INST(handleType));
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
  5111
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
  5112
	    if (ret > 0) {
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
  5113
		__UNGETC__(c, f, _buffered);
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
  5114
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
  5115
		if (__INST(binary) == true) {
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
  5116
		    RETURN ( __mkSmallInteger(c) );
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
  5117
		}
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
  5118
		RETURN ( __MKCHARACTER(c) );
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
  5119
	    }
17360
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5120
	    if ((ret < 0)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5121
#ifdef ECONNRESET
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5122
		&& (__threadErrno != ECONNRESET)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5123
#endif
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5124
	    ){
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
  5125
		error = __mkSmallInteger(__threadErrno);
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
  5126
	    } else /* ret == 0 */ {
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
  5127
		__INST(hitEOF) = true;
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
  5128
	    }
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
  5129
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5130
    }
159
514c749165c3 *** empty log message ***
claus
parents: 108
diff changeset
  5131
%}.
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5132
    hitEOF == true ifTrue:[^ self pastEndRead].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5133
    error notNil ifTrue:[
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
  5134
	lastErrorNumber := error.
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
  5135
	^ self readError:error.
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5136
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5137
    handle isNil ifTrue:[^ self errorNotOpen].
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5138
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5139
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5140
    readAhead isNil ifTrue:[
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
  5141
	readAhead := self nextOrNil.
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
  5142
	readAhead isNil ifTrue:[
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
  5143
	    ^ self pastEndRead.
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
  5144
	].
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5145
    ].
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5146
    ^ readAhead
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5147
!
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5148
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5149
peekOrNil
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5150
    "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
  5151
     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
  5152
     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
  5153
     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
  5154
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5155
    |error|
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5156
%{
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5157
    FILEPOINTER f;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5158
    unsigned char c;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5159
    int ret, _buffered;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5160
    OBJ fp;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5161
    OBJ ra;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5162
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5163
    if ((ra = __INST(readAhead)) != nil) {
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
  5164
	if (__INST(binary) == true) {
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
  5165
	    RETURN ( ra );
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
  5166
	}
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
  5167
	c = __intVal(ra);
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
  5168
	RETURN ( __MKCHARACTER(c) );
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5169
    }
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5170
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5171
    __INST(lastErrorNumber) = nil;
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5172
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5173
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5174
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5175
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5176
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5177
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
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
  5178
	if (((fp = __INST(handle)) != nil)
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
  5179
	    && (__INST(mode) != @symbol(writeonly))
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
  5180
	) {
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
  5181
	    f = __FILEVal(fp);
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
  5182
	    _buffered = (__INST(buffered) == true);
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
  5183
	    if (_buffered) {
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
  5184
		__READING__(f)
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
  5185
	    }
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
  5186
	    __READBYTE__(ret, f, &c, _buffered, __INST(handleType));
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
  5187
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
  5188
	    if (ret > 0) {
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
  5189
		__UNGETC__(c, f, _buffered);
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
  5190
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
  5191
		if (__INST(binary) == true) {
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
  5192
		    RETURN ( __mkSmallInteger(c) );
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
  5193
		}
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
  5194
		RETURN ( __MKCHARACTER(c) );
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
  5195
	    }
17360
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5196
	    if ((ret < 0)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5197
#ifdef ECONNRESET
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5198
		&& (__threadErrno != ECONNRESET)
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5199
#endif
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5200
	    ){
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
  5201
		error = __mkSmallInteger(__threadErrno);
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
  5202
	    } else /* ret == 0 */ {
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
  5203
		__INST(hitEOF) = true;
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
  5204
	    }
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
  5205
	}
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5206
    }
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5207
%}.
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5208
    hitEOF == true ifTrue:[^ nil].
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5209
    error notNil ifTrue:[
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
  5210
	lastErrorNumber := error.
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
  5211
	^ self readError:error.
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5212
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5213
    handle isNil ifTrue:[^ self errorNotOpen].
10348
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5214
    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5215
7ed317f7f2be Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents: 10339
diff changeset
  5216
    readAhead isNil ifTrue:[
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
  5217
	readAhead := self nextOrNil.
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5218
    ].
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5219
    ^ readAhead
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5220
!
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5221
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5222
upToEnd
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5223
    "return a collection of the elements up-to the end.
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5224
     Return nil if the stream-end is reached before."
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5225
6569
f8336e46ec9f Speedup #upToEnd
Stefan Vogel <sv@exept.de>
parents: 6563
diff changeset
  5226
    |answerStream buffer count|
f8336e46ec9f Speedup #upToEnd
Stefan Vogel <sv@exept.de>
parents: 6563
diff changeset
  5227
f8336e46ec9f Speedup #upToEnd
Stefan Vogel <sv@exept.de>
parents: 6563
diff changeset
  5228
    buffer := self contentsSpecies new:4096.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5229
    [self atEnd] whileFalse:[
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5230
	count := self nextAvailableBytes:4096 into:buffer startingAt:1.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5231
	count ~~ 0 ifTrue:[
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5232
	    answerStream isNil ifTrue:[
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5233
		answerStream := WriteStream with:buffer.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5234
		answerStream position:count.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5235
		buffer := self contentsSpecies new:4096.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5236
	    ] ifFalse:[
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5237
		answerStream nextPutAll:buffer startingAt:1 to:count.
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5238
	    ].
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5239
	].
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5240
    ].
6758
bc933037a1dc oops - upToEnd failed if already at the end
Claus Gittinger <cg@exept.de>
parents: 6661
diff changeset
  5241
    answerStream isNil ifTrue:[^ self contentsSpecies new].
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5242
    ^ answerStream contents
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1281
diff changeset
  5243
7054
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  5244
    "
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  5245
     ('smalltalk.rc' asFilename readStream upToEnd)
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  5246
     =
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  5247
     ('smalltalk.rc' asFilename readStream contentsOfEntireFile)
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  5248
    "
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5249
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5250
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5251
!ExternalStream methodsFor:'rel5 protocol'!
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5252
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5253
atEndFile:handle
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5254
    "for migration to rel5 only"
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5255
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5256
    self primitiveFailed
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5257
!
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5258
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5259
nextByteFromFile:handle
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5260
    "for migration to rel5 only"
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5261
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5262
    self primitiveFailed
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5263
!
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5264
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5265
nextPutByte:aByte toFile:handle
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5266
    "for migration to rel5 only"
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5267
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5268
    self primitiveFailed
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5269
! !
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5270
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5271
!ExternalStream methodsFor:'testing'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5272
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5273
atEnd
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5274
    "return true, if position is at end"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5275
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  5276
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  5277
    FILEPOINTER f;
7875
eb7d48b0425d made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents: 7832
diff changeset
  5278
    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
  5279
    char c;
2861
c098e5766682 Fix #atEnd: for signed chars.
Stefan Vogel <sv@exept.de>
parents: 2854
diff changeset
  5280
    int ret, _buffered;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5281
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  5282
    if (__INST(hitEOF) == true) {
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
  5283
	RETURN (true);
3906
7f2d4a3b63e2 oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents: 3878
diff changeset
  5284
    }
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  5285
    pos = __INST(position);
7875
eb7d48b0425d made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents: 7832
diff changeset
  5286
    lim = __INST(readLimit);
eb7d48b0425d made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents: 7832
diff changeset
  5287
    if (lim != nil) {
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
  5288
	off_t _pos, _readLimit;
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
  5289
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
  5290
	_pos = __signedLongIntVal(pos);
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
  5291
	_pos = _pos - __intVal( @global(PositionableStream:ZeroPosition)) + 1;
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
  5292
	_readLimit = __signedLongIntVal(lim);
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
  5293
	if (_pos > _readLimit) {
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
  5294
	    RETURN (true);
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
  5295
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5296
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5297
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  5298
    __INST(lastErrorNumber) = nil;
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  5299
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5300
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5301
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5302
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5303
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5304
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
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
  5305
	if ((fp = __INST(handle)) != nil) {
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
  5306
	    f = __FILEVal(fp);
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
  5307
	    if (_buffered = (__INST(buffered) == true)) {
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
  5308
		__READING__(f);
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
  5309
	    } else {
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
  5310
		if (__INST(readAhead) != nil) {
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
  5311
		    RETURN (false);
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
  5312
		}
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
  5313
	    }
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
  5314
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
  5315
	    /*
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
  5316
	     * read ahead ...
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
  5317
	     */
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
  5318
	    do {
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5319
#ifdef WIN32
10339
972d0164125a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10328
diff changeset
  5320
# if 1
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
  5321
		__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
  5322
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5323
# else
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
  5324
		__BEGIN_INTERRUPTABLE__
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
  5325
		__READBYTE__(ret, f, &c, _buffered, __INST(handleType));
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
  5326
		__END_INTERRUPTABLE__
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5327
# endif
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5328
#else /* not WIN32 */
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
  5329
		__BEGIN_INTERRUPTABLE__
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
  5330
		__READBYTE__(ret, f, &c, _buffered, __INST(handleType));
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
  5331
		__END_INTERRUPTABLE__
4914
Claus Gittinger <cg@exept.de>
parents: 4913
diff changeset
  5332
#endif
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
  5333
	    } while ((ret < 0) && (__threadErrno == EINTR));
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
  5334
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
  5335
	    if (ret > 0) {
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
  5336
		__UNGETC__(c&0xff, f, _buffered);
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
  5337
		RETURN (false);
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
  5338
	    }
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
  5339
	    if (ret == 0) {
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
  5340
		__INST(hitEOF) = true;
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
  5341
		RETURN (true);
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
  5342
	    }
17360
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5343
#ifdef ECONNRESET
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5344
	    // connection reset by peer is also an EOF
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5345
	    if (__threadErrno == ECONNRESET) {
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5346
		__INST(hitEOF) = true;
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5347
		RETURN (true);
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5348
	    }
24b114ab935a ECONNRESET must be treated like EOF; not like an error.
Claus Gittinger <cg@exept.de>
parents: 17332
diff changeset
  5349
#endif
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
  5350
	    /* ret < 0 */
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
  5351
	    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
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
  5352
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5353
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5354
%}.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5355
    lastErrorNumber notNil ifTrue:[^ self readError].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5356
    handle isNil ifTrue:[^ self errorNotOpen].
5420
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5357
    mode == #writeonly ifTrue:[^ self errorWriteOnly].
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5358
    readAhead notNil ifTrue:[^ false].
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5359
cec8306dff39 migration
Claus Gittinger <cg@exept.de>
parents: 5417
diff changeset
  5360
    "/ migration support
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5361
    ^ self
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
  5362
	atEndFile:handle
3906
7f2d4a3b63e2 oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents: 3878
diff changeset
  5363
7f2d4a3b63e2 oops - readLimit was not handled in #atEnd
Claus Gittinger <cg@exept.de>
parents: 3878
diff changeset
  5364
    "Modified: / 30.10.1998 / 20:16:06 / cg"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5365
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5366
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5367
canReadWithoutBlocking
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5368
    "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
  5369
     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
  5370
     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
  5371
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5372
    ^ readAhead notNil
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5373
	or:[handle isNil]
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5374
	or:[mode == #writeonly
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5375
	or:[OperatingSystem readCheck:self fileDescriptor]]
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5376
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5377
    "
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5378
     |pipe|
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5379
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5380
     pipe := PipeStream readingFrom:'(sleep 10; echo hello)'.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5381
     pipe canReadWithoutBlocking ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  5382
	 Transcript showCR:'data available'
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5383
     ] ifFalse:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 7017
diff changeset
  5384
	 Transcript showCR:'no data available'
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5385
     ].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5386
     pipe close
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5387
    "
2986
c6957cf6128f Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
  5388
c6957cf6128f Handle readAhead in #canReadWithoutBlocking
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
  5389
    "Modified: 25.9.1997 / 13:08:45 / stefan"
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5390
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5391
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5392
canWriteWithoutBlocking
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5393
    "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
  5394
     (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
  5395
     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
  5396
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5397
    ^ handle isNil
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5398
	or:[mode == #readonly
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5399
	or:[OperatingSystem writeCheck:self fileDescriptor]]
3274
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  5400
!
92a24829f14a added #nextAvailableBytes - very useful for pipes & sockets.
Claus Gittinger <cg@exept.de>
parents: 3255
diff changeset
  5401
10995
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  5402
gotErrorOrEOF
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  5403
    "answerv true, if amn error or eof has been occured on the stream"
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  5404
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  5405
    ^ hitEOF == true or:[lastErrorNumber notNil]
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  5406
!
94ed80f3bfc7 changed #gotErrorOrEOF
sr
parents: 10989
diff changeset
  5407
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5408
isBinary
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5409
    "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
  5410
     The default when created is false."
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5411
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5412
    ^ binary
6269
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  5413
!
3a888523e3a6 #errorReporter and #reportOn class-methods
Stefan Vogel <sv@exept.de>
parents: 6255
diff changeset
  5414
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5415
isBlocking
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5416
    "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
  5417
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5418
    handle isNil ifTrue:[^ self errorNotOpen].
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5419
    ^ OperatingSystem isBlockingOn:self fileDescriptor
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5420
!
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5421
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5422
isExternalStream
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5423
    "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
  5424
     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
  5425
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5426
    ^ true
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5427
!
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5428
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5429
isOpen
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5430
    "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
  5431
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5432
    ^ handle notNil
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5433
!
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5434
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5435
isReadable
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5436
    "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
  5437
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5438
    ^ (mode ~~ #writeonly)
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5439
!
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5440
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5441
isWritable
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5442
    "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
  5443
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5444
    ^ (mode ~~ #readonly)
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5445
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5446
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5447
!ExternalStream methodsFor:'waiting for I/O'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5448
5101
901c91d6dd50 support to poll write-fds
Claus Gittinger <cg@exept.de>
parents: 4968
diff changeset
  5449
canBeSelected
901c91d6dd50 support to poll write-fds
Claus Gittinger <cg@exept.de>
parents: 4968
diff changeset
  5450
    "return true, if this stream can be selected upon"
901c91d6dd50 support to poll write-fds
Claus Gittinger <cg@exept.de>
parents: 4968
diff changeset
  5451
901c91d6dd50 support to poll write-fds
Claus Gittinger <cg@exept.de>
parents: 4968
diff changeset
  5452
    ^ OperatingSystem supportsSelect
901c91d6dd50 support to poll write-fds
Claus Gittinger <cg@exept.de>
parents: 4968
diff changeset
  5453
!
901c91d6dd50 support to poll write-fds
Claus Gittinger <cg@exept.de>
parents: 4968
diff changeset
  5454
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5455
readWaitWithTimeoutMs:timeout
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5456
    "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
  5457
     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
  5458
     If data is already available, return immediate.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5459
     Return true if a timeout occured (i.e. false, if data is available).
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5460
     The other threads are not affected by the wait."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5461
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5462
    |fd inputSema hasTimedout wasBlocked|
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5463
2120
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  5464
    readAhead notNil ifTrue:[^ false].
71c1870df7ba implemented 1-character readAhead for unbuffered streams;
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
  5465
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5466
    handle isNil ifTrue:[^ self errorNotOpen].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5467
    mode == #writeonly ifTrue:[^ self errorWriteOnly].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5468
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5469
    fd := self fileDescriptor.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5470
    (OperatingSystem readCheck:fd) ifTrue:[^ false].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5471
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5472
    wasBlocked := OperatingSystem blockInterrupts.
16463
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5473
    inputSema := Semaphore new name:'readWait'.
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5474
    [
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5475
	timeout notNil ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5476
	    Processor signal:inputSema afterMilliseconds:timeout.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5477
	].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5478
	Processor signal:inputSema onInput:fd.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5479
	Processor activeProcess state:#ioWait.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5480
	inputSema wait.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5481
	hasTimedout := timeout notNil and:[(OperatingSystem readCheck:fd) not].
16463
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5482
    ] ifCurtailed:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5483
	Processor disableSemaphore:inputSema.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5484
	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5485
    ].
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5486
    timeout notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5487
	Processor disableSemaphore:inputSema.
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5488
    ].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5489
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5490
    ^ hasTimedout
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5491
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5492
3490
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5493
readWriteWaitWithTimeoutMs:timeout
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5494
    "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
  5495
     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
  5496
     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
  5497
     Return immediate if the receiver is already ready.
5506
ffa0d32f4a51 Comment
Stefan Vogel <sv@exept.de>
parents: 5494
diff changeset
  5498
     The other threads are not affected by the wait."
3490
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5499
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5500
    |fd sema hasTimedout wasBlocked|
3490
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5501
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5502
    handle isNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5503
	^ self errorNotOpen
3490
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5504
    ].
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5505
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5506
    fd := self fileDescriptor.
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5507
    (OperatingSystem readWriteCheck:fd) ifTrue:[^ false].
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5508
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5509
    wasBlocked := OperatingSystem blockInterrupts.
16463
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5510
    sema := Semaphore new name:'readWriteWait'.
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5511
    [
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5512
	timeout notNil ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5513
	    Processor signal:sema afterMilliseconds:timeout
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5514
	].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5515
	Processor signal:sema onOutput:fd.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5516
	Processor signal:sema onInput:fd.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5517
	Processor activeProcess state:#ioWait.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5518
	sema wait.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5519
	hasTimedout := timeout notNil and:[(OperatingSystem readWriteCheck:fd) not].
16463
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5520
    ] ifCurtailed:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5521
	Processor disableSemaphore:sema.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5522
	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
3490
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5523
    ].
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5524
    timeout notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5525
	Processor disableSemaphore:sema.
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5526
    ].
3490
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5527
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5528
    ^ hasTimedout
3490
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5529
!
6a87c2b37fd4 added #readWriteWait methods.
Claus Gittinger <cg@exept.de>
parents: 3474
diff changeset
  5530
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5531
writeWaitWithTimeoutMs:timeout
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5532
    "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
  5533
     becomes ready for writing or a timeout (in milliseconds) expired.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5534
     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
  5535
     Return immediate if the receiver is already ready.
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5536
     The other threads are not affected by the wait."
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5537
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5538
    |fd outputSema hasTimedout wasBlocked|
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5539
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5540
    handle isNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5541
	^ self errorNotOpen
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5542
    ].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5543
    mode == #readonly ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5544
	^ self errorReadOnly
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5545
    ].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5546
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5547
    fd := self fileDescriptor.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5548
    (OperatingSystem writeCheck:fd) ifTrue:[^ false].
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5549
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5550
    wasBlocked := OperatingSystem blockInterrupts.
16463
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5551
    outputSema := Semaphore new name:'writeWait'.
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5552
    [
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5553
	timeout notNil ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5554
	    Processor signal:outputSema afterMilliseconds:timeout
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5555
	].
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5556
	Processor signal:outputSema onOutput:fd.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5557
	Processor activeProcess state:#ioWait.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5558
	outputSema wait.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5559
	hasTimedout := timeout notNil and:[(OperatingSystem writeCheck:fd) not].
16463
2b3728727f5e class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16373
diff changeset
  5560
    ] ifCurtailed:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5561
	Processor disableSemaphore:outputSema.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5562
	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5563
    ].
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5564
    timeout notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5565
	Processor disableSemaphore:outputSema.
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5566
    ].
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5567
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
16480
2b0c4915f31d class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 16463
diff changeset
  5568
    ^ hasTimedout
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5569
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5570
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5571
!ExternalStream methodsFor:'writing'!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5572
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5573
cr
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5574
    "append an end-of-line character (or CRLF if in crlf mode).
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5575
     reimplemented for speed"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5576
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  5577
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  5578
    FILEPOINTER f;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5579
    int len, cnt, _buffered;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5580
    OBJ fp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5581
    char *cp;
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5582
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5583
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5584
     || (__INST(handleType) == @symbol(filePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  5585
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5586
     || (__INST(handleType) == @symbol(socketFilePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5587
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5588
	__INST(lastErrorNumber) = nil;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5589
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5590
	    && (__INST(mode) != @symbol(readonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5591
	    && (__INST(binary) != true)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5592
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5593
	    f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5594
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5595
	    if (_buffered = (__INST(buffered) == true)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5596
		__WRITING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5597
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5598
	    {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5599
		OBJ mode = __INST(eolMode);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5600
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5601
		if (mode == @symbol(cr)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5602
		    cp = "\r"; len = 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5603
		} else if (mode == @symbol(crlf)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5604
		    cp = "\r\n"; len = 2;
17430
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5605
		} else if (mode == @symbol(eot)) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5606
		    cp = "\004"; len = 1;
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5607
		} else if (mode == @symbol(etx)) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5608
		    cp = "\003"; len = 1;
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5609
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5610
		    cp = "\n"; len = 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5611
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5612
	    }
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5613
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5614
	    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5615
		cnt = __win32_fwrite(cp, 1, len, f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5616
	    } else
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5617
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5618
	    {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5619
		__WRITEBYTES__(cnt, f, cp, len, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5620
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5621
	    if (cnt == len) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5622
		if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5623
		    INT np = __intVal(__INST(position)) + len;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5624
		    OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5625
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5626
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5627
		} else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5628
		    __INST(position) = nil; /* i.e: dont know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5629
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5630
		RETURN ( self );
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5631
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5632
	    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  5633
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5634
    }
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5635
%}.
2326
01fa309e48d9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2325
diff changeset
  5636
    lastErrorNumber notNil ifTrue:[self writeError. ^ self].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  5637
    handle isNil ifTrue:[self errorNotOpen. ^ self].
2326
01fa309e48d9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2325
diff changeset
  5638
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
5414
8557e4e71e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5323
diff changeset
  5639
    (binary==true) ifTrue:[self errorBinary. ^ self].
8557e4e71e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5323
diff changeset
  5640
17426
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5641
    (eolMode == #cr) ifTrue:[
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5642
	self nextPut:(Character return).
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5643
	^ self
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5644
    ].
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5645
    (eolMode == #crlf) ifTrue:[
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5646
	self nextPut:(Character return).
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5647
    ].
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5648
    self nextPut:(Character nl).
5414
8557e4e71e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5323
diff changeset
  5649
    ^ self
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5650
!
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5651
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5652
flush
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5653
    "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
  5654
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5655
%{
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5656
    OBJ fp;
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5657
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5658
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5659
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5660
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5661
     || (__INST(handleType) == @symbol(socketFilePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  5662
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5663
	if ((fp = __INST(handle)) != nil) {
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5664
	    if (__INST(mode) != @symbol(readonly)) {
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5665
		if (__INST(buffered) == true) {
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5666
		    FILEPOINTER f = __FILEVal(fp);
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  5667
#ifdef WIN32
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5668
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5669
			__win32_fflush(f);
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5670
		    } else {
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5671
			int rslt;
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5672
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5673
			do {
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5674
			    __threadErrno = 0;
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5675
			    rslt = __STX_C_CALL1("fflush", fflush, f);
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5676
			} while((rslt < 0) && (__threadErrno == EINTR));
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5677
		    }
12678
662465401b77 changed:
Stefan Vogel <sv@exept.de>
parents: 12674
diff changeset
  5678
#else /* ! WIN32 */
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5679
		    __BEGIN_INTERRUPTABLE__
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5680
		    FFLUSH(f);
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5681
		    __END_INTERRUPTABLE__
12678
662465401b77 changed:
Stefan Vogel <sv@exept.de>
parents: 12674
diff changeset
  5682
#endif /* ! WIN32 */
14068
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5683
		}
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5684
	    }
46a8289bf92a pass linenr to dumpObject
Claus Gittinger <cg@exept.de>
parents: 13940
diff changeset
  5685
	}
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5686
    }
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5687
%}
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5688
!
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5689
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5690
nextPut:aCharacter
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5691
    "write the argument, aCharacter - return nil if failed, self if ok.
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5692
     Only single-byte characters are currently supported"
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  5693
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  5694
    |error|
1067
1e7959af0821 removed all NOCONTEXT (last version did no good - PROTECT-stack overflow ...)
Claus Gittinger <cg@exept.de>
parents: 1066
diff changeset
  5695
%{
2593
d678527ef34e changes for NT; prepare to use HANDLES instead of FILES (NT)
Claus Gittinger <cg@exept.de>
parents: 2591
diff changeset
  5696
    FILEPOINTER f;
8209
908ba4b9543e check for written characters to be in 00..FF
Claus Gittinger <cg@exept.de>
parents: 8074
diff changeset
  5697
    unsigned codePoint;
908ba4b9543e check for written characters to be in 00..FF
Claus Gittinger <cg@exept.de>
parents: 8074
diff changeset
  5698
    unsigned char c;
1463
ace6b72b6f4a Use common macros to make interruptable system calls signal proof.
Stefan Vogel <sv@exept.de>
parents: 1422
diff changeset
  5699
    int cnt, _buffered;
7875
eb7d48b0425d made all position-handling code 32bit save
Claus Gittinger <cg@exept.de>
parents: 7832
diff changeset
  5700
    OBJ fp;
17332
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5701
    char buff[2];
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5702
    int nBytes = 1;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5703
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5704
    __INST(lastErrorNumber) = nil;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5705
    if ((__INST(handleType) == nil)
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5706
     || (__INST(handleType) == @symbol(filePointer))
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5707
     || (__INST(handleType) == @symbol(socketFilePointer))
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5708
     || (__INST(handleType) == @symbol(socketHandle))
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5709
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5710
	if (((fp = __INST(handle)) != nil)
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5711
	    && (__INST(mode) != @symbol(readonly))
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5712
	) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5713
	    if (__INST(binary) != true) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5714
		if (__isCharacter(aCharacter)) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5715
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5716
		    codePoint = __intVal(__characterVal(aCharacter));
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5717
		    if (codePoint <= 0xFF) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5718
			c = codePoint;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5719
			buff[0] = c; nBytes = 1;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5720
17426
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5721
			if (c == '\n') {
17430
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5722
			    OBJ mode = __INST(eolMode);
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5723
			    if (mode == @symbol(nl)) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5724
				// no EOL translation
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5725
			    } else if (mode == nil) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5726
				// no EOL translation
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5727
			    } else if (mode == @symbol(cr)) {
17332
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5728
				buff[0] = '\r';
17430
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5729
			    } else if (mode == @symbol(eot)) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5730
				buff[0] = '\004';
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5731
			    } else if (mode == @symbol(etx)) {
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5732
				buff[0] = '\003';
c8749e1c02ad class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 17426
diff changeset
  5733
			    } else if (mode == @symbol(crlf)) {
17426
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5734
				buff[0] = '\r';
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5735
				buff[1] = '\n';
6e49243ce4e0 fix: code treated everything but nl,cr,crnl symbols als cr (by accident).
Claus Gittinger <cg@exept.de>
parents: 17420
diff changeset
  5736
				nBytes = 2;
17332
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5737
			    }
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5738
			}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5739
    doWrite:
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5740
			f = __FILEVal(fp);
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5741
			if (! f) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5742
			    fprintf(stderr, "oops - fileHandle is NULL in nextPut:\n");
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5743
			    __INST(handle) = nil;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5744
			    goto out;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5745
			}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5746
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5747
			if (_buffered = (__INST(buffered) == true)) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5748
			    __WRITING__(f)
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5749
			}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5750
#ifdef WIN32
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5751
			if ((f == __win32_stdout()) || (f == __win32_stderr())) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5752
			    cnt = __win32_fwrite(buff, 1, nBytes, f);
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5753
			} else
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5754
#endif
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5755
			{
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5756
			    __WRITEBYTES__(cnt, f, buff, nBytes, _buffered, __INST(handleType));
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5757
			}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5758
			if (cnt == nBytes) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5759
			    if (__isSmallInteger(__INST(position))) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5760
				INT np = __intVal(__INST(position)) + nBytes;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5761
				OBJ t;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5762
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5763
				t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5764
			    } else {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5765
				__INST(position) = nil; /* i.e. do not know */
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5766
			    }
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5767
			    RETURN ( self );
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5768
			}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5769
			error = __mkSmallInteger(__threadErrno);
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5770
		    }
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5771
		}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5772
	    } else {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5773
		if (__isSmallInteger(aCharacter)) {
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5774
		    c = __intVal(aCharacter);
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5775
		    buff[0] = c; nBytes = 1;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5776
		    goto doWrite;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5777
		}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5778
	    }
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5779
	}
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5780
    }
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5781
out: ;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5782
%}.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5783
    error notNil ifTrue:[
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5784
	lastErrorNumber := error.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5785
	self writeError:error.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5786
	^ self
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5787
    ].
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5788
    handle isNil ifTrue:[self errorNotOpen. ^ self].
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5789
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5790
    binary == true ifFalse:[
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5791
	(aCharacter isCharacter not
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5792
	or:[aCharacter codePoint > 16rFF]) ifTrue:[
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5793
	    self argumentMustBeCharacter.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5794
	    ^ self.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5795
	].
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5796
    ] ifTrue:[
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5797
	aCharacter isInteger ifFalse:[
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5798
	    self argumentMustBeInteger.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5799
	    ^ self.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5800
	].
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5801
    ].
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5802
    "/ migration support
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5803
    self
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5804
	nextPutByte:aCharacter asInteger
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5805
	toFile:handle
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5806
!
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  5807
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5808
nextPutAll:aCollection
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5809
    "write all elements of the argument, aCollection.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5810
     Reimplemented for speed when writing strings or byteArrays.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5811
     For others, falls back to general method in superclass."
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5812
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5813
    |error|
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5814
%{
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5815
    FILEPOINTER f;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5816
    INT len, cnt;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5817
    OBJ fp;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5818
    int _buffered;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5819
    INT o_offs;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5820
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5821
    __INST(lastErrorNumber) = nil;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5822
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5823
    if ((__INST(handleType) == nil)
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5824
     || (__INST(handleType) == @symbol(filePointer))
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5825
     || (__INST(handleType) == @symbol(socketFilePointer))
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5826
     || (__INST(handleType) == @symbol(socketHandle))
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5827
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5828
	if (((fp = __INST(handle)) != nil)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5829
	    && (__INST(mode) != @symbol(readonly))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5830
	) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5831
	    f = __FILEVal(fp);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5832
	    if (! f) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5833
		fprintf(stderr, "oops - fileHandle is NULL in nextPutAll:\n");
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5834
		__INST(handle) = nil;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5835
		goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5836
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5837
	    if (_buffered = (__INST(buffered) == true)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5838
		__WRITING__(f)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5839
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5840
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5841
	    if (__isStringLike(aCollection)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5842
		OBJ mode = __INST(eolMode);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5843
		char *stringP = __stringVal(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5844
		len = __stringSize(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5845
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5846
		if (__INST(binary) != true
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5847
		    && ((mode == @symbol(cr))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5848
			|| (mode == @symbol(etx))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5849
			|| (mode == @symbol(eot))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5850
			|| (mode == @symbol(crlf)))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5851
		    && memchr(stringP, '\n', len) != NULL)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5852
		{
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5853
		    // there is a '\n' to be translated, replace it into a buffer
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5854
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5855
		    char *end;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5856
		    char sep[2];
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5857
		    int sepLen = 1;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5858
		    int bufLen;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5859
		    char *buf, *endBuf, *sp, *dp;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5860
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5861
		    sep[0] = '\n';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5862
		    if (mode == @symbol(crlf)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5863
			 sep[0] = '\r'; sep[1] = '\n'; sepLen = 2;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5864
		    } else if (mode == @symbol(cr)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5865
			 sep[0] = '\r';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5866
		    } else if (mode == @symbol(eot)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5867
			 sep[0] = '\004';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5868
		    } else if (mode == @symbol(etx)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5869
			 sep[0] = '\003';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5870
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5871
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5872
		    // estimate size of buffer - assume every 4th char is a separator
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5873
		    bufLen = (sepLen == 1) ? len : (len + ((len/4) + 1) * sepLen);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5874
		    buf = (char *)malloc(bufLen);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5875
		    if (buf == NULL) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5876
			error = __mkSmallInteger(ENOMEM);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5877
			goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5878
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5879
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5880
		    endBuf = buf + bufLen;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5881
		    end = stringP + len;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5882
		    for (sp = stringP, dp = buf; sp < end; sp++) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5883
			char c;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5884
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5885
			if ((dp+sepLen) >= endBuf) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5886
			    char *newBuf;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5887
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5888
			    bufLen = bufLen * 2;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5889
			    newBuf = (char *)realloc(buf, bufLen);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5890
			    if (newBuf == NULL) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5891
				free(buf);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5892
				error = __mkSmallInteger(ENOMEM);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5893
				goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5894
			    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5895
			    endBuf = newBuf + bufLen;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5896
			    dp = newBuf + (dp-buf);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5897
			    buf = newBuf;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5898
			}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5899
17475
a31762a088b7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17474
diff changeset
  5900
			if ((c = *sp) != '\n') {
a31762a088b7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17474
diff changeset
  5901
			    *dp++ = c;
a31762a088b7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17474
diff changeset
  5902
			} else {
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5903
			    *dp++ = sep[0];
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5904
			    if (sepLen == 2) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5905
				*dp++ = sep[1];
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5906
			    };
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5907
			}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5908
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5909
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5910
		    len = dp - buf;
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  5911
#ifdef WIN32
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5912
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5913
			cnt = __win32_fwrite(buf, 1, len, f);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5914
		    } else
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  5915
#endif
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5916
		    {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5917
			__WRITEBYTES__(cnt, f, buf, len, _buffered, __INST(handleType));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5918
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5919
		    free(buf);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5920
		} else  {  // No EOL conversion needed
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  5921
#ifdef WIN32
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5922
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5923
			cnt = __win32_fwrite(stringP, 1, len, f);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5924
		    } else
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  5925
#endif
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5926
		    {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5927
			o_offs = stringP - (char *)__InstPtr(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5928
			__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered, __INST(handleType));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5929
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5930
		}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5931
	    } else {   // Not a String
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5932
		if (__INST(binary) == true) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5933
		    INT offs;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5934
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5935
		    if (__isByteArrayLike(aCollection)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5936
			offs = 0;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5937
			len = __byteArraySize(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5938
		    } else if (__isBytes(aCollection)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5939
			offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5940
			len = __byteArraySize(aCollection) - offs;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5941
		    } else
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5942
			goto out;
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5943
#ifdef WIN32
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5944
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5945
			cnt = __win32_fwrite(__stringVal(aCollection), 1, len, f);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5946
		    } else
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5947
#endif
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5948
		    {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5949
			o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element) - (char *)__InstPtr(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5950
			o_offs += offs;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5951
			__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered, __INST(handleType));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5952
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5953
		} else  // Not binary mode
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5954
		    goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5955
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5956
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5957
	    // Now check for errors
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5958
	    if (cnt == len) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5959
		if (__isSmallInteger(__INST(position))) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5960
		    INT np = __intVal(__INST(position)) + len;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5961
		    OBJ t;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5962
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5963
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5964
		} else {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5965
		    __INST(position) = nil; /* i.e. do not know */
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5966
		}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5967
		RETURN (self);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5968
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5969
	    fprintf(stderr, "cnt=%d len=%d\n", cnt, len);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5970
	    error = __mkSmallInteger(__threadErrno);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5971
	}
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5972
    }
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5973
out: ;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5974
%}.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5975
    error notNil ifTrue:[
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5976
	lastErrorNumber := error.
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5977
	self writeError:error.
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  5978
	^ self
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5979
    ].
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5980
    handle isNil ifTrue:[self errorNotOpen. ^ self].
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5981
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5982
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5983
    ^ super nextPutAll:aCollection
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5984
!
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5985
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5986
nextPutAll:initialWriteCount from:buffer startingAt:initialOffset
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5987
    "redefined, to wait until stream is writable, to avoid blocking in a write.
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5988
     I don't know how to write non-bytes to a external stream, but let superclass handle this."
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5989
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5990
    buffer isByteCollection ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  5991
	^ self nextPutBytes:initialWriteCount from:buffer startingAt:initialOffset.
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5992
    ].
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5993
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5994
    ^ super nextPutAll:initialWriteCount from:buffer startingAt:initialOffset
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5995
!
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  5996
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5997
nextPutAll:aCollection startingAt:start to:stop
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5998
    "write a range of elements of the argument, aCollection.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  5999
     Reimplemented for speed when writing strings or byteArrays.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6000
     For others, falls back to general method in superclass."
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6001
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6002
     |error|
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6003
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6004
%{
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6005
    FILEPOINTER f;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6006
    int offs, len, cnt, iStart, iStop, _buffered;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6007
    OBJ fp;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6008
    int o_offs;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6009
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6010
    __INST(lastErrorNumber) = nil;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6011
    if ((__INST(handleType) == nil)
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6012
     || (__INST(handleType) == @symbol(filePointer))
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6013
     || (__INST(handleType) == @symbol(socketFilePointer))
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6014
     || (__INST(handleType) == @symbol(socketHandle))
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6015
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6016
	if (((fp = __INST(handle)) != nil)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6017
	    && (__INST(mode) != @symbol(readonly))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6018
	    && __bothSmallInteger(start, stop)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6019
	) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6020
	    f = __FILEVal(fp);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6021
	    if (_buffered = (__INST(buffered) == true)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6022
		__WRITING__(f)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6023
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6024
	    iStart = __intVal(start);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6025
	    iStop = __intVal(stop);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6026
	    if ((iStart < 1) || (iStop < iStart)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6027
		RETURN(self);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6028
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6029
	    if (__isStringLike(aCollection)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6030
		char *stringP;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6031
		OBJ mode = __INST(eolMode);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6032
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6033
		len = __stringSize(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6034
		if (iStop > len) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6035
		    RETURN(self);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6036
		}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6037
		if (iStop > len)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6038
		    iStop = len;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6039
		len = iStop - iStart + 1;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6040
		stringP = __stringVal(aCollection) + iStart - 1;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6041
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6042
		if (__INST(binary) != true
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6043
		    && ((mode == @symbol(cr))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6044
			|| (mode == @symbol(etx))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6045
			|| (mode == @symbol(eot))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6046
			|| (mode == @symbol(crlf)))
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6047
		    && memchr(stringP, '\n', len) != NULL)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6048
		{
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6049
		    // see if there is a \n which needs to be translated, replace it
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6050
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6051
		    char *end = stringP + len;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6052
		    char sep[2];
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6053
		    int sepLen = 1;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6054
		    int bufLen;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6055
		    char *buf, *endBuf, *sp, *dp;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6056
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6057
		    sep[0] = '\n';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6058
		    if (mode == @symbol(crlf)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6059
			 sep[0] = '\r'; sep[1] = '\n'; sepLen = 2;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6060
		    } else if (mode == @symbol(cr)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6061
			 sep[0] = '\r';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6062
		    } else if (mode == @symbol(eot)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6063
			 sep[0] = '\004';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6064
		    } else if (mode == @symbol(etx)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6065
			 sep[0] = '\003';
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6066
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6067
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6068
		    // estimate size of buffer - assume every 4th char is a separator
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6069
		    bufLen = (sepLen == 1) ? len : (len + ((len/4) + 1) * sepLen);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6070
		    buf = (char *)malloc(bufLen);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6071
		    if (buf == NULL) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6072
			error = __mkSmallInteger(ENOMEM);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6073
			goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6074
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6075
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6076
		    endBuf = buf + bufLen;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6077
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6078
		    for (sp = stringP, dp = buf; sp < end; sp++) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6079
			char c;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6080
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6081
			if ((dp+sepLen) >= endBuf) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6082
			    char *newBuf;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6083
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6084
			    bufLen = bufLen * 2;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6085
			    newBuf = (char *)realloc(buf, bufLen);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6086
			    if (newBuf == NULL) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6087
				free(buf);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6088
				error = __mkSmallInteger(ENOMEM);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6089
				goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6090
			    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6091
			    endBuf = newBuf + bufLen;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6092
			    dp = newBuf + (dp-buf);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6093
			    buf = newBuf;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6094
			}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6095
17475
a31762a088b7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17474
diff changeset
  6096
			if ((c = *sp) == '\n') {
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6097
			    *dp++ = sep[0];
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6098
			    if (sepLen == 2) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6099
				*dp++ = sep[1];
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6100
			    };
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6101
			} else {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6102
			    *dp++ = c;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6103
			}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6104
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6105
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6106
		    len = dp - buf;
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  6107
#ifdef WIN32
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6108
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6109
			cnt = __win32_fwrite(buf, 1, len, f);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6110
		    } else
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  6111
#endif
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6112
		    {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6113
			__WRITEBYTES__(cnt, f, buf, len, _buffered, __INST(handleType));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6114
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6115
		    free(buf);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6116
		} else  {  // No EOL conversion needed
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  6117
#ifdef WIN32
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6118
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6119
			cnt = __win32_fwrite(stringP, 1, len, f);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6120
		    } else
17420
f0895ae69f69 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17418
diff changeset
  6121
#endif
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6122
		    {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6123
			o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6124
			__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered, __INST(handleType));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6125
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6126
		}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6127
	    } else {  // Not a string
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6128
		if (__INST(binary) == true) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6129
		    int offs;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6130
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6131
		    if (__isByteArrayLike(aCollection)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6132
			offs = 0;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6133
			len = __byteArraySize(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6134
		    } else if (__isBytes(aCollection)) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6135
			offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6136
			len = __byteArraySize(aCollection) - offs;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6137
		    } else
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6138
			goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6139
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6140
		    if (iStop > len) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6141
			RETURN(self);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6142
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6143
		    if (iStop > len)
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6144
			iStop = len;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6145
		    len = iStop - iStart + 1;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6146
		    offs += iStart - 1;
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6147
#ifdef WIN32
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6148
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6149
			cnt = __win32_fwrite(__stringVal(aCollection)+iStart-1, 1, len, f);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6150
		    } else
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6151
#endif
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6152
		    {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6153
			o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6154
			__WRITEBYTES_OBJ__(cnt, f,  aCollection, o_offs+offs, len, _buffered, __INST(handleType));
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6155
		    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6156
		} else
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6157
		    goto out;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6158
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6159
	    if (cnt == len) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6160
		if (__isSmallInteger(__INST(position))) {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6161
		    INT np = __intVal(__INST(position)) + len;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6162
		    OBJ t;
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6163
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6164
		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6165
		} else {
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6166
		    __INST(position) = nil; /* i.e. do not know */
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6167
		}
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6168
		RETURN (self);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6169
	    }
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6170
	    error = __mkSmallInteger(__threadErrno);
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6171
	}
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6172
    }
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6173
out: ;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6174
%}.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6175
    error notNil ifTrue:[
17474
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6176
	lastErrorNumber := error.
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6177
	self writeError:error.
18c7b1b817b4 oops - strcpy was wrong here (not 0 terminated)
Claus Gittinger <cg@exept.de>
parents: 17473
diff changeset
  6178
	^ self
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6179
    ].
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6180
    ^ super nextPutAll:aCollection startingAt:start to:stop
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6181
!
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6182
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6183
nextPutAllUnicode:aString
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6184
    "normal streams can not handle multi-byte characters, so convert them to utf8.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6185
     This is needed, so that you can do ('something' asUnicode16String errorPrintCR)"
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6186
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6187
    aString do:[:eachCharacter|
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6188
	self nextPutUtf8:eachCharacter.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6189
    ].
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6190
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6191
    "
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6192
	'Bönnigheim' asUnicode16String errorPrintCR
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6193
    "
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6194
!
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6195
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6196
nextPutBytes:count from:anObject startingAt:start
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6197
    "write count bytes from an object starting at index start.
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6198
     return the number of bytes written - which could be 0.
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6199
     The object must have non-pointer indexed instvars
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6200
     (i.e. be a ByteArray, String, Float- or DoubleArray),
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6201
     or an externalBytes object (with known size).
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6202
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6203
     Use with care - non object oriented i/o.
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6204
     Warning:
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6205
	in general, you cannot use this method to pass non-byte data to other
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6206
	architectures (unless you prepared the buffer with care),
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6207
	since it does not care for byte order or float representation."
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6208
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6209
    |error|
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6210
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6211
%{
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6212
#ifdef __SCHTEAM__
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6213
    char[] chars;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6214
    byte[] bytes;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6215
    STObject handle = self.instVarAt(I_handle);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6216
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6217
    if (anObject.isSTString()) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6218
	chars = anObject.asSTString().characters;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6219
	handle.writeCharacters(chars, start.intValue(), count.intValue());
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6220
	return context._RETURN(count);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6221
    }
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6222
#else
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6223
    FILEPOINTER f;
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6224
    int cnt, len, offs, ret;
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6225
    int objSize, nInstBytes, _buffered;
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6226
    char *extPtr;
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6227
    OBJ fp;
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6228
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6229
    __INST(lastErrorNumber) = nil;
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6230
    if ((__INST(handleType) == nil)
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6231
     || (__INST(handleType) == @symbol(filePointer))
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6232
     || (__INST(handleType) == @symbol(socketFilePointer))
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6233
     || (__INST(handleType) == @symbol(socketHandle))
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6234
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6235
	if (((fp = __INST(handle)) != nil)
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6236
	    && (__INST(mode) != @symbol(readonly))
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6237
	    && __bothSmallInteger(count, start)
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6238
	) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6239
	    len = __intVal(count);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6240
	    offs = __intVal(start) - 1;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6241
	    f = __FILEVal(fp);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6242
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6243
	    if (__isExternalBytesLike(anObject)) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6244
		OBJ sz;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6245
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6246
		nInstBytes = 0;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6247
		extPtr = (char *)__externalBytesAddress(anObject);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6248
		if (extPtr == NULL) goto bad;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6249
		sz = __externalBytesSize(anObject);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6250
		if (__isSmallInteger(sz)) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6251
		    objSize = __intVal(sz);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6252
		} else {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6253
		    objSize = 0; /* unknown */
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6254
		}
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6255
	    } else {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6256
		OBJ oClass = __Class(anObject);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6257
		int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6258
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6259
		nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6260
		switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6261
		    case BYTEARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6262
		    case WORDARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6263
		    case LONGARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6264
		    case SWORDARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6265
		    case SLONGARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6266
		    case FLOATARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6267
			break;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6268
		    case DOUBLEARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6269
# ifdef __NEED_DOUBLE_ALIGN
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6270
			nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6271
# endif
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6272
			break;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6273
		    case LONGLONGARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6274
		    case SLONGLONGARRAY:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6275
# ifdef __NEED_LONGLONG_ALIGN
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6276
			nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6277
# endif
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6278
			break;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6279
		    default:
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6280
			goto bad;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6281
		}
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6282
		extPtr = (char *)0;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6283
		objSize = __Size(anObject) - nInstBytes;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6284
	    }
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6285
	    if ( (offs >= 0) && (len >= 0) && (objSize >= (len + offs)) ) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6286
		if (_buffered = (__INST(buffered) == true)) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6287
		    __WRITING__(f)
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6288
		}
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6289
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6290
		if (extPtr) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6291
# ifdef WIN32
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6292
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6293
			cnt = __win32_fwrite(extPtr+offs, 1, len, f);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6294
		    } else
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6295
# endif
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6296
		    {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6297
			__WRITEBYTES__(cnt, f, extPtr+offs, len, _buffered, __INST(handleType));
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6298
		    }
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6299
		} else {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6300
		    /*
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6301
		     * on interrupt, anObject may be moved to another location.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6302
		     * So we pass anObject, and the offset to the __WRITEBYTES_OBJ__ macro.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6303
		     */
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6304
		    offs += nInstBytes;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6305
# ifdef WIN32
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6306
		    if ((f == __win32_stdout()) || (f == __win32_stderr())) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6307
			cnt = __win32_fwrite((char *)anObject+offs, 1, len, f);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6308
		    } else
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6309
# endif
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6310
		    {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6311
			 __WRITEBYTES_OBJ__(cnt, f, anObject, offs, len, _buffered, __INST(handleType));
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6312
		    }
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6313
		}
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6314
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6315
		if (cnt >= 0) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6316
		    if (__isSmallInteger(__INST(position))) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6317
			INT np = __intVal(__INST(position)) + cnt;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6318
			OBJ t;
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6319
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6320
			t = __MKINT(np); __INST(position) = t; __STORE(self, t);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6321
		    } else {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6322
			__INST(position) = nil; /* i.e. do not know */
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6323
		    }
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6324
		    RETURN ( __mkSmallInteger(cnt) );
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6325
		} else /* cnt < 0 */ {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6326
		    if (
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6327
# ifdef EWOULDBLOCK
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6328
			(__threadErrno == EWOULDBLOCK) ||
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6329
# endif
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6330
			(__threadErrno == EAGAIN)
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6331
		    ) {
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6332
			RETURN ( __mkSmallInteger(0) );
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6333
		    }
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6334
		    __INST(position) = nil; /* i.e. do not know */
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6335
		    error = __mkSmallInteger(__threadErrno);
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6336
		}
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6337
	    }
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6338
	}
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6339
    }
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6340
bad: ;
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6341
#endif /* not SCHTEAM */
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6342
%}.
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6343
    error notNil ifTrue:[
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6344
	lastErrorNumber := error.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6345
	self writeError:error.
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6346
	^ 0
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6347
    ].
17634
76a10ce9c870 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17618
diff changeset
  6348
    handle isNil ifTrue:[self errorNotOpen. ^ 0].
76a10ce9c870 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17618
diff changeset
  6349
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ 0].
76a10ce9c870 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17618
diff changeset
  6350
    self primitiveFailed.
76a10ce9c870 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17618
diff changeset
  6351
    ^ 0.
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6352
!
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6353
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6354
nextPutUnicode:aCharacter
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6355
    "normal streams can not handle multi-byte characters, so convert them to utf8"
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6356
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6357
    self nextPutUtf8:aCharacter.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6358
!
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6359
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6360
nextPutUtf16:aCharacter
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6361
    "append my UTF-16 representation to the argument, aStream.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6362
     UTF-16 can encode only characters with code points between 0 to 16r10FFFF."
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6363
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6364
    |codePoint|
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6365
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6366
    codePoint := aCharacter codePoint.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6367
    (codePoint <= 16rD7FF
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6368
      or:[codePoint >= 16rE000 and:[codePoint <= 16rFFFF]]) ifTrue:[
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6369
	self nextPutShort:codePoint MSB:true.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6370
    ] ifFalse:[codePoint <= 16r10FFFF ifTrue:[
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6371
	|highBits lowBits|
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6372
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6373
	codePoint := codePoint - 16r100000.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6374
	highBits := codePoint bitShift:-10.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6375
	lowBits := codePoint bitAnd:16r3FF.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6376
	self nextPutShort:highBits+16rD800 MSB:true.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6377
	self nextPutShort:lowBits+16rDC00 MSB:true.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6378
    ] ifFalse:[
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6379
	EncodingError raiseWith:aCharacter errorString:'Character cannot be encoded as UTF-16'.
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6380
    ]].
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6381
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6382
    "
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6383
	(FileStream newTemporary
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6384
	    nextPutUtf16:$B;
17608
03f28a6ee650 class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17533
diff changeset
  6385
	    nextPutUtf16:$Ä;
17418
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6386
	    nextPutUtf16:(Character codePoint:16r10CCCC);
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6387
	    reset;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6388
	    binary;
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6389
	    contents)
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6390
    "
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6391
!
e574f4adb86b class: ExternalStream
Stefan Vogel <sv@exept.de>
parents: 17360
diff changeset
  6392
17332
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6393
old_nextPut:aCharacter
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6394
    "write the argument, aCharacter - return nil if failed, self if ok.
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6395
     Only single-byte characters are currently supported"
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6396
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6397
    |error|
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6398
%{
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6399
    FILEPOINTER f;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6400
    unsigned codePoint;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6401
    unsigned char c;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6402
    int cnt, _buffered;
f223abac93ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 17115
diff changeset
  6403
    OBJ fp;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  6404
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1119
diff changeset
  6405
    __INST(lastErrorNumber) = nil;
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  6406
    if ((__INST(handleType) == nil)
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  6407
     || (__INST(handleType) == @symbol(filePointer))
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  6408
     || (__INST(handleType) == @symbol(socketFilePointer))
16368
1efe4d36fd3f Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 16308
diff changeset
  6409
     || (__INST(handleType) == @symbol(socketHandle))
12548
6a2abe88a7a2 handleTypes
Claus Gittinger <cg@exept.de>
parents: 12460
diff changeset
  6410
     || (__INST(handleType) == @symbol(pipeFilePointer))) {
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6411
	if (((fp = __INST(handle)) != nil)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6412
	    && (__INST(mode) != @symbol(readonly))
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6413
	) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6414
	    if (__INST(binary) != true) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6415
		if (__isCharacter(aCharacter)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6416
		    codePoint = __intVal(__characterVal(aCharacter));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6417
		    if (codePoint <= 0xFF) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6418
			c = codePoint;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  6419
    doWrite:
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6420
			f = __FILEVal(fp);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6421
			if (! f) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6422
			    fprintf(stderr, "oops - fileHandle is NULL in nextPut:\n");
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6423
			    __INST(handle) = nil;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6424
			    goto out;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6425
			}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6426
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6427
			if (_buffered = (__INST(buffered) == true)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6428
			    __WRITING__(f)
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6429
			}
9053
f3f3d48d6f44 fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents: 9024
diff changeset
  6430
#ifdef WIN32
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6431
			if ((f == __win32_stdout()) || (f == __win32_stderr())) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6432
			    cnt = __win32_fwrite(&c, 1, 1, f);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6433
			} else
9053
f3f3d48d6f44 fix writing to stdout/stderr for winstx non-console operation
Claus Gittinger <cg@exept.de>
parents: 9024
diff changeset
  6434
#endif
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6435
			__WRITEBYTE__(cnt, f, &c, _buffered, __INST(handleType));
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6436
			if (cnt == 1) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6437
			    if (__isSmallInteger(__INST(position))) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6438
				INT np = __intVal(__INST(position)) + 1;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6439
				OBJ t;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6440
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6441
				t = __MKINT(np); __INST(position) = t; __STORE(self, t);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6442
			    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6443
				__INST(position) = nil; /* i.e. do not know */
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6444
			    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6445
			    RETURN ( self );
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6446
			}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6447
			error = __mkSmallInteger(__threadErrno);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6448
		    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6449
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6450
	    } else {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6451
		if (__isSmallInteger(aCharacter)) {
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6452
		    c = __intVal(aCharacter);
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6453
		    goto doWrite;
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6454
		}
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6455
	    }
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6456
	}
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  6457
    }
7737
616d849b7838 fileHandle check
Claus Gittinger <cg@exept.de>
parents: 7700
diff changeset
  6458
out: ;
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  6459
%}.
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  6460
    error notNil ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6461
	lastErrorNumber := error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6462
	self writeError:error.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6463
	^ self
16296
f8548919e174 Fix error handling.
Stefan Vogel <sv@exept.de>
parents: 15994
diff changeset
  6464
    ].
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11751
diff changeset
  6465
    handle isNil ifTrue:[self errorNotOpen. ^ self].
2331
d6ddf452adc9 return codes in error cases
Claus Gittinger <cg@exept.de>
parents: 2326
diff changeset
  6466
    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
5417
0478ac31e042 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5414
diff changeset
  6467
    binary == true ifFalse:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6468
	(aCharacter isCharacter not
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6469
	or:[aCharacter codePoint > 16rFF]) ifTrue:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6470
	    self argumentMustBeCharacter.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6471
	    ^ self.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6472
	].
5417
0478ac31e042 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5414
diff changeset
  6473
    ] ifTrue:[
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6474
	aCharacter isInteger ifFalse:[
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6475
	    self argumentMustBeInteger.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6476
	    ^ self.
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6477
	].
5417
0478ac31e042 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5414
diff changeset
  6478
    ].
0478ac31e042 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5414
diff changeset
  6479
    "/ migration support
8818
4b1ffc19e3ce remerged WIN32 and Unix versions into common source
Claus Gittinger <cg@exept.de>
parents: 8616
diff changeset
  6480
    self
16508
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6481
	nextPutByte:aCharacter asInteger
f2d6afb19d35 added tcgetattr
Claus Gittinger <cg@exept.de>
parents: 16493
diff changeset
  6482
	toFile:handle
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  6483
! !
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  6484
2045
1ad17ca6520f fall back to super>>nextPutLine: if argument is not a string.
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
  6485
!ExternalStream class methodsFor:'documentation'!
730
62643519bf68 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 711
diff changeset
  6486
62643519bf68 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 711
diff changeset
  6487
version
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6488
    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.417 2015-05-03 12:38:52 cg Exp $'
12398
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  6489
!
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  6490
7243f8014d98 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 12395
diff changeset
  6491
version_CVS
18312
b414c7a96a1e comment sonly
Claus Gittinger <cg@exept.de>
parents: 18218
diff changeset
  6492
    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.417 2015-05-03 12:38:52 cg Exp $'
730
62643519bf68 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 711
diff changeset
  6493
! !
6804
caf6a4521d4c added utility: ExternalStream >> copyToEndInto:
Claus Gittinger <cg@exept.de>
parents: 6758
diff changeset
  6494
14603
f73f90e25023 class: ExternalStream
Claus Gittinger <cg@exept.de>
parents: 14518
diff changeset
  6495
612
e640bef6170e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 593
diff changeset
  6496
ExternalStream initialize!