lcls2
C++ API: public headers of xtcdata, psalg, psdaq and psana
Loading...
Searching...
No Matches
MyFILE Class Reference

Small binary file wrapper around a C FILE: open() for reading or writing (chosen at construction), raw read()/write(), and >> / << operators that read or write the bytes of basic types. More...

#include <LMF_IO.hh>

Public Member Functions

 MyFILE (bool mode_reading_)
 Set the mode (true reads, false writes) and clear error, eof, position and filesize; no file is open.
 
 ~MyFILE ()
 Call close() (which sets error to 1 if no file was open), then reset error and eof.
 
bool open (__int8 *name)
 Open name with fopen(): "rb" in read mode, recording the file size by seeking to the end and back, or "wb" in write mode.
 
void close ()
 Close the file if one is open (otherwise set error to 1), then reset position, filesize and eof.
 
unsigned __int64 tell ()
 Return the position kept by this class (bytes read or written since open, or the last seek()).
 
void seek (unsigned __int64 pos)
 fseeko() to pos from the start of the file; on success set the position to pos, otherwise set error to 1.
 
void read (__int8 *string, __int32 length_bytes)
 Read length_bytes bytes into string with fread().
 
void read (unsigned __int32 *dest, __int32 length_bytes)
 Read length_bytes bytes into dest with fread().
 
void write (const __int8 *string, __int32 length)
 Write length bytes from string with fwrite() (the result is not checked), advance the position and raise filesize to it if needed.
 
void flush ()
 Call fflush() on the file.
 
MyFILE & operator>> (unsigned __int8 &c)
 Read sizeof(unsigned __int8) bytes into c with read().
 
MyFILE & operator>> (__int8 &c)
 Read sizeof(__int8) bytes into c with read().
 
MyFILE & operator>> (unsigned __int16 &l)
 Read sizeof(unsigned __int16) bytes into l with read().
 
MyFILE & operator>> (unsigned __int32 &l)
 Read sizeof(unsigned __int32) bytes into l with read().
 
MyFILE & operator>> (unsigned __int64 &l)
 Read sizeof(unsigned __int64) bytes into l with read().
 
MyFILE & operator>> (__int16 &s)
 Read sizeof(__int16) bytes into s with read().
 
MyFILE & operator>> (__int32 &l)
 Read sizeof(__int32) bytes into l with read().
 
MyFILE & operator>> (__int64 &l)
 Read sizeof(__int64) bytes into l with read().
 
MyFILE & operator>> (double &d)
 Read sizeof(double) bytes into d with read().
 
MyFILE & operator>> (bool &d)
 Read sizeof(bool) bytes into d with read().
 
MyFILE & operator<< (unsigned __int8 c)
 Write the sizeof(unsigned __int8) bytes of c with write().
 
MyFILE & operator<< (__int8 c)
 Write the sizeof(__int8) bytes of c with write().
 
MyFILE & operator<< (unsigned __int16 l)
 Write the sizeof(unsigned __int16) bytes of l with write().
 
MyFILE & operator<< (unsigned __int32 l)
 Write the sizeof(unsigned __int32) bytes of l with write().
 
MyFILE & operator<< (unsigned __int64 l)
 Write the sizeof(unsigned __int64) bytes of l with write().
 
MyFILE & operator<< (__int16 s)
 Write the sizeof(__int16) bytes of s with write().
 
MyFILE & operator<< (__int32 l)
 Write the sizeof(__int32) bytes of l with write().
 
MyFILE & operator<< (__int64 l)
 Write the sizeof(__int64) bytes of l with write().
 
MyFILE & operator<< (double d)
 Write the sizeof(double) bytes of d with write().
 
MyFILE & operator<< (bool d)
 Write the sizeof(bool) bytes of d with write().
 

Public Attributes

FILE * file
 Underlying C FILE, or 0 when no file is open.
 
__int32 error
 Set to 1 on failures (open, close without a file, short read, failed seek); cleared only by the constructor and destructor.
 
bool eof
 Set when a short read reaches the end of the file; cleared by the constructor, open() and close().
 
unsigned __int64 filesize
 File size: measured by open() in read mode, or the largest position written in write mode; 0 after close().
 

Detailed Description

Small binary file wrapper around a C FILE: open() for reading or writing (chosen at construction), raw read()/write(), and >> / << operators that read or write the bytes of basic types.

It keeps its own position and file size and reports problems through error and eof instead of exceptions.

Constructor & Destructor Documentation

◆ MyFILE()

MyFILE::MyFILE ( bool  mode_reading_)
inline

Set the mode (true reads, false writes) and clear error, eof, position and filesize; no file is open.

◆ ~MyFILE()

MyFILE::~MyFILE ( )
inline

Call close() (which sets error to 1 if no file was open), then reset error and eof.

Member Function Documentation

◆ close()

void MyFILE::close ( )
inline

Close the file if one is open (otherwise set error to 1), then reset position, filesize and eof.

◆ flush()

void MyFILE::flush ( )
inline

Call fflush() on the file.

◆ open()

bool MyFILE::open ( __int8 *  name)
inline

Open name with fopen(): "rb" in read mode, recording the file size by seeking to the end and back, or "wb" in write mode.

Returns false and sets error to 1 if a file is already open or fopen() fails; otherwise true.

