home *** CD-ROM | disk | FTP | other *** search
- /* This source file is part of the LynxLib miscellaneous library by
- Robert Fischer, and is Copyright 1990 by Robert Fischer. It costs no
- money, and you may not make money off of it, but you may redistribute
- it. It comes with ABSOLUTELY NO WARRANTY. See the file LYNXLIB.DOC
- for more details.
- To contact the author:
- Robert Fischer \\80 Killdeer Rd \\Hamden, CT 06517 USA
- (203) 288-9599 fischer-robert@cs.yale.edu */
-
- #include <stdio.h>
- #include <vfile.h>
-
- extern free();
-
- typedef struct { /* A file in memory */
- char *base; /* Base of the file */
- char *end; /* Last char in file +1 */
- char *next; /* Next character to be read or written */
- BOOLEAN eof; /* Has end of file been reached? */
- } MFILE;
-
- /* Memory file driver for VFILE */
- /* -------------------------------------------------- */
- int mem_getc(m) /* getc */
- MFILE *m;
- {
- if (m->next == m->end) {
- m->eof = TRUE;
- return EOF;
- }
- return *(m->next++);
- }
-
- int mem_eof(m) /* feof */
- MFILE *m;
- {
- return m->eof;
- }
-
- mem_putc(c, m) /* putc */
- char c;
- MFILE *m;
- {
- if (m->next == m->end) {
- m->eof = TRUE;
- return;
- }
- *(m->next++) = c;
- }
-
- long mem_curpos(m) /* Figures out position of file pointer */
- MFILE *m;
- {
- return (long)(m->next - m->base);
- }
- /* -------------------------------------------------------- */
-
- VFILE *open_mfile(base, len)
- /* This opens an MFILE and returns a VFILE * */
- char *base; /* Start of MFILE */
- long len; /* Length of MFILE */
- {
- VFILE *v;
- MFILE *m;
- v = malloc(sizeof(*v));
- v->curpos = &mem_curpos;
- v->do_eof = &mem_eof;
- v->do_getc = &mem_getc;
- v->do_putc = &mem_putc;
- v->do_close = &free;
- m = v->p = malloc(sizeof(MFILE));
-
- m->base = base;
- m->next = base;
- m->end = base + len;
- m->eof = FALSE;
- return v;
- }
- /* -------------------------------------------------------- */
-