File I/O

Availability LightWave 6.0 | Component Layout, Modeler | Header lwio.h

This global provides functions for reading and writing data in files. The state structures returned by the open functions are the same as those passed to the handler save and load callbacks, making it possible for handlers to create and read custom preset files with the same code they use to write and read their settings in scene and object files. This is also an easy way for any kind of Layout plug-in to create block-structured, platform-independent configuration and data files.

Global Call

   LWFileIOFuncs *fiof;
   fiof = global( LWFILEIOFUNCS_GLOBAL, GFUSE_TRANSIENT );

The global function returns a pointer to an LWFileIOFuncs. The structure returned by the open functions is described on the file I/O page.

   typedef struct st_LWFileIOFuncs {
      LWSaveState * (*openSave) (const char *name, int ioMode);
      void          (*closeSave)(LWSaveState *save); 
      LWLoadState * (*openLoad) (const char *name, int ioMode);
      void          (*closeLoad)(LWLoadState *load); 
   } LWFileIOFuncs;
sstate = openSave( name, iomode )
Open a file for writing. The mode can be one of the following.
LWIO_ASCII
Create a text file. Write operations will be line-buffered.
LWIO_BINARY
Create a binary file. Block writes will always use 2-byte integers for block sizes.
LWIO_BINARY_IFF
Create a binary file. Block sizes will be 4-byte integers for the first two nesting levels and 2-byte integers at deeper levels, corresponding to the chunk and subchunk scheme used in LightWave object files.
closeSave( sstate )
Close a file opened by openSave.
lstate = openLoad( name, iomode )
Open a file for reading. The iomodes are the same as those for openSave.
closeLoad( lstate )
Close a file opened by openLoad.

Example

This code fragment creates a text file and writes the contents of a structure. It uses the Observer structure and the write_obs function defined in the Example section of the file I/O page.

   #include <lwserver.h>
   #include <lwio.h>

   LWFileIOFuncs *fiof;
   LWSaveState *save;
   Observer obs = {
      4.0f, "EDT", 2000, 4, 24, 2, 5, 30,
      37.75f, -122.55f,
      1, 40.0f, 30.0f, 100.0f,
      2000.0f
   };

   fiof = global( LWFILEIOFUNCS_GLOBAL, GFUSE_TRANSIENT );
   if ( !fiof ) return AFUNC_BADGLOBAL;

   if ( save = fiof->openSave( "testio.txt", LWIO_ASCII )) {
      write_obs( save, &obs );
      fiof->closeSave( save );
   }

   if ( save = fiof->openSave( "testio.bin", LWIO_BINARY )) {
      write_obs( save, &obs );
      fiof->closeSave( save );
   }