Xpace
Xpace::File Class Reference

#include <file.h>

Classes

class  Cant_Copy
 thrown if either of the above two fails e.g., if rhs is not read-only More...
 
struct  Segment
 

Public Types

enum  OpenMode {
  ReadSequential, ReadRandom, WriteSequential, WriteSequentialNew,
  WriteRandom, WriteRandomNew
}
 
enum  Type {
  Raw, Any, DataStore, DataRefs,
  DataMap, IndexList, IndexMap
}
 
typedef uint64 Position
 
typedef int64 Distance
 

Public Member Functions

 File ()
 constructor - Create an empty file More...
 
 File (const File &rhs)
 creates an independent copy rhs must be read-only; result is read-only copy this More...
 
Fileoperator= (const File &rhs)
 assign an independent copy rhs must be read-only; result is read-only copy this More...
 
virtual ~File ()
 destructor - closes if open More...
 
virtual bool open (const String name, OpenMode mode, Type type=Any, size_t bufSize=0)
 
virtual void fromHandle (FILE *handle, size_t bufSize=0)
 Open a special file, which is not seekable, such as a pipe. More...
 
virtual bool operator! () const
 
virtual String getName () const
 
virtual size_t getBufSize () const
 
virtual bool flush ()
 Make sure all changes are written. More...
 
virtual bool close ()
 Close the file, flushing if necessary. More...
 
virtual bool remove ()
 delete the file, if it exists file does not have to be open More...
 
size_t read (void *dest, size_t bytes)
 Read from the file. More...
 
template<typename T >
bool read (T *dest)
 Read a T from the file. More...
 
template<typename T >
read ()
 Read a T from the file. More...
 
String readString (bool raw=false, size_t maxLen=0x100000)
 Read thehe remainder of the as a 16-bit char string. More...
 
String readString8 (size_t maxLen=0x100000)
 Read the remainder of the file as an 8-bit char string. More...
 
size_t write (const void *src, size_t bytes)
 Write some data to the file. More...
 
template<typename T >
bool write (const T &src)
 Write a T to the file. More...
 
bool writeString (const String &str, bool raw=false)
 Write a String to the file. More...
 
template<typename T >
size_t get (T **dest, size_t bytes)
 Get by direct access to the file buffer not guaranteed to be valid after successive calls. More...
 
template<typename T >
size_t getUntil (T **dest, T delim)
 Get bytes until a delimiter. More...
 
template<typename T >
T * get ()
 Get a T by direct access to the file buffer return not guaranteed to be valid after successive calls. More...
 
virtual Position getLength () const
 Get file length. More...
 
virtual Position getLength (const String name)
 Get length of a file by name. More...
 
virtual Position getPos () const
 Get current position. More...
 
virtual bool seek (Position pos)
 Move current position. More...
 
bool seekRel (Distance dist)
 Move current position. More...
 
size_t bufRemains () const
 
bool copy (File *targ, File::Position size)
 copy from this file to another More...
 

Static Public Attributes

static const Position errorPosition
 

Protected Types

enum  { SEQ_BUF_SIZE = 65536, RAND_BUF_SIZE = 16384 }
 
enum  { OS_BUF_SIZE = 4096 }
 

Protected Member Functions

virtual bool seekFile (uint64)
 
virtual size_t fillBuffer ()
 
virtual size_t fileRead (byte *dest, size_t bytes)
 
virtual size_t fileWrite (const byte *src, size_t bytes)
 

Protected Attributes

String name
 
OpenMode mode
 
Type type
 the file's type More...
 
bool writable
 opened for write More...
 
bool bufDirty
 is buffer modified More...
 
bool seekable
 is not a special file More...
 
uint64 bufOffset
 offset of buffer in file More...
 
size_t bufSize
 actual buffer size More...
 
bytebuffer
 buffer More...
 
bytecurrent
 current position in buffer More...
 
byteend
 end of data in buffer More...
 
FILE * hd
 

Detailed Description

Definition at line 37 of file file.h.

Member Typedef Documentation

Definition at line 40 of file file.h.

Definition at line 41 of file file.h.

