Scopira
20080306
|
#include <fileflow.h>
Public Types | |
enum | { append_c = 1024, trunc_c = 2048, linked_c = 4096, existing_c = 8192 } |
access levels More... | |
enum | { stdin_c = -1, stdout_c = -2, stderr_c = -3 } |
special file descriptor (fd) numbers More... | |
enum | seek_dir_t { seek_start_c = 0, seek_end_c = 1, seek_cur_c } |
typedef ::off_t | offset_t |
Public Types inherited from scopira::tool::flow_i | |
enum | { input_c = 1, output_c = 2 } |
typedef scopira::tool::byte_t | byte_t |
typedef int | mode_t |
Public Member Functions | |
fileflow (void) | |
fileflow (const std::string &filename, mode_t mode) | |
fileflow (int fd, mode_t mode) | |
virtual bool | failed (void) const |
virtual size_t | read (byte_t *_buf, size_t _maxsize) |
virtual size_t | write (const byte_t *_buf, size_t _size) |
void | open (const std::string &filename, mode_t mode) |
void | open (int fd, mode_t mode) |
void | close (void) |
int | get_os_handle (void) const |
offset_t | seek (offset_t howmuch, seek_dir_t dir=seek_start_c) |
seekers | |
void | seek_reset (void) |
seeks to the start | |
offset_t | tell (void) const |
tells the current location of the file pointer | |
bool | resize (offset_t newsz) |
void | read_lock (void) |
bool | try_read_lock (void) |
void | write_lock (void) |
bool | try_write_lock (void) |
void | unlock (void) |
Public Member Functions inherited from scopira::tool::iflow_i | |
virtual size_t | read_byte (byte_t &out) |
template<class TT > | |
size_t | read_array (TT *_buf, size_t _numelem) |
size_t | read_void (void *_buf, size_t _maxsize) |
Public Member Functions inherited from scopira::tool::object | |
virtual | ~object () |
int | add_ref (void) const |
bool | sub_ref (void) const |
void | auto_ref (void) const |
int | current_ref (void) const |
virtual scopira::tool::oflow_i & | print (scopira::tool::oflow_i &o) const |
virtual bool | load (scopira::tool::iobjflow_i &in) |
virtual void | save (scopira::tool::oobjflow_i &out) const |
bool | is_alive_object (void) const |
bool | is_dead_object (void) const |
Public Member Functions inherited from scopira::tool::oflow_i | |
virtual size_t | write_byte (byte_t b) |
template<class TT > | |
size_t | write_array (const TT *_buf, size_t _numelem) |
size_t | write_void (const void *_buf, size_t _size) |
Protected Attributes | |
mode_t | dm_mode |
current modes, 0 for no-file | |
int | dm_hand |
the file handle | |
bool | dm_fail |
currently failed? | |
Additional Inherited Members | |
Protected Member Functions inherited from scopira::tool::object | |
object (void) | |
object (bool neverusecounter) | |
The is a binary/raw flow stream object that reads and writes files to disk.
This class is often the base of many chains, and can also be used by itself for basic file reading and writing.
anonymous enum |
anonymous enum |
scopira::tool::fileflow::fileflow | ( | void | ) |
The default constructor.
You should then use open() to open a file.
scopira::tool::fileflow::fileflow | ( | const std::string & | filename, |
mode_t | mode | ||
) |
A constructor that also opens a file. See the corresponding open() call for parameter information.
scopira::tool::fileflow::fileflow | ( | int | fd, |
mode_t | mode | ||
) |
A constructor that also opens a file. See the corresponding open() call for parameter information.
fd | the file descriptor (OS specific) |
mode | the file mode to use |
void scopira::tool::fileflow::close | ( | void | ) |
Close the currently opened file.
You normally do not need to call this as the file will be closed automatically if you:
Referenced by scopira::tool::filememory::c_array().
|
virtual |
Check if the file stream is in a failed state.
Implements scopira::tool::flow_i.
void scopira::tool::fileflow::open | ( | const std::string & | filename, |
mode_t | mode | ||
) |
Opens the given on disk file.
The mode is a bitmask (or-ed) of file modes.
It usually contains either (or both) of:
And possible some of:
Use failed() to check if the open was successfull.
filename | is the filename |
mode | is the file mode |
Referenced by scopira::tool::filememory::c_array().
void scopira::tool::fileflow::open | ( | int | fd, |
mode_t | mode | ||
) |
Opens a file via an existing OS file descriptor. File descriptors are typically low level OS objects that you typically do not want to use. This method is provided for the few times users would want streams that reflect into standard OS file descriptors.
You can either pass it a file descriptor (fd) or one of the standard (std*_c) file descriptors. If you pass one of the standard streams (std*._c), mode is ignored)
Use failed() to check if the open was successfull.
fd | the file descriptor (OS specific) |
mode | the file mode to use |
|
virtual |
Reads a raw array of bytes from the file.
_buf | the buffer to read into. |
_maxsize | the maximum number of bytes to read |
Implements scopira::tool::iflow_i.
void scopira::tool::fileflow::read_lock | ( | void | ) |
Locks the (whole) file for (shared) reading. Blocks until the lock is acquired. Don't forget to unlock! File must be open for reading!
Referenced by scopira::tool::read_flocker::read_flocker(), and seek_reset().
bool scopira::tool::fileflow::resize | ( | offset_t | newsz | ) |
bool scopira::tool::fileflow::try_read_lock | ( | void | ) |
Checks if a read lock is doable, right now. Returns true if a read lock is doable. Never blocks. File must be open for reading!
Referenced by seek_reset().
bool scopira::tool::fileflow::try_write_lock | ( | void | ) |
Checks if a write lock is doable, right now. Returns true if a write lock is doable. Never blocks. File must be open for writing!
Referenced by seek_reset().
void scopira::tool::fileflow::unlock | ( | void | ) |
Unlocks the current write or read lock. File must be open!
Referenced by seek_reset(), scopira::tool::read_flocker::~read_flocker(), and scopira::tool::write_flocker::~write_flocker().
|
virtual |
Writes a raw array of bytes from the file.
_buf | the buffer to write from. |
_size | the size of the buffer |
Implements scopira::tool::oflow_i.
void scopira::tool::fileflow::write_lock | ( | void | ) |
Locks the (whole) file for (exclusive) writing. Blocks until the lock is acquired. Don't forget to unlock! File must be open for writing!
Referenced by seek_reset(), and scopira::tool::write_flocker::write_flocker().