Top |
The data input stream used by CTPL. This is a buffered input stream on top
of GInputStream with current position information (line and position) and
some read scheme facilities (words (ctpl_input_stream_read_word()
), string
literals (ctpl_input_stream_read_string_literal()
), ...).
A CtplInputStream is created with ctpl_input_stream_new()
. There is some
convenient wrappers to create it from in-memory data
(ctpl_input_stream_new_for_memory()
), GFiles
(ctpl_input_stream_new_for_gfile()
), path
(ctpl_input_stream_new_for_path()
) and URIs
(ctpl_input_stream_new_for_uri()
).
CtplInputStream object uses a GObject-like refcounting, via
ctpl_input_stream_ref()
and ctpl_input_stream_unref()
.
The errors that the functions in this module can throw comes from the
G_IO_ERROR
or CTPL_IO_ERROR
domains unless otherwise mentioned.
CtplInputStream * ctpl_input_stream_new (GInputStream *stream
,const gchar *name
);
Creates a new CtplInputStream for a GInputStream. This function adds a reference to the GInputStream.
stream |
||
name |
The name of the stream, or |
Since 0.2
CtplInputStream * ctpl_input_stream_new_for_gfile (GFile *file
,GError **error
);
Creates a new CtplInputStream for a GFile. This is a wrapper around
g_file_read()
that also sets the name of the stream to the file's name.
The errors this function can throw are those from the G_IO_ERROR
domain.
See ctpl_input_stream_new()
.
Since 0.2
CtplInputStream * ctpl_input_stream_new_for_memory (const gchar *data
,gssize length
,GDestroyNotify destroy
,const gchar *name
);
Creates a new CtplInputStream for in-memory data. This is a wrapper around
GMemoryInputStream; see ctpl_input_stream_new()
.
data |
Data for which create the stream |
|
length |
length of |
|
destroy |
GDestroyNotify to call on |
|
name |
The name of the stream to identify it in error messages |
Since 0.2
CtplInputStream * ctpl_input_stream_new_for_path (const gchar *path
,GError **error
);
Creates a new CtplInputStream for a path. This is a wrapper for
ctpl_input_stream_new_for_gfile()
that simply creates a GFile for the given
path and call ctpl_input_stream_new_for_gfile()
on it.
path |
A local or absolute path to pass to |
|
error |
Return location for errors, or |
Since 0.2
CtplInputStream * ctpl_input_stream_new_for_uri (const gchar *uri
,GError **error
);
Creates a new CtplInputStream for an URI. This is a wrapper for
ctpl_input_stream_new_for_gfile()
that simply creates a GFile for the given
URI and call ctpl_input_stream_new_for_gfile()
on it.
uri |
A URI to pass to |
|
error |
Return location for errors, or |
Since 0.2
CtplInputStream *
ctpl_input_stream_ref (CtplInputStream *stream
);
Adds a reference to a CtplInputStream.
Since 0.2
void
ctpl_input_stream_unref (CtplInputStream *stream
);
Removes a reference from a CtplInputStream. If the reference count drops to 0, frees the stream.
Since 0.2
GInputStream *
ctpl_input_stream_get_stream (const CtplInputStream *stream
);
Gets the underlying GInputStream associated with a CtplInputStream.
Since 0.3
const gchar *
ctpl_input_stream_get_name (const CtplInputStream *stream
);
Gets the name associated with a CtplInputStream.
Since 0.3
guint
ctpl_input_stream_get_line (const CtplInputStream *stream
);
Gets the current line number of a CtplInputStream.
Since 0.3
guint
ctpl_input_stream_get_line_position (const CtplInputStream *stream
);
Gets the current offset position in the current line, in bytes, in a CtplInputStream.
Since 0.3
void ctpl_input_stream_set_error (CtplInputStream *stream
,GError **error
,GQuark domain
,gint code
,const gchar *format
,...
);
This is a wrapper around g_set_error()
that adds stream's position
information to the reported error.
stream |
||
error |
[out callee-allocates][allow-none] | |
domain |
The domain of the error to report |
|
code |
The code of the error |
|
format |
printf-like format string |
|
... |
printf-like arguments for |
Since 0.2
gssize ctpl_input_stream_read (CtplInputStream *stream
,void *buffer
,gsize count
,GError **error
);
Reads data from a CtplInputStream.
stream |
||
buffer |
buffer to fill with the read data |
|
count |
number of bytes to read (must be less than or qual to |
|
error |
return location for errors, or |
Since 0.2
gchar ctpl_input_stream_get_c (CtplInputStream *stream
,GError **error
);
Reads a character from a CtplInputStream.
Since
gdouble ctpl_input_stream_read_float (CtplInputStream *stream
,GError **error
);
Reads a real form a CtplInputStream. See ctpl_input_stream_read_number()
for
details.
Since 0.2
glong ctpl_input_stream_read_int (CtplInputStream *stream
,GError **error
);
Reads an integer from a CtplInputStream. See ctpl_input_stream_read_number()
for details.
Since 0.2
gboolean ctpl_input_stream_read_number (CtplInputStream *stream
,CtplValue *value
,GError **error
);
Reads a number from a CtplInputStream. A number can be a plain decimal
integer, a binary integer prefixed with 0b
, an octal integer
prefixed with 0o
, a hexadecimal integer prefixed with
0x
, a decimal real with possible decimal exponent separated by a
e
or a hexadecimal real with possible decimal power separated by
a p
; each possibly preceded by a plus or minus sign.
The decimal point of real numbers is always a period (.
).
Example 15. Some numeric constants
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* Decimal integers */ 42 +512 /* Binary integers */ 0b11 /* Octal integers */ 0o755 /* Hexadecimal integers */ -0x7FFBE 0x8ff /* Decimal reals */ +2.1 1.024e3 /* Hexadecimal reals */ 0x71F.A0 -0x88fe.2p8 |
stream |
||
value |
A CtplValue to fill with the read number, either |
|
error |
Return location for errors, or |
Since 0.2
gchar * ctpl_input_stream_read_string_literal (CtplInputStream *stream
,GError **error
);
Reads a string quoted with "
(double quote), containing
possible escaping sequences escaped by \
(backslash).
A plain escaping character (backslash) need to be escaped too, otherwise it
will simply escape the next character.
For instance, a string might look like this:
1 |
-->and \" (double quotes)" ]| |
Since 0.2
#define ctpl_input_stream_read_symbol(stream, error)
Reads a symbol (a word composed of the characters from CTPL_SYMBOL_CHARS
).
See ctpl_input_stream_read_word()
and ctpl_input_stream_read_symbol_full()
.
Since 0.2
gchar * ctpl_input_stream_read_symbol_full (CtplInputStream *stream
,gssize max_len
,gsize *length
,GError **error
);
Reads a symbol from a CtplInputStream. A symbol is a word composed of the
characters from CTPL_SYMBOL_CHARS
.
See ctpl_input_stream_read_word()
and ctpl_input_stream_read_symbol()
.
Since 0.2
gchar * ctpl_input_stream_read_word (CtplInputStream *stream
,const gchar *accept
,gssize accept_len
,gssize max_len
,gsize *length
,GError **error
);
Reads a word from a CtplInputStream. A word is a sequence of characters
referenced by accept
. Note that the word might be empty if no characters
matching accept
are found before one that doesn't match.
For example, reading a word composed of any ASCII lowercase characters may be as the following:
1 2 3 4 5 6 7 8 9 10 11 |
gchar *word; GError *error = NULL; word = ctpl_input_stream_read_word (stream, "abcdefghijklmnopqrstuvwxyz", -1, -1, NULL, &error); if (! word) { /<!-- -->* deal with the error *<!-- -->/ } else { printf ("Read the word \"%s\"\n", word); g_free (word); } |
stream |
||
accept |
string of the character acceptable for the word |
|
accept_len |
length of |
|
max_len |
maximum number of bytes to read, or -1 for no limit. |
[default -1] |
length |
return location for the length of the read word,
or |
[out][allow-none] |
error |
Return location for errors, or |
A newly allocated string containing the read word that should be
freed with g_free()
when no longer needed; or NULL
on error.
Since 0.2
gssize ctpl_input_stream_peek (CtplInputStream *stream
,void *buffer
,gsize count
,GError **error
);
Peeks data from a CtplInputStream. Peeking data is like reading, but it doesn't removes the data from the stream.
A peek might resize the internal stream's cache to fit at least count
.
Therefore, peeking too much data at once should be done with some care.
stream |
||
buffer |
buffer to fill with the peeked data |
|
count |
number of bytes to peek (must be less than or qual to |
|
error |
return location for errors, or |
Since 0.2
gchar ctpl_input_stream_peek_c (CtplInputStream *stream
,GError **error
);
Peeks a character from a CtplInputStream. This may be implemented as a macro.
Since 0.2
#define ctpl_input_stream_peek_symbol(stream, max_len, error)
Peeks a symbol from a CtplInputStream. See ctpl_input_stream_peek_word()
and
ctpl_input_stream_peek_symbol_full()
.
stream |
||
max_len |
The maximum number of bytes to peek, even if they still matches, or -1 for no limit. |
[default -1] |
error |
Return location for errors, or |
Since 0.2
gchar * ctpl_input_stream_peek_symbol_full (CtplInputStream *stream
,gssize max_len
,gsize *length
,GError **error
);
Peeks a symbol from a CtplInputStream. See ctpl_input_stream_peek_word()
and
ctpl_input_stream_peek_symbol()
.
Since 0.2
gchar * ctpl_input_stream_peek_word (CtplInputStream *stream
,const gchar *accept
,gssize accept_len
,gssize max_len
,gsize *length
,GError **error
);
Peeks a word from a CtplInputStream. See ctpl_input_stream_peek()
and
ctpl_input_stream_read_word()
.
stream |
||
accept |
string of the character acceptable for the word |
|
accept_len |
length of |
|
max_len |
maximum number of bytes to peek, or -1 for no limit. |
[default -1] |
length |
return location for the length of the read word,
or |
[out][allow-none] |
error |
return location for errors, or |
A newly allocated string containing the peeked word that should be
freed with g_free()
when no longer needed; or NULL
on error.
Since 0.2
gssize ctpl_input_stream_skip (CtplInputStream *stream
,gsize count
,GError **error
);
Skips count
bytes from a CtplInputStream.
stream |
||
count |
Number of bytes to skip |
|
error |
Return location for errors, or |
Since
gssize ctpl_input_stream_skip_blank (CtplInputStream *stream
,GError **error
);
Skips blank characters (as reported by ctpl_is_blank()
).
See ctpl_input_stream_skip()
.
Since 0.2
gssize ctpl_input_stream_skip_word (CtplInputStream *stream
,const gchar *reject
,gssize reject_len
,GError **error
);
Skips all the characters matching reject
from a CtplInputStream until the
first that doesn't match.
stream |
||
reject |
A string of the characters to skip |
|
reject_len |
Length of |
|
error |
Return location for errors, or |
Since 0.2
gboolean ctpl_input_stream_eof (CtplInputStream *stream
,GError **error
);
Reliably checks if the stream reached its end.
The error this function can throw are from the G_IO_ERROR
domain.
The return value of this function is quite uncommon: it returns TRUE
if
at stream's end, but also on error.
This is to be more convenient since this function is mainly used to
ensure the stream does NOT have reached its end. To differentiate an
error from EOF, check if the error was set.
Since 0.2
gboolean
ctpl_input_stream_eof_fast (CtplInputStream *stream
);
Checks if a CtplInputStream reached its end. See also
ctpl_input_stream_eof()
.
This function is reliable only to know if the stream already reached EOF,
not if next read will do so. To reliably check whether the stream have
data to be read, first call a function that will do a read if necessary,
and then reach the end of the stream. For example, use
ctpl_input_stream_peek_c()
:
1 2 3 4 5 |
ctpl_input_stream_peek_c (stream, &error); /<!-- -->* deal with the possible error *<!-- -->/ if (ctpl_input_stream_eof_fast (stream)) { /<!-- -->* here EOF is reliable *<!-- -->/ } |
There is also a reliable version, but that can fail:
ctpl_input_stream_eof()
.