home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 3.3 KB | 169 lines | [TEXT/MPS ] |
- /*
- File: RAMFile.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __RAMFILE__
- #include "RamFile.h"
- #endif
-
- #ifndef __NEWDELETE__
- #include "NewDelete.h"
- #endif
-
- #ifndef __DEBUGGINGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #pragma segment RamFile
-
- /***********************************|****************************************/
-
- TRamFile::TRamFile ( unsigned long initialLength, unsigned long growthRate ):
- TAbstractFile (),
- fHandle ( ::AllocateHandle ( 0 ) ),
- fPosition ( 0 ),
- fLogicalLength ( 0 ),
- fPhysicalLength ( 0 ),
- fGrowthRate ( growthRate )
- {
- SetEnd ( initialLength );
- }
-
- /***********************************|****************************************/
-
- TRamFile::~TRamFile ()
- {
- ::DeallocateHandle ( fHandle );
- }
-
- /***********************************|****************************************/
-
- OSErr
- TRamFile::ReadData ( void* buffer, long& bytesRequested )
- {
- OSErr error = noErr;
-
- const unsigned long bytesAvailable = fLogicalLength - fPosition;
-
- if ( bytesRequested > bytesAvailable )
- bytesRequested = bytesAvailable, error = eofErr;
-
- ::BlockMove ( *fHandle + fPosition, buffer, bytesRequested );
- fPosition += bytesRequested;
-
- return error;
- }
-
- /***********************************|****************************************/
-
- OSErr
- TRamFile::WriteData ( const void* buffer, long& bytesRequested )
- {
- OSErr error = noErr;
-
- const unsigned long bytesAvailable = fLogicalLength - fPosition;
-
- if ( bytesRequested > bytesAvailable )
- error = SetEnd ( fLogicalLength + bytesRequested - bytesAvailable );
-
- if ( error == noErr )
- {
- ::BlockMove ( buffer, *fHandle + fPosition, bytesRequested );
- fPosition += bytesRequested;
- }
-
- return error;
- }
-
- /***********************************|****************************************/
-
- OSErr
- TRamFile::SetEnd ( long length )
- {
- OSErr error = noErr;
-
- if ( length < 0 )
- {
- error = posErr;
- }
- else if ( length > fPhysicalLength )
- {
- ::SetHandleSize ( fHandle, length + fGrowthRate );
- error = MemError ();
-
- if ( error == noErr )
- {
- fPhysicalLength = length + fGrowthRate;
- fLogicalLength = length;
- }
- }
- else if ( length > fLogicalLength )
- {
- fLogicalLength = length;
- }
- else
- {
- ::SetHandleSize ( fHandle, fLogicalLength = fPhysicalLength = length );
- if ( fPosition > fLogicalLength ) fPosition = fLogicalLength;
- }
-
- return error;
- }
-
- /***********************************|****************************************/
-
- OSErr
- TRamFile::SetPosition ( short mode, long offset )
- {
- if ( mode == fsFromMark )
- offset += fPosition;
- else if ( mode == fsFromLEOF )
- offset += fLogicalLength;
-
- if ( offset < 0 )
- {
- return posErr;
- }
- else if ( offset <= fLogicalLength )
- {
- fPosition = offset;
- return noErr;
- }
- else
- return eofErr;
- }
-
- /***********************************|****************************************/
-
- OSErr
- TRamFile::GetEnd ( long& length ) const
- {
- length = (long) fLogicalLength;
- return noErr;
- }
-
- /***********************************|****************************************/
-
- OSErr
- TRamFile::GetPosition ( long& position ) const
- {
- position = (long) fPosition;
- return noErr;
- }
-
- /***********************************|****************************************/
-
-