home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11.lha / ccs-lib / lib / piperead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-15  |  4.2 KB  |  181 lines

  1. /*    PipeRead . C
  2. #
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  6. Permission is granted to reproduce this software for non-commercial
  7. purposes provided that this notice is left intact.
  8.  
  9. It is acknowledged that the U.S. Government has rights to this software
  10. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  11. and the University of California.
  12.  
  13. This software is provided as a professional and academic contribution
  14. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  15. with no warranties of any kind whatsoever, no support, no promise of
  16. updates, or printed documentation. By using this software, you
  17. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  18. University of California shall have no liability with respect to the
  19. infringement of other copyrights by any part of this software.
  20.  
  21. For further information about this notice, contact William Johnston,
  22. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  23. (wejohnston@lbl.gov)
  24.  
  25. For further information about this software, contact:
  26.     Jin Guojun
  27.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  28.     g_jin@lbl.gov
  29.  
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. %
  32. %    Reading procedure for piping operation
  33. %
  34. % Author:    Jin Guojun - LBL    01/01/1993
  35. */
  36.  
  37. #include    "imagedef.h"
  38. #if defined    TC_Need || defined    __STDIO_DEF_
  39. #define    _ptr    curp
  40. #define    _cnt    level
  41. #endif
  42.  
  43. #ifndef    MAX_FILES
  44. #define    MAX_FILES    12
  45. #endif
  46. #ifndef    OVHEAD
  47. #define    OVHEAD    16
  48. #endif
  49.  
  50. typedef    struct    {
  51.     MType    bleft, bsize, cur, pos;
  52.     short    set, flag;    /* be usable & for what job */
  53.     byte    *inpbuf;
  54. } fpfd;
  55.  
  56. static    fpfd    ffd[MAX_FILES];
  57. #define    FPT    register fpfd*    ffp = ffd + fileno((FILE*)fp)
  58. static    byte    garbage[OVHEAD];
  59. static    bool    bad_pipe =
  60. #if defined NASI | defined    TC_Need
  61.     False;
  62. #else
  63.     True;
  64. #endif
  65.  
  66. readpipe(buf, block, nblock, fp)
  67. byte    *buf;
  68. FILE    *fp;
  69. {
  70. register int    t = block * nblock;
  71. FPT;
  72. register byte*    temp = ffp->inpbuf;
  73. ffp->inpbuf = buf;    /* use new buf    */
  74.  
  75. if (t < 0)    {    /* block < 0, push data back to pseudo-pipe    */
  76.     ffp->bleft = nblock;
  77.     return    ffp->cur = 0;
  78. }
  79. if (!t && !block)    {    /* block == 0, setup buf_size    */
  80.     t = nblock;    block = 1;
  81.     ffp->bsize = pointer_buffer_size(buf);
  82. }
  83. if (!ffp->bleft)
  84.     return    fread(buf, block, nblock, fp);
  85. else    {
  86. register int    ret;
  87.     if (ffp->cur == ffp->bsize)    ffp->cur = 0;
  88.     memcpy(buf, temp+ffp->cur, ffp->bleft);
  89.     t -= ffp->bleft;
  90.     ret = fread(buf+ffp->cur, 1, t, fp);
  91.     ffp->bleft = ffp->cur = 0;
  92.     if (t == ret)    return    nblock;
  93.     else    return    ret / block + 1;
  94. }
  95. }
  96.  
  97. getbyte(fp)
  98. FILE    *fp;
  99. {
  100. FPT;
  101.  
  102. if (ffp->bleft)    {
  103.     if (ffp->cur == ffp->bsize)    ffp->cur = 0;
  104.     ffp->bleft--;    return    ffp->inpbuf[ffp->cur++];
  105. } else    {
  106.     readpipe(ffp->inpbuf+ffp->cur, 1, 1, fp);
  107.     return    ffp->inpbuf[ffp->cur++];
  108. }
  109. }
  110.  
  111. ungetbyte(b, fp)    /* compatible with ungetc    */
  112. FILE    *fp;
  113. {
  114. FPT;
  115.  
  116. if (!ffp->cur)    ffp->cur = ffp->bsize;
  117. ffp->bleft++;    ffp->cur--;
  118. return    ffp->inpbuf[ffp->cur] = b;
  119. }
  120.  
  121. /*    compatible with fseek when job in 0..3.
  122.     If job < 0, it peek a byte from inpbuf.
  123. */
  124.  
  125. peekbyte(fp, n, job)
  126. FILE    *fp;
  127. {
  128. FPT;
  129. if (job < 0)    {
  130. bool    peeked = ffp->flag == SEEK_PEEK;
  131. ffp->flag = job;
  132.     switch (job)    {
  133.     case SEEK_GETB:
  134. pcross:        return    ffp->set ? getbyte(fp) : fgetc(fp);
  135.     case SEEK_UGETB:
  136.         if (peeked)    return    0;
  137.         return    ffp->set ? ungetbyte(n, fp) : ungetc(n, fp);
  138.     case SEEK_PEEK:
  139.     default:if (!ffp->set)    {
  140.             if (fp->_cnt>0)    return    *fp->_ptr;
  141.             ffp->flag = 0;    goto    pcross;
  142.         }
  143.         if (!ffp->bleft)    {
  144.             readpipe(ffp->inpbuf+ffp->cur, 1, 1, fp);
  145.             ffp->bleft++;
  146.         }
  147.         return    ffp->inpbuf[ffp->cur];
  148.     }
  149. }
  150. if (ffp->set && job == SEEK_CUR && n>0)    {
  151.     while(n--)    getbyte(fp);
  152.     return    n;
  153. }
  154. return    fseek(fp, n, job);
  155. }
  156.  
  157. set_readpipe(img, job)
  158. U_IMAGE    *img;
  159. {
  160. if (fileno(img->IN_FP) < MAX_FILES)    {
  161. register fpfd*    ffp = ffd + fileno(img->IN_FP);
  162.  
  163. img->close = (StdInterface*)fclose;
  164. img->open = (StdInterface*)fopen;
  165. img->seek = peekbyte;
  166.  
  167.     if (job & bad_pipe)    {    /*    initial for bad pipe    */
  168.     img->read = readpipe;
  169.     ffp->cur = ffp->bleft = ffp->flag = ffp->pos = 0;
  170.     ffp->inpbuf = garbage;
  171.     ffp->bsize = OVHEAD;
  172.     return    ++ffp->set;
  173.     }    else    {    /*    reset    */
  174.     img->read = (StdInterface*)fread;
  175.     img->write = (StdInterface*)fwrite;
  176.     return    ffp->set = 0;
  177.     }
  178. }
  179. return    EOF;
  180. }
  181.