ccfile.cpp File Reference

(r1785/r1607)

#include "camtypes.h"
#include "zstream.h"
#include <errno.h>

Go to the source code of this file.

Defines

#define new   CAM_DEBUG_NEW
#define CHAR_EOF   ((char)EOF)

Functions

 DECLARE_SOURCE ("$Revision: 1607 $")
 CC_IMPLEMENT_DYNAMIC (CCFile, CCObject)
 CC_IMPLEMENT_DYNAMIC (CCLexFile, CCFile)
 CC_IMPLEMENT_DYNAMIC (CCStreamFile, CCLexFile)
 CC_IMPLEMENT_DYNAMIC (CCDiskFile, CCStreamFile)
 CC_IMPLEMENT_DYNAMIC (CCMemFile, CCLexFile)
 CC_IMPLEMENT_DYNAMIC (CCMemTextFile, CCMemFile)
UINT_PTR fake_filedesc (iostream &io)
 Creates a fake 'file-descriptor' that can be passed to C libraries, such as the Accusoft filter library, that allow the caller to redirect I/O.
iostream * unfake_filedesc (UINT_PTR nFileDesc)
 Complementary function to fake_filedesc.
INT32 ACCUAPI_IO stream_read (INT32 nFileDesc, char *pchBuffer, unsigned nBufSize)
 Adapts functions that call C-style I/O routines so that they can use C++ streams instead. This function substitutes for the standard C read() function. Instead of nFileDesc being a file-descriptor, it is expected to be an INT32 returned by fake_filedesc. Using this function we can persuade old code, such as the Accusoft library, to read and write from structured storage.
INT32 ACCUAPI_IO stream_write (INT32 nFileDesc, char *pchBuffer, unsigned nBufSize)
 Adapts functions that call C-style I/O routines so that they can use C++ streams instead. This function substitutes for the standard C write() function. Instead of nFileDesc being a file-descriptor, it is expected to be an INT32 returned by fake_filedesc. Using this function we can persuade old code, such as the Accusoft library, to read and write from structured storage.
INT32 ACCUAPI_IO stream_seek (INT32 nFileDesc, INT32 nOffset, unsigned fSeekType)
 Adapts functions that call C-style I/O routines so that they can use C++ streams instead. This function substitutes for the standard C seek() function. Instead of nFileDesc being a file-descriptor, it is expected to be an INT32 returned by fake_filedesc. Using this function we can persuade old code, such as the Accusoft library, to read and write from structured storage. Note that the function assumes that the stream has only one combined seek pointer, just like fstream and costream. If moving the input seek pointer doesn't also move the output seek pointer, as may be the case for some exotic iostream derivatives, then the function won't work properly.

Variables

const INT32 TokenBufSize = 256


Define Documentation

#define CHAR_EOF   ((char)EOF)
 

Definition at line 137 of file ccfile.cpp.

#define new   CAM_DEBUG_NEW
 

Definition at line 135 of file ccfile.cpp.


Function Documentation

CC_IMPLEMENT_DYNAMIC CCMemTextFile  ,
CCMemFile 
 

CC_IMPLEMENT_DYNAMIC CCMemFile  ,
CCLexFile 
 

CC_IMPLEMENT_DYNAMIC CCDiskFile  ,
CCStreamFile 
 

CC_IMPLEMENT_DYNAMIC CCStreamFile  ,
CCLexFile 
 

CC_IMPLEMENT_DYNAMIC CCLexFile  ,
CCFile 
 

CC_IMPLEMENT_DYNAMIC CCFile  ,
CCObject 
 

DECLARE_SOURCE "$Revision: 1607 $"   ) 
 

CCFile.cpp - Contains the function declarations of the Kernel CCFile derived classes. These include: CCDiskFile, CCMemFile and CCMemTextFile.

UINT_PTR fake_filedesc iostream &  io  ) 
 

Creates a fake 'file-descriptor' that can be passed to C libraries, such as the Accusoft filter library, that allow the caller to redirect I/O.

Author:
Justin_Flude (Xara Group Ltd) <camelotdev@xara.com>
Date:
11/8/96
Parameters:
io - a reference to an iostream object (not constant). [INPUTS]
Returns:
An integer 'file-descriptor'. This can be passed to the stream_* functions to identify the stream they should work on.
See also:
unfake_filedesc; stream_read; stream_write; stream_seek; _read; _write; _lseek; fstream::fd

