Chapter 5:
File I/O


 

This chapter describes the file I/O functions that are available in the PGSDK library:

 

Platform-Independent File I/O

 

Loading and Saving TIFF Files

 

RAM File Functions

PLATFORM-INDEPENDENT FILE I/O

 

PGSDK includes the following generic file input/output functions that can be used in any application development platform:

 

IOclose: Platform independent file close

 

IOconnect: Associates platform specific file reference w/IO path structure

 

IOgetc: Platform independent getc - get a character from file

 

IOgets: Platform independent gets - get a string from file

 

IOopen: Platform independent file open

 

IOread: Platform independent read - read from file

 

IOseek: Platform independent seek - seek into file

 

IOwrite: Platform independent write - write to file

LOADING AND SAVING TIFF FILES

 

A PGSDK graph can be loaded from or saved to a Tagged Information File Format (TIFF) file. The Load_TIFFGraph() function loads a TIFF format file into a 3D Graph Structure. The graph structure identified by pGraph must be preallocated and zeroed before this function is called:

 

INT16 Load_TIFFGraph (
     GraphPtr pGraph, /* Pointer to a valid graph */
     UINT32 ulRequest,
     #if defined(_WINDOWS) || UNIX char * pszFileName
     #else FSSpec * pFileSpec
     #endif
);

 

The Save_TIFFGraph() function saves a graph structure into a binary or ASCII file.

 

INT16 Save_TIFFGraph (
     GraphPtr pGraph, /* Pointer to a valid graph */
     UINT32 ulRequest,
     INT16 nFileFmt,
     INT16 Terminator,
     #if defined (_WINDOWS) || UNIX
     char * pszFileName
     #else
     FSSpec * pFileSpec
     #endif
);

 

In both functions, the ulRequest parameter can be used to identify the information to be loaded or saved:

 

REQUEST_TIFFTHUMBNAIL (0x0100): TIFF Thumbnail

 

REQUEST_TDL (0x00DF): Look of the graph only (template)

 

REQUEST_TDC (0x00FF): Look of the graph and chart data

 

REQUEST_TDF (0x01FF): Look of the graph and chart data and TIFF Thumbnail

 

REQUEST_THUMBFILEDESCRIPTION (0x0200): Thumbnail File Description

 

See the PGSDK API Guide for a description of these and other API functions that support the TIFF file format.

 

The following code segment shows how these functions are used to save and load a graph:

 

nError=TestStruct_PackAllDummyData((GraphPtr)
     &SaveGraph);
if (nError == E00_OK)
{
     /* Save the graph */
     ulRequest = REQUEST_TDF;
     Util_c2pcpy((char *)fileSpec.name, "testfile");
     nError = Save_TIFFGraph ((GraphPtr) &SaveGraph,
     ulRequest, FILEFMT_BINARY, TERMINATOR_YES,&fileSpec);
     /* Load the graph */
     nError = Load_TIFFGraph ((GraphPtr) &LoadGraph,
          ulRequest, &fileSpec);
}

RAM FILE FUNCTIONS

 

The RAM file functions provide your application with the ability to save a chart to memory instead of a file.

 

AllocRamFile; allocate a RAM File memory structure

 

FreeRamFile; free a RAM File memory structure

 

GetRamFileName; returns an ASCII coded character name for RAM File

 

GetRamFileBuffer; get memory address of RAM File

 

AllocRamFile: The first step in using a RAM file is to allocate a memory structure to hold the contents of the graph. The AllocRamFile() function allocates a RAM File memory structure and returns a pointer to the allocated memory location as shown in the following example:

 

/*** Create a new ramfile*/
/* 0=use default initial size */
pRamFile = AllocRamFile(0);
if (!pRamFile) {
     MessageBox(     hWnd, "AllocRamFile failed!",
     "MDEBUG_RAMFILE", MB_OK | MB_ICONHAND |
     MB_TASKMODAL);
     goto all_done;
}

 

GetRamFileName: This function returns an ASCII coded character name for the RAM File.

 

/* Convert ramfile ptr to a
string for use with STD_IORW.*/
lstrcpy(szFile, GetRamFileName(pRamFile));
/* Open the file for writing &
create ramfile handle (iop)*/
nErr = IOopen(&iop, szFile, 0, IO_RD_WR);
if (nErr)
{
     wsprintf(szTemp, "IOopen for '%s' failed(%d)!",
          szFile, nErr);
     MessageBox(     hWnd, szTemp, "MDEBUG_RAMFILE",
          MB_OK | MB_ICONHAND | MB_TASKMODAL );
     goto all_done;
}
/* Write the .3TF file out to the ramfile */
nErr= Save_TIFFGraphPath(&iop,gpGraph,nCloneMode,
     FILEFMT_BINARY,FALSE);
if (nErr)
{
     wsprintf(szTemp,
     "SaveTiffGraphPath failed(%d)!", nErr);
     MessageBox( hWnd, szTemp, "MDEBUG_RAMFILE",
          MB_OK | MB_ICONHAND | MB_TASKMODAL );
     goto all_done;
}

 

GetRamFileBuffer: This function gets the memory address of RAM File.

 

/* Get memory address of ramfile */
pRFB = GetRamFileBuffer (pRamFile);     
/* Get length of ramfile by seeking to end of file. */
lBytes = IOseek(&iop, 0L, IO_SEEK_END);
pHolder = pData;/* SAVE pData */
/* COPY TO A DIFFERENT PLACE IN MEMORY */
for (i=0; i < lBytes; i++)*pData++ = *pRFB++;
pData = pHolder; /* RESTORE pData */
/* Close the ramfile handle */
IOclose(&iop);

 

FreeRamFile: This function frees the RAM File memory structure.

 

/* Destroy the ramfile */
FreeRamFile(pRamFile);