home *** CD-ROM | disk | FTP | other *** search
- /* =========================
- * PedStreamInputBuffered.cc
- * =========================
- */
-
- #include <string.h>
-
- #include "PedStreamInputBuffered.hh"
- #include "PedBuffer.hh"
- #include "PedDataSource.hh"
-
- PedStreamInputBuffered::PedStreamInputBuffered(PedDataSource &inDataSource)
- : mDataSource(inDataSource), mCurrentBuffer(NULL), mMark(0), mCanUnget(false)
- {
- mDataSource.retain();
- }
-
- PedStreamInputBuffered::~PedStreamInputBuffered()
- {
- mDataSource.release();
- }
-
- short
- PedStreamInputBuffered::Flush()
- {
- mCanUnget = false;
- if (!mCurrentBuffer || mMark == mCurrentBuffer->Length()) {
- delete mCurrentBuffer; // Works for NULL value
- mCurrentBuffer = NextBuffer();
- mMark = 0;
- return (mEOF = mCurrentBuffer == NULL) ? -1 : 0;
- } else {
- return mCurrentBuffer->Length() - mMark;
- }
- }
-
- short
- PedStreamInputBuffered::GetByte()
- {
- return GetRawByte();
- }
-
- short
- PedStreamInputBuffered::GetRawByte()
- {
- if (!mCurrentBuffer || mMark == mCurrentBuffer->Length()) {
- delete mCurrentBuffer; // Works for NULL value
- mCurrentBuffer = NextBuffer();
- if (!mCurrentBuffer) {
- mEOF = true;
- return -1;
- }
- mMark = 0;
- }
- char *ptr = mCurrentBuffer->Ptr();
- char c = ptr[mMark++];
- mCanUnget = true;
- return c;
- }
-
- short
- PedStreamInputBuffered::Unget()
- {
- if (!mCanUnget || !mCurrentBuffer) {
- return -1;
- }
- mMark--;
- mCanUnget = false;
- return 0;
- }
-
- long
- PedStreamInputBuffered::GetChunk(long inCount, char *inBuf)
- {
- long len;
- if (!mCurrentBuffer || mMark == (len = mCurrentBuffer->Length())) {
- return 0;
- }
- if (inCount > len - mMark) {
- inCount = len - mMark;
- }
- char *ptr = mCurrentBuffer->Ptr();
- memcpy(inBuf, &ptr[mMark], inCount);
- mMark += inCount;
- Flush();
- return inCount;
- }
-
- long
- PedStreamInputBuffered::SizeOfNextChunk()
- {
- if (!mCurrentBuffer) {
- return 0;
- }
- return mCurrentBuffer->Length() - mMark;
- }
-
- long
- PedStreamInputBuffered::CountAheadThroughChar(char inChar)
- {
- if (mEOF) {
- return -1;
- } else if (!mCurrentBuffer) {
- return 0;
- }
- long len = mCurrentBuffer->Length();
-
- char *p = mCurrentBuffer->Ptr();
- char *end = p + len;
- char *start = p += mMark;
- while (p < end && *p != inChar) {
- p++;
- }
- return p == end ? 0 : p + 1 - start;
- }
-
- PedBuffer *
- PedStreamInputBuffered::NextBuffer()
- {
- PedBuffer *buf = mBufferChain.Behead();
- if (!buf) {
- ReadMore();
- buf = mBufferChain.Behead();
- }
- return buf;
- }
-
- void
- PedStreamInputBuffered::ReadMore()
- {
- // char str[] = "Put something in a buffer\r";
- // PedBuffer *buf = new PedBuffer(strlen(str), str);
- // mBufferChain.Append(buf);
- PedBuffer *buf = mDataSource.GetNextBuffer();
- if (buf) {
- mBufferChain.Append(buf);
- }
- }
-