blocxx
Public Member Functions | Private Member Functions | Private Attributes

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

List of all members.

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.

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

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

usr src packages BUILD blocxx src blocxx FileBuf cpp BLOCXX_NAMESPACE::FileBuf::FileBuf ( )

Constructor.

isOpen() == false

Definition at line 57 of file FileBuf.cpp.

BLOCXX_NAMESPACE::FileBuf::~FileBuf ( )

Calls close().

Definition at line 63 of file FileBuf.cpp.

References close().

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

Member Function Documentation

int BLOCXX_NAMESPACE::FileBuf::buffer_from_device ( char *  c,
int  n 
) [private, virtual]

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

References BLOCXX_THROW_ERRNO_MSG, isOpen(), and m_file.

int BLOCXX_NAMESPACE::FileBuf::buffer_to_device ( const char *  c,
int  n 
) [private, virtual]

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

References m_file.

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.

Reimplemented in BLOCXX_NAMESPACE::IFileStream.

Definition at line 182 of file FileBuf.cpp.

References isOpen(), and m_file.

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

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

Referenced by open().

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

Referenced by open().

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.

Reimplemented in BLOCXX_NAMESPACE::IFileStream.

Definition at line 69 of file FileBuf.cpp.

References m_file.

Referenced by buffer_from_device(), close(), and open().

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.

Reimplemented in BLOCXX_NAMESPACE::IFileStream.

Definition at line 87 of file FileBuf.cpp.

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

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.

Reimplemented in BLOCXX_NAMESPACE::IFileStream.

Definition at line 147 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().

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.

Reimplemented in BLOCXX_NAMESPACE::IFileStream.

Definition at line 75 of file FileBuf.cpp.

References isOpen(), and m_file.

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

FileBuf& BLOCXX_NAMESPACE::FileBuf::operator= ( const FileBuf arg) [private]
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 236 of file FileBuf.cpp.

Referenced by open().


Member Data Documentation

Definition at line 183 of file FileBuf.hpp.

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


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