◆ operator<<() [1/10]

MyFILE & MyFILE::operator<< ( __int16  s)
inline

Write the sizeof(__int16) bytes of s with write().

Returns
*this.

◆ operator<<() [2/10]

MyFILE & MyFILE::operator<< ( __int32  l)
inline

Write the sizeof(__int32) bytes of l with write().

Returns
*this.

◆ operator<<() [3/10]

MyFILE & MyFILE::operator<< ( __int64  l)
inline

Write the sizeof(__int64) bytes of l with write().

Returns
*this.

◆ operator<<() [4/10]

MyFILE & MyFILE::operator<< ( __int8  c)
inline

Write the sizeof(__int8) bytes of c with write().

Returns
*this.

◆ operator<<() [5/10]

MyFILE & MyFILE::operator<< ( bool  d)
inline

Write the sizeof(bool) bytes of d with write().

Returns
*this.

◆ operator<<() [6/10]

MyFILE & MyFILE::operator<< ( double  d)
inline

Write the sizeof(double) bytes of d with write().

Returns
*this.

◆ operator<<() [7/10]

MyFILE & MyFILE::operator<< ( unsigned __int16  l)
inline

Write the sizeof(unsigned __int16) bytes of l with write().

Returns
*this.

◆ operator<<() [8/10]

MyFILE & MyFILE::operator<< ( unsigned __int32  l)
inline

Write the sizeof(unsigned __int32) bytes of l with write().

Returns
*this.

◆ operator<<() [9/10]

MyFILE & MyFILE::operator<< ( unsigned __int64  l)
inline

Write the sizeof(unsigned __int64) bytes of l with write().

Returns
*this.

◆ operator<<() [10/10]

MyFILE & MyFILE::operator<< ( unsigned __int8  c)
inline

Write the sizeof(unsigned __int8) bytes of c with write().

Returns
*this.

◆ operator>>() [1/10]

MyFILE & MyFILE::operator>> ( __int16 &  s)
inline

Read sizeof(__int16) bytes into s with read().

Returns
*this.

◆ operator>>() [2/10]

MyFILE & MyFILE::operator>> ( __int32 &  l)
inline

Read sizeof(__int32) bytes into l with read().

Returns
*this.

◆ operator>>() [3/10]

MyFILE & MyFILE::operator>> ( __int64 &  l)
inline

Read sizeof(__int64) bytes into l with read().

Returns
*this.

◆ operator>>() [4/10]

MyFILE & MyFILE::operator>> ( __int8 &  c)
inline

Read sizeof(__int8) bytes into c with read().

Returns
*this.

◆ operator>>() [5/10]

MyFILE & MyFILE::operator>> ( bool &  d)
inline

Read sizeof(bool) bytes into d with read().

Returns
*this.

◆ operator>>() [6/10]

MyFILE & MyFILE::operator>> ( double &  d)
inline

Read sizeof(double) bytes into d with read().

Returns
*this.

◆ operator>>() [7/10]

MyFILE & MyFILE::operator>> ( unsigned __int16 &  l)
inline

Read sizeof(unsigned __int16) bytes into l with read().

Returns
*this.

◆ operator>>() [8/10]

MyFILE & MyFILE::operator>> ( unsigned __int32 &  l)
inline

Read sizeof(unsigned __int32) bytes into l with read().

Returns
*this.

◆ operator>>() [9/10]

MyFILE & MyFILE::operator>> ( unsigned __int64 &  l)
inline

Read sizeof(unsigned __int64) bytes into l with read().

Returns
*this.

◆ operator>>() [10/10]

MyFILE & MyFILE::operator>> ( unsigned __int8 &  c)
inline

Read sizeof(unsigned __int8) bytes into c with read().

Returns
*this.

◆ read() [1/2]

void MyFILE::read ( __int8 *  string,
__int32  length_bytes 
)
inline

Read length_bytes bytes into string with fread().

On a short read set error to 1, and eof if the end of the file was reached; the position always advances by length_bytes.

◆ read() [2/2]

void MyFILE::read ( unsigned __int32 *  dest,
__int32  length_bytes 
)
inline

Read length_bytes bytes into dest with fread().

On a short read set error to 1, and eof if the end of the file was reached; the position always advances by length_bytes.

◆ seek()

void MyFILE::seek ( unsigned __int64  pos)

fseeko() to pos from the start of the file; on success set the position to pos, otherwise set error to 1.

Defined in LMF_IO.cc.

◆ tell()

unsigned __int64 MyFILE::tell ( )
inline

Return the position kept by this class (bytes read or written since open, or the last seek()).

◆ write()

void MyFILE::write ( const __int8 *  string,
__int32  length 
)
inline

Write length bytes from string with fwrite() (the result is not checked), advance the position and raise filesize to it if needed.

Member Data Documentation

◆ eof

bool MyFILE::eof

Set when a short read reaches the end of the file; cleared by the constructor, open() and close().

◆ error

__int32 MyFILE::error

Set to 1 on failures (open, close without a file, short read, failed seek); cleared only by the constructor and destructor.

◆ file

FILE* MyFILE::file

Underlying C FILE, or 0 when no file is open.

◆ filesize

unsigned __int64 MyFILE::filesize

File size: measured by open() in read mode, or the largest position written in write mode; 0 after close().


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