386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / stream.h
CommitLineData
c58b7e5e
WJ
1/* Copyright (C) 1989, 1992 Aladdin Enterprises. All rights reserved.
2 Distributed by Free Software Foundation, Inc.
3
4This file is part of Ghostscript.
5
6Ghostscript is distributed in the hope that it will be useful, but
7WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8to anyone for the consequences of using it or for whether it serves any
9particular purpose or works at all, unless he says so in writing. Refer
10to the Ghostscript General Public License for full details.
11
12Everyone is granted permission to copy, modify and redistribute
13Ghostscript, but only under the conditions described in the Ghostscript
14General Public License. A copy of this license is supposed to have been
15given to you along with Ghostscript so you can know your rights and
16responsibilities. It should be in a file named COPYING. Among other
17things, the copyright notice and this notice must be preserved on all
18copies. */
19
20/* stream.h */
21/* Definitions for Ghostscript stream package */
22/* Requires stdio.h */
23
24/*
25 * Note that the stream package works with bytes, not chars.
26 * This is to ensure unsigned representation on all systems.
27 * A stream currently can only be read or written, not both.
28 * Note also that the read procedure returns an int,
29 * not a char or a byte, so we can use negative values for EOFC and ERRC.
30 * We distinguish "data" from "signal" results (EOFC, ERRC) with a macro:
31 */
32#define char_is_data(c) ((c) >= 0)
33#define char_is_signal(c) ((c) < 0)
34typedef struct stream_s stream;
35typedef struct {
36
37 /* Store # available for reading. */
38 /* Return 0 if OK, ERRC if error or not implemented. */
39 int (*available)(P2(stream *, long *));
40
41 /* Set position. */
42 /* Return 0 if OK, ERRC if error or not implemented. */
43 int (*seek)(P2(stream *, long));
44
45 /* Flush buffered data. */
46 /* Return 0 if OK, ERRC if error. */
47 int (*flush)(P1(stream *));
48
49 /* Flush data (if writing) & close stream. */
50 /* Return 0 if OK, ERRC if error. */
51 int (*close)(P1(stream *));
52
53 /* Refill buffer and reset cptr. */
54 /* Return ERRC if not implemented; */
55 /* otherwise, set end_status appropriately and return 0. */
56 int (*read_buf)(P1(stream *));
57
58 /* Write buffer, reset cptr. */
59 /* Return 0 if OK, ERRC if error or not implemented. */
60 int (*write_buf)(P1(stream *));
61
62} stream_procs;
63/* Structs for specialized streams -- see below. */
64struct lzw_decode_table_s;
65struct lzw_encode_table_s;
66struct stream_s {
67 byte *cptr; /* pointer to last byte */
68 /* read or written */
69 byte *endptr; /* pointer to last byte */
70 /* containing data for reading, */
71 /* or to be filled for writing */
72 byte *cbuf; /* base of buffer */
73 uint bsize; /* size of buffer, 0 if closed */
74 uint cbsize; /* size of buffer */
75 char mode; /* 2 if reading, 1 if writing */
76#define s_mode_read 2
77#define s_mode_write 1
78#define s_is_valid(s) ((s)->mode != 0)
79#define s_is_reading(s) ((s)->mode == s_mode_read)
80#define s_is_writing(s) ((s)->mode == s_mode_write)
81 int end_status; /* EOFC if at EOF when buffer */
82 /* becomes empty, ERRC if error */
83 long position; /* file position of beginning of */
84 /* buffer, -1 means not seekable */
85 stream_procs procs;
86 int num_format; /* format for Level 2 */
87 /* encoded number reader */
88 /* (only used locally) */
89 /* strm is non-zero iff this is a filter stream. */
90 stream *strm; /* the underlying stream */
91 int strm_is_temp; /* if true, strm is a temporary */
92 /* stream and should be freed */
93 /* when this stream is closed */
94 /*
95 * If were were able to program in a real object-oriented style,
96 * the remaining data would be per-subclass. It's just too much
97 * of a nuisance to do this in C, so we allocate space for the
98 * private data of ALL subclasses.
99 */
100 /* The following are for file streams. */
101 FILE *file; /* file handle for C library */
102 int can_close; /* 0 for stdin/out/err, */
103 /* -1 for line/statementedit, */
104 /* 1 for other files */
105 stream *prev, *next; /* keep track of all files */
106 /* The following is used by several decoding filters. */
107 int odd; /* odd digit */
108 /* The following are for RunLengthEncode filters. */
109 ulong record_size;
110 /* The following is for RunLengthEncode and PFBDecode. */
111 ulong record_left; /* bytes left in current record */
112 /* The following are for PFBDecode. */
113 int record_type;
114 /* The following are for eexec streams. */
115 ushort cstate; /* encryption state */
116 int binary; /* true=binary, false=hex */
117 /* The following are for LZW encoding/decoding streams. */
118 int enhanced; /* if true, use Aladdin's */
119 /* enhanced compression algorithm */
120 /* (Patent Pending) */
121 byte bits; /* most recent byte of input or */
122 /* current byte of output */
123 int bits_left; /* # of unused low bits in above, [0..7] */
124 struct lzw_decode_table_s *decode_table; /* decoding table */
125 struct lzw_encode_table_s *encode_table; /* encoding table */
126 uint next_code; /* next code to be assigned */
127 int code_size; /* current # of bits per code */
128 int prev_code; /* previous code recognized */
129 /* or assigned */
130};
131
132/* Stream functions. Some of these are macros -- beware. */
133/* Also note that unlike the C stream library, */
134/* ALL stream procedures take the stream as the first argument. */
135#define sendbufp(s) ((s)->cptr >= (s)->endptr) /* not for clients */
136
137/* Following are valid for all streams. */
138/* flush is a no-op for read streams. */
139/* close is NOT a no-op for non-file streams -- */
140/* it actively disables them. */
141/* The close routine must do a flush if needed. */
142#define sseekable(s) ((s)->position >= 0)
143#define serrorp(s) ((s)->cptr >= (s)->endptr && (s)->end_status == ERRC)
144#define savailable(s,pl) (*(s)->procs.available)(s,pl)
145#define sflush(s) (*(s)->procs.flush)(s)
146#define sclose(s) (*(s)->procs.close)(s)
147
148/* Following are only valid for read streams. */
149extern int spgetc(P1(stream *));
150#define sgetc(s) (!sendbufp(s) ? *++((s)->cptr) : spgetc(s))
151extern uint sgets(P3(stream *, byte *, uint));
152extern int sreadhex(P6(stream *, byte *, uint, uint *, int *, int));
153extern int sungetc(P2(stream *, byte)); /* ERRC on error, 0 if OK */
154#define sputback(s) ((s)->cptr--)
155#define seofp(s) (sendbufp(s) && (s)->end_status == EOFC)
156
157/* Following are only valid for write streams. */
158extern int spputc(P2(stream *, byte));
159#define sputc(s,c)\
160 (!sendbufp(s) ? ((int)(*++((s)->cptr)=(c))) : spputc((s),(c)))
161extern uint sputs(P3(stream *, const byte *, uint));
162
163/* Following are only valid for positionable streams. */
164#define stell(s) ((s)->cptr + 1 - (s)->cbuf + (s)->position)
165#define sseek(s,pos) (*(s)->procs.seek)(s,(long)(pos))
166
167/* Following are for high-performance clients. */
168/* bufptr points to the next item, bufend points beyond the last item. */
169#define sbufptr(s) ((s)->cptr + 1)
170#define sbufend(s) ((s)->endptr + 1)
171#define ssetbufptr(s,ptr) ((s)->cptr = (ptr) - 1)
172#define sbufavailable(s) ((s)->endptr - (s)->cptr)
173
174/* We cast EOFC and ERRC to int explicitly, because some compilers */
175/* don't do this if the other arm of a conditional is a byte. */
176/* Clients should use char_is_data and char_is_signal (see above) */
177/* to test for exceptional results. */
178#define EOFC ((int)(-1))
179#define ERRC ((int)(-2))
180/****** ERRC IS NOT RECOGNIZED IN MOST PLACES YET ******/
181
182/* Stream creation procedures */
183extern void sread_string(P3(stream *, byte *, uint)),
184 swrite_string(P3(stream *, byte *, uint));
185extern void sread_file(P4(stream *, FILE *, byte *, uint)),
186 swrite_file(P4(stream *, FILE *, byte *, uint));
187
188/* Standard stream initialization */
189extern void s_std_init(P5(stream *, byte *, uint, stream_procs *, int /*mode*/));
190/* Standard stream finalization */
191extern void s_disable(P1(stream *));
192/* Generic stream procedures exported for filters */
193extern int s_std_null(P1(stream *)),
194 s_std_noavailable(P2(stream *, long *)),
195 s_std_close(P1(stream *));