|
| Input () |
| Construct empty input character sequence. More...
|
|
| Input (const Input &input) |
| Copy constructor (with intended "move semantics" as internal state is shared, should not rely on using the rhs after copying). More...
|
|
| Input (const char *cstring, size_t size) |
| Construct input character sequence from a char* string. More...
|
|
| Input (const char *cstring) |
| Construct input character sequence from a NUL-terminated string. More...
|
|
| Input (const std::string &string) |
| Construct input character sequence from a std::string. More...
|
|
| Input (const std::string *string) |
| Construct input character sequence from a pointer to a std::string. More...
|
|
| Input (const wchar_t *wstring) |
| Construct input character sequence from a NUL-terminated wide character string. More...
|
|
| Input (const std::wstring &wstring) |
| Construct input character sequence from a std::wstring (may contain UTF-16 surrogate pairs). More...
|
|
| Input (const std::wstring *wstring) |
| Construct input character sequence from a pointer to a std::wstring (may contain UTF-16 surrogate pairs). More...
|
|
| Input (FILE *file) |
| Construct input character sequence from an open FILE* file descriptor, supports UTF-8 conversion from UTF-16 and UTF-32. More...
|
|
| Input (FILE *file, file_encoding_type enc, const unsigned short *page=NULL) |
| Construct input character sequence from an open FILE* file descriptor, using the specified file encoding. More...
|
|
| Input (std::istream &istream) |
| Construct input character sequence from a std::istream. More...
|
|
| Input (std::istream *istream) |
| Construct input character sequence from a pointer to a std::istream. More...
|
|
Input & | operator= (const Input &input) |
| Copy assignment operator. More...
|
|
| operator const char * () const |
| Cast this Input object to a string, returns NULL when this Input is not a string. More...
|
|
| operator const wchar_t * () const |
| Cast this Input object to a wide character string, returns NULL when this Input is not a wide string. More...
|
|
| operator FILE * () const |
| Cast this Input object to a file descriptor FILE*, returns NULL when this Input is not a FILE*. More...
|
|
| operator std::istream * () const |
| Cast this Input object to a std::istream*, returns NULL when this Input is not a std::istream. More...
|
|
| operator bool () const |
|
const char * | cstring () const |
| Get the remaining string of this Input object, returns NULL when this Input is not a string. More...
|
|
const wchar_t * | wstring () const |
| Get the remaining wide character string of this Input object, returns NULL when this Input is not a wide string. More...
|
|
FILE * | file () const |
| Get the FILE* of this Input object, returns NULL when this Input is not a FILE*. More...
|
|
std::istream * | istream () const |
| Get the std::istream of this Input object, returns NULL when this Input is not a std::istream. More...
|
|
size_t | size () |
| Get the size of the input character sequence in number of ASCII/UTF-8 bytes (zero if size is not determinable from a FILE* or std::istream source). More...
|
|
bool | assigned () const |
| Check if this Input object was assigned a character sequence. More...
|
|
void | clear () |
| Clear this Input by unassigning it. More...
|
|
bool | good () const |
| Check if input is available. More...
|
|
bool | eof () const |
| Check if input reached EOF. More...
|
|
int | get () |
| Get a single character (unsigned char 0..255) or EOF (-1) when end-of-input is reached. More...
|
|
size_t | get (char *s, size_t n) |
| Copy character sequence data into buffer. More...
|
|
void | file_encoding (file_encoding_type enc, const unsigned short *page=NULL) |
| Set encoding for FILE* input. More...
|
|
file_encoding_type | file_encoding () const |
| Get encoding of the current FILE* input. More...
|
|
void | init () |
| Initialize the state after (re)setting the input source, auto-detects UTF BOM in FILE* input if the file size is known. More...
|
|
void | file_init () |
| Called by init() for a FILE*. More...
|
|
void | wstring_size () |
| Called by size() for a wstring. More...
|
|
void | file_size () |
| Called by size() for a FILE*. More...
|
|
void | istream_size () |
| Called by size() for a std::istream. More...
|
|
size_t | file_get (char *s, size_t n) |
| Implements get() on a FILE*. More...
|
|
void | set_handler (Handler *handler) |
| Set FILE* handler. More...
|
|
Input character sequence class for unified access to sources of input text.
Description
The Input class unifies access to a source of input text that constitutes a sequence of characters:
- An Input object is instantiated and (re)assigned a (new) source input: either a
char*
string, a wchar_t*
wide string, a std::string
, a std::wstring
, a FILE*
descriptor, or a std::istream
object.
- Strings specified as input must be persistent and cannot be temporary. The input string contents are incrementally extracted and converted as necessary, when specified with an encoding format
reflex::Input::file_encoding
.
- When assigned a wide string source as input, the wide character content is automatically converted to an UTF-8 character sequence when reading with get(). Wide strings are UCS-2/UCS-4 and may contain UTF-16 surrogate pairs.
- When assigned a
FILE*
source as input, the file is checked for the presence of a UTF-8 or a UTF-16 BOM (Byte Order Mark). A UTF-8 BOM is ignored and will not appear on the input character stream (and size is adjusted by 3 bytes). A UTF-16 BOM is intepreted, resulting in the conversion of the file content automatically to an UTF-8 character sequence when reading the file with get(). Also, size() gives the content size in the number of UTF-8 bytes.
- An input object can be reassigned a new source of input for reading at any time.
- An input object obeys move semantics. That is, after assigning an input object to another, the former can no longer be used to read input. This prevents adding the overhead and complexity of file and stream duplication.
size_t Input::get(char *buf, size_t len);
reads source input and fills buf
with up to len
bytes, returning the number of bytes read or zero when a stream or file is bad or when EOF is reached.
size_t Input::size();
returns the number of ASCII/UTF-8 bytes available to read from the source input or zero (zero is also returned when the size is not determinable). Use this function only before reading input with get(). Wide character strings and UTF-16 FILE*
content is counted as the total number of UTF-8 bytes that will be produced by get(). The size of a std::istream
cannot be determined.
bool Input::good();
returns true if the input is readable and has no EOF or error state. Returns false on EOF or if an error condition is present.
bool Input::eof();
returns true if the input reached EOF. Note that good() == ! eof() for string source input only, since files and streams may have error conditions that prevent reading. That is, for files and streams eof() implies good() == false, but not vice versa. Thus, an error is diagnosed when the condition good() == false && eof() == false holds. Note that get(buf, len) == 0 && len > 0 implies good() == false.
class Input::streambbuf(const Input&)
creates a std::istream
for the given Input
object.
- Compile with
WITH_UTF8_UNRESTRICTED
to enable unrestricted UTF-8 beyond U+10FFFF, permitting lossless UTF-8 encoding of 32 bit words without limits.
Example
The following example shows how to use the Input class to read a character sequence in blocks from a std::ifstream
to copy to stdout:
std::ifstream ifs;
ifs.open("input.h", std::ifstream::in);
char buf[1024];
size_t len;
while ((len = input.get(buf, sizeof(buf))) > 0)
fwrite(buf, 1, len, stdout);
if (!input.eof())
std::cerr << "An IO error occurred" << std::endl;
ifs.close();
Example
The following example shows how to use the Input class to store the entire content of a file in a temporary buffer:
if (input.file() == NULL)
abort();
size_t len = input.size();
char *buf = new char[len];
input.get(buf, len);
if (!input.eof())
std::cerr << "An IO error occurred" << std::endl;
fwrite(buf, 1, len, stdout);
delete[] buf;
fclose(input.file());
In the above, files with UTF-16 and UTF-32 content are converted to UTF-8 by get(buf, len)
. Also, size()
returns the total number of UTF-8 bytes to copy in the buffer by get(buf, len)
. The size is computed depending on the UTF-8/16/32 file content encoding, i.e. given a leading UTF BOM in the file. This means that UTF-16/32 files are read twice, first internally with size()
and then again with get(buf, len)`.
Example
The following example shows how to use the Input class to read a character sequence in blocks from a file:
char buf[1024];
size_t len;
while ((len = input.get(buf, sizeof(buf))) > 0)
fwrite(buf, 1, len, stdout);
fclose(input);
Example
The following example shows how to use the Input class to echo characters one by one from stdin, e.g. reading input from a tty:
char c;
while (input.get(&c, 1))
fputc(c, stdout);
Or if you prefer to use an int character and check for EOF explicitly:
int c;
while ((c = input.get()) != EOF)
fputc(c, stdout);
Example
The following example shows how to use the Input class to read a character sequence in blocks from a wide character string, converting it to UTF-8 to copy to stdout:
char buf[8];
size_t len;
while ((len = input.get(buf, sizeof(buf))) > 0)
fwrite(buf, 1, len, stdout);
Example
The following example shows how to use the Input class to convert a wide character string to UTF-8:
size_t len = input.size();
char *buf = new char[len + 1];
input.get(buf, len);
buf[len] = '\0';
Example
The following example shows how to switch source inputs while reading input byte by byte (use a buffer as shown in other examples to improve efficiency):
std::string message;
char c;
message.append(c);
input = L" world! To ∞ and beyond.";
message.append(c);
Example
The following examples shows how to use reflex::Input::streambuf to create an unbuffered std::istream:
if (input.
file() == NULL)
abort();
std::istream stream(&streambuf);
std::string data;
int c;
while ((c = stream.get()) != EOF)
data.append(c);
With reflex::BufferedInput::streambuf to create a buffered std::istream:
if (input.
file() == NULL)
abort();
std::istream stream(&streambuf);
std::string data;
int c;
while ((c = stream.get()) != EOF)
data.append(c);