FileStream.st
author Claus Gittinger <cg@exept.de>
Wed, 30 Jul 1997 13:13:17 +0200
changeset 2808 3a9ce7aadad8
parent 2796 b45639a2fc1e
child 2811 21136662e8f1
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
ExternalStream subclass:#FileStream
1088
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
    14
	instanceVariableNames:'pathName'
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
    15
	classVariableNames:''
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
    16
	poolDictionaries:''
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
    17
	category:'Streams-External'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
216
a8abff749575 *** empty log message ***
claus
parents: 173
diff changeset
    20
!FileStream primitiveDefinitions!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
%{
a27a279701f8 Initial revision
claus
parents:
diff changeset
    22
#include <stdio.h>
437
claus
parents: 384
diff changeset
    23
#define _STDIO_H_INCLUDED_
claus
parents: 384
diff changeset
    24
10
claus
parents: 5
diff changeset
    25
#include <errno.h>
437
claus
parents: 384
diff changeset
    26
#define _ERRNO_H_INCLUDED_
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    27
a27a279701f8 Initial revision
claus
parents:
diff changeset
    28
#ifdef transputer
a27a279701f8 Initial revision
claus
parents:
diff changeset
    29
# include <iocntrl.h>
a27a279701f8 Initial revision
claus
parents:
diff changeset
    30
# ifndef fileno
a27a279701f8 Initial revision
claus
parents:
diff changeset
    31
   /* kludge: inmos forgot fileno */
a27a279701f8 Initial revision
claus
parents:
diff changeset
    32
#  define fileno(f)     ((f)->__file)
a27a279701f8 Initial revision
claus
parents:
diff changeset
    33
# endif
a27a279701f8 Initial revision
claus
parents:
diff changeset
    34
#else
a27a279701f8 Initial revision
claus
parents:
diff changeset
    35
# include <sys/types.h>
a27a279701f8 Initial revision
claus
parents:
diff changeset
    36
# include <sys/stat.h>
a27a279701f8 Initial revision
claus
parents:
diff changeset
    37
#endif
10
claus
parents: 5
diff changeset
    38
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
    39
#ifdef hpux
68
59faa75185ba *** empty log message ***
claus
parents: 49
diff changeset
    40
# define fileno(f)      ((f->__fileH << 8) | (f->__fileL))
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
    41
#endif
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
    42
10
claus
parents: 5
diff changeset
    43
#ifndef SEEK_SET
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
    44
# define SEEK_SET       0
10
claus
parents: 5
diff changeset
    45
#endif
claus
parents: 5
diff changeset
    46
#ifndef SEEK_CUR
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
    47
# define SEEK_CUR       1
10
claus
parents: 5
diff changeset
    48
#endif
claus
parents: 5
diff changeset
    49
#ifndef SEEK_END
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
    50
# define SEEK_END       2
10
claus
parents: 5
diff changeset
    51
#endif
claus
parents: 5
diff changeset
    52
223
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
    53
/*
2808
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    54
 * not all systems have time_t and off_t
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    55
 * explicit add of those we know to have ...
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    56
 */
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    57
#ifdef __osf__
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    58
# define OFF_T  off_t
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    59
#endif
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    60
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    61
#ifndef OFF_T
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    62
# define OFF_T  long
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    63
#endif
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    64
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
    65
/*
223
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
    66
 * on some systems errno is a macro ... check for it here
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
    67
 */
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
    68
#ifndef errno
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
    69
 extern errno;
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
    70
#endif
3075043790b8 immediateInterr & errno cleanup
claus
parents: 216
diff changeset
    71
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    72
%}
173
58e9778954bc reposition
claus
parents: 159
diff changeset
    73
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    75
!FileStream class methodsFor:'documentation'!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    76
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    77
copyright
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    78
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    79
 COPYRIGHT (c) 1989 by Claus Gittinger
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    80
	      All Rights Reserved
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    81
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    82
 This software is furnished under a license and may be used
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    83
 only in accordance with the terms of that license and with the
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    84
 inclusion of the above copyright notice.   This software may not
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    85
 be provided or otherwise made available to, or used by, any
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    86
 other person.  No title to or ownership of the software is
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    87
 hereby transferred.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    88
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    89
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    90
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    91
documentation
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    92
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    93
    This class provides access to the operating systems underlying file
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    94
    system (i.e. its an interface to the stdio library).
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    95
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    96
    Notice, that on some systems, the standard I/O library has performance
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    97
    problems when a file is opened for readwrite. 
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    98
    For best results, open files either readonly or writeonly.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    99
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   100
    [author:]
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   101
	Claus Gittinger
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   102
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   103
    [see also:]
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   104
	Filename DirectoryStream PipeStream Socket
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   105
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   106
! !
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   107
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   108
!FileStream class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   110
appendingOldFileNamed:filename
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   111
    "return a FileStream for existing file named filename, aString.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   112
     The file is opened for writeonly access."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   113
