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

  1. /*
  2.     File:        RAMFile.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 __ERRORS__
  15. #include <Errors.h>
  16. #endif
  17.  
  18. #ifndef __RAMFILE__
  19. #include "RamFile.h"
  20. #endif
  21.  
  22. #ifndef    __NEWDELETE__
  23. #include "NewDelete.h"
  24. #endif
  25.  
  26. #ifndef    __DEBUGGINGEAR__
  27. #include "DebuggingGear.h"
  28. #endif
  29.  
  30. #pragma segment RamFile
  31.  
  32. /***********************************|****************************************/
  33.  
  34. TRamFile::TRamFile ( unsigned long initialLength, unsigned long growthRate ):
  35.     TAbstractFile (),
  36.     fHandle ( ::AllocateHandle ( 0 ) ),
  37.     fPosition ( 0 ),
  38.     fLogicalLength ( 0 ),
  39.     fPhysicalLength ( 0 ),
  40.     fGrowthRate ( growthRate )
  41. {
  42.     SetEnd ( initialLength );
  43. }
  44.  
  45. /***********************************|****************************************/
  46.  
  47. TRamFile::~TRamFile ()
  48. {
  49.     ::DeallocateHandle ( fHandle );
  50. }
  51.  
  52. /***********************************|****************************************/
  53.  
  54. OSErr 
  55. TRamFile::ReadData ( void* buffer, long& bytesRequested )
  56. {
  57.     OSErr error = noErr;
  58.  
  59.     const unsigned long bytesAvailable = fLogicalLength - fPosition;
  60.     
  61.     if ( bytesRequested > bytesAvailable )
  62.         bytesRequested = bytesAvailable, error = eofErr;
  63.         
  64.     ::BlockMove ( *fHandle + fPosition, buffer, bytesRequested );
  65.     fPosition += bytesRequested;
  66.     
  67.     return error;
  68. }
  69.  
  70. /***********************************|****************************************/
  71.  
  72. OSErr 
  73. TRamFile::WriteData ( const void* buffer, long& bytesRequested )
  74. {
  75.     OSErr error = noErr;
  76.     
  77.     const unsigned long bytesAvailable = fLogicalLength - fPosition;
  78.     
  79.     if ( bytesRequested > bytesAvailable )
  80.         error = SetEnd ( fLogicalLength + bytesRequested - bytesAvailable );
  81.     
  82.     if ( error == noErr )
  83.     {
  84.         ::BlockMove ( buffer, *fHandle + fPosition, bytesRequested );
  85.         fPosition += bytesRequested;
  86.     }
  87.  
  88.     return error;
  89. }
  90.  
  91. /***********************************|****************************************/
  92.  
  93. OSErr 
  94. TRamFile::SetEnd ( long length )
  95. {
  96.     OSErr error = noErr;
  97.     
  98.     if ( length < 0 )
  99.     {
  100.         error = posErr;
  101.     }
  102.     else if ( length > fPhysicalLength )
  103.     {
  104.         ::SetHandleSize ( fHandle, length + fGrowthRate );
  105.         error = MemError ();
  106.         
  107.         if ( error == noErr )
  108.         {
  109.             fPhysicalLength = length + fGrowthRate;
  110.             fLogicalLength = length;
  111.         }
  112.     }
  113.     else if ( length > fLogicalLength )
  114.     {
  115.         fLogicalLength = length;
  116.     }
  117.     else
  118.     {
  119.         ::SetHandleSize ( fHandle, fLogicalLength = fPhysicalLength = length );
  120.         if ( fPosition > fLogicalLength ) fPosition = fLogicalLength;
  121.     }
  122.     
  123.     return error;
  124. }
  125.  
  126. /***********************************|****************************************/
  127.  
  128. OSErr 
  129. TRamFile::SetPosition ( short mode, long offset )
  130. {
  131.     if ( mode == fsFromMark )
  132.         offset += fPosition;
  133.     else if ( mode == fsFromLEOF )
  134.         offset += fLogicalLength;
  135.  
  136.     if ( offset < 0 )
  137.     {
  138.         return posErr;
  139.     }
  140.     else if ( offset <= fLogicalLength )
  141.     {
  142.         fPosition = offset;
  143.         return noErr;
  144.     }
  145.     else
  146.         return eofErr;
  147. }
  148.  
  149. /***********************************|****************************************/
  150.  
  151. OSErr 
  152. TRamFile::GetEnd ( long& length ) const
  153. {
  154.     length = (long) fLogicalLength;
  155.     return noErr;
  156. }
  157.  
  158. /***********************************|****************************************/
  159.  
  160. OSErr 
  161. TRamFile::GetPosition ( long& position ) const
  162. {
  163.     position = (long) fPosition;
  164.     return noErr;
  165. }
  166.  
  167. /***********************************|****************************************/
  168.  
  169.