home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / lynxlib / cofile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  3.7 KB  |  163 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. /* By Robert Fischer, August 1988 */
  11. /*
  12. This is another file driver for VFILE It reads and writes via the
  13. rendezvous coroutine function.  The lower 16 bits of the message it
  14. sends is the character it is sending.  Hopefully, when co_getc is
  15. called, the lower 16 bits of the thing it's getting is the character. */
  16.  
  17. #include <co.h>
  18. #include <vfile.h>
  19. #include <stdio.h>
  20. /* -------------------------------------------------- */
  21. typedef struct  {    /* A slave's end of the the cofile */
  22.     long    size;
  23.     BOOLEAN    eof;
  24. } SLAVE_FILE;
  25. typedef struct {
  26.     pcb        f;        /* The function which the cofile is made of */
  27.     long    size;    /* Current size of file (amount read or written) */
  28.     BOOLEAN    eof;    /* TRUE if end of file has been reached */
  29.     SLAVE_FILE *slave;
  30. } COFILE;
  31.  
  32. #define ASK_EOF 1
  33. #define ASK_GETC 2
  34.  
  35. /* -------------------------------------------------- */
  36. /* Master's cofile driver */
  37.  
  38. WORD co_getc(f)
  39. COFILE *f;
  40. {
  41. LONG c;    /* Character being gotten */
  42.     if (f->eof) return EOF;
  43.     if (resume_co(&f->f, NULL, &c)) {    /* A character was found */
  44.         f->size++;
  45.         return (WORD)c;
  46.     }
  47.  
  48.     /* A character was NOT found -- set eof */
  49.     f->eof = TRUE;
  50.     return EOF;
  51. }
  52.  
  53. co_eof(f)        /* Looks @ end of file */
  54. COFILE *f;
  55. {
  56.     return f->eof;
  57. }
  58.  
  59. co_putc(c, f)        /* Puts a character */
  60. int c;            /* Character to put */
  61. COFILE *f;
  62. {
  63. LONG dum;
  64.     if (f->eof) return;        /* The slave has quit already, something which shouldn't happen */
  65.     if (!resume_co(&f->f, (LONG)c, &dum))    /* slave quit */
  66.         f->eof = TRUE;
  67. }
  68.  
  69. co_close(f)
  70. COFILE *f;
  71. {
  72.     kill_co(&f->f);
  73.     free(f->slave);
  74.     free(f);
  75. }
  76.  
  77. long co_curpos(f)
  78. COFILE *f;
  79. {
  80.     return f->size;
  81. }
  82.  
  83. /* -------------------------------------------------- */
  84. /* Slave's cofile driver */
  85.  
  86. WORD slave_getc(f)
  87. SLAVE_FILE *f;
  88. {
  89.     if (!f->eof) {
  90.         f->size++;
  91.         return (WORD)to_master(NULL);
  92.     }
  93. }
  94.  
  95. BOOLEAN slave_eof(f)
  96. SLAVE_FILE *f;
  97. {
  98.     return f->eof;
  99. }
  100.  
  101. WORD slave_putc(c, f)
  102. int c;
  103. SLAVE_FILE *f;
  104. {
  105.     if (!f->eof) {
  106.         f->size++;
  107.         to_master((LONG)c);
  108.     }
  109. }
  110.  
  111. slave_close(f)
  112. SLAVE_FILE *f;
  113. {
  114.     f->eof = TRUE;
  115. }
  116.  
  117. long slave_curpos(f)
  118. SLAVE_FILE *f;
  119. {
  120.     return f->size;
  121. }
  122. /* -------------------------------------------------- */
  123. VFILE *open_cofile(f, x, stacksize, slave_vfile)
  124. func *f;        /* Function to make a cofile of */
  125. LONG x;            /* Argument to f */
  126. long stacksize;    /* Size to make cofile's stack */
  127. VFILE **slave_vfile;        /* Slave's cofile handle */
  128. {
  129. VFILE *v;    /* Master's cofile */
  130. VFILE *s;    /* Slave's cofile */
  131. COFILE *c;
  132. SLAVE_FILE *d;
  133.  
  134.     /* Init the master's cofile */
  135.     v = malloc(sizeof(*v));
  136.     v->curpos = &co_curpos;
  137.     v->do_eof = &co_eof; 
  138.     v->do_getc = &co_getc;
  139.     v->do_putc = &co_putc;
  140.     v->do_close = &co_close;
  141.     c = v->p = malloc(sizeof(COFILE));
  142.     c->size = 0;
  143.     c->eof = FALSE;
  144.     init_co(f, x, stacksize, &(c->f));
  145.  
  146.     /* Init the slave's cofile */
  147.     c->slave = s = malloc(sizeof(*s));
  148.     s->curpos = &slave_curpos;
  149.     s->do_eof = &slave_eof; 
  150.     s->do_getc = &slave_getc;
  151.     s->do_putc = &slave_putc;
  152.     s->do_close = &slave_close;
  153.     d = s->p = malloc(sizeof(*d));
  154.     d->size = 0;
  155.     d->eof = FALSE;
  156.  
  157.     /* Return cofiles */
  158.     *slave_vfile = s;
  159.     return v;
  160. }
  161. /* -------------------------------------------------- */
  162. /* -------------------------------------------------- */
  163.