a27a279701f8 Initial revision
claus
parents:
diff changeset
   114
    |newStream|
2
claus
parents: 1
diff changeset
   115
    newStream := self new pathName:filename.
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   116
    newStream openForAppending isNil ifTrue:[^nil].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   117
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   118
    this is not a good idea; I might like to read the written stuff ...
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   119
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   120
    newStream readLimit:(newStream size).
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   121
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   122
    ^ newStream
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   123
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   124
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   125
appendingOldFileNamed:filename in:aDirectory
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   126
    "return a FileStream for existing file named filename, aString
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   127
     in aDirectory, a FileDirectory.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   128
     The file is opened for writeonly access."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   129
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   130
    |newStream|
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   131
    newStream := self new pathName:filename in:aDirectory.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   132
    newStream openForAppending isNil ifTrue:[^nil].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   133
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   134
    this is not a good idea; I might like to read the written stuff ...
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   135
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   136
    newStream readLimit:(newStream size).
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   137
"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
    ^ newStream
a27a279701f8 Initial revision
claus
parents:
diff changeset
   139
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   140
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   141
fileNamed:filename
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   142
    "return a stream on file filename - if the file does not
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   143
     already exist, create it.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   144
     The file is opened for read/write access."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   145
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   146
    |stream|
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   147
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   148
    stream := self oldFileNamed:filename.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   149
    stream isNil ifTrue:[
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
	stream := self newFileNamed:filename
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   151
    ].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   152
    ^ stream
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   153
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   154
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   155
fileNamed:filename in:aDirectory
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   156
    "return a stream on file filename - if the file does not
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   157
     already exist, create it.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
     The file is opened for read/write access."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   159
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   160
    |stream|
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   161
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   162
    stream := self oldFileNamed:filename in:aDirectory.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   163
    stream isNil ifTrue:[
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   164
	stream := self newFileNamed:filename in:aDirectory
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   165
    ].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   166
    ^ stream
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   167
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   168
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   169
newFileForWritingNamed:filename
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   170
    "return a FileStream for new file named filename, aString.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   171
     If the file exists, it is truncated, otherwise created.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   172
     The file is opened for writeonly access."
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   173
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   174
    |newStream|
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   175
    newStream := self new pathName:filename.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   176
    newStream createForWriting isNil ifTrue:[^nil].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   177
    ^ newStream
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   178
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   179
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   180
newFileForWritingNamed:filename in:aDirectory
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   181
    "return a FileStream for new file named filename, aString
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   182
     in aDirectory, a FileDirectory.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   183
     If the file exists, it is truncated, otherwise created.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   184
     The file is opened for writeonly access."
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   185
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   186
    |newStream|
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   187
    newStream := self new pathName:filename in:aDirectory.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   188
    newStream createForWriting isNil ifTrue:[^nil].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   189
    ^ newStream
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   190
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   191
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   192
newFileNamed:filename
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   193
    "return a FileStream for new file named filename, aString.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   194
     If the file exists, it is truncated, otherwise created.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   195
     The file is opened for read/write access."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   196
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   197
    |newStream|
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   198
    newStream := self new pathName:filename.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   199
    newStream createForReadWrite isNil ifTrue:[^nil].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   200
    ^ newStream
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   201
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   202
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   203
newFileNamed:filename in:aDirectory
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   204
    "return a FileStream for new file named filename, aString
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   205
     in aDirectory, a FileDirectory.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   206
     If the file exists, it is truncated, otherwise created.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   207
     The file is opened for read/write access."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   208
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   209
    |newStream|
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   210
    newStream := self new pathName:filename in:aDirectory.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   211
    newStream createForReadWrite isNil ifTrue:[^nil].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   212
    ^ newStream
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   213
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   214
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   215
oldFileNamed:filename
a27a279701f8 Initial revision
claus
parents:
diff changeset
   216
    "return a FileStream for existing file named filename, aString.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   217
     The file is opened for read/write access."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   218
