home *** CD-ROM | disk | FTP | other *** search
- /*
-
- CIsleDataFile.c
- Superclass: CIsleFile
-
- CDataFile transplanted into CIsleFile
-
- March 20, 1992 isl
-
- */
-
- #include <CIsleDataFile.h>
-
- /*=====================*/
- /*===---------------===*/
-
- /******************************************************************************
- CIsleDataFile.c
-
- The File Class
-
- Class for a disk-based file. Information can be read from or written to
- a File.
-
- SUPERCLASS = CIsleFile
-
- Copyright © 1989 Symantec Corporation. All rights reserved.
-
- TCL 1.1 CHANGES
- [
- - changed implementation to use exceptions. This also changed
- several interfaces because errors are now reported through
- the failure mechanism instead of through function results.
- If you need or prefer the old interface, use the compatibility
- class ODataFile.
- ]
-
- ******************************************************************************/
-
- #include "CIsleDataFile.h"
-
- /**** C O N S T R U C T I O N / D E S T R U C T I O N M E T H O D S ****/
-
-
- /******************************************************************************
- IDataFile
-
- Initialize a Data File object
- ******************************************************************************/
-
- void CIsleDataFile::IDataFile()
- {
- CIsleFile::IIsleFile();
-
- refNum = 0;
- }
-
-
- /**** A C C E S S I N G M E T H O D S ****/
-
-
- /******************************************************************************
- SetLength
-
- Set the length, in bytes, of the data fork of a file. File
- must be already open.
- ******************************************************************************/
-
- void CIsleDataFile::SetLength(
- long aLength)
- {
- FailOSErr( SetEOF(refNum, aLength) );
- }
-
-
- /******************************************************************************
- GetLength
-
- Retrieve the length in bytes of the data fork of a file. File
- must be already open.
- ******************************************************************************/
-
- long CIsleDataFile::GetLength( void)
- {
- long theLength;
-
- FailOSErr( GetEOF( refNum, &theLength));
- return theLength;
- }
-
-
- /******************************************************************************
- SetMark
-
- Specify the position of the mark which determines determines the
- starting point for subsequent read/write operations. Position
- is specified as a distance from a certain location:
- fsFromStart, fsFromLEOF, fsFromMark (the beginning, end,
- or current mark).
- Positive distances offset towards the end of the file, negative
- ones towards the beginning. File must already be open.
- ******************************************************************************/
-
- void CIsleDataFile::SetMark(
- long howFar,
- short fromWhere)
- {
- FailOSErr( SetFPos(refNum, fromWhere, howFar) );
- }
-
-
- /******************************************************************************
- GetMark
-
- Retrieve the position of the mark which determines determines the
- starting point for subsequent read/write operations. File must
- already be open.
- ******************************************************************************/
-
- long CIsleDataFile::GetMark( void)
- {
- long markPos;
-
- FailOSErr( GetFPos(refNum, &markPos));
- return markPos;
- }
-
-
- /**** O P E N / C L O S E M E T H O D S ****/
-
-
- /******************************************************************************
- Open
-
- Open the data fork of a file with the specified access permission
- ******************************************************************************/
-
- void CIsleDataFile::Open(
- SignedByte permission)
- {
- short newRefNum;
- Boolean wasLocked;
-
- wasLocked = Lock( TRUE);
- FailOSErr( HOpen(volNum, dirID, name, permission, &newRefNum));
- Lock( wasLocked);
-
- refNum = newRefNum;
- }
-
-
- /******************************************************************************
- Close {OVERRIDE}
-
- Close a File and make sure contents are written to disk
- ******************************************************************************/
-
- void CIsleDataFile::Close()
- {
- register OSErr errCode;
- short theRefNum;
-
- if (refNum > 0)
- {
- theRefNum = refNum;
- refNum = 0; /* clear refnum before closing file, in case it fails */
- FailOSErr( FSClose( theRefNum));
- FailOSErr( FlushVol( NULL, volNum));
- }
-
- }
-
-
- /**** R E A D / W R I T E M E T H O D S ****/
-
-
- /******************************************************************************
- ReadAll
-
- Read the entire contents of a file into memory. A newly created
- handle containing the data is returned. File must already be open.
- ******************************************************************************/
-
- Handle CIsleDataFile::ReadAll( void)
- {
- register OSErr errCode;
- long length; /* Length of file in bytes */
- Handle contents = NULL;
-
- TRY
- {
- FailOSErr( GetEOF( refNum, &length));
- contents = NewHandleCanFail( length);
- FailNIL( contents);
- FailOSErr( SetFPos( refNum, fsFromStart, 0L));
- FailOSErr( FSRead( refNum, &length, *contents));
- }
- CATCH
- {
- ForgetHandle( contents);
- }
- ENDTRY;
-
- return contents;
- }
-
-
- /******************************************************************************
- ReadSome
-
- Read the specified number of bytes from a file starting at the
- current mark. Space must already have been allocated to store
- the data and a pointer to this space is passed as a parameter.
- The file must already be open.
- ******************************************************************************/
-
- void CIsleDataFile::ReadSome(
- Ptr info,
- long howMuch)
- {
- FailOSErr( FSRead(refNum, &howMuch, info) );
- }
-
-
- /******************************************************************************
- WriteAll
-
- Write all the data in a handle to a file. The existing contents
- of the file are replaces. File must already be open.
- ******************************************************************************/
-
- void CIsleDataFile::WriteAll(
- Handle contents)
- {
- register OSErr errCode;
- long length; /* Number of bytes to write out */
-
- length = GetHandleSize(contents);
- /* Move mark to beginning of file */
- FailOSErr( SetFPos( refNum, fsFromStart, 0L));
-
- /* Write out all the data */
- FailOSErr( FSWrite( refNum, &length, *contents));
-
- /* Set file length in case previous */
- /* contents was bigger */
- FailOSErr( SetEOF( refNum, length));
-
- /* Force write to disk */
- FailOSErr( FlushVol( NULL, volNum));
-
- }
-
-
- /******************************************************************************
- WriteSome
-
- Write the specified number of bytes to a file starting at the
- current mark. The file must already be open.
- ******************************************************************************/
-
- void CIsleDataFile::WriteSome(
- Ptr info,
- long howMuch)
- {
- FailOSErr( FSWrite(refNum, &howMuch, info) );
- }
-