|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T s
Length: 5770 (0x168a) Types: TextFile Names: »stream.c«
└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89 └─⟦ff23ba0e6⟧ »./ghostscript-1.3.tar.Z« └─⟦a24a58cd3⟧ └─⟦this⟧ »stream.c«
/* Copyright (C) 1989 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 copies. */ /* stream.c */ /* Stream package for GhostScript interpreter */ #include <stdio.h> #include "std.h" #include "stream.h" /* Forward declarations */ private int /* Strings */ ssread(P1(stream *)), ssseek(P2(stream *, long)), sswrite(P2(byte, stream *)), /* File reading */ sfread(P1(stream *)), srseek(P2(stream *, long)), srflush(P1(stream *)), srclose(P1(stream *)), /* File writing */ sfwrite(P2(byte, stream *)), swseek(P2(stream *, long)), swflush(P1(stream *)), swclose(P1(stream *)); /* ------ String streams ------ */ /* Initialize a stream for reading a string. */ void sread_string(register stream *s, byte *ptr, uint len) { s->buf = s->ptr = ptr; s->cnt = s->bsize = len; s->eof = 1; /* this is all there is */ s->writing = 0; s->position = 0; s->rproc = ssread; s->sproc = ssseek; } /* Handle end-of-buffer when reading from a string */ private int ssread(stream *s) { s->cnt = 0; return EOFC; } /* Initialize a stream for writing a string. */ void swrite_string(register stream *s, byte *ptr, uint len) { s->buf = s->ptr = ptr; s->cnt = s->bsize = len; s->eof = 1; s->position = 0; s->wproc = sswrite; s->sproc = ssseek; } /* Handle end-of-buffer when writing a string */ private int sswrite(byte c, stream *s) { s->cnt = 0; return EOFC; } /* Seek in a string. Return 0 if OK, -1 if not. */ private int ssseek(register stream *s, long pos) { if ( pos < 0 || pos > s->bsize ) return -1; s->ptr = s->buf + pos; s->cnt = s->bsize - pos; return 0; } /* ------ File streams ------ */ /* Initialize a stream for reading an OS file. */ void sread_file(register stream *s, FILE *file, byte *buf, uint len) { s->ptr = s->buf = buf; s->bsize = len; s->file = file; s->cnt = 0; s->eof = 0; s->writing = 0; s->position = 0; s->rproc = sfread; s->sproc = srseek; s->flproc = srflush; s->clproc = srclose; } /* Procedures for reading from a file */ private int sfread(register stream *s) { int nread; if ( s->eof ) { s->cnt = 0; return EOFC; } s->position = ftell(s->file); nread = fread(s->buf, 1, s->bsize, s->file); s->ptr = s->buf; s->eof = feof(s->file); if ( nread > 0 ) { s->cnt = nread - 1; return (int)*(s->ptr++); /* don't understand why the cast is needed.... */ } else if ( nread == 0 ) { s->cnt = 0; s->eof = 1; return EOFC; } else /* error, now what?? */ { s->cnt = 0; return 0; } } private int srseek(register stream *s, long pos) { uint end = s->ptr - s->buf + s->cnt; long offset = pos - s->position; if ( offset >= 0 && offset <= end ) { /* Staying within the same buffer */ s->ptr = s->buf + offset; s->cnt = end - offset; return 0; } if ( fseek(s->file, pos, 0) != 0 ) return -1; s->ptr = s->buf; s->cnt = 0; s->eof = 0; return 0; } private int srflush(stream *s) { return 0; /* no-op */ } private int srclose(stream *s) { return fclose(s->file); } /* Initialize a stream for writing an OS file. */ void swrite_file(register stream *s, FILE *file, byte *buf, uint len) { s->ptr = s->buf = buf; s->bsize = len; s->file = file; s->cnt = 0; s->eof = 0; s->writing = 1; s->position = 0; s->wproc = sfwrite; s->sproc = swseek; s->flproc = swflush; s->clproc = swclose; } /* Procedures for writing on a file */ private int sfwrite(byte c, register stream *s) { fwrite(s->buf, 1, (uint)(s->ptr - s->buf), s->file); s->position = ftell(s->file); s->ptr = s->buf; s->cnt = s->bsize; return sputc(c, s); } private int swseek(stream *s, long pos) { /* Output files are not positionable */ return -1; } private int swflush(register stream *s) { int result = fwrite(s->buf, 1, (uint)(s->ptr - s->buf), s->file); fflush(s->file); s->position = ftell(s->file); s->ptr = s->buf; s->cnt = s->bsize; return result; } private int swclose(register stream *s) { fwrite(s->buf, 1, (uint)(s->ptr - s->buf), s->file); return fclose(s->file); } /* ------ Generic procedures ------ */ /* Read a string from a stream. */ /* Return the number of bytes read. */ uint sgets(byte *str, uint rlen, register stream *s) { uint len = rlen; while ( len > 0 ) { if ( s->cnt > 0 ) { uint count = min(len, s->cnt); memcpy(str, s->ptr, count); s->ptr += count; s->cnt -= count; len -= count; } else { int ch = sgetc(s); if ( s->eof ) return rlen - len; *str++ = ch; len--; } } return rlen; } /* Write a string on a stream. */ /* Return the number of bytes written. */ uint sputs(byte *str, uint wlen, register stream *s) { uint len = wlen; while ( len > 0 ) { if ( s->cnt > 0 ) { uint count = min(len, s->cnt); memcpy(s->ptr, str, count); s->ptr += count; str += count; s->cnt -= count; len -= count; } else { byte ch = *str++; sputc(ch, s); if ( s->eof ) return wlen - len; len--; } } return wlen; }