a27a279701f8 Initial revision
claus
parents:
diff changeset
   219
    |newStream|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   220
a27a279701f8 Initial revision
claus
parents:
diff changeset
   221
    (OperatingSystem isReadable:filename) ifFalse:[^nil].
2
claus
parents: 1
diff changeset
   222
    newStream := self new pathName:filename.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   223
    newStream readwrite.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   224
    newStream openForReadWrite isNil ifTrue:[^nil].
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   225
"
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   226
    this is not a good idea; someone else might be appending ...
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   227
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   228
    newStream readLimit:(newStream size).
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   229
"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   230
    ^ newStream
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   232
a27a279701f8 Initial revision
claus
parents:
diff changeset
   233
oldFileNamed:filename in:aDirectory
a27a279701f8 Initial revision
claus
parents:
diff changeset
   234
    "return a FileStream for existing file named filename, aString
a27a279701f8 Initial revision
claus
parents:
diff changeset
   235
     in aDirectory, a FileDirectory.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   236
     The file is opened for read/write access."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   237
a27a279701f8 Initial revision
claus
parents:
diff changeset
   238
    |newStream|
2
claus
parents: 1
diff changeset
   239
    newStream := self new pathName:filename in:aDirectory.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   240
    newStream openForReadWrite isNil ifTrue:[^nil].
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   241
"
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   242
    this is not a good idea; someone else might be appending ...
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   243
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   244
    newStream readLimit:(newStream size).
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   245
"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   246
    ^ newStream
a27a279701f8 Initial revision
claus
parents:
diff changeset
   247
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   248
a27a279701f8 Initial revision
claus
parents:
diff changeset
   249
readonlyFileNamed:filename
a27a279701f8 Initial revision
claus
parents:
diff changeset
   250
    "return a readonly FileStream for existing file named filename, aString"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
a27a279701f8 Initial revision
claus
parents:
diff changeset
   252
    |newStream|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   253
a27a279701f8 Initial revision
claus
parents:
diff changeset
   254
    (OperatingSystem isReadable:filename) ifFalse:[^nil].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   255
2
claus
parents: 1
diff changeset
   256
    newStream := self new pathName:filename.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   257
    newStream openForReading isNil ifTrue:[^nil].
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   258
"
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   259
    this is not a good idea; someone else might be appending ...
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   260
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   261
    newStream readLimit:(newStream size).
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   262
"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   263
    ^ newStream
a27a279701f8 Initial revision
claus
parents:
diff changeset
   264
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   265
a27a279701f8 Initial revision
claus
parents:
diff changeset
   266
readonlyFileNamed:filename in:aDirectory
a27a279701f8 Initial revision
claus
parents:
diff changeset
   267
    "return a readonly FileStream for existing file named filename, aString
a27a279701f8 Initial revision
claus
parents:
diff changeset
   268
     in aDirectory, a FileDirectory"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   269
a27a279701f8 Initial revision
claus
parents:
diff changeset
   270
    |newStream|
