An input/output channel. More...
#include <file.h>


Public Member Functions | |
| file (const string &inpath) | |
| bool | openread (const string &fn) |
| bool | openread (int fd) |
| bool | openwrite (const string &fn, int mode=0644) |
| bool | openwrite (int fd) |
| bool | openappend (const string &fn, int mode=0644) |
| void | close (void) |
| string * | gets (int maxlinesize=1024) |
| bool | waitforline (string &into, int timeout_ms, int maxlinesize=1024) |
| bool | readuntil (string &into, const string &watchfor, unsigned int size, int tmout) |
| int | readbuffer (size_t, unsigned int tmout=0) |
| string * | read (size_t sz) |
| string * | read (size_t sz, int timeout_ms) |
| bool | writeln (const string &str) |
| bool | puts (const string &s) |
| bool | puts (const char *data, size_t sz) |
| int | tryputs (const char *str, size_t sz, unsigned int tmout=0) |
| bool | printf (const char *,...) |
| void | flush (void) |
| bool | eof (void) |
| off_t | pos (void) |
| operator bool (void) | |
| const string & | error (void) |
| unsigned int | errorcode (void) |
| string * | visitchild (int index) |
Public Attributes | |
| ringbuffer | buffer |
| iocodec * | codec |
| int | filno |
Protected Attributes | |
| bool | feof |
| bool | nonblocking |
| unsigned int | errcode |
| string | err |
An input/output channel.
| file::file | ( | const string & | inpath | ) | [inline] |
Constructor with explicit path.
Opens a file for reading (or writing)
| inpath | The disk file to open. If this starts with a '<', the file is opened for reading. If it starts with a '>', it is opened for writing (in both cases the first character is stripped). In other cases the file will be opened for reading. |
References codec, errcode, feof, filno, string::mid(), nonblocking, openread(), and openwrite().

| bool file::eof | ( | void | ) |
End-of-file.
References feof.
Referenced by httpsocket::connectToHost(), filesystem::cp(), process::eof(), httpdfileshare::httpdfileshare(), filesystem::load(), value::loadcsv(), value::loadini(), value::loadinitree(), httpdworker::run(), tcpsocket::sendfile(), and visitchild().
| string * file::gets | ( | int | maxlinesize = 1024 |
) |
Read a string terminated by a newline.
| maxlinesize | Set maximum size of a line. |
| endOfFileException | End of file reached. | |
| fileNotOpenException | File is not open. | |
| fileCodecException | Codec failure. | |
| fileReadException | File read error. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. | |
| EX_SSL_PROTOCOL_ERROR | Error in sslcodec protocol. | |
| EX_SSL_CLIENT_ALERT | Unhandled sslcodec client alert. |
References ringbuffer::add(), iocodec::addinput(), ringbuffer::backlog(), buffer, codec, string::crop(), iocodec::doneoutput(), err, errcode, iocodec::error(), feof, iocodec::fetchinput(), filno, ringbuffer::hasline(), nonblocking, iocodec::peekoutput(), string::printf(), ringbuffer::read(), read(), defaults::sz::file::readbuf, ringbuffer::readline(), ringbuffer::room(), string::str(), and string::strlen().
Referenced by daemon::daemonize(), smtpsocket::dosmtp(), process::gets(), httpdfileshare::httpdfileshare(), value::load(), value::loadcsv(), value::loadini(), value::loadinitree(), smtpworker::run(), httpdworker::run(), and visitchild().

| bool file::openappend | ( | const string & | fn, | |
| int | mode = 0644 | |||
| ) |
Open a disk file for appending.
| fn | Name (and path) of the file. | |
| mode | Unix permissio bits. |
References buffer, feof, filno, ringbuffer::flush(), string::str(), string::strlen(), and filesystem::transw().
Referenced by daemon::addlogtarget(), httpdlogger::httpdlogger(), and logthread::run().

| bool file::openread | ( | int | fd | ) |
Open a unix filedecriptor for reading.
| fd | The filedescriptor. |
References buffer, codec, feof, filno, ringbuffer::flush(), and iocodec::setup().

