home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 6.8 KB | 202 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWAccBuf.h
- // Release Version: $ 1.0d11 $
- //
- // Copyright: 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWACCBUF_H
- #define FWACCBUF_H
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #ifndef FWAUTODE_H
- #include "FWAutoDe.h"
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- //========================================================================================
- // CLASS FW_CPrivFileAccessBuffer
- //
- // The FW_CPrivFileAccessBuffer is intended to be used with the FW_CFileSink class to
- // provide buffered output of the data. This class is an implementation class and
- // should not be used as a stand-alone class.
- //
- // The way the buffer works, is that it maintains a window over the file stream. That
- // window is the RAM-based buffer which mirrors the data on the disk. Buffer methods
- // that read and write data, do so within that window. When the currentPosition moves
- // beyond the window's bounds, ReadPeeks and WritePeeks will return zero bytes read.
- // When FW_CFileSink detects this it will re-initialize the data in the buffer to
- // the new data window based on the currentPosition.
- //
- // ReadPeek and WritePeek do not actually read or write the data to the disk. The
- // FW_CFileSink is responsible for getting a pointer to the buffer and filling it
- // with data. The AccessBuffer is just responsible for caching the information and
- // maintaining the pointers to the data.
- //========================================================================================
-
- class FW_CLASS_ATTR FW_CPrivFileAccessBuffer FW_AUTO_DESTRUCT_OBJECT
- {
- public:
-
- enum
- {
- kInvalid, kReadPeek, kWritePeek
- };
-
-
- FW_CPrivFileAccessBuffer(long capacity);
- // Creates an instance with the specified capacity. The memory
- // for the buffer is not allocated until Initialize is called.
- // The default buffer type is kInvalid.
-
- FW_CPrivFileAccessBuffer(const FW_CPrivFileAccessBuffer& otherBuffer);
- // Copy constructor. Initializes this buffer to be the same size
- // as otherBuffer. It does not copy the data from otherBuffer.
-
- ~ FW_CPrivFileAccessBuffer();
-
- void Initialize(int type,
- long filePosition,
- long validBytes);
- // Allocates memory for the buffer and initializes the type and validity
- // of the buffer.
-
- FW_CPrivFileAccessBuffer& operator=(const FW_CPrivFileAccessBuffer& otherBuffer);
- // Assignment operator. Changes the size of this buffer only. Does not
- // copy the data from the other buffer.
-
-
- //===========================================================
- // Read/Write
- //===========================================================
-
- void Invalidate();
- // Re-initializes the buffer to contain invalid data. The buffer must
- // be re-initialized before the next ReadPeek or WritePeek.
-
- void* ReadPeek(long currentPosition,
- long& availableReadBytes);
- // Returns a pointer to the data in the buffer starting at currentPosition.
- // availableReadBytes returns the number of valid bytes in the buffer
- // returned.
-
- void ReadPeekAdvance(long currentPosition,
- long bytesRead);
- // Advances the ReadPeek buffer to indicate that the user has read the
- // number of bytes specified by bytesRead.
- // This routine should be called after the user uses the data from the buffer.
- // Otherwise, the same bytes will be returned by the next ReadPeek.
-
- void* WritePeek(long currentPosition,
- long& availableWriteBytes);
- // Returns a pointer to the buffer starting at currentPosition. The
- // number of bytes available for writing is returned in availableWriteBytes.
-
- void WritePeekAdvance(long currentPosition,
- long bytesWritten);
- // Advances the WritePeek buffer to indicate that the user has written the
- // number of bytes specified by bytesWritten starting at currentPosition.
- // This routine should be alled after the user writes the data. Otherwise,
- // the data could be overwritten by future writes.
-
-
- //===========================================================
- // Accessor methods
- //===========================================================
-
- long GetCapacity() const;
- // Returns the capacity of the data buffer.
-
- void* GetAddress() const;
- // Returns a pointer to the entire buffer.
-
- long GetInitialPosition() const;
- // Returns the position in the file where the buffer starts.
-
- long GetBytesWritten() const;
- // Returns the number of bytes that have been written into the buffer.
- // Used only for Write buffers.
-
- FW_Boolean IsDirty() const;
- // Used for Write buffers only. Returns TRUE if there is data in the buffer
- // that needs to be written.
-
- private:
-
- int fType;
- char* fBuffer;
- long fCapacity;
- long fInitialPosition;
- long fValidBytes;
- long fBytesWritten;
- };
-
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivFileAccessBuffer::Invalidate
- //----------------------------------------------------------------------------------------
-
- inline void FW_CPrivFileAccessBuffer::Invalidate()
- {
- Initialize(kInvalid, 0, 0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivFileAccessBuffer::GetCapacity
- //----------------------------------------------------------------------------------------
-
- inline long FW_CPrivFileAccessBuffer::GetCapacity() const
- {
- return fCapacity;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivFileAccessBuffer::GetAddress
- //----------------------------------------------------------------------------------------
-
- inline void* FW_CPrivFileAccessBuffer::GetAddress() const
- {
- return fBuffer;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivFileAccessBuffer::GetInitialPosition
- //----------------------------------------------------------------------------------------
-
- inline long FW_CPrivFileAccessBuffer::GetInitialPosition() const
- {
- return fInitialPosition;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivFileAccessBuffer::GetBytesWritten
- //----------------------------------------------------------------------------------------
-
- inline long FW_CPrivFileAccessBuffer::GetBytesWritten() const
- {
- return fBytesWritten;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivFileAccessBuffer::IsDirty
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean FW_CPrivFileAccessBuffer::IsDirty() const
- {
- return (fType == kWritePeek) && (fBytesWritten != 0);
- }
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export off
- #endif
-
- #endif
-