KCMS CMM Reference Manual

KcsFile Class

The KcsFile class is a file I/O derivative of the KcsIO class. It provides a way to manipulate files by creating a KcsIO object. You can use the protected and public member functions in this class in the implementation of your CMM; you cannot override any member function in this class.

The header file for the class is kcsfile.h.


Note -

It is highly recommended that you do not use any of the variables and functions for handle-based memory in the kcsmblk.h header file. Handle-based memory is not required on the Solaris system.


The constant and #define identifiers of this class as defined in the kcsids.h header file are:

const KcsId KcsIOFileId = {(0x46696C65UL)}; /* 'File' */
#define KcsIOFileIdd (0x46696C65UL) /* 'File' */

In addition to the KcsIO virtual functions that must be overridden for a minimal KcsIO implementation, the KcsFile class has member functions for reading from and writing to a file. See Table 2-4 for detailed information on the virtual functions that are minimally required to derive from the KcsIO class.

This class does not have any protected members; the public members are described.

Public Members

The KcsFile class provides the following public members.

Table 2-8 KcsFile Public Members

Member Function 

Description 

virtual long getFref();

Gets the file description being used. 

KcsFile(KcsStatus *status);

Constructor. Creates a file object without a file and offset. You must use setFref() to establish a link to a file before using any other methods.

KcsFile(KcsStatus *status,
 	const KcsFileId anFref,
 	const unsigned long absBaseOffset = 0);

Constructor. Creates a file object with an open file and offset. 

virtual ~KcsFile();

Destructor. 

virtual void setFref(long theFref);

Sets the file to use and the offset. 

Examples

The following examples show you how to use the KcsFile class.

Reading a File From a Specified Offset

This example shows you how to read a file from a specified offset with absRead(). See the kcsio.h header file for a description of absRead().


Example 2-3 Reading a File From a Specified Offset

KcsStatus readIt()
 {
 	KcsStatus sStatus;
 	KcsFileId sFileRef;
 	long index = 32;
 	long number;

 	// open the file, put the fileRef into sFileRef
 	sFileRef = open ("Profile", O_RDWR);
 	if (sFileRef == -1)
 			return (KCS_IO_ERROR);
 	// create a file object
 	file = new KcsFile(&sStatus, sFileRef, 0);
 	if (sStatus != KCS_SUCCESS)
 			return(sStatus);

 	// using the file object, read from the file into a buffer.
 	sStatus = file->absRead(index, sizeof(long), &number);
 	delete file;
 	close(sFileRef);

 	return (sStatus);
 }

Writing to a File From the Last Cursor Position

This example shows you how to write to a file with relWrite(). See Table 2-3 for a full definition of relWrite().


Example 2-4 Writing to a File From the Last Cursor Position

KcsStatus writeIt()
 {
 	KcsStatus sStatus;
 	KcsFileId sFileRef;
 	long nbytes;
 	char *buffer;

 	// open the file, get a fileRef
 	sFileRef = open ("Profile", O_RDWR);
 	if (sFileRef == -1)
 			return (KCS_IO_ERROR);

 	// create a file object
 	file = new KcsFile(&sStatus, sFileRef);
 	if (sStatus != KCS_SUCCESS)
 			return(sStatus);

 	// Allocate memory for the buffer, fill it with data.
 	// Set nbytes to the length of the buffer.
 	if ((buffer = (char*) malloc(nbytes)) == NULL)
 			return (KCS_IO_ERROR);
 	delete file;
 	close(sFileRef);

 	// using the file object, write the buffer to the file.
 	sStatus = file->relWrite(nbytes, buffer);

 	// Free the buffer's memory.
 	free (buffer);
 	delete file;
 	close(sFileRef);

 	return (sStatus);
 }