home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / STREAM.C < prev    next >
C/C++ Source or Header  |  1992-09-17  |  13KB  |  476 lines

  1. /* Copyright (C) 1989, 1992 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.c */
  21. /* Stream package for Ghostscript interpreter */
  22. #include "stdio_.h"        /* includes std.h */
  23. #include "memory_.h"
  24. #include "stream.h"
  25. #include "scanchar.h"
  26.  
  27. /* Forward declarations */
  28.     /* Generic */
  29. /* Export these for filters */
  30. void
  31.   s_std_init(P5(stream *, byte *, uint, const stream_procs *, int /*mode*/));
  32. int
  33.   s_std_null(P1(stream *)),
  34.   s_std_noavailable(P2(stream *, long *)),
  35.   s_std_noseek(P2(stream *, long)),
  36.   s_std_close(P1(stream *));
  37.     /* Strings */
  38. private int
  39.   s_string_read_buf(P1(stream *)),
  40.   s_string_write_buf(P1(stream *)),
  41.   s_string_available(P2(stream *, long *)),
  42.   s_string_seek(P2(stream *, long));
  43. private void
  44.   s_string_init(P4(stream *, byte *, uint, const stream_procs *));
  45.     /* Files */
  46. private int
  47.   s_file_read_buf(P1(stream *)),
  48.   s_file_available(P2(stream *, long *)),
  49.   s_file_read_seek(P2(stream *, long)),
  50.   s_file_read_close(P1(stream *));
  51. private int
  52.   s_file_write_buf(P1(stream *)),
  53.   s_file_write_seek(P2(stream *, long)),
  54.   s_file_write_flush(P1(stream *)),
  55.   s_file_write_close(P1(stream *));
  56.  
  57. /* ------ Generic procedures ------ */
  58.  
  59. /* Standard stream initialization */
  60. void
  61. s_std_init(register stream *s, byte *ptr, uint len, const stream_procs *pp,
  62.   int modes)
  63. {    s->cbuf = ptr;
  64.     s->cptr = ptr - 1;
  65.     s->endptr = (!(modes & s_mode_read) ? s->cptr + len : s->cptr);
  66.     s->modes = modes;
  67.     s->end_status = 0;
  68.     s->position = 0;
  69.     s->bsize = s->cbsize = len;
  70.     s->strm = 0;            /* not a filter */
  71.     s->procs = *pp;
  72. }
  73.  
  74. /* Implement a stream procedure as a no-op. */
  75. int
  76. s_std_null(stream *s)
  77. {    return 0;
  78. }
  79.  
  80. /* Flush data to end-of-file when reading. */
  81. int
  82. s_std_read_flush(stream *s)
  83. {    while ( 1 )
  84.     {    s->cptr = s->endptr;
  85.         if ( s->end_status ) break;
  86.         (*s->procs.read_buf)(s);
  87.     }
  88.     return (s->end_status == EOFC ? 0 : s->end_status);
  89. }
  90.  
  91. /* Flush buffered data when writing. */
  92. int
  93. s_std_write_flush(stream *s)
  94. {    return (*s->procs.write_buf)(s);
  95. }
  96.  
  97. /* Indicate an error when asked for available input bytes. */
  98. int
  99. s_std_noavailable(stream *s, long *pl)
  100. {    return ERRC;
  101. }
  102.  
  103. /* Indicate an error when asked to seek. */
  104. int
  105. s_std_noseek(stream *s, long pos)
  106. {    return ERRC;
  107. }
  108.  
  109. /* Standard stream finalization.  Disable the stream. */
  110. int
  111. s_std_close(stream *s)
  112. {    s_disable(s);
  113.     return 0;
  114. }
  115. void
  116. s_disable(register stream *s)
  117. {    s->bsize = 0;
  118.     s->modes = 0;
  119.     /****** SHOULD DO MORE THAN THIS ******/
  120. }
  121.  
  122. /* ------ Implementation-independent procedures ------ */
  123.  
  124. /* Implement sgetc when the buffer may be empty. */
  125. /* If the buffer really is empty, refill it and then read a byte. */
  126. int
  127. spgetc(register stream *s)
  128. {    int code;
  129.     if ( !sendbufp(s) )
  130.         return *++(s->cptr);
  131.     if ( s->end_status )
  132.         return s->end_status;
  133.     code = (*s->procs.read_buf)(s);
  134.     if ( code < 0 )
  135.         return code;
  136.     if ( !sendbufp(s) )
  137.         return *++(s->cptr);
  138.     return (s->end_status ? s->end_status : EOFC);
  139. }
  140.  
  141. /* Implementing sputc when the buffer is full, */
  142. /* by flushing the buffer and then writing the byte. */
  143. int
  144. spputc(register stream *s, byte b)
  145. {    int code;
  146.     if ( s->end_status ) return s->end_status;
  147.     code = (*s->procs.write_buf)(s);
  148.     if ( code < 0 ) return code;
  149.     return sputc(s, b);
  150. }
  151.  
  152. /* Push back a character onto a (read) stream. */
  153. /* The character must be the same as the last one read. */
  154. /* Return 0 on success, ERRC on failure. */
  155. int
  156. sungetc(register stream *s, byte c)
  157. {    if ( !s_is_reading(s) || s->cptr < s->cbuf || *(s->cptr) != c )
  158.         return ERRC;
  159.     s->cptr--;
  160.     return 0;
  161. }
  162.  
  163. /* Read a string from a stream. */
  164. /* Return the number of bytes read. */
  165. uint
  166. sgets(register stream *s, byte *str, uint rlen)
  167. {    uint len = rlen;
  168.     while ( len > 0 )
  169.        {    uint count = sbufavailable(s);
  170.         if ( count == 0 )
  171.            {    int code;
  172.             if ( s->end_status )
  173.                 return rlen - len;
  174.             code = (*s->procs.read_buf)(s);
  175.             if ( code < 0 || sendbufp(s) )
  176.                 return rlen - len;
  177.             continue;
  178.            }
  179.         if ( count > len ) count = len;
  180.         memcpy(str, s->cptr + 1, count);
  181.         s->cptr += count;
  182.         str += count;
  183.         len -= count;
  184.        }
  185.     return rlen;
  186. }
  187.  
  188. /* Write a string on a stream. */
  189. /* Return the number of bytes written. */
  190. uint
  191. sputs(register stream *s, const byte *str, uint wlen)
  192. {    uint len = wlen;
  193.     if ( wlen > s->bsize && s->procs.write_buf == s_file_write_buf )
  194.        {    /* Write directly on the file. */
  195.         uint write_count;
  196.         (*s->procs.write_buf)(s);
  197.         write_count = fwrite(str, 1, wlen, s->file);
  198.         if ( s_can_seek(s) )
  199.             s->position = ftell(s->file);
  200.         return write_count;
  201.        }
  202.     while ( len > 0 )
  203.        {    uint count = sbufavailable(s);
  204.         if ( count > 0 )
  205.            {    if ( count > len ) count = len;
  206.             memcpy(s->cptr + 1, str, count);
  207.             s->cptr += count;
  208.             str += count;
  209.             len -= count;
  210.            }
  211.         else
  212.            {    byte ch = *str++;
  213.             sputc(s, ch);
  214.             if ( s->end_status ) return wlen - len;
  215.             len--;
  216.            }
  217.        }
  218.     return wlen;
  219. }
  220.  
  221. /* Read a hex string from a stream. */
  222. /* Answer EOFC if we reached end-of-file before filling the string, */
  223. /* 0 if we filled the string first, or ERRC on error. */
  224. /* s->odd should be -1 initially: */
  225. /* if an odd number of hex digits was read, s->odd is set to */
  226. /* the odd digit value, otherwise s->odd is set to -1. */
  227. /* If ignore_garbage is true, characters other than hex digits are ignored; */
  228. /* if ignore_garbage is false, characters other than hex digits or */
  229. /* whitespace return an error. */
  230. int
  231. sreadhex(stream *s, byte *str, uint rlen, uint *nread,
  232.   int *odd_digit, int ignore_garbage)
  233. {    byte *ptr = str;
  234.     byte *limit = ptr + rlen;
  235.     byte val1 = (byte)*odd_digit;
  236.     byte val2;
  237.     byte save_last;
  238.     register byte _ds *decoder = scan_char_decoder;
  239.     s_declare_inline(s, sptr, endp);
  240.     int ch;
  241.     int code;
  242.     if ( rlen == 0 )
  243.        {    *nread = 0;
  244.         return 0;
  245.        }
  246.     s_begin_inline(s, sptr, endp);
  247.     if ( val1 <= 0xf ) goto d2;
  248. d1:    /* Fast check for common case */
  249.     if ( sptr >= endp ) goto x1;    /* no last char to save */
  250.     save_last = *endp;
  251.     *endp = ' ';            /* force exit from fast loop */
  252. f1:    if ( (val1 = decoder[sptr[1]]) <= 0xf &&
  253.          (val2 = decoder[sptr[2]]) <= 0xf
  254.        )
  255.        {    sptr += 2;
  256.         *ptr++ = (val1 << 4) + val2;
  257.         if ( ptr < limit ) goto f1;
  258.         *endp = save_last;
  259.         goto px;
  260.        }
  261.     *endp = save_last;
  262. x1:    while ( (val1 = decoder[ch = sgetc_inline(s, sptr, endp)]) > 0xf )
  263.        {    if ( val1 == ctype_eof )
  264.            {    code = ch; *odd_digit = -1; goto ended;    }
  265.         else if ( val1 != ctype_space && !ignore_garbage )
  266.            {    sptr--; *odd_digit = -1; goto err;    }
  267.        }
  268. d2:    while ( (val2 = decoder[ch = sgetc_inline(s, sptr, endp)]) > 0xf )
  269.        {    if ( val2 == ctype_eof )
  270.            {    code = ch; *odd_digit = val1; goto ended;    }
  271.         else if ( val2 != ctype_space && !ignore_garbage )
  272.            {    sptr--; *odd_digit = val1; goto err;    }
  273.        }
  274.     *ptr++ = (val1 << 4) + val2;
  275.     if ( ptr < limit ) goto d1;
  276. px:    *nread = rlen;
  277.     s_end_inline(s, sptr, endp);
  278.     return 0;
  279. err:    code = ERRC;
  280. ended:    *nread = ptr - str;
  281.     s_end_inline(s, sptr, endp);
  282.     return code;
  283. }
  284.  
  285. /* Skip a specified distance in a stream. */
  286. /* Return 0, EOFC, or ERRC. */
  287. int
  288. spskip(register stream *s, long n)
  289. {    if ( n < 0 || !s_is_reading(s) ) return ERRC;
  290.     if ( s_can_seek(s) )
  291.         return sseek(s, stell(s) + n);
  292.     while ( sbufavailable(s) < n )
  293.     {    int code;
  294.         n -= sbufavailable(s) + 1;
  295.         s->cptr = s->endptr;
  296.         if ( s->end_status )
  297.             return s->end_status;
  298.         code = sgetc(s);
  299.         if ( code < 0 ) return code;
  300.     }
  301.     s->cptr += n;
  302.     return 0;
  303. }    
  304.  
  305. /* ------ String streams ------ */
  306.  
  307. /* Initialize a stream for reading a string. */
  308. void
  309. sread_string(register stream *s, const byte *ptr, uint len)
  310. {    static const stream_procs p =
  311.        {    s_string_available, s_string_seek,
  312.         s_std_read_flush, s_std_null,
  313.         s_string_read_buf, NULL
  314.        };
  315.     s_string_init(s, (byte *)ptr, len, &p);
  316.     s->modes = s_mode_read + s_mode_seek;
  317. }
  318. /* Handle end-of-buffer when reading from a string. */
  319. private int
  320. s_string_read_buf(stream *s)
  321. {    s->cptr = s->endptr;
  322.     s->end_status = EOFC;
  323.     return EOFC;
  324. }
  325. /* Return the number of available bytes when reading from a string. */
  326. private int
  327. s_string_available(stream *s, long *pl)
  328. {    *pl = sbufavailable(s);
  329.     if ( *pl == 0 ) *pl = -1;    /* EOF */
  330.     return 0;
  331. }
  332.  
  333. /* Initialize a stream for writing a string. */
  334. void
  335. swrite_string(register stream *s, byte *ptr, uint len)
  336. {    static const stream_procs p =
  337.        {    s_std_noavailable, s_string_seek,
  338.         s_std_write_flush, s_std_null,
  339.         NULL, s_string_write_buf
  340.        };
  341.     s_string_init(s, ptr, len, &p);
  342.     s->modes = s_mode_write + s_mode_seek;
  343. }
  344. /* Handle end-of-buffer when writing a string. */
  345. private int
  346. s_string_write_buf(stream *s)
  347. {    s->cptr = s->endptr;
  348.     s->end_status = EOFC;
  349.     return EOFC;
  350. }
  351.  
  352. /* Seek in a string.  Return 0 if OK, ERRC if not. */
  353. private int
  354. s_string_seek(register stream *s, long pos)
  355. {    if ( pos < 0 || pos > s->bsize ) return ERRC;
  356.     s->cptr = s->cbuf + pos - 1;
  357.     return 0;
  358. }
  359.  
  360. /* Private initialization */
  361. private void
  362. s_string_init(register stream *s, byte *ptr, uint len, const stream_procs *pp)
  363. {    s_std_init(s, ptr, len, pp, s_mode_write);
  364.     s->end_status = EOFC;        /* this is all there is */
  365.     s->file = 0;            /* not a file stream */
  366. }
  367.  
  368. /* ------ File streams ------ */
  369.  
  370. /* Initialize a stream for reading an OS file. */
  371. void
  372. sread_file(register stream *s, FILE *file, byte *buf, uint len)
  373. {    static const stream_procs p =
  374.        {    s_file_available, s_file_read_seek,
  375.         s_std_read_flush, s_file_read_close,
  376.         s_file_read_buf, NULL
  377.        };
  378.     s_std_init(s, buf, len, &p,
  379.            (file == stdin ? s_mode_read : s_mode_read + s_mode_seek));
  380.     s->file = file;
  381. }
  382. /* Procedures for reading from a file */
  383. private int
  384. s_file_read_buf(register stream *s)
  385. {    int nread;
  386.     if ( s_can_seek(s) )
  387.         s->position = ftell(s->file);
  388.     nread = fread(s->cbuf, 1, s->bsize, s->file);
  389.     s->cptr = s->cbuf - 1;
  390.     s->end_status = (ferror(s->file) ? ERRC : feof(s->file) ? EOFC : 0);
  391.     if ( nread <= 0 ) nread = 0;
  392.     s->endptr = s->cptr + nread;
  393.     return 0;
  394. }
  395. private int
  396. s_file_available(register stream *s, long *pl)
  397. {    *pl = sbufavailable(s);
  398.     if ( sseekable(s) )
  399.        {    long pos, end;
  400.         pos = ftell(s->file);
  401.         if ( fseek(s->file, 0L, 2) ) return ERRC;
  402.         end = ftell(s->file);
  403.         if ( fseek(s->file, pos, 0) ) return ERRC;
  404.         *pl += end - pos;
  405.         if ( *pl == 0 ) *pl = -1;    /* EOF */
  406.        }
  407.     else
  408.        {    if ( *pl == 0 && feof(s->file) ) *pl = -1;    /* EOF */
  409.        }
  410.     return 0;
  411. }
  412. private int
  413. s_file_read_seek(register stream *s, long pos)
  414. {    uint end = s->endptr - s->cbuf + 1;
  415.     long offset = pos - s->position;
  416.     if ( offset >= 0 && offset <= end )
  417.        {    /* Staying within the same buffer */
  418.         s->cptr = s->cbuf + offset - 1;
  419.         return 0;
  420.        }
  421.     if ( fseek(s->file, pos, 0) != 0 )
  422.         return ERRC;
  423.     s->endptr = s->cptr = s->cbuf - 1;
  424.     s->end_status = 0;
  425.     return 0;
  426. }
  427. private int
  428. s_file_read_close(stream *s)
  429. {    return fclose(s->file);
  430. }
  431.  
  432. /* Initialize a stream for writing an OS file. */
  433. void
  434. swrite_file(register stream *s, FILE *file, byte *buf, uint len)
  435. {    static const stream_procs p =
  436.        {    s_std_noavailable, s_file_write_seek,
  437.         s_file_write_flush, s_file_write_close,
  438.         NULL, s_file_write_buf
  439.        };
  440.     s_std_init(s, buf, len, &p,
  441.            (file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
  442.     s->file = file;
  443.    }
  444. /* Procedures for writing on a file */
  445. private int
  446. s_file_write_buf(register stream *s)
  447. {    uint count = s->cptr + 1 - s->cbuf;
  448.     uint write_count = fwrite(s->cbuf, 1, count, s->file);
  449.     if ( s_can_seek(s) )
  450.         s->position = ftell(s->file);
  451.     s->cptr = s->cbuf - 1;
  452.     if ( write_count != count )
  453.        {    s->end_status = (ferror(s->file) ? ERRC : EOFC);
  454.         s->endptr = s->cptr;
  455.        }
  456.     else
  457.         s->endptr = s->cptr + s->bsize;
  458.     return 0;
  459. }
  460. private int
  461. s_file_write_seek(stream *s, long pos)
  462. {    /* Output files are not positionable */
  463.     return ERRC;
  464. }
  465. private int
  466. s_file_write_flush(register stream *s)
  467. {    int result = s_file_write_buf(s);
  468.     fflush(s->file);
  469.     return result;
  470. }
  471. private int
  472. s_file_write_close(register stream *s)
  473. {    s_file_write_buf(s);
  474.     return fclose(s->file);
  475. }
  476.