home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Streams / PedStreamInputBuffered.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  2.5 KB  |  139 lines

  1. /*    =========================
  2.  *    PedStreamInputBuffered.cc
  3.  *    =========================
  4.  */
  5.  
  6. #include <string.h>
  7.  
  8. #include "PedStreamInputBuffered.hh"
  9. #include "PedBuffer.hh"
  10. #include "PedDataSource.hh"
  11.  
  12. PedStreamInputBuffered::PedStreamInputBuffered(PedDataSource &inDataSource)
  13. : mDataSource(inDataSource), mCurrentBuffer(NULL), mMark(0), mCanUnget(false)
  14. {
  15.     mDataSource.retain();
  16. }
  17.  
  18. PedStreamInputBuffered::~PedStreamInputBuffered()
  19. {
  20.     mDataSource.release();
  21. }
  22.  
  23. short
  24. PedStreamInputBuffered::Flush()
  25. {
  26.     mCanUnget = false;
  27.     if (!mCurrentBuffer || mMark == mCurrentBuffer->Length()) {
  28.         delete mCurrentBuffer;  // Works for NULL value
  29.         mCurrentBuffer = NextBuffer();
  30.         mMark = 0;
  31.         return (mEOF = mCurrentBuffer == NULL) ? -1 : 0;
  32.     } else {
  33.         return mCurrentBuffer->Length() - mMark;
  34.     }
  35. }
  36.  
  37. short
  38. PedStreamInputBuffered::GetByte()
  39. {
  40.     return GetRawByte();
  41. }
  42.  
  43. short
  44. PedStreamInputBuffered::GetRawByte()
  45. {
  46.     if (!mCurrentBuffer || mMark == mCurrentBuffer->Length()) {
  47.         delete mCurrentBuffer;  // Works for NULL value
  48.         mCurrentBuffer = NextBuffer();
  49.         if (!mCurrentBuffer) {
  50.             mEOF = true;
  51.             return -1;
  52.         }
  53.         mMark = 0;
  54.     }
  55.     char *ptr = mCurrentBuffer->Ptr();
  56.     char c = ptr[mMark++];
  57.     mCanUnget = true;
  58.     return c;
  59. }
  60.  
  61. short
  62. PedStreamInputBuffered::Unget()
  63. {
  64.     if (!mCanUnget || !mCurrentBuffer) {
  65.         return -1;
  66.     }
  67.     mMark--;
  68.     mCanUnget = false;
  69.     return 0;
  70. }
  71.  
  72. long
  73. PedStreamInputBuffered::GetChunk(long inCount, char *inBuf)
  74. {
  75.     long len;
  76.     if (!mCurrentBuffer || mMark == (len = mCurrentBuffer->Length())) {
  77.         return 0;
  78.     }
  79.     if (inCount > len - mMark) {
  80.         inCount = len - mMark;
  81.     }
  82.     char *ptr = mCurrentBuffer->Ptr();
  83.     memcpy(inBuf, &ptr[mMark], inCount);
  84.     mMark += inCount;
  85.     Flush();
  86.     return inCount;
  87. }
  88.  
  89. long
  90. PedStreamInputBuffered::SizeOfNextChunk()
  91. {
  92.     if (!mCurrentBuffer) {
  93.         return 0;
  94.     }
  95.     return mCurrentBuffer->Length() - mMark;
  96. }
  97.  
  98. long
  99. PedStreamInputBuffered::CountAheadThroughChar(char inChar)
  100. {
  101.     if (mEOF) {
  102.         return -1;
  103.     } else if (!mCurrentBuffer) {
  104.         return 0;
  105.     }
  106.     long len = mCurrentBuffer->Length();
  107.     
  108.     char *p = mCurrentBuffer->Ptr();
  109.     char *end = p + len;
  110.     char *start = p += mMark;
  111.     while (p < end && *p != inChar) {
  112.         p++;
  113.     }
  114.     return p == end ? 0 : p + 1 - start;
  115. }
  116.  
  117. PedBuffer *
  118. PedStreamInputBuffered::NextBuffer()
  119. {
  120.     PedBuffer *buf = mBufferChain.Behead();
  121.     if (!buf) {
  122.         ReadMore();
  123.         buf = mBufferChain.Behead();
  124.     }
  125.     return buf;
  126. }
  127.  
  128. void
  129. PedStreamInputBuffered::ReadMore()
  130. {
  131. //    char str[] = "Put something in a buffer\r";
  132. //    PedBuffer *buf = new PedBuffer(strlen(str), str);
  133. //    mBufferChain.Append(buf);
  134.     PedBuffer *buf = mDataSource.GetNextBuffer();
  135.     if (buf) {
  136.         mBufferChain.Append(buf);
  137.     }
  138. }
  139.