/* Copyright (C) 1989, 1992 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
/* File I/O operators for Ghostscript */
#include "gsmatrix.h" /* for gxdevice.h */
int zreadline_from(P4(byte
*, uint
, uint
*, stream
*));
es_ptr
zget_current_file(P0());
private int write_string(P2(os_ptr
, stream
*));
/* ------ Operators ------ */
zread(register os_ptr op
)
zwrite(register os_ptr op
)
check_write_file(s
, op
- 1);
check_type(*op
, t_integer
);
if ( ch
> 0xff ) return e_rangecheck
;
zreadhexstring(register os_ptr op
)
check_read_file(s
, op
- 1);
check_write_type(*op
, t_string
);
code
= sreadhex(s
, op
->value
.bytes
, r_size(op
), &nread
, &odd
, 1);
/* Reached end-of-file before filling the string. */
/* Return an appropriate substring. */
zwritehexstring(register os_ptr op
)
register const char _ds
*hex_digits
= "0123456789abcdef";
check_write_file(s
, op
- 1);
check_read_type(*op
, t_string
);
sputc(s
, hex_digits
[ch
>> 4]);
sputc(s
, hex_digits
[ch
& 0xf]);
zreadstring(register os_ptr op
)
check_read_file(s
, op
- 1);
check_write_type(*op
, t_string
);
rlen
= sgets(s
, op
->value
.bytes
, len
);
make_bool(op
, (rlen
== len
? 1 : 0));
zwritestring(register os_ptr op
)
check_write_file(s
, op
- 1);
code
= write_string(op
, s
);
zreadline(register os_ptr op
)
check_read_file(s
, op
- 1);
check_write_type(*op
, t_string
);
code
= zreadline_from(op
->value
.bytes
, r_size(op
), &count
, s
);
if ( code
< 0 ) return code
;
/* Read a line from stdin. This is called from gs.c. */
zreadline_stdin(byte
*ptr
, uint size
, uint
*pcount
)
{ return zreadline_from(ptr
, size
, pcount
, &std_files
[0]);
/* Internal readline routine. */
/* Returns 1 if OK, 0 if end of file, or an error code. */
zreadline_from(byte
*ptr
, uint size
, uint
*pcount
, stream
*s
)
{ switch ( ch
= sgetc(s
) )
if ( ch
!= '\n' && ch
>= 0 ) sputback(s
);
if ( count
>= size
) /* filled the string */
/* token - this is called from zstring.c */
ztoken_file(register os_ptr op
)
switch ( code
= scan_token(s
, 0, &token
) )
case 0: /* read a token */
zbytesavailable(register os_ptr op
)
if ( savailable(s
, &avail
) < 0 ) return e_ioerror
;
zflush(register os_ptr op
)
zflushfile(register os_ptr op
)
fseek(s
->file
, 0L, 2); /* set to end */
zresetfile(register os_ptr op
)
zstatus(register os_ptr op
)
make_bool(op
, (fptr(op
)->bsize
!= 0 ? 1 : 0));
{ char *fname
= ref_to_string(op
, "status");
if ( fname
== 0 ) return e_VMerror
;
if ( gp_file_status(fname
, &fstat
) )
make_int(op
- 4, fstat
.size_pages
);
make_int(op
- 3, fstat
.size_bytes
);
make_int(op
- 2, fstat
.time_referenced
);
make_int(op
- 1, fstat
.time_created
);
alloc_free(fname
, r_size(op
) + 1, 1, "status");
zcurrentfile(register os_ptr op
)
/* Check the cache first */
else if ( (fp
= zget_current_file()) == 0 )
{ /* Return an invalid file object. */
/* This doesn't make a lot of sense to me, */
/* but it's what the PostScript manual specifies. */
make_file(op
, 0, &invalid_file_entry
);
/* Make the returned value literal. */
r_clear_attrs(op
, a_executable
);
zprint(register os_ptr op
)
{ int code
= write_string(op
, &std_files
[1]);
zecho(register os_ptr op
)
{ check_type(*op
, t_boolean
);
/****** NOT IMPLEMENTED YET ******/
/* ------ Level 2 extensions ------ */
zfileposition(register os_ptr op
)
if ( !sseekable(s
) ) return e_ioerror
;
zsetfileposition(register os_ptr op
)
check_type(*op
, t_integer
);
if ( sseek(s
, op
->value
.intval
) < 0 ) return e_ioerror
;
/* ------ Ghostscript extensions ------ */
zunread(register os_ptr op
)
check_read_file(s
, op
- 1);
check_type(*op
, t_integer
);
if ( ch
> 0xff ) return e_rangecheck
;
if ( sungetc(s
, (byte
)ch
) < 0 ) return e_ioerror
;
zwriteppmfile(register os_ptr op
)
check_write_file(s
, op
- 1);
check_type(*op
, t_device
);
if ( !gs_device_is_memory(op
->value
.pdevice
) ) return e_typecheck
;
code
= gs_writeppmfile((gx_device_memory
*)(op
->value
.pdevice
), s
->file
);
/* ------ Initialization procedure ------ */
op_def zfileio_op_defs
[] = {
{"1bytesavailable", zbytesavailable
},
{"0currentfile", zcurrentfile
},
{"1fileposition", zfileposition
},
{"1flushfile", zflushfile
},
{"2readhexstring", zreadhexstring
},
{"2readline", zreadline
},
{"2readstring", zreadstring
},
{"1resetfile", zresetfile
},
{"2setfileposition", zsetfileposition
},
{"2writehexstring", zwritehexstring
},
{"2writeppmfile", zwriteppmfile
},
{"2writestring", zwritestring
},
/* ------ Non-operator routines ------ */
/* Check a file for reading. */
/* The interpreter calls this to check an executable file. */
file_check_read(ref
*op
, stream
**ps
)
{ if ( !s_is_reading(*ps
= fptr(op
)) ) return e_invalidaccess
;
/* Get the current file from which the interpreter is reading. */
{ register es_ptr ep
= esp
;
{ if ( r_has_type_attrs(ep
, t_file
, a_executable
) )
/* ------ Internal routines ------ */
/* Write a string on a file. The file has been checked for validity, */
/* but not the string. */
write_string(os_ptr op
, stream
*s
)
check_read_type(*op
, t_string
);
if ( sputs(s
, op
->value
.bytes
, len
) != len
) return e_ioerror
;