1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/* Copyright (C) 1993, 2000 Aladdin Enterprises. All rights reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
For more information about licensing, please refer to
http://www.ghostscript.com/licensing/. For information on
commercial licensing, go to http://www.artifex.com/licensing/ or
contact Artifex Software, Inc., 101 Lucas Valley Road #110,
San Rafael, CA 94903, U.S.A., +1(415)492-9861.
*/
/*$RCSfile$ $Revision$ */
/* File stream implementation using stdio */
#include "stdio_.h" /* includes std.h */
#include "memory_.h"
#include "gdebug.h"
#include "gpcheck.h"
#include "stream.h"
#include "strimpl.h"
/* Forward references for file stream procedures */
private int
s_file_available(P2(stream *, long *)),
s_file_read_seek(P2(stream *, long)),
s_file_read_close(P1(stream *)),
s_file_read_process(P4(stream_state *, stream_cursor_read *,
stream_cursor_write *, bool));
private int
s_file_write_seek(P2(stream *, long)),
s_file_write_flush(P1(stream *)),
s_file_write_close(P1(stream *)),
s_file_write_process(P4(stream_state *, stream_cursor_read *,
stream_cursor_write *, bool));
private int
s_file_switch(P2(stream *, bool));
/* ------ File reading ------ */
/* Initialize a stream for reading an OS file. */
void
sread_file(register stream * s, FILE * file, byte * buf, uint len)
{
static const stream_procs p = {
s_file_available, s_file_read_seek, s_std_read_reset,
s_std_read_flush, s_file_read_close, s_file_read_process,
s_file_switch
};
/*
* There is no really portable way to test seekability, but this should
* work on most systems. Note that if our probe sets the ferror bit for
* the stream, we have to clear it again to avoid trouble later.
*/
int had_error = ferror(file);
long curpos = ftell(file);
bool seekable = (curpos != -1L && fseek(file, curpos, SEEK_SET) == 0);
if (!had_error)
clearerr(file);
s_std_init(s, buf, len, &p,
(seekable ? s_mode_read + s_mode_seek : s_mode_read));
if_debug1('s', "[s]read file=0x%lx\n", (ulong) file);
s->file = file;
s->file_modes = s->modes;
s->file_offset = 0;
s->file_limit = max_long;
}
/* Confine reading to a subfile. This is primarily for reusable streams. */
int
sread_subfile(stream *s, long start, long length)
{
if (s->file == 0 || s->modes != s_mode_read + s_mode_seek ||
s->file_offset != 0 || s->file_limit != max_long ||
((s->position < start || s->position > start + length) &&
sseek(s, start) < 0)
)
return ERRC;
s->position -= start;
s->file_offset = start;
s->file_limit = length;
return 0;
}
/* Procedures for reading from a file */
private int
s_file_available(register stream * s, long *pl)
{
long max_avail = s->file_limit - stell(s);
long buf_avail = sbufavailable(s);
*pl = min(max_avail, buf_avail);
if (sseekable(s)) {
long pos, end;
pos = ftell(s->file);
if (fseek(s->file, 0L, SEEK_END))
return ERRC;
end = ftell(s->file);
if (fseek(s->file, pos, SEEK_SET))
return ERRC;
buf_avail += end - pos;
*pl = min(max_avail, buf_avail);
if (*pl == 0)
*pl = -1; /* EOF */
} else {
if (*pl == 0 && feof(s->file))
*pl = -1; /* EOF */
}
return 0;
}
private int
s_file_read_seek(register stream * s, long pos)
{
uint end = s->srlimit - s->cbuf + 1;
long offset = pos - s->position;
if (offset >= 0 && offset <= end) { /* Staying within the same buffer */
s->srptr = s->cbuf + offset - 1;
return 0;
}
if (pos < 0 || pos > s->file_limit ||
fseek(s->file, s->file_offset + pos, SEEK_SET) != 0
)
return ERRC;
s->srptr = s->srlimit = s->cbuf - 1;
s->end_status = 0;
s->position = pos;
return 0;
}
private int
s_file_read_close(stream * s)
{
FILE *file = s->file;
if (file != 0) {
s->file = 0;
return (fclose(file) ? ERRC : 0);
}
return 0;
}
/*
* Process a buffer for a file reading stream.
* This is the first stream in the pipeline, so pr is irrelevant.
*/
private int
s_file_read_process(stream_state * st, stream_cursor_read * ignore_pr,
stream_cursor_write * pw, bool last)
{
stream *s = (stream *)st; /* no separate state */
FILE *file = s->file;
uint max_count = pw->limit - pw->ptr;
int status = 1;
int count;
if (s->file_limit < max_long) {
long limit_count = s->file_offset + s->file_limit - ftell(file);
if (max_count > limit_count)
max_count = limit_count, status = EOFC;
}
count = fread(pw->ptr + 1, 1, max_count, file);
if (count < 0)
count = 0;
pw->ptr += count;
process_interrupts();
return (ferror(file) ? ERRC : feof(file) ? EOFC : status);
}
/* ------ File writing ------ */
/* Initialize a stream for writing an OS file. */
void
swrite_file(register stream * s, FILE * file, byte * buf, uint len)
{
static const stream_procs p = {
s_std_noavailable, s_file_write_seek, s_std_write_reset,
s_file_write_flush, s_file_write_close, s_file_write_process,
s_file_switch
};
s_std_init(s, buf, len, &p,
(file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
if_debug1('s', "[s]write file=0x%lx\n", (ulong) file);
s->file = file;
s->file_modes = s->modes;
s->file_offset = 0; /* in case we switch to reading later */
s->file_limit = max_long; /* ibid. */
}
/* Initialize for appending to an OS file. */
void
sappend_file(register stream * s, FILE * file, byte * buf, uint len)
{
swrite_file(s, file, buf, len);
s->modes = s_mode_write + s_mode_append; /* no seek */
s->file_modes = s->modes;
fseek(file, 0L, SEEK_END);
s->position = ftell(file);
}
/* Procedures for writing on a file */
private int
s_file_write_seek(stream * s, long pos)
{
/* We must flush the buffer to reposition. */
int code = sflush(s);
if (code < 0)
return code;
if (fseek(s->file, pos, SEEK_SET) != 0)
return ERRC;
s->position = pos;
return 0;
}
private int
s_file_write_flush(register stream * s)
{
int result = s_process_write_buf(s, false);
fflush(s->file);
return result;
}
private int
s_file_write_close(register stream * s)
{
s_process_write_buf(s, true);
return s_file_read_close(s);
}
/*
* Process a buffer for a file writing stream.
* This is the last stream in the pipeline, so pw is irrelevant.
*/
private int
s_file_write_process(stream_state * st, stream_cursor_read * pr,
stream_cursor_write * ignore_pw, bool last)
{
uint count = pr->limit - pr->ptr;
/*
* The DEC C library on AXP architectures gives an error on
* fwrite if the count is zero!
*/
if (count != 0) {
FILE *file = ((stream *) st)->file;
int written = fwrite(pr->ptr + 1, 1, count, file);
if (written < 0)
written = 0;
pr->ptr += written;
process_interrupts();
return (ferror(file) ? ERRC : 0);
} else {
process_interrupts();
return 0;
}
}
/* ------ File switching ------ */
/* Switch a file stream to reading or writing. */
private int
s_file_switch(stream * s, bool writing)
{
uint modes = s->file_modes;
FILE *file = s->file;
long pos;
if (writing) {
if (!(s->file_modes & s_mode_write))
return ERRC;
pos = stell(s);
if_debug2('s', "[s]switch 0x%lx to write at %ld\n",
(ulong) s, pos);
fseek(file, pos, SEEK_SET);
if (modes & s_mode_append) {
sappend_file(s, file, s->cbuf, s->cbsize); /* sets position */
} else {
swrite_file(s, file, s->cbuf, s->cbsize);
s->position = pos;
}
s->modes = modes;
} else {
if (!(s->file_modes & s_mode_read))
return ERRC;
pos = stell(s);
if_debug2('s', "[s]switch 0x%lx to read at %ld\n",
(ulong) s, pos);
if (sflush(s) < 0)
return ERRC;
fseek(file, 0L, SEEK_CUR); /* pacify C library */
sread_file(s, file, s->cbuf, s->cbsize);
s->modes |= modes & s_mode_append; /* don't lose append info */
s->position = pos;
}
s->file_modes = modes;
return 0;
}
|