home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / lynxlib / memfile.c < prev    next >
C/C++ Source or Header  |  1993-10-23  |  2KB  |  80 lines

  1. /* This source file is part of the LynxLib miscellaneous library by
  2. Robert Fischer, and is Copyright 1990 by Robert Fischer.  It costs no
  3. money, and you may not make money off of it, but you may redistribute
  4. it.  It comes with ABSOLUTELY NO WARRANTY.  See the file LYNXLIB.DOC
  5. for more details.
  6. To contact the author:
  7.     Robert Fischer \\80 Killdeer Rd \\Hamden, CT   06517   USA
  8.     (203) 288-9599     fischer-robert@cs.yale.edu                 */
  9.  
  10. #include <stdio.h>
  11. #include <vfile.h>
  12.  
  13. extern free();
  14.  
  15. typedef struct {    /* A file in memory */
  16.     char *base;     /* Base of the file */
  17.     char *end;      /* Last char in file +1 */
  18.     char *next;     /* Next character to be read or written */
  19.     BOOLEAN eof;    /* Has end of file been reached? */
  20. } MFILE;
  21.  
  22. /* Memory file driver for VFILE */
  23. /* -------------------------------------------------- */
  24. int mem_getc(m)     /* getc */
  25. MFILE *m;
  26. {
  27.     if (m->next == m->end) {
  28.         m->eof = TRUE;
  29.         return EOF;
  30.     }
  31.     return *(m->next++);
  32. }
  33.  
  34. int mem_eof(m)      /* feof */
  35. MFILE *m;
  36. {
  37.     return m->eof;
  38. }
  39.  
  40. mem_putc(c, m)      /* putc */
  41. char c;
  42. MFILE *m;
  43. {
  44.     if (m->next == m->end) {
  45.         m->eof = TRUE;
  46.         return;
  47.     }
  48.     *(m->next++) = c;
  49. }
  50.  
  51. long mem_curpos(m)      /* Figures out position of file pointer */
  52. MFILE *m;
  53. {
  54.     return (long)(m->next - m->base);
  55. }
  56. /* -------------------------------------------------------- */
  57.  
  58. VFILE *open_mfile(base, len)
  59. /* This opens an MFILE and returns a VFILE * */
  60. char *base;     /* Start of MFILE */
  61. long len;           /* Length of MFILE */
  62. {
  63. VFILE *v;
  64. MFILE *m;
  65.     v = malloc(sizeof(*v));
  66.     v->curpos = &mem_curpos;
  67.     v->do_eof = &mem_eof; 
  68.     v->do_getc = &mem_getc;
  69.     v->do_putc = &mem_putc;
  70.     v->do_close = &free;
  71.     m = v->p = malloc(sizeof(MFILE));
  72.  
  73.     m->base = base;
  74.     m->next = base;
  75.     m->end = base + len;
  76.     m->eof = FALSE;
  77.     return v;
  78. }
  79. /* -------------------------------------------------------- */
  80.