home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / VirtualFile / AbstractFile.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  7.1 KB  |  184 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AbstractFile.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __ABSTRACTFILE__
  15. #define __ABSTRACTFILE__
  16.  
  17. #ifndef    __TYPES__
  18. #include "Types.h"
  19. #endif
  20.  
  21. #ifndef __FILES__
  22. #include <Files.h>
  23. #endif
  24.  
  25. #pragma push
  26. #pragma segment AbstractFile
  27.  
  28. const OSType kDefaultType = 'TEXT';
  29. const OSType kDefaultCreator = 'MPS ';
  30. extern ostream& operator << ( ostream&, const FSSpec& );
  31.  
  32. /***********************************|****************************************/
  33.  
  34. class TAbstractFile  : public THandleObject
  35. {
  36. public:
  37.  
  38.     virtual    ~TAbstractFile ();
  39.  
  40.     virtual    ostream&                operator >> ( ostream& ) const;
  41.  
  42. // subclasses must implement these…
  43.  
  44.     virtual OSErr                     WriteData ( const void*, long& ) = 0;
  45.     virtual OSErr                     ReadData ( void*, long& ) = 0;
  46.  
  47.     virtual OSErr                     SetPosition ( short mode, long offset ) = 0;
  48.     virtual OSErr                     GetPosition ( long& offset ) const = 0;
  49.  
  50.     virtual OSErr                     SetEnd ( long ) = 0;
  51.     virtual OSErr                     GetEnd ( long& ) const = 0;
  52.  
  53. // general methods (no need to override)
  54.  
  55.             OSErr                     SetPosition ( long absOffset );
  56.             OSErr                    MovePosition ( long delta );
  57.             long                    GetPosition () const;
  58.             long                    GetEnd () const;
  59.             Boolean                 AtEOF () const;
  60.  
  61.             OSErr                    Write ( const char );
  62.             OSErr                    Write ( const unsigned char );
  63.             OSErr                    Write ( const short );
  64.             OSErr                    Write ( const unsigned short );
  65.             OSErr                    Write ( const long );
  66.             OSErr                    Write ( const unsigned long );
  67.             OSErr                    Write ( const char* );
  68.             OSErr                    Write ( const StringPtr );
  69.             OSErr                    Write ( const class RString& );
  70.             OSErr                    Write ( const class TRString& );
  71.             
  72.             OSErr                    WriteFormat ( const char* format, ... );
  73.             OSErr                     WriteDataIgnore ( const void*, unsigned long );
  74.  
  75.             OSErr                    Read ( char& );
  76.             OSErr                    Read ( unsigned char& );
  77.             OSErr                    Read ( short& );
  78.             OSErr                    Read ( unsigned short& );
  79.             OSErr                    Read ( long& );
  80.             OSErr                    Read ( unsigned long& );
  81.             OSErr                     Read ( StringPtr, long );
  82.             OSErr                     Read ( char*, long, long& bytesRead );
  83.             OSErr                     ReadUntil ( StringPtr, const StringPtr, long maxBytes = 255 );
  84.             OSErr                     ReadUntil ( char*, const StringPtr, long maxBytes = 255 );
  85.             OSErr                     ReadDataIgnore ( void*, unsigned long );
  86.  
  87. // i/o with abstract files
  88.  
  89.             OSErr                    WriteTo ( TAbstractFile& );
  90.             OSErr                    ReadFrom ( TAbstractFile& );
  91.  
  92. // i/o with HFS files
  93.  
  94.     enum Fork { kResource, kData };
  95.  
  96.             OSErr                    WriteTo ( short refNum );
  97.             OSErr                     WriteTo ( Fork fork, const FSSpec&, OSType creator = kDefaultCreator, OSType type = kDefaultType );
  98.             OSErr                     WriteTo ( Fork fork, short vRefNum, long dirID, const StringPtr, OSType creator = kDefaultCreator, OSType type = kDefaultType );
  99.  
  100.             OSErr                    ReadFrom ( short refNum );
  101.             OSErr                     ReadFrom ( Fork fork, const FSSpec& );
  102.             OSErr                     ReadFrom ( Fork fork, short vRefNum, long dirID, const StringPtr );
  103.  
  104. protected:    TAbstractFile ();
  105.             TAbstractFile ( const TAbstractFile& );
  106.             TAbstractFile&            operator = ( const TAbstractFile& );
  107.  
  108.             long                    ComputeAbsoluteOffset ( short mode, long offset ) const;
  109. };
  110.  
  111. /***********************************|****************************************/
  112.  
  113. class TWrapperFile : public TAbstractFile
  114. {
  115. public:        TWrapperFile ( short refNum, Boolean adoptRef = false );
  116.     virtual    ~TWrapperFile ();
  117.  
  118.     virtual OSErr                     ReadData ( void*, long& );
  119.     virtual OSErr                     WriteData ( const void*, long& );
  120.  
  121.     virtual OSErr                     SetPosition ( short, long );
  122.     virtual OSErr                     GetPosition ( long& ) const;
  123.  
  124.     virtual OSErr                     SetEnd ( long );
  125.     virtual OSErr                     GetEnd ( long& ) const;
  126.  
  127. protected:    short                    fRefNum;
  128.             Boolean                    fAdopted;
  129. };
  130.  
  131. /***********************************|****************************************/
  132.  
  133. class TForkFile : public TWrapperFile
  134. {
  135. public:        TForkFile ( const FSSpec& spec, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
  136.             TForkFile ( short vRefNum, long dirID, const StringPtr name, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
  137.     virtual    ~TForkFile ();
  138.  
  139. protected:
  140.  
  141.     virtual    OSErr Open ( const FSSpec& spec, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
  142.     virtual    OSErr Open ( short vRefNum, long dirID, const StringPtr name, TAbstractFile::Fork fork = TAbstractFile::kData, OSType creator = kDefaultCreator, OSType type = kDefaultType );
  143. };
  144.  
  145. /***********************************|****************************************/
  146.  
  147. class TPositionPreserver
  148. {
  149. public:        TPositionPreserver ( TAbstractFile& file ) : fFile ( file ) { fFile.GetPosition ( fPosition ); }
  150.             ~TPositionPreserver () { fFile.SetPosition ( fsFromStart, fPosition ); };
  151.  
  152. private:    TAbstractFile&            fFile;
  153.             long                    fPosition;
  154. };
  155.  
  156. /***********************************|****************************************/
  157.  
  158. inline OSErr TAbstractFile::SetPosition ( long position ) { return SetPosition ( fsFromStart, position ); }
  159. inline OSErr TAbstractFile::MovePosition ( long delta ) { return SetPosition ( fsFromMark, delta ); }
  160. inline OSErr TAbstractFile::Write ( const char c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
  161. inline OSErr TAbstractFile::Read ( char& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
  162. inline OSErr TAbstractFile::Write ( const unsigned char c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
  163. inline OSErr TAbstractFile::Read ( unsigned char& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
  164. inline OSErr TAbstractFile::Write ( const short c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
  165. inline OSErr TAbstractFile::Read ( short& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
  166. inline OSErr TAbstractFile::Write ( const unsigned short c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
  167. inline OSErr TAbstractFile::Read ( unsigned short& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
  168. inline OSErr TAbstractFile::Write ( const long c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
  169. inline OSErr TAbstractFile::Read ( long& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
  170. inline OSErr TAbstractFile::Write ( const unsigned long c ) { return WriteDataIgnore ( &c, sizeof ( c ) ); }
  171. inline OSErr TAbstractFile::Read ( unsigned long& c ) { return ReadDataIgnore ( &c, sizeof ( c ) ); }
  172. inline OSErr TWrapperFile::ReadData ( void* buffer, long& count ) { return ::FSRead ( fRefNum, &count, buffer ); }
  173. inline OSErr TWrapperFile::WriteData ( const void* buffer, long& count) { return ::FSWrite ( fRefNum, &count, buffer ); }
  174. inline OSErr TWrapperFile::SetEnd ( long length ) { return ::SetEOF ( fRefNum, length ); }
  175. inline OSErr TWrapperFile::GetEnd ( long& length ) const { return ::GetEOF ( fRefNum, &length ); }
  176. inline OSErr TWrapperFile::SetPosition ( short mode, long offset ) { return ::SetFPos ( fRefNum, mode, offset ); }
  177. inline OSErr TWrapperFile::GetPosition ( long& position ) const { return ::GetFPos ( fRefNum, &position ); }
  178.  
  179. /***********************************|****************************************/
  180.  
  181. #pragma pop
  182.  
  183. #endif    // __ABSTRACTFILE__
  184.