How to Read

How to Read — Reading from streams

Functions

Includes

#include <libchimara/glk.h>

Description

You can read Latin-1 or Unicode characters, buffers, or whole lines from any stream. The characters will be converted into the form in which you request them.

Functions

glk_get_char_stream ()

glsi32
glk_get_char_stream (strid_t str);

Reads one character from the stream str . (There is no notion of a “current input stream.”) It is illegal for str to be NULL, or an output-only stream.

The result will be between 0 and 255. As with all basic text functions, Glk assumes the Latin-1 encoding. See Character Encoding. If the end of the stream has been reached, the result will be -1.

Note that high-bit characters (128..255) are not returned as negative numbers.

If the stream contains Unicode data — for example, if it was created with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then characters beyond 255 will be returned as 0x3F ("?").

It is usually more efficient to read several characters at once with glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling glk_get_char_stream() several times.

Parameters

str

An input stream.

 

Returns

A character value between 0 and 255, or -1 on end of stream.


glk_get_buffer_stream ()

glui32
glk_get_buffer_stream (strid_t str,
                       char *buf,
                       glui32 len);

Reads len characters from str , unless the end of stream is reached first. No terminal null is placed in the buffer.

Parameters

str

An input stream.

 

buf

A buffer with space for at least len characters.

 

len

The number of characters to read.

 

Returns

The number of characters actually read.


glk_get_line_stream ()

glui32
glk_get_line_stream (strid_t str,
                     char *buf,
                     glui32 len);

Reads characters from str , until either len - 1 characters have been read or a newline has been read. It then puts a terminal null ('\0') character on the end. It returns the number of characters actually read, including the newline (if there is one) but not including the terminal null.

Parameters

str

An input stream.

 

buf

A buffer with space for at least len characters.

 

len

The number of characters to read, plus one.

 

Returns

The number of characters actually read.


glk_get_char_stream_uni ()

glsi32
glk_get_char_stream_uni (strid_t str);

Reads one character from the stream str . If the end of the stream has been reached, the result will be -1.

Parameters

str

An input stream.

 

Returns

A value between 0 and 0x7FFFFFFF, or -1 on end of stream.


glk_get_buffer_stream_uni ()

glui32
glk_get_buffer_stream_uni (strid_t str,
                           glui32 *buf,
                           glui32 len);

Reads len Unicode characters from str , unless the end of stream is reached first. No terminal null is placed in the buffer.

Parameters

str

An input stream.

 

buf

A buffer with space for at least len Unicode code points.

 

len

The number of characters to read.

 

Returns

The number of Unicode characters actually read.


glk_get_line_stream_uni ()

glui32
glk_get_line_stream_uni (strid_t str,
                         glui32 *buf,
                         glui32 len);

Reads Unicode characters from str , until either len - 1 Unicode characters have been read or a newline has been read. It then puts a terminal null (a zero value) on the end.

Parameters

str

An input stream.

 

buf

A buffer with space for at least len Unicode code points.

 

len

The number of characters to read, plus one.

 

Returns

The number of characters actually read, including the newline (if there is one) but not including the terminal null.