| bool file::openread | ( | const string & | fn | ) |
Open a disk file for reading.
| fn | Name (and path) of the file. |
References buffer, codec, feof, filno, ringbuffer::flush(), iocodec::setup(), string::str(), string::strlen(), and filesystem::transr().
Referenced by application::application(), filesystem::cp(), daemon::daemonize(), file(), tcpsocket::getfd(), httpdfileshare::httpdfileshare(), process::init(), value::load(), filesystem::load(), value::loadcsv(), value::loadini(), value::loadinitree(), and tcpsocket::sendfile().

| bool file::openwrite | ( | int | fd | ) |
Open a unix filedescriptor for writing.
| fd | The filedescriptor. |
References buffer, close(), codec, feof, filno, ringbuffer::flush(), iocodec::peekoutput(), iocodec::setup(), string::str(), and string::strlen().

| bool file::openwrite | ( | const string & | fn, | |
| int | mode = 0644 | |||
| ) |
Open a disk file for writing.
| fn | Name (and path) of the file. | |
| mode | Unix permissio bits. |
References buffer, close(), codec, feof, filno, ringbuffer::flush(), iocodec::peekoutput(), iocodec::setup(), string::str(), string::strlen(), and filesystem::transw().
Referenced by application::application(), filesystem::cp(), file(), process::init(), value::save(), filesystem::save(), value::savecsv(), and daemon::writepid().

| file::operator bool | ( | void | ) | [inline] |
| off_t file::pos | ( | void | ) |
Position.
References filno.
Referenced by logthread::run().
| bool file::printf | ( | const char * | fmtx, | |
| ... | ||||
| ) |
A libc-style printf.
| EX_SSL_NO_HANDSHAKE | No sslcodec handshake done. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. |
References string::crop(), puts(), string::strcat(), and string::strlen().
Referenced by daemon::daemonize(), httpd::handle(), systemprocess::main(), cgi::main(), application::main(), value::print(), smtpworker::run(), value::savecsv(), cgi::sendpage(), and daemon::writepid().

| bool file::puts | ( | const char * | data, | |
| size_t | sz | |||
| ) |
Write a data block.
| data | Pointer to the data. | |
| sz | Size of teh data block. |
| EX_SSL_NO_HANDSHAKE | No sslcodec handshake done. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. |
References iocodec::addoutput(), codec, iocodec::doneoutput(), feof, filno, nonblocking, iocodec::peekoutput(), string::str(), and string::strlen().

| bool file::puts | ( | const string & | s | ) |
Write a data block.
| s | The data to write. |
References string::str(), and string::strlen().
Referenced by filesystem::cp(), smtpsocket::dosmtp(), httpsocket::get(), termbuffer::getkey(), httpd::handle(), httpsocket::post(), value::print(), cli< ctlclass >::printf(), printf(), process::puts(), smtpworker::run(), httpdfileshare::run(), httpdworker::run(), filesystem::save(), tcpsocket::sendfile(), httpd::sendfile(), cgi::sendpage(), and writeln().

| string * file::read | ( | size_t | sz, | |
| int | timeout_ms | |||
| ) |
Nonblocking read.
Returns a string object with the data.
| sz | Number of bytes to read. | |
| timeout_ms | Timeout in milliseconds. |
| endOfFileException | End of file reached. | |
| fileNotOpenException | File not open. | |
| fileReadException | File read error. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. | |
| EX_SSL_PROTOCOL_ERROR | Error in sslcodec protocol. | |
| EX_SSL_CLIENT_ALERT | Unhandled sslcodec client alert. |
References ringbuffer::add(), iocodec::addinput(), ringbuffer::backlog(), buffer, codec, string::crop(), iocodec::doneoutput(), err, errcode, feof, iocodec::fetchinput(), filno, nonblocking, iocodec::peekoutput(), string::printf(), read(), ringbuffer::read(), defaults::sz::file::readbuf, ringbuffer::room(), string::str(), and string::strlen().

