VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Reading Data from a File

The getSelf() and getData() member functions read data from a file.

The getSelf() member function works similarly to putSelf(), except that it operates on an MTL stream instead of an open file pointer.

  1. VR-Forces opens, reads, and converts the file being read into an MTL stream. The following example shows how to open an MTL file:

    FILE * fp;
    DtInputStream * fStream;
    fp = fopen("name of you file", "r");
    fStream = new DtExtFileInputStream(fp);
    DtMtlEnvironment mtlEnv;
    getSelf(DtcRead(fStream, mtlEnv.env()));

  2. The routines that handle MTL (mtl.h) retrieve the data.
  3. The getSelf() member function calls getData(), and then the DtReaderWriter's registry getSelf().

The getData() specification is:

virtual DtlObjPtr getData(DtlObjPtr args);

Example RD-1 illustrates the getData() member function for DtRwString.

Example RD-1

// args points to the lisp expression => (name "dataString")
// searchPath is not used in this example
DtlObjPtr DtRwString::getData(DtlObjPtr args, const DtFilename& searchPath)
{
DtlObjPtr currentObject;
DtlObjPtr remainderList;
DtlObjPtr dataList;
dataList = DtcCdr(args); // dataList now points to =>("dataString")
currentObject = DtcCar(dataList); // currentObject now points to
//=>"dataString"
remainderList = DtcCdr(dataList); // remainderList now points to =>()
// error check to make sure currentObject is a string
if (currentObject ->tag() == DtpString)
{
setString(currentObject ->string());
}
else
{
DtWarn("DtRwString::getSelf(): bad data stream\n");
DtcErrPrint(args);
}
return remainderList;
}

Suppose that the "args" DtlObjPtr in the example points to the LISP expression:

(name "dataString")

The "car" of this expression (retrieved with DtcCar(args)) is name (no parentheses) and the "cdr" (DtcCdr(args)) of the expression is ("dataString"). You do not need the name. (getSelf() will have grabbed it if the object was created without a name.) The local variable dataList is set to point to the list of data arguments. (In this case, there is only one element in the list.) The "car" of this expression is the data we want, and the "cdr" of this expression gets returned.

Example RD-2 shows an excerpt of a getData() call for an arbitrary list of numbers (from DtNLengthIntegerVector).

Example RD-2

// searchPath is not used in this example
DtlObjPtr DtNLengthIntegerVector::getData(DtlObjPtr args, const DtFilename& searchPath)
{
// this gives us the list of numbers (gets us past the name)
DtlObjPtr current = DtcCdr(args);
// setVectorLength allocates the correct-sized array of integers
// in myIntegers, and also initializes myVectorLength; DtcLength
// returns the number of elements in the lisp expression ‘current'
setVectorLength(DtcLength(current));
if (myVectorLength > 0)
{
// get each value
for (int i = 0; i < myVectorLength; i++)
{
myIntegers[i] = DtcCar(current)->fixnum();
current = DtcCdr(current);
}
}
return current;
}
Note
Although it was not used in the examples in this section, you must specify searchPath in the getData() call. searchPath is the name of a directory to search when loading files specified in DtReaderWriterFile objects. It is passed in to all DtReaderWriter objects because they may need to pass it down to any child DtReaderWriterFile nodes in its registry.

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)