Read utilities

Read utilities — Common functions used to read from a LibMB buffer

Synopsis

#include <ctpl/readutils.h>

#define             CTPL_BLANK_CHARS
#define             CTPL_SYMBOL_CHARS
#define             CTPL_ESCAPE_CHAR
#define             CTPL_STRING_DELIMITER_CHAR
gchar *             ctpl_read_word                      (MB *mb,
                                                         const gchar *accept);
#define             ctpl_read_symbol                    (mb)
gsize               ctpl_read_skip_chars                (MB *mb,
                                                         const gchar *reject);
#define             ctpl_read_skip_blank                (mb)
gchar *             ctpl_read_string_literal            (MB *mb);
gdouble             ctpl_read_double                    (MB *mb,
                                                         gsize *n_read);

Description

Useful functions to read data from a LibMB buffer.

These functions are somewhat generic and are used by different part of CTPL internally.

Details

CTPL_BLANK_CHARS

#define CTPL_BLANK_CHARS  " \t\v\r\n"

Characters treated as blank, commonly used as separator.


CTPL_SYMBOL_CHARS

#define             CTPL_SYMBOL_CHARS

Characters that are valid for a symbol.


CTPL_ESCAPE_CHAR

#define CTPL_ESCAPE_CHAR  '\\'

Character used to escape a special character.


CTPL_STRING_DELIMITER_CHAR

#define CTPL_STRING_DELIMITER_CHAR '"'

Character surrounding string literals.


ctpl_read_word ()

gchar *             ctpl_read_word                      (MB *mb,
                                                         const gchar *accept);

Reads a word composed of accept characters.

mb :

A MB

accept :

String of acceptable characters for the word

Returns :

A newly allocated string containing the read word or NULL if there was no word to read (e.g. no characters matching accept was found before one not matching it).

ctpl_read_symbol()

#define ctpl_read_symbol(mb) (ctpl_read_word ((mb), CTPL_SYMBOL_CHARS))

Reads a symbol (a word composed of the characters from CTPL_SYMBOL_CHARS). See ctpl_read_word()

mb :

A MB

Returns :

A newly allocated string containing the read symbol, or NULL if no symbol was read.

ctpl_read_skip_chars ()

gsize               ctpl_read_skip_chars                (MB *mb,
                                                         const gchar *reject);

Skips characters in reject, making the next call to mb_getc() on mb not getting one of them.

mb :

A MB

reject :

Characters to skip.

Returns :

The number of skipped characters.

ctpl_read_skip_blank()

#define ctpl_read_skip_blank(mb) (ctpl_read_skip_chars ((mb), CTPL_BLANK_CHARS))

Skips blank characters (those from CTPL_BLANK_CHARS). See ctpl_read_skip_chars().

mb :

A MB

Returns :

The number of skipped characters.

ctpl_read_string_literal ()

gchar *             ctpl_read_string_literal            (MB *mb);

Tries to read a string literal.

A string literal is something like "foo bar", with the quotes. Here, the quotes are CTPL_STRING_DELIMITER_CHAR. Delimiter character may appear inside a string literal if escaped with CTPL_ESCAPE_CHAR. A plain CTPL_ESCAPE_CHAR need to be escaped too, otherwise it will simply escape the next character.

mb :

A MB

Returns :

The read string, or NULL if none read.

ctpl_read_double ()

gdouble             ctpl_read_double                    (MB *mb,
                                                         gsize *n_read);

Reads a number from mb as a double, as g_ascii_strtod() would do.

Warning

errno may be modified by a call to this function (see g_ascii_strtod() for possible reasons).

Note

Regardless the above warning, you do not need to check errno to know if the conversion succeeded cleanly and completely, since this function already does it and reports an invalid value if errno reports any interesting error (i.e. ERANGE).

mb :

A MB

n_read :

Return location for the number of characters that were read to build the returned value, or NULL. This value is set to 0 if no valid number were read.

Returns :

The read value as a double, or 0 on error. See n_read to cleanly detect errors.