1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gxclfile.c 8250 2007-09-25 13:31:24Z giles $ */
15
/* File-based command list implementation */
25
/* This is an implementation of the command list I/O interface */
26
/* that uses the file system for storage. */
28
/* ------ Open/close/unlink ------ */
31
clist_fopen(char fname[gp_file_name_sizeof], const char *fmode,
32
clist_file_ptr * pcf, gs_memory_t * mem, gs_memory_t *data_mem,
37
return_error(gs_error_invalidfileaccess);
39
(clist_file_ptr) gp_open_scratch_file_64(gp_scratch_file_name_prefix,
42
*pcf = gp_fopen(fname, fmode);
44
eprintf1("Could not open the scratch file %s.\n", fname);
45
return_error(gs_error_invalidfileaccess);
51
clist_unlink(const char *fname)
53
return (unlink(fname) != 0 ? gs_note_error(gs_error_ioerror) : 0);
57
clist_fclose(clist_file_ptr cf, const char *fname, bool delete)
59
return (fclose((FILE *) cf) != 0 ? gs_note_error(gs_error_ioerror) :
60
delete ? clist_unlink(fname) :
64
/* ------ Writing ------ */
67
clist_fwrite_chars(const void *data, uint len, clist_file_ptr cf)
69
return fwrite(data, 1, len, (FILE *) cf);
72
/* ------ Reading ------ */
75
clist_fread_chars(void *data, uint len, clist_file_ptr cf)
77
FILE *f = (FILE *) cf;
80
/* The typical implementation of fread */
81
/* is extremely inefficient for small counts, */
82
/* so we just use straight-line code instead. */
85
return fread(str, 1, len, f);
87
*str++ = (byte) getc(f);
89
*str++ = (byte) getc(f);
91
*str++ = (byte) getc(f);
93
*str++ = (byte) getc(f);
95
*str++ = (byte) getc(f);
97
*str++ = (byte) getc(f);
99
*str++ = (byte) getc(f);
101
*str = (byte) getc(f);
106
/* ------ Position/status ------ */
109
clist_set_memory_warning(clist_file_ptr cf, int bytes_left)
111
return 0; /* no-op */
115
clist_ferror_code(clist_file_ptr cf)
117
return (ferror((FILE *) cf) ? gs_error_ioerror : 0);
121
clist_ftell(clist_file_ptr cf)
123
return gp_ftell_64((FILE *) cf);
127
clist_rewind(clist_file_ptr cf, bool discard_data, const char *fname)
129
FILE *f = (FILE *) cf;
133
* The ANSI C stdio specification provides no operation for
134
* truncating a file at a given position, or even just for
135
* deleting its contents; we have to use a bizarre workaround to
136
* get the same effect.
140
/* Opening with "w" mode deletes the contents when closing. */
141
freopen(fname, gp_fmode_wb, f);
143
strcat(fmode, gp_fmode_binary_suffix);
144
freopen(fname, fmode, f);
151
clist_fseek(clist_file_ptr cf, int64_t offset, int mode, const char *ignore_fname)
153
return gp_fseek_64((FILE *) cf, offset, mode);
156
static clist_io_procs_t clist_io_procs_file = {
162
clist_set_memory_warning,
169
init_proc(gs_gxclfile_init);
171
gs_gxclfile_init(gs_memory_t *mem)
173
clist_io_procs_file_global = &clist_io_procs_file;