| string * file::read | ( | size_t | sz | ) |
Blocking read.
Returns a string object with the data.
| sz | Number of bytes to read. |
| endOfFileException | End of file reached. | |
| fileNotOpenException | File not open. | |
| fileReadException | File read error. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. | |
| EX_SSL_PROTOCOL_ERROR | Error in sslcodec protocol. | |
| EX_SSL_CLIENT_ALERT | Unhandled sslcodec client alert. |
References ringbuffer::add(), iocodec::addinput(), ringbuffer::backlog(), buffer, codec, string::crop(), err, errcode, feof, iocodec::fetchinput(), filno, nonblocking, string::printf(), ringbuffer::read(), defaults::sz::file::readbuf, and ringbuffer::room().
Referenced by cgi::cgi(), tcpsocket::connect(), filesystem::cp(), termbuffer::getkey(), gets(), filesystem::load(), process::read(), read(), readbuffer(), httpdworker::run(), tcpsocket::sendfile(), and waitforline().

| int file::readbuffer | ( | size_t | sz, | |
| unsigned int | tmout = 0 | |||
| ) |
Read data into the internal buffer.
| sz | Number of bytes to read. | |
| tmout | Timeout in milliseconds. |
| endOfFileException | End of file reached. | |
| fileNotOpenException | File not open. | |
| fileReadException | File read error. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. | |
| EX_SSL_PROTOCOL_ERROR | Error in sslcodec protocol. | |
| EX_SSL_CLIENT_ALERT | Unhandled sslcodec client alert. |
References ringbuffer::add(), iocodec::addinput(), buffer, codec, string::crop(), err, errcode, feof, iocodec::fetchinput(), filno, nonblocking, string::printf(), read(), and ringbuffer::room().
Referenced by readuntil().

Keep reading into a string until a specific byte sequence occurs.
| into | String to append data to. | |
| watchfor | Pointer to byte sequence. | |
| size | Size of byte sequence. | |
| tmout | Timeout in milliseconds. |
References ringbuffer::backlog(), buffer, ringbuffer::findforward(), ringbuffer::read(), readbuffer(), and string::strcat().

| int file::tryputs | ( | const char * | str, | |
| size_t | sz, | |||
| unsigned int | tmout = 0 | |||
| ) |
Non-blocking puts.
| str | Pointer to the data. | |
| sz | Size of the data to be written. | |
| tmout | Timeout in milliseconds. |
| EX_SSL_NO_HANDSHAKE | No sslcodec handshake done. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. |
References iocodec::addoutput(), iocodec::canoutput(), codec, iocodec::doneoutput(), feof, filno, nonblocking, iocodec::peekoutput(), string::str(), and string::strlen().

| bool file::waitforline | ( | string & | into, | |
| int | timeout_ms, | |||
| int | maxlinesize = 1024 | |||
| ) |
Wait for a string terminated by a newline.
| into | String to append the line to. | |
| timeout_ms | Timeout in milliseconds. | |
| maxlinesize | Maximum size of a line. |
| endOfFileException | End of file reached. | |
| fileNotOpenException | File not open. | |
| fileReadException | File read error. | |
| EX_SSL_BUFFER_SNAFU | Error in sslcodec buffer. | |
| EX_SSL_PROTOCOL_ERROR | Error in sslcodec protocol. | |
| EX_SSL_CLIENT_ALERT | Unhandled sslcodec client alert. |
References ringbuffer::add(), iocodec::addinput(), buffer, codec, string::crop(), err, errcode, feof, iocodec::fetchinput(), filno, ringbuffer::hasline(), nonblocking, string::printf(), ringbuffer::read(), read(), ringbuffer::readline(), and ringbuffer::room().

| bool file::writeln | ( | const string & | str | ) |
Write a string followed by a newline.
| str | The string to write. |
References puts(), string::str(), and string::strlen().
Referenced by daemon::daemonize(), daemon::delayedexiterror(), daemon::delayedexitok(), daemon::log(), logthread::run(), and termbuffer::writeconsole().

1.6.1