blocxx
Public Member Functions | Private Member Functions | Private Attributes | List of all members
BLOCXX_NAMESPACE::FileBuf Class Reference

The purpose of this class is to provide an alternative implementation of std::filebuf that is based on a FILE*, and can be created using a FILE*, a raw file descriptor or an instance of blocxx::File. More...

#include <FileBuf.hpp>

Inheritance diagram for BLOCXX_NAMESPACE::FileBuf:
BLOCXX_NAMESPACE::BaseStreamBuffer BLOCXX_NAMESPACE::IFileStream

Public Member Functions

 FileBuf ()
 Constructor. More...
 
 ~FileBuf ()
 Calls close(). More...
 
bool isOpen () const
 
FileBufopen (FILE *fp)
 If isOpen() == true, returns 0, otherwise initializes the FileBuf to use fp. More...
 
FileBufopen (AutoDescriptor fd)
 If isOpen() == true, returns 0, otherwise initializes the FileBuf to use fd. More...
 
FileBufopen (const char *path, std::ios_base::openmode mode, mode_t permissions=0666)
 If isOpen() == true, returns 0, otherwise initializes the FileBuf by opening the file identified by path. More...
 
FileBufclose ()
 If isOpen() == false, returns a null pointer. More...
 
- Public Member Functions inherited from BLOCXX_NAMESPACE::BaseStreamBuffer
 BaseStreamBuffer (EDirectionFlag direction, size_t bufSize=BASE_BUF_SIZE)
 Create a base stream buffer. More...
 
 ~BaseStreamBuffer ()
 

Private Member Functions

virtual int buffer_to_device (const char *c, int n)
 Writes the buffer to the "device". More...
 
virtual int buffer_from_device (char *c, int n)
 Fill the buffer from the "device". More...
 
std::ios_base::openmode posixModeToCppMode (int posixMode)
 Converts POSIX io open mode flags to C++ iostream open mode. More...
 
int cppModeToPOSIXMode (std::ios_base::openmode cppMode)
 Converts C++ iostream open mode to POSIX io open mode. More...
 
const char * cppModeToCMode (std::ios_base::openmode cppMode)
 Converts C++ iostream open mode to C stdio file open mode or the empty string on error (never returns null) More...
 
 FileBuf (const FileBuf &arg)
 
FileBufoperator= (const FileBuf &arg)
 

Private Attributes

::FILE * m_file
 

Additional Inherited Members

- Public Types inherited from BLOCXX_NAMESPACE::BaseStreamBuffer
enum  EDirectionFlag { E_IN, E_OUT, E_IN_OUT }
 
- Protected Member Functions inherited from BLOCXX_NAMESPACE::BaseStreamBuffer
int underflow ()
 
std::streamsize xsputn (const char *s, std::streamsize n)
 
virtual int overflow (int c)
 
virtual int sync ()
 
virtual void initBuffers ()
 
virtual void initGetBuffer ()
 
virtual void initPutBuffer ()
 

Detailed Description

The purpose of this class is to provide an alternative implementation of std::filebuf that is based on a FILE*, and can be created using a FILE*, a raw file descriptor or an instance of blocxx::File.

FileBuf does not support all the operations that a std::filebuf does. FileBuf does not support seeking, character putback, facets, i18n. Basically all it does is support reading and writing.

Definition at line 88 of file FileBuf.hpp.

Constructor & Destructor Documentation

◆ FileBuf() [1/2]

BLOCXX_NAMESPACE::FileBuf::FileBuf ( )

Constructor.

@postcondition isOpen() == false

Definition at line 86 of file FileBuf.cpp.

References isOpen().

◆ ~FileBuf()

BLOCXX_NAMESPACE::FileBuf::~FileBuf ( )

Calls close().

Definition at line 92 of file FileBuf.cpp.

◆ FileBuf() [2/2]

BLOCXX_NAMESPACE::FileBuf::FileBuf ( const FileBuf arg)
private

Member Function Documentation

◆ buffer_from_device()

int BLOCXX_NAMESPACE::FileBuf::buffer_from_device ( char *  c,
int  n 
)
privatevirtual

Fill the buffer from the "device".

Parameters
cA pointer to the beginning of the buffer
nThe number of bytes to be read into the buffer.
Returns
-1 if no bytes are able to be read from the "device" (for instance, end of input stream). Otherwise, return the number of bytes read into the buffer.
Exceptions
IOExceptionon failure.

Reimplemented from BLOCXX_NAMESPACE::BaseStreamBuffer.

Definition at line 236 of file FileBuf.cpp.

◆ buffer_to_device()

int BLOCXX_NAMESPACE::FileBuf::buffer_to_device ( const char *  c,
int  n 
)
privatevirtual

Writes the buffer to the "device".

Parameters
cA pointer to the start of the buffer
nthe number of bytes to write
Returns
-1 if error, 0 if the entire buffer was written.

Reimplemented from BLOCXX_NAMESPACE::BaseStreamBuffer.

Definition at line 222 of file FileBuf.cpp.

◆ close()

FileBuf * BLOCXX_NAMESPACE::FileBuf::close ( )

If isOpen() == false, returns a null pointer.

If a put area exists, calls overflow(EOF) to flush characters. Finally it closes the file. If any of the calls to overflow or std::fclose fails then close fails.

Postcondition
isOpen() == false.
Returns
FileBuf*: this if successful, 0 otherwise.

Definition at line 211 of file FileBuf.cpp.

◆ cppModeToCMode()

const char * BLOCXX_NAMESPACE::FileBuf::cppModeToCMode ( std::ios_base::openmode  cppMode)
private

Converts C++ iostream open mode to C stdio file open mode or the empty string on error (never returns null)

Parameters
cppModeC++ iostream mode
Returns
const char* corresponding C stdio file open mode or empty string on error.

Definition at line 310 of file FileBuf.cpp.

◆ cppModeToPOSIXMode()

int BLOCXX_NAMESPACE::FileBuf::cppModeToPOSIXMode ( std::ios_base::openmode  cppMode)
private

Converts C++ iostream open mode to POSIX io open mode.

Parameters
cppModeThe C++ iostream open mode flags.
Returns
int The corresponding POSIX io open mode flags.

Definition at line 282 of file FileBuf.cpp.

◆ isOpen()

bool BLOCXX_NAMESPACE::FileBuf::isOpen ( ) const
Returns
bool: true if a previous call to open succeeded (returned a non-null value) and there has been no intervening call to close.

Definition at line 98 of file FileBuf.cpp.

Referenced by FileBuf().

◆ open() [1/3]

FileBuf * BLOCXX_NAMESPACE::FileBuf::open ( AutoDescriptor  fd)

If isOpen() == true, returns 0, otherwise initializes the FileBuf to use fd.

Parameters
fdThe descriptor to use. The FileBuf will take ownership of fd, so close() will close the descriptor.
Returns
FileBuf*: this if successful, 0 otherwise.

Definition at line 116 of file FileBuf.cpp.

◆ open() [2/3]

FileBuf * BLOCXX_NAMESPACE::FileBuf::open ( const char *  path,
std::ios_base::openmode  mode,
mode_t  permissions = 0666 
)

If isOpen() == true, returns 0, otherwise initializes the FileBuf by opening the file identified by path.

If the open operation succeeds and (mode & ios_base::ate) != 0, positions the file to the end. If the repositioning operation fails, calls close() and returns a null pointer to indicate failure.

Parameters
pathA null-terminated string identifying the file to open.
modeA bitmask of the file open mode flags defined in ios_base. Invalid combinations cause the operation to fail.
permissionsSpecifies the permissions to use in case a new file is created. It is modified by the process's umask in the usual way: the permissions of the created file are (mode & ~umask). Note that this mode only applies to future accesses of the newly created file; the open call that creates a read-only file may well return a read/write file descriptor.
Returns
FileBuf*: this if successful, 0 otherwise.

Definition at line 176 of file FileBuf.cpp.

◆ open() [3/3]

FileBuf * BLOCXX_NAMESPACE::FileBuf::open ( FILE *  fp)

If isOpen() == true, returns 0, otherwise initializes the FileBuf to use fp.

Parameters
fpThe FILE* to use. The FileBuf will take ownership of fp, so close() will call std::fclose() on fp.
Returns
FileBuf*: this if successful, 0 otherwise.

Definition at line 104 of file FileBuf.cpp.

◆ operator=()

FileBuf& BLOCXX_NAMESPACE::FileBuf::operator= ( const FileBuf arg)
private

◆ posixModeToCppMode()

std::ios_base::openmode BLOCXX_NAMESPACE::FileBuf::posixModeToCppMode ( int  posixMode)
private

Converts POSIX io open mode flags to C++ iostream open mode.

Parameters
posixModeThe POSIX io open mode flags.
Returns
std::ios_base::openmode C++ iostream open mode.

Definition at line 265 of file FileBuf.cpp.

Member Data Documentation

◆ m_file

::FILE* BLOCXX_NAMESPACE::FileBuf::m_file
private

Definition at line 243 of file FileBuf.hpp.


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