Definition at line 6063 of file ccfile.cpp.

06064 {
06065     // Make sure this dirty trick is possible.
06066     ERROR3IF(sizeof(INT32) < sizeof(void*),
06067              "type 'INT32' too small for type 'istream*' in fake_filedesc");
06068 
06069     // Return a "magic number" file-descriptor which is really a pointer to an iostream.
06070     return ~UINT32( (UINT_PTR)(&io) );
06071 }

INT32 ACCUAPI_IO stream_read INT32  nFileDesc,
char *  pchBuffer,
unsigned  nBufSize
 

Adapts functions that call C-style I/O routines so that they can use C++ streams instead. This function substitutes for the standard C read() function. Instead of nFileDesc being a file-descriptor, it is expected to be an INT32 returned by fake_filedesc. Using this function we can persuade old code, such as the Accusoft library, to read and write from structured storage.

Author:
Justin_Flude (Xara Group Ltd) <camelotdev@xara.com>
Date:
5/8/96
Parameters:
nFileDesc - the "file-descriptor" of the "file" to read from. This [INPUTS] is really an iostream* cast to an INT32. pchBuffer - where to put the data that is read. nBufSize - the size of the input buffer pointed to by pchBuffer
Returns:
The number of bytes read by the call, or zero if at the end of the file, or -1 if there is an error.

Errors: ERROR3 if the given buffer is larger than that allowed by istream.

See also:
stream_write; stream_seek; fake_filedesc; unfake_filedesc; iostream; istream::read; ios::eof; ios::bad; costream; fstream; _read

Definition at line 6124 of file ccfile.cpp.

06125 {
06126     // Extract the input stream object associated with this request.
06127     iostream* ps = unfake_filedesc(nFileDesc);
06128     if (ps == 0 || ps->bad()) return -1;
06129 
06130     // Make sure the buffer isn't too big for a signed integer.
06131     if (nBufSize > INT_MAX)
06132     {
06133         ERROR3("Buffer too large for type 'signed INT32' in stream_read");
06134         nBufSize = INT_MAX;
06135     }
06136 
06137     // If we're already at the end of the file then indicate this.
06138     if (ps->eof()) return 0;
06139 
06140     // Try to read the data into the given buffer.
06141     if (!ps->read(pchBuffer, (INT32) nBufSize) && !ps->eof()) return -1;
06142 
06143     // Return the number of bytes actually read.
06144     return ps->gcount();
06145 }

INT32 ACCUAPI_IO stream_seek INT32  nFileDesc,
INT32  nOffset,
unsigned  fSeekType
 

Adapts functions that call C-style I/O routines so that they can use C++ streams instead. This function substitutes for the standard C seek() function. Instead of nFileDesc being a file-descriptor, it is expected to be an INT32 returned by fake_filedesc. Using this function we can persuade old code, such as the Accusoft library, to read and write from structured storage. Note that the function assumes that the stream has only one combined seek pointer, just like fstream and costream. If moving the input seek pointer doesn't also move the output seek pointer, as may be the case for some exotic iostream derivatives, then the function won't work properly.

Author:
Justin_Flude (Xara Group Ltd) <camelotdev@xara.com>
Date:
5/8/96
Parameters:
nFileDesc - the "file-descriptor" of the "file" to write to. This [INPUTS] is really an iostream* cast to an INT32. nOffset - the amount to seek by or the position to seek to. fSeekType - the direction of the seek (see the run-time docs for the _lseek function to get these flags).
Returns:
The new seek position, or -1 if there was an error.

Errors: ERROR3 if the seek type is invalid or if the put and get seek positions aren't tied, as the semantics of the stream require.

See also:
stream_read; stream_write; fake_filedesc; iostream; iostream::seekg; fstream; costream; _lseek

Definition at line 6219 of file ccfile.cpp.

