home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / SBASIC.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  8KB  |  255 lines

  1. /* Copyright (C) 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* sbasic.c */
  20. /* Basic (file and string) streams for Ghostscript */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "memory_.h"
  23. #include "gdebug.h"
  24. #include "gsmemory.h"
  25. #include "gstypes.h"        /* for gsstruct.h */
  26. #include "gsstruct.h"
  27. #include "gpcheck.h"
  28. #include "stream.h"
  29. #include "strimpl.h"
  30.  
  31.     /* Strings */
  32. private int
  33.   s_string_available(P2(stream *, long *)),
  34.   s_string_read_seek(P2(stream *, long)),
  35.   s_string_write_seek(P2(stream *, long)),
  36.   s_string_read_process(P4(stream_state *, stream_cursor_read *,
  37.     stream_cursor_write *, bool)),
  38.   s_string_write_process(P4(stream_state *, stream_cursor_read *,
  39.     stream_cursor_write *, bool));
  40.     /* Files */
  41. private int
  42.   s_file_available(P2(stream *, long *)),
  43.   s_file_read_seek(P2(stream *, long)),
  44.   s_file_read_close(P1(stream *)),
  45.   s_file_read_process(P4(stream_state *, stream_cursor_read *,
  46.     stream_cursor_write *, bool));
  47. private int
  48.   s_file_write_seek(P2(stream *, long)),
  49.   s_file_write_flush(P1(stream *)),
  50.   s_file_write_close(P1(stream *)),
  51.   s_file_write_process(P4(stream_state *, stream_cursor_read *,
  52.     stream_cursor_write *, bool));
  53.  
  54. /* ------ String streams ------ */
  55.  
  56. /* Initialize a stream for reading a string. */
  57. void
  58. sread_string(register stream *s, const byte *ptr, uint len)
  59. {    static const stream_procs p =
  60.        {    s_string_available, s_string_read_seek, s_std_read_reset,
  61.         s_std_read_flush, s_std_null, s_string_read_process
  62.        };
  63.     s_std_init(s, (byte *)ptr, len, &p, s_mode_read + s_mode_seek);
  64.     s->srlimit = s->swlimit;
  65. }
  66. /* Return the number of available bytes when reading from a string. */
  67. private int
  68. s_string_available(stream *s, long *pl)
  69. {    *pl = sbufavailable(s);
  70.     if ( *pl == 0 ) *pl = -1;    /* EOF */
  71.     return 0;
  72. }
  73.  
  74. /* Seek in a string being read.  Return 0 if OK, ERRC if not. */
  75. private int
  76. s_string_read_seek(register stream *s, long pos)
  77. {    if ( pos < 0 || pos > s->bsize ) return ERRC;
  78.     s->srptr = s->cbuf + pos - 1;
  79.     return 0;
  80. }
  81.  
  82. /* Initialize a stream for writing a string. */
  83. void
  84. swrite_string(register stream *s, byte *ptr, uint len)
  85. {    static const stream_procs p =
  86.        {    s_std_noavailable, s_string_write_seek, s_std_write_reset,
  87.         s_std_write_flush, s_std_null, s_string_write_process
  88.        };
  89.     s_std_init(s, ptr, len, &p, s_mode_write + s_mode_seek);
  90. }
  91.  
  92. /* Seek in a string being written.  Return 0 if OK, ERRC if not. */
  93. private int
  94. s_string_write_seek(register stream *s, long pos)
  95. {    if ( pos < 0 || pos > s->bsize ) return ERRC;
  96.     s->swptr = s->cbuf + pos - 1;
  97.     return 0;
  98. }
  99.  
  100. /* Since we initialize the input buffer of a string read stream */
  101. /* to contain all of the data in the string, if we are ever asked */
  102. /* to refill the buffer, we should signal EOF. */
  103. private int
  104. s_string_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  105.   stream_cursor_write *pw, bool last)
  106. {    return EOFC;
  107. }
  108. /* Similarly, if we are ever asked to empty the buffer, it means that */
  109. /* there has been an overrun (unless we are closing the stream). */
  110. private int
  111. s_string_write_process(stream_state *st, stream_cursor_read *pr,
  112.   stream_cursor_write *ignore_pw, bool last)
  113. {    return (last ? EOFC : ERRC);
  114. }
  115.  
  116. /* ------ File streams ------ */
  117.  
  118. /* Initialize a stream for reading an OS file. */
  119. void
  120. sread_file(register stream *s, FILE *file, byte *buf, uint len)
  121. {    static const stream_procs p =
  122.        {    s_file_available, s_file_read_seek, s_std_read_reset,
  123.         s_std_read_flush, s_file_read_close, s_file_read_process
  124.        };
  125.     s_std_init(s, buf, len, &p,
  126.            (file == stdin ? s_mode_read : s_mode_read + s_mode_seek));
  127.     if_debug1('s', "[s]read file=0x%lx\n", (ulong)file);
  128.     s->file = file;
  129.     s->state = (stream_state *)s;    /* hack to avoid separate state */
  130. }
  131. /* Procedures for reading from a file */
  132. private int
  133. s_file_available(register stream *s, long *pl)
  134. {    *pl = sbufavailable(s);
  135.     if ( sseekable(s) )
  136.        {    long pos, end;
  137.         pos = ftell(s->file);
  138.         if ( fseek(s->file, 0L, SEEK_END) ) return ERRC;
  139.         end = ftell(s->file);
  140.         if ( fseek(s->file, pos, SEEK_SET) ) return ERRC;
  141.         *pl += end - pos;
  142.         if ( *pl == 0 ) *pl = -1;    /* EOF */
  143.        }
  144.     else
  145.        {    if ( *pl == 0 && feof(s->file) ) *pl = -1;    /* EOF */
  146.        }
  147.     return 0;
  148. }
  149. private int
  150. s_file_read_seek(register stream *s, long pos)
  151. {    uint end = s->srlimit - s->cbuf + 1;
  152.     long offset = pos - s->position;
  153.     if ( offset >= 0 && offset <= end )
  154.        {    /* Staying within the same buffer */
  155.         s->srptr = s->cbuf + offset - 1;
  156.         return 0;
  157.        }
  158.     if ( fseek(s->file, pos, SEEK_SET) != 0 )
  159.         return ERRC;
  160.     s->srptr = s->srlimit = s->cbuf - 1;
  161.     s->end_status = 0;
  162.     s->position = pos;
  163.     return 0;
  164. }
  165. private int
  166. s_file_read_close(stream *s)
  167. {    return fclose(s->file);
  168. }
  169.  
  170. /* Initialize a stream for writing an OS file. */
  171. void
  172. swrite_file(register stream *s, FILE *file, byte *buf, uint len)
  173. {    static const stream_procs p =
  174.        {    s_std_noavailable, s_file_write_seek, s_std_write_reset,
  175.         s_file_write_flush, s_file_write_close, s_file_write_process
  176.        };
  177.     s_std_init(s, buf, len, &p,
  178.            (file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
  179.     if_debug1('s', "[s]write file=0x%lx\n", (ulong)file);
  180.     s->file = file;
  181.     s->state = (stream_state *)s;    /* hack to avoid separate state */
  182. }
  183. /* Initialize for appending to an OS file. */
  184. void
  185. sappend_file(register stream *s, FILE *file, byte *buf, uint len)
  186. {    swrite_file(s, file, buf, len);
  187.     s->modes = s_mode_write + s_mode_append;    /* no seek */
  188.     fseek(file, 0L, SEEK_END);
  189.     s->position = ftell(file);
  190. }
  191. /* Procedures for writing on a file */
  192. private int
  193. s_file_write_seek(stream *s, long pos)
  194. {    /* We must flush the buffer to reposition. */
  195.     int code = sflush(s);
  196.     if ( code < 0 )
  197.         return code;
  198.     if ( fseek(s->file, pos, SEEK_SET) != 0 )
  199.         return ERRC;
  200.     s->position = pos;
  201.     return 0;
  202. }
  203. private int
  204. s_file_write_flush(register stream *s)
  205. {    int result = s_process_write_buf(s, false);
  206.     fflush(s->file);
  207.     return result;
  208. }
  209. private int
  210. s_file_write_close(register stream *s)
  211. {    s_process_write_buf(s, true);
  212.     return fclose(s->file);
  213. }
  214.  
  215. #define ss ((stream *)st)
  216.  
  217. /* Process a buffer for a file reading stream. */
  218. /* This is the first stream in the pipeline, so pr is irrelevant. */
  219. private int
  220. s_file_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  221.   stream_cursor_write *pw, bool last)
  222. {    FILE *file = ss->file;
  223.     int count = fread(pw->ptr + 1, 1, (uint)(pw->limit - pw->ptr), file);
  224.     if ( count < 0 )
  225.         count = 0;
  226.     pw->ptr += count;
  227.     (void)gp_check_interrupts();
  228.     return (ferror(file) ? ERRC : feof(file) ? EOFC : 1);
  229. }
  230.  
  231. /* Process a buffer for a file writing stream. */
  232. /* This is the last stream in the pipeline, so pw is irrelevant. */
  233. private int
  234. s_file_write_process(stream_state *st, stream_cursor_read *pr,
  235.   stream_cursor_write *ignore_pw, bool last)
  236. {    /* The DEC C library on AXP architectures gives an error on */
  237.     /* fwrite if the count is zero! */
  238.     uint count = pr->limit - pr->ptr;
  239.     if ( count != 0 )
  240.       {    FILE *file = ss->file;
  241.         int written = fwrite(pr->ptr + 1, 1, count, file);
  242.         if ( written < 0 )
  243.           written = 0;
  244.         pr->ptr += written;
  245.         (void)gp_check_interrupts();
  246.         return (ferror(file) ? ERRC : 0);
  247.       }
  248.     else
  249.       {    (void)gp_check_interrupts();
  250.         return 0;
  251.       }
  252. }
  253.  
  254. #undef ss
  255.