![]() |
PIDUINO
|
Provides an interface for reading from and writing to files. More...
#include <filedevice.h>
Public Types | |
enum | OpenModeFlag { NotOpen = 0x0000 , Append = std::ios_base::app , AtEnd = std::ios_base::ate , Binary = std::ios_base::binary , ReadOnly = std::ios_base::in , WriteOnly = std::ios_base::out , ReadWrite = ReadOnly | WriteOnly , Truncate = std::ios_base::trunc , IosModes = Append | AtEnd | Binary | ReadOnly | WriteOnly | Truncate , Unbuffered = (Truncate << 1) } |
Flags that specify how the device is to be opened. More... | |
typedef Flags< OpenModeFlag > | OpenMode |
Type representing a combination of OpenModeFlag values. | |
Public Member Functions | |
FileDevice () | |
FileDevice (int fd) | |
FileDevice (const std::string &path) | |
virtual | ~FileDevice () |
virtual bool | open (OpenMode mode=ReadWrite) |
virtual void | close () |
virtual void | setPath (const std::string &path) |
virtual std::string | path () const |
bool | setFd (int fd) |
int | fd () const |
bool | isOurFile () const |
virtual ssize_t | write (const char *data, size_t maxSize) |
ssize_t | write (const char *str) |
ssize_t | write (const std::string &str) |
virtual ssize_t | read (char *data, size_t maxSize) |
OpenMode | openMode () const |
Returns the current open mode of the device. | |
virtual bool | isOpen () const |
Returns true if the device is currently open. | |
bool | isReadable () const |
Returns true if the device is readable. | |
bool | isWritable () const |
Returns true if the device is writable. | |
bool | isBuffered () const |
Returns true if the device is buffered. | |
virtual bool | isSequential () const |
Returns true if this device is sequential; otherwise returns false. | |
void | setTextModeEnabled (bool enabled) |
Enables or disables text mode for the device. | |
bool | isTextModeEnabled () const |
Returns true if text mode is enabled. | |
void | setDebug (bool enabled) |
Enables or disables debug mode for the device. | |
bool | isDebug () const |
Returns true if debug mode is enabled. | |
virtual std::string | errorString () const |
Returns a human-readable description of the last error. | |
virtual int | error () const |
Returns the code of the last error. | |
Protected Member Functions | |
FileDevice (Private &dd) | |
Protected Attributes | |
std::unique_ptr< Private > | d_ptr |
Pointer to the private implementation. | |
Provides an interface for reading from and writing to files.
FileDevice is an I/O device for reading and writing binary and text files (with std::iostream features
The file name is usually passed in the constructor, but it can be set at any time using setFileName(). FileDevice expects the file separator to be '/' regardless of operating system.
The file is opened with open(), closed with close().
Data is usually read and written using std::iostream with ios(), but you can also call the functions read() and write().
Definition at line 45 of file filedevice.h.
|
inherited |
Type representing a combination of OpenModeFlag values.
Definition at line 70 of file iodevice.h.
|
inherited |
Flags that specify how the device is to be opened.
These flags control the behavior of the device when it is opened. They can be combined using bitwise OR.
Definition at line 48 of file iodevice.h.
Piduino::FileDevice::FileDevice | ( | ) |
Constructs a FileDevice object.
Piduino::FileDevice::FileDevice | ( | int | fd | ) |
Constructs a FileDevice object from an open file descriptor.
Piduino::FileDevice::FileDevice | ( | const std::string & | path | ) |
Constructs a new file object to represent the file with the given path.
|
virtual |
Destroys the file object, closing it if necessary.
|
protected |
|
virtual |
Closes the device and sets its OpenMode to NotOpen
Reimplemented from Piduino::IoDevice.
|
virtualinherited |
Returns the code of the last error.
|
virtualinherited |
Returns a human-readable description of the last error.
int Piduino::FileDevice::fd | ( | ) | const |
Returns the file descriptor of the file, if it is open, else returns -1.
Do not use the close() function with this descriptor !
|
inherited |
Returns true if the device is buffered.
|
inherited |
Returns true if debug mode is enabled.
|
virtualinherited |
Returns true if the device is currently open.
bool Piduino::FileDevice::isOurFile | ( | ) | const |
|
inherited |
Returns true if the device is readable.
|
virtualinherited |
Returns true if this device is sequential; otherwise returns false.
Sequential devices, as opposed to random-access devices, have no concept of a start, an end, a size, or a current position, and do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential. Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.
The IoDevice implementation returns false.
|
inherited |
Returns true if text mode is enabled.
|
inherited |
Returns true if the device is writable.
Opens the file device using OpenMode mode, and then returns true
if successful; otherwise returns false
and sets an error code which can be obtained by calling the error() method. The error string is also reset.
Reimplemented from Piduino::IoDevice.
|
inherited |
Returns the current open mode of the device.
|
virtual |
Returns the path set by setPath() or to the FileDevice constructors.
|
virtual |
Reads at most maxSize bytes from the device into data, and returns the number of bytes read. If an error occurs, such as when attempting to read from a device opened in WriteOnly mode, this function returns -1.
0 is returned when no more data is available for reading. However, reading past the end of the stream is considered an error, so this function returns -1 in those cases (that is, reading on a closed socket or after a process has died).
Reimplemented in Piduino::FileStream, and Piduino::Terminal.
|
inherited |
Enables or disables debug mode for the device.
enabled | True to enable debug mode, false to disable. |
bool Piduino::FileDevice::setFd | ( | int | fd | ) |
|
virtual |
Sets the path of the file. The path can have no prefix, a relative path, or an absolute path.
Do not call this function if the file has already been opened, if so, the file is closed before the path is modified.
If the file name has no path or a relative path, the path used will be the application's current directory path at the time of the open() call.
Note that the directory separator is "/".
|
inherited |
Enables or disables text mode for the device.
enabled | True to enable text mode, false to disable. |
|
virtual |
Writes at most maxSize bytes of data from data to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
Reimplemented in Piduino::FileStream, and Piduino::SerialPort.
ssize_t Piduino::FileDevice::write | ( | const char * | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Writes data from a zero-terminated string of 8-bit characters to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
ssize_t Piduino::FileDevice::write | ( | const std::string & | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Writes the content of string to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
|
protectedinherited |
Pointer to the private implementation.
Definition at line 190 of file iodevice.h.