home *** CD-ROM | disk | FTP | other *** search
- /*
- File: AbstractFile.h
-
- 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 __ABSTRACTFILE__
- #define __ABSTRACTFILE__
-
- #ifndef __TYPES__
- #include "Types.h"
- #endif
-
- #ifndef __FILES__
- #include <Files.h>
- #endif
-
- #pragma push
- #pragma segment AbstractFile
-
- const OSType kDefaultType = 'TEXT';
- const OSType kDefaultCreator = 'MPS ';
- extern ostream& operator << ( ostream&, const FSSpec& );
-
- /***********************************|****************************************/
-
- class TAbstractFile : public THandleObject
- {
- public:
-
- virtual ~TAbstractFile ();
-
- virtual ostream& operator >> ( ostream& ) const;
-
- // subclasses must implement these…
-
- virtual OSErr WriteData ( const void*, long& ) = 0;
- virtual OSErr ReadData ( void*, long& ) = 0;
-
- virtual OSErr SetPosition ( short mode, long offset ) = 0;
- virtual OSErr GetPosition ( long& offset ) const = 0;
-
- virtual OSErr SetEnd ( long ) = 0;
- virtual OSErr GetEnd ( long& ) const = 0;
-
- // general methods (no need to override)
-
- OSErr SetPosition ( long absOffset );
- OSErr MovePosition ( long delta );
- long GetPosition () const;
- long GetEnd () const;
- Boolean AtEOF () const;
-
- OSErr Write ( const char );
- OSErr Write ( const unsigned char );
- OSErr Write ( const short );
- OSErr Write ( const unsigned short );
- OSErr Write ( const long );
- OSErr Write ( const unsigned long );
- OSErr Write ( const char* );
- OSErr Write ( const StringPtr );
- OSErr Write ( const class RString& );
- OSErr Write ( const class TRString& );
-
- OSErr WriteFormat ( const char* format, ... );
- OSErr WriteDataIgnore ( const void*, unsigned long );
-
- OSErr Read ( char& );
- OSErr Read ( unsigned char& );
- OSErr Read ( short& );
- OSErr Read ( unsigned short& );
- OSErr Read ( long& );
- OSErr Read ( unsigned long& );
- OSErr Read ( StringPtr, long );
- OSErr Read ( char*, long, long& bytesRead );
- OSErr ReadUntil ( StringPtr, const StringPtr, long maxBytes = 255 );
- OSErr ReadUntil ( char*, const StringPtr, long maxBytes = 255 );
- OSErr ReadDataIgnore ( void*, unsigned long );
-
- // i/o with abstract files
-
- OSErr WriteTo ( TAbstractFile& );
- OSErr ReadFrom ( TAbstractFile& );
-
- // i/o with HFS files
-
- enum Fork { kResource, kData };
-
- OSErr WriteTo ( short refNum );
- OSErr WriteTo ( Fork fork, const FSSpec&, OSType creator = kDefaultCreator, OSType type = kDefaultType );
- OSErr WriteTo ( Fork fork, short vRefNum, long dirID, const StringPtr, OSType creator = kDefaultCreator, OSType type = kDefaultType );
-
- OSErr ReadFrom ( short refNum );
- OSErr ReadFrom ( Fork fork, const FSSpec& );
- OSErr ReadFrom ( Fork fork, short vRefNum, long dirID, const StringPtr );
-
- protected: TAbstractFile ();
- TAbstractFile ( const TAbstractFile& );
- TAbstractFile& operator = ( const TAbstractFile& );
-
- long ComputeAbsoluteOffset ( short mode, long offset ) const;
- };
-
- /***********************************|****************************************/
-
- class TWrapperFile : public TAbstractFile
- {
- public: TWrapperFile ( short refNum, Boolean adoptRef = false );
- virtual ~TWrapperFile ();
-
- virtual OSErr ReadData ( void*, long& );
- virtual OSErr WriteData ( const void*, long& );
-
- virtual OSErr SetPosition ( short, long );
- virtual OSErr GetPosition ( long& ) const;
-
- virtual OSErr SetEnd ( long );
- virtual OSErr GetEnd ( long& ) const;
-
- protected: short fRefNum;
- Boolean fAdopted;
- };
-
- /***********************************|****************************************/
-
- class TForkFile : public TWrapperFile
- {
- public: TForkFile ( const FSSpec& spec, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
- TForkFile ( short vRefNum, long dirID, const StringPtr name, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
- virtual ~TForkFile ();
-
- protected:
-
- virtual OSErr Open ( const FSSpec& spec, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
- virtual OSErr Open ( short vRefNum, long dirID, const StringPtr name, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
- };
-
- /***********************************|****************************************/
-
- class TPositionPreserver
- {
- public: TPositionPreserver ( TAbstractFile& file ) : fFile ( file ) { fFile.GetPosition ( fPosition ); }
- ~TPositionPreserver () { fFile.SetPosition ( fsFromStart, fPosition ); };
-
- private: TAbstractFile& fFile;
- long fPosition;
- };
-
- /***********************************|****************************************/
-
- inline OSErr TAbstractFile::SetPosition ( long position ) { return SetPosition ( fsFromStart, position ); }
- inline OSErr TAbstractFile::MovePosition ( long delta ) { return SetPosition ( fsFromMark, delta ); }
- inline OSErr TAbstractFile::Write ( const char c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Read ( char& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Write ( const unsigned char c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Read ( unsigned char& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Write ( const short c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Read ( short& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Write ( const unsigned short c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Read ( unsigned short& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Write ( const long c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Read ( long& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Write ( const unsigned long c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TAbstractFile::Read ( unsigned long& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
- inline OSErr TWrapperFile::ReadData ( void* buffer, long& count ) { return ::FSRead ( fRefNum, &count, buffer ); }
- inline OSErr TWrapperFile::WriteData ( const void* buffer, long& count) { return ::FSWrite ( fRefNum, &count, buffer ); }
- inline OSErr TWrapperFile::SetEnd ( long length ) { return ::SetEOF ( fRefNum, length ); }
- inline OSErr TWrapperFile::GetEnd ( long& length ) const { return ::GetEOF ( fRefNum, &length ); }
- inline OSErr TWrapperFile::SetPosition ( short mode, long offset ) { return ::SetFPos ( fRefNum, mode, offset ); }
- inline OSErr TWrapperFile::GetPosition ( long& position ) const { return ::GetFPos ( fRefNum, &position ); }
-
- /***********************************|****************************************/
-
- #pragma pop
-
- #endif // __ABSTRACTFILE__
-