2
claus
parents: 1
diff changeset
   271
    newStream := self new pathName:filename in:aDirectory.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   272
    newStream openForReading isNil ifTrue:[^nil].
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   273
"
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   274
    this is not a good idea; someone else might be appending ...
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   275
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   276
    newStream readLimit:(newStream size).
42
e33491f6f260 *** empty log message ***
claus
parents: 31
diff changeset
   277
"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   278
    ^ newStream
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   279
! !
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   280
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   281
!FileStream methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   282
a27a279701f8 Initial revision
claus
parents:
diff changeset
   283
directoryName
a27a279701f8 Initial revision
claus
parents:
diff changeset
   284
    "return the name of the directory I'm in"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   285
a27a279701f8 Initial revision
claus
parents:
diff changeset
   286
    |path lastIndex index|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   287
a27a279701f8 Initial revision
claus
parents:
diff changeset
   288
    path := pathName.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   289
    lastIndex := 0.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   290
    index := path indexOf:$/.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   291
    [index ~~ 0] whileTrue:[
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   292
	lastIndex := index.
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   293
	index := path indexOf:$/ startingAt:(index + 1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   294
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   295
    (lastIndex == 0) ifTrue:[^ '.'].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   296
    (lastIndex == 1) ifTrue:[^ '/'].
31
75f2b9f78be2 *** empty log message ***
claus
parents: 13
diff changeset
   297
    ^ path copyTo:(lastIndex - 1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   298
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   299
a27a279701f8 Initial revision
claus
parents:
diff changeset
   300
name
a27a279701f8 Initial revision
claus
parents:
diff changeset
   301
    "return my name without leading direcory-path"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   302
a27a279701f8 Initial revision
claus
parents:
diff changeset
   303
    |lastIndex index|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   304
a27a279701f8 Initial revision
claus
parents:
diff changeset
   305
    lastIndex := 1.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   306
    [true] whileTrue:[
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   307
	index := pathName indexOf:$/ startingAt:lastIndex.
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   308
	(index == 0) ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   309
	    ^ pathName copyFrom:lastIndex
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   310
	].
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   311
	lastIndex := index + 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   312
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
   313
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   314
a27a279701f8 Initial revision
claus
parents:
diff changeset
   315
pathName
a27a279701f8 Initial revision
claus
parents:
diff changeset
   316
    "return the pathname"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   317
a27a279701f8 Initial revision
claus
parents:
diff changeset
   318
    ^ pathName
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   319
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   320
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   321
store:something
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   322
    "what really should this do"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   323
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   324
    self nextPutAll:something storeString
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   325
! !
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   326
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   327
!FileStream methodsFor:'error handling'!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   328
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   329
openError
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
    "report an error, that file open failed"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   331
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   332
    "/
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   333
    "/ for now, do not raise any signal (see super>>openError).
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   334
    "/ Its not yet handled anywhere. Instead, senders of open
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   335
    "/ check for nil return value (which is a historic leftover)
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   336
    "/
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   337
    LastErrorNumber := lastErrorNumber.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   338
    ^ nil.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
! !
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   340
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   341
!FileStream methodsFor:'printing & storing'!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   342
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   343
printOn:aStream
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   344
    aStream nextPutAll:'(a FileStream for:'.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
    aStream nextPutAll:pathName.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   346
    aStream nextPut:$)
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   347
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   348
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   349
storeOn:aStream
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   350
    aStream nextPutAll:'(FileStream oldFileNamed:'.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   351
    aStream nextPutAll:pathName.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   352
    (self position ~~ 1) ifTrue:[
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   353
	aStream nextPutAll:'; position:'.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   354
	self position storeOn:aStream
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   355
    ].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   356
    aStream nextPut:$)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   357
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   358
a27a279701f8 Initial revision
claus
parents:
diff changeset
   359
!FileStream methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   360
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   361
createForReadWrite
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   362
    "create/truncate the file for read/write.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   363
     If the file existed, its truncated; otherwise its created."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   364
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   365
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   366
    mode := #readwrite.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   367
    ^ self openWithMode:'w+'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   368
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   369
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   370
createForWriting
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   371
    "create/truncate the file for writeonly.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   372
     If the file existed, its truncated; otherwise its created."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   373
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   374
    mode := #writeonly.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   375
    didWrite := true.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   376
    ^ self openWithMode:'w'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   377
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   378
a27a279701f8 Initial revision
claus
parents:
diff changeset
   379
open
a27a279701f8 Initial revision
claus
parents:
diff changeset
   380
    "open the file"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   381
a27a279701f8 Initial revision
claus
parents:
diff changeset
   382
    pathName isNil ifTrue:[^nil].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   383
    (mode == #readonly) ifTrue: [
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   384
	didWrite := false.
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   385
	^ self openWithMode:'r'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   386
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   387
    (mode == #writeonly) ifTrue: [
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   388
	didWrite := true.
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   389
	^ self openWithMode:'w'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   390
    ].
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   391
    ^ self openWithMode:'r+'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   392
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   393
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   394
openForAppending
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   395
    "open the file for writeonly appending to the end.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   396
     If the file does not exist its an error, return nil; 
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   397
     otherwise return the receiver."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   398
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   399
    mode := #writeonly.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   400
    didWrite := true.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   401
    ^ self openWithMode:'a+'
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   402
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   403
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   404
openForReadWrite
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   405
    "open the file for read/write.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   406
     If the file does not exist its an error, return nil; 
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   407
     otherwise return the receiver."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   408
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   409
    mode := #readwrite.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   410
    ^ self openWithMode:'r+'
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   411
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   412
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   413
openForReading
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   414
    "open the file for readonly.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
     If the file does not exist its an error, return nil; 
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   416
     otherwise return the receiver."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   417
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
    mode := #readonly.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   419
    didWrite := false.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   420
    ^ self openWithMode:'r'
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   421
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   422
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   423
openForWriting
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   424
    "open the file writeonly.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   425
     If the file does not exist its an error, return nil; 
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   426
     otherwise return the receiver."
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   427
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   428
    mode := #writeonly.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   429
    didWrite := true.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   430
    ^ self openWithMode:'r+'   "unix-io does not allow this; open for update here"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   431
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   432
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   433
openWithMode:openmode
a27a279701f8 Initial revision
claus
parents:
diff changeset
   434
    "open the file; openmode is the string defining the way to open"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   435
2078
0a5a557b5194 Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   436
    |retVal laspass|
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   437
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   438
    filePointer notNil ifTrue:[^ self errorOpen].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   439
%{
a27a279701f8 Initial revision
claus
parents:
diff changeset
   440
    FILE *f;
2763
cea404472e7c 64bit (alpha) changes
Claus Gittinger <cg@exept.de>
parents: 2161
diff changeset
   441
    FILE *fopen();
477
8710aba7876b oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents: 475
diff changeset
   442
    OBJ path, fp;
2078
0a5a557b5194 Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   443
    int pass = 0;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   444
2078
0a5a557b5194 Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   445
retry:
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   446
    path = __INST(pathName);
328
claus
parents: 255
diff changeset
   447
    if (__isNonNilObject(path) && (__qClass(path)==String)) {
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   448
	__BEGIN_INTERRUPTABLE__
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   449
	do {
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   450
#ifdef LINUX
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   451
	    /* LINUX returns a non-NULL f even when interrupted */
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   452
	    errno = 0;
2763
cea404472e7c 64bit (alpha) changes
Claus Gittinger <cg@exept.de>
parents: 2161
diff changeset
   453
	    f = fopen((char *) __stringVal(path), (char *) __stringVal(openmode));
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   454
	    if (errno == EINTR)
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   455
		f = NULL;
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   456
#else
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   457
2763
cea404472e7c 64bit (alpha) changes
Claus Gittinger <cg@exept.de>
parents: 2161
diff changeset
   458
	    f = fopen((char *) __stringVal(path), (char *) __stringVal(openmode));
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   459
#endif
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   460
	} while ((f == NULL) && (errno == EINTR));
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   461
	__END_INTERRUPTABLE__
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   462
	if (f == NULL) {
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   463
	    /*
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   464
	     * If no filedescriptors available, try to finalize
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   465
	     * possibly collected fd's and try again.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   466
	     */
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   467
	    if (pass == 0 && (errno == ENFILE || errno == EMFILE)) {
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   468
		pass = 1;
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   469
		__SSEND0(@global(ObjectMemory), @symbol(scavenge), 0);
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   470
		__SSEND0(@global(ObjectMemory), @symbol(finalize), 0);
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   471
		goto retry;
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   472
	    }
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   473
	    __INST(lastErrorNumber) = __MKSMALLINT(errno);
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   474
	    __INST(position) = nil;
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   475
	} else {
2766
755c65a368eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
   476
	    __INST(filePointer) = fp = __MKOBJ((INT)f); __STORE(self, fp);
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   477
	    __INST(position) = __MKSMALLINT(1);
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   478
	    retVal = self;
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   479
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   480
    }
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   481
%}.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   482
    retVal notNil ifTrue:[
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   483
	buffered := true.       "default is buffered"
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   484
	Lobby register:self.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   485
	^ retVal
2078
0a5a557b5194 Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   486
    ] ifFalse:[
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   487
	"
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   488
	 the open failed for some reason ...
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   489
	"
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   490
	^ self openError
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   491
    ].
2078
0a5a557b5194 Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   492
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   493
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   494
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   495
pathName:filename
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   496
    "set the pathname"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   497
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   498
    pathName := filename
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   499
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   500
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   501
pathName:filename in:aDirectory
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   502
    "set the pathname starting at aDirectory, a FileDirectory"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   503
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   504
    ((filename at:1) == $/) ifTrue:[
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   505
	"filename may not start with a '/'"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   506
	pathName := nil
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   507
    ] ifFalse:[
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   508
	pathName := aDirectory pathName.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   509
	(pathName endsWith:'/') ifFalse:[
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   510
	    pathName := pathName , '/'
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   511
	].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   512
	pathName := pathName , filename
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   513
    ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   514
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   515
a27a279701f8 Initial revision
claus
parents:
diff changeset
   516
reOpen
a27a279701f8 Initial revision
claus
parents:
diff changeset
   517
    "sent after snapin to reopen streams"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   518
173
58e9778954bc reposition
claus
parents: 159
diff changeset
   519
    |oldPos|
58e9778954bc reposition
claus
parents: 159
diff changeset
   520
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   521
    filePointer notNil ifTrue:[
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   522
	"it was open, when snapped-out"
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   523
	filePointer := nil.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   524
	Lobby unregister:self.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   525
	oldPos := position.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   526
	self open.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   527
	filePointer isNil ifTrue:[
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   528
	    "this happens, if after a restart, the file is no longer accessable ..."
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   529
2161
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   530
	    (self class name , ' [warning]: could not reOpen file: ', pathName) errorPrintCR.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   531
	] ifFalse:[
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   532
	    self position:oldPos.
0472a226a714 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2134
diff changeset
   533
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   534
    ]
1088
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
   535
2134
246a3bdab8b4 newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents: 2078
diff changeset
   536
    "Modified: 10.1.1997 / 17:50:51 / cg"
31
75f2b9f78be2 *** empty log message ***
claus
parents: 13
diff changeset
   537
! !
75f2b9f78be2 *** empty log message ***
claus
parents: 13
diff changeset
   538
75f2b9f78be2 *** empty log message ***
claus
parents: 13
diff changeset
   539
!FileStream methodsFor:'queries'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   540
a27a279701f8 Initial revision
claus
parents:
diff changeset
   541
position
a27a279701f8 Initial revision
claus
parents:
diff changeset
   542
    "return the read/write position in the file -
31
75f2b9f78be2 *** empty log message ***
claus
parents: 13
diff changeset
   543
     notice, in smalltalk indices start at 1 so begin of file is 1"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   544
a27a279701f8 Initial revision
claus
parents:
diff changeset
   545
%{  /* NOCONTEXT */
a27a279701f8 Initial revision
claus
parents:
diff changeset
   546
a27a279701f8 Initial revision
claus
parents:
diff changeset
   547
    FILE *f;
a27a279701f8 Initial revision
claus
parents:
diff changeset
   548
    long currentPosition;
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   549
    extern long ftell(), lseek();
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   550
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   551
    if (__INST(filePointer) != nil) {
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   552
	f = __FILEVal(__INST(filePointer));
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   553
	do {
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   554
	    if (__INST(buffered) == true) {
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   555
		currentPosition = ftell(f);
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   556
	    } else {
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   557
		currentPosition = lseek(fileno(f), 0L, SEEK_CUR);
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   558
	    }
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   559
	} while ((currentPosition < 0) && (errno == EINTR));
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   560
	if (currentPosition >= 0) {
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   561
	    /*
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   562
	     * notice: Smalltalk index starts at 1
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   563
	     */
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   564
	    RETURN ( __MKSMALLINT(currentPosition + 1) );
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   565
	}
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   566
	__INST(lastErrorNumber) = __MKSMALLINT(errno);
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   567
    }
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   568
%}.
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   569
    lastErrorNumber notNil ifTrue:[^ self ioError].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   570
    filePointer isNil ifTrue:[^ self errorNotOpen].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   571
    ^ self primitiveFailed
a27a279701f8 Initial revision
claus
parents:
diff changeset
   572
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   573
a27a279701f8 Initial revision
claus
parents:
diff changeset
   574
position:newPos
a27a279701f8 Initial revision
claus
parents:
diff changeset
   575
    "set the read/write position in the file"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   576
a27a279701f8 Initial revision
claus
parents:
diff changeset
   577
%{  /* NOCONTEXT */
a27a279701f8 Initial revision
claus
parents:
diff changeset
   578
a27a279701f8 Initial revision
claus
parents:
diff changeset
   579
    FILE *f;
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   580
    long ret;
249
claus
parents: 223
diff changeset
   581
    OBJ fp;
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   582
    long nP;
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   583
    extern long lseek();
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   584
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   585
    if ((fp = __INST(filePointer)) != nil) {
249
claus
parents: 223
diff changeset
   586
	if (__isSmallInteger(newPos)) {
475
b57530aa1b0a use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents: 437
diff changeset
   587
	    f = __FILEVal(fp);
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   588
	    nP = (long)__intVal(newPos);
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   589
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   590
	    /*
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   591
	     * notice: Smalltalk index starts at 1
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   592
	     */
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   593
	    nP--;
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   594
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   595
	    do {
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   596
		if (__INST(buffered) == true) {
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   597
		    ret = fseek(f, nP, SEEK_SET);
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   598
		} else {
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   599
		    ret = lseek(fileno(f), nP, SEEK_SET);
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   600
		}
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   601
	    } while ((ret < 0) && (errno == EINTR));
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   602
	    if (ret >= 0) {
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   603
		__INST(position) = newPos;
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   604
		/*
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   605
		 * just to make certain ...
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   606
		 */
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   607
		__INST(hitEOF) = false;
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   608
		RETURN ( self );
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   609
	    }
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   610
	    __INST(lastErrorNumber) = __MKSMALLINT(errno);
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   611
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   612
    }
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   613
%}.
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   614
    lastErrorNumber notNil ifTrue:[^ self ioError].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   615
    filePointer isNil ifTrue:[^ self errorNotOpen].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   616
    ^ self primitiveFailed
a27a279701f8 Initial revision
claus
parents:
diff changeset
   617
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   618
a27a279701f8 Initial revision
claus
parents:
diff changeset
   619
setToEnd
a27a279701f8 Initial revision
claus
parents:
diff changeset
   620
    "set the read/write position in the file to be at the end of the file"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   621
a27a279701f8 Initial revision
claus
parents:
diff changeset
   622
%{
a27a279701f8 Initial revision
claus
parents:
diff changeset
   623
    FILE *f;
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   624
    long ret;
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   625
    extern long lseek();
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   626
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   627
    if (__INST(filePointer) != nil) {
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   628
	f = __FILEVal(__INST(filePointer));
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   629
	__INST(position) = nil;
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   630
	do {
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   631
	    if (__INST(buffered) == true) {
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   632
		ret = fseek(f, 0L, SEEK_END);
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   633
	    } else {
2796
b45639a2fc1e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2766
diff changeset
   634
		ret = lseek(fileno(f), 0L, SEEK_END);
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   635
	    }
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   636
	} while ((ret < 0) && (errno == EINTR));
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   637
	if (ret >= 0) {
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   638
	    RETURN ( self );
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   639
	}
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   640
	__INST(lastErrorNumber) = __MKSMALLINT(errno);
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   641
    }
159
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   642
%}.
514c749165c3 *** empty log message ***
claus
parents: 92
diff changeset
   643
    lastErrorNumber notNil ifTrue:[^ self ioError].
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 42
diff changeset
   644
    filePointer isNil ifTrue:[^ self errorNotOpen].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   645
    ^ self primitiveFailed
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   646
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   647
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   648
size
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   649
    "return the size in bytes of the file"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   650
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   651
%{  /* NOCONTEXT */
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   652
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   653
#ifdef transputer
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   654
    FILE *f;
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   655
    int size;
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   656
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   657
    if (__INST(filePointer) != nil) {
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   658
	f = __FILEVal(__INST(filePointer));
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   659
	if ((size = filesize(fileno(f))) >= 0) {
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   660
	    RETURN ( __MKSMALLINT(size) );
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   661
	}
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   662
    }
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   663
#else
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   664
    FILE *f;
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   665
    struct stat buf;
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   666
    int ret;
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   667
    int fd;
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   668
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   669
    if (__INST(filePointer) != nil) {
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   670
	f = __FILEVal(__INST(filePointer));
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   671
	fd = fileno(f);
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   672
	do {
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   673
	    ret = fstat(fd, &buf);
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   674
	} while ((ret < 0) && (errno == EINTR));
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   675
	if (ret >= 0) {
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   676
	    RETURN ( __MKSMALLINT(buf.st_size) );
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   677
	}
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1088
diff changeset
   678
	__INST(lastErrorNumber) = __MKSMALLINT(errno);
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   679
    }
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   680
#endif
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   681
%}.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   682
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   683
    "could add a fall-back here:
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   684
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   685
	oldPosition := self position.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   686
	self setToEnd.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   687
	sz := self position.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   688
	self position:oldPosition.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   689
	^ sz
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   690
    "
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   691
    lastErrorNumber notNil ifTrue:[^ self ioError].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   692
    filePointer isNil ifTrue:[^ self errorNotOpen].
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   693
    ^ self primitiveFailed
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   694
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   695
2
claus
parents: 1
diff changeset
   696
!FileStream methodsFor:'testing'!
claus
parents: 1
diff changeset
   697
claus
parents: 1
diff changeset
   698
isFileStream
claus
parents: 1
diff changeset
   699
    "return true, if the receiver is some kind of fileStream.
claus
parents: 1
diff changeset
   700
     redefined from Object"
claus
parents: 1
diff changeset
   701
claus
parents: 1
diff changeset
   702
    ^ true
claus
parents: 1
diff changeset
   703
! !
claus
parents: 1
diff changeset
   704
1088
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
   705
!FileStream class methodsFor:'documentation'!
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
   706
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
   707
version
2808
3a9ce7aadad8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2796
diff changeset
   708
    ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.40 1997-07-30 11:13:17 cg Exp $'
1088
989f0e510a32 nicer message
Claus Gittinger <cg@exept.de>
parents: 857
diff changeset
   709
! !