![]() |
![]() |
![]() |
CTPL Reference Manual | ![]() |
---|---|---|---|---|
Top | Description |
#include <ctpl/environ.h> #define CTPL_ENVIRON_ERROR enum CtplEnvironError; CtplEnviron; CtplEnviron * ctpl_environ_new (void); void ctpl_environ_free (CtplEnviron *env); const CtplValue * ctpl_environ_lookup (const CtplEnviron *env, const gchar *symbol); void ctpl_environ_push (CtplEnviron *env, const gchar *symbol, const CtplValue *value); void ctpl_environ_push_int (CtplEnviron *env, const gchar *symbol, glong value); void ctpl_environ_push_float (CtplEnviron *env, const gchar *symbol, gdouble value); void ctpl_environ_push_string (CtplEnviron *env, const gchar *symbol, const gchar *value); const CtplValue * ctpl_environ_pop (CtplEnviron *env, const gchar *symbol); gboolean ctpl_environ_add_from_file (CtplEnviron *env, const gchar *filename, GError **error); gboolean ctpl_environ_add_from_mb (CtplEnviron *env, MB *mb, GError **error); gboolean ctpl_environ_add_from_string (CtplEnviron *env, const gchar *string, GError **error);
A CtplEnviron represents an environment of symbols used to lookup, push and pop symbols when computing a template.
Example 7. Creating and filling a environment
1 2 3 4 5 6 7 8 9 |
CtplEnviron *env; env = ctpl_environ_new () ctpl_environ_push_string (env, "symbol name", "symbol value"); ctpl_environ_push_int (env, "response", 42); // ... ctpl_environ_free (env); |
Environments can be loaded from MB, strings or files using
ctpl_environ_add_from_mb()
, ctpl_environ_add_from_string()
or
ctpl_environ_add_from_file()
. Environment descriptions are of the form
SYMBOL = VALUE;
. Some examples below:
Example 8. An environment description
1 2 3 4 5 6 7 |
foo = "string value"; bar = 42; str = "a more complex\" string"; array = [1, 2, "hello", ["world", "dolly"]]; complex_number = 2.12e-9; hex_number = 0xffe2; |
#define CTPL_ENVIRON_ERROR (ctpl_environ_error_quark ())
Error domain of CtplEnviron.
typedef enum _CtplEnvironError { CTPL_ENVIRON_ERROR_LOADER_MISSING_SYMBOL, CTPL_ENVIRON_ERROR_LOADER_MISSING_VALUE, CTPL_ENVIRON_ERROR_LOADER_MISSING_SEPARATOR, CTPL_ENVIRON_ERROR_FAILED } CtplEnvironError;
Errors in the CtplEnviron domain.
CtplEnviron * ctpl_environ_new (void);
Creates a new CtplEnviron
Returns : |
A new CtplEnviron |
void ctpl_environ_free (CtplEnviron *env);
Frees a CtplEnviron and all its allocated resources.
|
a CtplEnviron |
const CtplValue * ctpl_environ_lookup (const CtplEnviron *env, const gchar *symbol);
Looks up for a symbol in the given CtplEnviron.
|
A CtplEnviron |
|
A symbol name |
Returns : |
A CtplValue or NULL if the symbol can't be found. This value
should not be freed.
|
void ctpl_environ_push (CtplEnviron *env, const gchar *symbol, const CtplValue *value);
Pushes a symbol into a CtplEnviron. Pushing a symbol adds it or overwrites the value in place for it while keeping any already present value for latter poping. The push/pop concept is simple as a stack: when you push, to adds a value on to of a stack, and when you pop, you remove the top element of this stack, revealing the previous value.
|
A CtplEnviron |
|
The symbol name |
|
The symbol value |
void ctpl_environ_push_int (CtplEnviron *env, const gchar *symbol, glong value);
Pushes an integer symbol into a CtplEnviron. See ctpl_environ_push()
.
|
A CtplEnviron |
|
A symbol name |
|
The symbol value |
void ctpl_environ_push_float (CtplEnviron *env, const gchar *symbol, gdouble value);
Pushes a float symbol into a CtplEnviron. See ctpl_environ_push()
.
|
A CtplEnviron |
|
A symbol name |
|
The symbol value |
void ctpl_environ_push_string (CtplEnviron *env, const gchar *symbol, const gchar *value);
Pushes a string symbol into a CtplEnviron. See ctpl_environ_push()
.
|
A CtplEnviron |
|
A symbol name |
|
The symbol value |
const CtplValue * ctpl_environ_pop (CtplEnviron *env, const gchar *symbol);
Pops a symbol from a CtplValue. See ctpl_environ_push()
for details on
pushing and poping.
Use ctpl_environ_lookup()
if you want to get the symbol's value without
poping it from the environ.
|
A CtplEnviron |
|
A symbol name |
Returns : |
A CtplValue or NULL if the symbol can't be found. This value
should not be freed.
|
gboolean ctpl_environ_add_from_file (CtplEnviron *env, const gchar *filename, GError **error);
Loads an environment description from a file.
See ctpl_environ_add_from_mb()
.
Errors can come from the G_FILE_ERROR
domain if the file loading failed, or
from the CTPL_ENVIRON_ERROR
domain if the parsing of the environment
description failed.
|
A CtplEnviron to fill |
|
The filename of the file from which load the environment description, in the GLib's filename encoding |
|
Return location for an error, or NULL to ignore them
|
Returns : |
TRUE on success, FALSE otherwise.
|
gboolean ctpl_environ_add_from_mb (CtplEnviron *env, MB *mb, GError **error);
Loads an environment description from a MB.
|
A CtplEnviron to fill |
|
A MB from where read the environment description. |
|
Return location for an error, or NULL to ignore them
|
Returns : |
TRUE on success, FALSE otherwise.
|
gboolean ctpl_environ_add_from_string (CtplEnviron *env, const gchar *string, GError **error);
Loads an environment description from a string.
See ctpl_environ_add_from_mb()
.
|
A CtplEnviron to fill |
|
A string containing an environment description |
|
Return location for an error, or NULL to ignore them
|
Returns : |
TRUE on success, FALSE otherwise.
|