KrisLibrary  1.0.0
Public Member Functions | List of all members
File Class Reference

A cross-platform class for reading/writing binary data. More...

#include <File.h>

Public Member Functions

bool Open (const char *, int openmode=FILEREAD|FILEWRITE)
 Opens a named file on the hard drive with the given open mode.
 
bool Open (void *, int openmode=FILEREAD|FILEWRITE)
 Connects this File object to a previously opened FILE object.
 
bool OpenData (void *buf, int size, int openmode=FILEREAD|FILEWRITE)
 Connects this File object to a memory buffer.
 
bool OpenData (int openmode=FILEREAD|FILEWRITE)
 
bool OpenTCPSocket (SOCKET sockfd)
 Connects this File object to a socket, using TCP transmission.
 
bool OpenUDPSocket (SOCKET sockfd)
 Connects this File object to a socket, using UDP transmission.
 
void Close ()
 Closes this File object.
 
bool Seek (int amount, int from=FILESEEKCURRENT)
 
int Position () const
 
int Length () const
 
bool ReadData (void *, int size)
 
bool WriteData (const void *, int size)
 
bool ReadString (char *, int bufsize)
 Reads a null-terminated string of at most bufsize characters.
 
bool WriteString (const char *)
 Writes a null-terminated string.
 
bool IsOpen () const
 Returns true if the file object is open.
 
void ResizeDataBuffer (int size)
 
unsigned char * GetDataBuffer () const
 
void * FileObjectPointer ()
 
bool ReadAvailable (int numbytes=1) const
 Returns true if you can read up to numbytes bytes on the file object.
 
bool WriteAvailable (int numbytes=1) const
 

Detailed Description

A cross-platform class for reading/writing binary data.

Can be configured to read from files on disk, memory buffers, or sockets.

To read/write to memory buffers, you may either provide a fixed-size buffer to OpenData(buf,size,mode) or just call OpenData(mode), which allocates a buffer for writing to. GetDataBuffer can then be used to retrieve the buffer.

To open a socket, provide an address in the form http://servername, ftp://servername, tcp://servername:port or udp://servername:port to Open. If opened in FILESERVER mode, the Open call will block until a single connection is accepted.

Seek and Length do not work in sockets. Position returns 0 if the socket is open and -1 otherwise.

Read/WriteString operate differently in sockets. Rather than providing a null-terminated string, the message's first 4 bytes are the length of the string (interpreted as an integer) and the remaining bytes are the string.

Member Function Documentation

void * File::FileObjectPointer ( )

Provides low level access to the file object. If it's a file, returns a pointer to the FILE_POINTER object. If it's a data buffer, returns the data buffer pointe.r If it's a socket, returns a pointer to the SOCKET object. If the object is not open, returns NULL.

Referenced by SocketClientTransport::Start().

unsigned char * File::GetDataBuffer ( ) const

Provides low level access to the memory buffer, or NULL if it's not a data buffer

int File::Length ( ) const

Returns the total length of the data, or -1 if this file object is a stream.

References FILEREAD.

bool File::OpenData ( int  openmode = FILEREAD | FILEWRITE)

Creates a new memory buffer to be written to / read from via this File object.

int File::Position ( ) const

Returns the position of the read/write position, or -1 if the file object is a stream (does not support positioning).

void File::ResizeDataBuffer ( int  size)

In an internally managed memory buffer created via OpenData(), resizes the buffer to the given size.

bool File::Seek ( int  amount,
int  from = FILESEEKCURRENT 
)

Seeks the current read/write position by the given amount, from the given pointer (may be any of the FILESEEK___ constants). Returns false if the file object does not support seeking, or the amount is invalid (before or after the file contents)

References FILESEEKSTART.

bool File::WriteAvailable ( int  numbytes = 1) const

Returns true if you can write up to numbytes bytes on the file object

References FILEREAD.

Referenced by SocketClientTransport::WriteReady().


The documentation for this class was generated from the following files: