blocxx
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.
 
 ~FileBuf ()
 Calls close().
 
bool isOpen () const
 
FileBufopen (FILE *fp)
 If isOpen() == true, returns 0, otherwise initializes the FileBuf to use fp.
 
FileBufopen (AutoDescriptor fd)
 If isOpen() == true, returns 0, otherwise initializes the FileBuf to use fd.
 
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.
 
FileBufclose ()
 If isOpen() == false, returns a null pointer.
 
- Public Member Functions inherited from BLOCXX_NAMESPACE::BaseStreamBuffer
 BaseStreamBuffer (EDirectionFlag direction, size_t bufSize=BASE_BUF_SIZE)
 Create a base stream buffer.
 
 ~BaseStreamBuffer ()
 

Private Member Functions

virtual int buffer_to_device (const char *c, int n)
 Writes the buffer to the "device".
 
virtual int buffer_from_device (char *c, int n)
 Fill the buffer from the "device".
 
std::ios_base::openmode posixModeToCppMode (int posixMode)
 Converts POSIX io open mode flags to C++ iostream open mode.
 
int cppModeToPOSIXMode (std::ios_base::openmode cppMode)
 Converts C++ iostream open mode to POSIX io open mode.
 
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)
 
 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 58 of file FileBuf.hpp.

Constructor & Destructor Documentation

◆ FileBuf() [1/2]

BLOCXX_NAMESPACE::FileBuf::FileBuf ( )

◆ ~FileBuf()

BLOCXX_NAMESPACE::FileBuf::~FileBuf ( )

Calls close().

Definition at line 62 of file FileBuf.cpp.

References close().

◆ FileBuf() [2/2]

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

References FileBuf().

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 206 of file FileBuf.cpp.

References BLOCXX_THROW_ERRNO_MSG, isOpen(), and m_file.

◆ 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 192 of file FileBuf.cpp.

References m_file.

◆ 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 181 of file FileBuf.cpp.

References isOpen(), and m_file.

Referenced by BLOCXX_NAMESPACE::IFileStream::close(), and ~FileBuf().

◆ 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 280 of file FileBuf.cpp.

Referenced by open(), and open().

◆ 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 252 of file FileBuf.cpp.

Referenced by open().

◆ 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 68 of file FileBuf.cpp.

References m_file.

Referenced by buffer_from_device(), close(), BLOCXX_NAMESPACE::IFileStream::isOpen(), open(), open(), and open().

◆ 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 86 of file FileBuf.cpp.

References cppModeToCMode(), BLOCXX_NAMESPACE::AutoResource< Policy >::get(), isOpen(), m_file, posixModeToCppMode(), and BLOCXX_NAMESPACE::AutoResource< Policy >::release().

◆ 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 146 of file FileBuf.cpp.

References BLOCXX_INVALID_HANDLE, cppModeToCMode(), cppModeToPOSIXMode(), BLOCXX_NAMESPACE::AutoResource< Policy >::get(), isOpen(), m_file, open(), and BLOCXX_NAMESPACE::AutoResource< Policy >::release().

◆ 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 74 of file FileBuf.cpp.

References isOpen(), and m_file.

Referenced by open(), BLOCXX_NAMESPACE::IFileStream::open(), BLOCXX_NAMESPACE::IFileStream::open(), and BLOCXX_NAMESPACE::IFileStream::open().

◆ operator=()

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

References FileBuf().

◆ 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 235 of file FileBuf.cpp.

Referenced by open().

Member Data Documentation

◆ m_file

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

Definition at line 183 of file FileBuf.hpp.

Referenced by buffer_from_device(), buffer_to_device(), close(), FileBuf(), isOpen(), open(), open(), and open().


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