home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / h / stream < prev    next >
Encoding:
Text File  |  1991-10-25  |  6.2 KB  |  157 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* stream.h */
  21. /* Definitions for standard stream package for Ghostscript */
  22. /* Requires stdio.h */
  23.  
  24. /* Note that the stream package works with bytes, not chars. */
  25. /* This is to ensure unsigned representation on all systems. */
  26. /* A stream can only be read or written, not both. */
  27. /* Note also that the read procedure returns an int, */
  28. /* not a char or a byte, because EOFC is -1. */
  29. typedef struct stream_s stream;
  30. typedef struct {
  31.         /* Store # available for reading */
  32.     int (*available)(P2(stream *, long *));
  33.         /* Set position */
  34.     int (*seek)(P2(stream *, long));
  35.         /* Flush buffered data */
  36.     int (*flush)(P1(stream *));
  37.         /* Flush data (if writing) & close stream */
  38.     int (*close)(P1(stream *));
  39.         /* Refill buffer, reset cptr, return count or EOFC */
  40.     int (*read_buf)(P1(stream *));
  41.         /* Write buffer, reset cptr, return 0/error */
  42.     int (*write_buf)(P1(stream *));
  43. } stream_procs;
  44. /* Structs for specialized streams -- see below. */
  45. struct lzw_decode_s;
  46. struct lzw_encode_s;
  47. struct stream_s {
  48.     byte *cptr;            /* pointer to last byte */
  49.                     /* read or written */
  50.     byte *endptr;            /* pointer to last byte */
  51.                     /* containing data for reading, */
  52.                     /* or to be filled for writing */
  53.     byte *cbuf;            /* base of buffer */
  54.     uint bsize;            /* size of buffer */
  55.     char writing;            /* 0 if reading, 1 if writing */
  56.     char eof;            /* non-zero if at EOF when buffer */
  57.                     /* becomes empty */
  58.     long position;            /* file position of beginning of */
  59.                     /* buffer, -1 means not seekable */
  60.     stream_procs procs;
  61.     int num_format;            /* format for Level 2 */
  62.                     /* encoded number reader */
  63.                     /* (only used locally) */
  64.     /* strm is non-zero iff this is a filter stream. */
  65.     stream *strm;            /* the underlying stream */
  66.     /*
  67.      * If were were able to program in a real object-oriented style, 
  68.      * the remaining data would be per-subclass.  It's just too much
  69.      * of a nuisance to do this in C, so we allocate space for the
  70.      * private data of ALL subclasses.
  71.      */
  72.     /* The following are for file streams. */
  73.     FILE *file;            /* file handle for C library */
  74.     int can_close;            /* 0 for stdin/out/err, */
  75.                     /* -1 for line/statementedit, */
  76.                     /* 1 for other files */
  77.     stream *prev, *next;        /* keep track of all files */
  78.     /* The following is used by several decoding filters. */
  79.     int odd;            /* odd digit */
  80.     /* The following are for RunLengthEncode filters. */
  81.     uint record_size;
  82.     uint record_left;        /* bytes left in current record */
  83.     /* The following are for eexec streams. */
  84.     ushort cstate;            /* encryption state */
  85.     int binary;            /* true=binary, false=hex */
  86.     /* The following are for LZW encoding/decoding streams. */
  87.     byte bits;        /* most recent byte of input or */
  88.                 /* current byte of output */
  89.     int bits_left;        /* # of unused low bits in above, [0..7] */
  90.     struct lzw_decode_s *decode;    /* decoding table */
  91.     struct lzw_encode_s *encode;    /* encoding table */
  92.     uint next_code;            /* next code to be assigned */
  93.     int code_size;            /* current # of bits per code */
  94.     int prev_code;            /* previous code recognized */
  95.                     /* or assigned */
  96.     uint prev_hash;            /* accumulated encoding hash */
  97. };
  98.  
  99. /* Stream functions.  Some of these are macros -- beware. */
  100. /* Also note that unlike the C stream library, */
  101. /* ALL stream procedures take the stream as the first argument. */
  102.  
  103. /* Following are valid for all streams. */
  104. /* flush is a no-op for read streams. */
  105. /* close is NOT a no-op for non-file streams -- */
  106. /* it actively disables them. */
  107. #define sseekable(s) ((s)->position >= 0)
  108. #define savailable(s,pl) (*(s)->procs.available)(s,pl)
  109. #define sflush(s) (*(s)->procs.flush)(s)
  110. #define sclose(s) (*(s)->procs.close)(s)
  111.  
  112. /* Following are only valid for read streams. */
  113. extern int spgetc(P1(stream *));
  114. #define sgetc(s)\
  115.   ((s)->cptr < (s)->endptr ? *++((s)->cptr) : spgetc(s))
  116. extern uint sgets(P3(stream *, byte *, uint));
  117. extern int sreadhex(P5(stream *, byte *, uint, uint *, int *));
  118. extern int sungetc(P2(stream *, byte));    /* -1 on error, 0 if OK */
  119. #define sputback(s) ((s)->cptr--)
  120. #define seofp(s) ((s)->cptr >= (s)->endptr && (s)->eof)
  121.  
  122. /* Following are only valid for write streams. */
  123. extern int spputc(P2(stream *, byte));
  124. #define sputc(s,c)\
  125.   ((s)->cptr < (s)->endptr ? ((int)(*++((s)->cptr)=(c))) :\
  126.    spputc((s),(c)))
  127. extern uint sputs(P3(stream *, byte *, uint));
  128.  
  129. /* Following are only valid for positionable streams. */
  130. #define stell(s) ((s)->cptr + 1 - (s)->cbuf + (s)->position)
  131. #define sseek(s,pos) (*(s)->procs.seek)(s,(long)(pos))
  132.  
  133. /* Following are for high-performance clients. */
  134. /* bufptr points to the next item, bufend points beyond the last item. */
  135. #define sbufptr(s) ((s)->cptr + 1)
  136. #define sbufend(s) ((s)->endptr + 1)
  137. #define ssetbufptr(s,ptr) ((s)->cptr = (ptr) - 1)
  138. #define sbufavailable(s) ((s)->endptr - (s)->cptr)
  139.  
  140. #define EOFC (-1)
  141.  
  142. /* Stream creation procedures */
  143. extern    void    sread_string(P3(stream *, byte *, uint)),
  144.         swrite_string(P3(stream *, byte *, uint));
  145. extern    void    sread_file(P4(stream *, FILE *, byte *, uint)),
  146.         swrite_file(P4(stream *, FILE *, byte *, uint));
  147. extern    void    sread_decrypt(P5(stream *, stream *, byte *, uint, ushort));
  148.  
  149. /* Standard stream initialization */
  150. extern    void    s_std_init(P5(stream *, byte *, uint, stream_procs *, int /*writing*/));
  151. /* Standard stream finalization */
  152. extern    void    s_disable(P1(stream *));
  153. /* Generic stream procedures exported for filters */
  154. extern    int    s_std_null(P1(stream *)),
  155.         s_std_noavailable(P2(stream *, long *)),
  156.         s_std_close(P1(stream *));
  157.