home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 4.0 KB | 190 lines | [TEXT/MPS ] |
- /*
- File: FileInFile.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 __FILEINFILE__
- #include "FileInFile.h"
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- /***********************************|****************************************/
-
- #pragma segment FileInFile
-
- /***********************************|****************************************/
-
- extern ostream& DumpHex (ostream& s, const void *p, unsigned long size);
-
- /***********************************|****************************************/
-
- inline long min ( long a, long b )
- {
- return ( a < b) ? a : b;
- }
-
- /***********************************|****************************************/
-
- TFileInFile::TFileInFile(TVirtualFile* parentFile, long offset, long theSize):
- TVirtualFile (),
- fParentFile ( *parentFile ),
- fParentOffset ( offset ),
- fSubSize ( theSize ),
- fSubPosition ( 0 )
- {
- fParentFile.RegisterReference ();
- }
-
- /***********************************|****************************************/
-
- TFileInFile::~TFileInFile()
- {
- fParentFile.UnregisterReference ();
- }
-
- /***********************************|****************************************/
-
- OSErr TFileInFile::ReadData ( void* buffer, long& count )
- {
- TPositionPreserver preserver ( fParentFile );
- long origCount = count;
- OSErr error;
-
- if ( count + fSubPosition > fSubSize )
- count = fSubSize - fSubPosition;
-
- error = (fParentFile.SetPosition ( fsFromStart, fParentOffset + fSubPosition) );
- if ( error )
- {
- count = 0;
- return error;
- }
-
- #if debug
- if (steveFlag.Flag(18)) {
- keith << "TFileInFile::ReadData(" << count << " [" << origCount << "]) pos=" << fSubPosition << " from parent." << endl;
- }
- #endif
-
- error = fParentFile.ReadData ( buffer, count );
- fSubPosition += count;
-
- #if debug
- if (steveFlag.Flag(18)) {
- keith << "TFileInFile::ReadData(" << count << " [" << origCount << "]) done. err=" << error << endl;
- }
- #endif
-
-
- if ( error )
- return error;
- else
- return count == origCount ? noErr : eofErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TFileInFile::WriteData ( const void* buffer, long& count )
- {
- TPositionPreserver preserver ( fParentFile );
- OSErr error;
-
- error = (fParentFile.SetPosition ( fsFromStart, fParentOffset + fSubPosition ));
- if ( error ) {
- steveF ( 18, "TFileInFile::WriteData(), error " << error << " in fParent.SetPosition()" );
- count = 0;
- return error;
- }
-
- steveF ( 18, "TFileInFile::WriteData(" << count << ") fPos=" << fSubPosition);
-
- error = fParentFile.WriteData ( buffer, count );
-
- #if debug
- if (steveFlag.Flag(18)) {
- keith << "TFileInFile::WriteData(" << count << ") done, err=" << error;
- DumpHex(keith, buffer, min ( count, 16 ) );
- }
- #endif
-
- fSubPosition += count;
- if ( fSubPosition > fSubSize )
- fSubSize = fSubPosition;
-
- return error;
- }
-
- /***********************************|****************************************/
-
- OSErr TFileInFile::SetEnd ( long end )
- {
- if ( end >= 0 && end <= fSubSize )
- {
- fSubSize = end;
-
- if ( fSubPosition > fSubSize )
- fSubPosition = fSubSize;
-
- return noErr;
- }
- else
- {
- return posErr;
- }
- }
-
- /***********************************|****************************************/
-
- OSErr
- TFileInFile::SetPosition ( short mode, long offset )
- {
- if ( mode == fsFromMark )
- offset += fSubPosition;
- else if ( mode == fsFromLEOF )
- offset += fSubSize;
-
- if ( offset < 0 )
- {
- return posErr;
- }
- else if ( offset <= fSubSize )
- {
- fSubPosition = offset;
- return noErr;
- }
- else
- return eofErr;
- }
-
- /***********************************|****************************************/
-
- OSErr
- TFileInFile::GetPosition ( long& position ) const
- {
- position = fSubPosition;
- return noErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TFileInFile::GetEnd ( long& end ) const
- {
- end = fSubSize;
- return noErr;
- }
-
- /***********************************|****************************************/
-
-
-