Marine Library  1.0
C++ library for Linux Networking Development
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
CFile Class Reference

Regular file operations. More...

#include <file.hh>

Inheritance diagram for CFile:
Inheritance graph
[legend]
Collaboration diagram for CFile:
Collaboration graph
[legend]

Public Member Functions

 CFile ()
 Default constructor.
 
int fdType () const
 Get type id of this object. More...
 
const char * fdTypeName () const
 Get readable description of this object. More...
 
virtual bool unlink ()
 Remove file. More...
 
bool rename (const std::string &newfile)
 Rename file. More...
 
std::string toString () const
 Get readable description of this object. More...
 
Open or create a file

mode is useful only when a new file is created.

Parameters
pathnameFile pathname
flagsFlags for open(2), e.g. O_RDONLY, O_WRONLY, or O_RDWR
modeMode for open(2) when new file is created, e.g. S_IRUSR
See also
ErrMsg
 CFile (const char *pathname, int flags=kFlagsDefault, mode_t mode=kModeDefault)
 
 CFile (const std::string &pathname, int flags=kFlagsDefault, mode_t mode=kModeDefault)
 
virtual bool open (const char *pathname, int flags=kFlagsDefault, mode_t mode=kModeDefault)
 
virtual bool open (const std::string &pathname, int flags=kFlagsDefault, mode_t mode=kModeDefault)
 
Duplicate file descriptor

These functions do NOT copy file content, it only duplicates fd using dup(2).

Parameters
otherAnother file object
 CFile (const CFile &other)
 
CFileoperator= (const CFile &other)
 
Read from file

These functions read at most size bytes from file.

Parameters
[out]bufA buffer to receive the data
[in]sizeSize of bytes to read at most
[in]append
  • true: New data will append to buf
  • false: New data will overwrite old ones in buf
See also
ErrMsg
ssize_t read (char *buf, size_t size)
 
bool read (std::vector< char > &buf, size_t size, bool append)
 
bool read (std::string &buf, size_t size, bool append)
 
Write to file
Parameters
bufA buffer to write
sizeBytes size of buf
Returns
  • -1: Failed
  • Non-negative number: Size of bytes actually written
See also
ErrMsg
ssize_t write (const char *buf, size_t size)
 
ssize_t write (const std::vector< char > &buf)
 
ssize_t write (const std::string &buf)
 
Set cursor

See lseek(2) for more information.

Parameters
offsetOffset to move the cursor
whenceDirective for offset, showing as follows:
Directive Explanation
SEEK_SET The offset is set to offset bytes
SEEK_CUR The offset is set to its current location plus offset bytes
SEEK_END The offset is set to the size of the file plus offset bytes
Returns
true if succeeded; false otherwise
See also
ErrMsg
bool seek (off_t offset, int whence)
 
bool seek64 (off64_t offset, int whence)
 
Get cursor
Returns
  • -1: Failed
  • Non-negative number: Current cursor position
See also
ErrMsg
off_t tell () const
 
off64_t tell64 () const
 
- Public Member Functions inherited from IFileDesc
 IFileDesc ()
 Construct a default object.
 
virtual ~IFileDesc ()
 Destroy this object. More...
 
bool valid () const
 Test if this object is a valid file descriptor. More...
 
int fd () const
 Get fd (file descriptor). More...
 
std::string filename () const
 Get file name opened by this object. More...
 
off_t length () const
 Get byte size of file opened by this object. More...
 
bool deleted () const
 Test if file is deleted. More...
 
bool block (bool on)
 Set block/non-block for operations. More...
 
void close ()
 Close this fd.
 

Static Public Member Functions

static bool Unlink (const std::string &pathname)
 Remove a file. More...
 
static bool Rename (const std::string &oldfile, const std::string &newfile)
 Rename a file. More...
 
- Static Public Member Functions inherited from IFileDesc
static std::string ErrMsg ()
 Get latest errno and error message. More...
 

Static Public Attributes

static const int kFdType = 1
 Type id.
 
- Static Public Attributes inherited from IFileDesc
static const int kInvalidFd = -1
 

Detailed Description

Regular file operations.

Member Function Documentation

int CFile::fdType ( ) const
inlinevirtual

Get type id of this object.

Any concrete class derived from IFileDesc shall define a distinct type id named kFdType, so it's possible to check this id and determine what the object really is.
Currently following kFdTypes are defined:

Type Id Value
CFile::kFdType 1
CTcpConnSocket::kFdType 2
CListenSocket::kFdType 3
CUdpSocket::kFdType 4
CPosixShmFile::kFdType 5
CEpoll::kFdType 6
Returns
Type id of this object
See also
kFdType

Implements IFileDesc.

Reimplemented in CPosixShmFile.

const char* CFile::fdTypeName ( ) const
inlinevirtual

Get readable description of this object.

Returns
"CFile"

Implements IFileDesc.

Reimplemented in CPosixShmFile.

ssize_t CFile::read ( char *  buf,
size_t  size 
)
inline
Returns
  • -1: Failed
  • 0: The end of file is reached
  • Positive number: Size of bytes actually read
bool CFile::read ( std::vector< char > &  buf,
size_t  size,
bool  append 
)
inline
Returns
true if succeeded; false otherwise
bool CFile::read ( std::string &  buf,
size_t  size,
bool  append 
)
inline
Returns
true if succeeded; false otherwise
static bool CFile::Rename ( const std::string &  oldfile,
const std::string &  newfile 
)
inlinestatic

Rename a file.

Parameters
oldfileOld pathname
newfileNew pathname
Returns
true if succeeded; false otherwise
See also
ErrMsg
bool CFile::rename ( const std::string &  newfile)
inline

Rename file.

Parameters
newfileNew pathname for the file
Returns
true if succeeded; false otherwise
See also
ErrMsg
std::string CFile::toString ( ) const
inlinevirtual

Get readable description of this object.

Returns
Description string

Reimplemented from IFileDesc.

Reimplemented in CPosixShmFile.

static bool CFile::Unlink ( const std::string &  pathname)
inlinestatic

Remove a file.

Parameters
pathnameFile pathname
Returns
true if succeeded; false otherwise
See also
ErrMsg
virtual bool CFile::unlink ( )
inlinevirtual

Remove file.

Returns
true if succeeded; false otherwise
See also
ErrMsg

Reimplemented in CPosixShmFile.


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