Member Enumeration Documentation

Enumerator
ReadSequential 

open existing for (primarily) sequential reading

ReadRandom 

open existing for (primarily) random reading

WriteSequential 

open existing or new for (primarily) sequential writing

WriteSequentialNew 

create for (primarily) sequential writing

WriteRandom 

open existing or new for (primarily) random writing

WriteRandomNew 

create for (primarily) random writing

Definition at line 77 of file file.h.

Enumerator
Raw 

an untyped file from an external source

Any 

match any type (except raw)

DataStore 
DataRefs 
DataMap 
IndexList 
IndexMap 

Definition at line 87 of file file.h.

anonymous enum
protected
Enumerator
SEQ_BUF_SIZE 
RAND_BUF_SIZE 

Definition at line 302 of file file.h.

anonymous enum
protected
Enumerator
OS_BUF_SIZE 

Definition at line 337 of file file.h.

Constructor & Destructor Documentation

Xpace::File::File ( )

constructor - Create an empty file

Xpace::File::File ( const File rhs)

creates an independent copy rhs must be read-only; result is read-only copy this

virtual Xpace::File::~File ( )
virtual

destructor - closes if open

Member Function Documentation

File& Xpace::File::operator= ( const File rhs)

assign an independent copy rhs must be read-only; result is read-only copy this

virtual bool Xpace::File::open ( const String  name,
OpenMode  mode,
Type  type = Any,
size_t  bufSize = 0 
)
virtual
Parameters
namefull path name
modefile open mode
typethe file's type; an attempt to open an existing file of the wrong type will fail
bufSizeif 0, use defaults
Returns
true iff successful
virtual void Xpace::File::fromHandle ( FILE *  handle,
size_t  bufSize = 0 
)
virtual

Open a special file, which is not seekable, such as a pipe.

Parameters
handleOS handle
bufSizeif 0, use defaults
bool Xpace::File::operator! ( ) const
inlinevirtual
Returns
true iff the file is not open

Definition at line 430 of file file.h.

References getName().

size_t Xpace::File::getBufSize ( ) const
inlinevirtual
Returns
the file's internal buffer size

Definition at line 450 of file file.h.

References read().

Referenced by getName().

virtual bool Xpace::File::flush ( )
virtual

Make sure all changes are written.

virtual bool Xpace::File::close ( )
virtual

Close the file, flushing if necessary.

virtual bool Xpace::File::remove ( )
virtual

delete the file, if it exists file does not have to be open

Returns
true iff file existed and successfully deleted
size_t Xpace::File::read ( void *  dest,
size_t  bytes 
)
inline

Read from the file.

Parameters
destdestination buffer
bytesnumber of bytes requested
Returns
actual bytes read; read will stop at end-of-file

Definition at line 458 of file file.h.

References read().

template<typename T >
bool Xpace::File::read ( T *  dest)
inline

Read a T from the file.

Parameters
destfillin destination T
Returns
true iff successful

Definition at line 476 of file file.h.

References read().

template<typename T >
T Xpace::File::read ( )
inline

Read a T from the file.

Returns
the T
Exceptions
File_Cant_Readiff unsuccessful

Definition at line 484 of file file.h.

References get().

Referenced by getBufSize(), and read().

String Xpace::File::readString ( bool  raw = false,
size_t  maxLen = 0x100000 
)

Read thehe remainder of the as a 16-bit char string.

Parameters
rawread remainder of file as one string, otherwise read string written with writeString
maxLenread no more than this many chars
Returns
read String
String Xpace::File::readString8 ( size_t  maxLen = 0x100000)

Read the remainder of the file as an 8-bit char string.

Parameters
maxLenread no more than this many chars
Returns
read String
size_t Xpace::File::write ( const void *  src,
size_t  bytes 
)
inline

Write some data to the file.

Parameters
srcsource buffer
bytesnumber of bytes requested
Returns
number of bytes written

Definition at line 602 of file file.h.

Referenced by get().

template<typename T >
bool Xpace::File::write ( const T &  src)
inline

Write a T to the file.

Parameters
srcsource T
Returns
true if successful

