UCommon
|
A generic file streaming class built from the buffer protocol. More...
#include <buffer.h>
Public Member Functions | |
void | cancel (void) |
void | close (void) |
Close the file, flush buffers. | |
void | create (const char *path, fsys::access_t access=fsys::ACCESS_APPEND, unsigned permissions=0640, size_t size=512) |
Create and open the specified file. | |
fbuf () | |
Construct an unopened file buffer. | |
fbuf (const char *path, fsys::access_t access, unsigned permissions, size_t size) | |
Construct a file buffer that creates and opens a specific file. | |
fbuf (const char *path, fsys::access_t access, char **argv, size_t size, char **envp=((void *) 0)) | |
Construct a pipe buffer from an existing file. | |
fbuf (const char *path, fsys::access_t access, size_t size) | |
Construct a file buffer that opens an existing file. | |
void | open (const char *path, fsys::access_t access=fsys::ACCESS_RDWR, size_t size=512) |
Construct a file buffer that opens an existing file. | |
void | open (const char *path, fsys::access_t access, char **argv, size_t size=512, char **envp=((void *) 0)) |
Construct a pipe buffer from an existing file. | |
bool | seek (offset_t offset) |
Seek specific offset in open file and reset I/O buffers. | |
offset_t | tell (void) |
Give the current position in the currently open file. | |
void | terminate (void) |
Force terminate child and close. | |
bool | trunc (offset_t offset) |
Truncate the currently open file to a specific position. | |
~fbuf () | |
Destroy object and release all resources. | |
![]() | |
int | _getch (void) |
Get a character from the buffer. | |
int | _putch (int ch) |
Put a character into the buffer. | |
bool | eof (void) |
Check if at end of input. | |
int | err (void) |
bool | flush (void) |
Flush buffered memory to physical I/O. | |
size_t | get (char *address, size_t count) |
Get memory from the buffer. | |
size_t | getline (char *string, size_t size) |
Get text as a line of input from the buffer. | |
size_t | getline (string &buffer) |
Get a string as a line of input from the buffer. | |
bool | is_input (void) |
See if input active. | |
bool | is_open (void) |
See if buffer open. | |
bool | is_output (void) |
See if output active. | |
bool | is_pending (void) |
See if pending input. | |
operator bool () | |
See if buffer open. | |
bool | operator! () |
See if buffer closed. | |
size_t | printf (const char *format,...) |
Print formatted string to the buffer. | |
void | purge (void) |
Purge any pending input or output buffer data. | |
size_t | put (const char *address, size_t count=0) |
Put memory into the buffer. | |
size_t | putline (const char *string) |
Put a string as a line of output to the buffer. | |
void | reset (void) |
Reset input buffer state. | |
void | seteof (void) |
Set eof flag. | |
![]() | |
int | get (void) |
Get the next character. | |
int | put (int code) |
Put the next character. |
Protected Member Functions | |
void | _clear (void) |
int | _err (void) const |
size_t | _pull (char *address, size_t size) |
size_t | _push (const char *address, size_t size) |
fd_t | getfile (void) |
![]() | |
virtual bool | _blocking (void) |
Return true if blocking. | |
virtual void | _clear (void)=0 |
Method to clear low level i/o error. | |
virtual int | _err (void) const =0 |
Method to get low level i/o error. | |
virtual bool | _flush (void) |
Flush buffer to physical i/o. | |
virtual bool | _pending (void) |
Check if data is pending. | |
virtual size_t | _pull (char *address, size_t size)=0 |
Method to pull buffer from physical i/o (read). | |
virtual size_t | _push (const char *address, size_t size)=0 |
Method to push buffer into physical i/o (write). | |
void | allocate (size_t size, type_t access=BUF_RDWR) |
Allocate I/O buffer(s) of specified size. | |
BufferProtocol () | |
Construct an empty (unallocated) buffer. | |
BufferProtocol (size_t size, type_t access=BUF_RDWR) | |
Construct a buffer of pre-allocated size and access type. | |
char * | gather (size_t size) |
Gather returns a pointer to contiguous input of specified size. | |
void | release (void) |
Release (free) buffer memory. | |
char * | request (size_t size) |
Request workspace in output buffer. | |
void | seteol (const char *string) |
Set end of line marker. | |
size_t | unread (void) |
Get current input position. | |
size_t | unsaved (void) |
Get current output position. | |
virtual | ~BufferProtocol () |
Destroy object by releasing buffer memory. |
Additional Inherited Members | |
![]() | |
enum | type_t { BUF_RD, BUF_WR, BUF_RDWR } |
![]() | |
const char * | format |
![]() | |
enum | access_t { ACCESS_RDONLY, ACCESS_WRONLY, ACCESS_REWRITE, ACCESS_RDWR = ACCESS_REWRITE, ACCESS_APPEND, ACCESS_SHARED, ACCESS_DIRECTORY, ACCESS_STREAM, ACCESS_RANDOM } |
Enumerated file access modes. | |
typedef struct stat | fileinfo_t |
typedef long | offset_t |
File offset type. | |
![]() | |
void | assign (fd_t descriptor) |
Assign descriptor directly. | |
void | close (void) |
Close a fsys resource. | |
void | create (const char *path, access_t access, unsigned mode) |
Open a file descriptor directly. | |
int | drop (offset_t size=0) |
Drop cached data from start of file. | |
int | err (void) const |
Get last error. | |
int | fileinfo (fileinfo_t *buffer) |
Get status of open descriptor. | |
fsys () | |
Construct an unattached fsys descriptor. | |
fsys (fd_t handle) | |
Contruct fsys from raw file handle. | |
fsys (const fsys &descriptor) | |
Copy (dup) an existing fsys descriptor. | |
fsys (const char *path, access_t access) | |
Create a fsys descriptor by opening an existing file or directory. | |
fsys (const char *path, access_t access, unsigned permission) | |
Create a fsys descriptor by creating a file. | |
fd_t | getHandle (void) const |
Get the native system descriptor handle of the file descriptor. | |
bool | istty (void) |
See if current file stream is a tty device. | |
void | open (const char *path, access_t access) |
Open a file or directory. | |
operator bool () const | |
Test if file descriptor is open. | |
operator fd_t () const | |
Get the descriptor from the object by casting reference. | |
bool | operator! () const |
Test if file descriptor is closed. | |
fd_t | operator* () const |
Get the descriptor from the object by pointer reference. | |
void | operator*= (fd_t &descriptor) |
Replace current file descriptor with an external descriptor. | |
void | operator= (const fsys &descriptor) |
Assign file descriptor by duplicating another descriptor. | |
void | operator= (fd_t descriptor) |
Assing file descriptor from system descriptor. | |
ssize_t | read (void *buffer, size_t count) |
Read data from descriptor or scan directory. | |
fd_t | release (void) |
Release descriptor, do not close. | |
int | seek (offset_t offset) |
Set the position of a file descriptor. | |
void | set (fd_t descriptor) |
Set with external descriptor. | |
int | sync (void) |
Commit changes to the filesystem. | |
int | trunc (offset_t offset) |
Truncate file to specified length. | |
ssize_t | write (const void *buffer, size_t count) |
Write data to descriptor. | |
~fsys () | |
Close and release a file descriptor. | |
![]() | |
static int | access (const char *path, unsigned mode) |
Test path access. | |
static fd_t | append (const char *path) |
Direct means to create or append a writable path and return descriptor. | |
static void | assign (fsys &object, fd_t descriptor) |
Assign a descriptor directly. | |
static int | change (const char *path, unsigned mode) |
Change file access mode. | |
static int | changeDir (const char *path) |
Set directory prefix (chdir). | |
static void | close (fsys &descriptor) |
Close a file descriptor or directory directly. | |
static int | copy (const char *source, const char *target, size_t size=1024) |
Copy a file. | |
static void | create (fsys &object, const char *path, access_t access, unsigned mode) |
create a file descriptor or directory directly. | |
static int | createDir (const char *path, unsigned mode) |
Simple direct method to create a directory. | |
static int | drop (fsys &descriptor, offset_t size) |
Drop cached data from a file descriptor. | |
static int | exec (const char *path, char **argv) |
Execute a process and get exit code. | |
static int | exec (const char *path, char **argv, char **envp) |
static int | fileinfo (const char *path, fileinfo_t *buffer) |
Stat a file. | |
static void * | find (fsys &module, const char *symbol) |
Find symbol in loaded module. | |
static int | getPrefix (char *path, size_t size) |
Get current directory prefix (pwd). | |
static int | hardlink (const char *path, const char *target) |
Create a hard link. | |
static int | inherit (fd_t &descriptor, bool enable) |
Changle inheritable handle. | |
static fd_t | input (const char *path) |
Direct means to open a read-only file path and return a descriptor. | |
static bool | ischar (struct stat *inode) |
static bool | isdev (struct stat *inode) |
static bool | isdir (const char *path) |
Test if path is a directory. | |
static bool | isdir (struct stat *inode) |
static bool | isdisk (struct stat *inode) |
static bool | isfile (const char *path) |
Test if path is a file. | |
static bool | isfile (struct stat *inode) |
static bool | ishidden (const char *path) |
Test if path is a hidden file. | |
static bool | islink (const char *path) |
Test if path is a symlink. | |
static bool | islink (struct stat *inode) |
static bool | issys (struct stat *inode) |
static bool | istty (fd_t fd) |
See if the file handle is a tty device. | |
static int | link (const char *path, const char *target) |
Create a symbolic link. | |
static int | linkinfo (const char *path, char *buffer, size_t size) |
Read a symbolic link to get it's target. | |
static int | load (const char *path) |
Load an unmaged plugin directly. | |
static void | load (fsys &module, const char *path) |
Load a plugin into memory. | |
static fd_t | nullfile (void) |
Create inheritable /dev/null handle. | |
static void | open (fsys &object, const char *path, access_t access) |
Open a file or directory. | |
static fd_t | output (const char *path) |
Direct means to create or access a writable path and return descriptor. | |
static int | pipe (fd_t &input, fd_t &output, size_t size=0) |
Create pipe. | |
static ssize_t | read (fsys &descriptor, void *buffer, size_t count) |
Read data from file descriptor or directory. | |
static void | release (fd_t descriptor) |
Release a file descriptor. | |
static int | remapError (void) |
static int | remove (const char *path) |
Remove a file. | |
static int | removeDir (const char *path) |
Simple direct method to remove a directory. | |
static int | rename (const char *oldpath, const char *newpath) |
Rename a file. | |
static int | seek (fsys &descriptor, offset_t offset) |
Set the position of a file descriptor. | |
static int | unlink (const char *path) |
Remove a symbolic link explicitly. | |
static void | unload (fsys &module) |
unload a specific plugin. | |
static ssize_t | write (fsys &descriptor, const void *buffer, size_t count) |
write data to file descriptor. | |
![]() | |
static const offset_t | end |
Used to mark "append" in set position operations. |
A generic file streaming class built from the buffer protocol.
This can be used in place of fopen based file operations and does not require libstdc++.
ucommon::fbuf::fbuf | ( | const char * | path, |
fsys::access_t | access, | ||
unsigned | permissions, | ||
size_t | size | ||
) |
Construct a file buffer that creates and opens a specific file.
path | of file to create. |
access | mode of file (rw, rdonly, etc). |
permissions | of the newly created file. |
size | of the stream buffer. |
ucommon::fbuf::fbuf | ( | const char * | path, |
fsys::access_t | access, | ||
char ** | argv, | ||
size_t | size, | ||
char ** | envp = ((void *) 0) |
||
) |
Construct a pipe buffer from an existing file.
path | of existing process. |
access | mode of pipe. |
argv | to pass. |
envp | to use. |
ucommon::fbuf::fbuf | ( | const char * | path, |
fsys::access_t | access, | ||
size_t | size | ||
) |
Construct a file buffer that opens an existing file.
path | of existing file to open. |
access | mode of file (rw, rdonly, etc). |
size | of the stream buffer. |
void ucommon::fbuf::close | ( | void | ) |
Close the file, flush buffers.
Capture exit code in error for pipe.
void ucommon::fbuf::create | ( | const char * | path, |
fsys::access_t | access = fsys::ACCESS_APPEND , |
||
unsigned | permissions = 0640 , |
||
size_t | size = 512 |
||
) |
Create and open the specified file.
If a file is currently open, it is closed first.
path | of file to create. |
access | mode of file (rw, rdonly, etc). |
permissions | of the newly created file. |
size | of the stream buffer. |
void ucommon::fbuf::open | ( | const char * | path, |
fsys::access_t | access = fsys::ACCESS_RDWR , |
||
size_t | size = 512 |
||
) |
Construct a file buffer that opens an existing file.
path | of existing file to open. |
access | mode of file (rw, rdonly, etc). |
size | of the stream buffer. |
void ucommon::fbuf::open | ( | const char * | path, |
fsys::access_t | access, | ||
char ** | argv, | ||
size_t | size = 512 , |
||
char ** | envp = ((void *) 0) |
||
) |
Construct a pipe buffer from an existing file.
path | of existing process. |
access | mode of pipe. |
argv | to pass. |
envp | to use. |
bool ucommon::fbuf::seek | ( | offset_t | offset | ) |
Seek specific offset in open file and reset I/O buffers.
If the file is opened for both read and write, both the read and write position will be reset.
offset | to seek. |
offset_t ucommon::fbuf::tell | ( | void | ) |
Give the current position in the currently open file.
If we are appending, this is always seek::end. If we have a file opened for both read and write, this gives the read offset.
bool ucommon::fbuf::trunc | ( | offset_t | offset | ) |
Truncate the currently open file to a specific position.
All I/O buffers are reset and the file pointer is set to the end.
offset | to truncate. |