06220 {
06221     // Extract the stream object associated with this request.  Note that in disk-based
06222     // streams (including the costream), the get and put seek positions are tied, so
06223     // moving one moves the other.  Hence we can use a plain istream here.
06224     iostream* ps = unfake_filedesc(nFileDesc);
06225     if (ps == 0 || ps->bad()) return -1;
06226 
06227     // Convert the standard C flags for fSeekType into the iostream equivalents.
06228     // NB. make sure the flags as passed by Accusoft are the same as those used by
06229     // the Microsoft run-time library!
06230     ios::seekdir dir;
06231     switch (fSeekType)
06232     {
06233     case SEEK_SET:  dir = ios::beg; break;
06234     case SEEK_CUR:  dir = ios::cur; break;
06235     case SEEK_END:  dir = ios::end; break;
06236     default:        ERROR3("Bad seek direction in stream_seek"); return -1;
06237     }
06238 
06239     // Try to seek to the new position.
06240     if( !ps->seekg( (streampos)nOffset, ios_base::seekdir(dir) ) )
06241         return -1;
06242 
06243     // Check if the iostream has similar semantics to fstream.
06244     ERROR3IF(ps->tellp() != ps->tellg(),
06245              "Put and get seek pointers aren't tied in stream_seek");
06246 
06247     // Return the new seek position.
06248     return (INT32) ps->tellg();
06249 }

INT32 ACCUAPI_IO stream_write INT32  nFileDesc,
char *  pchBuffer,
unsigned  nBufSize
 

Adapts functions that call C-style I/O routines so that they can use C++ streams instead. This function substitutes for the standard C write() function. Instead of nFileDesc being a file-descriptor, it is expected to be an INT32 returned by fake_filedesc. Using this function we can persuade old code, such as the Accusoft library, to read and write from structured storage.

Author:
Justin_Flude (Xara Group Ltd) <camelotdev@xara.com>
Date:
5/8/96
Parameters:
nFileDesc - the "file-descriptor" of the "file" to write to. This [INPUTS] is really an iostream* cast to an INT32. pchBuffer - where to get the data that is to be written. nBufSize - the size of the output buffer pointed to by pchBuffer
Returns:
The number of bytes written by the call, or -1 if there is an error.

Errors: ERROR3 if the given buffer is larger than that allowed by ostream.

See also:
stream_read; stream_seek; fake_filedesc; iostream; ostream::write; costream; fstream; _write

Definition at line 6170 of file ccfile.cpp.

06171 {
06172     // Extract the output stream object associated with this request.
06173     iostream* ps = unfake_filedesc(nFileDesc);
06174     if (ps == 0 || ps->bad()) return -1;
06175 
06176     // Make sure the buffer isn't too big for a signed integer.
06177     if (nBufSize > INT_MAX)
06178     {
06179         ERROR3("Buffer too large for type 'signed INT32' in stream_write");
06180         nBufSize = INT_MAX;
06181     }
06182 
06183     // Try to write the data from the given buffer.
06184     if (!ps->write(pchBuffer, (INT32) nBufSize)) return -1;
06185 
06186     // Return the number of bytes actually written, which is always all or none.
06187     return (INT32) nBufSize;
06188 }

iostream* unfake_filedesc UINT_PTR  nFileDesc  ) 
 

Complementary function to fake_filedesc.

Author:
Justin_Flude (Xara Group Ltd) <camelotdev@xara.com>
Date:
12/8/96
Parameters:
nFileDesc - the integer fake 'file-descriptor' previously obtained [INPUTS] by a call to fake_filedesc.
Returns:
A pointer to the iostream object the 'file-descriptor' refers to.
See also:
fake_filedesc; stream_read; stream_write; stream_seek; _read; _write; _lseek; fstream::fd

Definition at line 6088 of file ccfile.cpp.

06089 {
06090     // Make sure this dirty trick is possible.
06091     ERROR3IF(sizeof(INT32) < sizeof(void*),
06092              "type 'INT32' too small for type 'istream*' in unfake_filedesc");
06093 
06094     // Convert this magic number back into a pointer to an iostream.
06095     iostream* ps = (iostream*) (void*) ~nFileDesc;
06096     ERROR3IF(ps == 0, "Null 'file-descriptor' in unfake_filedesc");
06097     return ps;
06098 }


Variable Documentation

const INT32 TokenBufSize = 256
 

Definition at line 840 of file ccfile.cpp.


Generated on Sat Nov 10 03:49:05 2007 for Camelot by  doxygen 1.4.4