author | Claus Gittinger <cg@exept.de> |
Wed, 22 Sep 1999 17:39:21 +0200 | |
changeset 4789 | 72a61dc595ad |
parent 4369 | eaac1c04a81d |
permissions | -rw-r--r-- |
3746 | 1 |
" |
2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
ExternalStream subclass:#FileStream |
|
3755 | 14 |
instanceVariableNames:'pathName canPosition removeOnClose' |
3746 | 15 |
classVariableNames:'OpenErrorSignal' |
16 |
poolDictionaries:'' |
|
17 |
category:'Streams-External' |
|
18 |
! |
|
19 |
||
20 |
!FileStream primitiveDefinitions! |
|
21 |
%{ |
|
22 |
||
23 |
#include <stdio.h> |
|
24 |
#define _STDIO_H_INCLUDED_ |
|
25 |
||
26 |
#include <errno.h> |
|
27 |
#define _ERRNO_H_INCLUDED_ |
|
28 |
||
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
29 |
#include <sys/types.h> |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
30 |
#include <sys/stat.h> |
3746 | 31 |
|
32 |
#ifndef SEEK_SET |
|
33 |
# define SEEK_SET 0 |
|
34 |
#endif |
|
35 |
#ifndef SEEK_CUR |
|
36 |
# define SEEK_CUR 1 |
|
37 |
#endif |
|
38 |
#ifndef SEEK_END |
|
39 |
# define SEEK_END 2 |
|
40 |
#endif |
|
41 |
||
42 |
/* |
|
43 |
* not all systems have off_t |
|
44 |
* explicit add of those we know to have ... |
|
45 |
*/ |
|
46 |
||
47 |
#ifndef OFF_T |
|
48 |
# define OFF_T long |
|
49 |
#endif |
|
50 |
||
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
51 |
#if 1 |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
52 |
# define NO_STDIO |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
53 |
#else |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
54 |
# undef __BEGIN_INTERRUPTABLE__ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
55 |
# undef __END_INTERRUPTABLE__ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
56 |
# define __BEGIN_INTERRUPTABLE__ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
57 |
# define __END_INTERRUPTABLE__ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
58 |
#endif |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
59 |
|
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
60 |
#ifdef i386 |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
61 |
# define _X86_ |
3746 | 62 |
#endif |
63 |
||
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
64 |
#undef INT |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
65 |
#undef Array |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
66 |
#undef Number |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
67 |
#undef Method |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
68 |
#undef Point |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
69 |
#undef Rectangle |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
70 |
#undef Block |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
71 |
#undef Time |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
72 |
#undef Date |
3746 | 73 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
74 |
#define NOATOM |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
75 |
#define NOGDICAPMASKS |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
76 |
#define NOMETAFILE |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
77 |
#define NOMINMAX |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
78 |
//# define NOOPENFILE |
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
79 |
#define NOSOUND |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
80 |
#define NOWH |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
81 |
//# define NOCOMM |
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
82 |
#define NOKANJI |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
83 |
#define NOCRYPT |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
84 |
//# define NOMCX |
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
85 |
#define WIN32_LEAN_AND_MEAN |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
86 |
#include <windows.h> |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
87 |
#include <winsock.h> /* */ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
88 |
#if !defined(__BORLANDC__) |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
89 |
# define stat _stat |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
90 |
#endif |
3746 | 91 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
92 |
#ifdef __DEF_Array |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
93 |
# define Array __DEF_Array |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
94 |
#endif |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
95 |
#ifdef __DEF_Number |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
96 |
# define Number __DEF_Number |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
97 |
#endif |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
98 |
#ifdef __DEF_Method |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
99 |
# define Method __DEF_Method |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
100 |
#endif |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
101 |
#ifdef __DEF_Point |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
102 |
# define Point __DEF_Point |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
103 |
#endif |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
104 |
#ifdef __DEF_Block |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
105 |
# define Block __DEF_Block |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
106 |
#endif |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
107 |
#ifdef __DEF_Time |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
108 |
# define Time __DEF_Time |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
109 |
#endif |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
110 |
#ifdef __DEF_Date |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
111 |
# define Date __DEF_Date |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
112 |
#endif |
3746 | 113 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
114 |
#define INT int |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
115 |
|
3746 | 116 |
#ifdef NO_STDIO |
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
117 |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
118 |
/*# define HFILE HANDLE*/ |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
119 |
# define HFILE FILE * |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
120 |
//# define fileno(f) f |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
121 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
122 |
# define TELL(currentPosition,f,buffered) \ |
3746 | 123 |
if (buffered) { \ |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
124 |
currentPosition = ftell(f); \ |
3746 | 125 |
} else { \ |
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
126 |
OBJ rA = __INST(readAhead); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
127 |
if (rA != nil) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
128 |
__INST(readAhead) = nil; \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
129 |
currentPosition = lseek(fileno(f), -1L, SEEK_CUR); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
130 |
} else { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
131 |
currentPosition = lseek(fileno(f), 0L, SEEK_CUR); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
132 |
} \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
133 |
} |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
134 |
|
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
135 |
# define SEEK(ret,f,nP,buffered) \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
136 |
if (buffered) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
137 |
ret = fseek(f, nP, SEEK_SET); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
138 |
} else { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
139 |
OBJ rA = __INST(readAhead); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
140 |
if (rA != nil) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
141 |
__INST(readAhead) = nil; \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
142 |
} \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
143 |
ret = lseek(fileno(f), nP, SEEK_SET); \ |
3746 | 144 |
} |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
145 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
146 |
# define TOEND(ret,f,buffered) \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
147 |
if (buffered) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
148 |
ret = fseek(f, 0L, SEEK_END); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
149 |
} else { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
150 |
OBJ rA = __INST(readAhead); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
151 |
if (rA != nil) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
152 |
__INST(readAhead) = nil; \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
153 |
} \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
154 |
ret = lseek(fileno(f), 0L, SEEK_END); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
155 |
} |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
156 |
|
3746 | 157 |
#else |
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
158 |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
159 |
# define HFILE FILE * |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
160 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
161 |
# define TELL(currentPosition,f,buffered) \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
162 |
if (buffered) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
163 |
currentPosition = ftell(f); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
164 |
} else { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
165 |
currentPosition = lseek(fileno(f), 0L, SEEK_CUR); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
166 |
} |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
167 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
168 |
# define SEEK(ret,f,nP,buffered) \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
169 |
if (buffered) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
170 |
ret = fseek(f, nP, SEEK_SET); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
171 |
} else { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
172 |
ret = lseek(fileno(f), nP, SEEK_SET); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
173 |
} |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
174 |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
175 |
# define TOEND(ret,f,buffered) \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
176 |
if (buffered) { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
177 |
ret = fseek(f, 0L, SEEK_END); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
178 |
} else { \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
179 |
ret = lseek(fileno(f), 0L, SEEK_END); \ |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
180 |
} |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
181 |
|
3746 | 182 |
extern long ftell(), lseek(); |
183 |
#endif |
|
184 |
||
185 |
/* |
|
186 |
* on some systems errno is a macro ... check for it here |
|
187 |
*/ |
|
188 |
#ifndef errno |
|
189 |
extern errno; |
|
190 |
#endif |
|
191 |
||
192 |
%} |
|
193 |
! ! |
|
194 |
||
195 |
!FileStream class methodsFor:'documentation'! |
|
196 |
||
197 |
copyright |
|
198 |
" |
|
199 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
200 |
All Rights Reserved |
|
201 |
||
202 |
This software is furnished under a license and may be used |
|
203 |
only in accordance with the terms of that license and with the |
|
204 |
inclusion of the above copyright notice. This software may not |
|
205 |
be provided or otherwise made available to, or used by, any |
|
206 |
other person. No title to or ownership of the software is |
|
207 |
hereby transferred. |
|
208 |
" |
|
209 |
! |
|
210 |
||
211 |
documentation |
|
212 |
" |
|
213 |
This class provides access to the operating systems underlying file |
|
214 |
system (i.e. its an interface to the stdio library). |
|
215 |
||
216 |
Notice, that on some systems, the standard I/O library has performance |
|
217 |
problems when a file is opened for readwrite. |
|
218 |
For best results, open files either readonly or writeonly. |
|
219 |
||
220 |
Also notice, that some OperatingSystems do not fully support |
|
221 |
positioning a file stream. |
|
222 |
For example, poor VMS does not allow positioning onto arbitrary |
|
223 |
byte boundaries if the file is a variable-record-RMS file. |
|
224 |
(stupid enough, this is the default for textfiles as created by |
|
225 |
some tools ...) |
|
226 |
Therefore, the instance variable canPosition is set according to |
|
227 |
this and an error is raised, if a position: is attemted. |
|
228 |
I know, this is ugly, but what else could we do ? |
|
229 |
||
230 |
[instance variables:] |
|
231 |
pathName <String> the files path (if known) |
|
232 |
canPosition <Boolean> positionable - read above comment |
|
233 |
||
234 |
[author:] |
|
4361
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
235 |
Claus Gittinger (original Unix version) |
6ce4ff1e7f3c
care for fopen returning a FILE* of -1
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
236 |
Manfred Dierolf (dos port) |
3746 | 237 |
|
238 |
[see also:] |
|
239 |
Filename DirectoryStream PipeStream Socket |
|
240 |
" |
|
241 |
! ! |
|
242 |
||
243 |
!FileStream class methodsFor:'initialization'! |
|
244 |
||
245 |
initialize |
|
246 |
OpenErrorSignal isNil ifTrue:[ |
|
247 |
"/ |
|
248 |
"/ this is temporary - for now allow an openError to |
|
249 |
"/ be unhandled and proceed by returning a nil from the |
|
250 |
"/ stream creation method. |
|
251 |
"/ In the future, this will be a hard signal. |
|
252 |
"/ |
|
253 |
OpenErrorSignal := QuerySignal new. |
|
254 |
OpenErrorSignal parent:(super openErrorSignal) mayProceed:true. |
|
255 |
OpenErrorSignal nameClass:self message:#openErrorSignal. |
|
256 |
OpenErrorSignal notifierString:'open error'. |
|
257 |
] |
|
258 |
||
259 |
"Modified: 8.10.1997 / 11:56:39 / cg" |
|
260 |
! ! |
|
261 |
||
262 |
!FileStream class methodsFor:'instance creation'! |
|
263 |
||
264 |
appendingOldFileNamed:filename |
|
265 |
"return a FileStream for existing file named filename, aString. |
|
266 |
The file is opened for writeonly access." |
|
267 |
||
268 |
|newStream| |
|
269 |
newStream := self new pathName:filename. |
|
270 |
newStream openForAppending isNil ifTrue:[^nil]. |
|
271 |
" |
|
272 |
this is not a good idea; I might like to read the written stuff ... |
|
273 |
||
274 |
newStream readLimit:(newStream size). |
|
275 |
" |
|
276 |
^ newStream |
|
277 |
! |
|
278 |
||
279 |
appendingOldFileNamed:filename in:aDirectory |
|
280 |
"return a FileStream for existing file named filename, aString |
|
281 |
in aDirectory, a FileDirectory. |
|
282 |
The file is opened for writeonly access." |
|
283 |
||
284 |
|newStream| |
|
285 |
newStream := self new pathName:filename in:aDirectory. |
|
286 |
newStream openForAppending isNil ifTrue:[^nil]. |
|
287 |
" |
|
288 |
this is not a good idea; I might like to read the written stuff ... |
|
289 |
||
290 |
newStream readLimit:(newStream size). |
|
291 |
" |
|
292 |
^ newStream |
|
293 |
! |
|
294 |
||
295 |
fileNamed:filename |
|
296 |
"return a stream on file filename - if the file does not |
|
297 |
already exist, create it. |
|
298 |
The file is opened for read/write access." |
|
299 |
||
300 |
|stream| |
|
301 |
||
302 |
stream := self oldFileNamed:filename. |
|
303 |
stream isNil ifTrue:[ |
|
304 |
stream := self newFileNamed:filename |
|
305 |
]. |
|
306 |
^ stream |
|
307 |
! |
|
308 |
||
309 |
fileNamed:filename in:aDirectory |
|
310 |
"return a stream on file filename - if the file does not |
|
311 |
already exist, create it. |
|
312 |
The file is opened for read/write access." |
|
313 |
||
314 |
|stream| |
|
315 |
||
316 |
stream := self oldFileNamed:filename in:aDirectory. |
|
317 |
stream isNil ifTrue:[ |
|
318 |
stream := self newFileNamed:filename in:aDirectory |
|
319 |
]. |
|
320 |
^ stream |
|
321 |
! |
|
322 |
||
323 |
newFileForWritingNamed:filename |
|
324 |
"return a FileStream for new file named filename, aString. |
|
325 |
If the file exists, it is truncated, otherwise created. |
|
326 |
The file is opened for writeonly access." |
|
327 |
||
328 |
|newStream| |
|
329 |
newStream := self new pathName:filename. |
|
330 |
newStream createForWriting isNil ifTrue:[^nil]. |
|
331 |
^ newStream |
|
332 |
! |
|
333 |
||
334 |
newFileForWritingNamed:filename in:aDirectory |
|
335 |
"return a FileStream for new file named filename, aString |
|
336 |
in aDirectory, a FileDirectory. |
|
337 |
If the file exists, it is truncated, otherwise created. |
|
338 |
The file is opened for writeonly access." |
|
339 |
||
340 |
|newStream| |
|
341 |
newStream := self new pathName:filename in:aDirectory. |
|
342 |
newStream createForWriting isNil ifTrue:[^nil]. |
|
343 |
^ newStream |
|
344 |
! |
|
345 |
||
346 |
newFileNamed:filename |
|
347 |
"return a FileStream for new file named filename, aString. |
|
348 |
If the file exists, it is truncated, otherwise created. |
|
349 |
The file is opened for read/write access." |
|
350 |
||
351 |
|newStream| |
|
352 |
newStream := self new pathName:filename. |
|
353 |
newStream createForReadWrite isNil ifTrue:[^nil]. |
|
354 |
^ newStream |
|
355 |
||
356 |
"Modified: / 28.10.1997 / 14:28:08 / cg" |
|
357 |
! |
|
358 |
||
359 |
newFileNamed:filename in:aDirectory |
|
360 |
"return a FileStream for new file named filename, aString |
|
361 |
in aDirectory, a FileDirectory. |
|
362 |
If the file exists, it is truncated, otherwise created. |
|
363 |
The file is opened for read/write access." |
|
364 |
||
365 |
|newStream| |
|
366 |
newStream := self new pathName:filename in:aDirectory. |
|
367 |
newStream createForReadWrite isNil ifTrue:[^nil]. |
|
368 |
^ newStream |
|
369 |
! |
|
370 |
||
371 |
oldFileNamed:filename |
|
372 |
"return a FileStream for existing file named filename, aString. |
|
373 |
The file is opened for read/write access." |
|
374 |
||
375 |
|newStream| |
|
376 |
||
377 |
(OperatingSystem isReadable:filename) ifFalse:[^nil]. |
|
378 |
newStream := self new pathName:filename. |
|
379 |
newStream readwrite. |
|
380 |
newStream openForReadWrite isNil ifTrue:[^nil]. |
|
381 |
" |
|
382 |
this is not a good idea; someone else might be appending ... |
|
383 |
||
384 |
newStream readLimit:(newStream size). |
|
385 |
" |
|
386 |
^ newStream |
|
387 |
! |
|
388 |
||
389 |
oldFileNamed:filename in:aDirectory |
|
390 |
"return a FileStream for existing file named filename, aString |
|
391 |
in aDirectory, a FileDirectory. |
|
392 |
The file is opened for read/write access." |
|
393 |
||
394 |
|newStream| |
|
395 |
newStream := self new pathName:filename in:aDirectory. |
|
396 |
newStream openForReadWrite isNil ifTrue:[^nil]. |
|
397 |
" |
|
398 |
this is not a good idea; someone else might be appending ... |
|
399 |
||
400 |
newStream readLimit:(newStream size). |
|
401 |
" |
|
402 |
^ newStream |
|
403 |
! |
|
404 |
||
405 |
readonlyFileNamed:filename |
|
406 |
"return a readonly FileStream for existing file named filename, aString" |
|
407 |
||
408 |
|newStream| |
|
409 |
||
410 |
(OperatingSystem isReadable:filename) ifFalse:[^nil]. |
|
411 |
||
412 |
newStream := self new pathName:filename. |
|
413 |
newStream openForReading isNil ifTrue:[^nil]. |
|
414 |
" |
|
415 |
this is not a good idea; someone else might be appending ... |
|
416 |
||
417 |
newStream readLimit:(newStream size). |
|
418 |
" |
|
419 |
^ newStream |
|
420 |
! |
|
421 |
||
422 |
readonlyFileNamed:filename in:aDirectory |
|
423 |
"return a readonly FileStream for existing file named filename, aString |
|
424 |
in aDirectory, a FileDirectory" |
|
425 |
||
426 |
|newStream| |
|
427 |
newStream := self new pathName:filename in:aDirectory. |
|
428 |
newStream openForReading isNil ifTrue:[^nil]. |
|
429 |
" |
|
430 |
this is not a good idea; someone else might be appending ... |
|
431 |
||
432 |
newStream readLimit:(newStream size). |
|
433 |
" |
|
434 |
^ newStream |
|
435 |
! ! |
|
436 |
||
437 |
!FileStream class methodsFor:'Signal constants'! |
|
438 |
||
439 |
openErrorSignal |
|
440 |
"return the FileStream specific openErrorSignal. |
|
441 |
This is different from ExternalStream>>OpenErrorSignal!!" |
|
442 |
||
443 |
^ OpenErrorSignal |
|
444 |
||
445 |
"Created: / 28.1.1998 / 14:41:18 / stefan" |
|
446 |
! ! |
|
447 |
||
448 |
!FileStream methodsFor:'Squeak compatibility'! |
|
449 |
||
450 |
fullName |
|
451 |
"Squeak compatibility: return the full pathname" |
|
452 |
||
453 |
^ pathName asFilename pathName |
|
454 |
||
455 |
"Created: 17.10.1997 / 17:04:12 / cg" |
|
456 |
"Modified: 20.10.1997 / 19:22:44 / cg" |
|
457 |
! ! |
|
458 |
||
459 |
!FileStream methodsFor:'accessing'! |
|
460 |
||
461 |
directoryName |
|
462 |
"return the name of the directory I'm in as a string" |
|
463 |
||
464 |
^ pathName asFilename directoryName |
|
465 |
! |
|
466 |
||
467 |
name |
|
468 |
"return my name without leading direcory-path (i.e. the plain fileName) |
|
469 |
as a string" |
|
470 |
||
471 |
^ pathName asFilename baseName |
|
472 |
! |
|
473 |
||
474 |
pathName |
|
475 |
"return the pathname" |
|
476 |
||
477 |
^ pathName |
|
478 |
! |
|
479 |
||
3755 | 480 |
removeOnClose:aBoolean |
481 |
"set/clear the removeOnClose flag. |
|
482 |
If set, the file will be removed when closed. |
|
483 |
Provided mostly for OS's which do not allow an |
|
484 |
open file to be removed (i.e. non unixes), |
|
485 |
and a fileStream for a tempFile is used. |
|
486 |
Especially, the CVS-SourceCodeManager returns |
|
487 |
this kind of file-handles occasionally. |
|
488 |
This is an ST/X special feature which is not portable |
|
489 |
to other systems." |
|
490 |
||
491 |
removeOnClose := aBoolean |
|
492 |
||
493 |
"Modified: / 13.8.1998 / 12:10:07 / cg" |
|
494 |
! |
|
495 |
||
3746 | 496 |
store:something |
497 |
"what really should this do" |
|
498 |
||
499 |
self nextPutAll:something storeString |
|
500 |
! ! |
|
501 |
||
502 |
!FileStream methodsFor:'error handling'! |
|
503 |
||
504 |
openError |
|
505 |
"{ Pragma: +optSpace }" |
|
506 |
||
507 |
"report an error, that file open failed" |
|
508 |
||
509 |
"/ |
|
510 |
"/ for now, error on open only raises a querySignal. |
|
511 |
"/ If its not handled, a failing open will return nil. |
|
512 |
"/ Currently, all users of fileStream check for this |
|
513 |
"/ nil return value (which is a historic leftover) |
|
514 |
"/ |
|
515 |
"/ This will change in one of the next ST/X releases - for |
|
516 |
"/ now, it is best to provide an exceptionHandler AND check the |
|
517 |
"/ return value. |
|
518 |
"/ Sorry about that. |
|
519 |
"/ |
|
520 |
LastErrorNumber := lastErrorNumber. |
|
521 |
super openError. |
|
522 |
^ nil |
|
523 |
! ! |
|
524 |
||
3755 | 525 |
!FileStream methodsFor:'instance release'! |
526 |
||
527 |
closeFile |
|
528 |
"low level close - may be redefined in subclasses" |
|
529 |
||
530 |
super closeFile. |
|
531 |
removeOnClose == true ifTrue:[ |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
532 |
pathName asFilename remove. |
3755 | 533 |
] |
534 |
||
535 |
"Created: / 13.8.1998 / 12:11:22 / cg" |
|
536 |
! ! |
|
537 |
||
3746 | 538 |
!FileStream methodsFor:'positioning'! |
539 |
||
540 |
position |
|
541 |
"return the read/write position in the file - |
|
542 |
notice, in smalltalk indices start at 1 so begin of file is 1" |
|
543 |
||
544 |
%{ /* NOCONTEXT */ |
|
545 |
||
546 |
HFILE f; |
|
547 |
long currentPosition; |
|
548 |
||
549 |
if (__INST(filePointer) != nil) { |
|
550 |
f = __FILEVal(__INST(filePointer)); |
|
551 |
do { |
|
552 |
TELL(currentPosition,f,__INST(buffered) == true); |
|
553 |
} while ((currentPosition < 0) && (errno == EINTR)); |
|
554 |
if (currentPosition >= 0) { |
|
555 |
/* |
|
556 |
* notice: Smalltalk index starts at 1 |
|
557 |
*/ |
|
558 |
RETURN ( __MKSMALLINT(currentPosition + 1) ); |
|
559 |
} |
|
560 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
561 |
} |
|
562 |
%}. |
|
563 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
564 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
565 |
^ self primitiveFailed |
|
566 |
! |
|
567 |
||
568 |
position:newPos |
|
569 |
"set the read/write position in the file" |
|
570 |
||
571 |
%{ /* NOCONTEXT */ |
|
572 |
||
573 |
HFILE f; |
|
574 |
long ret; |
|
575 |
OBJ fp; |
|
576 |
long nP; |
|
577 |
||
578 |
if ((__INST(canPosition) != false) |
|
579 |
|| (newPos == __MKSMALLINT(1))) { |
|
580 |
if ((fp = __INST(filePointer)) != nil) { |
|
581 |
if (__isSmallInteger(newPos)) { |
|
582 |
f = __FILEVal(fp); |
|
583 |
nP = (long)__intVal(newPos); |
|
584 |
||
585 |
/* |
|
586 |
* notice: Smalltalk index starts at 1 |
|
587 |
*/ |
|
588 |
nP--; |
|
589 |
||
590 |
do { |
|
591 |
SEEK(ret,f,nP,__INST(buffered) == true); |
|
592 |
} while ((ret < 0) && (errno == EINTR)); |
|
593 |
if (ret >= 0) { |
|
594 |
__INST(position) = newPos; |
|
595 |
/* |
|
596 |
* just to make certain ... |
|
597 |
*/ |
|
598 |
__INST(hitEOF) = false; |
|
599 |
RETURN ( self ); |
|
600 |
} |
|
601 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
602 |
} |
|
603 |
} |
|
604 |
} |
|
605 |
%}. |
|
606 |
canPosition == false ifTrue:[ |
|
607 |
"/ position by rewinding & re-reading everything up-to |
|
608 |
"/ that point. |
|
609 |
^ self slowPosition:newPos |
|
610 |
]. |
|
611 |
lastErrorNumber notNil ifTrue:[ |
|
612 |
(OperatingSystem errorSymbolForNumber:lastErrorNumber) == #EINVAL ifTrue:[ |
|
613 |
"/ invalid position |
|
614 |
^ self positionError |
|
615 |
]. |
|
616 |
"/ assume I/O error |
|
617 |
^ self ioError |
|
618 |
]. |
|
619 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
620 |
^ self primitiveFailed |
|
621 |
! |
|
622 |
||
623 |
setToEnd |
|
624 |
"set the read/write position in the file to be at the end of the file" |
|
625 |
||
626 |
%{ |
|
627 |
HFILE f; |
|
628 |
long ret; |
|
629 |
||
630 |
if (__INST(filePointer) != nil) { |
|
631 |
f = __FILEVal(__INST(filePointer)); |
|
632 |
__INST(position) = nil; |
|
633 |
do { |
|
634 |
TOEND(ret,f,__INST(buffered) == true); |
|
635 |
} while ((ret < 0) && (errno == EINTR)); |
|
636 |
if (ret >= 0) { |
|
637 |
RETURN ( self ); |
|
638 |
} |
|
639 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
640 |
} |
|
641 |
%}. |
|
642 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
643 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
644 |
^ self primitiveFailed |
|
645 |
! |
|
646 |
||
647 |
slowPosition:newPos |
|
648 |
"position the file by re-reading everything up-to newPos. |
|
649 |
The effect is the same as that of #position:, but its much slower. |
|
650 |
This is required to reposition nonPositionable streams, such |
|
651 |
as tape-streams or variable-record-RMS files under VMS. |
|
652 |
Caveat: |
|
653 |
This should really be done transparently by the stdio library." |
|
654 |
||
655 |
|buffer amount| |
|
656 |
||
657 |
self isReadable ifFalse:[ |
|
658 |
"/ sorry |
|
659 |
^ self positionError |
|
660 |
]. |
|
661 |
||
662 |
buffer := ByteArray new:8*1024. |
|
663 |
||
664 |
(position isNil "/ i.e. unknown |
|
665 |
or:[newPos < position]) ifTrue:[ |
|
666 |
self reset. |
|
667 |
]. |
|
668 |
[position < newPos] whileTrue:[ |
|
669 |
amount := (buffer size) min:(newPos-position). |
|
670 |
(self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[ |
|
671 |
^ self positionError |
|
672 |
]. |
|
673 |
]. |
|
674 |
"/ ('FileStream [info]: slow position - please convert ''' , pathName printString , ''' to streamLF format') infoPrintCR. |
|
675 |
! ! |
|
676 |
||
677 |
!FileStream methodsFor:'printing & storing'! |
|
678 |
||
679 |
printOn:aStream |
|
680 |
"append a human readable representation of the receiver to aStream" |
|
681 |
||
682 |
aStream nextPutAll:'(a FileStream for:'. |
|
683 |
aStream nextPutAll:pathName printString. |
|
684 |
aStream nextPut:$) |
|
685 |
||
686 |
"Modified: 30.7.1997 / 13:51:01 / cg" |
|
687 |
! |
|
688 |
||
689 |
storeOn:aStream |
|
690 |
"append a representation of the receiver to aStream, |
|
691 |
from which a copy can be reconstructed later." |
|
692 |
||
693 |
aStream nextPutAll:'(FileStream oldFileNamed:'. |
|
694 |
aStream nextPutAll:pathName storeString. |
|
695 |
(self position ~~ 1) ifTrue:[ |
|
696 |
aStream nextPutAll:'; position:'. |
|
697 |
self position storeOn:aStream |
|
698 |
]. |
|
699 |
aStream nextPut:$) |
|
700 |
||
701 |
"Modified: 30.7.1997 / 16:43:50 / cg" |
|
702 |
! ! |
|
703 |
||
704 |
!FileStream methodsFor:'private'! |
|
705 |
||
706 |
createForReadWrite |
|
707 |
"create/truncate the file for read/write. |
|
708 |
If the file existed, its truncated; otherwise its created." |
|
709 |
||
710 |
mode := #readwrite. |
|
711 |
^ self openWithMode:CreateReadWriteMode |
|
712 |
! |
|
713 |
||
714 |
createForWriting |
|
715 |
"create/truncate the file for writeonly. |
|
716 |
If the file existed, its truncated; otherwise its created." |
|
717 |
||
718 |
mode := #writeonly. |
|
719 |
didWrite := true. |
|
720 |
^ self openWithMode:WriteMode |
|
721 |
! |
|
722 |
||
723 |
open |
|
724 |
"open the file" |
|
725 |
||
726 |
pathName isNil ifTrue:[^nil]. |
|
727 |
(mode == #readonly) ifTrue: [ |
|
728 |
didWrite := false. |
|
729 |
^ self openWithMode:ReadMode |
|
730 |
]. |
|
731 |
(mode == #writeonly) ifTrue: [ |
|
732 |
didWrite := true. |
|
733 |
^ self openWithMode:WriteMode |
|
734 |
]. |
|
735 |
^ self openWithMode:ReadWriteMode |
|
736 |
! |
|
737 |
||
738 |
openForAppending |
|
739 |
"open the file for writeonly appending to the end. |
|
740 |
If the file does not exist its an error, return nil; |
|
741 |
otherwise return the receiver." |
|
742 |
||
743 |
mode := #writeonly. |
|
744 |
didWrite := true. |
|
745 |
^ self openWithMode:AppendMode |
|
746 |
! |
|
747 |
||
748 |
openForReadWrite |
|
749 |
"open the file for read/write. |
|
750 |
If the file does not exist its an error, return nil; |
|
751 |
otherwise return the receiver." |
|
752 |
||
753 |
mode := #readwrite. |
|
754 |
^ self openWithMode:ReadWriteMode |
|
755 |
! |
|
756 |
||
757 |
openForReading |
|
758 |
"open the file for readonly. |
|
759 |
If the file does not exist its an error, return nil; |
|
760 |
otherwise return the receiver." |
|
761 |
||
762 |
mode := #readonly. |
|
763 |
didWrite := false. |
|
764 |
^ self openWithMode:ReadMode |
|
765 |
! |
|
766 |
||
767 |
openForWriting |
|
768 |
"open the file writeonly. |
|
769 |
If the file does not exist its an error, return nil; |
|
770 |
otherwise return the receiver." |
|
771 |
||
772 |
mode := #writeonly. |
|
773 |
didWrite := true. |
|
774 |
^ self openWithMode:ReadWriteMode "unix-io does not allow this; open for update here" |
|
775 |
! |
|
776 |
||
777 |
openWithMode:openmode |
|
778 |
"open the file; |
|
779 |
openmode is the string defining the way to open as defined by the stdio library |
|
780 |
(i.e. the 2nd fopen argument). |
|
781 |
||
782 |
This is a private entry, but maybe useful to open a file in a special mode, |
|
783 |
which is proprietrary to the operatingSystem." |
|
784 |
||
785 |
^ self openWithMode:openmode attributes:nil |
|
786 |
! |
|
787 |
||
788 |
openWithMode:openmode attributes:attributeSpec |
|
789 |
"open the file; |
|
790 |
openmode is the string defining the way to open as defined by the stdio library |
|
791 |
(i.e. the 2nd fopen argument). |
|
792 |
||
793 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
794 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
795 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
796 |
attributes (see fopen description). |
|
797 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
798 |
This argument is ignored in UNIX & MSDOS systems. |
|
799 |
||
800 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
801 |
which is proprietrary to the operatingSystem." |
|
802 |
||
803 |
|ok| |
|
804 |
||
805 |
filePointer notNil ifTrue:[^ self errorOpen]. |
|
806 |
ok := false. |
|
807 |
%{ |
|
808 |
HFILE f; |
|
809 |
#ifndef xxNO_STDIO |
|
810 |
HFILE fopen(); |
|
811 |
#endif |
|
812 |
OBJ path, fp; |
|
813 |
int pass = 0; |
|
814 |
||
815 |
retry: |
|
816 |
path = __INST(pathName); |
|
817 |
if (__isNonNilObject(path) && (__qClass(path)==String)) { |
|
818 |
do { |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
819 |
# ifdef NO_STDIO |
3746 | 820 |
/*printf("open %s mode=%s\n",__stringVal(path),__stringVal(openmode)); */ |
3818 | 821 |
# ifdef DO_WRAP_CALLS |
3824 | 822 |
f = __STX_C_CALL2((void*)fopen,(void *)__stringVal(path), (void *)__stringVal(openmode)); |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
823 |
# else |
3746 | 824 |
f = fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
825 |
# endif |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
826 |
# else |
3746 | 827 |
__BEGIN_INTERRUPTABLE__ |
828 |
f = fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3755
diff
changeset
|
829 |
__END_INTERRUPTABLE__ |
3746 | 830 |
# endif |
831 |
/* must refetch - could be GC'd */ |
|
832 |
path = __INST(pathName); |
|
833 |
} while ((f == NULL) && (errno == EINTR)); |
|
834 |
||
4369 | 835 |
if ((f == NULL) || (f == (HFILE)(-1))) { |
3746 | 836 |
/* |
837 |
* If no filedescriptors available, try to finalize |
|
838 |
* possibly collected fd's and try again. |
|
839 |
*/ |
|
840 |
if (pass == 0 && (errno == ENFILE || errno == EMFILE)) { |
|
841 |
pass = 1; |
|
842 |
__SSEND0(@global(ObjectMemory), @symbol(scavenge), 0); |
|
843 |
__SSEND0(@global(ObjectMemory), @symbol(finalize), 0); |
|
844 |
goto retry; |
|
845 |
} |
|
846 |
getOutOfHere: ; |
|
847 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
848 |
__INST(position) = nil; |
|
849 |
} else { |
|
850 |
__INST(canPosition) = true; |
|
851 |
||
852 |
__INST(filePointer) = fp = __MKOBJ((INT)f); __STORE(self, fp); |
|
853 |
__INST(position) = __MKSMALLINT(1); |
|
854 |
ok = true; |
|
855 |
} |
|
856 |
} |
|
857 |
%}. |
|
858 |
ok ifFalse:[ |
|
859 |
" |
|
860 |
the open failed for some reason ... |
|
861 |
" |
|
862 |
^ self openError |
|
863 |
]. |
|
864 |
self buffered:true. "default is buffered" |
|
865 |
Lobby register:self. |
|
866 |
! |
|
867 |
||
868 |
pathName:filename |
|
869 |
"set the pathname" |
|
870 |
||
871 |
pathName := filename asString |
|
872 |
||
873 |
"Modified: / 28.10.1997 / 14:29:01 / cg" |
|
874 |
! |
|
875 |
||
876 |
pathName:filename in:aDirectory |
|
877 |
"set the pathname starting at aDirectory, a FileDirectory" |
|
878 |
||
879 |
pathName := aDirectory asFilename constructString:filename |
|
880 |
||
881 |
"Modified: / 28.10.1997 / 14:28:54 / cg" |
|
882 |
! |
|
883 |
||
884 |
reOpen |
|
885 |
"sent after snapin to reopen streams" |
|
886 |
||
887 |
|oldPos| |
|
888 |
||
889 |
filePointer notNil ifTrue:[ |
|
890 |
"it was open, when snapped-out" |
|
891 |
filePointer := nil. |
|
892 |
Lobby unregister:self. |
|
893 |
oldPos := position. |
|
894 |
self open. |
|
895 |
filePointer isNil ifTrue:[ |
|
896 |
"/ this happens, if after a restart, |
|
897 |
"/ the file is no longer present or accessable ..." |
|
898 |
||
899 |
(self class name , ' [warning]: could not reOpen file: ', pathName) errorPrintCR. |
|
900 |
] ifFalse:[ |
|
901 |
self position:oldPos. |
|
902 |
] |
|
903 |
] |
|
904 |
||
905 |
"Modified: 10.1.1997 / 17:50:51 / cg" |
|
906 |
! |
|
907 |
||
908 |
setMode:aModeSymbol |
|
909 |
mode := aModeSymbol |
|
910 |
! ! |
|
911 |
||
912 |
!FileStream methodsFor:'queries'! |
|
913 |
||
914 |
fileSize |
|
915 |
"return the size in bytes of the file" |
|
916 |
|oldPosition sz| |
|
917 |
%{ |
|
918 |
||
919 |
#ifndef xxNO_STDIO |
|
920 |
HFILE f; |
|
921 |
struct stat buf; |
|
922 |
int ret; |
|
923 |
int fd; |
|
924 |
||
925 |
if (__INST(filePointer) != nil) { |
|
926 |
f = __FILEVal(__INST(filePointer)); |
|
927 |
fd = fileno(f); |
|
928 |
do { |
|
929 |
ret = fstat(fd, &buf); |
|
930 |
} while ((ret < 0) && (errno == EINTR)); |
|
931 |
if (ret >= 0) { |
|
932 |
RETURN ( __MKSMALLINT(buf.st_size) ); |
|
933 |
} |
|
934 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
935 |
} |
|
936 |
#endif |
|
937 |
%}. |
|
938 |
||
939 |
"could add a fall-back here: |
|
940 |
||
941 |
oldPosition := self position. |
|
942 |
self setToEnd. |
|
943 |
sz := self position. |
|
944 |
self position:oldPosition. |
|
945 |
^ sz |
|
946 |
" |
|
947 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
948 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
949 |
oldPosition := self position. |
|
950 |
self setToEnd. |
|
951 |
sz := self position. |
|
952 |
self position:oldPosition. |
|
953 |
^ sz |
|
954 |
! |
|
955 |
||
956 |
size |
|
957 |
"return the size of the stream; |
|
958 |
thats the number of bytes of the file." |
|
959 |
||
960 |
"/ no obsoleteMethodWarning - size is ok for streams. |
|
961 |
"/ self obsoleteMethodWarning:'use #fileSize'. |
|
962 |
||
963 |
^ self fileSize. |
|
964 |
||
965 |
"Modified: 4.10.1997 / 18:01:09 / cg" |
|
966 |
! ! |
|
967 |
||
968 |
!FileStream methodsFor:'testing'! |
|
969 |
||
970 |
isFileStream |
|
971 |
"return true, if the receiver is some kind of fileStream. |
|
972 |
redefined from Object" |
|
973 |
||
974 |
^ true |
|
975 |
! ! |
|
976 |
||
977 |
!FileStream class methodsFor:'documentation'! |
|
978 |
||
979 |
version |
|
4369 | 980 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/FileStream_win32.st,v 1.8 1999-07-10 22:17:00 cg Exp $' |
3746 | 981 |
! ! |
982 |
FileStream initialize! |