![]() |
![]() |
![]() |
CTPL Reference Manual | ![]() |
---|---|---|---|---|
Top | Description |
#include <ctpl/ctpl.h> #define CTPL_EOF CtplInputStream; CtplInputStream * ctpl_input_stream_new (GInputStream *stream
,const gchar *name
); CtplInputStream * ctpl_input_stream_new_for_gfile (GFile *file
,GError **error
); CtplInputStream * ctpl_input_stream_new_for_memory (const gchar *data
,gssize length
,GDestroyNotify destroy
,const gchar *name
); CtplInputStream * ctpl_input_stream_new_for_path (const gchar *path
,GError **error
); CtplInputStream * ctpl_input_stream_new_for_uri (const gchar *uri
,GError **error
); CtplInputStream * ctpl_input_stream_ref (CtplInputStream *stream
); void ctpl_input_stream_unref (CtplInputStream *stream
); GInputStream * ctpl_input_stream_get_stream (const CtplInputStream *stream
); const gchar * ctpl_input_stream_get_name (const CtplInputStream *stream
); guint ctpl_input_stream_get_line (const CtplInputStream *stream
); guint ctpl_input_stream_get_line_position (const CtplInputStream *stream
); void ctpl_input_stream_set_error (CtplInputStream *stream
,GError **error
,GQuark domain
,gint code
,const gchar *format
,...
); gssize ctpl_input_stream_read (CtplInputStream *stream
,void *buffer
,gsize count
,GError **error
); gchar ctpl_input_stream_get_c (CtplInputStream *stream
,GError **error
); gdouble ctpl_input_stream_read_float (CtplInputStream *stream
,GError **error
); glong ctpl_input_stream_read_int (CtplInputStream *stream
,GError **error
); gboolean ctpl_input_stream_read_number (CtplInputStream *stream
,CtplValue *value
,GError **error
); gchar * ctpl_input_stream_read_string_literal (CtplInputStream *stream
,GError **error
); #define ctpl_input_stream_read_symbol (stream, error) gchar * ctpl_input_stream_read_symbol_full (CtplInputStream *stream
,gssize max_len
,gsize *length
,GError **error
); gchar * ctpl_input_stream_read_word (CtplInputStream *stream
,const gchar *accept
,gssize accept_len
,gssize max_len
,gsize *length
,GError **error
); gssize ctpl_input_stream_peek (CtplInputStream *stream
,void *buffer
,gsize count
,GError **error
); gchar ctpl_input_stream_peek_c (CtplInputStream *stream
,GError **error
); #define ctpl_input_stream_peek_symbol (stream, max_len, error) gchar * ctpl_input_stream_peek_symbol_full (CtplInputStream *stream
,gssize max_len
,gsize *length
,GError **error
); gchar * ctpl_input_stream_peek_word (CtplInputStream *stream
,const gchar *accept
,gssize accept_len
,gssize max_len
,gsize *length
,GError **error
); gssize ctpl_input_stream_skip (CtplInputStream *stream
,gsize count
,GError **error
); gssize ctpl_input_stream_skip_blank (CtplInputStream *stream
,GError **error
); gssize ctpl_input_stream_skip_word (CtplInputStream *stream
,const gchar *reject
,gssize reject_len
,GError **error
); gboolean ctpl_input_stream_eof (CtplInputStream *stream
,GError **error
); gboolean ctpl_input_stream_eof_fast (CtplInputStream *stream
);
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.
typedef struct _CtplInputStream CtplInputStream;
An opaque object representing an input data stream.
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.
|
A GInputStream |
|
The name of the stream, or NULL for none. This is used to identify
the stream in error messages
|
Returns : |
A new CtplInputStream |
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()
.
|
A GFile to read |
|
Return location for errors, or NULL to ignore them
|
Returns : |
A new CtplInputStream on success, NULL on error.
|
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 for which create the stream |
|
length of data
|
|
GDestroyNotify to call on data when finished, or NULL
|
|
The name of the stream to identify it in error messages |
Returns : |
A new CtplInputStream for the given data |
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.
|
A local or absolute path to pass to g_file_new_for_path()
|
|
Return location for errors, or NULL to ignore them
|
Returns : |
A new CtplInputStream on success, NULL on error.
|
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.
|
A URI to pass to g_file_new_for_uri() .
|
|
Return location for errors, or NULL to ignore them
|
Returns : |
A new CtplInputStream on success, NULL on error.
|
Since 0.2
CtplInputStream * ctpl_input_stream_ref (CtplInputStream *stream
);
Adds a reference to a CtplInputStream.
|
A CtplInputStream |
Returns : |
The stream |
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.
|
A CtplInputStream |
Since 0.2
GInputStream * ctpl_input_stream_get_stream (const CtplInputStream *stream
);
Gets the underlying GInputStream associated with a CtplInputStream.
|
A CtplInputStream |
Returns : |
The underlying GInputStream of stream . [transfer none]
|
Since 0.3
const gchar * ctpl_input_stream_get_name (const CtplInputStream *stream
);
Gets the name associated with a CtplInputStream.
|
A CtplInputStream |
Returns : |
The name associated with stream , or NULL if none. [allow-none]
|
Since 0.3
guint ctpl_input_stream_get_line (const CtplInputStream *stream
);
Gets the current line number of a CtplInputStream.
|
A CtplInputStream |
Returns : |
The current line number associated with stream .
|
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.
|
A CtplInputStream |
Returns : |
The current offset in the current line. |
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.
|
A CtplInputStream |
|
A GError to fill, may be NULL . [out callee-allocates][allow-none callee-allocates]
|
|
The domain of the error to report |
|
The code of the error |
|
printf-like format string |
|
printf-like arguments for format
|
Since 0.2
gssize ctpl_input_stream_read (CtplInputStream *stream
,void *buffer
,gsize count
,GError **error
);
Reads data from a CtplInputStream.
|
A CtplInputStream |
|
buffer to fill with the read data |
|
number of bytes to read (must be less than or qual to G_MAXSSIZE , or
a G_IO_ERROR_INVALID_ARGUMENT will be thrown)
|
|
return location for errors, or NULL to ignore them
|
Returns : |
The number of bytes read, or -1 on error. |
Since 0.2
gchar ctpl_input_stream_get_c (CtplInputStream *stream
,GError **error
);
Reads a character from a CtplInputStream.
|
A CtplInputStream |
|
Return location for errors, or NULL to ignore them
|
Returns : |
The read character, or CTPL_EOF at stream's end or on error
|
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.
|
A CtplInputStream |
|
Return location for errors, or NULL to ignore them
|
Returns : |
The read value, or 0 on error. |
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.
|
A CtplInputStream |
|
Return location for errors, or NULL to ignore them
|
Returns : |
The read integer, or 0 on error. |
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 |
|
A CtplInputStream |
|
A CtplValue to fill with the read number, either CTPL_VTYPE_INT
or CTPL_VTYPE_FLOAT
|
|
Return location for errors, or NULL to ignore them
|
Returns : |
TRUE on success, FALSE otherwise.
|
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 |
"a valid string with \"special\" characters such as \\ (backslash) and \" (double quotes)" |
|
A CtplInputStream |
|
Return location for errors, or NULL to ignore them
|
Returns : |
The read string, or NULL on error
|
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()
.
|
A CtplInputStream |
|
return location for errors, or NULL to ignore them
|
Returns : |
A newly allocated string containing the read symbol, or NULL on
error.
|
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()
.
|
A CtplInputStream |
|
The maximum number of bytes to read, or -1 for no limit. [default -1] |
|
Return location for the read symbol length, or
NULL . [out][allow-none]
|
|
return location for errors, or NULL to ignore them
|
Returns : |
A newly allocated string containing the read symbol, or NULL on
error.
|
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); } |
|
A CtplInputStream |
|
string of the character acceptable for the word |
|
length of accept , can be -1 if accept is 0-terminated
|
|
maximum number of bytes to read, or -1 for no limit. [default -1] |
|
return location for the length of the read word,
or NULL . [out][allow-none]
|
|
Return location for errors, or NULL to ignore them
|
Returns : |
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.
|
A CtplInputStream |
|
buffer to fill with the peeked data |
|
number of bytes to peek (must be less than or qual to G_MAXSSIZE , or
a G_IO_ERROR_INVALID_ARGUMENT will be thrown)
|
|
return location for errors, or NULL to ignore them
|
Returns : |
the number of bytes peeked, or -1 on error |
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.
|
A CtplInputStream |
|
Return location for errors, or NULL to ignore them
|
Returns : |
The peeked character, or CTPL_EOF at end of the stream or on error
|
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()
.
|
A CtplInputStream |
|
The maximum number of bytes to peek, even if they still matches, or -1 for no limit. [default -1] |
|
Return location for errors, or NULL to ignore them
|
Returns : |
A newly allocated string containing the peeked symbol, or NULL on
error.
|
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()
.
|
A CtplInputStream |
|
The maximum number of bytes to peek, even if they still matches, or -1 for no limit. [default -1] |
|
Return location for the peeked length, or NULL . [out][allow-none]
|
|
Return location for errors, or NULL to ignore them
|
Returns : |
A newly allocated string containing the peeked symbol, or NULL on
error.
|
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()
.
|
A CtplInputStream |
|
string of the character acceptable for the word |
|
length of accept , can be -1 if accept is 0-terminated
|
|
maximum number of bytes to peek, or -1 for no limit. [default -1] |
|
return location for the length of the read word,
or NULL . [out][allow-none]
|
|
return location for errors, or NULL to ignore them
|
Returns : |
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.
|
A CtplInputStream |
|
Number of bytes to skip |
|
Return location for errors, or NULL to ignore them
|
Returns : |
The number of skipped bytes, or -1 on error. |
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()
.
|
A CtplInputStream |
|
Return location for errors, or NULL to ignore them
|
Returns : |
The number of skipped characters. |
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.
|
A CtplInputStream |
|
A string of the characters to skip |
|
Length of reject , can be -1 if 0-terminated
|
|
Return location for errors, or NULL to ignore them
|
Returns : |
The number of skipped bytes, or -1 on error. |
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.
|
A CtplInputStream |
|
return location for errors, or NULL to ignore them
|
Returns : |
FALSE if not at EOF, TRUE otherwise (note that this includes I/O
error).
|
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()
.
|
A CtplInputStream |
Returns : |
TRUE if at end of stream, FALSE otherwise.
|