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 / FileInFile.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  4.0 KB  |  190 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        FileInFile.cp
  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 __FILEINFILE__
  15. #include "FileInFile.h"
  16. #endif
  17.  
  18. #ifndef __ERRORS__
  19. #include <Errors.h>
  20. #endif
  21.  
  22. /***********************************|****************************************/
  23.  
  24. #pragma segment FileInFile
  25.  
  26. /***********************************|****************************************/
  27.  
  28. extern ostream& DumpHex (ostream& s, const void *p, unsigned long size);
  29.  
  30. /***********************************|****************************************/
  31.  
  32. inline long min ( long a, long b )
  33. {
  34.     return ( a < b) ? a : b;
  35. }
  36.  
  37. /***********************************|****************************************/
  38.  
  39. TFileInFile::TFileInFile(TVirtualFile* parentFile, long offset, long theSize):
  40.     TVirtualFile (),
  41.     fParentFile ( *parentFile ),
  42.     fParentOffset ( offset ),
  43.     fSubSize ( theSize ),
  44.     fSubPosition ( 0 )
  45. {
  46.     fParentFile.RegisterReference ();
  47. }
  48.  
  49. /***********************************|****************************************/
  50.  
  51. TFileInFile::~TFileInFile()
  52. {
  53.     fParentFile.UnregisterReference ();
  54. }
  55.  
  56. /***********************************|****************************************/
  57.  
  58. OSErr TFileInFile::ReadData ( void* buffer, long& count )
  59. {
  60.     TPositionPreserver preserver ( fParentFile );
  61.     long origCount = count;
  62.     OSErr error; 
  63.  
  64.     if ( count + fSubPosition > fSubSize )
  65.         count = fSubSize - fSubPosition;
  66.  
  67.     error = (fParentFile.SetPosition ( fsFromStart, fParentOffset + fSubPosition) );
  68.     if ( error )
  69.     {
  70.         count = 0;
  71.         return error;
  72.     }
  73.  
  74.     #if debug
  75.     if (steveFlag.Flag(18)) {
  76.         keith << "TFileInFile::ReadData(" << count << " [" << origCount << "])  pos=" << fSubPosition << " from parent." << endl;
  77.     }
  78.     #endif
  79.     
  80.     error = fParentFile.ReadData ( buffer, count );
  81.     fSubPosition += count;
  82.  
  83.     #if debug
  84.     if (steveFlag.Flag(18)) {
  85.         keith << "TFileInFile::ReadData(" << count << " [" << origCount << "]) done. err=" << error << endl;
  86.     }
  87.     #endif
  88.     
  89.     
  90.     if ( error )
  91.         return error;
  92.     else
  93.         return count == origCount ? noErr : eofErr;
  94. }
  95.  
  96. /***********************************|****************************************/
  97.  
  98. OSErr TFileInFile::WriteData ( const void* buffer, long& count )
  99. {
  100.     TPositionPreserver preserver ( fParentFile );
  101.     OSErr error;
  102.  
  103.     error = (fParentFile.SetPosition ( fsFromStart, fParentOffset + fSubPosition ));
  104.     if ( error ) {
  105.         steveF ( 18, "TFileInFile::WriteData(), error " << error << " in fParent.SetPosition()" );
  106.         count = 0;
  107.         return error;
  108.     }
  109.  
  110.     steveF ( 18, "TFileInFile::WriteData(" << count << ")  fPos=" << fSubPosition);
  111.     
  112.     error = fParentFile.WriteData ( buffer, count );
  113.  
  114.     #if debug
  115.     if (steveFlag.Flag(18)) {
  116.         keith << "TFileInFile::WriteData(" << count << ") done, err=" << error;
  117.         DumpHex(keith, buffer, min ( count, 16 ) );
  118.     }
  119.     #endif
  120.     
  121.     fSubPosition += count;
  122.     if ( fSubPosition > fSubSize )
  123.         fSubSize = fSubPosition;
  124.  
  125.     return error;
  126. }
  127.  
  128. /***********************************|****************************************/
  129.  
  130. OSErr TFileInFile::SetEnd ( long end )
  131. {
  132.     if ( end >= 0 && end <= fSubSize )
  133.     {
  134.         fSubSize = end;
  135.  
  136.         if ( fSubPosition > fSubSize )
  137.             fSubPosition = fSubSize;
  138.         
  139.         return noErr;
  140.     }
  141.     else
  142.     {
  143.         return posErr;
  144.     }
  145. }
  146.  
  147. /***********************************|****************************************/
  148.  
  149. OSErr 
  150. TFileInFile::SetPosition ( short mode, long offset )
  151. {
  152.     if ( mode == fsFromMark )
  153.         offset += fSubPosition;
  154.     else if ( mode == fsFromLEOF )
  155.         offset += fSubSize;
  156.  
  157.     if ( offset < 0 )
  158.     {
  159.         return posErr;
  160.     }
  161.     else if ( offset <= fSubSize )
  162.     {
  163.         fSubPosition = offset;
  164.         return noErr;
  165.     }
  166.     else
  167.         return eofErr;
  168. }
  169.  
  170. /***********************************|****************************************/
  171.  
  172. OSErr 
  173. TFileInFile::GetPosition ( long& position ) const
  174. {
  175.     position = fSubPosition;
  176.     return noErr;
  177. }
  178.  
  179. /***********************************|****************************************/
  180.  
  181. OSErr TFileInFile::GetEnd ( long& end ) const
  182. {
  183.     end = fSubSize;
  184.     return noErr;
  185. }
  186.  
  187. /***********************************|****************************************/
  188.  
  189.  
  190.