Definition at line 627 of file file.h.

References seekRel().

bool Xpace::File::writeString ( const String str,
bool  raw = false 
)

Write a String to the file.

Parameters
strString to be written
rawif false, write for readString(), if true just write chars
Returns
true if successful
template<typename T >
size_t Xpace::File::get ( T **  dest,
size_t  bytes 
)
inline

Get by direct access to the file buffer not guaranteed to be valid after successive calls.

Parameters
destptr to data file buffer
countnumber of Ts requested
Returns
actual Ts available

Definition at line 495 of file file.h.

References getUntil().

template<typename T >
size_t Xpace::File::getUntil ( T **  dest,
delim 
)
inline

Get bytes until a delimiter.

Parameters
destptr to data file buffer
delimthe delimiter
Returns
bytes between dest and the delimiter

Definition at line 530 of file file.h.

References get().

Referenced by get().

template<typename T >
T * Xpace::File::get ( )
inline

Get a T by direct access to the file buffer return not guaranteed to be valid after successive calls.

Returns
ptr to T on success, 0 if fail

Definition at line 593 of file file.h.

References write().

Referenced by getUntil(), and read().

virtual Position Xpace::File::getLength ( ) const
virtual

Get file length.

Returns
offset of first byte past end-of-file
virtual Position Xpace::File::getLength ( const String  name)
virtual

Get length of a file by name.

Parameters
namefull path name
Returns
filelength; or positionError if file doesn't exist
virtual Position Xpace::File::getPos ( ) const
virtual

Get current position.

Returns
current offset in file; -1 if file not open

Referenced by Xpace::File_Cant_Read::File_Cant_Read(), Xpace::File_Cant_Write::File_Cant_Write(), and Xpace::File_Corrupt::File_Corrupt().

virtual bool Xpace::File::seek ( Position  pos)
virtual

Move current position.

Parameters
posseek to this position
Returns
true if successful
bool Xpace::File::seekRel ( Distance  dist)
inline

Move current position.

Parameters
distmove this amount
Returns
true if successful

Definition at line 634 of file file.h.

References bufRemains().

Referenced by write().

size_t Xpace::File::bufRemains ( ) const
inline
Returns
how much can be read before a physical read

Definition at line 657 of file file.h.

Referenced by seekRel().

bool Xpace::File::copy ( File targ,
File::Position  size 
)

copy from this file to another

Parameters
targcopy to this file
sizecopy this many bytes
Returns
true if successful
virtual bool Xpace::File::seekFile ( uint64  )
protectedvirtual
virtual size_t Xpace::File::fillBuffer ( )
protectedvirtual
virtual size_t Xpace::File::fileRead ( byte dest,
size_t  bytes 
)
protectedvirtual
virtual size_t Xpace::File::fileWrite ( const byte src,
size_t  bytes 
)
protectedvirtual

Member Data Documentation

const Position Xpace::File::errorPosition
static

Definition at line 42 of file file.h.

String Xpace::File::name
protected

Definition at line 304 of file file.h.

OpenMode Xpace::File::mode
protected

Definition at line 305 of file file.h.

Type Xpace::File::type
protected

the file's type

Definition at line 307 of file file.h.

bool Xpace::File::writable
protected

opened for write

Definition at line 308 of file file.h.

bool Xpace::File::bufDirty
protected

is buffer modified

Definition at line 309 of file file.h.

bool Xpace::File::seekable
protected

is not a special file

Definition at line 310 of file file.h.

uint64 Xpace::File::bufOffset
protected

offset of buffer in file

Definition at line 311 of file file.h.

size_t Xpace::File::bufSize
protected

actual buffer size

Definition at line 312 of file file.h.

byte* Xpace::File::buffer
protected

buffer

Definition at line 313 of file file.h.

byte* Xpace::File::current
protected

current position in buffer

Definition at line 314 of file file.h.

byte* Xpace::File::end
protected

end of data in buffer

Definition at line 315 of file file.h.

FILE* Xpace::File::hd
protected

Definition at line 334 of file file.h.


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

current as of Wed Jun 10 2026 12:00:06