13.1 Creating a Virtual File

The following are the necessary steps (as well as some example code) for creating a virtual file.

  1. Create a file.

  2. Write a virtual I/O command to the file, which tells the system that you want to access the actual contents of the file.

  3. Write the transformation template to the file.

The following example contains some of the text to write to a transformation template for a virtual file. It contains the template for accessing portions of our hypothetical toaster object and begins with a virtual I/O command that tells the file system to work on the actual file contents. This template can be cut and pasted into a text file and copied to a virtual file, which will result in the virtual file containing a transformation template.

<virtualIO><define></virtualIO><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE transform SYSTEM "virtualTemplate.dtd">
<transform>

<!-- The following datastream has no name and will be used as the default for read and write requests that have no virtual I/O command preceding them. It takes a long value from eight bytes past toasterObj and converts it to ASCII, then combines it with leadingtext and trailingtext. When this datastream is read, it will return "The current toast temperature is set to 125 degrees Fahrenheit," assuming that the value is 125. A write operation will change the long to the value in the write operation (and must be a number in ASCII).
-->
   <datastream>
      <location symname="toasterObj" offset="8">
         <readloc>
            <format><long signed="yes"></format>
            <leadingtext>The current toast temperature is set to
                  </leadingtext>
            <trailingtext>] degrees Fahrenheit.</trailingtext>
         </readloc>
         <writeloc>
            <format><long></format>
         </writeloc>
      </location>
   </datastream>

<!-- The following toastTemp datastream is similar to the default datastream except that it has no leading and trailing text elements.
-->
   <datastream name="toastTemp">
      <location symname="toasterObj" offset="8">
         <readloc>
            <format><long signed="yes"></format>
         </readloc>
         <writeloc>
            <format><long></format>
         </writeloc>
      </location>
   </datastream>

<!-- The following toasterStructure datastream returns 24 bytes of raw data that represents a structure of toaster data. A write operation will write up to 24 bytes of binary data into memory, starting at toasterObj.
-->
   <datastream name="toasterStructure">
      <location symname="toasterObj">
         <readloc>
            <format><raw length="24"></format>
         </readloc>
         <writeloc>
            <format><raw length="24"></format>
         </writeloc>
      </location>
   </datastream>

<!-- The following toasterAccess datastream uses user-written functions (ReadToasterVariable and WriterToasterVariable) to read and write the virtual contents of the file. The value contained in the readfunc and writefunc tags ("temperature") is passed to the function as well as a buffer for the read and write operations.
-->
   <datastream name="toasterAccess">
      <function>
         <readfunc symname="ReadToasterVariable">temperature</readfunc>
         <writefunc symname="WriteToasterVariable">temperature
            </writefunc> 
      </function>
   </datastream>

<!-- The following toasterStatus datastream allows a command with a response to be sent to the function ProcessToasterCommand. The write operations are taken to be commands and the read operations read the response.
-->
   <datastream name="toasterStatus">
      <function>
         <writefunc symname="ProcessToasterCommand">full</writefunc>
      </function>
   </datastream> 

<!-- These last two datastreams represent data taken directly from the template. In this example, a read operation will result in a help message.
--> 
  <datastream name="shorthelp"> 
      <data>Information about the toaster</data>
   </datastream>

   <datastream name="help">
      <data>This file contains information dealing with the toaster
        object.
            The datastreams are as follows:
          default:          The toast temperature with text.
          toastTemp:        The toast temperation (with no additional
                             text).
          toasterStructure: The raw data from the toasterStructure.
          toasterAccess:   A function that can allow access to any field
                              in the toaster object.
            toasterStatus:    Returns the contents of all fields in the
                              toaster object.
      </data>
   </datastream>
</transform>