Glib Docs PDF
Glib Docs PDF
ii
Contents
1 GLib Overview 1
1.1 Compiling the GLib package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Cross-compiling the GLib package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Compiling GLib Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Running GLib Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Changes to GLib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6 Regular expression syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7 Mailing lists and bug reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2 GLib Fundamentals 33
2.1 Version Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2 Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.3 Limits of Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.4 Standard Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5 Type Conversion Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.6 Byte Order Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.7 Numerical Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.8 Miscellaneous Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.9 Atomic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
iii
CONTENTS
Index 593
iv
List of Figures
4 GLib Utilities
4.1 Conversion between File Name Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
v
List of Tables
1 GLib Overview
1.1 Metacharacters outside square brackets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2 Metacharacters inside square brackets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3 Non-printing characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4 Non-printing characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.5 Generic characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.6 Generic character types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.7 Property codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.8 Simple assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.9 Posix classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.10 Option settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.11 Abbreviations for quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
vii
Chapter 1
GLib Overview
GLib is a general-purpose utility library, which provides many useful data types, macros, type con-
versions, string utilities, file utilities, a main loop abstraction, and so on. It works on many UNIX-like
platforms, Windows, OS/2 and BeOS. GLib is released under the GNU Library General Public License
(GNU LGPL).
The general policy of GLib is that all functions are invisibly threadsafe with the exception of data
structure manipulation functions, where, if you have two threads manipulating the same data structure,
they must use a lock to synchronize their operation.
./configure
make
make install
The standard options provided by GNU autoconf may be passed to the configure script. Please see
the autoconf documentation or run ./configure --help for information about the standard options.
The GTK+ documentation contains further details about the build process and ways to influence it.
Dependencies
Before you can compile the GLib library, you need to have various other tools and libraries installed on
your system. The two tools needed during the build process (as differentiated from the tools used in
when creating GLib mentioned above such as autoconf) are pkg-config and GNU make.
• pkg-config is a tool for tracking the compilation flags needed for libraries that are used by the GLib
library. (For each library, a small .pc text file is installed in a standard location that contains the
compilation flags needed for that library along with version number information.) The version of
pkg-config needed to build GLib is mirrored in the dependencies directory on the GTK+ FTP
site.
• The GTK+ makefiles will mostly work with different versions of make, however, there tends to be
a few incompatibilities, so the GTK+ team recommends installing GNU make if you don’t already
have it on your system and using it. (It may be called gmake rather than make.)
1
CHAPTER 1. GLIB OVERVIEW 1.1. COMPILING THE GLIB PACKAGE
• The GNU libiconv library is needed to build GLib if your system doesn’t have the iconv() func-
tion for doing conversion between character encodings. Most modern systems should have ic-
onv(), however many older systems lack an iconv() implementation. On such systems, you
must install the libiconv library. This can be found at: http://www.gnu.org/software/libiconv.
If your system has an iconv() implementation but you want to use libiconv instead, you can
pass the --with-libiconv option to configure. This forces libiconv to be used.
Note that if you have libiconv installed in your default include search path (for instance, in /usr/
local/), but don’t enable it, you will get an error while compiling GLib because the iconv.h
that libiconv installs hides the system iconv.
If you are using the native iconv implementation on Solaris instead of libiconv, you’ll need to make
sure that you have the converters between locale encodings and UTF-8 installed. At a minimum
you’ll need the SUNWuiu8 package. You probably should also install the SUNWciu8, SUNWhiu8,
SUNWjiu8, and SUNWkiu8 packages.
The native iconv on Compaq Tru64 doesn’t contain support for UTF-8, so you’ll need to use GNU
libiconv instead. (When using GNU libiconv for GLib, you’ll need to use GNU libiconv for GNU
gettext as well.) This probably applies to related operating systems as well.
• The libintl library from the GNU gettext package is needed if your system doesn’t have the get-
text() functionality for handling message translation databases.
• A thread implementation is needed, unless you want to compile GLib without thread support,
which is not recommended. The thread support in GLib can be based upon several native thread
implementations, e.g. POSIX threads, DCE threads or Solaris threads.
• GRegex uses the PCRE library for regular expression matching. The default is to use the internal
version of PCRE that is patched to use GLib for memory management and Unicode handling. If
you prefer to use the system-supplied PCRE library you can pass the --with-pcre=system option
to configure, but it is not recommended.
• The optional extended attribute support in GIO requires the getxattr() family of functions that may
be provided by glibc or by the standalone libattr library. To build GLib without extended attribute
support, use the --disable-xattr configure option.
• The optional SELinux support in GIO requires libselinux. To build GLib without SELinux support,
use the --disable-selinux configure option.
2
CHAPTER 1. GLIB OVERVIEW 1.1. COMPILING THE GLIB PACKAGE
• When shrinking a GArray, Glib will clear the memory no longer available in the array: shrink an
array from 10 bytes to 7, and the last 3 bytes will be cleared. This includes removals of single and
multiple elements.
•
• When growing a GArray, Glib will clear the new chunk of memory. Grow an array from 7 bytes to
10 bytes, and the last 3 bytes will be cleared.
• The above applies to GPtrArray as well.
• When freeing a node from a GHashTable, Glib will first clear the node, which used to have pointers
to the key and the value stored at that node.
• When destroying or removing a GTree node, Glib will clear the node, which used to have pointers
to the node’s value, and the left and right subnodes.
3
CHAPTER 1. GLIB OVERVIEW 1.2. CROSS-COMPILING THE GLIB PACKAGE
The complete list of cache file variables follows. Most of these won’t need to be set in most cases.
4
CHAPTER 1. GLIB OVERVIEW 1.3. COMPILING GLIB APPLICATIONS
If your application uses threads or GObject features, it must be compiled and linked with the options
returned by the following pkg-config invocations:
$ pkg-config --cflags --libs gthread-2.0
$ pkg-config --cflags --libs gobject-2.0
5
CHAPTER 1. GLIB OVERVIEW 1.4. RUNNING GLIB APPLICATIONS
If your application uses modules, it must be compiled and linked with the options returned by one
of the following pkg-config invocations:
$ pkg-config --cflags --libs gmodule-no-export-2.0
$ pkg-config --cflags --libs gmodule-2.0
The difference between the two is that gmodule-2.0 adds --export-dynamic to the linker flags, which
is often not needed.
The simplest way to compile a program is to use the "backticks" feature of the shell. If you enclose
a command in backticks (not single quotes), then its output will be substituted into the command line
before execution. So to compile a GLib Hello, World, you would type the following:
$ cc ‘pkg-config --cflags --libs glib-2.0‘ hello.c -o hello
If you want to make sure that your program doesn’t use any deprecated functions, you can define
the preprocessor symbol G_DISABLE_DEPRECATED by using the command line option -DG_DISAB-
LE_DEPRECATED=1.
The recommended way of using GLib has always been to only include the toplevel headers glib.h,
glib-object.h, gio.h. Starting with 2.17, GLib enforces this by generating an error when individual
headers are directly included. To help with the transition, the enforcement is not turned on by default
for GLib headers (it is turned on for GObject and GIO). To turn it on, define the preprocessor symbol
G_DISABLE_SINGLE_INCLUDES by using the command line option -DG_DISABLE_SINGLE_INCL-
UDES.
fatal_warnings Causes GLib to abort the program at the first call to g_warning() or g_critical(). This
option is special in that it doesn’t require GLib to be configured with debugging support.
fatal_criticals Causes GLib to abort the program at the first call to g_critical(). This option is special in
that it doesn’t require GLib to be configured with debugging support.
gc-friendly Newly allocated memory that isn’t directly initialized, as well as memory being freed will
be reset to 0. The point here is to allow memory checkers and similar programs that use bohem GC
alike algorithms to produce more accurate results. This option is special in that it doesn’t require
GLib to be configured with debugging support.
6
CHAPTER 1. GLIB OVERVIEW 1.4. RUNNING GLIB APPLICATIONS
resident-modules All modules loaded by GModule will be made resident. This can be useful for track-
ing memory leaks in modules which are later unloaded; but it can also hide bugs where code is
accessed after the module would have normally been unloaded. This option is special in that it
doesn’t require GLib to be configured with debugging support.
bind-now-modules All modules loaded by GModule will bind their symbols at load time, even when
the code uses %G_MODULE_BIND_LAZY. This option is special in that it doesn’t require GLib to
be configured with debugging support.
The special value all can be used to turn on all debug options. The special value help can be used to
print all available options.
G_SLICE
This environment variable allows reconfiguration of the GSlice memory allocator.
always-malloc This will cause all slices allocated through g_slice_alloc() and released by g_slice_free1()
to be actually allocated via direct calls to g_malloc() and g_free(). This is most useful for mem-
ory checkers and similar programs that use Bohem GC alike algorithms to produce more accurate
results. It can also be in conjunction with debugging features of the system’s malloc implemen-
tation such as glibc’s MALLOC_CHECK_=2 to debug erroneous slice allocation code, allthough
debug-blocks usually is a better suited debugging tool.
debug-blocks Using this option (present since GLib-2.13) engages extra code which performs sanity
checks on the released memory slices. Invalid slice adresses or slice sizes will be reported and lead
to a program halt. This option is for debugging scenarios. In particular, client packages sporting
their own test suite should always enable this option when running tests. Global slice validation is en-
sured by storing size and address information for each allocated chunk, and maintaining a global
hash table of that data. That way, multi-thread scalability is given up, and memory consumption
is increased. However, the resulting code usually performs acceptably well, possibly better than
with comparable memory checking carried out using external tools. An example of a memory cor-
ruption scenario that cannot be reproduced with G_SLICE=always-malloc, but will be caught
by G_SLICE=debug-blocks is as follows:
The special value all can be used to turn on all options. The special value help can be used to print all
available options.
G_RANDOM_VERSION
If this environment variable is set to ’2.0’, the outdated pseudo-random number seeding and genera-
tion algorithms from GLib-2.0 are used instead of the new better ones. Use the GLib-2.0 algorithms only
if you have sequences of numbers generated with Glib-2.0 that you need to reproduce exactly.
LIBCHARSET_ALIAS_DIR
Allows to specify a nonstandard location for the charset.aliases file that is used by the character
set conversion routines. The default location is the libdir specified at compilation time.
Locale
A number of interfaces in GLib depend on the current locale in which an application is running. There-
fore, most GLib-using applications should call setlocale (LC_ALL, "") to set up the current locale.
On Windows, in a C program there are several locale concepts that not necessarily are synchronized.
On one hand, there is the system default ANSI code-page, which determines what encoding is used for
file names handled by the C library’s functions and the Win32 API. (We are talking about the "narrow"
functions here that take character pointers, not the "wide" ones.)
On the other hand, there is the C library’s current locale. The character set (code-page) used by that is
not necessarily the same as the system default ANSI code-page. Strings in this character set are returned
by functions like strftime().
7
CHAPTER 1. GLIB OVERVIEW 1.5. CHANGES TO GLIB
If set to a size > 0, g_free(), g_realloc() and g_malloc() will be intercepted if the size matches the size of
the corresponding memory block. This will only work with g_mem_set_vtable (glib_mem_pro-
filer_table) upon startup though, because memory profiling is required to match on the memory
block sizes.
Note that many modern debuggers support conditional breakpoints, which achieve pretty much the
same. E.g. in gdb, you can do
break g_malloc
condition 1 n_bytes == 20
to break only on g_malloc() calls where the size of the allocated memory block is 20.
Memory statistics
g_mem_profile() will output a summary g_malloc() memory usage, if memory profiling has been en-
abled by calling g_mem_set_vtable (glib_mem_profiler_table) upon startup.
If GLib has been configured with --enable-debug=yes, then g_slice_debug_tree_statistics() can
be called in a debugger to output details about the memory usage of the slice allocator.
• The event loop functionality GMain has extensively been revised to support multiple separate
main loops in separate threads. All sources (timeouts, idle functions, etc.) are associated with a
GMainContext.
Compatibility functions exist so that most application code dealing with the main loop will con-
tinue to work. However, code that creates new custom types of sources will require modification.
The main changes here are:
8
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
– Sources are now exposed as GSource *, rather than simply as numeric ids.
– New types of sources are created by structure "derivation" from GSource, so the source_d-
ata parameter to the GSource virtual functions has been replaced with a GSource *.
– Sources are first created, then later added to a specific GMainContext.
– Dispatching has been modified so both the callback and data are passed in to the dispatc-
h() virtual function.
To go along with this change, the vtable for GIOChannel has changed and add_watch() has been
replaced by create_watch().
• g_list_foreach() and g_slist_foreach() have been changed so they are now safe against
removal of the current item, not the next item.
It’s not recommended to mutate the list in the callback to these functions in any case.
• GDate now works in UTF-8, not in the current locale. If you want to use it with the encoding of
the locale, you need to convert strings using g_locale_to_utf8() first.
matches a portion of a string that is identical to itself. When caseless matching is specified (the
G_REGEX_CASELESS flag), letters are matched independently of case.
The power of regular expressions comes from the ability to include alternatives and repetitions in
the pattern. These are encoded in the pattern by the use of metacharacters, which do not stand for
themselves but instead are interpreted in some special way.
There are two different sets of metacharacters: those that are recognized anywhere in the pattern ex-
cept within square brackets, and those that are recognized in square brackets. Outside square brackets,
the metacharacters are as follows:
Part of a pattern that is in square brackets is called a "character class". In a character class the only
metacharacters are:
Backslash
The backslash character has several uses. Firstly, if it is followed by a non-alphanumeric character, it
takes away any special meaning that character may have. This use of backslash as an escape character
applies both inside and outside character classes.
For example, if you want to match a * character, you write \* in the pattern. This escaping action
applies whether or not the following character would otherwise be interpreted as a metacharacter, so
9
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
it is always safe to precede a non-alphanumeric with backslash to specify that it stands for itself. In
particular, if you want to match a backslash, you write \\.
If a pattern is compiled with the G_REGEX_EXTENDED option, whitespace in the pattern (other than
in a character class) and characters between a # outside a character class and the next newline are ig-
nored. An escaping backslash can be used to include a whitespace or # character as part of the pattern.
If you want to remove the special meaning from a sequence of characters, you can do so by putting
them between \Q and \E. The \Q...\E sequence is recognized both inside and outside character classes.
Non-printing characters
A second use of backslash provides a way of encoding non-printing characters in patterns in a visible
manner. There is no restriction on the appearance of non-printing characters, apart from the binary zero
that terminates a pattern, but when a pattern is being prepared by text editing, it is usually easier to use
one of the following escape sequences than the binary character it represents:
The precise effect of \cx is as follows: if x is a lower case letter, it is converted to upper case. Then
10
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
bit 6 of the character (hex 40) is inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c;
becomes hex 7B.
After \x, from zero to two hexadecimal digits are read (letters can be in upper or lower case). Any
number of hexadecimal digits may appear between \x{ and }, but the value of the character code must be
less than 2**31 (that is, the maximum hexadecimal value is 7FFFFFFF). If characters other than hexadeci-
mal digits appear between \x{ and }, or if there is no terminating }, this form of escape is not recognized.
Instead, the initial \x will be interpreted as a basic hexadecimal escape, with no following digits, giving
a character whose value is zero.
Characters whose value is less than 256 can be defined by either of the two syntaxes for \x. There is
no difference in the way they are handled. For example, \xdc is exactly the same as \x{dc}.
After \0 up to two further octal digits are read. If there are fewer than two digits, just those that are
present are used. Thus the sequence \0\x\07 specifies two binary zeros followed by a BEL character
(code value 7). Make sure you supply two digits after the initial zero if the pattern character that follows
is itself an octal digit.
The handling of a backslash followed by a digit other than 0 is complicated. Outside a character
class, GRegex reads it and any following digits as a decimal number. If the number is less than 10, or
if there have been at least that many previous capturing left parentheses in the expression, the entire
sequence is taken as a back reference. A description of how this works is given later, following the
discussion of parenthesized subpatterns.
Inside a character class, or if the decimal number is greater than 9 and there have not been that many
capturing subpatterns, GRegex re-reads up to three octal digits following the backslash, and uses them
to generate a data character. Any subsequent digits stand for themselves. For example:
Note that octal values of 100 or greater must not be introduced by a leading zero, because no more
than three octal digits are ever read.
All the sequences that define a single character can be used both inside and outside character classes.
In addition, inside a character class, the sequence \b is interpreted as the backspace character (hex 08),
and the sequences \R and \X are interpreted as the characters "R" and "X", respectively. Outside a
character class, these sequences have different meanings (see below).
11
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
These character type sequences can appear both inside and outside character classes. They each
match one character of the appropriate type. If the current matching point is at the end of the passed
string, all of them fail, since there is no character to match.
For compatibility with Perl, \s does not match the VT character (code 11). This makes it different
from the the POSIX "space" class. The \s characters are HT (9), LF (10), FF (12), CR (13), and space (32).
A "word" character is an underscore or any character less than 256 that is a letter or digit.
Characters with values greater than 128 never match \d, \s, or \w, and always match \D, \S, and
\W.
Newline sequences
Outside a character class, the escape sequence \R matches any Unicode newline sequence. This partic-
ular group matches either the two-character sequence CR followed by LF, or one of the single characters
LF (linefeed, U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D),
NEL (next line, U+0085), LS (line separator, U+2028), or PS (paragraph separator, U+2029). The two-
character sequence is treated as a single unit that cannot be split. Inside a character class, \R matches
the letter "R".
The property names represented by xx above are limited to the Unicode script names, the general
category properties, and "Any", which matches any character (including newline). Other properties such
as "InMusicalSymbols" are not currently supported. Note that \P{Any} does not match any characters,
so always causes a match failure.
Sets of Unicode characters are defined as belonging to certain scripts. A character from one of these
sets can be matched using a script name. For example, \p{Greek} or \P{Han}.
Those that are not part of an identified script are lumped together as "Common". The current list of
scripts is:
• Arabic
• Armenian
• Balinese
• Bengali
• Bopomofo
• Braille
• Buginese
12
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
• Buhid
• Canadian_Aboriginal
• Cherokee
• Common
• Coptic
• Cuneiform
• Cypriot
• Cyrillic
• Deseret
• Devanagari
• Ethiopic
• Georgian
• Glagolitic
• Gothic
• Greek
• Gujarati
• Gurmukhi
• Han
• Hangul
• Hanunoo
• Hebrew
• Hiragana
• Inherited
• Kannada
• Katakana
• Kharoshthi
• Khmer
• Lao
• Latin
• Limbu
• Linear_B
• Malayalam
• Mongolian
• Myanmar
• New_Tai_Lue
• Nko
13
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
• Ogham
• Old_Italic
• Old_Persian
• Oriya
• Osmanya
• Phags_Pa
• Phoenician
• Runic
• Shavian
• Sinhala
• Syloti_Nagri
• Syriac
• Tagalog
• Tagbanwa
• Tai_Le
• Tamil
• Telugu
• Thaana
• Thai
• Tibetan
• Tifinagh
• Ugaritic
• Yi
Each character has exactly one general category property, specified by a two-letter abbreviation. For
compatibility with Perl, negation can be specified by including a circumflex between the opening brace
and the property name. For example, \p{ˆLu} is the same as \P{Lu}.
If only one letter is specified with \p or \P, it includes all the general category properties that start
with that letter. In this case, in the absence of negation, the curly brackets in the escape sequence are
optional; these two examples have the same effect:
\p{L}
\pL
14
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
15
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
(?>\PM\pM*)
That is, it matches a character without the "mark" property, followed by zero or more characters with
the "mark" property, and treats the sequence as an atomic group (see below). Characters with the "mark"
property are typically accents that affect the preceding character.
Matching characters by Unicode property is not fast, because GRegex has to search a structure that
contains data for over fifteen thousand characters. That is why the traditional escape sequences such as
\d and \w do not use Unicode properties.
Simple assertions
The final use of backslash is for certain simple assertions. An assertion specifies a condition that has to
be met at a particular point in a match, without consuming any characters from the string. The use of
subpatterns for more complicated assertions is described below. The backslashed assertions are:
These assertions may not appear in character classes (but note that \b has a different meaning,
namely the backspace character, inside a character class).
A word boundary is a position in the string where the current character and the previous character
do not both match \w or \W (i.e. one matches \w and the other matches \W), or the start or end of the
string if the first or last character matches \w, respectively.
The \A, \Z, and \z assertions differ from the traditional circumflex and dollar (described in the
next section) in that they only ever match at the very start and end of the string, whatever options are
set. Thus, they are independent of multiline mode. These three assertions are not affected by the G_-
REGEX_MATCH_NOTBOL or G_REGEX_MATCH_NOTEOL options, which affect only the behaviour of the
circumflex and dollar metacharacters. However, if the start_position argument of a matching function
is non-zero, indicating that matching is to start at a point other than the beginning of the string, \A can
never match. The difference between \Z and \z is that \Z matches before a newline at the end of the
string as well at the very end, whereas \z matches only at the end.
The \G assertion is true only when the current matching position is at the start point of the match, as
specified by the start_position argument to the matching functions. It differs from \A when the value
of startoffset is non-zero.
Note, however, that the interpretation of \G, as the start of the current match, is subtly different
from Perl’s, which defines it as the end of the previous match. In Perl, these can be different when the
previously matched string was empty.
If all the alternatives of a pattern begin with \G, the expression is anchored to the starting match
position, and the "anchored" flag is set in the compiled regular expression.
16
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
A dollar character is an assertion that is true only if the current matching point is at the end of the
string, or immediately before a newline at the end of the string (by default). Dollar need not be the last
character of the pattern if a number of alternatives are involved, but it should be the last item in any
branch in which it appears. Dollar has no special meaning in a character class.
The meaning of dollar can be changed so that it matches only at the very end of the string, by setting
the G_REGEX_DOLLAR_ENDONLY option at compile time. This does not affect the \Z assertion.
The meanings of the circumflex and dollar characters are changed if the G_REGEX_MULTILINE op-
tion is set. When this is the case, a circumflex matches immediately after internal newlines as well as at
the start of the string. It does not match after a newline that ends the string. A dollar matches before any
newlines in the string, as well as at the very end, when G_REGEX_MULTILINE is set. When newline is
specified as the two-character sequence CRLF, isolated CR and LF characters do not indicate newlines.
For example, the pattern /ˆabc$/ matches the string "def\nabc" (where \n represents a newline) in
multiline mode, but not otherwise. Consequently, patterns that are anchored in single line mode because
all branches start with ˆ are not anchored in multiline mode, and a match for circumflex is possible when
the start_position argument of a matching function is non-zero. The G_REGEX_DOLLAR_ENDONLY
option is ignored if G_REGEX_MULTILINE is set.
Note that the sequences \A, \Z, and \z can be used to match the start and end of the string in both
modes, and if all branches of a pattern start with \A it is always anchored, whether or not G_REGEX_-
MULTILINE is set.
17
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
For example, the character class [aeiou] matches any lower case vowel, while [ˆaeiou] matches any
character that is not a lower case vowel. Note that a circumflex is just a convenient notation for spec-
ifying the characters that are in the class by enumerating those that are not. A class that starts with a
circumflex is not an assertion: it still consumes a character from the string, and therefore it fails if the
current pointer is at the end of the string.
In UTF-8 mode, characters with values greater than 255 can be included in a class as a literal string
of bytes, or by using the \x{ escaping mechanism.
When caseless matching is set, any letters in a class represent both their upper case and lower case
versions, so for example, a caseless [aeiou] matches "A" as well as "a", and a caseless [ˆaeiou] does not
match "A", whereas a caseful version would.
Characters that might indicate line breaks are never treated in any special way when matching char-
acter classes, whatever line-ending sequence is in use, and whatever setting of the G_REGEX_DOTALL
and G_REGEX_MULTILINE options is used. A class such as [ˆa] always matches one of these characters.
The minus (hyphen) character can be used to specify a range of characters in a character class. For
example, [d-m] matches any letter between d and m, inclusive. If a minus character is required in a class,
it must be escaped with a backslash or appear in a position where it cannot be interpreted as indicating
a range, typically as the first or last character in the class.
It is not possible to have the literal character "]" as the end character of a range. A pattern such as
[W-]46] is interpreted as a class of two characters ("W" and "-") followed by a literal string "46]", so it
would match "W46]" or "-46]". However, if the "]" is escaped with a backslash it is interpreted as the end
of range, so [W-\]46] is interpreted as a class containing a range followed by two other characters. The
octal or hexadecimal representation of "]" can also be used to end a range.
Ranges operate in the collating sequence of character values. They can also be used for characters
specified numerically, for example [\000-\037]. In UTF-8 mode, ranges can include characters whose
values are greater than 255, for example [\x{100}-\x{2ff}].
The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear in a character class, and add
the characters that they match to the class. For example, [\dABCDEF] matches any hexadecimal digit.
A circumflex can conveniently be used with the upper case character types to specify a more restricted
set of characters than the matching lower case type. For example, the class [ˆ\W_] matches any letter or
digit, but not underscore.
The only metacharacters that are recognized in character classes are backslash, hyphen (only where
it can be interpreted as specifying a range), circumflex (only at the start), opening square bracket (only
when it can be interpreted as introducing a POSIX class name - see the next section), and the terminating
closing square bracket. However, escaping other non-alphanumeric characters does no harm.
matches "0", "1", any alphabetic character, or "%". The supported class names are
The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and space (32). Notice that this
list includes the VT character (code 11). This makes "space" different to \s, which does not include VT
(for Perl compatibility).
The name "word" is a Perl extension, and "blank" is a GNU extension. Another Perl extension is
negation, which is indicated by a ˆ character after the colon. For example,
[12[:^digit:]]
matches "1", "2", or any non-digit. GRegex also recognize the POSIX syntax [.ch.] and [=ch=] where
"ch" is a "collating element", but these are not supported, and an error is given if they are encountered.
In UTF-8 mode, characters with values greater than 128 do not match any of the POSIX character
classes.
Vertical bar
Vertical bar characters are used to separate alternative patterns. For example, the pattern
gilbert|sullivan
18
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
matches either "gilbert" or "sullivan". Any number of alternatives may appear, and an empty alter-
native is permitted (matching the empty string). The matching process tries each alternative in turn,
from left to right, and the first one that succeeds is used. If the alternatives are within a subpattern
(defined below), "succeeds" means matching the rest of the main pattern as well as the alternative in the
subpattern.
For example, (?im) sets caseless, multiline matching. It is also possible to unset these options by
preceding the letter with a hyphen, and a combined setting and unsetting such as (?im-sx), which sets
G_REGEX_CASELESS and G_REGEX_MULTILINE while unsetting G_REGEX_DOTALL and G_REGEX_-
EXTENDED, is also permitted. If a letter appears both before and after the hyphen, the option is unset.
When an option change occurs at top level (that is, not inside subpattern parentheses), the change
applies to the remainder of the pattern that follows.
An option change within a subpattern (see below for a description of subpatterns) affects only that
part of the current pattern that follows it, so
(a(?i)b)c
matches abc and aBc and no other strings (assuming G_REGEX_CASELESS is not used). By this
means, options can be made to have different settings in different parts of the pattern. Any changes
made in one alternative do carry on into subsequent branches within the same subpattern. For example,
(a(?i)b|c)
matches "ab", "aB", "c", and "C", even though when matching "C" the first branch is abandoned before
the option setting. This is because the effects of option settings happen at compile time. There would be
some very weird behaviour otherwise.
The options G_REGEX_UNGREEDY and G_REGEX_EXTRA and G_REGEX_DUPNAMES can be changed
in the same way as the Perl-compatible options by using the characters U, X and J respectively.
19
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
Subpatterns
Subpatterns are delimited by parentheses (round brackets), which can be nested. Turning part of a
pattern into a subpattern does two things:
• It localizes a set of alternatives. For example, the pattern cat(aract|erpillar|) matches one of the
words "cat", "cataract", or "caterpillar". Without the parentheses, it would match "cataract", "erpil-
lar" or an empty string.
• It sets up the subpattern as a capturing subpattern. This means that, when the whole pattern
matches, that portion of the string that matched the subpattern can be obtained using g_regex-
_fetch(). Opening parentheses are counted from left to right (starting from 1, as subpattern 0 is
the whole matched string) to obtain numbers for the capturing subpatterns.
For example, if the string "the red king" is matched against the pattern
the ((red|white) (king|queen))
the captured substrings are "red king", "red", and "king", and are numbered 1, 2, and 3, respectively.
The fact that plain parentheses fulfil two functions is not always helpful. There are often times
when a grouping subpattern is required without a capturing requirement. If an opening parenthesis is
followed by a question mark and a colon, the subpattern does not do any capturing, and is not counted
when computing the number of any subsequent capturing subpatterns. For example, if the string "the
white queen" is matched against the pattern
the ((?:red|white) (king|queen))
the captured substrings are "white queen" and "queen", and are numbered 1 and 2. The maximum
number of capturing subpatterns is 65535.
As a convenient shorthand, if any option settings are required at the start of a non-capturing subpat-
tern, the option letters may appear between the "?" and the ":". Thus the two patterns
(?i:saturday|sunday)
(?:(?i)saturday|sunday)
match exactly the same set of strings. Because alternative branches are tried from left to right, and
options are not reset until the end of the subpattern is reached, an option setting in one branch does
affect subsequent branches, so the above patterns match "SUNDAY" as well as "Saturday".
Named subpatterns
Identifying capturing parentheses by number is simple, but it can be very hard to keep track of the
numbers in complicated regular expressions. Furthermore, if an expression is modified, the numbers
may change. To help with this difficulty, GRegex supports the naming of subpatterns. A subpattern can
be named in one of three ways: (?<name>...) or (?’name’...) as in Perl, or (?P<name>...) as in Python.
References to capturing parentheses from other parts of the pattern, such as backreferences, recursion,
and conditions, can be made by name as well as by number.
Names consist of up to 32 alphanumeric characters and underscores. Named capturing parentheses
are still allocated numbers as well as names, exactly as if the names were not present. By default, a
name must be unique within a pattern, but it is possible to relax this constraint by setting the G_REG-
EX_DUPNAMES option at compile time. This can be useful for patterns where only one instance of the
named parentheses can match. Suppose you want to match the name of a weekday, either as a 3-letter
abbreviation or as the full name, and in both cases you want to extract the abbreviation. This pattern
(ignoring the line breaks) does the job:
(?<DN>Mon|Fri|Sun)(?:day)?|
(?<DN>Tue)(?:sday)?|
(?<DN>Wed)(?:nesday)?|
(?<DN>Thu)(?:rsday)?|
(?<DN>Sat)(?:urday)?
There are five capturing substrings, but only one is ever set after a match. The function for extracting
the data by name returns the substring for the first (and in this example, the only) subpattern of that
20
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
name that matched. This saves searching to find which numbered subpattern it was. If you make a
reference to a non-unique named subpattern from elsewhere in the pattern, the one that corresponds to
the lowest number is used.
Repetition
Repetition is specified by quantifiers, which can follow any of the following items:
• a character class
The general repetition quantifier specifies a minimum and maximum number of permitted matches,
by giving the two numbers in curly brackets (braces), separated by a comma. The numbers must be less
than 65536, and the first must be less than or equal to the second. For example:
z{2,4}
matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special character. If the second
number is omitted, but the comma is present, there is no upper limit; if the second number and the
comma are both omitted, the quantifier specifies an exact number of required matches. Thus
[aeiou]{3,}
matches at least 3 successive vowels, but may match many more, while
\d{8}
matches exactly 8 digits. An opening curly bracket that appears in a position where a quantifier is
not allowed, or one that does not match the syntax of a quantifier, is taken as a literal character. For
example, {,6} is not a quantifier, but a literal string of four characters.
In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual bytes. Thus, for
example, \x{100}{2} matches two UTF-8 characters, each of which is represented by a two-byte sequence.
Similarly, \X{3} matches three Unicode extended sequences, each of which may be several bytes long
(and they may be of different lengths).
The quantifier {0} is permitted, causing the expression to behave as if the previous item and the
quantifier were not present.
For convenience, the three most common quantifiers have single-character abbreviations:
It is possible to construct infinite loops by following a subpattern that can match no characters with
a quantifier that has no upper limit, for example:
(a?)*
21
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
Because there are cases where this can be useful, such patterns are accepted, but if any repetition of
the subpattern does in fact match no characters, the loop is forcibly broken.
By default, the quantifiers are "greedy", that is, they match as much as possible (up to the maximum
number of permitted times), without causing the rest of the pattern to fail. The classic example of where
this gives problems is in trying to match comments in C programs. These appear between /* and */ and
within the comment, individual * and / characters may appear. An attempt to match C comments by
applying the pattern
/\*.*\*/
to the string
fails, because it matches the entire string owing to the greediness of the .* item.
However, if a quantifier is followed by a question mark, it ceases to be greedy, and instead matches
the minimum number of times possible, so the pattern
/\*.*?\*/
does the right thing with the C comments. The meaning of the various quantifiers is not otherwise
changed, just the preferred number of matches. Do not confuse this use of question mark with its use as
a quantifier in its own right. Because it has two uses, it can sometimes appear doubled, as in
\d??\d
which matches one digit by preference, but can match two if that is the only way the rest of the
pattern matches.
If the G_REGEX_UNGREEDY flag is set, the quantifiers are not greedy by default, but individual ones
can be made greedy by following them with a question mark. In other words, it inverts the default
behaviour.
When a parenthesized subpattern is quantified with a minimum repeat count that is greater than 1
or with a limited maximum, more memory is required for the compiled pattern, in proportion to the
size of the minimum or maximum.
If a pattern starts with .* or .{0,} and the G_REGEX_DOTALL flag is set, thus allowing the dot to
match newlines, the pattern is implicitly anchored, because whatever follows will be tried against every
character position in the string, so there is no point in retrying the overall match at any position after
the first. GRegex normally treats such a pattern as though it were preceded by \A.
In cases where it is known that the string contains no newlines, it is worth setting G_REGEX_DOTALL
in order to obtain this optimization, or alternatively using ˆ to indicate anchoring explicitly.
However, there is one situation where the optimization cannot be used. When .* is inside capturing
parentheses that are the subject of a backreference elsewhere in the pattern, a match at the start may fail
where a later one succeeds. Consider, for example:
(.*)abc\1
If the string is "xyz123abc123" the match point is the fourth character. For this reason, such a pattern
is not implicitly anchored.
When a capturing subpattern is repeated, the value captured is the substring that matched the final
iteration. For example, after
(tweedle[dume]{3}\s*)+
has matched "tweedledum tweedledee" the value of the captured substring is "tweedledee". How-
ever, if there are nested capturing subpatterns, the corresponding captured values may have been set in
previous iterations. For example, after
/(a|(b))+/
22
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
After matching all 6 digits and then failing to match "foo", the normal action of the matcher is to try
again with only 5 digits matching the \d+ item, and then with 4, and so on, before ultimately failing.
"Atomic grouping" (a term taken from Jeffrey Friedl’s book) provides the means for specifying that once
a subpattern has matched, it is not to be re-evaluated in this way.
If we use atomic grouping for the previous example, the matcher give up immediately on failing
to match "foo" the first time. The notation is a kind of special parenthesis, starting with (?> as in this
example:
(?>\d+)foo
This kind of parenthesis "locks up" the part of the pattern it contains once it has matched, and a
failure further into the pattern is prevented from backtracking into it. Backtracking past it to previous
items, however, works as normal.
An alternative description is that a subpattern of this type matches the string of characters that an
identical standalone pattern would match, if anchored at the current point in the string.
Atomic grouping subpatterns are not capturing subpatterns. Simple cases such as the above example
can be thought of as a maximizing repeat that must swallow everything it can. So, while both \d+ and
\d+? are prepared to adjust the number of digits they match in order to make the rest of the pattern
match, (?>\d+) can only match an entire sequence of digits.
Atomic groups in general can of course contain arbitrarily complicated subpatterns, and can be
nested. However, when the subpattern for an atomic group is just a single repeated item, as in the
example above, a simpler notation, called a "possessive quantifier" can be used. This consists of an ad-
ditional + character following a quantifier. Using this notation, the previous example can be rewritten
as
\d++foo
Possessive quantifiers are always greedy; the setting of the G_REGEX_UNGREEDY option is ignored.
They are a convenient notation for the simpler forms of atomic group. However, there is no difference
in the meaning of a possessive quantifier and the equivalent atomic group, though there may be a
performance difference; possessive quantifiers should be slightly faster.
The possessive quantifier syntax is an extension to the Perl syntax. It was invented by Jeffrey Friedl
in the first edition of his book and then implemented by Mike McCloskey in Sun’s Java package. It
ultimately found its way into Perl at release 5.10.
GRegex has an optimization that automatically "possessifies" certain simple pattern constructs. For
example, the sequence A+B is treated as A++B because there is no point in backtracking into a sequence
of A’s when B must follow.
When a pattern contains an unlimited repeat inside a subpattern that can itself be repeated an unlim-
ited number of times, the use of an atomic group is the only way to avoid some failing matches taking a
very long time indeed. The pattern
(\D+|<\d+>)*[!?]
matches an unlimited number of substrings that either consist of non- digits, or digits enclosed in
<>, followed by either ! or ?. When it matches, it runs quickly. However, if it is applied to
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
it takes a long time before reporting failure. This is because the string can be divided between the
internal \D+ repeat and the external * repeat in a large number of ways, and all have to be tried. (The
example uses [!?] rather than a single character at the end, because GRegex has an optimization that
allows for fast failure when a single character is used. It remember the last single character that is
23
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
required for a match, and fail early if it is not present in the string.) If the pattern is changed so that it
uses an atomic group, like this:
((?>\D+)|<\d+>)*[!?]
Back references
Outside a character class, a backslash followed by a digit greater than 0 (and possibly further digits) is
a back reference to a capturing subpattern earlier (that is, to its left) in the pattern, provided there have
been that many previous capturing left parentheses.
However, if the decimal number following the backslash is less than 10, it is always taken as a back
reference, and causes an error only if there are not that many capturing left parentheses in the entire
pattern. In other words, the parentheses that are referenced need not be to the left of the reference
for numbers less than 10. A "forward back reference" of this type can make sense when a repetition is
involved and the subpattern to the right has participated in an earlier iteration.
It is not possible to have a numerical "forward back reference" to subpattern whose number is 10 or
more using this syntax because a sequence such as \e50 is interpreted as a character defined in octal.
See the subsection entitled "Non-printing characters" above for further details of the handling of digits
following a backslash. There is no such problem when named parentheses are used. A back reference to
any subpattern is possible using named parentheses (see below).
Another way of avoiding the ambiguity inherent in the use of digits following a backslash is to
use the \g escape sequence (introduced in Perl 5.10.) This escape must be followed by a positive or a
negative number, optionally enclosed in braces.
A positive number specifies an absolute reference without the ambiguity that is present in the older
syntax. It is also useful when literal digits follow the reference. A negative number is a relative reference.
Consider "(abc(def)ghi)\g{-1}", the sequence \g{-1} is a reference to the most recently started capturing
subpattern before \g, that is, is it equivalent to \2. Similarly, \g{-2} would be equivalent to \1. The
use of relative references can be helpful in long patterns, and also in patterns that are created by joining
together fragments that contain references within themselves.
A back reference matches whatever actually matched the capturing subpattern in the current string,
rather than anything matching the subpattern itself (see "Subpatterns as subroutines" below for a way
of doing that). So the pattern
(sens|respons)e and \1ibility
matches "sense and sensibility" and "response and responsibility", but not "sense and responsibility".
If caseful matching is in force at the time of the back reference, the case of letters is relevant. For example,
((?i)rah)\s+\1
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original capturing subpat-
tern is matched caselessly.
Back references to named subpatterns use the Perl syntax \k<name> or \k’name’ or the Python
syntax (?P=name). We could rewrite the above example in either of the following ways:
(?<p1>(?i)rah)\s+\k<p1>
(?P<p1>(?i)rah)\s+(?P=p1)
A subpattern that is referenced by name may appear in the pattern before or after the reference.
There may be more than one back reference to the same subpattern. If a subpattern has not actually
been used in a particular match, any back references to it always fail. For example, the pattern
(a|(bc))\2
always fails if it starts to match "a" rather than "bc". Because there may be many capturing parenthe-
ses in a pattern, all digits following the backslash are taken as part of a potential back reference number.
If the pattern continues with a digit character, some delimiter must be used to terminate the back refer-
ence. If the G_REGEX_EXTENDED flag is set, this can be whitespace. Otherwise an empty comment (see
"Comments" below) can be used.
A back reference that occurs inside the parentheses to which it refers fails when the subpattern is
first used, so, for example, (a\1) never matches. However, such references can be useful inside repeated
subpatterns. For example, the pattern
24
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
(a|b\1)+
matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of the subpattern, the
back reference matches the character string corresponding to the previous iteration. In order for this to
work, the pattern must be such that the first iteration does not need to match the back reference. This
can be done using alternation, as in the example above, or by a quantifier with a minimum of zero.
Assertions
An assertion is a test on the characters following or preceding the current matching point that does not
actually consume any characters. The simple assertions coded as \b, \B, \A, \G, \Z, \z, ˆ and $ are
described above.
More complicated assertions are coded as subpatterns. There are two kinds: those that look ahead
of the current position in the string, and those that look behind it. An assertion subpattern is matched
in the normal way, except that it does not cause the current matching position to be changed.
Assertion subpatterns are not capturing subpatterns, and may not be repeated, because it makes no
sense to assert the same thing several times. If any kind of assertion contains capturing subpatterns
within it, these are counted for the purposes of numbering the capturing subpatterns in the whole pat-
tern. However, substring capturing is carried out only for positive assertions, because it does not make
sense for negative assertions.
Lookahead assertions
Lookahead assertions start with (?= for positive assertions and (?! for negative assertions. For example,
\w+(?=;)
matches a word followed by a semicolon, but does not include the semicolon in the match, and
foo(?!bar)
matches any occurrence of "foo" that is not followed by "bar". Note that the apparently similar
pattern
(?!foo)bar
does not find an occurrence of "bar" that is preceded by something other than "foo"; it finds any oc-
currence of "bar" whatsoever, because the assertion (?!foo) is always true when the next three characters
are "bar". A lookbehind assertion is needed to achieve the other effect.
If you want to force a matching failure at some point in a pattern, the most convenient way to do it is
with (?!) because an empty string always matches, so an assertion that requires there not to be an empty
string must always fail.
Lookbehind assertions
Lookbehind assertions start with (?<= for positive assertions and (?<! for negative assertions. For exam-
ple,
(?<!foo)bar
does find an occurrence of "bar" that is not preceded by "foo". The contents of a lookbehind assertion
are restricted such that all the strings it matches must have a fixed length. However, if there are several
top-level alternatives, they do not all have to have the same fixed length. Thus
(?<=bullock|donkey)
is permitted, but
(?<!dogs?|cats?)
causes an error at compile time. Branches that match different length strings are permitted only at
the top level of a lookbehind assertion. An assertion such as
(?<=ab(c|de))
25
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
is not permitted, because its single top-level branch can match two different lengths, but it is accept-
able if rewritten to use two top- level branches:
(?<=abc|abde)
The implementation of lookbehind assertions is, for each alternative, to temporarily move the current
position back by the fixed length and then try to match. If there are insufficient characters before the
current position, the assertion fails.
GRegex does not allow the \C escape (which matches a single byte in UTF-8 mode) to appear in
lookbehind assertions, because it makes it impossible to calculate the length of the lookbehind. The \X
and \R escapes, which can match different numbers of bytes, are also not permitted.
Possessive quantifiers can be used in conjunction with lookbehind assertions to specify efficient
matching at the end of the subject string. Consider a simple pattern such as
abcd$
when applied to a long string that does not match. Because matching proceeds from left to right,
GRegex will look for each "a" in the string and then see if what follows matches the rest of the pattern.
If the pattern is specified as
^.*abcd$
the initial .* matches the entire string at first, but when this fails (because there is no following "a"),
it backtracks to match all but the last character, then all but the last two characters, and so on. Once
again the search for "a" covers the entire string, from right to left, so we are no better off. However, if
the pattern is written as
^.*+(?<=abcd)
there can be no backtracking for the .*+ item; it can match only the entire string. The subsequent
lookbehind assertion does a single test on the last four characters. If it fails, the match fails immediately.
For long strings, this approach makes a significant difference to the processing time.
matches "foo" preceded by three digits that are not "999". Notice that each of the assertions is applied
independently at the same point in the string. First there is a check that the previous three characters
are all digits, and then there is a check that the same three characters are not "999". This pattern does not
match "foo" preceded by six characters, the first of which are digits and the last three of which are not
"999". For example, it doesn’t match "123abcfoo". A pattern to do that is
(?<=\d{3}...)(?<!999)foo
This time the first assertion looks at the preceding six characters, checking that the first three are
digits, and then the second assertion checks that the preceding three characters are not "999".
Assertions can be nested in any combination. For example,
(?<=(?<!foo)bar)baz
matches an occurrence of "baz" that is preceded by "bar" which in turn is not preceded by "foo", while
(?<=\d{3}(?!999)...)foo
is another pattern that matches "foo" preceded by three digits and any three characters that are not
"999".
Conditional subpatterns
It is possible to cause the matching process to obey a subpattern conditionally or to choose between
two alternative subpatterns, depending on the result of an assertion, or whether a previous capturing
subpattern matched or not. The two possible forms of conditional subpattern are
26
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
(?(condition)yes-pattern)
(?(condition)yes-pattern|no-pattern)
If the condition is satisfied, the yes-pattern is used; otherwise the no-pattern (if present) is used. If
there are more than two alternatives in the subpattern, a compile-time error occurs.
There are four kinds of condition: references to subpatterns, references to recursion, a pseudo-
condition called DEFINE, and assertions.
The first part matches an optional opening parenthesis, and if that character is present, sets it as the
first captured substring. The second part matches one or more characters that are not parentheses. The
third part is a conditional subpattern that tests whether the first set of parentheses matched or not. If
they did, that is, if string started with an opening parenthesis, the condition is true, and so the yes-
pattern is executed and a closing parenthesis is required. Otherwise, since no-pattern is not present,
the subpattern matches nothing. In other words, this pattern matches a sequence of non-parentheses,
optionally enclosed in parentheses.
the condition is true if the most recent recursion is into the subpattern whose number or name is
given. This condition does not check the entire recursion stack.
At "top level", all these recursion test conditions are false. Recursive patterns are described below.
27
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
The first part of the pattern is a DEFINE group inside which a another group named "byte" is defined.
This matches an individual component of an IPv4 address (a number less than 256). When matching
takes place, this part of the pattern is skipped because DEFINE acts like a false condition.
The rest of the pattern uses references to the named group to match the four dot-separated compo-
nents of an IPv4 address, insisting on a word boundary at each end.
Assertion conditions
If the condition is not in any of the above formats, it must be an assertion. This may be a positive or
negative lookahead or lookbehind assertion. Consider this pattern, again containing non-significant
white space, and with the two alternatives on the second line:
(?(?=[^a-z]*[a-z])
\d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
The condition is a positive lookahead assertion that matches an optional sequence of non-letters
followed by a letter. In other words, it tests for the presence of at least one letter in the string. If a letter
is found, the string is matched against the first alternative; otherwise it is matched against the second.
This pattern matches strings in one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are letters and
dd are digits.
Comments
The sequence (?# marks the start of a comment that continues up to the next closing parenthesis. Nested
parentheses are not permitted. The characters that make up a comment play no part in the pattern
matching at all.
If the G_REGEX_EXTENDED option is set, an unescaped # character outside a character class intro-
duces a comment that continues to immediately after the next newline in the pattern.
Recursive patterns
Consider the problem of matching a string in parentheses, allowing for unlimited nested parentheses.
Without the use of recursion, the best that can be done is to use a pattern that matches up to some fixed
depth of nesting. It is not possible to handle an arbitrary nesting depth.
For some time, Perl has provided a facility that allows regular expressions to recurse (amongst other
things). It does this by interpolating Perl code in the expression at run time, and the code can refer to
the expression itself. A Perl pattern using code interpolation to solve the parentheses problem can be
created like this:
$re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;
The (?p{...}) item interpolates Perl code at run time, and in this case refers recursively to the pattern
in which it appears.
Obviously, GRegex cannot support the interpolation of Perl code. Instead, it supports special syntax
for recursion of the entire pattern, and also for individual subpattern recursion. This kind of recursion
was introduced into Perl at release 5.10.
A special item that consists of (? followed by a number greater than zero and a closing parenthesis
is a recursive call of the subpattern of the given number, provided that it occurs inside that subpattern.
(If not, it is a "subroutine" call, which is described in the next section.) The special item (?R) or (?0) is a
recursive call of the entire regular expression.
In GRegex (like Python, but unlike Perl), a recursive subpattern call is always treated as an atomic
group. That is, once it has matched some of the subject string, it is never re-entered, even if it contains
untried alternatives and there is a subsequent matching failure.
This pattern solves the nested parentheses problem (assume the G_REGEX_EXTENDED option is set
so that white space is ignored):
\( ( (?>[^()]+) | (?R) )* \)
First it matches an opening parenthesis. Then it matches any number of substrings which can either
be a sequence of non-parentheses, or a recursive match of the pattern itself (that is, a correctly parenthe-
sized substring). Finally there is a closing parenthesis.
28
CHAPTER 1. GLIB OVERVIEW 1.6. REGULAR EXPRESSION SYNTAX
If this were part of a larger pattern, you would not want to recurse the entire pattern, so instead you
could use this:
( \( ( (?>[^()]+) | (?1) )* \) )
We have put the pattern into parentheses, and caused the recursion to refer to them instead of the
whole pattern. In a larger pattern, keeping track of parenthesis numbers can be tricky. It may be more
convenient to use named parentheses instead. The Perl syntax for this is (?&name); GRegex also sup-
ports the(?P>name) syntac. We could rewrite the above example as follows:
(?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )
If there is more than one subpattern with the same name, the earliest one is used. This particular
example pattern contains nested unlimited repeats, and so the use of atomic grouping for matching
strings of non-parentheses is important when applying the pattern to strings that do not match. For
example, when this pattern is applied to
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
it yields "no match" quickly. However, if atomic grouping is not used, the match runs for a very long
time indeed because there are so many different ways the + and * repeats can carve up the string, and
all have to be tested before failure can be reported.
At the end of a match, the values set for any capturing subpatterns are those from the outermost
level of the recursion at which the subpattern value is set. If the pattern above is matched against
(ab(cd)ef)
the value for the capturing parentheses is "ef", which is the last value taken on at the top level. If
additional parentheses are added, giving
\( ( ( (?>[^()]+) | (?R) )* ) \)
^ ^
^ ^
the string they capture is "ab(cd)ef", the contents of the top level parentheses.
Do not confuse the (?R) item with the condition (R), which tests for recursion. Consider this pattern,
which matches text in angle brackets, allowing for arbitrary nesting. Only digits are allowed in nested
brackets (that is, when recursing), whereas any characters are permitted at the outer level.
< (?: (?(R) \d++ | [^<>]*+) | (?R)) * >
In this pattern, (?(R) is the start of a conditional subpattern, with two different alternatives for the
recursive and non-recursive cases. The (?R) item is the actual recursive call.
Subpatterns as subroutines
If the syntax for a recursive subpattern reference (either by number or by name) is used outside the
parentheses to which it refers, it operates like a subroutine in a programming language. The "called"
subpattern may be defined before or after the reference. An earlier example pointed out that the pattern
(sens|respons)e and \1ibility
matches "sense and sensibility" and "response and responsibility", but not "sense and responsibility".
If instead the pattern
(sens|respons)e and (?1)ibility
is used, it does match "sense and responsibility" as well as the other two strings. Another example is
given in the discussion of DEFINE above.
Like recursive subpatterns, a "subroutine" call is always treated as an atomic group. That is, once it
has matched some of the string, it is never re-entered, even if it contains untried alternatives and there
is a subsequent matching failure.
When a subpattern is used as a subroutine, processing options such as case-independence are fixed
when the subpattern is defined. They cannot be changed for different calls. For example, consider this
pattern:
29
CHAPTER 1. GLIB OVERVIEW 1.7. MAILING LISTS AND BUG REPORTS
(abc)(?i:(?1))
It matches "abcabc". It does not match "abcABC" because the change of processing option does not
affect the called subpattern.
Copyright
This document was copied and adapted from the PCRE documentation, specifically from the man page
for pcrepattern. The original copyright note is:
* Neither the name of the University of Cambridge nor the name of Google
Inc. nor the names of their contributors may be used to endorse or
promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
30
CHAPTER 1. GLIB OVERVIEW 1.7. MAILING LISTS AND BUG REPORTS
Submitting Patches
If you develop a bugfix or enhancement for GLib, please file that in Bugzilla as well. Bugzilla allows
you to attach files; please attach a patch generated by the diff utility, using the -u option to make the
patch more readable. All patches must be offered under the terms of the GNU LGPL license, so be sure
you are authorized to give us the patch under those terms.
If you want to discuss your patch before or after developing it, mail [email protected]. But
be sure to file the Bugzilla report as well; if the patch is only on the list and not in Bugzilla, it’s likely to
slip through the cracks.
Mailing lists
There are several mailing lists dedicated to GTK+ and related libraries. Discussion of GLib generally
takes place on these lists. You can subscribe or view the archives of these lists on http://mail.gnome.org.
[email protected] gtk-list covers general GTK+ (and GLib) topics; questions about using GLib in
programs, GLib from a user standpoint, announcements of GLib-related projects would all be on-
topic. The bulk of the traffic consists of GTK+ programming questions.
[email protected] gtk-devel-list is for discussion of work on GTK+ (and GLib) itself, it is not for
asking questions about how to use GTK+ (or GLib) in applications. gtk-devel-list is appropriate
for discussion of patches, bugs, proposed features, and so on.
[email protected] gtk-doc-list is for discussion of the gtk-doc documentation system (used to
document GTK+ and Glib), and for work on the GTK+ (and GLib) documentation.
31
Chapter 2
GLib Fundamentals
Synopsis
#include <glib.h>
#define GLIB_MAJOR_VERSION
#define GLIB_MINOR_VERSION
#define GLIB_MICRO_VERSION
#define GLIB_CHECK_VERSION (major,minor,micro)
Description
GLib provides version information, primarily useful in configure checks for builds that have a configure
script. Applications will not typically use the features described here.
Details
glib_major_version
The major version number of the GLib library. (e.g. in GLib version 1.2.5 this is 1.)
This variable is in the library, so represents the GLib library you have linked against. Contrast with
the GLIB_MAJOR_VERSION macro, which represents the major version of the GLib headers you have
included.
glib_minor_version
33
CHAPTER 2. GLIB FUNDAMENTALS 2.1. VERSION INFORMATION
The minor version number of the GLib library. (e.g. in GLib version 1.2.5 this is 2.)
This variable is in the library, so represents the GLib library you have linked against. Contrast with
the GLIB_MINOR_VERSION macro, which represents the minor version of the GLib headers you have
included.
glib_micro_version
The micro version number of the GLib library. (e.g. in GLib version 1.2.5 this is 5.)
This variable is in the library, so represents the GLib library you have linked against. Contrast with
the GLIB_MICRO_VERSION macro, which represents the micro version of the GLib headers you have
included.
glib_binary_age
This is the binary age passed to libtool. If libtool means nothing to you, don’t worry about it. ;-)
glib_interface_age
This is the interface age passed to libtool. If libtool means nothing to you, don’t worry about it. ;-)
glib_check_version ()
Checks that the GLib library in use is compatible with the given version. Generally you would pass
in the constants GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION as the
three arguments to this function; that produces a check that the library in use is compatible with the
version of GLib the application or module was compiled against.
Compatibility is defined by two things: first the version of the running library is newer than the ver-
sion required_major.required_minor .required_micro. Second the running library must be binary
compatible with the version required_major.required_minor .required_micro (same major version.)
Returns : NULL if the GLib library is compatible with the given version, or a string describing the
version mismatch. The returned string is owned by GLib and must not be modified or freed.
Since 2.6
GLIB_MAJOR_VERSION
#define GLIB_MAJOR_VERSION 2
The major version number of the GLib library. Like glib_major_version, but from the headers used
at application compile time, rather than from the library linked against at application run time.
34
CHAPTER 2. GLIB FUNDAMENTALS 2.2. BASIC TYPES
GLIB_MINOR_VERSION
#define GLIB_MINOR_VERSION 21
The minor version number of the GLib library. Like gtk_minor_version, but from the headers used
at application compile time, rather than from the library linked against at application run time.
GLIB_MICRO_VERSION
#define GLIB_MICRO_VERSION 1
The micro version number of the GLib library. Like gtk_micro_version, but from the headers used at
application compile time, rather than from the library linked against at application run time.
GLIB_CHECK_VERSION()
#define GLIB_CHECK_VERSION(major,minor,micro)
Checks the version of the GLib library. Returns TRUE if the version of the GLib header files is the
same as or newer than the passed-in version.
Synopsis
#include <glib.h>
typedef gboolean;
typedef gpointer;
typedef gconstpointer;
typedef gchar;
typedef guchar;
typedef gint;
typedef guint;
typedef gshort;
typedef gushort;
typedef glong;
typedef gulong;
typedef gint8;
typedef guint8;
35
CHAPTER 2. GLIB FUNDAMENTALS 2.2. BASIC TYPES
typedef gint16;
typedef guint16;
typedef gint32;
typedef guint32;
#define G_HAVE_GINT64
typedef gint64;
typedef guint64;
#define G_GINT64_CONSTANT (val)
#define G_GUINT64_CONSTANT (val)
typedef gfloat;
typedef gdouble;
typedef gsize;
typedef gssize;
typedef goffset;
#define G_GOFFSET_CONSTANT (val)
Description
GLib defines a number of commonly used types, which can be divided into 4 groups:
• New types which are not part of standard C - gboolean, gsize, gssize.
• Integer types which are guaranteed to be the same size across all platforms - gint8, guint8, gint16,
guint16, gint32, guint32, gint64, guint64.
• Types which are easier to use than their standard C counterparts - gpointer, gconstpointer, guchar,
guint, gushort, gulong.
• Types which correspond exactly to standard C types, but are included for completeness - gchar,
gint, gshort, glong, gfloat, gdouble.
Details
gboolean
A standard boolean type. Variables of this type should only contain the value TRUE or FALSE.
gpointer
An untyped pointer. gpointer looks better and is easier to use than void*.
gconstpointer
An untyped pointer to constant data. The data pointed to should not be changed.
This is typically used in function prototypes to indicate that the data pointed to will not be altered
by the function.
gchar
36
CHAPTER 2. GLIB FUNDAMENTALS 2.2. BASIC TYPES
guchar
gint
Corresponds to the standard C int type. Values of this type can range from G_MININT to G_MAXINT.
guint
Corresponds to the standard C unsigned int type. Values of this type can range from 0 to G_MAXUINT.
gshort
Corresponds to the standard C short type. Values of this type can range from G_MINSHORT to
G_MAXSHORT.
gushort
Corresponds to the standard C unsigned short type. Values of this type can range from 0 to G_MAXUSHORT.
glong
Corresponds to the standard C long type. Values of this type can range from G_MINLONG to
G_MAXLONG.
gulong
Corresponds to the standard C unsigned long type. Values of this type can range from 0 to G_MAXULONG.
gint8
A signed integer guaranteed to be 8 bits on all platforms. Values of this type can range from -128 to
127.
guint8
An unsigned integer guaranteed to be 8 bits on all platforms. Values of this type can range from 0 to
255.
37
CHAPTER 2. GLIB FUNDAMENTALS 2.2. BASIC TYPES
gint16
A signed integer guaranteed to be 16 bits on all platforms. Values of this type can range from -32,768
to 32,767.
To print or scan values of this type, use G_GINT16_MODIFIER and/or G_GINT16_FORMAT.
guint16
An unsigned integer guaranteed to be 16 bits on all platforms. Values of this type can range from 0
to 65,535.
To print or scan values of this type, use G_GINT16_MODIFIER and/or G_GUINT16_FORMAT.
gint32
A signed integer guaranteed to be 32 bits on all platforms. Values of this type can range from -
2,147,483,648 to 2,147,483,647.
To print or scan values of this type, use G_GINT32_MODIFIER and/or G_GINT32_FORMAT.
guint32
An unsigned integer guaranteed to be 32 bits on all platforms. Values of this type can range from 0
to 4,294,967,295.
To print or scan values of this type, use G_GINT32_MODIFIER and/or G_GUINT32_FORMAT.
G_HAVE_GINT64
WARNING
This macro is defined if 64-bit signed and unsigned integers are available on the platform.
gint64
A signed integer guaranteed to be 64 bits on all platforms. Values of this type can range from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
To print or scan values of this type, use G_GINT64_MODIFIER and/or G_GINT64_FORMAT.
38
CHAPTER 2. GLIB FUNDAMENTALS 2.2. BASIC TYPES
guint64
An unsigned integer guaranteed to be 64 bits on all platforms. Values of this type can range from 0
to 18,446,744,073,709,551,615.
To print or scan values of this type, use G_GINT64_MODIFIER and/or G_GUINT64_FORMAT.
G_GINT64_CONSTANT()
This macro is used to insert 64-bit integer literals into the source code.
val : a literal integer value, e.g. 0x1d636b02300a7aa7.
G_GUINT64_CONSTANT()
This macro is used to insert 64-bit unsigned integer literals into the source code.
val : a literal integer value, e.g. 0x1d636b02300a7aa7U.
Since 2.10
gfloat
Corresponds to the standard C float type. Values of this type can range from -G_MAXFLOAT to
G_MAXFLOAT.
gdouble
Corresponds to the standard C double type. Values of this type can range from -G_MAXDOUBLE to
G_MAXDOUBLE.
gsize
An unsigned integer type of the result of the sizeof operator, corresponding to the size_t type defined
in C99. This type is wide enough to hold the numeric value of a pointer, so it is usually 32bit wide on a
32bit platform and 64bit wide on a 64bit platform.
To print or scan values of this type, use G_GSIZE_MODIFIER and/or G_GSIZE_FORMAT.
gssize
goffset
A signed integer type that is used for file offsets, corresponding to the C99 type off64_t.
Since: 2.14
39
CHAPTER 2. GLIB FUNDAMENTALS 2.3. LIMITS OF BASIC TYPES
G_GOFFSET_CONSTANT()
This macro is used to insert goffset 64-bit integer literals into the source code. See also G_GINT64_CONSTANT.
val : a literal integer value, e.g. 0x1d636b02300a7aa7. Since: 2.20
Synopsis
#include <glib.h>
#define G_MININT
#define G_MAXINT
#define G_MAXUINT
#define G_MINSHORT
#define G_MAXSHORT
#define G_MAXUSHORT
#define G_MINLONG
#define G_MAXLONG
#define G_MAXULONG
#define G_MININT8
#define G_MAXINT8
#define G_MAXUINT8
#define G_MININT16
#define G_MAXINT16
#define G_MAXUINT16
#define G_MININT32
#define G_MAXINT32
#define G_MAXUINT32
#define G_MININT64
#define G_MAXINT64
#define G_MAXUINT64
#define G_MAXSIZE
#define G_MINSSIZE
#define G_MAXSSIZE
#define G_MINOFFSET
#define G_MAXOFFSET
#define G_MINFLOAT
#define G_MAXFLOAT
#define G_MINDOUBLE
#define G_MAXDOUBLE
40
CHAPTER 2. GLIB FUNDAMENTALS 2.3. LIMITS OF BASIC TYPES
Description
These macros provide a portable method to determine the limits of some of the standard integer and
floating point types.
Details
G_MININT
G_MAXINT
G_MAXUINT
G_MINSHORT
G_MAXSHORT
G_MAXUSHORT
G_MINLONG
G_MAXLONG
G_MAXULONG
41
CHAPTER 2. GLIB FUNDAMENTALS 2.3. LIMITS OF BASIC TYPES
G_MININT8
G_MAXINT8
G_MAXUINT8
G_MININT16
G_MAXINT16
G_MAXUINT16
G_MININT32
G_MAXINT32
42
CHAPTER 2. GLIB FUNDAMENTALS 2.3. LIMITS OF BASIC TYPES
G_MAXUINT32
G_MININT64
G_MAXINT64
G_MAXUINT64
G_MAXSIZE
G_MINSSIZE
G_MAXSSIZE
G_MINOFFSET
G_MAXOFFSET
43
CHAPTER 2. GLIB FUNDAMENTALS 2.4. STANDARD MACROS
G_MINFLOAT
G_MAXFLOAT
G_MINDOUBLE
G_MAXDOUBLE
Synopsis
#include <glib.h>
#define G_OS_WIN32
#define G_OS_BEOS
#define G_OS_UNIX
#define G_DIR_SEPARATOR
#define G_DIR_SEPARATOR_S
#define G_IS_DIR_SEPARATOR (c)
#define G_SEARCHPATH_SEPARATOR
#define G_SEARCHPATH_SEPARATOR_S
#define TRUE
#define FALSE
#define NULL
44
CHAPTER 2. GLIB FUNDAMENTALS 2.4. STANDARD MACROS
#define G_MEM_ALIGN
#define G_CONST_RETURN
Description
These macros provide a few commonly-used features.
Details
G_OS_WIN32
#define G_OS_WIN32
This macro is defined only on Windows. So you can bracket Windows-specific code in "#ifdef
G_OS_WIN32".
G_OS_BEOS
#define G_OS_BEOS
This macro is defined only on BeOS. So you can bracket BeOS-specific code in "#ifdef G_OS_BEOS".
G_OS_UNIX
#define G_OS_UNIX
This macro is defined only on UNIX. So you can bracket UNIX-specific code in "#ifdef G_OS_UNIX".
G_DIR_SEPARATOR
#define G_DIR_SEPARATOR
The directory separator character. This is ’/’ on UNIX machines and ’\’ under Windows.
G_DIR_SEPARATOR_S
#define G_DIR_SEPARATOR_S
The directory separator as a string. This is "/" on UNIX machines and "\" under Windows.
G_IS_DIR_SEPARATOR()
#define G_IS_DIR_SEPARATOR(c)
Checks whether a character is a directory separator. It returns TRUE for ’/’ on UNIX machines and
for ’\’ or ’/’ under Windows.
c : a character
Since 2.6
G_SEARCHPATH_SEPARATOR
#define G_SEARCHPATH_SEPARATOR
The search path separator character. This is ’:’ on UNIX machines and ’;’ under Windows.
45
CHAPTER 2. GLIB FUNDAMENTALS 2.4. STANDARD MACROS
G_SEARCHPATH_SEPARATOR_S
#define G_SEARCHPATH_SEPARATOR_S
The search path separator as a string. This is ":" on UNIX machines and ";" under Windows.
TRUE
FALSE
NULL
#define NULL
MIN()
b : a numeric value.
MAX()
b : a numeric value.
ABS()
Calculates the absolute value of a. The absolute value is simply the number with any negative sign
taken away.
For example,
• ABS(-10) is 10.
• ABS(10) is also 10.
a : a numeric value.
46
CHAPTER 2. GLIB FUNDAMENTALS 2.4. STANDARD MACROS
CLAMP()
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : ←-
(x)))
Ensures that x is between the limits set by low and high. If low is greater than high the result is
undefined.
For example,
• CLAMP(5, 10, 15) is 10.
• CLAMP(15, 5, 10) is 10.
• CLAMP(20, 15, 25) is 20.
Returns : the value of x clamped to the range between low and high.
G_STRUCT_MEMBER()
struct_offset : the offset of the field from the start of the struct, in bytes.
G_STRUCT_MEMBER_P()
G_STRUCT_OFFSET()
G_MEM_ALIGN
#define G_MEM_ALIGN
Indicates the number of bytes to which memory will be aligned on the current platform.
47
CHAPTER 2. GLIB FUNDAMENTALS 2.5. TYPE CONVERSION MACROS
G_CONST_RETURN
#define G_CONST_RETURN
Synopsis
#include <glib.h>
Description
Many times GLib, GTK+, and other libraries allow you to pass "user data" to a callback, in the form of a
void pointer. From time to time you want to pass an integer instead of a pointer. You could allocate an
integer, with something like:
int *ip = g_new (int, 1);
*ip = 42;
But this is inconvenient, and it’s annoying to have to free the memory at some later time.
Pointers are always at least 32 bits in size (on all platforms GLib intends to support). Thus you can
store at least 32-bit integer values in a pointer value. Naively, you might try this, but it’s incorrect:
gpointer p;
int i;
p = (void*) 42;
i = (int) p;
Again, that example was not correct, don’t copy it. The problem is that on some systems you need to do
this:
gpointer p;
int i;
p = (void*) (long) 42;
i = (int) (long) p;
48
CHAPTER 2. GLIB FUNDAMENTALS 2.5. TYPE CONVERSION MACROS
WARNING
YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE IN ANY
WAY SHAPE OR FORM. These macros ONLY allow storing integers in pointers, and
only preserve 32 bits of the integer; values outside the range of a 32-bit integer will be
mangled.
Details
GINT_TO_POINTER()
GPOINTER_TO_INT()
Extracts an integer from a pointer. The integer must have been stored in the pointer with GINT_TO_POINTER().
Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE IN ANY
WAY SHAPE OR FORM. These macros ONLY allow storing integers in pointers, and only preserve 32
bits of the integer; values outside the range of a 32-bit integer will be mangled.
GUINT_TO_POINTER()
GPOINTER_TO_UINT()
Extracts an unsigned integer from a pointer. The integer must have been stored in the pointer with
GUINT_TO_POINTER().
GSIZE_TO_POINTER()
49
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GPOINTER_TO_SIZE()
Extracts a gsize from a pointer. The gsize must have been stored in the pointer with GSIZE_TO_POINTER().
Synopsis
#include <glib.h>
#define G_BYTE_ORDER
#define G_LITTLE_ENDIAN
#define G_BIG_ENDIAN
#define G_PDP_ENDIAN
50
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
Description
These macros provide a portable way to determine the host byte order and to convert values between
different byte orders.
The byte order is the order in which bytes are stored to create larger data types such as the gint and
glong values. The host byte order is the byte order used on the current machine.
Some processors store the most significant bytes (i.e. the bytes that hold the largest part of the value)
first. These are known as big-endian processors.
Other processors (notably the x86 family) store the most significant byte last. These are known as
little-endian processors.
Finally, to complicate matters, some other processors store the bytes in a rather curious order known
as PDP-endian. For a 4-byte word, the 3rd most significant byte is stored first, then the 4th, then the 1st
and finally the 2nd.
Obviously there is a problem when these different processors communicate with each other, for
example over networks or by using binary file formats. This is where these macros come in. They are
typically used to convert values into a byte order which has been agreed on for use when communicating
between different processors. The Internet uses what is known as ’network byte order’ as the standard
byte order (which is in fact the big-endian byte order).
Note that the byte order conversion macros may evaluate their arguments multiple times, thus you
should not use them with arguments which have side-effects.
Details
G_BYTE_ORDER
The host byte order. This can be either G_LITTLE_ENDIAN or G_BIG_ENDIAN (support for G_PDP_ENDIAN
may be added in future.)
51
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
G_LITTLE_ENDIAN
G_BIG_ENDIAN
G_PDP_ENDIAN
Specifies one of the possible types of byte order (currently unused). See G_BYTE_ORDER.
g_htonl()
#define g_htonl(val)
g_htons()
#define g_htons(val)
g_ntohl()
#define g_ntohl(val)
g_ntohs()
#define g_ntohs(val)
52
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GINT_FROM_BE()
GINT_FROM_LE()
GINT_TO_BE()
GINT_TO_LE()
GUINT_FROM_BE()
GUINT_FROM_LE()
53
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GUINT_TO_BE()
GUINT_TO_LE()
GLONG_FROM_BE()
GLONG_FROM_LE()
GLONG_TO_BE()
GLONG_TO_LE()
54
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GULONG_FROM_BE()
GULONG_FROM_LE()
GULONG_TO_BE()
GULONG_TO_LE()
GINT16_FROM_BE()
GINT16_FROM_LE()
55
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GINT16_TO_BE()
GINT16_TO_LE()
GUINT16_FROM_BE()
GUINT16_FROM_LE()
GUINT16_TO_BE()
GUINT16_TO_LE()
56
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GINT32_FROM_BE()
GINT32_FROM_LE()
GINT32_TO_BE()
GINT32_TO_LE()
GUINT32_FROM_BE()
GUINT32_FROM_LE()
57
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GUINT32_TO_BE()
GUINT32_TO_LE()
GINT64_FROM_BE()
GINT64_FROM_LE()
GINT64_TO_BE()
GINT64_TO_LE()
58
CHAPTER 2. GLIB FUNDAMENTALS 2.6. BYTE ORDER MACROS
GUINT64_FROM_BE()
GUINT64_FROM_LE()
GUINT64_TO_BE()
GUINT64_TO_LE()
GUINT16_SWAP_BE_PDP()
Converts a guint16 value between big-endian and pdp-endian byte order. The conversion is sym-
metric so it can be used both ways.
GUINT16_SWAP_LE_BE()
#define GUINT16_SWAP_LE_BE(val)
Converts a guint16 value between little-endian and big-endian byte order. The conversion is sym-
metric so it can be used both ways.
59
CHAPTER 2. GLIB FUNDAMENTALS 2.7. NUMERICAL DEFINITIONS
GUINT16_SWAP_LE_PDP()
Converts a guint16 value between little-endian and pdp-endian byte order. The conversion is sym-
metric so it can be used both ways.
GUINT32_SWAP_BE_PDP()
#define GUINT32_SWAP_BE_PDP(val)
Converts a guint32 value between big-endian and pdp-endian byte order. The conversion is sym-
metric so it can be used both ways.
GUINT32_SWAP_LE_BE()
#define GUINT32_SWAP_LE_BE(val)
Converts a guint32 value between little-endian and big-endian byte order. The conversion is sym-
metric so it can be used both ways.
GUINT32_SWAP_LE_PDP()
#define GUINT32_SWAP_LE_PDP(val)
Converts a guint32 value between little-endian and pdp-endian byte order. The conversion is sym-
metric so it can be used both ways.
GUINT64_SWAP_LE_BE()
#define GUINT64_SWAP_LE_BE(val)
Converts a guint64 value between little-endian and big-endian byte order. The conversion is sym-
metric so it can be used both ways.
60
CHAPTER 2. GLIB FUNDAMENTALS 2.7. NUMERICAL DEFINITIONS
Synopsis
#include <glib.h>
#define G_IEEE754_FLOAT_BIAS
#define G_IEEE754_DOUBLE_BIAS
union GFloatIEEE754;
union GDoubleIEEE754;
#define G_E
#define G_LN2
#define G_LN10
#define G_PI
#define G_PI_2
#define G_PI_4
#define G_SQRT2
#define G_LOG_2_BASE_10
Description
GLib offers mathematical constants such as G_PI for the value of pi; many platforms have these in the
C library, but some don’t, the GLib versions always exist.
The GFloatIEEE754 and GDoubleIEEE754 unions are used to access the sign, mantissa and expo-
nent of IEEE floats and doubles. These unions are defined as appropriate for a given platform. IEEE
floats and doubles are supported (used for storage) by at least Intel, PPC and Sparc, for reference:
http://cch.loria.fr/documentation/IEEE754/numerical_comp_guide/ncg_math.doc.html
Details
G_IEEE754_FLOAT_BIAS
See http://cch.loria.fr/documentation/IEEE754/numerical_comp_guide/ncg_math.doc.html
G_IEEE754_DOUBLE_BIAS
See http://cch.loria.fr/documentation/IEEE754/numerical_comp_guide/ncg_math.doc.html
union GFloatIEEE754
union GFloatIEEE754
{
gfloat v_float;
struct {
guint mantissa : 23;
guint biased_exponent : 8;
guint sign : 1;
} mpn;
};
The GFloatIEEE754 and GDoubleIEEE754 unions are used to access the sign, mantissa and expo-
nent of IEEE floats and doubles. These unions are defined as appropriate for a given platform. IEEE
floats and doubles are supported (used for storage) by at least Intel, PPC and Sparc, for reference:
http://cch.loria.fr/documentation/IEEE754/numerical_comp_guide/ncg_math.doc.html
61
CHAPTER 2. GLIB FUNDAMENTALS 2.7. NUMERICAL DEFINITIONS
union GDoubleIEEE754
union GDoubleIEEE754
{
gdouble v_double;
struct {
guint mantissa_low : 32;
guint mantissa_high : 20;
guint biased_exponent : 11;
guint sign : 1;
} mpn;
};
The GFloatIEEE754 and GDoubleIEEE754 unions are used to access the sign, mantissa and expo-
nent of IEEE floats and doubles. These unions are defined as appropriate for a given platform. IEEE
floats and doubles are supported (used for storage) by at least Intel, PPC and Sparc, for reference:
http://cch.loria.fr/documentation/IEEE754/numerical_comp_guide/ncg_math.doc.html
G_E
G_LN2
G_LN10
G_PI
G_PI_2
Pi divided by 2.
G_PI_4
Pi divided by 4.
G_SQRT2
62
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_LOG_2_BASE_10
See Also
http://cch.loria.fr/documentation/IEEE754/numerical_comp_guide/ncg_math.doc.html
Synopsis
#include <glib.h>
#define G_INLINE_FUNC
#define G_STMT_START
#define G_STMT_END
#define G_BEGIN_DECLS
#define G_END_DECLS
#define G_GNUC_EXTENSION
#define G_GNUC_CONST
#define G_GNUC_PURE
#define G_GNUC_MALLOC
#define G_GNUC_ALLOC_SIZE (x)
#define G_GNUC_ALLOC_SIZE2 (x,y)
#define G_GNUC_DEPRECATED
#define G_GNUC_NORETURN
#define G_GNUC_UNUSED
#define G_GNUC_PRINTF ( format_idx, arg_idx )
#define G_GNUC_SCANF ( format_idx, arg_idx )
#define G_GNUC_FORMAT ( arg_idx )
#define G_GNUC_NULL_TERMINATED
#define G_GNUC_WARN_UNUSED_RESULT
#define G_GNUC_FUNCTION
#define G_GNUC_PRETTY_FUNCTION
#define G_GNUC_NO_INSTRUMENT
#define G_HAVE_GNUC_VISIBILITY
#define G_GNUC_INTERNAL
63
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
#define G_GNUC_MAY_ALIAS
if G_LIKELY ();
#define G_UNLIKELY (expr)
#define G_STRLOC
#define G_STRFUNC
#define G_GINT16_MODIFIER
#define G_GINT16_FORMAT
#define G_GUINT16_FORMAT
#define G_GINT32_MODIFIER
#define G_GINT32_FORMAT
#define G_GUINT32_FORMAT
#define G_GINT64_MODIFIER
#define G_GINT64_FORMAT
#define G_GUINT64_FORMAT
#define G_GSIZE_MODIFIER
#define G_GSIZE_FORMAT
#define G_GSSIZE_FORMAT
#define G_GOFFSET_MODIFIER
#define G_GOFFSET_FORMAT
Description
These macros provide more specialized features which are not needed so often by application program-
mers.
Details
G_INLINE_FUNC
#define G_INLINE_FUNC
This macro is used to export function prototypes so they can be linked with an external version when
no inlining is performed. The file which implements the functions should define G_IMPLEMENTS_INLINES
before including the headers which contain G_INLINE_FUNC declarations. Since inlining is very compiler-
dependent using these macros correctly is very difficult. Their use is strongly discouraged.
This macro is often mistaken for a replacement for the inline keyword; inline is already declared in a
portable manner in the glib headers and can be used normally.
G_STMT_START
# define G_STMT_START do
Used within multi-statement macros so that they can be used in places where only one statement is
expected by the compiler.
G_STMT_END
Used within multi-statement macros so that they can be used in places where only one statement is
expected by the compiler.
64
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_BEGIN_DECLS
#define G_BEGIN_DECLS
Used (along with G_END_DECLS) to bracket header files. If the compiler in use is a C++ compiler,
adds extern "C" around the header.
G_END_DECLS
#define G_END_DECLS
Used (along with G_BEGIN_DECLS) to bracket header files. If the compiler in use is a C++ compiler,
adds extern "C" around the header.
G_N_ELEMENTS()
Determines the number of elements in an array. The array must be declared so the compiler knows
its size at compile-time; this macro will not work on an array allocated on the heap, only static arrays or
arrays on the stack.
G_VA_COPY()
#define G_VA_COPY(ap1,ap2)
ap2 : a va_list.
G_STRINGIFY()
Accepts a macro or a string and converts it into a string after preprocessor argument expansion.
G_PASTE()
Yields a new preprocessor pasted identifier ’identifier1identifier2’ from its expanded arguments
’identifier1’ and ’identifier2’.
identifier1 : an identifier
identifier2 : an identifier
Since 2.20
65
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_PASTE_ARGS()
identifier1 :
identifier2 :
G_STATIC_ASSERT()
The G_STATIC_ASSERT macro lets the programmer check a condition at compile time, the condition
needs to be compile time computable. The macro can be used in any place where a typedef is valid.
The macro should only be used once per source code line.
Since 2.20
G_GNUC_EXTENSION
#define G_GNUC_EXTENSION
Expands to __extension__ when gcc is used as the compiler. This simply tells gcc not to warn
about the following non-standard code when compiling with the -pedantic option.
G_GNUC_CONST
#define G_GNUC_CONST
Expands to the GNU C const function attribute if the compiler is gcc. Declaring a function as const
enables better optimization of calls to the function. A const function doesn’t examine any values except
its parameters, and has no effects except its return value. See the GNU C documentation for details.
N OTE
A function that has pointer arguments and examines the data pointed to must not be
declared const. Likewise, a function that calls a non-const function usually must not be
const. It doesn’t make sense for a const function to return void.
G_GNUC_PURE
#define G_GNUC_PURE
Expands to the GNU C pure function attribute if the compiler is gcc. Declaring a function as pure
enables better optimization of calls to the function. A pure function has no effects except its return
value and the return value depends only on the parameters and/or global variables. See the GNU C
documentation for details.
66
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_GNUC_MALLOC
#define G_GNUC_MALLOC
Expands to the GNU C malloc function attribute if the compiler is gcc. Declaring a function as
malloc enables better optimization of the function. A function can have the malloc attribute if it returns
a pointer which is guaranteed to not alias with any other pointer when the function returns (in practice,
this means newly allocated memory). See the GNU C documentation for details.
Since 2.6
G_GNUC_ALLOC_SIZE()
#define G_GNUC_ALLOC_SIZE(x)
Expands to the GNU C alloc_size function attribute if the compiler is a new enough gcc. This
attribute tells the compiler that the function returns a pointer to memory of a size that is specified by the
x th function parameter. See the GNU C documentation for details.
Since 2.18
G_GNUC_ALLOC_SIZE2()
#define G_GNUC_ALLOC_SIZE2(x,y)
Expands to the GNU C alloc_size function attribute if the compiler is a new enough gcc. This
attribute tells the compiler that the function returns a pointer to memory of a size that is specified by the
product of two function parameters. See the GNU C documentation for details.
x : the index of the argument specifying one factor of the allocation size
y : the index of the argument specifying the second factor of the allocation size
Since 2.18
G_GNUC_DEPRECATED
#define G_GNUC_DEPRECATED
Expands to the GNU C deprecated attribute if the compiler is gcc. It can be used to mark typedefs,
variables and functions as deprecated. When called with the -Wdeprecated option, the compiler will
generate warnings when deprecated interfaces are used. See the GNU C documentation for details.
Since 2.2
G_GNUC_NORETURN
#define G_GNUC_NORETURN
Expands to the GNU C noreturn function attribute if the compiler is gcc. It is used for declaring
functions which never return. It enables optimization of the function, and avoids possible compiler
warnings. See the GNU C documentation for details.
G_GNUC_UNUSED
#define G_GNUC_UNUSED
Expands to the GNU C unused function attribute if the compiler is gcc. It is used for declaring func-
tions which may never be used. It avoids possible compiler warnings. See the GNU C documentation
for details.
67
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_GNUC_PRINTF()
Expands to the GNU C format function attribute if the compiler is gcc. This is used for declaring
functions which take a variable number of arguments, with the same syntax as printf(). It allows the
compiler to type-check the arguments passed to the function. See the GNU C documentation for details.
gint g_snprintf (gchar *string,
gulong n,
gchar const *format,
...) G_GNUC_PRINTF (3, 4);
format_idx : the index of the argument corresponding to the format string. (The arguments are num-
bered from 1).
arg_idx : the index of the first of the format arguments.
G_GNUC_SCANF()
Expands to the GNU C format function attribute if the compiler is gcc. This is used for declaring
functions which take a variable number of arguments, with the same syntax as scanf(). It allows the
compiler to type-check the arguments passed to the function. See the GNU C documentation for details.
format_idx : the index of the argument corresponding to the format string. (The arguments are num-
bered from 1).
arg_idx : the index of the first of the format arguments.
G_GNUC_FORMAT()
Expands to the GNU C format_arg function attribute if the compiler is gcc. This function attribute
specifies that a function takes a format string for a printf(), scanf(), strftime() or strfmon()
style function and modifies it, so that the result can be passed to a printf(), scanf(), strftime()
or strfmon() style function (with the remaining arguments to the format function the same as they
would have been for the unmodified string). See the GNU C documentation for details.
gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2);
G_GNUC_NULL_TERMINATED
#define G_GNUC_NULL_TERMINATED
Expands to the GNU C sentinel function attribute if the compiler is gcc, or "" if it isn’t. This func-
tion attribute only applies to variadic functions and instructs the compiler to check that the argument
list is terminated with an explicit NULL. See the GNU C documentation for details.
Since: 2.8
G_GNUC_WARN_UNUSED_RESULT
#define G_GNUC_WARN_UNUSED_RESULT
Expands to the GNU C warn_unused_result function attribute if the compiler is gcc, or "" if it
isn’t. This function attribute makes the compiler emit a warning if the result of a function call is ignored.
See the GNU C documentation for details.
Since 2.10
68
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_GNUC_FUNCTION
#define G_GNUC_FUNCTION
WARNING
Expands to "" on all modern compilers, and to __FUNCTION__ on gcc version 2.x. Don’t use it.
G_GNUC_PRETTY_FUNCTION
#define G_GNUC_PRETTY_FUNCTION
WARNING
Expands to "" on all modern compilers, and to __PRETTY_FUNCTION__ on gcc version 2.x. Don’t
use it.
G_GNUC_NO_INSTRUMENT
#define G_GNUC_NO_INSTRUMENT
Expands to the GNU C no_instrument_function function attribute if the compiler is gcc. Func-
tions with this attribute will not be instrumented for profiling, when the compiler is called with the
-finstrument-functions option. See the GNU C documentation for details.
G_HAVE_GNUC_VISIBILITY
#define G_HAVE_GNUC_VISIBILITY 1
G_GNUC_INTERNAL
#define G_GNUC_INTERNAL
This attribute can be used for marking library functions as being used internally to the library only,
which may allow the compiler to handle function calls more efficiently. Note that static functions do not
need to be marked as internal in this way. See the GNU C documentation for details.
When using a compiler that supports the GNU C hidden visibility attribute, this macro expands to
__attribute__((visibility("hidden"))). When using the Sun Studio compiler, it expands to
__hidden.
Note that for portability, the attribute should be placed before the function declaration. While GCC
allows the macro after the declaration, Sun Studio does not.
69
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_GNUC_INTERNAL
void _g_log_fallback_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer unused_data);
Since: 2.6
G_GNUC_MAY_ALIAS
#define G_GNUC_MAY_ALIAS
Expands to the GNU C may_alias type attribute if the compiler is gcc. Types with this attribute
will not be subjected to type-based alias analysis, but are assumed to alias with any other type, just like
char. See the GNU C documentation for details.
Since: 2.14
G_LIKELY ()
if G_LIKELY ();
Hints the compiler that the expression is likely to evaluate to a true value. The compiler may use this
information for optimizations.
if (G_LIKELY (random () != 1))
g_print ("not one");
Since 2.2
G_UNLIKELY()
#define G_UNLIKELY(expr)
Hints the compiler that the expression is unlikely to evaluate to a true value. The compiler may use
this information for optimizations.
if (G_UNLIKELY (random () == 1))
g_print ("a random one");
Since 2.2
G_STRLOC
#define G_STRLOC
G_STRFUNC
#define G_STRFUNC
70
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_GINT16_MODIFIER
The platform dependent length modifier for conversion specifiers for scanning and printing values
of type gint16 or guint16. It is a string literal, but doesn’t include the percent-sign, such that you can add
precision and length modifiers between percent-sign and conversion specifier and append a conversion
specifier.
The following example prints "0x7b";
gint16 value = 123;
g_print ("%#" G_GINT16_MODIFIER "x", value);
Since 2.4
G_GINT16_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type gint16.
It is a string literal, but doesn’t include the percent-sign, such that you can add precision and length
modifiers between percent-sign and conversion specifier.
gint16 in;
gint32 out;
sscanf ("42", "%" G_GINT16_FORMAT, &in)
out = in * 1000;
g_print ("%" G_GINT32_FORMAT, out);
G_GUINT16_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type guint16.
See also G_GINT16_FORMAT.
G_GINT32_MODIFIER
The platform dependent length modifier for conversion specifiers for scanning and printing values
of type gint32 or guint32. It is a string literal, See also G_GINT16_MODIFIER.
Since 2.4
G_GINT32_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type gint32.
See also G_GINT16_FORMAT.
G_GUINT32_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type guint32.
See also G_GINT16_FORMAT.
71
CHAPTER 2. GLIB FUNDAMENTALS 2.8. MISCELLANEOUS MACROS
G_GINT64_MODIFIER
The platform dependent length modifier for conversion specifiers for scanning and printing values
of type gint64 or guint64. It is a string literal.
N OTE
Some platforms do not support printing 64 bit integers, even though the types are sup-
ported. On such platforms G_GINT64_MODIFIER is not defined.
Since 2.4
G_GINT64_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type gint64.
See also G_GINT16_FORMAT.
N OTE
Some platforms do not support scanning and printing 64 bit integers, even though the
types are supported. On such platforms G_GINT64_FORMAT is not defined. Note that
scanf() may not support 64 bit integers, even if G_GINT64_FORMAT is defined. Due to
its weak error handling, scanf() is not recommended for parsing anyway; consider using
g_ascii_strtoull() instead.
G_GUINT64_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type guint64.
See also G_GINT16_FORMAT.
N OTE
Some platforms do not support scanning and printing 64 bit integers, even though the
types are supported. On such platforms G_GUINT64_FORMAT is not defined. Note that
scanf() may not support 64 bit integers, even if G_GINT64_FORMAT is defined. Due to
its weak error handling, scanf() is not recommended for parsing anyway; consider using
g_strtoull() instead.
G_GSIZE_MODIFIER
The platform dependent length modifier for conversion specifiers for scanning and printing values
of type gsize or gssize. It is a string literal,
Since 2.6
72
CHAPTER 2. GLIB FUNDAMENTALS 2.9. ATOMIC OPERATIONS
G_GSIZE_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type gsize.
See also G_GINT16_FORMAT.
Since 2.6
G_GSSIZE_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type gssize.
See also G_GINT16_FORMAT.
Since 2.6
G_GOFFSET_MODIFIER
The platform dependent length modifier for conversion specifiers for scanning and printing values
of type goffset. It is a string literal. See also G_GINT64_MODIFIER.
Since 2.20
G_GOFFSET_FORMAT
This is the platform dependent conversion specifier for scanning and printing values of type goffset.
See also G_GINT64_FORMAT.
Since: 2.20
Synopsis
#include <glib.h>
73
CHAPTER 2. GLIB FUNDAMENTALS 2.9. ATOMIC OPERATIONS
Description
The following functions can be used to atomically access integers and pointers. They are implemented
as inline assembler function on most platforms and use slower fall-backs otherwise. Using them can
sometimes save you from using a performance-expensive GMutex to protect the integer or pointer.
The most important usage is reference counting. Using g_atomic_int_inc() and g_atomic_int_dec_and_test()
makes reference counting a very fast operation.
N OTE
You must not directly read integers or pointers concurrently accessed by multiple threads,
but use the atomic accessor functions instead. That is, always use g_atomic_int_get()
and g_atomic_pointer_get() for read outs. They provide the neccessary synchonization
mechanisms like memory barriers to access memory locations concurrently.
N OTE
If you are using those functions for anything apart from simple reference counting,
you should really be aware of the implications of doing that. There are literally thou-
sands of ways to shoot yourself in the foot. So if in doubt, use a GMutex. If you
don’t know, what memory barriers are, do not use anything but g_atomic_int_inc() and
g_atomic_int_dec_and_test().
N OTE
It is not safe to set an integer or pointer just by assigning to it, when it
is concurrently accessed by other threads with the following functions. Use
g_atomic_int_compare_and_exchange() or g_atomic_pointer_compare_and_exchange()
respectively.
Details
g_atomic_int_get ()
Reads the value of the integer pointed to by atomic. Also acts as a memory barrier.
Returns : the value of *atomic
Since 2.4
g_atomic_int_set ()
Sets the value of the integer pointed to by atomic. Also acts as a memory barrier.
Since 2.10
g_atomic_int_add ()
74
CHAPTER 2. GLIB FUNDAMENTALS 2.9. ATOMIC OPERATIONS
Atomically adds val to the integer pointed to by atomic. Also acts as a memory barrier.
Since 2.4
g_atomic_int_exchange_and_add ()
Atomically adds val to the integer pointed to by atomic. It returns the value of *atomic just before
the addition took place. Also acts as a memory barrier.
Returns : the value of *atomic before the addition.
Since 2.4
g_atomic_int_compare_and_exchange ()
Compares oldval with the integer pointed to by atomic and if they are equal, atomically exchanges
*atomic with newval. Also acts as a memory barrier.
Returns : %TRUE, if *atomic was equal oldval. FALSE otherwise.
Since 2.4
g_atomic_pointer_get ()
Reads the value of the pointer pointed to by atomic. Also acts as a memory barrier.
Returns : the value to add to *atomic.
Since 2.4
g_atomic_pointer_set ()
Sets the value of the pointer pointed to by atomic. Also acts as a memory barrier.
Since 2.10
g_atomic_pointer_compare_and_exchange ()
gboolean g_atomic_pointer_compare_and_exchange
();
Compares oldval with the pointer pointed to by atomic and if they are equal, atomically exchanges
*atomic with newval. Also acts as a memory barrier.
Returns : %TRUE, if *atomic was equal oldval. FALSE otherwise.
Since 2.4
g_atomic_int_inc ()
Since 2.4
75
CHAPTER 2. GLIB FUNDAMENTALS 2.9. ATOMIC OPERATIONS
g_atomic_int_dec_and_test ()
See Also
76
Chapter 3
Synopsis
#include <glib.h>
GMainLoop;
GMainLoop * g_main_loop_new (GMainContext *context,
gboolean is_running);
GMainLoop * g_main_loop_ref (GMainLoop *loop);
void g_main_loop_unref (GMainLoop *loop);
void g_main_loop_run (GMainLoop *loop);
void g_main_loop_quit (GMainLoop *loop);
gboolean g_main_loop_is_running (GMainLoop *loop);
GMainContext * g_main_loop_get_context (GMainLoop *loop);
#define g_main_new (is_running)
#define g_main_destroy (loop)
#define g_main_run (loop)
#define g_main_quit (loop)
#define g_main_is_running (loop)
#define G_PRIORITY_HIGH
#define G_PRIORITY_DEFAULT
#define G_PRIORITY_HIGH_IDLE
#define G_PRIORITY_DEFAULT_IDLE
#define G_PRIORITY_LOW
GMainContext;
GMainContext * g_main_context_new (void);
GMainContext * g_main_context_ref (GMainContext *context);
void g_main_context_unref (GMainContext *context);
GMainContext * g_main_context_default (void);
gboolean g_main_context_iteration (GMainContext *context,
gboolean may_block);
#define g_main_iteration (may_block)
gboolean g_main_context_pending (GMainContext *context);
#define g_main_pending ()
GSource * g_main_context_find_source_by_id (GMainContext *context,
guint source_id);
77
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
GSource * g_main_context_find_source_by_user_data
(GMainContext *context,
gpointer user_data);
GSource * g_main_context_find_source_by_funcs_user_data
(GMainContext *context,
GSourceFuncs *funcs,
gpointer user_data);
void g_main_context_wakeup (GMainContext *context);
gboolean g_main_context_acquire (GMainContext *context);
void g_main_context_release (GMainContext *context);
gboolean g_main_context_is_owner (GMainContext *context);
gboolean g_main_context_wait (GMainContext *context,
GCond *cond,
GMutex *mutex);
gboolean g_main_context_prepare (GMainContext *context,
gint *priority);
gint g_main_context_query (GMainContext *context,
gint max_priority,
gint *timeout_,
GPollFD *fds,
gint n_fds);
gint g_main_context_check (GMainContext *context,
gint max_priority,
GPollFD *fds,
gint n_fds);
void g_main_context_dispatch (GMainContext *context);
void g_main_context_set_poll_func (GMainContext *context,
GPollFunc func);
GPollFunc g_main_context_get_poll_func (GMainContext *context);
gint (*GPollFunc) (GPollFD *ufds,
guint nfsd,
gint timeout_);
void g_main_context_add_poll (GMainContext *context,
GPollFD *fd,
gint priority);
void g_main_context_remove_poll (GMainContext *context,
GPollFD *fd);
gint g_main_depth (void);
GSource * g_main_current_source (void);
#define g_main_set_poll_func (func)
78
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
typedef GPid;
void (*GChildWatchFunc) (GPid pid,
gint status,
gpointer data);
GSource * g_child_watch_source_new (GPid pid);
guint g_child_watch_add (GPid pid,
GChildWatchFunc function,
gpointer data);
guint g_child_watch_add_full (gint priority,
GPid pid,
GChildWatchFunc function,
gpointer data,
GDestroyNotify notify);
GPollFD;
gint g_poll (GPollFD *fds,
guint nfds,
gint timeout);
GSource;
void (*GSourceDummyMarshal) (void);
GSourceFuncs;
GSourceCallbackFuncs;
GSource * g_source_new (GSourceFuncs *source_funcs,
guint struct_size);
GSource * g_source_ref (GSource *source);
void g_source_unref (GSource *source);
void g_source_set_funcs (GSource *source,
GSourceFuncs *funcs);
guint g_source_attach (GSource *source,
GMainContext *context);
void g_source_destroy (GSource *source);
gboolean g_source_is_destroyed (GSource *source);
void g_source_set_priority (GSource *source,
gint priority);
gint g_source_get_priority (GSource *source);
void g_source_set_can_recurse (GSource *source,
gboolean can_recurse);
gboolean g_source_get_can_recurse (GSource *source);
guint g_source_get_id (GSource *source);
GMainContext * g_source_get_context (GSource *source);
void g_source_set_callback (GSource *source,
GSourceFunc func,
gpointer data,
GDestroyNotify notify);
gboolean (*GSourceFunc) (gpointer data);
void g_source_set_callback_indirect (GSource *source,
gpointer callback_data,
GSourceCallbackFuncs *callbac
79
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
Description
The main event loop manages all the available sources of events for GLib and GTK+ applications. These
events can come from any number of different types of sources such as file descriptors (plain files, pipes
or sockets) and timeouts. New types of event sources can also be added using g_source_attach().
To allow multiple independent sets of sources to be handled in different threads, each source is
associated with a GMainContext. A GMainContext can only be running in a single thread, but sources
can be added to it and removed from it from other threads.
Each event source is assigned a priority. The default priority, G_PRIORITY_DEFAULT, is 0. Values
less than 0 denote higher priorities. Values greater than 0 denote lower priorities. Events from high
priority sources are always processed before events from lower priority sources.
Idle functions can also be added, and assigned a priority. These will be run whenever no events with
a higher priority are ready to be processed.
The GMainLoop data type represents a main event loop. A GMainLoop is created with g_main_loop_new().
After adding the initial event sources, g_main_loop_run() is called. This continuously checks for new
events from each of the event sources and dispatches them. Finally, the processing of an event from
one of the sources leads to a call to g_main_loop_quit() to exit the main loop, and g_main_loop_run()
returns.
It is possible to create new instances of GMainLoop recursively. This is often used in GTK+ appli-
cations when showing modal dialog boxes. Note that event sources are associated with a particular
GMainContext, and will be checked and dispatched for all main loops associated with that GMainCon-
text.
GTK+ contains wrappers of some of these functions, e.g. gtk_main(), gtk_main_quit() and gtk_events_pending().
One of the unusual features of the GTK+ main loop functionality is that new types of event source can
be created and used in addition to the builtin type of event source. A new event source type is used
for handling GDK events. A new source type is created by deriving from the GSource structure. The
derived type of source is represented by a structure that has the GSource structure as a first element,
and other elements specific to the new source type. To create an instance of the new source type, call
g_source_new() passing in the size of the derived structure and a table of functions. These GSourceFuncs
determine the behavior of the new source types.
New source types basically interact with the main context in two ways. Their prepare function in
GSourceFuncs can set a timeout to determine the maximum amount of time that the main loop will
sleep before checking the source again. In addition, or as well, the source can add file descriptors to the
set that the main context checks using g_source_add_poll().
Single iterations of a GMainContext can be run with g_main_context_iteration(). In some cases, more
detailed control of exactly how the details of the main loop work is desired, for instance, when integrat-
ing the GMainLoop with an external main loop. In such cases, you can call the component functions of
g_main_context_iteration() directly. These functions are g_main_context_prepare(), g_main_context_query(),
g_main_context_check() and g_main_context_dispatch().
The operation of these functions can best be seen in terms of a state diagram, as shown in Figure 3.1.
80
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
Details
GMainLoop
The GMainLoop struct is an opaque data type representing the main event loop of a GLib or GTK+
application.
g_main_loop_new ()
is_running : set to TRUE to indicate that the loop is running. This is not very important since calling
g_main_loop_run() will set this to TRUE anyway.
Returns : a new GMainLoop.
g_main_loop_ref ()
Returns : loop
g_main_loop_unref ()
Decreases the reference count on a GMainLoop object by one. If the result is zero, free the loop and
free all associated memory.
loop : a GMainLoop
g_main_loop_run ()
Runs a main loop until g_main_loop_quit() is called on the loop. If this is called for the thread of the
loop’s GMainContext, it will process events from the loop, otherwise it will simply wait.
loop : a GMainLoop
g_main_loop_quit ()
Stops a GMainLoop from running. Any calls to g_main_loop_run() for the loop will return.
Note that sources that have already been dispatched when g_main_loop_quit() is called will still be
executed.
loop : a GMainLoop
81
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_loop_is_running ()
Checks to see if the main loop is currently being run via g_main_loop_run().
loop : a GMainLoop.
g_main_loop_get_context ()
loop : a GMainLoop.
g_main_new()
#define g_main_new(is_running)
WARNING
g_main_new has been deprecated since version 2.2 and should not be used in newly-
written code. Use g_main_loop_new() instead.
is_running : set to TRUE to indicate that the loop is running. This is not very important since calling
g_main_run() will set this to TRUE anyway.
g_main_destroy()
#define g_main_destroy(loop)
WARNING
g_main_destroy has been deprecated since version 2.2 and should not be used in
newly-written code. Use g_main_loop_unref() instead.
loop : a GMainLoop.
82
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_run()
#define g_main_run(loop)
WARNING
g_main_run has been deprecated since version 2.2 and should not be used in newly-
written code. Use g_main_loop_run() instead.
loop : a GMainLoop.
g_main_quit()
#define g_main_quit(loop)
WARNING
g_main_quit has been deprecated since version 2.2 and should not be used in newly-
written code. Use g_main_loop_quit() instead.
Stops the GMainLoop. If g_main_run() was called to run the GMainLoop, it will now return.
loop : a GMainLoop.
g_main_is_running()
#define g_main_is_running(loop)
WARNING
g_main_is_running has been deprecated since version 2.2 and should not be used
in newly-written code. USe g_main_loop_is_running() instead.
loop : a GMainLoop.
G_PRIORITY_HIGH
Use this for high priority event sources. It is not used within GLib or GTK+.
83
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
G_PRIORITY_DEFAULT
#define G_PRIORITY_DEFAULT 0
Use this for default priority event sources. In GLib this priority is used when adding timeout func-
tions with g_timeout_add(). In GDK this priority is used for events from the X server.
G_PRIORITY_HIGH_IDLE
Use this for high priority idle functions. GTK+ uses G_PRIORITY_HIGH_IDLE + 10 for resizing
operations, and G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is done to ensure that
any pending resizes are processed before any pending redraws, so that widgets are not redrawn twice
unnecessarily.)
G_PRIORITY_DEFAULT_IDLE
Use this for default priority idle functions. In GLib this priority is used when adding idle functions
with g_idle_add().
G_PRIORITY_LOW
Use this for very low priority background tasks. It is not used within GLib or GTK+.
GMainContext
The GMainContext struct is an opaque data type representing a set of sources to be handled in a
main loop.
g_main_context_new ()
g_main_context_ref ()
g_main_context_unref ()
Decreases the reference count on a GMainContext object by one. If the result is zero, free the context
and free all associated memory.
context : a GMainContext
84
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_context_default ()
Returns the default main context. This is the main context used for main loop functions when a main
loop is not explicitly specified.
g_main_context_iteration ()
Runs a single iteration for the given main loop. This involves checking to see if any event sources are
ready to be processed, then if no events sources are ready and may_block is TRUE, waiting for a source
to become ready, then dispatching the highest priority events sources that are ready. Otherwise, if may-
_block is FALSE sources are not waited to become ready, only those highest priority events sources will
be dispatched (if any), that are ready at this given moment without further waiting.
Note that even when may_block is TRUE, it is still possible for g_main_context_iteration() to return
FALSE, since the the wait may be interrupted for other reasons than an event source becoming ready.
g_main_iteration()
#define g_main_iteration(may_block)
WARNING
g_main_iteration has been deprecated since version 2.2 and should not be used
in newly-written code. Use g_main_context_iteration() instead.
may_block : set to TRUE if it should block (i.e. wait) until an event source becomes ready. It will return
after an event source has been processed. If set to FALSE it will return immediately if no event
source is ready to be processed.
g_main_context_pending ()
Checks if any sources have pending events for the given context.
85
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_pending()
#define g_main_pending()
WARNING
g_main_pending has been deprecated since version 2.2 and should not be used in
newly-written code. Use g_main_context_pending() instead.
Checks if any events are pending for the default GMainContext (i.e. ready to be processed).
g_main_context_find_source_by_id ()
g_main_context_find_source_by_user_data ()
GSource * g_main_context_find_source_by_user_data
(GMainContext *context,
gpointer user_data);
Finds a source with the given user data for the callback. If multiple sources exist with the same user
data, the first one found will be returned.
context : a GMainContext
g_main_context_find_source_by_funcs_user_data ()
GSource * g_main_context_find_source_by_funcs_user_data
(GMainContext *context,
GSourceFuncs *funcs,
gpointer user_data);
Finds a source with the given source functions and user data. If multiple sources exist with the same
source function and user data, the first one found will be returned.
86
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_context_wakeup ()
If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.
context : a GMainContext
g_main_context_acquire ()
Tries to become the owner of the specified context. If some other thread is the owner of the context,
returns FALSE immediately. Ownership is properly recursive: the owner can require ownership again
and will release ownership when g_main_context_release() is called as many times as g_main_context_acquire().
You must be the owner of a context before you can call g_main_context_prepare(), g_main_context_query(),
g_main_context_check(), g_main_context_dispatch().
context : a GMainContext
Returns : TRUE if the operation succeeded, and this thread is now the owner of context.
g_main_context_release ()
context : a GMainContext
g_main_context_is_owner ()
Determines whether this thread holds the (recursive) ownership of this GMaincontext. This is useful
to know before waiting on another thread that may be blocking to get ownership of context.
context : a GMainContext
Since 2.10
g_main_context_wait ()
Tries to become the owner of the specified context, as with g_main_context_acquire(). But if another
thread is the owner, atomically drop mutex and wait on cond until that owner releases ownership or
until cond is signaled, then try again (once) to become the owner.
context : a GMainContext
Returns : TRUE if the operation succeeded, and this thread is now the owner of context.
87
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_context_prepare ()
Prepares to poll sources within a main loop. The resulting information for polling is determined by
calling g_main_context_query().
context : a GMainContext
g_main_context_query ()
context : a GMainContext
Returns : the number of records actually stored in fds, or, if more than n_fds records need to be stored,
the number of records that need to be stored.
g_main_context_check ()
context : a GMainContext
fds : array of GPollFD’s that was passed to the last call to g_main_context_query()
g_main_context_dispatch ()
context : a GMainContext
88
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_context_set_poll_func ()
Sets the function to use to handle polling of file descriptors. It will be used instead of the poll()
system call (or GLib’s replacement function, which is used where poll() isn’t available).
This function could possibly be used to integrate the GLib event loop with an external event loop.
context : a GMainContext
g_main_context_get_poll_func ()
context : a GMainContext
GPollFunc ()
Specifies the type of function passed to g_main_context_set_poll_func(). The semantics of the func-
tion should match those of the poll() system call.
timeout_ : the maximum time to wait for an event of the file descriptors. A negative value indicates an
infinite timeout.
Returns : the number of GPollFD elements which have events or errors reported, or -1 if an error oc-
curred.
g_main_context_add_poll ()
Adds a file descriptor to the set of file descriptors polled for this context. This will very seldomly be
used directly. Instead a typical event source will use g_source_add_poll() instead.
priority : the priority for this file descriptor which should be the same as the priority used for g_source_attach()
to ensure that the file descriptor is polled whenever the results may be needed.
89
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_main_context_remove_poll ()
Removes file descriptor from the set of file descriptors to be polled for a particular context.
context : a GMainContext
g_main_depth ()
Returns the depth of the stack of calls to g_main_context_dispatch() on any GMainContext in the
current thread. That is, when called from the toplevel, it gives 0. When called from within a callback
from g_main_context_iteration() (or g_main_loop_run(), etc.) it returns 1. When called from within a
callback to a recursive call to g_main_context_iterate(), it returns 2. And so forth.
This function is useful in a situation like the following: Imagine an extremely simple "garbage col-
lected" system.
static GList *free_list;
gpointer
allocate_memory (gsize size)
{
gpointer result = g_malloc (size);
free_list = g_list_prepend (free_list, result);
return result;
}
void
free_allocated_memory (void)
{
GList *l;
for (l = free_list; l; l = l->next);
g_free (l->data);
g_list_free (free_list);
free_list = NULL;
}
[...]
while (TRUE);
{
g_main_context_iteration (NULL, TRUE);
free_allocated_memory();
}
This works from an application, however, if you want to do the same thing from a library, it gets
more difficult, since you no longer control the main loop. You might think you can simply use an idle
function to make the call to free_allocated_memory(), but that doesn’t work, since the idle function
could be called from a recursive callback. This can be fixed by using g_main_depth()
gpointer
allocate_memory (gsize size)
{
FreeListBlock *block = g_new (FreeListBlock, 1);
block->mem = g_malloc (size);
block->depth = g_main_depth ();
free_list = g_list_prepend (free_list, block);
return block->mem;
}
90
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
void
free_allocated_memory (void)
{
GList *l;
l = next;
}
}
There is a temptation to use g_main_depth() to solve problems with reentrancy. For instance, while
waiting for data to be received from the network in response to a menu item, the menu item might be
selected again. It might seem that one could make the menu item’s callback return immediately and
do nothing if g_main_depth() returns a value greater than 1. However, this should be avoided since
the user then sees selecting the menu item do nothing. Furthermore, you’ll find yourself adding these
checks all over your code, since there are doubtless many, many things that the user could do. Instead,
you can use the following techniques:
1. Use gtk_widget_set_sensitive() or modal dialogs to prevent the user from interacting with ele-
ments while the main loop is recursing.
2. Avoid main loop recursion in situations where you can’t handle arbitrary callbacks. Instead, struc-
ture your code so that you simply return to the main loop and then get called again when there is
more work to do.
g_main_current_source ()
g_main_set_poll_func()
#define g_main_set_poll_func(func)
WARNING
g_main_set_poll_func has been deprecated since version 2.2 and should not be
used in newly-written code. Use g_main_context_set_poll_func() instead.
Sets the function to use for the handle polling of file descriptors for the default main context.
func : the function to call to poll all file descriptors.
91
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_timeout_source_new ()
g_timeout_source_new_seconds ()
g_timeout_add ()
Sets a function to be called at regular intervals, with the default priority, G_PRIORITY_DEFAULT.
The function is called repeatedly until it returns FALSE, at which point the timeout is automatically
destroyed and the function will not be called again. The first call to the function will be at the end of the
first interval.
Note that timeout functions may be delayed, due to the processing of other event sources. Thus they
should not be relied on for precise timing. After each call to the timeout function, the time of the next
timeout is recalculated based on the current time and the given interval (it does not try to ’catch up’ time
lost in delays).
If you want to have a timer in the "seconds" range and do not care about the exact time of the first
call of the timer, use the g_timeout_add_seconds() function; this function allows for more optimizations
and more efficient system power usage.
This internally creates a main loop source using g_timeout_source_new() and attaches it to the main
loop context using g_source_attach(). You can do these steps manually if you need greater control.
interval : the time between calls to the function, in milliseconds (1/1000ths of a second)
g_timeout_add_full ()
92
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
Sets a function to be called at regular intervals, with the given priority. The function is called repeat-
edly until it returns FALSE, at which point the timeout is automatically destroyed and the function will
not be called again. The notify function is called when the timeout is destroyed. The first call to the
function will be at the end of the first interval.
Note that timeout functions may be delayed, due to the processing of other event sources. Thus they
should not be relied on for precise timing. After each call to the timeout function, the time of the next
timeout is recalculated based on the current time and the given interval (it does not try to ’catch up’ time
lost in delays).
This internally creates a main loop source using g_timeout_source_new() and attaches it to the main
loop context using g_source_attach(). You can do these steps manually if you need greater control.
priority : the priority of the timeout source. Typically this will be in the range between G_PRIORITY_DEFAULT
and G_PRIORITY_HIGH.
interval : the time between calls to the function, in milliseconds (1/1000ths of a second)
g_timeout_add_seconds ()
Sets a function to be called at regular intervals with the default priority, G_PRIORITY_DEFAULT.
The function is called repeatedly until it returns FALSE, at which point the timeout is automatically
destroyed and the function will not be called again.
This internally creates a main loop source using g_timeout_source_new_seconds() and attaches it to
the main loop context using g_source_attach(). You can do these steps manually if you need greater
control. Also see g_timout_add_seconds_full().
Since 2.14
g_timeout_add_seconds_full ()
Sets a function to be called at regular intervals, with priority . The function is called repeatedly
until it returns FALSE, at which point the timeout is automatically destroyed and the function will not
be called again.
Unlike g_timeout_add(), this function operates at whole second granularity. The initial starting point
of the timer is determined by the implementation and the implementation is expected to group multi-
ple timers together so that they fire all at the same time. To allow this grouping, the interval to the
first timer is rounded and can deviate up to one second from the specified interval. Subsequent timer
iterations will generally run at the specified interval.
93
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
Note that timeout functions may be delayed, due to the processing of other event sources. Thus they
should not be relied on for precise timing. After each call to the timeout function, the time of the next
timeout is recalculated based on the current time and the given interval
If you want timing more precise than whole seconds, use g_timeout_add() instead.
The grouping of timers to fire at the same time results in a more power and CPU efficient behavior
so if your timer is in multiples of seconds and you don’t require the first timer exactly one second from
now, the use of g_timeout_add_seconds() is preferred over g_timeout_add().
This internally creates a main loop source using g_timeout_source_new_seconds() and attaches it to
the main loop context using g_source_attach(). You can do these steps manually if you need greater
control.
priority : the priority of the timeout source. Typically this will be in the range between G_PRIORITY_DEFAULT
and G_PRIORITY_HIGH.
Since 2.14
g_idle_source_new ()
g_idle_add ()
Adds a function to be called whenever there are no higher priority events pending to the default
main loop. The function is given the default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function
returns FALSE it is automatically removed from the list of event sources and will not be called again.
This internally creates a main loop source using g_idle_source_new() and attaches it to the main loop
context using g_source_attach(). You can do these steps manually if you need greater control.
g_idle_add_full ()
94
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
Adds a function to be called whenever there are no higher priority events pending. If the function
returns FALSE it is automatically removed from the list of event sources and will not be called again.
This internally creates a main loop source using g_idle_source_new() and attaches it to the main loop
context using g_source_attach(). You can do these steps manually if you need greater control.
priority : the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE
and G_PRIORITY_HIGH_IDLE.
g_idle_remove_by_data ()
GPid
A type which is used to hold a process identification. On Unix, processes are identified by a process
id (an integer), while Windows uses process handles (which are pointers).
GChildWatchFunc ()
status : Status information about the child process, see waitpid(2) for more information about this field
g_child_watch_source_new ()
95
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
pid : process to watch. On POSIX the pid of a child process. On Windows a handle for a process (which
doesn’t have to be a child).
Since 2.4
g_child_watch_add ()
Sets a function to be called when the child indicated by pid exits, at a default priority, G_PRIORITY_DEFAULT.
If you obtain pid from g_spawn_async() or g_spawn_async_with_pipes() you will need to pass
G_SPAWN_DO_NOT_REAP_CHILD as flag to the spawn function for the child watching to work.
Note that on platforms where GPid must be explicitly closed (see g_spawn_close_pid()) pid must
not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the
callback function for the source.
GLib supports only a single callback per process id.
This internally creates a main loop source using g_child_watch_source_new() and attaches it to the
main loop context using g_source_attach(). You can do these steps manually if you need greater control.
pid : process id to watch. On POSIX the pid of a child process. On Windows a handle for a process
(which doesn’t have to be a child).
Since 2.4
g_child_watch_add_full ()
Sets a function to be called when the child indicated by pid exits, at the priority priority .
If you obtain pid from g_spawn_async() or g_spawn_async_with_pipes() you will need to pass
G_SPAWN_DO_NOT_REAP_CHILD as flag to the spawn function for the child watching to work.
Note that on platforms where GPid must be explicitly closed (see g_spawn_close_pid()) pid must
not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the
callback function for the source.
GLib supports only a single callback per process id.
This internally creates a main loop source using g_child_watch_source_new() and attaches it to the
main loop context using g_source_attach(). You can do these steps manually if you need greater control.
priority : the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE
and G_PRIORITY_HIGH_IDLE.
pid : process to watch. On POSIX the pid of a child process. On Windows a handle for a process (which
doesn’t have to be a child).
96
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
GPollFD
typedef struct {
#if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
gint64 fd;
#else
gint fd;
#endif
gushort events;
gushort revents;
} GPollFD;
gint fd; the file descriptor to poll (or a HANDLE on Win32 platforms).
a bitwise combination of flags from GIOCondition, specifying which events
should be polled for. Typically for reading from a file descriptor you would
gushort events;
use G_IO_IN | G_IO_HUP | G_IO_ERR, and for writing you would use
G_IO_OUT | G_IO_ERR.
a bitwise combination of flags from GIOCondition, returned from the
gushort revents;
poll() function to indicate which events occurred.
g_poll ()
Polls fds, as with the poll() system call, but portably. (On systems that don’t have poll(), it is emu-
lated using select().) This is used internally by GMainContext, but it can be called directly if you need
to block until a file descriptor is ready, but don’t want to run the full main loop.
Each element of fds is a GPollFD describing a single file descriptor to poll. The fd field indicates the
file descriptor, and the events field indicates the events to poll for. On return, the revents fields will be
filled with the events that actually occurred.
On POSIX systems, the file descriptors in fds can be any sort of file descriptor, but the situation is
much more complicated on Windows. If you need to use g_poll() in code that has to run on Windows,
the easiest solution is to construct all of your GPollFDs with g_io_channel_win32_make_pollfd().
Returns : the number of entries in fds whose revents fields were filled in, or 0 if the operation timed
out, or -1 on error or if the call was interrupted.
Since 2.20
GSource
typedef struct {
} GSource;
97
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
GSourceDummyMarshal ()
This is just a placeholder for GClosureMarshal, which cannot be used here for dependency reasons.
GSourceFuncs
typedef struct {
gboolean (*prepare) (GSource *source,
gint *timeout_);
gboolean (*check) (GSource *source);
gboolean (*dispatch) (GSource *source,
GSourceFunc callback,
gpointer user_data);
void (*finalize) (GSource *source); /* Can be NULL */
The GSourceFuncs struct contains a table of functions used to handle event sources in a generic
manner.
For idle sources, the prepare and check functions always return TRUE to indicate that the source is
always ready to be processed. The prepare function also returns a timeout value of 0 to ensure that the
poll() call doesn’t block (since that would be time wasted which could have been spent running the idle
function).
For timeout sources, the prepare and check functions both return TRUE if the timeout interval has
expired. The prepare function also returns a timeout value to ensure that the poll() call doesn’t block too
long and miss the next timeout.
For file descriptor sources, the prepare function typically returns FALSE, since it must wait until
poll() has been called before it knows whether any events need to be processed. It sets the returned
timeout to -1 to indicate that it doesn’t mind how long the poll() call blocks. In the check function, it
tests the results of the poll() call to see if the required condition has been met, and returns TRUE if so.
prepare () Called before all the file descriptors are polled. If the source can determine that it is ready
here (without waiting for the results of the poll() call) it should return TRUE. It can also return a
timeout_ value which should be the maximum timeout (in milliseconds) which should be passed
to the poll() call. The actual timeout used will be -1 if all sources returned -1, or it will be the
minimum of all the timeout_ values returned which were >= 0.
check () Called after all the file descriptors are polled. The source should return TRUE if it is ready
to be dispatched. Note that some time may have passed since the previous prepare function was
called, so the source should be checked again here.
dispatch () Called to dispatch the event source, after it has returned TRUE in either its prepare or its c-
heck function. The dispatch function is passed in a callback function and data. The callback func-
tion may be NULL if the source was never connected to a callback using g_source_set_callback().
The dispatch function should call the callback function with user_data and whatever additional
parameters are needed for this type of event source.
GSourceFunc closure_callback ;
GSourceDummyMarshal closure_marshal;
98
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
GSourceCallbackFuncs
typedef struct {
void (*ref) (gpointer cb_data);
void (*unref) (gpointer cb_data);
void (*get) (gpointer cb_data,
GSource *source,
GSourceFunc *func,
gpointer *data);
} GSourceCallbackFuncs;
get () Called to extract the callback function and data from the callback object.
g_source_new ()
Creates a new GSource structure. The size is specified to allow creating structures derived from
GSource that contain additional data. The size passed in must be at least sizeof (GSource).
The source will not initially be associated with any GMainContext and must be added to one with
g_source_attach() before it will be executed.
source_funcs : structure containing functions that implement the sources behavior.
g_source_ref ()
Returns : source
g_source_unref ()
Decreases the reference count of a source by one. If the resulting reference count is zero the source
and associated memory will be destroyed.
source : a GSource
g_source_set_funcs ()
Sets the source functions (can be used to override default implementations) of an unattached source.
source : a GSource
99
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_source_attach ()
Adds a GSource to a context so that it will be executed within that context. Remove it by calling
g_source_destroy().
source : a GSource
Returns : the ID (greater than 0) for the source within the GMainContext.
g_source_destroy ()
Removes a source from its GMainContext, if any, and mark it as destroyed. The source cannot be
subsequently added to another context.
source : a GSource
g_source_is_destroyed ()
GDK_THREADS_ENTER ();
/* do stuff with self */
self->idle_id = 0;
GDK_THREADS_LEAVE ();
return FALSE;
}
static void
some_widget_do_stuff_later (SomeWidget *self)
{
self->idle_id = g_idle_add (idle_callback, self);
}
static void
some_widget_finalize (GObject *object)
{
SomeWidget *self = SOME_WIDGET (object);
if (self->idle_id)
g_source_remove (self->idle_id);
This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires
due to the use after free in the callback. A solution, to this particular problem, is to check to if the source
has already been destroy within the callback.
100
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
static gboolean
idle_callback (gpointer data)
{
SomeWidget *self = data;
GDK_THREADS_ENTER ();
if (!g_source_is_destroyed (g_main_current_source ()))
{
/* do stuff with self */
}
GDK_THREADS_LEAVE ();
return FALSE;
}
source : a GSource
g_source_set_priority ()
Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready
to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.
source : a GSource
g_source_get_priority ()
g_source_set_can_recurse ()
Sets whether a source can be called recursively. If can_recurse is TRUE, then while the source is
being dispatched then this source will be processed normally. Otherwise, all processing of this source is
blocked until the dispatch function returns.
source : a GSource
g_source_get_can_recurse ()
101
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_source_get_id ()
Returns the numeric ID for a particular source. The ID of a source is a positive integer which is
unique within a particular main loop context. The reverse mapping from ID to source is done by
g_main_context_find_source_by_id().
source : a GSource
g_source_get_context ()
Gets the GMainContext with which the source is associated. Calling this function on a destroyed
source is an error.
source : a GSource
Returns : the GMainContext with which the source is associated, or NULL if the context has not yet
been added to a source.
g_source_set_callback ()
Sets the callback function for a source. The callback for a source is called from the source’s dispatch
function.
The exact type of func depends on the type of source; ie. you should not count on func being called
with data as its first parameter.
Typically, you won’t use this function. Instead use functions specific to the type of source you are
using.
GSourceFunc ()
data : data passed to the function, set when the source was created with one of the above functions.
102
CHAPTER 3. GLIB CORE APPLICATION . . . 3.1. THE MAIN EVENT LOOP
g_source_set_callback_indirect ()
Sets the callback function storing the data as a refcounted callback "object". This is used internally.
Note that calling g_source_set_callback_indirect() assumes an initial reference count on callback_data,
and thus callback_funcs->unref will eventually be called once more than callback_funcs->ref .
source : the source
callback_funcs : functions for reference counting callback_data and getting the callback and data
g_source_add_poll ()
Adds a file descriptor to the set of file descriptors polled for this source. This is usually combined
with g_source_new() to add an event source. The event source’s check function will typically test the
revents field in the GPollFD struct and return TRUE if events need to be processed.
source : a GSource
g_source_remove_poll ()
Removes a file descriptor from the set of file descriptors polled for this source.
source : a GSource
g_source_get_current_time ()
Gets the "current time" to be used when checking this source. The advantage of calling this function
over calling g_get_current_time() directly is that when checking multiple sources, GLib can cache a
single value instead of having to repeatedly get the system time.
source : a GSource
g_source_remove ()
Removes the source with the given id from the default main context. The id of a GSource is given by
g_source_get_id(), or will be returned by the functions g_source_attach(), g_idle_add(), g_idle_add_full(),
g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), g_child_watch_add_full(), g_io_add_watch(),
and g_io_add_watch_full().
See also g_source_destroy(). You must use g_source_destroy() for sources added to a non-default
main context.
103
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
g_source_remove_by_funcs_user_data ()
Removes a source from the default main loop context given the source functions and user data. If
multiple sources exist with the same source functions and user data, only one will be destroyed.
g_source_remove_by_user_data ()
Removes a source from the default main loop context given the user data for the callback. If multiple
sources exist with the same user data, only one will be destroyed.
3.2 Threads
Name
Threads – thread abstraction; including threads, different mutexes, conditions and thread private data
Synopsis
#include <glib.h>
#define G_THREADS_ENABLED
#define G_THREADS_IMPL_POSIX
#define G_THREADS_IMPL_NONE
#define G_THREAD_ERROR
enum GThreadError;
GThreadFunctions;
void g_thread_init (GThreadFunctions *vtable);
gboolean g_thread_supported ();
gboolean g_thread_get_initialized (void);
104
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
gpointer data,
gulong stack_size,
gboolean joinable,
gboolean bound,
GThreadPriority priority,
GError **error);
GThread* g_thread_self (void);
gpointer g_thread_join (GThread *thread);
void g_thread_set_priority (GThread *thread,
GThreadPriority priority);
void g_thread_yield ();
void g_thread_exit (gpointer retval);
void g_thread_foreach (GFunc thread_func,
gpointer user_data);
GMutex;
GMutex * g_mutex_new ();
void g_mutex_lock (GMutex *mutex);
gboolean g_mutex_trylock (GMutex *mutex);
void g_mutex_unlock (GMutex *mutex);
void g_mutex_free (GMutex *mutex);
GStaticMutex;
#define G_STATIC_MUTEX_INIT
void g_static_mutex_init (GStaticMutex *mutex);
void g_static_mutex_lock (GStaticMutex *mutex);
gboolean g_static_mutex_trylock (GStaticMutex *mutex);
void g_static_mutex_unlock (GStaticMutex *mutex);
GMutex * g_static_mutex_get_mutex (GStaticMutex *mutex);
void g_static_mutex_free (GStaticMutex *mutex);
GStaticRecMutex;
#define G_STATIC_REC_MUTEX_INIT
void g_static_rec_mutex_init (GStaticRecMutex *mutex);
void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
guint depth);
guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
void g_static_rec_mutex_free (GStaticRecMutex *mutex);
GStaticRWLock;
#define G_STATIC_RW_LOCK_INIT
void g_static_rw_lock_init (GStaticRWLock *lock);
void g_static_rw_lock_reader_lock (GStaticRWLock *lock);
gboolean g_static_rw_lock_reader_trylock (GStaticRWLock *lock);
void g_static_rw_lock_reader_unlock (GStaticRWLock *lock);
void g_static_rw_lock_writer_lock (GStaticRWLock *lock);
gboolean g_static_rw_lock_writer_trylock (GStaticRWLock *lock);
void g_static_rw_lock_writer_unlock (GStaticRWLock *lock);
void g_static_rw_lock_free (GStaticRWLock *lock);
105
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
GCond;
GCond* g_cond_new ();
void g_cond_signal (GCond *cond);
void g_cond_broadcast (GCond *cond);
void g_cond_wait (GCond *cond,
GMutex *mutex);
gboolean g_cond_timed_wait (GCond *cond,
GMutex *mutex,
GTimeVal *abs_time);
void g_cond_free (GCond *cond);
GPrivate;
GPrivate* g_private_new (GDestroyNotify destructor);
gpointer g_private_get (GPrivate *private_key);
void g_private_set (GPrivate *private_key,
gpointer data);
GStaticPrivate;
#define G_STATIC_PRIVATE_INIT
void g_static_private_init (GStaticPrivate *private_key);
gpointer g_static_private_get (GStaticPrivate *private_key);
void g_static_private_set (GStaticPrivate *private_key,
gpointer data,
GDestroyNotify notify);
void g_static_private_free (GStaticPrivate *private_key);
GOnce;
enum GOnceStatus;
#define G_ONCE_INIT
#define g_once (once, func, arg)
gboolean g_once_init_enter (volatile gsize *value_location);
void g_once_init_leave (volatile gsize *value_location,
gsize initialization_value);
Description
Threads act almost like processes, but unlike processes all threads of one process share the same mem-
ory. This is good, as it provides easy communication between the involved threads via this shared
memory, and it is bad, because strange things (so called "Heisenbugs") might happen if the program is
not carefully designed. In particular, due to the concurrent nature of threads, no assumptions on the
order of execution of code running in different threads can be made, unless order is explicitly forced by
the programmer through synchronization primitives.
The aim of the thread related functions in GLib is to provide a portable means for writing multi-
threaded software. There are primitives for mutexes to protect the access to portions of memory (GMu-
tex, GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and GStaticRWLock). There are primitives for
condition variables to allow synchronization of threads (GCond). There are primitives for thread-private
data - data that every thread has a private instance of (GPrivate, GStaticPrivate). Last but definitely not
least there are primitives to portably create and manage threads (GThread).
You must call g_thread_init() before executing any other GLib functions (except g_mem_set_vtable())
in a GLib program if g_thread_init() will be called at all. This is a requirement even if no threads are in
fact ever created by the process. It is enough that g_thread_init() is called. If other GLib functions have
been called before that, the behaviour of the program is undefined. An exception is g_mem_set_vtable()
which may be called before g_thread_init(). Failing this requirement can lead to hangs or crashes, appar-
ently more easily on Windows than on Linux, for example. Please note that if you call functions in some
GLib-using library, in particular those above the GTK+ stack, that library might well call g_thread_init()
itself, or call some other library that calls g_thread_init(). Thus, if you use some GLib-based library that
is above the GTK+ stack, it is safest to call g_thread_init() in your application’s main() before calling any
GLib functions or functions in GLib-using libraries. After calling g_thread_init(), GLib is completely
106
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
thread safe (all global data is automatically locked), but individual data structure instances are not au-
tomatically locked for performance reasons. So, for example you must coordinate accesses to the same
GHashTable from multiple threads. The two notable exceptions from this rule are GMainLoop and
GAsyncQueue, which are threadsafe and needs no further application-level locking to be accessed from
multiple threads.
To help debugging problems in multithreaded applications, GLib supports error-checking mutexes
that will give you helpful error messages on common problems. To use error-checking mutexes, define
the symbol G_ERRORCHECK_MUTEXES when compiling the application.
Details
G_THREADS_ENABLED
#define G_THREADS_ENABLED
This macro is defined if GLib was compiled with thread support. This does not necessarily mean
that there is a thread implementation available, but it does mean that the infrastructure is in place and
that once you provide a thread implementation to g_thread_init(), GLib will be multi-thread safe. If
G_THREADS_ENABLED is not defined, then Glib is not, and cannot be, multi-thread safe.
G_THREADS_IMPL_POSIX
#define G_THREADS_IMPL_POSIX
G_THREADS_IMPL_NONE
#define G_THREADS_IMPL_NONE
This macro is defined if no thread implementation is used. You can, however, provide one to
g_thread_init() to make GLib multi-thread safe.
G_THREAD_ERROR
enum GThreadError
typedef enum
{
G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
} GThreadError;
GThreadFunctions
typedef struct {
GMutex* (*mutex_new) (void);
void (*mutex_lock) (GMutex *mutex);
gboolean (*mutex_trylock) (GMutex *mutex);
void (*mutex_unlock) (GMutex *mutex);
void (*mutex_free) (GMutex *mutex);
GCond* (*cond_new) (void);
void (*cond_signal) (GCond *cond);
107
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
This function table is used by g_thread_init() to initialize the thread system. The functions in the
table are directly used by their g_* prepended counterparts (described in this document). For example,
if you call g_mutex_new() then mutex_new() from the table provided to g_thread_init() will be called.
N OTE
Do not use this struct unless you know what you are doing.
g_thread_init ()
If you use GLib from more than one thread, you must initialize the thread system by calling g_thread_init().
Most of the time you will only have to call g_thread_init (NULL).
N OTE
Do not call g_thread_init() with a non-NULL parameter unless you really know what you
are doing.
108
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
N OTE
g_thread_init() must not be called directly or indirectly as a callback from GLib. Also no
mutexes may be currently locked while calling g_thread_init().
N OTE
g_thread_init() changes the way in which GTimer measures elapsed time. As a conse-
quence, timers that are running while g_thread_init() is called may report unreliable times.
g_thread_init() might only be called once. On the second call it will abort with an error. If you want
to make sure that the thread system is initialized, you can do this:
if (!g_thread_supported ()) g_thread_init (NULL);
After that line, either the thread system is initialized or, if no thread system is available in GLib (i.e.
either G_THREADS_ENABLED is not defined or G_THREADS_IMPL_NONE is defined), the program
will abort.
If no thread system is available and vtable is NULL or if not all elements of vtable are non-NULL,
then g_thread_init() will abort.
N OTE
To use g_thread_init() in your program, you have to link with the libraries that the command
pkg-config --libs gthread-2.0 outputs. This is not the case for all the other thread related
functions of GLib. Those can be used without having to link with the thread libraries.
vtable : a function table of type GThreadFunctions, that provides the entry points to the thread system
to be used.
g_thread_supported ()
This function returns TRUE if the thread system is initialized, and FALSE if it is not.
N OTE
This function is actually a macro. Apart from taking the address of it you can however use
it as if it was a function.
g_thread_get_initialized ()
109
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
Since 2.20
GThreadFunc ()
Returns : the return value of the thread, which will be returned by g_thread_join().
enum GThreadPriority
typedef enum
{
G_THREAD_PRIORITY_LOW,
G_THREAD_PRIORITY_NORMAL,
G_THREAD_PRIORITY_HIGH,
G_THREAD_PRIORITY_URGENT
} GThreadPriority;
N OTE
It is not guaranteed that threads with different priorities really behave accordingly. On
some systems (e.g. Linux) there are no thread priorities. On other systems (e.g. Solaris)
there doesn’t seem to be different scheduling for different priorities. All in all try to avoid
being dependent on priorities.
GThread
typedef struct {
} GThread;
The GThread struct represents a running thread. It has three public read-only members, but the
underlying struct is bigger, so you must not copy this struct.
N OTE
Resources for a joinable thread are not fully released until g_thread_join() is called for that
thread.
110
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
g_thread_create ()
g_thread_create_full ()
This function creates a new thread with the priority priority . If the underlying thread implementa-
tion supports it, the thread gets a stack size of stack_size or the default value for the current platform,
if stack_size is 0.
If joinable is TRUE, you can wait for this threads termination calling g_thread_join(). Otherwise
the thread will just disappear when it terminates. If bound is TRUE, this thread will be scheduled in
the system scope, otherwise the implementation is free to do scheduling in the process scope. The first
variant is more expensive resource-wise, but generally faster. On some systems (e.g. Linux) all threads
are bound.
The new thread executes the function func with the argument data. If the thread was created suc-
cessfully, it is returned.
error can be NULL to ignore errors, or non-NULL to report errors. The error is set, if and only if the
function returns NULL.
N OTE
It is not guaranteed that threads with different priorities really behave accordingly. On
some systems (e.g. Linux) there are no thread priorities. On other systems (e.g. Solaris)
there doesn’t seem to be different scheduling for different priorities. All in all try to avoid
being dependent on priorities. Use G_THREAD_PRIORITY_NORMAL here as a default.
111
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
N OTE
g_thread_self ()
g_thread_join ()
Waits until thread finishes, i.e. the function func, as given to g_thread_create(), returns or g_thread_exit()
is called by thread . All resources of thread including the GThread struct are released. thread must
have been created with joinable=TRUE in g_thread_create(). The value returned by func or given to
g_thread_exit() by thread is returned by this function.
thread : a GThread to be waited for.
g_thread_set_priority ()
N OTE
It is not guaranteed that threads with different priorities really behave accordingly. On
some systems (e.g. Linux) there are no thread priorities. On other systems (e.g. Solaris)
there doesn’t seem to be different scheduling for different priorities. All in all try to avoid
being dependent on priorities.
112
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
thread : a GThread.
g_thread_yield ()
g_thread_exit ()
Exits the current thread. If another thread is waiting for that thread using g_thread_join() and the
current thread is joinable, the waiting thread will be woken up and get retval as the return value of
g_thread_join(). If the current thread is not joinable, retval is ignored. Calling
g_thread_exit (retval);
is equivalent to calling
return retval;
N OTE
Never call g_thread_exit() from within a thread of a GThreadPool, as that will mess up the
bookkeeping and lead to funny and unwanted results.
g_thread_foreach ()
Call thread_func on all existing GThread structures. Note that threads may decide to exit while
thread_func is running, so without intimate knowledge about the lifetime of foreign threads, thread-
_func shouldn’t access the GThread* pointer passed in as first argument. However, thread_func will
not be called for threads which are known to have exited already.
Due to thread lifetime checks, this function has an execution complexity which is quadratic in the
number of existing threads.
Since 2.10
113
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
GMutex
The GMutex struct is an opaque data structure to represent a mutex (mutual exclusion). It can be
used to protect data against shared access. Take for example the following function:
It is easy to see that this won’t work in a multi-threaded application. There current_number must be
protected against shared access. A first naive implementation would be:
This looks like it would work, but there is a race condition while constructing the mutex and this
code cannot work reliable. Please do not use such constructs in your own programs! One working
solution is:
114
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
N OTE
All of the g_mutex_* functions are actually macros. Apart from taking their addresses,
you can however use them as if they were functions.
g_mutex_new ()
N OTE
This function will abort if g_thread_init() has not been called yet.
g_mutex_lock ()
Locks mutex . If mutex is already locked by another thread, the current thread will block until mutex
is unlocked by the other thread.
This function can be used even if g_thread_init() has not yet been called, and, in that case, will do
nothing.
N OTE
mutex : a GMutex.
g_mutex_trylock ()
Tries to lock mutex . If mutex is already locked by another thread, it immediately returns FALSE.
Otherwise it locks mutex and returns TRUE.
This function can be used even if g_thread_init() has not yet been called, and, in that case, will
immediately return TRUE.
115
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
N OTE
GMutex is neither guaranteed to be recursive nor to be non-recursive, i.e. the return value
of g_mutex_trylock() could be both FALSE or TRUE, if the current thread already has
locked mutex . Use GStaticRecMutex, if you need recursive mutexes.
mutex : a GMutex.
g_mutex_unlock ()
Unlocks mutex . If another thread is blocked in a g_mutex_lock() call for mutex , it will be woken and
can lock mutex itself.
This function can be used even if g_thread_init() has not yet been called, and, in that case, will do
nothing.
mutex : a GMutex.
g_mutex_free ()
Destroys mutex .
mutex : a GMutex.
GStaticMutex
A GStaticMutex works like a GMutex, but it has one significant advantage. It doesn’t need to be
created at run-time like a GMutex, but can be defined at compile-time. Here is a shorter, easier and safer
version of our give_me_next_number() example:
Sometimes you would like to dynamically create a mutex. If you don’t want to require prior calling
to g_thread_init(), because your code should also be usable in non-threaded programs, you are not able
to use g_mutex_new() and thus GMutex, as that requires a prior call to g_thread_init(). In theses cases
you can also use a GStaticMutex. It must be initialized with g_static_mutex_init() before using it and
freed with with g_static_mutex_free() when not needed anymore to free up any allocated resources.
116
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
Even though GStaticMutex is not opaque, it should only be used with the following functions, as it
is defined differently on different platforms.
All of the g_static_mutex_* functions apart from g_static_mutex_get_mutex can also be
used even if g_thread_init() has not yet been called. Then they do nothing, apart from g_static_mu-
tex_trylock, which does nothing but returning TRUE.
N OTE
All of the g_static_mutex_* functions are actually macros. Apart from taking their
addresses, you can however use them as if they were functions.
G_STATIC_MUTEX_INIT
#define G_STATIC_MUTEX_INIT
A GStaticMutex must be initialized with this macro, before it can be used. This macro can used be to
initialize a variable, but it cannot be assigned to a variable. In that case you have to use g_static_mutex_init().
GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
g_static_mutex_init ()
g_static_mutex_lock ()
mutex : a GStaticMutex.
g_static_mutex_trylock ()
mutex : a GStaticMutex.
g_static_mutex_unlock ()
mutex : a GStaticMutex.
117
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
g_static_mutex_get_mutex ()
For some operations (like g_cond_wait()) you must have a GMutex instead of a GStaticMutex. This
function will return the corresponding GMutex for mutex .
mutex : a GStaticMutex.
g_static_mutex_free ()
G_LOCK_DEFINE()
#define G_LOCK_DEFINE(name)
The G_LOCK_* macros provide a convenient interface to GStaticMutex with the advantage that they
will expand to nothing in programs compiled against a thread-disabled GLib, saving code and memory
there. G_LOCK_DEFINE defines a lock. It can appear anywhere variable definitions may appear in
programs, i.e. in the first block of a function or outside of functions. The name parameter will be mangled
to get the name of the GStaticMutex. This means that you can use names of existing variables as the
parameter - e.g. the name of the variable you intent to protect with the lock. Look at our give_me_n-
ext_number() example using the G_LOCK_* macros:
G_LOCK_DEFINE_STATIC()
#define G_LOCK_DEFINE_STATIC(name)
118
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
G_LOCK_EXTERN()
#define G_LOCK_EXTERN(name)
G_LOCK()
#define G_LOCK(name)
G_TRYLOCK()
#define G_TRYLOCK(name)
G_UNLOCK()
#define G_UNLOCK(name)
GStaticRecMutex
typedef struct {
} GStaticRecMutex;
A GStaticRecMutex works like a GStaticMutex, but it can be locked multiple times by one thread.
If you enter it n times, you have to unlock it n times again to let other threads lock it. An exception is
the function g_static_rec_mutex_unlock_full(): that allows you to unlock a GStaticRecMutex completely
returning the depth, (i.e. the number of times this mutex was locked). The depth can later be used to
restore the state of the GStaticRecMutex by calling g_static_rec_mutex_lock_full().
Even though GStaticRecMutex is not opaque, it should only be used with the following functions.
All of the g_static_rec_mutex_* functions can be used even if g_thread_init() has not been
called. Then they do nothing, apart from g_static_rec_mutex_trylock, which does nothing but
returning TRUE.
G_STATIC_REC_MUTEX_INIT
A GStaticRecMutex must be initialized with this macro before it can be used. This macro can
used be to initialize a variable, but it cannot be assigned to a variable. In that case you have to use
g_static_rec_mutex_init().
GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
119
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
g_static_rec_mutex_init ()
A GStaticRecMutex must be initialized with this function before it can be used. Alternatively you
can initialize it with G_STATIC_REC_MUTEX_INIT.
mutex : a GStaticRecMutex to be initialized.
g_static_rec_mutex_lock ()
Locks mutex . If mutex is already locked by another thread, the current thread will block until mutex
is unlocked by the other thread. If mutex is already locked by the calling thread, this functions increases
the depth of mutex and returns immediately.
mutex : a GStaticRecMutex to lock.
g_static_rec_mutex_trylock ()
Tries to lock mutex . If mutex is already locked by another thread, it immediately returns FALSE. Oth-
erwise it locks mutex and returns TRUE. If mutex is already locked by the calling thread, this functions
increases the depth of mutex and immediately returns TRUE.
mutex : a GStaticRecMutex to lock.
g_static_rec_mutex_unlock ()
Unlocks mutex . Another thread will be allowed to lock mutex only when it has been unlocked as
many times as it had been locked before. If mutex is completely unlocked and another thread is blocked
in a g_static_rec_mutex_lock() call for mutex , it will be woken and can lock mutex itself.
mutex : a GStaticRecMutex to unlock.
g_static_rec_mutex_lock_full ()
g_static_rec_mutex_unlock_full ()
Completely unlocks mutex . If another thread is blocked in a g_static_rec_mutex_lock() call for mut-
ex , it will be woken and can lock mutex itself. This function returns the number of times that mutex has
been locked by the current thread. To restore the state before the call to g_static_rec_mutex_unlock_full()
you can call g_static_rec_mutex_lock_full() with the depth returned by this function.
mutex : a GStaticRecMutex to completely unlock.
Returns : number of times mutex has been locked by the current thread.
120
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
g_static_rec_mutex_free ()
GStaticRWLock
typedef struct {
} GStaticRWLock;
The GStaticRWLock struct represents a read-write lock. A read-write lock can be used for protecting
data that some portions of code only read from, while others also write. In such situations it is desirable
that several readers can read at once, whereas of course only one writer may write at a time. Take a look
at the following example:
This example shows an array which can be accessed by many readers (the my_array_get() func-
tion) simultaneously, whereas the writers (the my_array_set() function) will only be allowed once
at a time and only if no readers currently access the array. This is because of the potentially dangerous
resizing of the array. Using these functions is fully multi-thread safe now.
Most of the time, writers should have precedence over readers. That means, for this implementation,
that as soon as a writer wants to lock the data, no other reader is allowed to lock the data, whereas, of
course, the readers that already have locked the data are allowed to finish their operation. As soon as
the last reader unlocks the data, the writer will lock it.
Even though GStaticRWLock is not opaque, it should only be used with the following functions.
All of the g_static_rw_lock_* functions can be used even if g_thread_init() has not been called.
Then they do nothing, apart from g_static_rw_lock_*_trylock, which does nothing but returning
TRUE.
121
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
N OTE
A read-write lock has a higher overhead than a mutex. For example, both
g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock() have to lock and
unlock a GStaticMutex, so it takes at least twice the time to lock and unlock a GStaticR-
WLock that it does to lock and unlock a GStaticMutex. So only data structures that are
accessed by multiple readers, and which keep the lock for a considerable time justify a
GStaticRWLock. The above example most probably would fare better with a GStaticMu-
tex.
G_STATIC_RW_LOCK_INIT
A GStaticRWLock must be initialized with this macro before it can be used. This macro can used be to
initialize a variable, but it cannot be assigned to a variable. In that case you have to use g_static_rw_lock_init().
GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
g_static_rw_lock_init ()
A GStaticRWLock must be initialized with this function before it can be used. Alternatively you can
initialize it with G_STATIC_RW_LOCK_INIT.
g_static_rw_lock_reader_lock ()
Locks lock for reading. There may be unlimited concurrent locks for reading of a GStaticRWLock
at the same time. If lock is already locked for writing by another thread or if another thread is al-
ready waiting to lock lock for writing, this function will block until lock is unlocked by the other
writing thread and no other writing threads want to lock lock. This lock has to be unlocked by
g_static_rw_lock_reader_unlock().
GStaticRWLock is not recursive. It might seem to be possible to recursively lock for reading, but that
can result in a deadlock, due to writer preference.
g_static_rw_lock_reader_trylock ()
Tries to lock lock for reading. If lock is already locked for writing by another thread or if another
thread is already waiting to lock lock for writing, immediately returns FALSE. Otherwise locks lock
for reading and returns TRUE. This lock has to be unlocked by g_static_rw_lock_reader_unlock().
122
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
g_static_rw_lock_reader_unlock ()
Unlocks lock. If a thread waits to lock lock for writing and all locks for reading have been unlocked,
the waiting thread is woken up and can lock lock for writing.
g_static_rw_lock_writer_lock ()
Locks lock for writing. If lock is already locked for writing or reading by other threads, this func-
tion will block until lock is completely unlocked and then lock lock for writing. While this func-
tions waits to lock lock, no other thread can lock lock for reading. When lock is locked for writ-
ing, no other thread can lock lock (neither for reading nor writing). This lock has to be unlocked by
g_static_rw_lock_writer_unlock().
g_static_rw_lock_writer_trylock ()
Tries to lock lock for writing. If lock is already locked (for either reading or writing) by another
thread, it immediately returns FALSE. Otherwise it locks lock for writing and returns TRUE. This lock
has to be unlocked by g_static_rw_lock_writer_unlock().
g_static_rw_lock_writer_unlock ()
Unlocks lock. If a thread is waiting to lock lock for writing and all locks for reading have been
unlocked, the waiting thread is woken up and can lock lock for writing. If no thread is waiting to lock
lock for writing, and some thread or threads are waiting to lock lock for reading, the waiting threads
are woken up and can lock lock for reading.
g_static_rw_lock_free ()
123
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
GCond
The GCond struct is an opaque data structure that represents a condition. Threads can block on a
GCond if they find a certain condition to be false. If other threads change the state of this condition they
signal the GCond, and that causes the waiting threads to be woken up.
Whenever a thread calls pop_data() now, it will wait until current_data is non-NULL, i.e. until
some other thread has called push_data().
N OTE
It is important to use the g_cond_wait() and g_cond_timed_wait() functions only inside
a loop which checks for the condition to be true. It is not guaranteed that the waiting
thread will find the condition fulfilled after it wakes up, even if the signaling thread left the
condition in that state: another thread may have altered the condition before the waiting
thread got the chance to be woken up, even if the condition itself is protected by a GMutex,
like above.
N OTE
All of the g_cond_* functions are actually macros. Apart from taking their addresses,
you can however use them as if they were functions.
g_cond_new ()
124
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
Creates a new GCond. This function will abort, if g_thread_init() has not been called yet.
g_cond_signal ()
If threads are waiting for cond , exactly one of them is woken up. It is good practice to hold the same
lock as the waiting thread while calling this function, though not required.
This function can be used even if g_thread_init() has not yet been called, and, in that case, will do
nothing.
cond : a GCond.
g_cond_broadcast ()
If threads are waiting for cond , all of them are woken up. It is good practice to lock the same mutex
as the waiting threads, while calling this function, though not required.
This function can be used even if g_thread_init() has not yet been called, and, in that case, will do
nothing.
cond : a GCond.
g_cond_wait ()
Waits until this thread is woken up on cond . The mutex is unlocked before falling asleep and locked
again before resuming.
This function can be used even if g_thread_init() has not yet been called, and, in that case, will
immediately return.
cond : a GCond.
g_cond_timed_wait ()
Waits until this thread is woken up on cond , but not longer than until the time specified by abs_time.
The mutex is unlocked before falling asleep and locked again before resuming.
If abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
This function can be used even if g_thread_init() has not yet been called, and, in that case, will
immediately return TRUE.
To easily calculate abs_time a combination of g_get_current_time() and g_time_val_add() can be
used.
cond : a GCond.
125
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
g_cond_free ()
cond : a GCond.
GPrivate
The GPrivate struct is an opaque data structure to represent a thread private data key. Threads can
thereby obtain and set a pointer which is private to the current thread. Take our give_me_next_n-
umber() example from above. Suppose we don’t want current_number to be shared between the
threads, but instead to be private to each thread. This can be done as follows:
Here the pointer belonging to the key current_number_key is read. If it is NULL, it has not been
set yet. Then get memory for an integer value, assign this memory to the pointer and write the pointer
back. Now we have an integer value that is private to the current thread.
The GPrivate struct should only be accessed via the following functions.
N OTE
All of the g_private_* functions are actually macros. Apart from taking their ad-
dresses, you can however use them as if they were functions.
g_private_new ()
126
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
N OTE
N OTE
A GPrivate can not be freed. Reuse it instead, if you can, to avoid shortage, or use
GStaticPrivate.
N OTE
This function will abort if g_thread_init() has not been called yet.
destructor : a function to destroy the data keyed to GPrivate when a thread ends.
g_private_get ()
Returns the pointer keyed to private_key for the current thread. If g_private_set() hasn’t been
called for the current private_key and thread yet, this pointer will be NULL.
This function can be used even if g_thread_init() has not yet been called, and, in that case, will return
the value of private_key casted to gpointer. Note however, that private data set before g_thread_init()
will not be retained after the call. Instead, NULL will be returned in all threads directly after g_thread_init(),
regardless of any g_private_set() calls issued before threading system intialization.
private_key : a GPrivate.
g_private_set ()
private_key : a GPrivate.
GStaticPrivate
typedef struct {
} GStaticPrivate;
127
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
A GStaticPrivate works almost like a GPrivate, but it has one significant advantage. It doesn’t need
to be created at run-time like a GPrivate, but can be defined at compile-time. This is similar to the
difference between GMutex and GStaticMutex. Now look at our give_me_next_number() example
with ""
G_STATIC_PRIVATE_INIT
#define G_STATIC_PRIVATE_INIT
Every GStaticPrivate must be initialized with this macro, before it can be used.
GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
g_static_private_init ()
g_static_private_get ()
private_key : a GStaticPrivate.
g_static_private_set ()
128
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
Sets the pointer keyed to private_key for the current thread and the function notify to be called
with that pointer (NULL or non-NULL), whenever the pointer is set again or whenever the current
thread ends.
This function works even if g_thread_init() has not yet been called. If g_thread_init() is called later,
the data keyed to private_key will be inherited only by the main thread, i.e. the one that called
g_thread_init().
N OTE
private_key : a GStaticPrivate.
notify : a function to be called with the pointer whenever the current thread ends or sets this pointer
again.
g_static_private_free ()
GOnce
typedef struct {
volatile GOnceStatus status;
volatile gpointer retval;
} GOnce;
A GOnce struct controls a one-time initialization function. Any one-time initialization function must
have its own unique GOnce struct.
volatile GOnceStatus status; the status of the GOnce
volatile gpointer retval; the value returned by the call to the function, if status is G_ONCE_STATUS_READY
Since 2.4
enum GOnceStatus
typedef enum
{
G_ONCE_STATUS_NOTCALLED,
G_ONCE_STATUS_PROGRESS,
G_ONCE_STATUS_READY
} GOnceStatus;
129
CHAPTER 3. GLIB CORE APPLICATION . . . 3.2. THREADS
Since 2.4
G_ONCE_INIT
Since 2.4
g_once()
The first call to this routine by a process with a given GOnce struct calls func with the given argu-
ment. Thereafter, subsequent calls to g_once() with the same GOnce struct do not call func again, but re-
turn the stored result of the first call. On return from g_once(), the status of once will be G_ONCE_STATUS_READY.
For example, a mutex or a thread-specific data key must be created exactly once. In a threaded
environment, calling g_once() ensures that the initialization is serialized across multiple threads.
N OTE
Calling g_once() recursively on the same GOnce struct in func will lead to a deadlock.
gpointer
get_debug_flags ()
{
static GOnce my_once = G_ONCE_INIT;
g_once (&my_once, parse_debug_flags, NULL);
return my_once.retval;
}
func : the GThreadFunc function associated to once. This function is called only once, regardless of the
number of times it and its associated GOnce struct are passed to g_once() .
Since 2.4
g_once_init_enter ()
Function to be called when starting a critical initialization section. The argument value_locatio-
n must point to a static 0-initialized variable that will be set to a value other than 0 at the end of the
initialization section. In combination with g_once_init_leave() and the unique address value_locatio-
n, it can be ensured that an initialization section will be executed only once during a program’s life time,
and that concurrent threads are blocked until initialization completed. To be used in constructs like this:
130
CHAPTER 3. GLIB CORE APPLICATION . . . 3.3. THREAD POOLS
Returns : %TRUE if the initialization section should be entered, FALSE and blocks otherwise
Since 2.14
g_once_init_leave ()
Since 2.14
See Also
Synopsis
#include <glib.h>
GThreadPool;
GThreadPool* g_thread_pool_new (GFunc func,
gpointer user_data,
gint max_threads,
gboolean exclusive,
GError **error);
void g_thread_pool_push (GThreadPool *pool,
gpointer data,
GError **error);
void g_thread_pool_set_max_threads (GThreadPool *pool,
131
CHAPTER 3. GLIB CORE APPLICATION . . . 3.3. THREAD POOLS
gint max_threads,
GError **error);
gint g_thread_pool_get_max_threads (GThreadPool *pool);
guint g_thread_pool_get_num_threads (GThreadPool *pool);
guint g_thread_pool_unprocessed (GThreadPool *pool);
void g_thread_pool_free (GThreadPool *pool,
gboolean immediate,
gboolean wait_);
void g_thread_pool_set_max_unused_threads
(gint max_threads);
gint g_thread_pool_get_max_unused_threads
(void);
guint g_thread_pool_get_num_unused_threads
(void);
void g_thread_pool_stop_unused_threads (void);
void g_thread_pool_set_sort_function (GThreadPool *pool,
GCompareDataFunc func,
gpointer user_data);
void g_thread_pool_set_max_idle_time (guint interval);
guint g_thread_pool_get_max_idle_time (void);
Description
Sometimes you wish to asynchronously fork out the execution of work and continue working in your
own thread. If that will happen often, the overhead of starting and destroying a thread each time might
be too high. In such cases reusing already started threads seems like a good idea. And it indeed is, but
implementing this can be tedious and error-prone.
Therefore GLib provides thread pools for your convenience. An added advantage is, that the threads
can be shared between the different subsystems of your program, when they are using GLib.
To create a new thread pool, you use g_thread_pool_new(). It is destroyed by g_thread_pool_free().
If you want to execute a certain task within a thread pool, you call g_thread_pool_push().
To get the current number of running threads you call g_thread_pool_get_num_threads(). To get the
number of still unprocessed tasks you call g_thread_pool_unprocessed(). To control the maximal num-
ber of threads for a thread pool, you use g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads().
Finally you can control the number of unused threads, that are kept alive by GLib for future use. The
current number can be fetched with g_thread_pool_get_num_unused_threads(). The maximal number
can be controlled by g_thread_pool_get_max_unused_threads() and g_thread_pool_set_max_unused_threads().
All currently unused threads can be stopped by calling g_thread_pool_stop_unused_threads().
Details
GThreadPool
typedef struct {
GFunc func;
gpointer user_data;
gboolean exclusive;
} GThreadPool;
The GThreadPool struct represents a thread pool. It has three public read-only members, but the
underlying struct is bigger, so you must not copy this struct.
gpointer user_data; the user data for the threads of this pool
132
CHAPTER 3. GLIB CORE APPLICATION . . . 3.3. THREAD POOLS
g_thread_pool_new ()
user_data : user data that is handed over to func every time it is called
max_threads : the maximal number of threads to execute concurrently in the new thread pool, -1 means
no limit
g_thread_pool_push ()
Inserts data into the list of tasks to be executed by pool. When the number of currently running
threads is lower than the maximal allowed number of threads, a new thread is started (or reused) with
the properties given to g_thread_pool_new(). Otherwise data stays in the queue until a thread in this
pool finishes its previous task and processes data.
error can be NULL to ignore errors, or non-NULL to report errors. An error can only occur when a
new thread couldn’t be created. In that case data is simply appended to the queue of work to do.
pool : a GThreadPool
g_thread_pool_set_max_threads ()
133
CHAPTER 3. GLIB CORE APPLICATION . . . 3.3. THREAD POOLS
Sets the maximal allowed number of threads for pool. A value of -1 means, that the maximal number
of threads is unlimited.
Setting max_threads to 0 means stopping all work for pool. It is effectively frozen until max_thre-
ads is set to a non-zero value again.
A thread is never terminated while calling func, as supplied by g_thread_pool_new(). Instead the
maximal number of threads only has effect for the allocation of new threads in g_thread_pool_push().
A new thread is allocated, whenever the number of currently running threads in pool is smaller than
the maximal number.
error can be NULL to ignore errors, or non-NULL to report errors. An error can only occur when a
new thread couldn’t be created.
pool : a GThreadPool
g_thread_pool_get_max_threads ()
g_thread_pool_get_num_threads ()
g_thread_pool_unprocessed ()
g_thread_pool_free ()
134
CHAPTER 3. GLIB CORE APPLICATION . . . 3.3. THREAD POOLS
g_thread_pool_set_max_unused_threads ()
void g_thread_pool_set_max_unused_threads
(gint max_threads);
Sets the maximal number of unused threads to max_threads. If max_threads is -1, no limit is im-
posed on the number of unused threads.
g_thread_pool_get_max_unused_threads ()
gint g_thread_pool_get_max_unused_threads
(void);
g_thread_pool_get_num_unused_threads ()
guint g_thread_pool_get_num_unused_threads
(void);
g_thread_pool_stop_unused_threads ()
Stops all currently unused threads. This does not change the maximal number of unused threads.
This function can be used to regularly stop all unused threads e.g. from g_timeout_add().
g_thread_pool_set_sort_function ()
Sets the function used to sort the list of tasks. This allows the tasks to be processed by a priority
determined by func, and not just in the order in which they were added to the pool.
Note, if the maximum number of threads is more than 1, the order that threads are executed can
not be guranteed 100%. Threads are scheduled by the operating system and are executed at random. It
cannot be assumed that threads are executed in the order they are created.
pool : a GThreadPool
func : the GCompareDataFunc used to sort the list of tasks. This function is passed two tasks. It should
return 0 if the order in which they are handled does not matter, a negative value if the first task
should be processed before the second or a positive value if the second task should be processed
first.
Since 2.10
135
CHAPTER 3. GLIB CORE APPLICATION . . . 3.4. ASYNCHRONOUS QUEUES
g_thread_pool_set_max_idle_time ()
This function will set the maximum interval that a thread waiting in the pool for new tasks can be
idle for before being stopped. This function is similar to calling g_thread_pool_stop_unused_threads()
on a regular timeout, except, this is done on a per thread basis.
By setting interval to 0, idle threads will not be stopped. This function makes use of g_async_queue_timed_pop()
using interval.
Since 2.10
g_thread_pool_get_max_idle_time ()
This function will return the maximum interval that a thread will wait in the thread pool for new
tasks before being stopped.
If this function returns 0, threads waiting in the thread pool for new work are not stopped.
Returns : the maximum interval to wait for new tasks in the thread pool before stopping the thread
(1/1000ths of a second).
Since 2.10
See Also
Synopsis
#include <glib.h>
GAsyncQueue;
GAsyncQueue* g_async_queue_new (void);
GAsyncQueue* g_async_queue_new_full (GDestroyNotify item_free_func);
GAsyncQueue* g_async_queue_ref (GAsyncQueue *queue);
void g_async_queue_unref (GAsyncQueue *queue);
void g_async_queue_push (GAsyncQueue *queue,
gpointer data);
void g_async_queue_push_sorted (GAsyncQueue *queue,
gpointer data,
GCompareDataFunc func,
gpointer user_data);
gpointer g_async_queue_pop (GAsyncQueue *queue);
gpointer g_async_queue_try_pop (GAsyncQueue *queue);
gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
GTimeVal *end_time);
gint g_async_queue_length (GAsyncQueue *queue);
void g_async_queue_sort (GAsyncQueue *queue,
136
CHAPTER 3. GLIB CORE APPLICATION . . . 3.4. ASYNCHRONOUS QUEUES
GCompareDataFunc func,
gpointer user_data);
Description
Often you need to communicate between different threads. In general it’s safer not to do this by shared
memory, but by explicit message passing. These messages only make sense asynchronously for multi-
threaded applications though, as a synchronous operation could as well be done in the same thread.
Asynchronous queues are an exception from most other GLib data structures, as they can be used
simultaneously from multiple threads without explicit locking and they bring their own builtin reference
counting. This is because the nature of an asynchronous queue is that it will always be used by at least
2 concurrent threads.
For using an asynchronous queue you first have to create one with g_async_queue_new(). A newly-
created queue will get the reference count 1. Whenever another thread is creating a new reference of
(that is, pointer to) the queue, it has to increase the reference count (using g_async_queue_ref()). Also,
before removing this reference, the reference count has to be decreased (using g_async_queue_unref()).
After that the queue might no longer exist so you must not access it after that point.
A thread, which wants to send a message to that queue simply calls g_async_queue_push() to push
the message to the queue.
A thread, which is expecting messages from an asynchronous queue simply calls g_async_queue_pop()
for that queue. If no message is available in the queue at that point, the thread is now put to sleep
until a message arrives. The message will be removed from the queue and returned. The functions
g_async_queue_try_pop() and g_async_queue_timed_pop() can be used to only check for the presence
of messages or to only wait a certain time for messages respectively.
For almost every function there exist two variants, one that locks the queue and one that doesn’t.
That way you can hold the queue lock (acquire it with g_async_queue_lock() and release it with g_async_queue_unloc
over multiple queue accessing instructions. This can be necessary to ensure the integrity of the queue,
but should only be used when really necessary, as it can make your life harder if used unwisely. Nor-
mally you should only use the locking function variants (those without the suffix _unlocked)
Details
GAsyncQueue
The GAsyncQueue struct is an opaque data structure, which represents an asynchronous queue. It
should only be accessed through the g_async_queue_* functions.
137
CHAPTER 3. GLIB CORE APPLICATION . . . 3.4. ASYNCHRONOUS QUEUES
g_async_queue_new ()
g_async_queue_new_full ()
Creates a new asynchronous queue with an initial reference count of 1 and sets up a destroy notify
function that is used to free any remaining queue items when the queue is destroyed after the final unref.
Since 2.16
g_async_queue_ref ()
Increases the reference count of the asynchronous queue by 1. You do not need to hold the lock to
call this function.
queue : a GAsyncQueue.
g_async_queue_unref ()
Decreases the reference count of the asynchronous queue by 1. If the reference count went to 0, the
queue will be destroyed and the memory allocated will be freed. So you are not allowed to use the
queue afterwards, as it might have disappeared. You do not need to hold the lock to call this function.
queue : a GAsyncQueue.
g_async_queue_push ()
Pushes the data into the queue. data must not be NULL.
queue : a GAsyncQueue.
138
CHAPTER 3. GLIB CORE APPLICATION . . . 3.4. ASYNCHRONOUS QUEUES
g_async_queue_push_sorted ()
Inserts data into queue using func to determine the new position.
This function requires that the queue is sorted before pushing on new elements.
This function will lock queue before it sorts the queue and unlock it when it is finished.
For an example of func see g_async_queue_sort().
queue : a GAsyncQueue
func : the GCompareDataFunc is used to sort queue. This function is passed two elements of the que-
ue. The function should return 0 if they are equal, a negative value if the first element should be
higher in the queue or a positive value if the first element should be lower in the queue than the
second element.
Since 2.10
g_async_queue_pop ()
Pops data from the queue. This function blocks until data become available.
queue : a GAsyncQueue.
g_async_queue_try_pop ()
Tries to pop data from the queue. If no data is available, NULL is returned.
queue : a GAsyncQueue.
Returns : data from the queue or NULL, when no data is available immediately.
g_async_queue_timed_pop ()
Pops data from the queue. If no data is received before end_time, NULL is returned.
To easily calculate end_time a combination of g_get_current_time() and g_time_val_add() can be
used.
queue : a GAsyncQueue.
Returns : data from the queue or NULL, when no data is received before end_time.
139
CHAPTER 3. GLIB CORE APPLICATION . . . 3.4. ASYNCHRONOUS QUEUES
g_async_queue_length ()
Returns the length of the queue, negative values mean waiting threads, positive values mean avail-
able entries in the queue. Actually this function returns the number of data items in the queue minus the
number of waiting threads. Thus a return value of 0 could mean ’n’ entries in the queue and ’n’ thread
waiting. That can happen due to locking of the queue or due to scheduling.
queue : a GAsyncQueue.
g_async_queue_sort ()
queue : a GAsyncQueue
func : the GCompareDataFunc is used to sort queue. This function is passed two elements of the que-
ue. The function should return 0 if they are equal, a negative value if the first element should be
higher in the queue or a positive value if the first element should be lower in the queue than the
second element.
Since 2.10
g_async_queue_lock ()
Acquires the queue’s lock. After that you can only call the g_async_queue_*_unlocked() func-
tion variants on that queue. Otherwise it will deadlock.
queue : a GAsyncQueue.
g_async_queue_unlock ()
queue : a GAsyncQueue.
140
CHAPTER 3. GLIB CORE APPLICATION . . . 3.4. ASYNCHRONOUS QUEUES
g_async_queue_ref_unlocked ()
WARNING
queue : a GAsyncQueue.
g_async_queue_unref_and_unlock ()
WARNING
Decreases the reference count of the asynchronous queue by 1 and releases the lock. This function
must be called while holding the queue’s lock. If the reference count went to 0, the queue will be
destroyed and the memory allocated will be freed.
Deprecated : Since 2.8, reference counting is done atomically so g_async_queue_unref() can be used
regardless of the queue’s lock.
queue : a GAsyncQueue.
g_async_queue_push_unlocked ()
Pushes the data into the queue. data must not be NULL. This function must be called while holding
the queue’s lock.
queue : a GAsyncQueue.
g_async_queue_push_sorted_unlocked ()
141
CHAPTER 3. GLIB CORE APPLICATION . . . 3.4. ASYNCHRONOUS QUEUES
Inserts data into queue using func to determine the new position.
This function requires that the queue is sorted before pushing on new elements.
This function is called while holding the queue’s lock.
For an example of func see g_async_queue_sort().
queue : a GAsyncQueue
func : the GCompareDataFunc is used to sort queue. This function is passed two elements of the que-
ue. The function should return 0 if they are equal, a negative value if the first element should be
higher in the queue or a positive value if the first element should be lower in the queue than the
second element.
Since 2.10
g_async_queue_pop_unlocked ()
Pops data from the queue. This function blocks until data become available. This function must be
called while holding the queue’s lock.
queue : a GAsyncQueue.
g_async_queue_try_pop_unlocked ()
Tries to pop data from the queue. If no data is available, NULL is returned. This function must be
called while holding the queue’s lock.
queue : a GAsyncQueue.
Returns : data from the queue or NULL, when no data is available immediately.
g_async_queue_timed_pop_unlocked ()
Pops data from the queue. If no data is received before end_time, NULL is returned. This function
must be called while holding the queue’s lock.
To easily calculate end_time a combination of g_get_current_time() and g_time_val_add() can be
used.
queue : a GAsyncQueue.
Returns : data from the queue or NULL, when no data is received before end_time.
142
CHAPTER 3. GLIB CORE APPLICATION . . . 3.5. DYNAMIC LOADING OF MODULES
g_async_queue_length_unlocked ()
Returns the length of the queue, negative values mean waiting threads, positive values mean avail-
able entries in the queue. Actually this function returns the number of data items in the queue minus
the number of waiting threads. Thus a return value of 0 could mean ’n’ entries in the queue and ’n’
thread waiting. That can happen due to locking of the queue or due to scheduling. This function must
be called while holding the queue’s lock.
queue : a GAsyncQueue.
g_async_queue_sort_unlocked ()
func : the GCompareDataFunc is used to sort queue. This function is passed two elements of the que-
ue. The function should return 0 if they are equal, a negative value if the first element should be
higher in the queue or a positive value if the first element should be lower in the queue than the
second element.
user_data : user data passed to func
Since 2.10
Synopsis
#include <gmodule.h>
GModule;
gboolean g_module_supported (void);
gchar* g_module_build_path (const gchar *directory,
const gchar *module_name);
GModule* g_module_open (const gchar *file_name,
GModuleFlags flags);
enum GModuleFlags;
gboolean g_module_symbol (GModule *module,
const gchar *symbol_name,
gpointer *symbol);
const gchar* g_module_name (GModule *module);
void g_module_make_resident (GModule *module);
gboolean g_module_close (GModule *module);
const gchar* g_module_error (void);
143
CHAPTER 3. GLIB CORE APPLICATION . . . 3.5. DYNAMIC LOADING OF MODULES
Description
These functions provide a portable way to dynamically load object files (commonly known as ’plug-
ins’). The current implementation supports all systems that provide an implementation of dlopen() (e.g.
Linux/Sun), as well as HP-UX via its shl_load() mechanism, and Windows platforms via DLLs.
A program which wants to use these functions must be linked to the libraries output by the command
pkg-config --libs gmodule-2.0.
To use them you must first determine whether dynamic loading is supported on the platform by
calling g_module_supported(). If it is, you can open a module with g_module_open(), find the module’s
symbols (e.g. function names) with g_module_symbol(), and later close the module with g_module_close().
g_module_name() will return the file name of a currently opened module.
If any of the above functions fail, the error status can be found with g_module_error().
The GModule implementation features reference counting for opened modules, and supports hook
functions within a module which are called when the module is loaded and unloaded (see GMod-
uleCheckInit and GModuleUnload).
If your module introduces static data to common subsystems in the running program, e.g. through
calling g_quark_from_static_string ("my-module-stuff"), it must ensure that it is never un-
loaded, by calling g_module_make_resident().
144
CHAPTER 3. GLIB CORE APPLICATION . . . 3.5. DYNAMIC LOADING OF MODULES
Details
GModule
g_module_supported ()
g_module_build_path ()
A portable way to build the filename of a module. The platform-specific prefix and suffix are added
to the filename, if needed, and the result is added to the directory, using the correct separator character.
The directory should specify the directory where the module can be found. It can be NULL or an
empty string to indicate that the module is in a standard platform-specific directory, though this is not
recommended since the wrong module may be found.
For example, calling g_module_build_path() on a Linux system with a directory of /lib and
a module_name of "mylibrary" will return /lib/libmylibrary.so. On a Windows system, using
\Windows as the directory it will return \Windows\mylibrary.dll.
directory : the directory where the module is. This can be NULL or the empty string to indicate that
the standard platform-specific directories will be used, though that is not recommended.
Returns : the complete path of the module, including the standard library prefix and suffix. This should
be freed when no longer needed.
g_module_open ()
Opens a module. If the module has already been opened, its reference count is incremented.
First of all g_module_open() tries to open file_name as a module. If that fails and file_name has the
".la"-suffix (and is a libtool archive) it tries to open the corresponding module. If that fails and it doesn’t
have the proper module suffix for the platform (G_MODULE_SUFFIX), this suffix will be appended and
the corresponding module will be opended. If that fails and file_name doesn’t have the ".la"-suffix, this
suffix is appended and g_module_open() tries to open the corresponding module. If eventually that
fails as well, NULL is returned.
file_name : the name of the file containing the module, or NULL to obtain a GModule representing the
main program itself.
flags : the flags used for opening the module. This can be the logical OR of any of the GModuleFlags.
145
CHAPTER 3. GLIB CORE APPLICATION . . . 3.5. DYNAMIC LOADING OF MODULES
enum GModuleFlags
typedef enum
{
G_MODULE_BIND_LAZY = 1 << 0,
G_MODULE_BIND_LOCAL = 1 << 1,
G_MODULE_BIND_MASK = 0x03
} GModuleFlags;
Flags passed to g_module_open(). Note that these flags are not supported on all platforms.
G_MODULE_BIND_LAZY specifies that symbols are only resolved when needed. The default action is to
bind all symbols when the module is loaded.
G_MODULE_BIND_LOCAL specifies that symbols in the module should not be added to the global name
space. The default action on most platforms is to place symbols in the module in the global name
space, which may cause conflicts with existing symbols.
G_MODULE_BIND_MASK mask for all flags.
g_module_symbol ()
g_module_name ()
Returns : the filename of the module, or "main" if the module is the main program itself.
g_module_make_resident ()
Ensures that a module will never be unloaded. Any future g_module_close() calls on the module
will be ignored.
module : a GModule to make permanently resident.
g_module_close ()
Closes a module.
module : a GModule to close.
146
CHAPTER 3. GLIB CORE APPLICATION . . . 3.6. MEMORY ALLOCATION
g_module_error ()
GModuleCheckInit ()
Specifies the type of the module initialization function. If a module contains a function named
g_module_check_init() it is called automatically when the module is loaded. It is passed the GMod-
ule structure and should return NULL on success or a string describing the initialization error.
module : the GModule corresponding to the module which has just been loaded.
GModuleUnload ()
Specifies the type of the module function called when it is unloaded. If a module contains a function
named g_module_unload() it is called automatically when the module is unloaded. It is passed the
GModule structure.
G_MODULE_SUFFIX
Expands to the proper shared library suffix for the current platform without the leading dot. For the
most Unices and Linux this is "so", for some HP-UX versions this is "sl" and for Windows this is "dll".
G_MODULE_EXPORT
#define G_MODULE_EXPORT
Used to declare functions exported by modules. This is a no-op on Linux and Unices, but when
compiling for Windows, it marks a symbol to be exported from the library or executable being built.
G_MODULE_IMPORT
147
CHAPTER 3. GLIB CORE APPLICATION . . . 3.6. MEMORY ALLOCATION
Synopsis
#include <glib.h>
GMemVTable;
void g_mem_set_vtable (GMemVTable *vtable);
gboolean g_mem_is_system_malloc (void);
Description
These functions provide support for allocating and freeing memory.
N OTE
If any call to allocate memory fails, the application is terminated. This also means that
there is no need to check if the call succeeded.
N OTE
It’s important to match g_malloc() with g_free(), plain malloc() with free(), and (if you’re
using C++) new with delete and new[] with delete[]. Otherwise bad things can happen,
since these allocators may use different memory pools (and new/delete call constructors
and destructors). See also g_mem_set_vtable().
148
CHAPTER 3. GLIB CORE APPLICATION . . . 3.6. MEMORY ALLOCATION
Details
g_new()
Allocates n_structs elements of type struct_type. The returned pointer is cast to a pointer to the
given type. If n_structs is 0 it returns NULL.
Since the returned pointer is already casted to the right type, it is normally unnecessary to cast it
explicitly, and doing so might hide memory allocation errors.
g_new0()
Allocates n_structs elements of type struct_type, initialized to 0’s. The returned pointer is cast to
a pointer to the given type. If n_structs is 0 it returns NULL.
Since the returned pointer is already casted to the right type, it is normally unnecessary to cast it
explicitly, and doing so might hide memory allocation errors.
g_renew()
Reallocates the memory pointed to by mem, so that it now has space for n_structs elements of type
struct_type. It returns the new address of the memory, which may have been moved.
g_try_new()
Attempts to allocate n_structs elements of type struct_type, and returns NULL on failure. Con-
trast with g_new(), which aborts the program on failure. The returned pointer is cast to a pointer to the
given type. If n_structs is 0 it returns NULL.
Since 2.8
149
CHAPTER 3. GLIB CORE APPLICATION . . . 3.6. MEMORY ALLOCATION
g_try_new0()
Attempts to allocate n_structs elements of type struct_type, initialized to 0’s, and returns NULL
on failure. Contrast with g_new0(), which aborts the program on failure. The returned pointer is cast to
a pointer to the given type. The function returns NULL when n_structs is 0.
struct_type : the type of the elements to allocate
g_try_renew()
Attempts to reallocate the memory pointed to by mem, so that it now has space for n_structs ele-
ments of type struct_type, and returns NULL on failure. Contrast with g_renew(), which aborts the
program on failure. It returns the new address of the memory, which may have been moved.
struct_type : the type of the elements to allocate
g_malloc ()
g_malloc0 ()
g_realloc ()
Reallocates the memory pointed to by mem, so that it now has space for n_bytes bytes of memory.
It returns the new address of the memory, which may have been moved. mem may be NULL, in which
case it’s considered to have zero-length. n_bytes may be 0, in which case NULL will be returned and
mem will be freed unless it is NULL.
mem : the memory to reallocate
150
CHAPTER 3. GLIB CORE APPLICATION . . . 3.6. MEMORY ALLOCATION
g_try_malloc ()
Attempts to allocate n_bytes, and returns NULL on failure. Contrast with g_malloc(), which aborts
the program on failure.
g_try_malloc0 ()
Attempts to allocate n_bytes, initialized to 0’s, and returns NULL on failure. Contrast with g_malloc0(),
which aborts the program on failure.
Since 2.8
g_try_realloc ()
Attempts to realloc mem to a new size, n_bytes, and returns NULL on failure. Contrast with g_realloc(),
which aborts the program on failure. If mem is NULL, behaves the same as g_try_malloc().
g_free ()
g_mem_gc_friendly
This variable is TRUE if the G_DEBUG environment variable includes the key gc-friendly.
g_alloca()
#define g_alloca(size)
Allocates size bytes on the stack; these bytes will be freed when the current stack frame is cleaned
up. This macro essentially just wraps the alloca() function present on most UNIX variants. Thus it
provides the same advantages and pitfalls as alloca():
+ alloca() is very fast, as on most systems it’s implemented by just adjusting the stack pointer register.
151
CHAPTER 3. GLIB CORE APPLICATION . . . 3.6. MEMORY ALLOCATION
+ It doesn’t cause any memory fragmentation, within its scope, separate alloca() blocks just build up
and are released together at function end.
- Allocation sizes have to fit into the current stack frame. For instance in a threaded environment on
Linux, the per-thread stack size is limited to 2 Megabytes, so be sparse with alloca() uses.
- Allocation failure due to insufficient stack space is not indicated with a NULL return like e.g. with
malloc(). Instead, most systems probably handle it the same way as out of stack space situations
from infinite function recursion, i.e. with a segmentation fault.
- Special care has to be taken when mixing alloca() with GNU C variable sized arrays. Stack space
allocated with alloca() in the same scope as a variable sized array will be freed together with the
variable sized array upon exit of that scope, and not upon exit of the enclosing function scope.
g_newa()
g_memmove()
#define g_memmove(dest,src,len)
Copies a block of memory len bytes long, from src to dest. The source and destination areas may
overlap.
In order to use this function, you must include string.h yourself, because this macro will typically
simply resolve to memmove() and GLib does not include string.h for you.
g_memdup ()
Allocates byte_size bytes of memory, and copies byte_size bytes into it from mem. If mem is NULL
it returns NULL.
Returns : a pointer to the newly-allocated copy of the memory, or NULL if mem is NULL.
152
CHAPTER 3. GLIB CORE APPLICATION . . . 3.6. MEMORY ALLOCATION
GMemVTable
typedef struct {
gpointer (*malloc) (gsize n_bytes);
gpointer (*realloc) (gpointer mem,
gsize n_bytes);
void (*free) (gpointer mem);
/* optional; set to NULL if not used ! */
gpointer (*calloc) (gsize n_blocks,
gsize n_block_bytes);
gpointer (*try_malloc) (gsize n_bytes);
gpointer (*try_realloc) (gpointer mem,
gsize n_bytes);
} GMemVTable;
A set of functions used to perform memory allocation. The same GMemVTable must be used for all
allocations in the same program; a call to g_mem_set_vtable(), if it exists, should be prior to any use of
GLib.
try_malloc () function to use for allocating memory without a default error handler.
try_realloc () function to use for reallocating memory without a default error handler.
g_mem_set_vtable ()
Sets the GMemVTable to use for memory allocation. You can use this to provide custom memory
allocation routines. This function must be called before using any other GLib functions. The vtable only
needs to provide malloc(), realloc(), and free() functions; GLib can provide default implementations of
the others. The malloc() and realloc() implementations should return NULL on failure, GLib will handle
error-checking for you. vtable is copied, so need not persist after this function has been called.
g_mem_is_system_malloc ()
Checks whether the allocator used by g_malloc() is the system’s malloc implementation. If it re-
turns TRUE memory allocated with malloc() can be used interchangeable with memory allocated using
g_malloc(). This function is useful for avoiding an extra copy of allocated memory returned by a non-
GLib-based API.
A different allocator can be set using g_mem_set_vtable().
glib_mem_profiler_table
A GMemVTable containing profiling variants of the memory allocation functions. Use them together
with g_mem_profile() in order to get information about the memory allocation pattern of your program.
153
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_mem_profile ()
3.7 IO Channels
Name
IO Channels – portable support for using files, pipes and sockets
Synopsis
#include <glib.h>
GIOChannel;
154
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
gssize count,
gsize *bytes_written,
GError **error);
GIOStatus g_io_channel_write_unichar (GIOChannel *channel,
gunichar thechar,
GError **error);
GIOStatus g_io_channel_flush (GIOChannel *channel,
GError **error);
GIOStatus g_io_channel_seek_position (GIOChannel *channel,
gint64 offset,
GSeekType type,
GError **error);
enum GSeekType;
GIOStatus g_io_channel_shutdown (GIOChannel *channel,
gboolean flush,
GError **err);
enum GIOStatus;
enum GIOChannelError;
#define G_IO_CHANNEL_ERROR
GIOChannelError g_io_channel_error_from_errno (gint en);
GIOFuncs;
155
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
Description
The GIOChannel data type aims to provide a portable method for using file descriptors, pipes, and
sockets, and integrating them into the main event loop. Currently full support is available on UNIX
platforms, support for Windows is only partially complete.
To create a new GIOChannel on UNIX systems use g_io_channel_unix_new(). This works for plain
file descriptors, pipes and sockets. Alternatively, a channel can be created for a file in a system indepen-
dent manner using g_io_channel_new_file().
Once a GIOChannel has been created, it can be used in a generic manner with the functions g_io_channel_read_chars(),
g_io_channel_write_chars(), g_io_channel_seek_position(), and g_io_channel_shutdown().
To add a GIOChannel to the main event loop use g_io_add_watch() or g_io_add_watch_full(). Here
you specify which events you are interested in on the GIOChannel, and provide a function to be called
whenever these events occur.
GIOChannel instances are created with an initial reference count of 1. g_io_channel_ref() and g_io_channel_unref()
can be used to increment or decrement the reference count respectively. When the reference count
falls to 0, the GIOChannel is freed. (Though it isn’t closed automatically, unless it was created using
g_io_channel_new_from_file().) Using g_io_add_watch() or g_io_add_watch_full() increments a chan-
nel’s reference count.
The new functions g_io_channel_read_chars(), g_io_channel_read_line(), g_io_channel_read_line_string(),
g_io_channel_read_to_end(), g_io_channel_write_chars(), g_io_channel_seek_position(), and g_io_channel_flush()
should not be mixed with the deprecated functions g_io_channel_read(), g_io_channel_write(), and
g_io_channel_seek() on the same channel.
Details
GIOChannel
typedef struct {
} GIOChannel;
A data structure representing an IO Channel. The fields should be considered private and should
only be accessed with the following functions.
156
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_io_channel_unix_new ()
Creates a new GIOChannel given a file descriptor. On UNIX systems this works for plain files, pipes,
and sockets.
The returned GIOChannel has a reference count of 1.
The default encoding for GIOChannel is UTF-8. If your application is reading output from a com-
mand using via pipe, you may need to set the encoding to the encoding of the current locale (see
g_get_charset()) with the g_io_channel_set_encoding() function.
If you want to read raw binary data without interpretation, then call the g_io_channel_set_encoding()
function with NULL for the encoding argument.
This function is available in GLib on Windows, too, but you should avoid using it on Windows. The
domain of file descriptors and sockets overlap. There is no way for GLib to know which one you mean
in case the argument you pass to this function happens to be both a valid file descriptor and socket. If
that happens a warning is issued, and GLib assumes that it is the file descriptor you mean.
fd : a file descriptor.
g_io_channel_unix_get_fd ()
g_io_channel_win32_new_fd ()
Creates a new GIOChannel given a file descriptor on Windows. This works for file descriptors from
the C runtime.
This function works for file descriptors as returned by the open(), creat(), pipe() and fileno() calls in
the Microsoft C runtime. In order to meaningfully use this function your code should use the same C
runtime as GLib uses, which is msvcrt.dll. Note that in current Microsoft compilers it is near impossible
to convince it to build code that would use msvcrt.dll. The last Microsoft compiler version that sup-
ported using msvcrt.dll as the C runtime was version 6. The GNU compiler and toolchain for Windows,
also known as Mingw, fully supports msvcrt.dll.
If you have created a GIOChannel for a file descriptor and started watching (polling) it, you shouldn’t
call read() on the file descriptor. This is because adding polling for a file descriptor is implemented in
GLib on Windows by starting a thread that sits blocked in a read() from the file descriptor most of the
time. All reads from the file descriptor should be done by this internal GLib thread. Your code should
call only g_io_channel_read().
This function is available only in GLib on Windows.
g_io_channel_win32_new_socket ()
157
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_io_channel_win32_new_messages ()
g_io_channel_init ()
channel : a GIOChannel
g_io_channel_new_file ()
Open a file filename as a GIOChannel using mode mode. This channel will be closed when the last
reference to it is dropped, so there is no need to call g_io_channel_close() (though doing so will not cause
problems, as long as no attempt is made to access the channel after it is closed).
mode : One of "r", "w", "a", "r+", "w+", "a+". These have the same meaning as in fopen()
g_io_channel_read_chars ()
channel : a GIOChannel
158
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
count : the size of the buffer. Note that the buffer may not be complelely filled even if there is data in
the buffer if the remaining data is not a complete character.
bytes_read : The number of bytes read. This may be zero even on success if count < 6 and the channel’s
encoding is non-NULL. This indicates that the next UTF-8 character is too wide for the buffer.
error : a location to return an error of type GConvertError or GIOChannelError.
g_io_channel_read_unichar ()
Reads a Unicode character from channel. This function cannot be called on a channel with NULL
encoding.
channel : a GIOChannel
Returns : a GIOStatus
g_io_channel_read_line ()
Reads a line, including the terminating character(s), from a GIOChannel into a newly-allocated
string. str_return will contain allocated memory if the return is G_IO_STATUS_NORMAL.
channel : a GIOChannel
str_return : The line read from the GIOChannel, including the line terminator. This data should be
freed with g_free() when no longer needed. This is a nul-terminated string. If a length of zero is
returned, this will be NULL instead.
length : location to store length of the read data, or NULL
g_io_channel_read_line_string ()
buffer : a GString into which the line will be written. If buffer already contains data, the old data will
be overwritten.
159
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_io_channel_read_to_end ()
channel : a GIOChannel
str_return : Location to store a pointer to a string holding the remaining data in the GIOChannel. This
data should be freed with g_free() when no longer needed. This data is terminated by an extra nul
character, but there may be other nuls in the intervening data.
g_io_channel_write_chars ()
channel : a GIOChannel
count : the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.
bytes_written : The number of bytes written. This can be nonzero even if the return value is not
G_IO_STATUS_NORMAL. If the return value is G_IO_STATUS_NORMAL and the channel is
blocking, this will always be equal to count if count >= 0.
g_io_channel_write_unichar ()
Writes a Unicode character to channel. This function cannot be called on a channel with NULL
encoding.
channel : a GIOChannel
160
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
thechar : a character
Returns : a GIOStatus
g_io_channel_flush ()
channel : a GIOChannel
g_io_channel_seek_position ()
channel : a GIOChannel
type : a GSeekType. The type G_SEEK_CUR is only allowed in those cases where a call to g_io_channel_set_encoding
is allowed. See the documentation for g_io_channel_set_encoding() for details.
enum GSeekType
typedef enum
{
G_SEEK_CUR,
G_SEEK_SET,
G_SEEK_END
} GSeekType;
161
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_io_channel_shutdown ()
Close an IO channel. Any pending data to be written will be flushed if flush is TRUE. The channel
will not be freed until the last reference is dropped using g_io_channel_unref().
channel : a GIOChannel
enum GIOStatus
typedef enum
{
G_IO_STATUS_ERROR,
G_IO_STATUS_NORMAL,
G_IO_STATUS_EOF,
G_IO_STATUS_AGAIN
} GIOStatus;
G_IO_STATUS_NORMAL Success.
enum GIOChannelError
typedef enum
{
/* Derived from errno */
G_IO_CHANNEL_ERROR_FBIG,
G_IO_CHANNEL_ERROR_INVAL,
G_IO_CHANNEL_ERROR_IO,
G_IO_CHANNEL_ERROR_ISDIR,
G_IO_CHANNEL_ERROR_NOSPC,
G_IO_CHANNEL_ERROR_NXIO,
G_IO_CHANNEL_ERROR_OVERFLOW,
G_IO_CHANNEL_ERROR_PIPE,
/* Other */
G_IO_CHANNEL_ERROR_FAILED
} GIOChannelError;
G_IO_CHANNEL_ERROR_IO IO error.
162
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
G_IO_CHANNEL_ERROR
Error domain for GIOChannel operations. Errors in this domain will be from the GIOChannelError
enumeration. See GError for information on error domains.
g_io_channel_error_from_errno ()
g_io_channel_ref ()
channel : a GIOChannel
g_io_channel_unref ()
channel : a GIOChannel
g_io_create_watch ()
Creates a GSource that’s dispatched when condition is met for the given channel. For example, if
condition is G_IO_IN, the source will be dispatched when there’s data available for reading.
g_io_add_watch() is a simpler interface to this same functionality, for the case where you want to
add the source to the default main loop context at the default priority.
On Windows, polling a GSource created to watch a channel for a socket puts the socket in non-
blocking mode. This is a side-effect of the implementation and unavoidable.
163
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_io_add_watch ()
Adds the GIOChannel into the default main loop context with the default priority.
channel : a GIOChannel
g_io_add_watch_full ()
Adds the GIOChannel into the default main loop context with the given priority.
This internally creates a main loop source using g_io_create_watch() and attaches it to the main loop
context with g_source_attach(). You can do these steps manuallt if you need greater control.
channel : a GIOChannel
enum GIOCondition
typedef enum
{
G_IO_IN GLIB_SYSDEF_POLLIN,
G_IO_OUT GLIB_SYSDEF_POLLOUT,
G_IO_PRI GLIB_SYSDEF_POLLPRI,
G_IO_ERR GLIB_SYSDEF_POLLERR,
G_IO_HUP GLIB_SYSDEF_POLLHUP,
G_IO_NVAL GLIB_SYSDEF_POLLNVAL
} GIOCondition;
164
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
G_IO_HUP Hung up (the connection has been broken, usually for pipes and sockets).
GIOFunc ()
Returns : the function should return FALSE if the event source should be removed
GIOFuncs
typedef struct {
GIOStatus (*io_read) (GIOChannel *channel,
gchar * buf,
gsize count,
gsize *bytes_read,
GError **err);
GIOStatus (*io_write) (GIOChannel *channel,
const gchar *buf,
gsize count,
gsize *bytes_written,
GError **err);
GIOStatus (*io_seek) (GIOChannel *channel,
gint64 offset,
GSeekType type,
GError **err);
GIOStatus (*io_close) (GIOChannel *channel,
GError **err);
GSource* (*io_create_watch) (GIOChannel *channel,
GIOCondition condition);
void (*io_free) (GIOChannel *channel);
GIOStatus (*io_set_flags) (GIOChannel *channel,
GIOFlags flags,
GError **err);
GIOFlags (*io_get_flags) (GIOChannel *channel);
} GIOFuncs;
g_io_channel_get_buffer_size ()
channel : a GIOChannel
165
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_io_channel_set_buffer_size ()
size : the size of the buffer, or 0 to let GLib pick a good size
g_io_channel_get_buffer_condition ()
This function returns a GIOCondition depending on whether there is data to be read/space to write
data in the internal buffers in the GIOChannel. Only the flags G_IO_IN and G_IO_OUT may be set.
channel : A GIOChannel
Returns : A GIOCondition
g_io_channel_get_flags ()
Gets the current flags for a GIOChannel, including read-only flags such as G_IO_FLAG_IS_READABLE.
The values of the flags G_IO_FLAG_IS_READABLE and G_IO_FLAG_IS_WRITEABLE are cached
for internal use by the channel when it is created. If they should change at some later point (e.g.
partial shutdown of a socket with the UNIX shutdown() function), the user should immediately call
g_io_channel_get_flags() to update the internal values of these flags.
channel : a GIOChannel
g_io_channel_set_flags ()
enum GIOFlags
typedef enum
{
G_IO_FLAG_APPEND = 1 << 0,
G_IO_FLAG_NONBLOCK = 1 << 1,
G_IO_FLAG_IS_READABLE = 1 << 2, /* Read only flag */
G_IO_FLAG_IS_WRITEABLE = 1 << 3, /* Read only flag */
G_IO_FLAG_IS_SEEKABLE = 1 << 4, /* Read only flag */
G_IO_FLAG_MASK = (1 << 5) - 1,
G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK,
G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
} GIOFlags;
166
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
Specifies properties of a GIOChannel. Some of the flags can only be read with g_io_channel_get_flags(),
but not changed with g_io_channel_set_flags().
G_IO_FLAG_IS_READABLE indicates that the io channel is readable. This flag can not be changed.
G_IO_FLAG_IS_WRITEABLE indicates that the io channel is writable. This flag can not be changed.
G_IO_FLAG_MASK
G_IO_FLAG_GET_MASK
G_IO_FLAG_SET_MASK
g_io_channel_get_line_term ()
This returns the string that GIOChannel uses to determine where in the file a line break occurs. A
value of NULL indicates autodetection.
channel : a GIOChannel
Returns : The line termination string. This value is owned by GLib and must not be freed.
g_io_channel_set_line_term ()
This sets the string that GIOChannel uses to determine where in the file a line break occurs.
channel : a GIOChannel
line_term : The line termination string. Use NULL for autodetect. Autodetection breaks on "\n",
"\r\n", "\r", "\0", and the Unicode paragraph separator. Autodetection should not be used for
anything other than file-based channels.
length : The length of the termination string. If -1 is passed, the string is assumed to be nul-terminated.
This option allows termination strings with embedded nuls.
g_io_channel_get_buffered ()
channel : a GIOChannel
167
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
g_io_channel_set_buffered ()
The buffering state can only be set if the channel’s encoding is NULL. For any other encoding, the
channel must be buffered.
A buffered channel can only be set unbuffered if the channel’s internal buffers have been flushed.
Newly created channels or channels which have returned G_IO_STATUS_EOF not require such a flush.
For write-only channels, a call to g_io_channel_flush() is sufficient. For all other channels, the buffers
may be flushed by a call to g_io_channel_seek_position(). This includes the possibility of seeking with
seek type G_SEEK_CUR and an offset of zero. Note that this means that socket-based channels cannot
be set unbuffered once they have had data read from them.
On unbuffered channels, it is safe to mix read and write calls from the new and old APIs, if this is
necessary for maintaining old code.
The default state of the channel is buffered.
channel : a GIOChannel
g_io_channel_get_encoding ()
Gets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The
encoding NULL makes the channel safe for binary data.
channel : a GIOChannel
Returns : A string containing the encoding, this string is owned by GLib and must not be freed.
g_io_channel_set_encoding ()
Sets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The
default encoding for the external file is UTF-8.
The encoding NULL is safe to use with binary data.
The encoding can only be set if one of the following conditions is true:
• The channel was just created, and has not been written to or read from yet.
• The channel is write-only.
• The channel is a file, and the file pointer was just repositioned by a call to g_io_channel_seek_position().
(This flushes all the internal buffers.)
• The current encoding is NULL or UTF-8.
• One of the (new API) read functions has just returned G_IO_STATUS_EOF (or, in the case of
g_io_channel_read_to_end(), G_IO_STATUS_NORMAL).
• One of the functions g_io_channel_read_chars() or g_io_channel_read_unichar() has returned G_IO_STATUS_AGAIN
or G_IO_STATUS_ERROR. This may be useful in the case of G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
Returning one of these statuses from g_io_channel_read_line(), g_io_channel_read_line_string(),
or g_io_channel_read_to_end() does not guarantee that the encoding can be changed.
Channels which do not meet one of the above conditions cannot call g_io_channel_seek_position() with
an offset of G_SEEK_CUR, and, if they are "seekable", cannot call g_io_channel_write_chars() after call-
ing one of the API "read" functions.
168
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
channel : a GIOChannel
g_io_channel_get_close_on_unref ()
Returns whether the file/socket/whatever associated with channel will be closed when channe-
l receives its final unref and is destroyed. The default value of this is TRUE for channels created by
g_io_channel_new_file(), and FALSE for all other channels.
channel : a GIOChannel.
Returns : Whether the channel will be closed on the final unref of the GIOChannel data structure.
g_io_channel_set_close_on_unref ()
Setting this flag to TRUE for a channel you have already closed can cause problems.
channel : a GIOChannel
do_close : Whether to close the channel on the final unref of the GIOChannel data structure. The
default value of this is TRUE for channels created by g_io_channel_new_file(), and FALSE for all
other channels.
g_io_channel_read ()
WARNING
g_io_channel_read has been deprecated since version 2.2 and should not be used
in newly-written code. Use g_io_channel_read_chars() instead.
channel : a GIOChannel
buf : a buffer to read the data into (which should be at least count bytes long)
169
CHAPTER 3. GLIB CORE APPLICATION . . . 3.7. IO CHANNELS
enum GIOError
typedef enum
{
G_IO_ERROR_NONE,
G_IO_ERROR_AGAIN,
G_IO_ERROR_INVAL,
G_IO_ERROR_UNKNOWN
} GIOError;
g_io_channel_write ()
WARNING
g_io_channel_write has been deprecated since version 2.2 and should not be
used in newly-written code. Use g_io_channel_write_chars() instead.
g_io_channel_seek ()
WARNING
g_io_channel_seek has been deprecated since version 2.2 and should not be used
in newly-written code. Use g_io_channel_seek_position() instead.
Sets the current position in the GIOChannel, similar to the standard library function fseek().
170
CHAPTER 3. GLIB CORE APPLICATION . . . 3.8. ERROR REPORTING
channel : a GIOChannel
type : the position in the file, which can be G_SEEK_CUR (the current position), G_SEEK_SET (the start
of the file), or G_SEEK_END (the end of the file)
g_io_channel_close ()
WARNING
g_io_channel_close has been deprecated since version 2.2 and should not be
used in newly-written code. Use g_io_channel_shutdown() instead.
Close an IO channel. Any pending data to be written will be flushed, ignoring errors. The channel
will not be freed until the last reference is dropped using g_io_channel_unref().
channel : A GIOChannel
See Also
Synopsis
#include <glib.h>
GError;
GError* g_error_new (GQuark domain,
gint code,
const gchar *format,
...);
GError* g_error_new_literal (GQuark domain,
gint code,
const gchar *message);
void g_error_free (GError *error);
GError* g_error_copy (const GError *error);
gboolean g_error_matches (const GError *error,
GQuark domain,
gint code);
void g_set_error (GError **err,
GQuark domain,
171
CHAPTER 3. GLIB CORE APPLICATION . . . 3.8. ERROR REPORTING
gint code,
const gchar *format,
...);
void g_set_error_literal (GError **err,
GQuark domain,
gint code,
const gchar *message);
void g_propagate_error (GError **dest,
GError *src);
void g_clear_error (GError **err);
void g_prefix_error (GError **err,
const gchar *format,
...);
void g_propagate_prefixed_error (GError **dest,
GError *src,
const gchar *format,
...);
Description
GLib provides a standard method of reporting errors from a called function to the calling code. (This
is the same problem solved by exceptions in other languages.) It’s important to understand that this
method is both a data type (the GError object) and a set of rules. If you use GError incorrectly, then
your code will not properly interoperate with other code that uses GError, and users of your API will
probably get confused.
First and foremost: GError should only be used to report recoverable runtime errors, never to report program-
ming errors. If the programmer has screwed up, then you should use g_warning(), g_return_if_fail(),
g_assert(), g_error(), or some similar facility. (Incidentally, remember that the g_error() function should
only be used for programming errors, it should not be used to print any error reportable via GError.)
Examples of recoverable runtime errors are "file not found" or "failed to parse input." Examples of
programming errors are "NULL passed to strcmp()" or "attempted to free the same pointer twice." These
two kinds of errors are fundamentally different: runtime errors should be handled or reported to the
user, programming errors should be eliminated by fixing the bug in the program. This is why most
functions in GLib and GTK+ do not use the GError facility.
Functions that can fail take a return location for a GError as their last argument. For example:
gboolean g_file_get_contents (const gchar *filename,
gchar **contents,
gsize *length,
GError **error);
If you pass a non-NULL value for the error argument, it should point to a location where an error can
be placed. For example:
gchar *contents;
GError *err = NULL;
g_file_get_contents ("foo.txt", &contents, NULL, &err);
g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL)) ←-
;
if (err != NULL)
{
/* Report error to user, and free error */
g_assert (contents == NULL);
fprintf (stderr, "Unable to read file: %s\n", err->message);
g_error_free (err);
}
else
{
/* Use file contents */
g_assert (contents != NULL);
}
172
CHAPTER 3. GLIB CORE APPLICATION . . . 3.8. ERROR REPORTING
Note that err != NULL in this example is a reliable indicator of whether g_file_get_contents() failed.
Additionally, g_file_get_contents() returns a boolean which indicates whether it was successful.
Because g_file_get_contents() returns FALSE on failure, if you are only interested in whether it failed
and don’t need to display an error message, you can pass NULL for the error argument:
if (g_file_get_contents ("foo.txt", &contents, NULL, NULL)) /* ignore errors */
/* no error occurred */ ;
else
/* error */ ;
The GError object contains three fields: domain indicates the module the error-reporting function is
located in, code indicates the specific error that occurred, and message is a user-readable error mes-
sage with as many details as possible. Several functions are provided to deal with an error received
from a called function: g_error_matches() returns TRUE if the error matches a given domain and code,
g_propagate_error() copies an error into an error location (so the calling function will receive it), and
g_clear_error() clears an error location by freeing the error and resetting the location to NULL. To display
an error to the user, simply display error->message, perhaps along with additional context known
only to the calling function (the file being opened, or whatever -- though in the g_file_get_contents()
case, error->message already contains a filename).
When implementing a function that can report errors, the basic tool is g_set_error(). Typically, if a
fatal error occurs you want to g_set_error(), then return immediately. g_set_error() does nothing if the
error location passed to it is NULL. Here’s an example:
gint
foo_open_file (GError **error)
{
gint fd;
fd = open ("file.txt", O_RDONLY);
if (fd < 0)
{
g_set_error (error,
FOO_ERROR, /* error domain */
FOO_ERROR_BLAH, /* error code */
"Failed to open file: %s", /* error message format string */
g_strerror (errno));
return -1;
}
else
return fd;
}
Things are somewhat more complicated if you yourself call another function that can report a GError.
If the sub-function indicates fatal errors in some way other than reporting a GError, such as by returning
TRUE on success, you can simply do the following:
gboolean
my_function_that_can_fail (GError **err)
{
g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
if (!sub_function_that_can_fail (err))
{
/* assert that error was set by the sub-function */
g_assert (err == NULL || *err != NULL);
return FALSE;
}
/* otherwise continue, no error occurred */
g_assert (err == NULL || *err == NULL);
}
If the sub-function does not indicate errors other than by reporting a GError, you need to create a
temporary GError since the passed-in one may be NULL. g_propagate_error() is intended for use in this
case.
gboolean
173
CHAPTER 3. GLIB CORE APPLICATION . . . 3.8. ERROR REPORTING
Error pileups are always a bug. For example, this code is incorrect:
gboolean
my_function_that_can_fail (GError **err)
{
GError *tmp_error;
g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
tmp_error = NULL;
sub_function_that_can_fail (&tmp_error);
other_function_that_can_fail (&tmp_error);
if (tmp_error != NULL)
{
g_propagate_error (err, tmp_error);
return FALSE;
}
}
Note that passing NULL for the error location ignores errors; it’s equivalent to try { sub_funct-
ion_that_can_fail(); } catch (...) {} in C++. It does not mean to leave errors unhandled;
it means to handle them by doing nothing.
Error domains and codes are conventionally named as follows:
174
CHAPTER 3. GLIB CORE APPLICATION . . . 3.8. ERROR REPORTING
g_spawn_error_quark (void)
{
return g_quark_from_static_string ("g-spawn-error-quark");
}
• The error codes are in an enumeration called <Namespace><Module>Error; for example, GTh-
readError or GSpawnError.
• Members of the error code enumeration are called <NAMESPACE>_<MODULE>_ERROR_<CODE>,
for example G_SPAWN_ERROR_FORK or G_THREAD_ERROR_AGAIN.
• If there’s a "generic" or "unknown" error code for unrecoverable errors it doesn’t make sense to
distinguish with specific codes, it should be called <NAMESPACE>_<MODULE>_ERROR_FAILED,
for example G_SPAWN_ERROR_FAILED or G_THREAD_ERROR_FAILED.
Summary of rules for use of ""
• Do not report programming errors via GError.
• The last argument of a function that returns an error should be a location where a GError can
be placed (i.e. "GError** error"). If GError is used with varargs, the GError** should be the last
argument before the "...".
• The caller may pass NULL for the GError** if they are not interested in details of the exact error
that occurred.
• If NULL is passed for the GError** argument, then errors should not be returned to the caller, but
your function should still abort and return if an error occurs. That is, control flow should not be
affected by whether the caller wants to get a GError.
• If a GError is reported, then your function by definition had a fatal failure and did not complete what-
ever it was supposed to do. If the failure was not fatal, then you handled it and you should not report
it. If it was fatal, then you must report it and discontinue whatever you were doing immediately.
• A GError* must be initialized to NULL before passing its address to a function that can report
errors.
• "Piling up" errors is always a bug. That is, if you assign a new GError to a GError* that is non-
NULL, thus overwriting the previous error, it indicates that you should have aborted the operation
instead of continuing. If you were able to continue, you should have cleared the previous error
with g_clear_error(). g_set_error() will complain if you pile up errors.
• By convention, if you return a boolean value indicating success then TRUE means success and
FALSE means failure. If FALSE is returned, the error must be set to a non-NULL value.
• A NULL return value is also frequently used to mean that an error occurred. You should make
clear in your documentation whether NULL is a valid return value in non-error cases; if NULL is a
valid value, then users must check whether an error was returned to see if the function succeeded.
• When implementing a function that can report errors, you may want to add a check at the top of
your function that the error return location is either NULL or contains a NULL error (e.g. g_re-
turn_if_fail (error == NULL || *error == NULL);).
Details
GError
typedef struct {
GQuark domain;
gint code;
gchar *message;
} GError;
The GError structure contains information about an error that has occurred.
175
CHAPTER 3. GLIB CORE APPLICATION . . . 3.8. ERROR REPORTING
g_error_new ()
Creates a new GError with the given domain and code, and a message formatted with format.
g_error_new_literal ()
Creates a new GError; unlike g_error_new(), message is not a printf()-style format string. Use this
function if message contains text you don’t have control over, that could include printf() escape se-
quences.
g_error_free ()
error : a GError
g_error_copy ()
error : a GError
176
CHAPTER 3. GLIB CORE APPLICATION . . . 3.8. ERROR REPORTING
g_error_matches ()
g_set_error ()
Does nothing if err is NULL; if err is non-NULL, then *err must be NULL. A new GError is created
and assigned to *err .
err : a return location for a GError, or NULL
g_set_error_literal ()
Does nothing if err is NULL; if err is non-NULL, then *err must be NULL. A new GError is created
and assigned to *err . Unlike g_set_error(), message is not a printf()-style format string. Use this func-
tion if message contains text you don’t have control over, that could include printf() escape sequences.
err : a return location for a GError, or NULL
g_propagate_error ()
If dest is NULL, free src; otherwise, moves src into *dest. The error variable dest points to must
be NULL.
dest : error return location
177
CHAPTER 3. GLIB CORE APPLICATION . . . 3.9. MESSAGE OUTPUT AND DEBUGGING . . .
g_clear_error ()
If err is NULL, does nothing. If err is non-NULL, calls g_error_free() on *err and sets *err to
NULL.
g_prefix_error ()
Formats a string according to format and prefix it to an existing error message. If err is NULL (ie:
no error variable) then do nothing.
If *err is NULL (ie: an error variable is present but there is no error condition) then also do nothing.
Whether or not it makes sense to take advantage of this feature is up to you.
Since 2.16
g_propagate_prefixed_error ()
If dest is NULL, free src; otherwise, moves src into *dest. *dest must be NULL. After the move,
add a prefix as with g_prefix_error().
Since 2.16
178
CHAPTER 3. GLIB CORE APPLICATION . . . 3.9. MESSAGE OUTPUT AND DEBUGGING . . .
Synopsis
#include <glib.h>
#define G_BREAKPOINT ()
Description
These functions provide support for outputting messages.
The g_return family of macros (g_return_if_fail(), g_return_val_if_fail(), g_return_if_reached(),
g_return_val_if_reached()) should only be used for programming errors, a typical use case is check-
ing for invalid parameters at the beginning of a public function. They should not be used if you just
mean "if (error) return", they should only be used if you mean "if (bug in program) return". The pro-
gram behavior is generally considered undefined after one of these checks fails. They are not intended
for normal control flow, only to give a perhaps-helpful warning before giving up.
Details
g_print ()
Outputs a formatted message via the print handler. The default print handler simply outputs the
message to stdout.
g_print() should not be used from within libraries for debugging messages, since it may be redirected
by applications to special purpose message windows or even files. Instead, libraries should use g_log(),
or the convenience functions g_message(), g_warning() and g_error().
g_set_print_handler ()
Sets the print handler. Any messages passed to g_print() will be output via the new handler. The
default handler simply outputs the message to stdout. By providing your own handler you can redirect
the output, to a GTK+ widget or a log file for example.
179
CHAPTER 3. GLIB CORE APPLICATION . . . 3.9. MESSAGE OUTPUT AND DEBUGGING . . .
GPrintFunc ()
Specifies the type of the print handler functions. These are called with the complete formatted string
to output.
g_printerr ()
Outputs a formatted message via the error message handler. The default handler simply outputs the
message to stderr.
g_printerr() should not be used from within libraries. Instead g_log() should be used, or the conve-
nience functions g_message(), g_warning() and g_error().
g_set_printerr_handler ()
Sets the handler for printing error messages. Any messages passed to g_printerr() will be output via
the new handler. The default handler simply outputs the message to stderr. By providing your own
handler you can redirect the output, to a GTK+ widget or a log file for example.
g_return_if_fail()
#define g_return_if_fail(expr)
Returns from the current function if the expression is not true. If the expression evaluates to FALSE,
a critical message is logged and the function returns. This can only be used in functions which do not
return a value.
g_return_val_if_fail()
#define g_return_val_if_fail(expr,val)
Returns from the current function, returning the value val, if the expression is not true. If the ex-
pression evaluates to FALSE, a critical message is logged and val is returned.
val : the value to return from the current function if the expression is not true.
180
CHAPTER 3. GLIB CORE APPLICATION . . . 3.9. MESSAGE OUTPUT AND DEBUGGING . . .
g_return_if_reached()
#define g_return_if_reached()
Logs a critical message and returns from the current function. This can only be used in functions
which do not return a value.
g_return_val_if_reached()
#define g_return_val_if_reached(val)
g_warn_if_fail()
#define g_warn_if_fail(expr)
Since 2.16
g_warn_if_reached()
#define g_warn_if_reached()
g_on_error_query ()
Prompts the user with [E]xit, [H]alt, show [S]tack trace or [P]roceed. This func-
tion is intended to be used for debugging use only. The following example shows how it can be used
together with the g_log() functions.
#include <glib.h>
static void
log_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
g_log_default_handler (log_domain, log_level, message, user_data);
g_on_error_query (MY_PROGRAM_NAME);
}
int main (int argc, char *argv[])
{
g_log_set_handler (MY_LOG_DOMAIN,
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_ERROR |
G_LOG_LEVEL_CRITICAL,
log_handler,
NULL);
/* ... */
181
CHAPTER 3. GLIB CORE APPLICATION . . . 3.10. MESSAGE LOGGING
g_on_error_stack_trace ()
Invokes gdb, which attaches to the current process and shows a stack trace. Called by g_on_error_query()
when the [S]tack trace option is selected.
This function may cause different actions on non-UNIX platforms.
prg_name : the program name, needed by gdb for the [S]tack trace option. If prg_name is NULL,
g_get_prgname() is called to get the program name (which will work correctly if gdk_init() or
gtk_init() has been called).
G_BREAKPOINT()
#define G_BREAKPOINT()
Inserts a breakpoint instruction into the code. On x86 and alpha systems this is implemented as a
soft interrupt and on other architectures it raises a SIGTRAP signal.
Synopsis
#include <glib.h>
#define G_LOG_DOMAIN
#define G_LOG_FATAL_MASK
#define G_LOG_LEVEL_USER_SHIFT
void (*GLogFunc) (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data);
enum GLogLevelFlags;
182
CHAPTER 3. GLIB CORE APPLICATION . . . 3.10. MESSAGE LOGGING
Description
These functions provide support for logging error messages or messages used for debugging.
There are several built-in levels of messages, defined in GLogLevelFlags. These can be extended
with user-defined levels.
Details
G_LOG_DOMAIN
Defines the log domain. For applications, this is typically left as the default NULL (or "") domain.
Libraries should define this so that any messages which they log can be differentiated from messages
from other libraries and application code. But be careful not to define it in any public header files.
For example, GTK+ uses this in its Makefile.am:
INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
G_LOG_FATAL_MASK
G_LOG_LEVEL_USER_SHIFT
Log level shift offset for user defined log levels (0-7 are used by GLib).
GLogFunc ()
183
CHAPTER 3. GLIB CORE APPLICATION . . . 3.10. MESSAGE LOGGING
log_level : the log level of the message (including the fatal and recursion flags).
enum GLogLevelFlags
typedef enum
{
/* log flags */
G_LOG_FLAG_RECURSION = 1 << 0,
G_LOG_FLAG_FATAL = 1 << 1,
Flags specifying the level of log messages. It is possible to change how GLib treats messages of the
various levels using g_log_set_handler() and g_log_set_fatal_mask().
G_LOG_LEVEL_ERROR log level for errors, see g_error(). This level is also used for messages produced
by g_assert().
G_LOG_LEVEL_CRITICAL log level for critical messages, see g_critical(). This level is also used for
messages produced by g_return_if_fail() and g_return_val_if_fail().
184
CHAPTER 3. GLIB CORE APPLICATION . . . 3.10. MESSAGE LOGGING
g_log ()
Logs an error or debugging message. If the log level has been set as fatal, the abort() function is
called to terminate the program.
log_domain : the log domain, usually G_LOG_DOMAIN.
g_logv ()
Logs an error or debugging message. If the log level has been set as fatal, the abort() function is
called to terminate the program.
log_domain : the log domain.
g_message()
#define g_message(...)
g_warning()
#define g_warning(...)
g_critical()
#define g_critical(...)
185
CHAPTER 3. GLIB CORE APPLICATION . . . 3.10. MESSAGE LOGGING
g_error()
#define g_error(...)
A convenience function/macro to log an error message. Error messages are always fatal, resulting
in a call to abort() to terminate the application. This function will result in a core dump; don’t use it for
errors you expect. Using this function indicates a bug in your program, i.e. an assertion failure.
... : format string, followed by parameters to insert into the format string (as with printf())
g_debug()
#define g_debug(...)
Since 2.6
g_log_set_handler ()
Sets the log handler for a domain and a set of log levels. To handle fatal and recursive messages the l-
og_levels parameter must be combined with the G_LOG_FLAG_FATAL and G_LOG_FLAG_RECURSION
bit flags.
Note that since the G_LOG_LEVEL_ERROR log level is always fatal, if you want to set a handler for
this log level you must combine it with G_LOG_FLAG_FATAL.
Example 3.11 Adding a log handler for all warning messages in the default (application) domain
g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION, my_log_handler, NULL);
Example 3.12 Adding a log handler for all critical messages from GTK+
g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION, my_log_handler, NULL);
Example 3.13 Adding a log handler for all messages from GLib
g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION, my_log_handler, NULL);
log_domain : the log domain, or NULL for the default "" application domain.
log_levels : the log levels to apply the log handler for. To handle fatal and recursive messages as well,
combine the log levels with the G_LOG_FLAG_FATAL and G_LOG_FLAG_RECURSION bit flags.
log_func : the log handler function.
186
CHAPTER 3. GLIB CORE APPLICATION . . . 3.10. MESSAGE LOGGING
g_log_remove_handler ()
g_log_set_always_fatal ()
Sets the message levels which are always fatal, in any log domain. When a message with any of
these levels is logged the program terminates. You can only set the levels defined by GLib to be fatal.
G_LOG_LEVEL_ERROR is always fatal.
You can also make some message levels fatal at runtime by setting the G_DEBUG environment vari-
able (see Running GLib Applications).
fatal_mask : the mask containing bits set for each level of error which is to be fatal.
g_log_set_fatal_mask ()
Sets the log levels which are fatal in the given domain. G_LOG_LEVEL_ERROR is always fatal.
g_log_default_handler ()
The default log handler set up by GLib; g_log_set_default_handler() allows to install an alternate
default log handler. This is used if no log handler has been set for the particular log domain and log
level combination. It outputs the message to stderr or stdout and if the log level is fatal it calls abort().
stderr is used for levels G_LOG_LEVEL_ERROR, G_LOG_LEVEL_CRITICAL, G_LOG_LEVEL_WARNING
and G_LOG_LEVEL_MESSAGE. stdout is used for the rest.
187
CHAPTER 3. GLIB CORE APPLICATION . . . 3.10. MESSAGE LOGGING
g_log_set_default_handler ()
Installs a default log handler which is used if no log handler has been set for the particular log
domain and log level combination. By default, GLib uses g_log_default_handler() as default log handler.
log_func : the log handler function.
188
Chapter 4
GLib Utilities
Synopsis
#include <glib.h>
#include <glib/gprintf.h>
189
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
190
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
191
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
...);
gchar* g_strjoinv (const gchar *separator,
gchar **str_array);
guint g_strv_length (gchar **str_array);
Description
This section describes a number of utility functions for creating, duplicating, and manipulating strings.
Note that the functions g_printf(), g_fprintf(), g_sprintf(), g_snprintf(), g_vprintf(), g_vfprintf(), g_vsprintf()
and g_vsnprintf() are declared in the header gprintf.h which is not included in glib.h (otherwise
using glib.h would drag in stdio.h), so you’ll have to explicitly include <glib/gprintf.h> in
order to use the GLib printf() functions.
While you may use the printf() functions to format UTF-8 strings, notice that the precision of a %Ns
parameter is interpreted as the number of bytes, not characters to print. On top of that, the GNU libc
implementation of the printf() functions has the "feature" that it checks that the string given for the %Ns
parameter consists of a whole number of characters in the current encoding. So, unless you are sure you
are always going to be in an UTF-8 locale or your know your text is restricted to ASCII, avoid using %Ns.
If your intention is to format strings for a certain number of columns, then %Ns is not a correct solution
anyway, since it fails to take wide characters (see g_unichar_iswide()) into account.
Details
g_strdup ()
Duplicates a string. If str is NULL it returns NULL. The returned string should be freed with
g_free() when no longer needed.
g_strndup ()
Duplicates the first n bytes of a string, returning a newly-allocated buffer n + 1 bytes long which will
always be nul-terminated. If str is less than n bytes long the buffer is padded with nuls. If str is NULL
it returns NULL. The returned value should be freed when no longer needed.
N OTE
To copy a number of characters from a UTF-8 encoded string, use g_utf8_strncpy() in-
stead.
192
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_strdupv ()
Copies NULL-terminated array of strings. The copy is a deep copy; the new array should be freed by
first freeing each string, then the array itself. g_strfreev() does this for you. If called on a NULL value,
g_strdupv() simply returns NULL.
g_strnfill ()
Creates a new string length bytes long filled with fill_char . The returned string should be freed
when no longer needed.
g_stpcpy ()
Copies a nul-terminated string into the dest buffer, include the trailing nul, and return a pointer
to the trailing nul byte. This is useful for concatenating multiple strings together without having to
repeatedly scan for the end.
g_strstr_len ()
Searches the string haystack for the first occurrence of the string needle, limiting the length of the
search to haystack_len.
haystack : a string.
haystack_len : the maximum length of haystack . Note that -1 is a valid length, if haystack is nul-
terminated, meaning it will search through the whole string.
193
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_strrstr ()
Searches the string haystack for the last occurrence of the string needle.
g_strrstr_len ()
Searches the string haystack for the last occurrence of the string needle, limiting the length of the
search to haystack_len.
g_str_has_prefix ()
Since 2.2
g_str_has_suffix ()
Since 2.2
194
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_strcmp0 ()
Compares str1 and str2 like strcmp(). Handles NULL gracefully by sorting it before non-NULL
strings.
str1 : a C string or NULL
g_strlcpy ()
Portability wrapper that calls strlcpy() on systems which have it, and emulates strlcpy() otherwise.
Copies src to dest; dest is guaranteed to be nul-terminated; src must be nul-terminated; dest_size
is the buffer size, not the number of chars to copy.
At most dest_size - 1 characters will be copied. Always nul-terminates (unless dest_size == 0). This
function does not allocate memory. Unlike strncpy(), this function doesn’t pad dest (so it’s often faster).
It returns the size of the attempted result, strlen (src), so if retval >= dest_size, truncation occurred.
N OTE
Caveat: strlcpy() is supposedly more secure than strcpy() or strncpy(), but if you really
want to avoid screwups, g_strdup() is an even better idea.
g_strlcat ()
Portability wrapper that calls strlcat() on systems which have it, and emulates it otherwise. Appends
nul-terminated src string to dest, guaranteeing nul-termination for dest. The total size of dest won’t
exceed dest_size.
At most dest_size - 1 characters will be copied. Unlike strncat, dest_size is the full size of dest, not
the space left over. This function does NOT allocate memory. This always NUL terminates (unless siz
== 0 or there were no NUL characters in the dest_size characters of dest to start with).
dest : destination buffer, already containing one nul-terminated string
dest_size : length of dest buffer in bytes (not length of existing string inside dest)
195
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
Returns : size of attempted result, which is MIN (dest_size, strlen (original dest)) + strlen (src), so if
retval >= dest_size, truncation occurred.
N OTE
Caveat: this is supposedly a more secure alternative to strcat() or strncat(), but for
real security g_strconcat() is harder to mess up.
g_strdup_printf ()
Similar to the standard C sprintf() function but safer, since it calculates the maximum space required
and allocates memory to hold the result. The returned string should be freed with g_free() when no
longer needed.
format : a standard printf() format string, but notice string precision pitfalls
g_strdup_vprintf ()
Similar to the standard C vsprintf() function but safer, since it calculates the maximum space required
and allocates memory to hold the result. The returned string should be freed with g_free() when no
longer needed.
See also g_vasprintf(), which offers the same functionality, but additionally returns the length of the
allocated string.
format : a standard printf() format string, but notice string precision pitfalls
g_printf ()
An implementation of the standard printf() function which supports positional parameters, as spec-
ified in the Single Unix Specification.
format : a standard printf() format string, but notice string precision pitfalls.
Since 2.2
196
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_vprintf ()
An implementation of the standard vprintf() function which supports positional parameters, as spec-
ified in the Single Unix Specification.
format : a standard printf() format string, but notice string precision pitfalls.
g_fprintf ()
An implementation of the standard fprintf() function which supports positional parameters, as spec-
ified in the Single Unix Specification.
file : the stream to write to.
format : a standard printf() format string, but notice string precision pitfalls.
g_vfprintf ()
An implementation of the standard fprintf() function which supports positional parameters, as spec-
ified in the Single Unix Specification.
file : the stream to write to.
format : a standard printf() format string, but notice string precision pitfalls.
g_sprintf ()
An implementation of the standard sprintf() function which supports positional parameters, as spec-
ified in the Single Unix Specification.
string : A pointer to a memory buffer to contain the resulting string. It is up to the caller to ensure that
the allocated buffer is large enough to hold the formatted result
197
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
format : a standard printf() format string, but notice string precision pitfalls.
Since 2.2
g_vsprintf ()
format : a standard printf() format string, but notice string precision pitfalls.
Since 2.2
g_snprintf ()
A safer form of the standard sprintf() function. The output is guaranteed to not exceed n characters
(including the terminating nul character), so it is easy to ensure that a buffer overflow cannot occur.
See also g_strdup_printf().
In versions of GLib prior to 1.2.3, this function may return -1 if the output was truncated, and the
truncated string may not be nul-terminated. In versions prior to 1.3.12, this function returns the length
of the output string.
The return value of g_snprintf() conforms to the snprintf() function as standardized in ISO C99. Note
that this is different from traditional snprintf(), which returns the length of the output string.
The format string may contain positional parameters, as specified in the Single Unix Specification.
n : the maximum number of bytes to produce (including the terminating nul character).
format : a standard printf() format string, but notice string precision pitfalls.
Returns : the number of bytes which would be produced if the buffer was large enough.
g_vsnprintf ()
198
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
A safer form of the standard vsprintf() function. The output is guaranteed to not exceed n characters
(including the terminating nul character), so it is easy to ensure that a buffer overflow cannot occur.
See also g_strdup_vprintf().
In versions of GLib prior to 1.2.3, this function may return -1 if the output was truncated, and the
truncated string may not be nul-terminated. In versions prior to 1.3.12, this function returns the length
of the output string.
The return value of g_vsnprintf() conforms to the vsnprintf() function as standardized in ISO C99.
Note that this is different from traditional vsnprintf(), which returns the length of the output string.
The format string may contain positional parameters, as specified in the Single Unix Specification.
string : the buffer to hold the output.
n : the maximum number of bytes to produce (including the terminating nul character).
format : a standard printf() format string, but notice string precision pitfalls.
Returns : the number of bytes which would be produced if the buffer was large enough.
g_vasprintf ()
An implementation of the GNU vasprintf() function which supports positional parameters, as spec-
ified in the Single Unix Specification. This function is similar to g_vsprintf(), except that it allocates a
string to hold the output, instead of putting the output in a buffer you allocate in advance.
string : the return location for the newly-allocated string.
format : a standard printf() format string, but notice string precision pitfalls.
g_printf_string_upper_bound ()
Calculates the maximum space needed to store the output of the sprintf() function.
format : the format string. See the printf() documentation.
g_ascii_isalnum ()
199
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_ascii_isalpha ()
c : any character
g_ascii_iscntrl ()
c : any character
g_ascii_isdigit ()
c : any character
g_ascii_isgraph ()
c : any character
200
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_ascii_islower ()
c : any character
g_ascii_isprint ()
c : any character
g_ascii_ispunct ()
c : any character
g_ascii_isspace ()
c : any character
201
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_ascii_isupper ()
c : any character
g_ascii_isxdigit ()
c : any character
g_ascii_digit_value ()
Determines the numeric value of a character as a decimal digit. Differs from g_unichar_digit_value()
because it takes a char, so there’s no worry about sign extension if characters are signed.
c : an ASCII character.
Returns : If c is a decimal digit (according to g_ascii_isdigit()), its numeric value. Otherwise, -1.
g_ascii_xdigit_value ()
Determines the numeric value of a character as a hexidecimal digit. Differs from g_unichar_xdigit_value()
because it takes a char, so there’s no worry about sign extension if characters are signed.
c : an ASCII character.
Returns : If c is a hex digit (according to g_ascii_isxdigit()), its numeric value. Otherwise, -1.
g_ascii_strcasecmp ()
202
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
Returns : 0 if the strings match, a negative value if s1 < s2, or a positive value if s1 > s2.
g_ascii_strncasecmp ()
Compare s1 and s2, ignoring the case of ASCII characters and any characters after the first n in each
string.
Unlike the BSD strcasecmp() function, this only recognizes standard ASCII letters and ignores the
locale, treating all non-ASCII characters as if they are not letters.
The same warning as in g_ascii_strcasecmp() applies: Use this function only on strings known to be
in encodings where bytes corresponding to ASCII letters always represent themselves.
Returns : 0 if the strings match, a negative value if s1 < s2, or a positive value if s1 > s2.
g_ascii_strup ()
Converts all lower case ASCII letters to upper case ASCII letters.
str : a string.
Returns : a newly allocated string, with all the lower case characters in str converted to upper case,
with semantics that exactly match g_ascii_toupper(). (Note that this is unlike the old g_strup(),
which modified the string in place.)
g_ascii_strdown ()
Converts all upper case ASCII letters to lower case ASCII letters.
str : a string.
Returns : a newly-allocated string, with all the upper case characters in str converted to lower case,
with semantics that exactly match g_ascii_tolower(). (Note that this is unlike the old g_strdown(),
which modified the string in place.)
203
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_ascii_tolower ()
c : any character.
Returns : the result of converting c to lower case. If c is not an ASCII upper case letter, c is returned
unchanged.
g_ascii_toupper ()
c : any character.
Returns : the result of converting c to upper case. If c is not an ASCII lower case letter, c is returned
unchanged.
g_string_ascii_up ()
Converts all lower case ASCII letters to upper case ASCII letters.
string : a GString
Returns : passed-in string pointer, with all the lower case characters converted to upper case in place,
with semantics that exactly match g_ascii_toupper().
g_string_ascii_down ()
Converts all upper case ASCII letters to lower case ASCII letters.
string : a GString
Returns : passed-in string pointer, with all the upper case characters converted to lower case in place,
with semantics that exactly match g_ascii_tolower().
204
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_strup ()
WARNING
g_strup has been deprecated since version 2.2 and should not be used in newly-written
code. This function is totally broken for the reasons discussed in the g_strncasecmp()
docs - use g_ascii_strup() or g_utf8_strup() instead.
g_strdown ()
WARNING
g_strdown has been deprecated since version 2.2 and should not be used in
newly-written code. This function is totally broken for the reasons discussed in the
g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown() instead.
g_strcasecmp ()
WARNING
g_strcasecmp has been deprecated since version 2.2 and should not be used in
newly-written code. See g_strncasecmp() for a discussion of why this function is dep-
recated and how to replace it.
s1 : a string.
Returns : 0 if the strings match, a negative value if s1 < s2, or a positive value if s1 > s2.
205
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_strncasecmp ()
WARNING
g_strncasecmp has been deprecated since version 2.2 and should not be used in
newly-written code. The problem with g_strncasecmp() is that it does the comparison
by calling toupper()/tolower(). These functions are locale-specific and operate on single
bytes. However, it is impossible to handle things correctly from an I18N standpoint by
operating on bytes, since characters may be multibyte. Thus g_strncasecmp() is broken
if your string is guaranteed to be ASCII, since it’s locale-sensitive, and it’s broken if your
string is localized, since it doesn’t work on many encodings at all, including UTF-8, EUC-
JP, etc.
There are therefore two replacement functions: g_ascii_strncasecmp(), which only works
on ASCII and is not locale-sensitive, and g_utf8_casefold(), which is good for case-
insensitive sorting of UTF-8.
s1 : a string.
Returns : 0 if the strings match, a negative value if s1 < s2, or a positive value if s1 > s2.
g_strreverse ()
Reverses all of the bytes in a string. For example, g_strreverse ("abcdef") will result in "fed-
cba".
Note that g_strreverse() doesn’t work on UTF-8 strings containing multibyte characters. For that
purpose, use g_utf8_strreverse().
g_ascii_strtoll ()
Converts a string to a gint64 value. This function behaves like the standard strtoll() function does in
the C locale. It does this without actually changing the current locale, since that would not be thread-
safe.
This function is typically used when reading configuration files or other non-user input that should
be locale independent. To handle input from the user you should normally use the locale-sensitive
system strtoll() function.
206
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
If the correct value would cause overflow, G_MAXINT64 or G_MININT64 is returned, and ERANGE
is stored in errno. If the base is outside the valid range, zero is returned, and EINVAL is stored in errno.
If the string conversion fails, zero is returned, and endptr returns nptr (if endptr is non-NULL).
nptr : the string to convert to a numeric value.
endptr : if non-NULL, it returns the character after the last character used in the conversion.
g_ascii_strtoull ()
Converts a string to a guint64 value. This function behaves like the standard strtoull() function does
in the C locale. It does this without actually changing the current locale, since that would not be thread-
safe.
This function is typically used when reading configuration files or other non-user input that should
be locale independent. To handle input from the user you should normally use the locale-sensitive
system strtoull() function.
If the correct value would cause overflow, G_MAXUINT64 is returned, and ERANGE is stored in
errno. If the base is outside the valid range, zero is returned, and EINVAL is stored in errno. If the string
conversion fails, zero is returned, and endptr returns nptr (if endptr is non-NULL).
nptr : the string to convert to a numeric value.
endptr : if non-NULL, it returns the character after the last character used in the conversion.
G_ASCII_DTOSTR_BUF_SIZE
A good size for a buffer to be passed into g_ascii_dtostr(). It is guaranteed to be enough for all output
of that function on systems with 64bit IEEE-compatible doubles.
The typical usage would be something like:
char buf[G_ASCII_DTOSTR_BUF_SIZE];
fprintf (out, "value=%s\n", g_ascii_dtostr (buf, sizeof (buf), value));
g_ascii_strtod ()
207
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
endptr : if non-NULL, it returns the character after the last character used in the conversion.
g_ascii_dtostr ()
g_ascii_formatd ()
Converts a gdouble to a string, using the ’.’ as decimal point. To format the number you pass in a
printf()-style format string. Allowed conversion specifiers are ’e’, ’E’, ’f’, ’F’, ’g’ and ’G’.
If you just want to want to serialize the value into a string, use g_ascii_dtostr().
buffer : A buffer to place the resulting string in
format : The printf()-style format to use for the code to use for converting.
g_strtod ()
Converts a string to a gdouble value. It calls the standard strtod() function to handle the conversion,
but if the string is not completely converted it attempts the conversion again with g_ascii_strtod(), and
returns the best match.
This function should seldomly be used. The normal situation when reading numbers not for hu-
man consumption is to use g_ascii_strtod(). Only when you know that you must expect both locale
formatted and C formatted numbers should you use this. Make sure that you don’t pass strings such
as comma separated lists of values, since the commas may be interpreted as a decimal point in some
locales, causing unexpected results.
208
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
endptr : if non-NULL, it returns the character after the last character used in the conversion.
g_strchug ()
Removes leading whitespace from a string, by moving the rest of the characters forward.
This function doesn’t allocate or reallocate any memory; it modifies string in place. The pointer to
string is returned to allow the nesting of functions.
Also see g_strchomp() and g_strstrip().
Returns : @string.
g_strchomp ()
Returns : @string.
g_strstrip()
Removes leading and trailing whitespace from a string. See g_strchomp() and g_strchug().
g_strdelimit ()
Converts any delimiter characters in string to new_delimiter . Any characters in string which
are found in delimiters are changed to the new_delimiter character. Modifies string in place, and
returns string itself, not a copy. The return value is to allow nesting such as g_ascii_strup (g_s-
trdelimit (str, "abc", ’?’)).
delimiters : a string containing the current delimiters, or NULL to use the standard delimiters defined
in G_STR_DELIMITERS.
Returns : @string.
209
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
G_STR_DELIMITERS
g_strescape ()
Escapes the special characters ’\b’, ’\f’, ’\n’, ’\r’, ’\t’, ’\’ and ’"’ in the string source by inserting
a ’\’ before them. Additionally all characters in the range 0x01-0x1F (everything below SPACE) and in
the range 0x7F-0xFF (all non-ASCII chars) are replaced with a ’\’ followed by their octal representation.
Characters supplied in exceptions are not escaped.
g_strcompress() does the reverse conversion.
Returns : a newly-allocated copy of source with certain characters escaped. See above.
g_strcompress ()
Replaces all escaped characters with their one byte equivalent. It does the reverse conversion of
g_strescape().
g_strcanon ()
For each character in string , if the character is not in valid_chars, replaces the character with su-
bstitutor . Modifies string in place, and return string itself, not a copy. The return value is to allow
nesting such as g_ascii_strup (g_strcanon (str, "abc", ’?’)).
Returns : @string.
g_strsplit ()
210
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
Splits a string into a maximum of max_tokens pieces, using the given delimiter . If max_tokens is
reached, the remainder of string is appended to the last token.
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing
a single string. The reason for this special case is that being able to represent a empty vector is typically
more useful than consistent handling of empty elements. If you do need to represent empty elements,
you’ll need to check for the empty string before calling g_strsplit().
delimiter : a string which specifies the places at which to split the string. The delimiter is not included
in any of the resulting strings, unless max_tokens is reached.
max_tokens : the maximum number of pieces to split string into. If this is less than 1, the string is split
completely.
g_strsplit_set ()
Splits string into a number of tokens not containing any of the characters in delimiter . A token
is the (possibly empty) longest string that does not contain any of the characters in delimiters. If
max_tokens is reached, the remainder is appended to the last token.
For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a NULL-terminated vector contain-
ing the three strings "abc", "def", and "ghi".
The result if g_strsplit_set (":def/ghi:", ":/", -1) is a NULL-terminated vector containing the four
strings "", "def", "ghi", and "".
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing
a single string. The reason for this special case is that being able to represent a empty vector is typically
more useful than consistent handling of empty elements. If you do need to represent empty elements,
you’ll need to check for the empty string before calling g_strsplit_set().
Note that this function works on bytes not characters, so it can’t be used to delimit UTF-8 strings for
anything but ASCII characters.
delimiters : A nul-terminated string containing bytes that are used to split the string.
max_tokens : The maximum number of tokens to split string into. If this is less than 1, the string is
split completely
Since 2.4
g_strfreev ()
Frees a NULL-terminated array of strings, and the array itself. If called on a NULL value, g_strfreev()
simply returns.
211
CHAPTER 4. GLIB UTILITIES 4.1. STRING UTILITY FUNCTIONS
g_strconcat ()
Concatenates all of the given strings into one long string. The returned string should be freed with
g_free() when no longer needed.
WARNING
The variable argument list must end with NULL. If you forget the NULL, g_strconcat() will
start appending random memory junk to your string.
g_strjoin ()
Joins a number of strings together to form one long string, with the optional separator inserted
between each of them. The returned string should be freed with g_free().
separator : a string to insert between each of the strings, or NULL
Returns : a newly-allocated string containing all of the strings joined together, with separator between
them
g_strjoinv ()
Joins a number of strings together to form one long string, with the optional separator inserted
between each of them. The returned string should be freed with g_free().
separator : a string to insert between each of the strings, or NULL
Returns : a newly-allocated string containing all of the strings joined together, with separator between
them
g_strv_length ()
212
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
g_strerror ()
Returns a string corresponding to the given error code, e.g. "no such process". You should use
this function in preference to strerror(), because it returns a string in UTF-8 encoding, and since not all
platforms support the strerror() function.
errnum : the system error number. See the standard C errno documentation
Returns : a UTF-8 string describing the error code. If the error code is unknown, it returns "unknown
error (<code>)". The string can only be used until the next call to g_strerror()
g_strsignal ()
Returns a string describing the given signal, e.g. "Segmentation fault". You should use this function
in preference to strsignal(), because it returns a string in UTF-8 encoding, and since not all platforms
support the strsignal() function.
signum : the signal number. See the signal documentation
Returns : a UTF-8 string describing the signal. If the signal is unknown, it returns "unknown signal
(<signum>)". The string can only be used until the next call to g_strsignal()
Synopsis
#include <glib.h>
213
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
Description
File Name Encodings
Historically, Unix has not had a defined encoding for file names: a file name is valid as long as it does
not have path separators in it ("/"). However, displaying file names may require conversion: from the
character set in which they were created, to the character set in which the application operates. Consider
the Spanish file name "Presentación.sxi". If the application which created it uses ISO-8859-1 for its
encoding, then the actual file name on disk would look like this:
Character: P r e s e n t a c i ó n . s x i
Hex code: 50 72 65 73 65 6e 74 61 63 69 f3 6e 2e 73 78 69
However, if the application use UTF-8, the actual file name on disk would look like this:
Character: P r e s e n t a c i ó n . s x i
Hex code: 50 72 65 73 65 6e 74 61 63 69 c3 b3 6e 2e 73 78 69
214
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
Glib uses UTF-8 for its strings, and GUI toolkits like GTK+ that use Glib do the same thing. If you
get a file name from the file system, for example, from readdir(3) or from g_dir_read_name(), and
you wish to display the file name to the user, you will need to convert it into UTF-8. The opposite case
is when the user types the name of a file he wishes to save: the toolkit will give you that string in UTF-8
encoding, and you will need to convert it to the character set used for file names before you can create
the file with open(2) or fopen(3).
By default, Glib assumes that file names on disk are in UTF-8 encoding. This is a valid assumption
for file systems which were created relatively recently: most applications use UTF-8 encoding for their
strings, and that is also what they use for the file names they create. However, older file systems may
still contain file names created in "older" encodings, such as ISO-8859-1. In this case, for compatibility
reasons, you may want to instruct Glib to use that particular encoding for file names rather than UTF-8.
You can do this by specifying the encoding for file names in the G_FILENAME_ENCODING environ-
ment variable. For example, if your installation uses ISO-8859-1 for file names, you can put this in your
~/.profile:
export G_FILENAME_ENCODING=ISO-8859-1
Glib provides the functions g_filename_to_utf8() and g_filename_from_utf8() to perform the neces-
sary conversions. These functions convert file names from the encoding specified in G_FILENAME_EN-
CODING to UTF-8 and vice-versa. Figure 4.1 illustrates how these functions are used to convert between
UTF-8 and the encoding for file names in the file system.
Checklist for Application Writers This section is a practical summary of the detailed description
above. You can use this as a checklist of things to do to make sure your applications process file name
encodings correctly.
1. If you get a file name from the file system from a function such as readdir(3) or gtk_file-
_chooser_get_filename(), you do not need to do any conversion to pass that file name to
functions like open(2), rename(2), or fopen(3) — those are "raw" file names which the file
system understands.
2. If you need to display a file name, convert it to UTF-8 first by using g_filename_to_utf8(). If
conversion fails, display a string like "Unknown file name". Do not convert this string back into
the encoding used for file names if you wish to pass it to the file system; use the original file name
instead. For example, the document window of a word processor could display "Unknown file
name" in its title bar but still let the user save the file, as it would keep the raw file name internally.
This can happen if the user has not set the G_FILENAME_ENCODING environment variable even
though he has files whose names are not encoded in UTF-8.
3. If your user interface lets the user type a file name for saving or renaming, convert it to the encod-
ing used for file names in the file system by using g_filename_from_utf8(). Pass the converted file
name to functions like fopen(3). If conversion fails, ask the user to enter a different file name.
This can happen if the user types Japanese characters when G_FILENAME_ENCODING is set to
ISO-8859-1, for example.
Details
g_convert ()
215
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
bytes_read : location to store the number of bytes in the input string that were successfully converted,
or NULL. Even if the conversion was successful, this may be less than len if there were partial
characters at the end of the input. If the error G_CONVERT_ERROR_ILLEGAL_SEQUENCE oc-
curs, the value stored will the byte offset after the last valid input sequence.
bytes_written : the number of bytes stored in the output buffer (not including the terminating nul).
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
Returns : If the conversion was successful, a newly allocated nul-terminated string, which must be freed
with g_free(). Otherwise NULL and error will be set.
g_convert_with_fallback ()
Converts a string from one character set to another, possibly including fallback sequences for char-
acters not representable in the output. Note that it is not guaranteed that the specification for the fall-
back sequences in fallback will be honored. Some systems may do an approximate conversion from
from_codeset to to_codeset in their iconv() functions, in which case GLib will simply return that
approximate conversion.
Note that you should use g_iconv() for streaming conversions2 .
str : the string to convert
fallback : UTF-8 string to use in place of character not present in the target encoding. (The string must
be representable in the target encoding). If NULL, characters not in the target encoding will be
represented as Unicode escapes \uxxxx or \Uxxxxyyyy.
bytes_read : location to store the number of bytes in the input string that were successfully converted,
or NULL. Even if the conversion was successful, this may be less than len if there were partial
characters at the end of the input.
bytes_written : the number of bytes stored in the output buffer (not including the terminating nul).
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
Returns : If the conversion was successful, a newly allocated nul-terminated string, which must be freed
with g_free(). Otherwise NULL and error will be set.
1 Note that some encodings may allow nul bytes to occur inside strings. In that case, using -1 for the len parameter is unsafe.
216
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
GIConv
The GIConv struct wraps an iconv() conversion descriptor. It contains private data and should
only be accessed using the following functions.
g_convert_with_iconv ()
bytes_read : location to store the number of bytes in the input string that were successfully converted,
or NULL. Even if the conversion was successful, this may be less than len if there were partial
characters at the end of the input. If the error G_CONVERT_ERROR_ILLEGAL_SEQUENCE oc-
curs, the value stored will the byte offset after the last valid input sequence.
bytes_written : the number of bytes stored in the output buffer (not including the terminating nul).
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
Returns : If the conversion was successful, a newly allocated nul-terminated string, which must be freed
with g_free(). Otherwise NULL and error will be set.
G_CONVERT_ERROR
Error domain for character set conversions. Errors in this domain will be from the GConvertError
enumeration. See GError for information on error domains.
g_iconv_open ()
Same as the standard UNIX routine iconv_open(), but may be implemented via libiconv on UNIX
flavors that lack a native implementation.
GLib provides g_convert() and g_locale_to_utf8() which are likely more convenient than the raw
iconv wrappers.
to_codeset : destination codeset
2 Despite the fact that byes_read can return information about partial characters, the g_convert_... functions are not
generally suitable for streaming. If the underlying converter being used maintains internal state, then this won’t be preserved
across successive calls to g_convert(), g_convert_with_iconv() or g_convert_with_fallback(). (An example of this is the GNU C
converter for CP1255 which does not emit a base character until it knows that the next character is not a mark that could combine
with the base character.)
217
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
g_iconv ()
Same as the standard UNIX routine iconv(), but may be implemented via libiconv on UNIX flavors
that lack a native implementation.
GLib provides g_convert() and g_locale_to_utf8() which are likely more convenient than the raw
iconv wrappers.
converter : conversion descriptor from g_iconv_open()
g_iconv_close ()
Same as the standard UNIX routine iconv_close(), but may be implemented via libiconv on UNIX
flavors that lack a native implementation. Should be called to clean up the conversion descriptor from
g_iconv_open() when you are done converting things.
GLib provides g_convert() and g_locale_to_utf8() which are likely more convenient than the raw
iconv wrappers.
converter : a conversion descriptor from g_iconv_open()
g_locale_to_utf8 ()
Converts a string which is in the encoding used for strings by the C runtime (usually the same as
that used by the operating system) in the current locale into a UTF-8 string.
opsysstring : a string in the encoding of the current locale. On Windows this means the system code-
page.
len : the length of the string, or -1 if the string is nul-terminated1 .
bytes_read : location to store the number of bytes in the input string that were successfully converted,
or NULL. Even if the conversion was successful, this may be less than len if there were partial
characters at the end of the input. If the error G_CONVERT_ERROR_ILLEGAL_SEQUENCE oc-
curs, the value stored will the byte offset after the last valid input sequence.
218
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
bytes_written : the number of bytes stored in the output buffer (not including the terminating nul).
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
g_filename_to_utf8 ()
Converts a string which is in the encoding used by GLib for filenames into a UTF-8 string. Note that
on Windows GLib uses UTF-8 for filenames; on other platforms, this function indirectly depends on the
current locale.
bytes_read : location to store the number of bytes in the input string that were successfully converted,
or NULL. Even if the conversion was successful, this may be less than len if there were partial
characters at the end of the input. If the error G_CONVERT_ERROR_ILLEGAL_SEQUENCE oc-
curs, the value stored will the byte offset after the last valid input sequence.
bytes_written : the number of bytes stored in the output buffer (not including the terminating nul).
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
g_filename_from_utf8 ()
Converts a string from UTF-8 to the encoding GLib uses for filenames. Note that on Windows GLib
uses UTF-8 for filenames; on other platforms, this function indirectly depends on the current locale.
bytes_read : location to store the number of bytes in the input string that were successfully converted,
or NULL. Even if the conversion was successful, this may be less than len if there were partial
characters at the end of the input. If the error G_CONVERT_ERROR_ILLEGAL_SEQUENCE oc-
curs, the value stored will the byte offset after the last valid input sequence.
bytes_written : the number of bytes stored in the output buffer (not including the terminating nul).
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
219
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
g_filename_from_uri ()
Converts an escaped ASCII-encoded URI to a local filename in the encoding used for filenames.
hostname : Location to store hostname for the URI, or NULL. If there is no hostname in the URI, NULL
will be stored in this location.
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
g_filename_to_uri ()
Converts an absolute filename to an escaped ASCII-encoded URI, with the path component follow-
ing Section 3.3. of RFC 2396.
filename : an absolute filename specified in the GLib file name encoding, which is the on-disk file name
bytes on Unix, and UTF-8 on Windows
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
g_get_filename_charsets ()
Determines the preferred character sets used for filenames. The first character set from the charsets
is the filename encoding, the subsequent character sets are used when trying to generate a displayable
representation of a filename, see g_filename_display_name().
On Unix, the character sets are determined by consulting the environment variables G_FILENAME_-
ENCODING and G_BROKEN_FILENAMES. On Windows, the character set used in the GLib API is always
UTF-8 and said environment variables have no effect.
G_FILENAME_ENCODING may be set to a comma-separated list of character set names. The special
token "@locale" is taken to mean the character set for the current locale. If G_FILENAME_ENCODING is
not set, but G_BROKEN_FILENAMES is, the character set of the current locale is taken as the filename en-
coding. If neither environment variable is set, UTF-8 is taken as the filename encoding, but the character
set of the current locale is also put in the list of encodings.
The returned charsets belong to GLib and must not be freed.
Note that on Unix, regardless of the locale character set or G_FILENAME_ENCODING value, the actual
file names present on a system might be in any random encoding or just gibberish.
Since 2.6
220
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
g_filename_display_name ()
Converts a filename into a valid UTF-8 string. The conversion is not necessarily reversible, so you
should keep the original around and use the return value of this function only for display purposes.
Unlike g_filename_to_utf8(), the result is guaranteed to be non-NULL even if the filename actually isn’t
in the GLib file name encoding.
If GLib can not make sense of the encoding of filename, as a last resort it replaces unknown charac-
ters with U+FFFD, the Unicode replacement character. You can search the result for the UTF-8 encoding
of this character (which is "\357\277\275" in octal notation) to find out if filename was in an invalid
encoding.
If you know the whole pathname of the file you should use g_filename_display_basename(), since
that allows location-based translation of filenames.
Returns : a newly allocated string containing a rendition of the filename in valid UTF-8
Since 2.6
g_filename_display_basename ()
Returns the display basename for the particular filename, guaranteed to be valid UTF-8. The display
name might not be identical to the filename, for instance there might be problems converting it to UTF-8,
and some files can be translated in the display.
If GLib can not make sense of the encoding of filename, as a last resort it replaces unknown charac-
ters with U+FFFD, the Unicode replacement character. You can search the result for the UTF-8 encoding
of this character (which is "\357\277\275" in octal notation) to find out if filename was in an invalid
encoding.
You must pass the whole absolute pathname to this functions so that translation of well known
locations can be done.
This function is preferred over g_filename_display_name() if you know the whole path, as it allows
translation.
Returns : a newly allocated string containing a rendition of the basename of the filename in valid UTF-8
Since 2.6
g_uri_list_extract_uris ()
Splits an URI list conforming to the text/uri-list mime type defined in RFC 2483 into individual URIs,
discarding any comments. The URIs are not validated.
Returns : a newly allocated NULL-terminated list of strings holding the individual URIs. The array
should be freed with g_strfreev().
Since 2.6
221
CHAPTER 4. GLIB UTILITIES 4.2. CHARACTER SET CONVERSION
g_locale_from_utf8 ()
Converts a string from UTF-8 to the encoding used for strings by the C runtime (usually the same as
that used by the operating system) in the current locale. On Windows this means the system codepage.
utf8string : a UTF-8 encoded string
bytes_read : location to store the number of bytes in the input string that were successfully converted,
or NULL. Even if the conversion was successful, this may be less than len if there were partial
characters at the end of the input. If the error G_CONVERT_ERROR_ILLEGAL_SEQUENCE oc-
curs, the value stored will the byte offset after the last valid input sequence.
bytes_written : the number of bytes stored in the output buffer (not including the terminating nul).
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror may occur.
Returns : The converted string, or NULL on an error.
enum GConvertError
typedef enum
{
G_CONVERT_ERROR_NO_CONVERSION,
G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
G_CONVERT_ERROR_FAILED,
G_CONVERT_ERROR_PARTIAL_INPUT,
G_CONVERT_ERROR_BAD_URI,
G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
} GConvertError;
g_get_charset ()
Obtains the character set for the current locale; you might use this character set as an argument
to g_convert(), to convert from the current locale’s encoding to some other encoding. (Frequently
g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts, though.)
On Windows the character set returned by this function is the so-called system default ANSI code-
page. That is the character set used by the "narrow" versions of C library and Win32 functions that
handle file names. It might be different from the character set used by the C library’s current locale.
222
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
The return value is TRUE if the locale’s encoding is UTF-8, in that case you can perhaps avoid calling
g_convert().
The string returned in charset is not allocated, and should not be freed.
Synopsis
#include <glib.h>
typedef gunichar;
typedef gunichar2;
223
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
224
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
glong *items_written);
gunichar * g_utf16_to_ucs4 (const gunichar2 *str,
glong len,
glong *items_read,
glong *items_written,
GError **error);
gchar* g_utf16_to_utf8 (const gunichar2 *str,
glong len,
glong *items_read,
glong *items_written,
GError **error);
gunichar2 * g_ucs4_to_utf16 (const gunichar *str,
glong len,
glong *items_read,
glong *items_written,
GError **error);
gchar* g_ucs4_to_utf8 (const gunichar *str,
glong len,
glong *items_read,
glong *items_written,
GError **error);
gint g_unichar_to_utf8 (gunichar c,
gchar *outbuf);
Description
This section describes a number of functions for dealing with Unicode characters and strings. There
are analogues of the traditional ctype.h character classification and case conversion functions, UTF-
8 analogues of some string utility functions, functions to perform normalization, case conversion and
collation on UTF-8 strings and finally functions to convert between the UTF-8, UTF-16 and UCS-4 en-
codings of Unicode.
The implementations of the Unicode functions in GLib are based on the Unicode Character Data
tables, which are available from www.unicode.org. GLib 2.8 supports Unicode 4.0, GLib 2.10 supports
Unicode 4.1, GLib 2.12 supports Unicode 5.0, GLib 2.16.3 supports Unicode 5.1.
Details
gunichar
A type which can hold any UTF-32 or UCS-4 character code, also known as a Unicode code point.
To print/scan values of this type to/from text you need to convert to/from UTF-8, using g_utf32_to_utf8()/g_utf8_
To print/scan values of this type as integer, use G_GINT32_MODIFIER and/or G_GUINT32_FORMAT.
The notation to express a Unicode code point in running text is as a hexadecimal number with four to
six digits and uppercase letters, prefixed by the string "U+". Leading zeros are omitted, unless the code
point would have fewer than four hexadecimal digits. For example, "U+0041 LATIN CAPITAL LETTER
A". To print a code point in the U+-notation, use the format string "U+04"G_GINT32_FORMAT"X". To
scan, use the format string "U+06"G_GINT32_FORMAT"X".
gunichar c;
sscanf ("U+0041", "U+%06"G_GINT32_FORMAT"X", &c)
g_print ("Read U+%04"G_GINT32_FORMAT"X", c);
gunichar2
225
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_validate ()
Checks whether ch is a valid Unicode character. Some possible integer values of ch will not be valid.
0 is considered a valid character, though it’s normally a string terminator.
ch : a Unicode character
g_unichar_isalnum ()
Determines whether a character is alphanumeric. Given some UTF-8 text, obtain a character value
with g_utf8_get_char().
c : a Unicode character
g_unichar_isalpha ()
Determines whether a character is alphabetic (i.e. a letter). Given some UTF-8 text, obtain a character
value with g_utf8_get_char().
c : a Unicode character
g_unichar_iscntrl ()
Determines whether a character is a control character. Given some UTF-8 text, obtain a character
value with g_utf8_get_char().
c : a Unicode character
g_unichar_isdefined ()
c : a Unicode character
cannot be stored in a single gunichar2 field, but all GLib functions accepting gunichar2 arrays will correctly interpret surrogate
pairs.
226
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_isdigit ()
Determines whether a character is numeric (i.e. a digit). This covers ASCII 0-9 and also digits in
other languages/scripts. Given some UTF-8 text, obtain a character value with g_utf8_get_char().
c : a Unicode character
g_unichar_isgraph ()
Determines whether a character is printable and not a space (returns FALSE for control characters,
format characters, and spaces). g_unichar_isprint() is similar, but returns TRUE for spaces. Given some
UTF-8 text, obtain a character value with g_utf8_get_char().
c : a Unicode character
g_unichar_islower ()
Determines whether a character is a lowercase letter. Given some UTF-8 text, obtain a character value
with g_utf8_get_char().
c : a Unicode character
g_unichar_ismark ()
Determines whether a character is a mark (non-spacing mark, combining mark, or enclosing mark
in Unicode speak). Given some UTF-8 text, obtain a character value with g_utf8_get_char().
Note: in most cases where isalpha characters are allowed, ismark characters should be allowed to as
they are essential for writing most European languages as well as many non-Latin scripts.
c : a Unicode character
Since 2.14
g_unichar_isprint ()
Determines whether a character is printable. Unlike g_unichar_isgraph(), returns TRUE for spaces.
Given some UTF-8 text, obtain a character value with g_utf8_get_char().
c : a Unicode character
227
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_ispunct ()
Determines whether a character is punctuation or a symbol. Given some UTF-8 text, obtain a char-
acter value with g_utf8_get_char().
c : a Unicode character
g_unichar_isspace ()
Determines whether a character is a space, tab, or line separator (newline, carriage return, etc.).
Given some UTF-8 text, obtain a character value with g_utf8_get_char().
(Note: don’t use this to do word breaking; you have to use Pango or equivalent to get word breaking
right, the algorithm is fairly complex.)
c : a Unicode character
g_unichar_istitle ()
Determines if a character is titlecase. Some characters in Unicode which are composites, such as the
DZ digraph have three case variants instead of just two. The titlecase form is used at the beginning of
a word where only the first letter is capitalized. The titlecase form of the DZ digraph is U+01F2 LATIN
CAPITAL LETTTER D WITH SMALL LETTER Z.
c : a Unicode character
g_unichar_isupper ()
g_unichar_isxdigit ()
g_unichar_iswide ()
228
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_iswide_cjk ()
Determines if a character is typically rendered in a double-width cell under legacy East Asian locales.
If a character is wide according to g_unichar_iswide(), then it is also reported wide with this function,
but the converse is not necessarily true. See the Unicode Standard Annex #11 for details.
If a character passes the g_unichar_iswide() test then it will also pass this test, but not the other way
around. Note that some characters may pas both this test and g_unichar_iszerowidth().
c : a Unicode character
g_unichar_iszerowidth ()
Determines if a given character typically takes zero width when rendered. The return value is TRUE
for all non-spacing and enclosing marks (e.g., combining accents), format characters, zero-width space,
but not U+00AD SOFT HYPHEN.
A typical use of this function is with one of g_unichar_iswide() or g_unichar_iswide_cjk() to deter-
mine the number of cells a string occupies when displayed on a grid display (terminals). However, note
that not all terminals support zero-width rendering of zero-width marks.
c : a Unicode character
g_unichar_toupper ()
Returns : the result of converting c to uppercase. If c is not an lowercase or titlecase character, or has
no upper case equivalent c is returned unchanged.
g_unichar_tolower ()
Returns : the result of converting c to lower case. If c is not an upperlower or titlecase character, or has
no lowercase equivalent c is returned unchanged.
g_unichar_totitle ()
229
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_digit_value ()
c : a Unicode character
Returns : If c is a decimal digit (according to g_unichar_isdigit()), its numeric value. Otherwise, -1.
g_unichar_xdigit_value ()
c : a Unicode character
Returns : If c is a hex digit (according to g_unichar_isxdigit()), its numeric value. Otherwise, -1.
enum GUnicodeType
typedef enum
{
G_UNICODE_CONTROL,
G_UNICODE_FORMAT,
G_UNICODE_UNASSIGNED,
G_UNICODE_PRIVATE_USE,
G_UNICODE_SURROGATE,
G_UNICODE_LOWERCASE_LETTER,
G_UNICODE_MODIFIER_LETTER,
G_UNICODE_OTHER_LETTER,
G_UNICODE_TITLECASE_LETTER,
G_UNICODE_UPPERCASE_LETTER,
G_UNICODE_COMBINING_MARK,
G_UNICODE_ENCLOSING_MARK,
G_UNICODE_NON_SPACING_MARK,
G_UNICODE_DECIMAL_NUMBER,
G_UNICODE_LETTER_NUMBER,
G_UNICODE_OTHER_NUMBER,
G_UNICODE_CONNECT_PUNCTUATION,
G_UNICODE_DASH_PUNCTUATION,
G_UNICODE_CLOSE_PUNCTUATION,
G_UNICODE_FINAL_PUNCTUATION,
G_UNICODE_INITIAL_PUNCTUATION,
G_UNICODE_OTHER_PUNCTUATION,
G_UNICODE_OPEN_PUNCTUATION,
G_UNICODE_CURRENCY_SYMBOL,
G_UNICODE_MODIFIER_SYMBOL,
G_UNICODE_MATH_SYMBOL,
G_UNICODE_OTHER_SYMBOL,
G_UNICODE_LINE_SEPARATOR,
G_UNICODE_PARAGRAPH_SEPARATOR,
G_UNICODE_SPACE_SEPARATOR
} GUnicodeType;
These are the possible character classifications from the Unicode specification. See http://www.unicode.org/-
Public/UNIDATA/UnicodeData.html.
230
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_type ()
c : a Unicode character
231
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
enum GUnicodeBreakType
typedef enum
{
G_UNICODE_BREAK_MANDATORY,
G_UNICODE_BREAK_CARRIAGE_RETURN,
G_UNICODE_BREAK_LINE_FEED,
G_UNICODE_BREAK_COMBINING_MARK,
G_UNICODE_BREAK_SURROGATE,
G_UNICODE_BREAK_ZERO_WIDTH_SPACE,
G_UNICODE_BREAK_INSEPARABLE,
G_UNICODE_BREAK_NON_BREAKING_GLUE,
G_UNICODE_BREAK_CONTINGENT,
G_UNICODE_BREAK_SPACE,
G_UNICODE_BREAK_AFTER,
G_UNICODE_BREAK_BEFORE,
G_UNICODE_BREAK_BEFORE_AND_AFTER,
G_UNICODE_BREAK_HYPHEN,
G_UNICODE_BREAK_NON_STARTER,
G_UNICODE_BREAK_OPEN_PUNCTUATION,
G_UNICODE_BREAK_CLOSE_PUNCTUATION,
G_UNICODE_BREAK_QUOTATION,
G_UNICODE_BREAK_EXCLAMATION,
G_UNICODE_BREAK_IDEOGRAPHIC,
G_UNICODE_BREAK_NUMERIC,
G_UNICODE_BREAK_INFIX_SEPARATOR,
G_UNICODE_BREAK_SYMBOL,
G_UNICODE_BREAK_ALPHABETIC,
G_UNICODE_BREAK_PREFIX,
G_UNICODE_BREAK_POSTFIX,
G_UNICODE_BREAK_COMPLEX_CONTEXT,
G_UNICODE_BREAK_AMBIGUOUS,
G_UNICODE_BREAK_UNKNOWN,
G_UNICODE_BREAK_NEXT_LINE,
G_UNICODE_BREAK_WORD_JOINER,
G_UNICODE_BREAK_HANGUL_L_JAMO,
G_UNICODE_BREAK_HANGUL_V_JAMO,
G_UNICODE_BREAK_HANGUL_T_JAMO,
G_UNICODE_BREAK_HANGUL_LV_SYLLABLE,
G_UNICODE_BREAK_HANGUL_LVT_SYLLABLE
} GUnicodeBreakType;
These are the possible line break classifications. The five Hangul types were added in Unicode 4.1, so,
has been introduced in GLib 2.10. Note that new types may be added in the future. Applications should
be ready to handle unknown values. They may be regarded as G_UNICODE_BREAK_UNKNOWN.
See http://www.unicode.org/unicode/reports/tr14/.
G_UNICODE_BREAK_MANDATORY Mandatory Break (BK)
G_UNICODE_BREAK_CARRIAGE_RETURN Carriage Return (CR)
G_UNICODE_BREAK_LINE_FEED Line Feed (LF)
G_UNICODE_BREAK_COMBINING_MARK Attached Characters and Combining Marks (CM)
G_UNICODE_BREAK_SURROGATE Surrogates (SG)
G_UNICODE_BREAK_ZERO_WIDTH_SPACE Zero Width Space (ZW)
G_UNICODE_BREAK_INSEPARABLE Inseparable (IN)
G_UNICODE_BREAK_NON_BREAKING_GLUE Non-breaking ("Glue") (GL)
G_UNICODE_BREAK_CONTINGENT Contingent Break Opportunity (CB)
G_UNICODE_BREAK_SPACE Space (SP)
232
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_break_type ()
Determines the break type of c. c should be a Unicode character (to derive a character from UTF-8
encoded text, use g_utf8_get_char()). The break type is used to find word and line breaks ("text bound-
aries"), Pango implements the Unicode boundary resolution algorithms and normally you would use a
function such as pango_break() instead of caring about break types yourself.
c : a Unicode character
233
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_unichar_combining_class ()
uc : a Unicode character
Since 2.14
g_unicode_canonical_ordering ()
Computes the canonical ordering of a string in-place. This rearranges decomposed characters in the
string according to their combining classes. See the Unicode manual for more information.
g_unicode_canonical_decomposition ()
ch : a Unicode character.
Returns : a newly allocated string of Unicode characters. result_len is set to the resulting length of
the string.
g_unichar_get_mirror_char ()
In Unicode, some characters are mirrored. This means that their images are mirrored horizontally in
text that is laid out from right to left. For instance, "(" would become its mirror image, ")", in right-to-left
text.
If ch has the Unicode mirrored property and there is another unicode character that typically has a
glyph that is the mirror image of ch’s glyph and mirrored_ch is set, it puts that character in the address
pointed to by mirrored_ch. Otherwise the original character is put.
ch : a Unicode character
Since 2.4
234
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
enum GUnicodeScript
typedef enum
{ /* ISO 15924 code */
G_UNICODE_SCRIPT_INVALID_CODE = -1,
G_UNICODE_SCRIPT_COMMON = 0, /* Zyyy */
G_UNICODE_SCRIPT_INHERITED, /* Qaai */
G_UNICODE_SCRIPT_ARABIC, /* Arab */
G_UNICODE_SCRIPT_ARMENIAN, /* Armn */
G_UNICODE_SCRIPT_BENGALI, /* Beng */
G_UNICODE_SCRIPT_BOPOMOFO, /* Bopo */
G_UNICODE_SCRIPT_CHEROKEE, /* Cher */
G_UNICODE_SCRIPT_COPTIC, /* Qaac */
G_UNICODE_SCRIPT_CYRILLIC, /* Cyrl (Cyrs) */
G_UNICODE_SCRIPT_DESERET, /* Dsrt */
G_UNICODE_SCRIPT_DEVANAGARI, /* Deva */
G_UNICODE_SCRIPT_ETHIOPIC, /* Ethi */
G_UNICODE_SCRIPT_GEORGIAN, /* Geor (Geon, Geoa) */
G_UNICODE_SCRIPT_GOTHIC, /* Goth */
G_UNICODE_SCRIPT_GREEK, /* Grek */
G_UNICODE_SCRIPT_GUJARATI, /* Gujr */
G_UNICODE_SCRIPT_GURMUKHI, /* Guru */
G_UNICODE_SCRIPT_HAN, /* Hani */
G_UNICODE_SCRIPT_HANGUL, /* Hang */
G_UNICODE_SCRIPT_HEBREW, /* Hebr */
G_UNICODE_SCRIPT_HIRAGANA, /* Hira */
G_UNICODE_SCRIPT_KANNADA, /* Knda */
G_UNICODE_SCRIPT_KATAKANA, /* Kana */
G_UNICODE_SCRIPT_KHMER, /* Khmr */
G_UNICODE_SCRIPT_LAO, /* Laoo */
G_UNICODE_SCRIPT_LATIN, /* Latn (Latf, Latg) */
G_UNICODE_SCRIPT_MALAYALAM, /* Mlym */
G_UNICODE_SCRIPT_MONGOLIAN, /* Mong */
G_UNICODE_SCRIPT_MYANMAR, /* Mymr */
G_UNICODE_SCRIPT_OGHAM, /* Ogam */
G_UNICODE_SCRIPT_OLD_ITALIC, /* Ital */
G_UNICODE_SCRIPT_ORIYA, /* Orya */
G_UNICODE_SCRIPT_RUNIC, /* Runr */
G_UNICODE_SCRIPT_SINHALA, /* Sinh */
G_UNICODE_SCRIPT_SYRIAC, /* Syrc (Syrj, Syrn, Syre) */
G_UNICODE_SCRIPT_TAMIL, /* Taml */
G_UNICODE_SCRIPT_TELUGU, /* Telu */
G_UNICODE_SCRIPT_THAANA, /* Thaa */
G_UNICODE_SCRIPT_THAI, /* Thai */
G_UNICODE_SCRIPT_TIBETAN, /* Tibt */
G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL, /* Cans */
G_UNICODE_SCRIPT_YI, /* Yiii */
G_UNICODE_SCRIPT_TAGALOG, /* Tglg */
G_UNICODE_SCRIPT_HANUNOO, /* Hano */
G_UNICODE_SCRIPT_BUHID, /* Buhd */
G_UNICODE_SCRIPT_TAGBANWA, /* Tagb */
/* Unicode-4.0 additions */
G_UNICODE_SCRIPT_BRAILLE, /* Brai */
G_UNICODE_SCRIPT_CYPRIOT, /* Cprt */
G_UNICODE_SCRIPT_LIMBU, /* Limb */
G_UNICODE_SCRIPT_OSMANYA, /* Osma */
G_UNICODE_SCRIPT_SHAVIAN, /* Shaw */
G_UNICODE_SCRIPT_LINEAR_B, /* Linb */
G_UNICODE_SCRIPT_TAI_LE, /* Tale */
G_UNICODE_SCRIPT_UGARITIC, /* Ugar */
/* Unicode-4.1 additions */
G_UNICODE_SCRIPT_NEW_TAI_LUE, /* Talu */
235
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
G_UNICODE_SCRIPT_BUGINESE, /* Bugi */
G_UNICODE_SCRIPT_GLAGOLITIC, /* Glag */
G_UNICODE_SCRIPT_TIFINAGH, /* Tfng */
G_UNICODE_SCRIPT_SYLOTI_NAGRI, /* Sylo */
G_UNICODE_SCRIPT_OLD_PERSIAN, /* Xpeo */
G_UNICODE_SCRIPT_KHAROSHTHI, /* Khar */
/* Unicode-5.0 additions */
G_UNICODE_SCRIPT_UNKNOWN, /* Zzzz */
G_UNICODE_SCRIPT_BALINESE, /* Bali */
G_UNICODE_SCRIPT_CUNEIFORM, /* Xsux */
G_UNICODE_SCRIPT_PHOENICIAN, /* Phnx */
G_UNICODE_SCRIPT_PHAGS_PA, /* Phag */
G_UNICODE_SCRIPT_NKO, /* Nkoo */
/* Unicode-5.1 additions */
G_UNICODE_SCRIPT_KAYAH_LI, /* Kali */
G_UNICODE_SCRIPT_LEPCHA, /* Lepc */
G_UNICODE_SCRIPT_REJANG, /* Rjng */
G_UNICODE_SCRIPT_SUNDANESE, /* Sund */
G_UNICODE_SCRIPT_SAURASHTRA, /* Saur */
G_UNICODE_SCRIPT_CHAM, /* Cham */
G_UNICODE_SCRIPT_OL_CHIKI, /* Olck */
G_UNICODE_SCRIPT_VAI, /* Vaii */
G_UNICODE_SCRIPT_CARIAN, /* Cari */
G_UNICODE_SCRIPT_LYCIAN, /* Lyci */
G_UNICODE_SCRIPT_LYDIAN /* Lydi */
} GUnicodeScript;
The GUnicodeScript enumeration identifies different writing systems. The values correspond to
the names as defined in the Unicode standard. The enumeration has been added in GLib 2.14, and
is interchangeable with PangoScript. Note that new types may be added in the future. Applications
should be ready to handle unknown values. See Unicode Standard Annex #24: Script names.
G_UNICODE_SCRIPT_INHERITED a mark glyph that takes its script from the base glyph to which it is
attached
G_UNICODE_SCRIPT_ARABIC Arabic
G_UNICODE_SCRIPT_ARMENIAN Armenian
G_UNICODE_SCRIPT_BENGALI Bengali
G_UNICODE_SCRIPT_BOPOMOFO Bopomofo
G_UNICODE_SCRIPT_CHEROKEE Cherokee
G_UNICODE_SCRIPT_COPTIC Coptic
G_UNICODE_SCRIPT_CYRILLIC Cyrillic
G_UNICODE_SCRIPT_DESERET Deseret
G_UNICODE_SCRIPT_DEVANAGARI Devanagari
G_UNICODE_SCRIPT_ETHIOPIC Ethiopic
G_UNICODE_SCRIPT_GEORGIAN Georgian
G_UNICODE_SCRIPT_GOTHIC Gothic
G_UNICODE_SCRIPT_GREEK Greek
236
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
G_UNICODE_SCRIPT_GUJARATI Gujarati
G_UNICODE_SCRIPT_GURMUKHI Gurmukhi
G_UNICODE_SCRIPT_HAN Han
G_UNICODE_SCRIPT_HANGUL Hangul
G_UNICODE_SCRIPT_HEBREW Hebrew
G_UNICODE_SCRIPT_HIRAGANA Hiragana
G_UNICODE_SCRIPT_KANNADA Kannada
G_UNICODE_SCRIPT_KATAKANA Katakana
G_UNICODE_SCRIPT_KHMER Khmer
G_UNICODE_SCRIPT_LAO Lao
G_UNICODE_SCRIPT_LATIN Latin
G_UNICODE_SCRIPT_MALAYALAM Malayalam
G_UNICODE_SCRIPT_MONGOLIAN Mongolian
G_UNICODE_SCRIPT_MYANMAR Myanmar
G_UNICODE_SCRIPT_OGHAM Ogham
G_UNICODE_SCRIPT_OLD_ITALIC Old Italic
G_UNICODE_SCRIPT_ORIYA Oriya
G_UNICODE_SCRIPT_RUNIC Runic
G_UNICODE_SCRIPT_SINHALA Sinhala
G_UNICODE_SCRIPT_SYRIAC Syriac
G_UNICODE_SCRIPT_TAMIL Tamil
G_UNICODE_SCRIPT_TELUGU Telugu
G_UNICODE_SCRIPT_THAANA Thaana
G_UNICODE_SCRIPT_THAI Thai
G_UNICODE_SCRIPT_TIBETAN Tibetan
G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL Canadian Aboriginal
G_UNICODE_SCRIPT_YI Yi
G_UNICODE_SCRIPT_TAGALOG Tagalog
G_UNICODE_SCRIPT_HANUNOO Hanunoo
G_UNICODE_SCRIPT_BUHID Buhid
G_UNICODE_SCRIPT_TAGBANWA Tagbanwa
G_UNICODE_SCRIPT_BRAILLE Braille
G_UNICODE_SCRIPT_CYPRIOT Cypriot
G_UNICODE_SCRIPT_LIMBU Limbu
G_UNICODE_SCRIPT_OSMANYA Osmanya
G_UNICODE_SCRIPT_SHAVIAN Shavian
237
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
G_UNICODE_SCRIPT_LINEAR_B Linear B
G_UNICODE_SCRIPT_TAI_LE Tai Le
G_UNICODE_SCRIPT_UGARITIC Ugaritic
G_UNICODE_SCRIPT_BUGINESE Buginese
G_UNICODE_SCRIPT_GLAGOLITIC Glagolitic
G_UNICODE_SCRIPT_TIFINAGH Tifinagh
G_UNICODE_SCRIPT_KHAROSHTHI Kharoshthi
G_UNICODE_SCRIPT_BALINESE Balinese
G_UNICODE_SCRIPT_CUNEIFORM Cuneiform
G_UNICODE_SCRIPT_PHOENICIAN Phoenician
G_UNICODE_SCRIPT_PHAGS_PA Phags-pa
G_UNICODE_SCRIPT_NKO N’Ko
g_unichar_get_script ()
Looks up the GUnicodeScript for a particular character (as defined by Unicode Standard Annex 24).
No check is made for ch being a valid Unicode character; if you pass in invalid character, the result is
undefined.
This function is equivalent to pango_script_for_unichar() and the two are interchangeable.
ch : a Unicode character
Since 2.14
238
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_utf8_next_char()
#define g_utf8_next_char(p)
Skips to the next character in a UTF-8 string. The string must be valid; this macro is as fast as possible,
and has no error-checking. You would use this macro to iterate over a string character by character. The
macro returns the start of the next UTF-8 character. Before using this macro, use g_utf8_validate() to
validate strings that may contain invalid UTF-8.
g_utf8_get_char ()
Converts a sequence of bytes encoded as UTF-8 to a Unicode character. If p does not point to a valid
UTF-8 encoded character, results are undefined. If you are not sure that the bytes are complete valid
Unicode characters, you should use g_utf8_get_char_validated() instead.
g_utf8_get_char_validated ()
Convert a sequence of bytes encoded as UTF-8 to a Unicode character. This function checks for
incomplete characters, for invalid characters such as characters that are out of the range of Unicode, and
for overlong encodings of valid characters.
max_len : the maximum number of bytes to read, or -1, for no maximum or if p is nul-terminated
Returns : the resulting character. If p points to a partial sequence at the end of a string that could begin
a valid character (or if max_len is zero), returns (gunichar)-2; otherwise, if p does not point to a
valid UTF-8 encoded Unicode character, returns (gunichar)-1.
g_utf8_offset_to_pointer ()
Converts from an integer character offset to a pointer to a position within the string.
Since 2.10, this function allows to pass a negative offset to step backwards. It is usually worth
stepping backwards from the end instead of forwards if offset is in the last fourth of the string, since
moving forward is about 3 times faster than moving backward.
N OTE
This function doesn’t abort when reaching the end of str . Therefore you should be sure
that offset is within string boundaries before calling that function. Call g_utf8_strlen()
when unsure.
This limitation exists as this function is called frequently during text rendering and there-
fore has to be as fast as possible.
239
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_utf8_pointer_to_offset ()
g_utf8_prev_char ()
g_utf8_find_next_char ()
Finds the start of the next UTF-8 character in the string after p.
p does not have to be at the beginning of a UTF-8 character. No check is made to see if the character
found is actually valid other than it starts with an appropriate byte.
p : a pointer to a position within a UTF-8 encoded string
end : a pointer to the byte following the end of the string, or NULL to indicate that the string is nul-
terminated.
Returns : a pointer to the found character or NULL
g_utf8_find_prev_char ()
Given a position p with a UTF-8 encoded string str , find the start of the previous UTF-8 character
starting before p. Returns NULL if no UTF-8 characters are present in str before p.
p does not have to be at the beginning of a UTF-8 character. No check is made to see if the character
found is actually valid other than it starts with an appropriate byte.
str : pointer to the beginning of a UTF-8 encoded string
240
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_utf8_strlen ()
max : the maximum number of bytes to examine. If max is less than 0, then the string is assumed to be
nul-terminated. If max is 0, p will not be examined and may be NULL.
Returns : the length of the string in characters
g_utf8_strncpy ()
Like the standard C strncpy() function, but copies a given number of characters instead of a given
number of bytes. The src string must be valid UTF-8 encoded text. (Use g_utf8_validate() on all text
before trying to use UTF-8 utility functions with it.)
dest : buffer to fill with characters from src
n : character count
Returns : dest
g_utf8_strchr ()
Finds the leftmost occurrence of the given Unicode character in a UTF-8 encoded string, while limit-
ing the search to len bytes. If len is -1, allow unbounded search.
p : a nul-terminated UTF-8 encoded string
c : a Unicode character
Returns : NULL if the string does not contain the character, otherwise, a pointer to the start of the
leftmost occurrence of the character in the string.
g_utf8_strrchr ()
Find the rightmost occurrence of the given Unicode character in a UTF-8 encoded string, while lim-
iting the search to len bytes. If len is -1, allow unbounded search.
p : a nul-terminated UTF-8 encoded string
c : a Unicode character
Returns : NULL if the string does not contain the character, otherwise, a pointer to the start of the
rightmost occurrence of the character in the string.
241
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_utf8_strreverse ()
Reverses a UTF-8 string. str must be valid UTF-8 encoded text. (Use g_utf8_validate() on all text
before trying to use UTF-8 utility functions with it.)
This function is intended for programmatic uses of reversed strings. It pays no attention to de-
composed characters, combining marks, byte order marks, directional indicators (LRM, LRO, etc) and
similar characters which might need special handling when reversing a string for display purposes.
Note that unlike g_strreverse(), this function returns newly-allocated memory, which should be freed
with g_free() when no longer needed.
len : the maximum length of str to use, in bytes. If len < 0, then the string is nul-terminated.
Since 2.2
g_utf8_validate ()
Validates UTF-8 encoded text. str is the text to validate; if str is nul-terminated, then max_len can
be -1, otherwise max_len should be the number of bytes to validate. If end is non-NULL, then the end of
the valid range will be stored there (i.e. the start of the first invalid character if some bytes were invalid,
or the end of the text being validated otherwise).
Note that g_utf8_validate() returns FALSE if max_len is positive and NUL is met before max_len
bytes have been read.
Returns TRUE if all of str was valid. Many GLib and GTK+ routines require valid UTF-8 as input;
so data read from a file or the network should be checked with g_utf8_validate() before doing anything
else with it.
g_utf8_strup ()
Converts all Unicode characters in the string that have a case to uppercase. The exact manner that
this is done depends on the current locale, and may result in the number of characters in the string
increasing. (For instance, the German ess-zet will be changed to SS.)
242
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_utf8_strdown ()
Converts all Unicode characters in the string that have a case to lowercase. The exact manner that
this is done depends on the current locale, and may result in the number of characters in the string
changing.
g_utf8_casefold ()
Converts a string into a form that is independent of case. The result will not correspond to any
particular case, but can be compared for equality or ordered with the results of calling g_utf8_casefold()
on other strings.
Note that calling g_utf8_casefold() followed by g_utf8_collate() is only an approximation to the cor-
rect linguistic case insensitive ordering, though it is a fairly good one. Getting this exactly right would
require a more sophisticated collation function that takes case sensitivity into account. GLib does not
currently provide such a function.
g_utf8_normalize ()
Converts a string into canonical form, standardizing such issues as whether a character with an
accent is represented as a base character and combining accent or as a single precomposed character. The
string has to be valid UTF-8, otherwise NULL is returned. You should generally call g_utf8_normalize()
before comparing two Unicode strings.
The normalization mode G_NORMALIZE_DEFAULT only standardizes differences that do not af-
fect the text content, such as the above-mentioned accent representation. G_NORMALIZE_ALL also
standardizes the "compatibility" characters in Unicode, such as SUPERSCRIPT THREE to the standard
forms (in this case DIGIT THREE). Formatting information may be lost but for most text operations such
characters should be considered the same.
G_NORMALIZE_DEFAULT_COMPOSE and G_NORMALIZE_ALL_COMPOSE are like G_NORMALIZE_DEFA
and G_NORMALIZE_ALL, but returned a result with composed forms rather than a maximally decom-
posed form. This is often useful if you intend to convert the string to a legacy encoding or pass it to a
system with less capable Unicode handling.
Returns : a newly allocated string, that is the normalized form of str , or NULL if str is not valid UTF-8.
243
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
enum GNormalizeMode
typedef enum {
G_NORMALIZE_DEFAULT,
G_NORMALIZE_NFD = G_NORMALIZE_DEFAULT,
G_NORMALIZE_DEFAULT_COMPOSE,
G_NORMALIZE_NFC = G_NORMALIZE_DEFAULT_COMPOSE,
G_NORMALIZE_ALL,
G_NORMALIZE_NFKD = G_NORMALIZE_ALL,
G_NORMALIZE_ALL_COMPOSE,
G_NORMALIZE_NFKC = G_NORMALIZE_ALL_COMPOSE
} GNormalizeMode;
Defines how a Unicode string is transformed in a canonical form, standardizing such issues as
whether a character with an accent is represented as a base character and combining accent or as a
single precomposed character. Unicode strings should generally be normalized before comparing them.
G_NORMALIZE_DEFAULT standardize differences that do not affect the text content, such as the above-
mentioned accent representation.
G_NORMALIZE_NFD another name for G_NORMALIZE_DEFAULT.
G_NORMALIZE_DEFAULT_COMPOSE like G_NORMALIZE_DEFAULT, but with composed forms rather
than a maximally decomposed form.
G_NORMALIZE_NFC another name for G_NORMALIZE_DEFAULT_COMPOSE.
G_NORMALIZE_ALL beyond G_NORMALIZE_DEFAULT also standardize the "compatibility" charac-
ters in Unicode, such as SUPERSCRIPT THREE to the standard forms (in this case DIGIT THREE).
Formatting information may be lost but for most text operations such characters should be con-
sidered the same.
G_NORMALIZE_NFKD another name for G_NORMALIZE_ALL.
G_NORMALIZE_ALL_COMPOSE like G_NORMALIZE_ALL, but with composed forms rather than a max-
imally decomposed form.
G_NORMALIZE_NFKC another name for G_NORMALIZE_ALL_COMPOSE.
g_utf8_collate ()
Compares two strings for ordering using the linguistically correct rules for the current locale. When
sorting a large number of strings, it will be significantly faster to obtain collation keys with g_utf8_collate_key()
and compare the keys with strcmp() when sorting instead of sorting the original strings.
str1 : a UTF-8 encoded string
Returns : < 0 if str1 compares before str2, 0 if they compare equal, > 0 if str1 compares after str2.
g_utf8_collate_key ()
Converts a string into a collation key that can be compared with other collation keys produced by
the same function using strcmp().
The results of comparing the collation keys of two strings with strcmp() will always be the same as
comparing the two original keys with g_utf8_collate().
Note that this function depends on the current locale.
244
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
Returns : a newly allocated string. This string should be freed with g_free() when you are done with it.
g_utf8_collate_key_for_filename ()
Converts a string into a collation key that can be compared with other collation keys produced by
the same function using strcmp().
In order to sort filenames correctly, this function treats the dot ’.’ as a special case. Most dictionary
orderings seem to consider it insignificant, thus producing the ordering "event.c" "eventgenerator.c"
"event.h" instead of "event.c" "event.h" "eventgenerator.c". Also, we would like to treat numbers intelli-
gently so that "file1" "file10" "file5" is sorted as "file1" "file5" "file10".
Note that this function depends on the current locale.
Returns : a newly allocated string. This string should be freed with g_free() when you are done with it.
Since 2.8
g_utf8_to_utf16 ()
Convert a string from UTF-8 to UTF-16. A 0 character will be added to the result after the converted
text.
len : the maximum length (number of bytes) of str to use. If len < 0, then the string is nul-terminated.
items_read : location to store number of bytes read, or NULL. If NULL, then G_CONVERT_ERROR_PARTIAL_INPU
will be returned in case str contains a trailing partial character. If an error occurs then the index
of the invalid input is stored here.
items_written : location to store number of gunichar2 written, or NULL. The value stored here does
not include the trailing 0.
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror other than G_CONVERT_ERROR_NO_CONVERSION may occur.
Returns : a pointer to a newly allocated UTF-16 string. This value must be freed with g_free(). If an
error occurs, NULL will be returned and error set.
g_utf8_to_ucs4 ()
245
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
Convert a string from UTF-8 to a 32-bit fixed width representation as UCS-4. A trailing 0 will be
added to the string after the converted text.
len : the maximum length of str to use, in bytes. If len < 0, then the string is nul-terminated.
items_read : location to store number of bytes read, or NULL. If NULL, then G_CONVERT_ERROR_PARTIAL_INPUT
will be returned in case str contains a trailing partial character. If an error occurs then the index
of the invalid input is stored here.
items_written : location to store number of characters written or NULL. The value here stored does
not include the trailing 0 character.
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror other than G_CONVERT_ERROR_NO_CONVERSION may occur.
Returns : a pointer to a newly allocated UCS-4 string. This value must be freed with g_free(). If an error
occurs, NULL will be returned and error set.
g_utf8_to_ucs4_fast ()
Convert a string from UTF-8 to a 32-bit fixed width representation as UCS-4, assuming valid UTF-8
input. This function is roughly twice as fast as g_utf8_to_ucs4() but does no error checking on the input.
len : the maximum length of str to use, in bytes. If len < 0, then the string is nul-terminated.
Returns : a pointer to a newly allocated UCS-4 string. This value must be freed with g_free().
g_utf16_to_ucs4 ()
len : the maximum length (number of gunichar2) of str to use. If len < 0, then the string is nul-
terminated.
items_read : location to store number of words read, or NULL. If NULL, then G_CONVERT_ERROR_PARTIAL_INPUT
will be returned in case str contains a trailing partial character. If an error occurs then the index
of the invalid input is stored here.
items_written : location to store number of characters written, or NULL. The value stored here does
not include the trailing 0 character.
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror other than G_CONVERT_ERROR_NO_CONVERSION may occur.
Returns : a pointer to a newly allocated UCS-4 string. This value must be freed with g_free(). If an error
occurs, NULL will be returned and error set.
246
CHAPTER 4. GLIB UTILITIES 4.3. UNICODE MANIPULATION
g_utf16_to_utf8 ()
Convert a string from UTF-16 to UTF-8. The result will be terminated with a 0 byte.
Note that the input is expected to be already in native endianness, an initial byte-order-mark charac-
ter is not handled specially. g_convert() can be used to convert a byte buffer of UTF-16 data of ambigu-
ous endianess.
len : the maximum length (number of gunichar2) of str to use. If len < 0, then the string is nul-
terminated.
items_read : location to store number of words read, or NULL. If NULL, then G_CONVERT_ERROR_PARTIAL_INP
will be returned in case str contains a trailing partial character. If an error occurs then the index
of the invalid input is stored here.
items_written : location to store number of bytes written, or NULL. The value stored here does not
include the trailing 0 byte.
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror other than G_CONVERT_ERROR_NO_CONVERSION may occur.
Returns : a pointer to a newly allocated UTF-8 string. This value must be freed with g_free(). If an error
occurs, NULL will be returned and error set.
g_ucs4_to_utf16 ()
Convert a string from UCS-4 to UTF-16. A 0 character will be added to the result after the converted
text.
len : the maximum length (number of characters) of str to use. If len < 0, then the string is nul-
terminated.
items_read : location to store number of bytes read, or NULL. If an error occurs then the index of the
invalid input is stored here.
items_written : location to store number of gunichar2 written, or NULL. The value stored here does
not include the trailing 0.
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror other than G_CONVERT_ERROR_NO_CONVERSION may occur.
Returns : a pointer to a newly allocated UTF-16 string. This value must be freed with g_free(). If an
error occurs, NULL will be returned and error set.
247
CHAPTER 4. GLIB UTILITIES 4.4. BASE64 ENCODING
g_ucs4_to_utf8 ()
Convert a string from a 32-bit fixed width representation as UCS-4. to UTF-8. The result will be
terminated with a 0 byte.
len : the maximum length (number of characters) of str to use. If len < 0, then the string is nul-
terminated.
items_written : location to store number of bytes written or NULL. The value here stored does not
include the trailing 0 byte.
error : location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertEr-
ror other than G_CONVERT_ERROR_NO_CONVERSION may occur.
Returns : a pointer to a newly allocated UTF-8 string. This value must be freed with g_free(). If an error
occurs, NULL will be returned and error set. In that case, items_read will be set to the position
of the first invalid input character.
g_unichar_to_utf8 ()
outbuf : output buffer, must have at least 6 bytes of space. If NULL, the length will be computed and
returned and nothing will be written to outbuf .
See Also
g_locale_to_utf8(), g_locale_from_utf8() Convenience functions for converting between UTF-8 and the
locale encoding.
Synopsis
#include <glib.h>
248
CHAPTER 4. GLIB UTILITIES 4.4. BASE64 ENCODING
gchar *out,
gint *state,
gint *save);
gsize g_base64_encode_close (gboolean break_lines,
gchar *out,
gint *state,
gint *save);
gchar* g_base64_encode (const guchar *data,
gsize len);
gsize g_base64_decode_step (const gchar *in,
gsize len,
guchar *out,
gint *state,
guint *save);
guchar * g_base64_decode (const gchar *text,
gsize *out_len);
guchar * g_base64_decode_inplace (gchar *text,
gsize *out_len);
Description
Base64 is an encoding that allows to encode a sequence of arbitrary bytes as a sequence of printable
ASCII characters. For the definition of Base64, see RFC 1421 or RFC 2045. Base64 is most commonly
used as a MIME transfer encoding for email.
GLib supports incremental encoding using g_base64_encode_step() and g_base64_encode_close().
Incremental decoding can be done with g_base64_decode_step(). To encode or decode data in one go,
use g_base64_encode() or g_base64_decode(). To avoid memory allocation when decoding, you can use
g_base64_decode_inplace().
Support for Base64 encoding has been added in GLib 2.12.
Details
g_base64_encode_step ()
Incrementally encode a sequence of binary data into its Base-64 stringified representation. By calling
this function multiple times you can convert data in chunks to avoid having to have the full encoded
data in memory.
When all of the data has been converted you must call g_base64_encode_close() to flush the saved
state.
The output buffer must be large enough to fit all the data that will be written to it. Due to the way
base64 encodes you will need at least: (len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of non-zero
state). If you enable line-breaking you will need at least: ((len / 3 + 1) * 4 + 4) / 72 + 1 bytes of extra
space.
break_lines is typically used when putting base64-encoded data in emails. It breaks the lines at 72
columns instead of putting all of the text on the same line. This avoids problems with long lines in the
email system.
in : the binary data to encode
249
CHAPTER 4. GLIB UTILITIES 4.4. BASE64 ENCODING
g_base64_encode_close ()
g_base64_encode ()
Returns : a newly allocated, zero-terminated Base-64 encoded string representing data. The returned
string must be freed with g_free().
Since 2.12
g_base64_decode_step ()
Incrementally decode a sequence of binary data from its Base-64 stringified representation. By calling
this function multiple times you can convert data in chunks to avoid having to have the full encoded
data in memory.
The output buffer must be large enough to fit all the data that will be written to it. Since base64
encodes 3 bytes in 4 chars you need at least: (len / 4) * 3 + 3 bytes (+ 3 may be needed in case of
non-zero state).
in : binary input data
250
CHAPTER 4. GLIB UTILITIES 4.5. DATA CHECKSUMS
Since 2.12
g_base64_decode ()
Returns : a newly allocated buffer containing the binary data that text represents. The returned buffer
must be freed with g_free().
Since 2.12
g_base64_decode_inplace ()
Decode a sequence of Base-64 encoded text into binary data by overwriting the input data.
Returns : The binary data that text responds. This pointer is the same as the input text.
Since 2.20
Synopsis
#include <glib.h>
enum GChecksumType;
gssize g_checksum_type_get_length (GChecksumType checksum_type);
GChecksum;
GChecksum * g_checksum_new (GChecksumType checksum_type);
GChecksum * g_checksum_copy (const GChecksum *checksum);
void g_checksum_free (GChecksum *checksum);
void g_checksum_reset (GChecksum *checksum);
void g_checksum_update (GChecksum *checksum,
const guchar *data,
gssize length);
const gchar * g_checksum_get_string (GChecksum *checksum);
void g_checksum_get_digest (GChecksum *checksum,
251
CHAPTER 4. GLIB UTILITIES 4.5. DATA CHECKSUMS
guint8 *buffer,
gsize *digest_len);
Description
GLib provides a generic API for computing checksums (or "digests") for a sequence of arbitrary bytes,
using various hashing algorithms like MD5, SHA-1 and SHA-256. Checksums are commonly used in
various environments and specifications.
GLib supports incremental checksums using the GChecksum data structure, by calling g_checksum_update()
as long as there’s data available and then using g_checksum_get_string() or g_checksum_get_digest() to
compute the checksum and return it either as a string in hexadecimal form, or as a raw sequence of bytes.
To compute the checksum for binary blobs and NUL-terminated strings in one go, use the convenience
functions g_compute_checksum_for_data() and g_compute_checksum_for_string(), respectively.
Support for checksums has been added in GLib 2.16
Details
enum GChecksumType
typedef enum {
G_CHECKSUM_MD5,
G_CHECKSUM_SHA1,
G_CHECKSUM_SHA256
} GChecksumType;
The hashing algorithm to be used by GChecksum when performing the digest of some data.
Note that the GChecksumType enumeration may be extended at a later date to include new hashing
algorithm types.
G_CHECKSUM_MD5 Use the MD5 hashing algorithm
G_CHECKSUM_SHA1 Use the SHA-1 hashing algorithm
G_CHECKSUM_SHA256 Use the SHA-256 hashing algorithm
Since 2.16
g_checksum_type_get_length ()
GChecksum
252
CHAPTER 4. GLIB UTILITIES 4.5. DATA CHECKSUMS
g_checksum_new ()
Creates a new GChecksum, using the checksum algorithm checksum_type. If the checksum_type is
not known, NULL is returned. A GChecksum can be used to compute the checksum, or digest, of an
arbitrary binary blob, using different hashing algorithms.
A GChecksum works by feeding a binary blob through g_checksum_update() until there is data
to be checked; the digest can then be extracted using g_checksum_get_string(), which will return the
checksum as a hexadecimal string; or g_checksum_get_digest(), which will return a vector of raw bytes.
Once either g_checksum_get_string() or g_checksum_get_digest() have been called on a GChecksum,
the checksum will be closed and it won’t be possible to call g_checksum_update() on it anymore.
Returns : the newly created GChecksum, or NULL. Use g_checksum_free() to free the memory allocated
by it.
Since 2.16
g_checksum_copy ()
Returns : the copy of the passed GChecksum. Use g_checksum_free() when finished using it.
Since 2.16
g_checksum_free ()
checksum : a GChecksum
Since 2.16
g_checksum_reset ()
Since 2.18
253
CHAPTER 4. GLIB UTILITIES 4.5. DATA CHECKSUMS
g_checksum_update ()
Feeds data into an existing GChecksum. The checksum must still be open, that is g_checksum_get_string()
or g_checksum_get_digest() must not have been called on checksum.
checksum : a GChecksum
Since 2.16
g_checksum_get_string ()
Returns : the hexadecimal representation of the checksum. The returned string is owned by the check-
sum and should not be modified or freed.
Since 2.16
g_checksum_get_digest ()
Gets the digest from checksum as a raw binary vector and places it into buffer . The size of the digest
depends on the type of checksum.
Once this function has been called, the GChecksum is closed and can no longer be updated with
g_checksum_update().
checksum : a GChecksum
digest_len : an inout parameter. The caller initializes it to the size of buffer . After the call it contains
the length of the digest.
Since 2.16
g_compute_checksum_for_data ()
Computes the checksum for a binary data of length. This is a convenience wrapper for g_checksum_new(),
g_checksum_get_string() and g_checksum_free().
The hexadecimal string returned will be in lower case.
checksum_type : a GChecksumType
254
CHAPTER 4. GLIB UTILITIES 4.6. INTERNATIONALIZATION
Returns : the digest of the binary data as a string in hexadecimal. The returned string should be freed
with g_free() when done using it.
Since 2.16
g_compute_checksum_for_string ()
Returns : the checksum as a hexadecimal string. The returned string should be freed with g_free() when
done using it.
Since 2.16
4.6 Internationalization
Name
Internationalization – gettext support macros
Synopsis
#include <glib.h>
#include <glib/gi18n.h>
#define Q_ (String)
#define C_ (Context,String)
#define N_ (String)
#define NC_ (Context, String)
const gchar * g_dgettext (const gchar *domain,
const gchar *msgid);
const gchar * g_dngettext (const gchar *domain,
const gchar *msgid,
const gchar *msgid_plural,
gulong n);
const gchar * g_dpgettext (const gchar *domain,
const gchar *msgctxtid,
gsize msgidoffset);
const gchar * g_dpgettext2 (const gchar *domain,
const gchar *context,
const gchar *msgid);
const gchar * g_strip_context (const gchar *msgid,
const gchar *msgval);
255
CHAPTER 4. GLIB UTILITIES 4.6. INTERNATIONALIZATION
Description
GLib doesn’t force any particular localization method upon its users. But since GLib itself is localized
using the gettext() mechanism, it seems natural to offer the de-facto standard gettext() support macros
in an easy-to-use form.
In order to use these macros in an application, you must include glib/gi18n.h. For use in a
library, must include glib/gi18n-lib.h after defining the GETTEXT_PACKAGE macro suitably for
your library:
#define GETTEXT_PACKAGE "gtk20"
#include <glib/gi18n-lib.h>
The gettext manual covers details of how to set up message extraction with xgettext.
Details
Q_()
#define Q_(String)
Like _(), but handles context in message ids. This has the advantage that the string can be adorned
with a prefix to guarantee uniqueness and provide context to the translator.
One use case given in the gettext manual is GUI translation, where one could e.g. disambiguate
two "Open" menu entries as "File|Open" and "Printer|Open". Another use case is the string "Russian"
which may have to be translated differently depending on whether it’s the name of a character set or a
language. This could be solved by using "charset|Russian" and "language|Russian".
See the C_() macro for a different way to mark up translatable strings with context.
N OTE
If you are using the Q_() macro, you need to make sure that you pass --keyword=Q_
to xgettext when extracting messages. If you are using GNU gettext >= 0.15, you can also
use --keyword=Q_:1g to let xgettext split the context string off into a msgctxt line in
the po file.
String : the string to be translated, with a ’|’-separated prefix which must not be translated
C_()
#define C_(Context,String)
Uses gettext to get the translation for msgid . msgctxt is used as a context. This is mainly useful for
short strings which may need different translations, depending on the context in which they are used.
label1 = C_("Navigation", "Back");
label2 = C_("Body part", "Back");
N OTE
If you are using the C_() macro, you need to make sure that you pass --keyword=C_-
:1c,2 to xgettext when extracting messages. Note that this only works with GNU gettext
>= 0.15.
256
CHAPTER 4. GLIB UTILITIES 4.6. INTERNATIONALIZATION
N_()
#define N_(String)
Only marks a string for translation. This is useful in situations where the translated strings can’t be
directly used, e.g. in string array initializers. To get the translated string, call gettext() at runtime.
{
static const char *messages[] = {
N_("some very meaningful message"),
N_("and another one")
};
const char *string;
...
string
= index > 1 ? _("a default message") : gettext (messages[index]);
fputs (string);
...
}
NC_()
Only marks a string for translation, with context. This is useful in situations where the translated
strings can’t be directly used, e.g. in string array initializers. To get the translated string, you should call
g_dpgettext2() at runtime.
{
static const char *messages[] = {
NC_("some context", "some very meaningful message"),
NC_("some context", "and another one")
};
const char *string;
...
string
= index > 1 ? g_dpgettext2 (NULL, "some context", "a default message") : ←-
g_dpgettext2 (NULL, "some context", messages[index]);
fputs (string);
...
}
N OTE
If you are using the NC_() macro, you need to make sure that you pass --keyword=-
NC_:1c,2 to xgettext when extracting messages. Note that this only works with GNU
gettext >= 0.15. Intltool has support for the NC_() macro since version 0.40.1.
257
CHAPTER 4. GLIB UTILITIES 4.6. INTERNATIONALIZATION
Since 2.18
g_dgettext ()
This function is a wrapper of dgettext() which does not translate the message if the default domain
as set with textdomain() has no translations for the current locale.
The advantage of using this function over dgettext() proper is that libraries using this function (like
GTK+) will not use translations if the application using the library does not have translations for the
current locale. This results in a consistent English-only interface instead of one having partial transla-
tions. For this feature to work, the call to textdomain() and setlocale() should precede any g_dgettext()
invocations. For GTK+, it means calling textdomain() before gtk_init or its variants.
This function disables translations if and only if upon its first call all the following conditions hold:
• there is no translations available for the default text domain and the current locale
• current locale is not "C" or any English locales (those starting with "en_")
Note that this behavior may not be desired for example if an application has its untranslated mes-
sages in a language other than English. In those cases the application should call textdomain() after
initializing GTK+.
Applications should normally not use this function directly, but use the _() macro for translations.
domain : the translation domain to use, or NULL to use the domain set with textdomain()
Since 2.18
g_dngettext ()
This function is a wrapper of dngettext() which does not translate the message if the default domain
as set with textdomain() has no translations for the current locale.
See g_dgettext() for details of how this differs from dngettext() proper.
domain : the translation domain to use, or NULL to use the domain set with textdomain()
Since 2.18
258
CHAPTER 4. GLIB UTILITIES 4.6. INTERNATIONALIZATION
g_dpgettext ()
This function is a variant of g_dgettext() which supports a disambiguating message context. GNU
gettext uses the ’\004’ character to separate the message context and message id in msgctxtid . If 0 is
passed as msgidoffset, this function will fall back to trying to use the deprecated convention of using
"|" as a separation character.
This uses g_dgettext() internally. See that functions for differences with dgettext() proper.
Applications should normally not use this function directly, but use the C_() macro for translations
with context.
domain : the translation domain to use, or NULL to use the domain set with textdomain()
msgctxtid : a combined message context and message id, separated by a \004 character
Since 2.16
g_dpgettext2 ()
This function is a variant of g_dgettext() which supports a disambiguating message context. GNU
gettext uses the ’\004’ character to separate the message context and message id in msgctxtid .
This uses g_dgettext() internally. See that functions for differences with dgettext() proper.
This function differs from C_() in that it is not a macro and thus you may use non-string-literals as
context and msgid arguments.
domain : the translation domain to use, or NULL to use the domain set with textdomain()
Since 2.18
g_strip_context ()
msgid : a string
Returns : msgval, unless msgval is identical to msgid and contains a ’|’ character, in which case a
pointer to the substring of msgid after the first ’|’ character is returned.
Since 2.4
259
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_get_language_names ()
Computes a list of applicable locale names, which can be used to e.g. construct locale-dependent
filenames or search paths. The returned list is sorted from most desirable to least desirable and always
contains the default locale "C".
For example, if LANGUAGE=de:en_US, then the returned list is "de", "en_US", "en", "C".
This function consults the environment variables LANGUAGE, LC_ALL, LC_MESSAGES and LANG to
find the list of locales specified by the user.
Returns : a NULL-terminated array of strings owned by GLib that must not be modified or freed.
Since 2.6
See Also
The gettext manual.
Synopsis
#include <glib.h>
#define G_USEC_PER_SEC
GTimeVal;
void g_get_current_time (GTimeVal *result);
void g_usleep (gulong microseconds);
void g_time_val_add (GTimeVal *time_,
glong microseconds);
gboolean g_time_val_from_iso8601 (const gchar *iso_date,
GTimeVal *time_);
gchar* g_time_val_to_iso8601 (GTimeVal *time_);
GDate;
typedef GTime;
enum GDateDMY;
typedef GDateDay;
enum GDateMonth;
typedef GDateYear;
enum GDateWeekday;
#define G_DATE_BAD_DAY
#define G_DATE_BAD_JULIAN
#define G_DATE_BAD_YEAR
260
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
261
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
Description
The GDate data structure represents a day between January 1, Year 1, and sometime a few thousand
years in the future (right now it will go to the year 65535 or so, but g_date_set_parse() only parses up
to the year 8000 or so - just count on "a few thousand"). GDate is meant to represent everyday dates,
not astronomical dates or historical dates or ISO timestamps or the like. It extrapolates the current
Gregorian calendar forward and backward in time; there is no attempt to change the calendar to match
time periods or locations. GDate does not store time information; it represents a day.
The GDate implementation has several nice features; it is only a 64-bit struct, so storing large num-
bers of dates is very efficient. It can keep both a Julian and day-month-year representation of the date,
since some calculations are much easier with one representation or the other. A Julian representation
is simply a count of days since some fixed day in the past; for GDate the fixed day is January 1, 1 AD.
("Julian" dates in the GDate API aren’t really Julian dates in the technical sense; technically, Julian dates
count from the start of the Julian period, Jan 1, 4713 BC).
GDate is simple to use. First you need a "blank" date; you can get a dynamically allocated date
from g_date_new(), or you can declare an automatic variable or array and initialize it to a sane state by
calling g_date_clear(). A cleared date is sane; it’s safe to call g_date_set_dmy() and the other mutator
functions to initialize the value of a cleared date. However, a cleared date is initially invalid, meaning
that it doesn’t represent a day that exists. It is undefined to call any of the date calculation routines on an
invalid date. If you obtain a date from a user or other unpredictable source, you should check its validity
with the g_date_valid() predicate. g_date_valid() is also used to check for errors with g_date_set_parse()
and other functions that can fail. Dates can be invalidated by calling g_date_clear() again.
It is very important to use the API to access the GDate struct. Often only the day-month-year or only the
Julian representation is valid. Sometimes neither is valid. Use the API.
GLib doesn’t contain any time-manipulation functions; however, there is a GTime typedef and a
GTimeVal struct which represents a more precise time (with microseconds). You can request the current
time as a GTimeVal with g_get_current_time().
Details
G_USEC_PER_SEC
Number of microseconds in one second (1 million). This macro is provided for code readability.
GTimeVal
262
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
typedef struct {
glong tv_sec;
glong tv_usec;
} GTimeVal;
Represents a precise time, with seconds and microseconds. Similar to the struct timeval returned by
the gettimeofday() UNIX call.
g_get_current_time ()
g_usleep ()
Pauses the current thread for the given number of microseconds. There are 1 million microseconds
per second (represented by the G_USEC_PER_SEC macro). g_usleep() may have limited precision, de-
pending on hardware and operating system; don’t rely on the exact length of the sleep.
g_time_val_add ()
Adds the given number of microseconds to time_. microseconds can also be negative to decrease
the value of time_.
time_ : a GTimeVal
g_time_val_from_iso8601 ()
Converts a string containing an ISO 8601 encoded date and time to a GTimeVal and puts it into
time_.
time_ : a GTimeVal
Since 2.12
263
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_time_val_to_iso8601 ()
Converts time_ into an ISO 8601 encoded string, relative to the Coordinated Universal Time (UTC).
time_ : a GTimeVal
GDate
typedef struct {
guint julian_days : 32; /* julian days representation - we use a
* bitfield hoping that 64 bit platforms
* will pack this whole struct in one big
* int
*/
/* DMY representation */
guint day : 6;
guint month : 4;
guint year : 16;
} GDate;
Represents a day between January 1, Year 1 and a few thousand years in the future. None of its
members should be accessed directly. If the GDate is obtained from g_date_new(), it will be safe to
mutate but invalid and thus not safe for calendrical computations. If it’s declared on the stack, it will
contain garbage so must be initialized with g_date_clear(). g_date_clear() makes the date invalid but
sane. An invalid date doesn’t represent a day, it’s "empty." A date becomes valid after you set it to a
Julian day or you set a day, month, and year.
guint julian_days : 32; the Julian representation of the date
guint julian : 1; this bit is set if julian_days is valid
guint dmy : 1; this is set if day , month and year are valid
guint day : 6; the day of the day-month-year representation of the date, as a number between 1 and 31
guint month : 4; the day of the day-month-year representation of the date, as a number between 1 and
12
guint year : 16; the day of the day-month-year representation of the date
GTime
Simply a replacement for time_t. It has been deprected since it is not equivalent to time_t on 64-bit
platforms with a 64-bit time_t. Unrelated to GTimer.
Note that GTime is defined to always be a 32bit integer, unlike time_t which may be 64bit on some
systems. Therefore, GTime will overflow in the year 2038, and you cannot use the address of a GTime
variable as argument to the UNIX time() function. Instead, do the following:
time_t ttime;
GTime gtime;
time (&ttime);
gtime = (GTime)ttime;
264
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
enum GDateDMY
typedef enum
{
G_DATE_DAY = 0,
G_DATE_MONTH = 1,
G_DATE_YEAR = 2
} GDateDMY;
This enumeration isn’t used in the API, but may be useful if you need to mark a number as a day,
month, or year.
G_DATE_DAY a day
G_DATE_MONTH a month
G_DATE_YEAR a year
GDateDay
Integer representing a day of the month; between 1 and 31. G_DATE_BAD_DAY represents an in-
valid day of the month.
enum GDateMonth
typedef enum
{
G_DATE_BAD_MONTH = 0,
G_DATE_JANUARY = 1,
G_DATE_FEBRUARY = 2,
G_DATE_MARCH = 3,
G_DATE_APRIL = 4,
G_DATE_MAY = 5,
G_DATE_JUNE = 6,
G_DATE_JULY = 7,
G_DATE_AUGUST = 8,
G_DATE_SEPTEMBER = 9,
G_DATE_OCTOBER = 10,
G_DATE_NOVEMBER = 11,
G_DATE_DECEMBER = 12
} GDateMonth;
G_DATE_JANUARY January
G_DATE_FEBRUARY February
G_DATE_MARCH March
G_DATE_APRIL April
G_DATE_MAY May
G_DATE_JUNE June
G_DATE_JULY July
G_DATE_AUGUST August
265
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
G_DATE_SEPTEMBER September
G_DATE_OCTOBER October
G_DATE_NOVEMBER November
G_DATE_DECEMBER December
GDateYear
Integer representing a year; G_DATE_BAD_YEAR is the invalid value. The year must be 1 or higher;
negative (BC) years are not allowed. The year is represented with four digits.
enum GDateWeekday
typedef enum
{
G_DATE_BAD_WEEKDAY = 0,
G_DATE_MONDAY = 1,
G_DATE_TUESDAY = 2,
G_DATE_WEDNESDAY = 3,
G_DATE_THURSDAY = 4,
G_DATE_FRIDAY = 5,
G_DATE_SATURDAY = 6,
G_DATE_SUNDAY = 7
} GDateWeekday;
G_DATE_BAD_DAY
#define G_DATE_BAD_DAY 0U
G_DATE_BAD_JULIAN
#define G_DATE_BAD_JULIAN 0U
G_DATE_BAD_YEAR
#define G_DATE_BAD_YEAR 0U
266
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_new ()
Allocates a GDate and initializes it to a sane state. The new date will be cleared (as if you’d called
g_date_clear()) but invalid (it won’t represent an existing day). Free the return value with g_date_free().
g_date_new_dmy ()
Like g_date_new(), but also sets the value of the date. Assuming the day-month-year triplet you
pass in represents an existing day, the returned date will be valid.
year : year
g_date_new_julian ()
Like g_date_new(), but also sets the value of the date. Assuming the Julian day number you pass in
is valid (greater than 0, less than an unreasonably large number), the returned date will be valid.
g_date_clear ()
Initializes one or more GDate structs to a sane but invalid state. The cleared dates will not represent
an existing date, but will not contain garbage. Useful to init a date declared on the stack. Validity can
be tested with g_date_valid().
g_date_free ()
date : a GDate
267
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_set_day ()
Sets the day of the month for a GDate. If the resulting day-month-year triplet is invalid, the date will
be invalid.
date : a GDate
g_date_set_month ()
Sets the month of the year for a GDate. If the resulting day-month-year triplet is invalid, the date
will be invalid.
date : a GDate
g_date_set_year ()
Sets the year for a GDate. If the resulting day-month-year triplet is invalid, the date will be invalid.
date : a GDate
g_date_set_dmy ()
Sets the value of a GDate from a day, month, and year. The day-month-year triplet must be valid; if
you aren’t sure it is, call g_date_valid_dmy() to check before you set it.
date : a GDate
day : day
month : month
y : year
g_date_set_julian ()
date : a GDate
268
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_set_time ()
WARNING
Sets the value of a date from a GTime value. The time to date conversion is done using the user’s
current timezone.
Deprecated :2.10: Use g_date_set_time_t() instead.
date : a GDate.
g_date_set_time_t ()
Sets the value of a date to the date corresponding to a time specified as a time_t. The time to date
conversion is done using the user’s current timezone.
To set the value of a date to the current day, you could write:
g_date_set_time_t (date, time (NULL));
date : a GDate
Since 2.10
g_date_set_time_val ()
Sets the value of a date from a GTimeVal value. Note that the tv_usec member is ignored, because
GDate can’t make use of the additional precision.
date : a GDate
Since 2.10
g_date_set_parse ()
Parses a user-inputted string str , and try to figure out what date it represents, taking the current
locale into account. If the string is successfully parsed, the date will be valid after the call. Otherwise, it
will be invalid. You should check using g_date_valid() to see whether the parsing succeeded.
This function is not appropriate for file formats and the like; it isn’t very precise, and its exact behav-
ior varies with the locale. It’s intended to be a heuristic routine that guesses what the user means by a
given string (and it does work pretty well in that capacity).
269
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_add_days ()
Increments a date some number of days. To move forward by weeks, add weeks*7 days. The date
must be valid.
date : a GDate to increment
g_date_subtract_days ()
Moves a date some number of days into the past. To move by weeks, just move by weeks*7 days.
The date must be valid.
date : a GDate to decrement
g_date_add_months ()
Increments a date by some number of months. If the day of the month is greater than 28, this routine
may change the day of the month (because the destination month may not have the current day in it).
The date must be valid.
date : a GDate to increment
g_date_subtract_months ()
Moves a date some number of months into the past. If the current day of the month doesn’t exist in
the destination month, the day of the month may change. The date must be valid.
date : a GDate to decrement
g_date_add_years ()
Increments a date by some number of years. If the date is February 29, and the destination year is
not a leap year, the date will be changed to February 28. The date must be valid.
date : a GDate to increment
270
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_subtract_years ()
Moves a date some number of years into the past. If the current day doesn’t exist in the destination
year (i.e. it’s February 29 and you move to a non-leap-year) then the day is changed to February 29. The
date must be valid.
date : a GDate to decrement
g_date_days_between ()
Computes the number of days between two dates. If date2 is prior to date1, the returned value is
negative. Both dates must be valid.
date1 : the first date
g_date_compare ()
Returns : 0 for equal, less than zero if lhs is less than rhs, greater than zero if lhs is greater than rhs
g_date_clamp ()
If date is prior to min_date, sets date equal to min_date. If date falls after max_date, sets date
equal to max_date. Otherwise, date is unchanged. Either of min_date and max_date may be NULL.
All non-NULL dates must be valid.
date : a GDate to clamp
g_date_order ()
Checks if date1 is less than or equal to date2, and swap the values if this is not the case.
date1 : the first date
271
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_get_day ()
g_date_get_month ()
g_date_get_year ()
date : a GDate
g_date_get_julian ()
Returns the Julian day or "serial number" of the GDate. The Julian day is simply the number of days
since January 1, Year 1; i.e., January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2, etc. The
date must be valid.
g_date_get_weekday ()
Returns the day of the week for a GDate. The date must be valid.
date : a GDate.
g_date_get_day_of_year ()
Returns the day of the year, where Jan 1 is the first day of the year. The date must be valid.
272
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_get_days_in_month ()
Returns the number of days in a month, taking leap years into account.
month : month
year : year
g_date_is_first_of_month ()
Returns TRUE if the date is on the first of a month. The date must be valid.
g_date_is_last_of_month ()
Returns TRUE if the date is the last day of the month. The date must be valid.
g_date_is_leap_year ()
g_date_get_monday_week_of_year ()
Returns the week of the year, where weeks are understood to start on Monday. If the date is before
the first Monday of the year, return 0. The date must be valid.
date : a GDate
by 100 it would be a leap year only if that year is also divisible by 400.
273
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
g_date_get_monday_weeks_in_year ()
Returns the number of weeks in the year, where weeks are taken to start on Monday. Will be 52 or
53. The date must be valid. (Years always have 52 7-day periods, plus 1 or 2 extra days depending on
whether it’s a leap year. This function is basically telling you how many Mondays are in the year, i.e.
there are 53 Mondays if one of the extra days happens to be a Monday.)
year : a year
g_date_get_sunday_week_of_year ()
Returns the week of the year during which this date falls, if weeks are understood to being on Sun-
day. The date must be valid. Can return 0 if the day is before the first Sunday of the year.
date : a GDate
g_date_get_sunday_weeks_in_year ()
Returns the number of weeks in the year, where weeks are taken to start on Sunday. Will be 52 or
53. The date must be valid. (Years always have 52 7-day periods, plus 1 or 2 extra days depending on
whether it’s a leap year. This function is basically telling you how many Sundays are in the year, i.e.
there are 53 Sundays if one of the extra days happens to be a Sunday.)
year : year to count weeks in
g_date_get_iso8601_week_of_year ()
Returns the week of the year, where weeks are interpreted according to ISO 8601.
date : a valid GDate
g_date_strftime ()
Generates a printed representation of the date, in a locale-specific way. Works just like the platform’s
C library strftime() function, but only accepts date-related formats; time-related formats give undefined
results. Date must be valid. Unlike strftime() (which uses the locale encoding), works on a UTF-8 format
string and stores a UTF-8 result.
This function does not provide any conversion specifiers in addition to those implemented by the
platform’s C library. For example, don’t expect that using g_date_strftime() would make the F provided
by the C99 strftime() work on Windows where the C library only complies to C89.
274
CHAPTER 4. GLIB UTILITIES 4.7. DATE AND TIME FUNCTIONS
s : destination buffer
Returns : number of characters written to the buffer, or 0 the buffer was too small
g_date_to_struct_tm ()
Fills in the date-related bits of a struct tm using the date value. Initializes the non-date parts with
something sane but meaningless.
date : a GDate to set the struct tm from.
tm : struct tm to fill.
g_date_valid ()
Returns TRUE if the GDate represents an existing day. The date must not contain garbage; it should
have been initialized with g_date_clear() if it wasn’t allocated by one of the g_date_new() variants.
date : a GDate to check
g_date_valid_day ()
Returns TRUE if the day of the month is valid (a day is valid if it’s between 1 and 31 inclusive).
day : day to check
g_date_valid_month ()
Returns TRUE if the month value is valid. The 12 GDateMonth enumeration values are the only
valid months.
month : month
g_date_valid_year ()
Returns TRUE if the year is valid. Any year greater than 0 is valid, though there is a 16-bit limit to
what GDate will understand.
year : year
275
CHAPTER 4. GLIB UTILITIES 4.8. RANDOM NUMBERS
g_date_valid_dmy ()
Returns TRUE if the day-month-year triplet forms a valid, existing day in the range of days GDate
understands (Year 1 or later, no more than a few thousand years in the future).
day : day
month : month
year : year
g_date_valid_julian ()
Returns TRUE if the Julian day is valid. Anything greater than zero is basically a valid Julian, though
there is a 32-bit limit.
g_date_valid_weekday ()
Returns TRUE if the weekday is valid. The seven GDateWeekday enumeration values are the only
valid weekdays.
weekday : weekday
Synopsis
#include <glib.h>
GRand;
GRand* g_rand_new_with_seed (guint32 seed);
GRand* g_rand_new_with_seed_array (const guint32 *seed,
guint seed_length);
GRand* g_rand_new (void);
GRand* g_rand_copy (GRand *rand_);
void g_rand_free (GRand *rand_);
void g_rand_set_seed (GRand *rand_,
guint32 seed);
void g_rand_set_seed_array (GRand *rand_,
const guint32 *seed,
276
CHAPTER 4. GLIB UTILITIES 4.8. RANDOM NUMBERS
guint seed_length);
#define g_rand_boolean (rand_)
guint32 g_rand_int (GRand *rand_);
gint32 g_rand_int_range (GRand *rand_,
gint32 begin,
gint32 end);
gdouble g_rand_double (GRand *rand_);
gdouble g_rand_double_range (GRand *rand_,
gdouble begin,
gdouble end);
void g_random_set_seed (guint32 seed);
#define g_random_boolean ()
guint32 g_random_int (void);
gint32 g_random_int_range (gint32 begin,
gint32 end);
gdouble g_random_double (void);
gdouble g_random_double_range (gdouble begin,
gdouble end);
Description
The following functions allow you to use a portable, fast and good pseudo-random number generator
(PRNG). It uses the Mersenne Twister PRNG, which was originally developed by Makoto Matsumoto
and Takuji Nishimura. Further information can be found at www.math.keio.ac.jp/~matumoto/emt.html.
If you just need a random number, you simply call the g_random_* functions, which will create
a globally used GRand and use the according g_rand_* functions internally. Whenever you need a
stream of reproducible random numbers, you better create a GRand yourself and use the g_rand_-
* functions directly, which will also be slightly faster. Initializing a GRand with a certain seed will
produce exactly the same series of random numbers on all platforms. This can thus be used as a seed
for e.g. games.
The g_rand*_range functions will return high quality equally distributed random numbers, whereas
for example the (g_random_int()%max) approach often doesn’t yield equally distributed numbers.
GLib changed the seeding algorithm for the pseudo-random number generator Mersenne Twister, as
used by GRand and GRandom. This was necessary, because some seeds would yield very bad pseudo-
random streams. Also the pseudo-random integers generated by g_rand*_int_range() will have a
slightly better equal distribution with the new version of GLib.
The original seeding and generation algorithms, as found in GLib 2.0.x, can be used instead of the
new ones by setting the environment variable G_RANDOM_VERSION to the value of ’2.0’. Use the GLib-
2.0 algorithms only if you have sequences of numbers generated with Glib-2.0 that you need to repro-
duce exactly.
Details
GRand
The GRand struct is an opaque data structure. It should only be accessed through the g_rand_*
functions.
g_rand_new_with_seed ()
277
CHAPTER 4. GLIB UTILITIES 4.8. RANDOM NUMBERS
g_rand_new_with_seed_array ()
Since 2.4
g_rand_new ()
Creates a new random number generator initialized with a seed taken either from /dev/urandom
(if existing) or from the current time (as a fallback).
g_rand_copy ()
Copies a GRand into a new one with the same exact state as before. This way you can take a snapshot
of the random number generator for replaying later.
rand_ : a GRand.
Since 2.4
g_rand_free ()
rand_ : a GRand.
g_rand_set_seed ()
Sets the seed for the random number generator GRand to seed .
rand_ : a GRand.
278
CHAPTER 4. GLIB UTILITIES 4.8. RANDOM NUMBERS
g_rand_set_seed_array ()
Initializes the random number generator by an array of longs. Array can be of arbitrary size, though
only the first 624 values are taken. This function is useful if you have many low entropy seeds, or if you
require more then 32bits of actual entropy for your application.
rand_ : a GRand.
Since 2.4
g_rand_boolean()
#define g_rand_boolean(rand_)
Returns a random gboolean from rand_. This corresponds to a unbiased coin toss.
rand_ : a GRand.
g_rand_int ()
Returns the next random guint32 from rand_ equally distributed over the range [0..2ˆ32-1].
rand_ : a GRand.
g_rand_int_range ()
Returns the next random gint32 from rand_ equally distributed over the range [begin..end -1].
rand_ : a GRand.
g_rand_double ()
Returns the next random gdouble from rand_ equally distributed over the range [0..1).
rand_ : a GRand.
279
CHAPTER 4. GLIB UTILITIES 4.8. RANDOM NUMBERS
g_rand_double_range ()
Returns the next random gdouble from rand_ equally distributed over the range [begin..end ).
rand_ : a GRand.
g_random_set_seed ()
Sets the seed for the global random number generator, which is used by the g_random_* functions,
to seed .
g_random_boolean()
#define g_random_boolean()
g_random_int ()
g_random_int_range ()
Returns a random gint32 equally distributed over the range [begin..end -1].
g_random_double ()
280
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
g_random_double_range ()
Synopsis
#include <glib.h>
GHookList;
void (*GHookFinalizeFunc) (GHookList *hook_list,
GHook *hook);
GHook;
void (*GHookFunc) (gpointer data);
gboolean (*GHookCheckFunc) (gpointer data);
281
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
GHookCompareFunc func);
gint (*GHookCompareFunc) (GHook *new_hook,
GHook *sibling);
gint g_hook_compare_ids (GHook *new_hook,
GHook *sibling);
Description
The GHookList, GHook and their related functions provide support for lists of hook functions. Func-
tions can be added and removed from the lists, and the list of hook functions can be invoked.
282
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
Details
GHookList
typedef struct {
gulong seq_id;
guint hook_size : 16;
guint is_setup : 1;
GHook *hooks;
gpointer dummy3;
GHookFinalizeFunc finalize_hook;
gpointer dummy[2];
} GHookList;
GHookFinalizeFunc ()
Defines the type of function to be called when a hook in a list of hooks gets finalized.
hook_list : a GHookList
GHook
typedef struct {
gpointer data;
GHook *next;
GHook *prev;
guint ref_count;
gulong hook_id;
guint flags;
gpointer func;
GDestroyNotify destroy;
} GHook;
283
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
guint flags; flags which are set for this hook. See GHookFlagMask for predefined flags
gpointer func; the function to call when this hook is invoked. The possible signatures for this function
are GHookFunc and GHookCheckFunc
GDestroyNotify destroy ; the default finalize_hook function of a GHookList calls this member of
the hook that is being finalized
GHookFunc ()
data : the data field of the GHook is passed to the hook function here
GHookCheckFunc ()
data : the data field of the GHook is passed to the hook function here
g_hook_list_init ()
hook_list : a GHookList
hook_size : the size of each element in the GHookList, typically sizeof (GHook)
g_hook_list_invoke ()
hook_list : a GHookList
may_recurse : %TRUE if functions which are already running (e.g. in another thread) can be called. If
set to FALSE, these are skipped
g_hook_list_invoke_check ()
Calls all of the GHook functions in a GHookList. Any function which returns FALSE is removed
from the GHookList.
hook_list : a GHookList
may_recurse : %TRUE if functions which are already running (e.g. in another thread) can be called. If
set to FALSE, these are skipped
284
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
g_hook_list_marshal ()
hook_list : a GHookList
may_recurse : %TRUE if hooks which are currently running (e.g. in another thread) are considered
valid. If set to FALSE, these are skipped
GHookMarshaller ()
hook : a GHook
g_hook_list_marshal_check ()
Calls a function on each valid GHook and destroys it if the function returns FALSE.
hook_list : a GHookList
may_recurse : %TRUE if hooks which are currently running (e.g. in another thread) are considered
valid. If set to FALSE, these are skipped
GHookCheckMarshaller ()
hook : a GHook
285
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
g_hook_list_clear ()
g_hook_alloc ()
g_hook_append()
g_hook_prepend ()
g_hook_insert_before ()
g_hook_insert_sorted ()
286
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
GHookCompareFunc ()
g_hook_compare_ids ()
Compares the ids of two GHook elements, returning a negative value if the second id is greater than
the first.
new_hook : a GHook
g_hook_get ()
Returns the GHook with the given id, or NULL if it is not found.
hook_list : a GHookList
hook_id : a hook id
Returns : the GHook with the given id, or NULL if it is not found
g_hook_find ()
Finds a GHook in a GHookList using the given function to test for a match.
hook_list : a GHookList
need_valids : %TRUE if GHook elements which have been destroyed should be skipped
func : the function to call for each GHook, which should return TRUE when the GHook has been found
287
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
GHookFindFunc ()
hook : a GHook
g_hook_find_data ()
hook_list : a GHookList
need_valids : %TRUE if GHook elements which have been destroyed should be skipped
Returns : the GHook with the given data or NULL if no matching GHook is found
g_hook_find_func ()
hook_list : a GHookList
need_valids : %TRUE if GHook elements which have been destroyed should be skipped
Returns : the GHook with the given func or NULL if no matching GHook is found
g_hook_find_func_data ()
hook_list : a GHookList
need_valids : %TRUE if GHook elements which have been destroyed should be skipped
Returns : the GHook with the given func and data or NULL if no matching GHook is found
288
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
g_hook_first_valid ()
Returns the first GHook in a GHookList which has not been destroyed. The reference count for the
GHook is incremented, so you must call g_hook_unref() to restore it when no longer needed. (Or call
g_hook_next_valid() if you are stepping through the GHookList.)
hook_list : a GHookList
may_be_in_call : %TRUE if hooks which are currently running (e.g. in another thread) are considered
valid. If set to FALSE, these are skipped
g_hook_next_valid ()
Returns the next GHook in a GHookList which has not been destroyed. The reference count for
the GHook is incremented, so you must call g_hook_unref() to restore it when no longer needed. (Or
continue to call g_hook_next_valid() until NULL is returned.)
hook_list : a GHookList
may_be_in_call : %TRUE if hooks which are currently running (e.g. in another thread) are considered
valid. If set to FALSE, these are skipped
enum GHookFlagMask
typedef enum
{
G_HOOK_FLAG_ACTIVE = 1 << 0,
G_HOOK_FLAG_IN_CALL = 1 << 1,
G_HOOK_FLAG_MASK = 0x0f
} GHookFlagMask;
G_HOOK_FLAG_MASK A mask covering all bits reserved for hook flags; see G_HOOK_FLAGS_USER_SHIFT
G_HOOK_FLAGS()
hook : a GHook
289
CHAPTER 4. GLIB UTILITIES 4.9. HOOK FUNCTIONS
G_HOOK_FLAG_USER_SHIFT
The position of the first bit which is not reserved for internal use be the GHook implementation, i.e.
1 << G_HOOK_FLAG_USER_SHIFT is the first bit which can be used for application-defined flags.
G_HOOK()
G_HOOK_IS_VALID()
#define G_HOOK_IS_VALID(hook)
Returns TRUE if the GHook is valid, i.e. it is in a GHookList, it is active and it has not been destroyed.
hook : a GHook
G_HOOK_ACTIVE()
#define G_HOOK_ACTIVE(hook)
Returns TRUE if the GHook is active, which is normally TRUE until the GHook is destroyed.
hook : a GHook
G_HOOK_IN_CALL()
#define G_HOOK_IN_CALL(hook)
G_HOOK_IS_UNLINKED()
#define G_HOOK_IS_UNLINKED(hook)
g_hook_ref ()
290
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_hook_unref ()
Decrements the reference count of a GHook. If the reference count falls to 0, the GHook is removed
from the GHookList and g_hook_free() is called to free it.
hook_list : a GHookList
g_hook_free ()
Calls the GHookList finalize_hook function if it exists, and frees the memory allocated for the
GHook.
hook_list : a GHookList
g_hook_destroy ()
hook_list : a GHookList
hook_id : a hook ID
Returns : %TRUE if the GHook was found in the GHookList and destroyed
g_hook_destroy_link ()
Removes one GHook from a GHookList, marking it inactive and calling g_hook_unref() on it.
hook_list : a GHookList
291
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
Synopsis
#include <glib.h>
292
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
guint nkeys);
GDebugKey;
Description
These are portable utility functions.
Details
g_get_application_name ()
Gets a human-readable name for the application, as set by g_set_application_name(). This name
should be localized if possible, and is intended for display to the user. Contrast with g_get_prgname(),
which gets a non-localized name. If g_set_application_name() has not been called, returns the result of
g_get_prgname() (which may be NULL if g_set_prgname() has also not been called).
Since 2.2
g_set_application_name ()
Sets a human-readable name for the application. This name should be localized if possible, and
is intended for display to the user. Contrast with g_set_prgname(), which sets a non-localized name.
g_set_prgname() will be called automatically by gtk_init(), but g_set_application_name() will not.
Note that for thread safety reasons, this function can only be called once.
The application name will be used in contexts such as error messages, or when displaying an appli-
cation’s name in the task list.
Since 2.2
g_get_prgname ()
Gets the name of the program. This name should not be localized, contrast with g_get_application_name().
(If you are using GDK or GTK+ the program name is set in gdk_init(), which is called by gtk_init(). The
program name is found by taking the last component of argv[0].)
Returns : the name of the program. The returned string belongs to GLib and must not be modified or
freed.
293
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_set_prgname ()
Sets the name of the program. This name should not be localized, contrast with g_set_application_name().
Note that for thread-safety reasons this function can only be called once.
g_getenv ()
Returns the value of an environment variable. The name and value are in the GLib file name encod-
ing. On UNIX, this means the actual bytes which might or might not be in some consistent character
set and encoding. On Windows, it is in UTF-8. On Windows, in case the environment variable’s value
contains references to other environment variables, they are expanded.
variable : the environment variable to get, in the GLib file name encoding.
Returns : the value of the environment variable, or NULL if the environment variable is not found. The
returned string may be overwritten by the next call to g_getenv(), g_setenv() or g_unsetenv().
g_setenv ()
Sets an environment variable. Both the variable’s name and value should be in the GLib file name
encoding. On UNIX, this means that they can be any sequence of bytes. On Windows, they should be
in UTF-8.
Note that on some systems, when variables are overwritten, the memory used for the previous vari-
ables and its value isn’t reclaimed.
Since 2.4
g_unsetenv ()
Since 2.4
294
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_listenv ()
Returns : a NULL-terminated list of strings which must be freed with g_strfreev(). Programs that want
to be portable to Windows should typically use this function and g_getenv() instead of using the
environ array from the C library directly. On Windows, the strings in the environ array are in
system codepage encoding, while in most of the typical use cases for environment variables in
GLib-using programs you want the UTF-8 encoding that this function and g_getenv() provide.
Since 2.8
g_get_user_name ()
Gets the user name of the current user. The encoding of the returned string is system-defined. On
UNIX, it might be the preferred file name encoding, or something else, and there is no guarantee that it
is even consistent on a machine. On Windows, it is always UTF-8.
g_get_real_name ()
Gets the real name of the user. This usually comes from the user’s entry in the passwd file. The
encoding of the returned string is system-defined. (On Windows, it is, however, always UTF-8.) If the
real user name cannot be determined, the string "Unknown" is returned.
g_get_user_cache_dir ()
Returns a base directory in which to store non-essential, cached data specific to particular user.
On UNIX platforms this is determined using the mechanisms described in the XDG Base Directory
Specification
Since 2.6
g_get_user_data_dir ()
Returns a base directory in which to access application data such as icons that is customized for a
particular user.
On UNIX platforms this is determined using the mechanisms described in the XDG Base Directory
Specification
Since 2.6
295
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_get_user_config_dir ()
Returns a base directory in which to store user-specific application configuration information such
as user preferences and settings.
On UNIX platforms this is determined using the mechanisms described in the XDG Base Directory
Specification
Returns : a string owned by GLib that must not be modified or freed.
Since 2.6
enum GUserDirectory
typedef enum {
G_USER_DIRECTORY_DESKTOP,
G_USER_DIRECTORY_DOCUMENTS,
G_USER_DIRECTORY_DOWNLOAD,
G_USER_DIRECTORY_MUSIC,
G_USER_DIRECTORY_PICTURES,
G_USER_DIRECTORY_PUBLIC_SHARE,
G_USER_DIRECTORY_TEMPLATES,
G_USER_DIRECTORY_VIDEOS,
G_USER_N_DIRECTORIES
} GUserDirectory;
These are logical ids for special directories which are defined depending on the platform used. You
should use g_get_user_special_dir() to retrieve the full path associated to the logical id.
The GUserDirectory enumeration can be extended at later date. Not every platform has a directory
for every logical id in this enumeration.
G_USER_DIRECTORY_DESKTOP the user’s Desktop directory
G_USER_DIRECTORY_DOCUMENTS the user’s Documents directory
G_USER_DIRECTORY_DOWNLOAD the user’s Downloads directory
G_USER_DIRECTORY_MUSIC the user’s Music directory
G_USER_DIRECTORY_PICTURES the user’s Pictures directory
G_USER_DIRECTORY_PUBLIC_SHARE the user’s shared directory
G_USER_DIRECTORY_TEMPLATES the user’s Templates directory
G_USER_DIRECTORY_VIDEOS the user’s Movies directory
G_USER_N_DIRECTORIES the number of enum values
Since 2.14
g_get_user_special_dir ()
Returns the full path of a special directory using its logical id.
On Unix this is done using the XDG special user directories. For compatibility with existing practise,
G_USER_DIRECTORY_DESKTOP falls back to $HOME/Desktop when XDG special user directories
have not been set up.
Depending on the platform, the user might be able to change the path of the special directory without
requiring the session to restart; GLib will not reflect any change once the special directories are loaded.
296
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
Returns : the path to the specified special directory, or NULL if the logical id was not found. The re-
turned string is owned by GLib and should not be modified or freed.
Since 2.14
g_get_system_data_dirs ()
Returns an ordered list of base directories in which to access system-wide application data.
On UNIX platforms this is determined using the mechanisms described in the XDG Base Directory
Specification
On Windows the first elements in the list are the Application Data and Documents folders for All
Users. (These can be determined only on Windows 2000 or later and are not present in the list on other
Windows versions.) See documentation for CSIDL_COMMON_APPDATA and CSIDL_COMMON_DOCUMENTS.
Then follows the "share" subfolder in the installation folder for the package containing the DLL that
calls this function, if it can be determined.
Finally the list contains the "share" subfolder in the installation folder for GLib, and in the installation
folder for the package the application’s .exe file belongs to.
The installation folders above are determined by looking up the folder where the module (DLL or
EXE) in question is located. If the folder’s name is "bin", its parent is used, otherwise the folder itself.
Note that on Windows the returned list can vary depending on where this function is called.
Returns : a NULL-terminated array of strings owned by GLib that must not be modified or freed.
Since 2.6
g_get_system_config_dirs ()
Returns an ordered list of base directories in which to access system-wide configuration information.
On UNIX platforms this is determined using the mechanisms described in the XDG Base Directory
Specification
Returns : a NULL-terminated array of strings owned by GLib that must not be modified or freed.
Since 2.6
g_get_host_name ()
Since 2.8
297
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_get_home_dir ()
Gets the current user’s home directory as defined in the password database.
Note that in contrast to traditional UNIX tools, this function prefers passwd entries over the HOME
environment variable.
One of the reasons for this decision is that applications in many cases need special handling to deal
with the case where HOME is
Not owned by the user
Not writeable
Not even readable
Since applications are in general not written to deal with these situations it was considered better to
make g_get_home_dir() not pay attention to HOME and to return the real home directory for the user. If
applications want to pay attention to HOME, they can do:
const char *homedir = g_getenv ("HOME");
if (!homedir)
homedir = g_get_home_dir ();
g_get_tmp_dir ()
Gets the directory to use for temporary files. This is found from inspecting the environment variables
TMPDIR, TMP, and TEMP in that order. If none of those are defined "/tmp" is returned on UNIX and "C:\"
on Windows. The encoding of the returned string is system-defined. On Windows, it is always UTF-8.
The return value is never NULL.
Returns : the directory to use for temporary files.
g_get_current_dir ()
Gets the current directory. The returned string should be freed when no longer needed. The encoding
of the returned string is system defined. On Windows, it is always UTF-8.
Returns : the current directory.
g_basename ()
WARNING
g_basename has been deprecated since version 2.2 and should not be used
in newly-written code. Use g_path_get_basename() instead, but notice that
g_path_get_basename() allocates new memory for the returned string, unlike this function
which returns a pointer into the argument.
Gets the name of the file without any leading directory components. It returns a pointer into the
given file name string.
file_name : the name of the file.
Returns : the name of the file without any leading directory components.
298
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_dirname
#define g_dirname
WARNING
This function is deprecated and will be removed in the next major release of GLib. Use g_path_get_dirname()
instead.
Gets the directory components of a file name. If the file name has no directory components "." is
returned. The returned string should be freed when no longer needed.
Returns : the directory components of the file.
g_path_is_absolute ()
Returns TRUE if the given file_name is an absolute file name, i.e. it contains a full path from the
root directory such as "/usr/local" on UNIX or "C:\windows" on Windows systems.
file_name : a file name.
g_path_skip_root ()
Returns a pointer into file_name after the root component, i.e. after the "/" in UNIX or "C:\" under
Windows. If file_name is not an absolute path it returns NULL.
file_name : a file name.
g_path_get_basename ()
Gets the last component of the filename. If file_name ends with a directory separator it gets the
component before the last slash. If file_name consists only of directory separators (and on Windows,
possibly a drive letter), a single separator is returned. If file_name is empty, it gets ".".
file_name : the name of the file.
Returns : a newly allocated string containing the last component of the filename.
g_path_get_dirname ()
Gets the directory components of a file name. If the file name has no directory components "." is
returned. The returned string should be freed when no longer needed.
file_name : the name of the file.
299
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_build_filename ()
Creates a filename from a series of elements using the correct separator for filenames.
On Unix, this function behaves identically to g_build_path (G_DIR_SEPARATOR_S, first_-
element, ....).
On Windows, it takes into account that either the backslash (\ or slash (/) can be used as separator
in filenames, but otherwise behaves as on Unix. When file pathname separators need to be inserted, the
one that last previously occurred in the parameters (reading from left to right) is used.
No attempt is made to force the resulting filename to be an absolute path. If the first element is a
relative path, the result will be a relative path.
first_element : the first element in the path
g_build_filenamev ()
Behaves exactly like g_build_filename(), but takes the path elements as a string array, instead of
varargs. This function is mainly meant for language bindings.
args : NULL-terminated array of strings containing the path elements.
g_build_path ()
Creates a path from a series of elements using separator as the separator between elements. At the
boundary between two elements, any trailing occurrences of separator in the first element, or leading
occurrences of separator in the second element are removed and exactly one copy of the separator is
inserted.
Empty elements are ignored.
The number of leading copies of the separator on the result is the same as the number of leading
copies of the separator on the first non-empty element.
The number of trailing copies of the separator on the result is the same as the number of trailing
copies of the separator on the last non-empty element. (Determination of the number of trailing copies
is done without stripping leading copies, so if the separator is ABA, ABABA has 1 trailing copy.)
However, if there is only a single non-empty element, and there are no characters in that element not
part of the leading or trailing separators, then the result is exactly the original value of that element.
Other than for determination of the number of leading and trailing copies of the separator, elements
consisting only of copies of the separator are ignored.
separator : a string used to separator the elements of the path.
300
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_build_pathv ()
Behaves exactly like g_build_path(), but takes the path elements as a string array, instead of varargs.
This function is mainly meant for language bindings.
separator : a string used to separator the elements of the path.
g_format_size_for_display ()
Formats a size (for example the size of a file) into a human readable string. Sizes are rounded to
the nearest size prefix (KB, MB, GB) and are displayed rounded to the nearest tenth. E.g. the file size
3292528 bytes will be converted into the string "3.1 MB".
The prefix units base is 1024 (i.e. 1 KB is 1024 bytes).
This string should be freed with g_free() when not needed any longer.
size : a size in bytes.
g_find_program_in_path ()
Locates the first executable named program in the user’s path, in the same way that execvp() would
locate it. Returns an allocated string with the absolute path name, or NULL if the program is not found
in the path. If program is already an absolute path, returns a copy of program if program exists and is
executable, and NULL otherwise. On Windows, if program does not have a file type suffix, tries with
the suffixes .exe, .cmd, .bat and .com, and the suffixes in the PATHEXT environment variable.
On Windows, it looks for the file in the same way as CreateProcess() would. This means first in
the directory where the executing program was loaded from, then in the current directory, then in the
Windows 32-bit system directory, then in the Windows directory, and finally in the directories in the
PATH environment variable. If the program is found, the return value contains the full name including
the type suffix.
program : a program name in the GLib file name encoding
g_bit_nth_lsf ()
Find the position of the first bit set in mask, searching from (but not including) nth_bit upwards.
Bits are numbered from 0 (least significant) to sizeof(gulong) * 8 - 1 (31 or 63, usually). To start searching
from the 0th bit, set nth_bit to -1.
mask : a gulong containing flags.
Returns : the index of the first bit set which is higher than nth_bit.
301
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_bit_nth_msf ()
Find the position of the first bit set in mask, searching from (but not including) nth_bit downwards.
Bits are numbered from 0 (least significant) to sizeof(gulong) * 8 - 1 (31 or 63, usually). To start searching
from the last bit, set nth_bit to -1 or GLIB_SIZEOF_LONG * 8.
Returns : the index of the first bit set which is lower than nth_bit.
g_bit_storage ()
Gets the number of bits used to hold number , e.g. if number is 4, 3 bits are needed.
number : a guint.
g_spaced_primes_closest ()
Gets the smallest prime number from a built-in array of primes which is larger than num. This is used
within GLib to calculate the optimum size of a GHashTable.
The built-in array of primes ranges from 11 to 13845163 such that each prime is approximately 1.5-2
times the previous prime.
num : a guint.
Returns : the smallest prime number from a built-in array of primes which is larger than num.
g_atexit ()
302
CHAPTER 4. GLIB UTILITIES 4.10. MISCELLANEOUS UTILITY FUNCTIONS
g_parse_debug_string ()
Parses a string containing debugging options into a guint containing bit flags. This is used within
GDK and GTK+ to parse the debug options passed on the command line or through environment vari-
ables.
If string is equal to "all", all flags are set. If string is equal to "help", all the available keys in keys
are printed out to standard error.
keys : pointer to an array of GDebugKey which associate strings with bit flags.
GDebugKey
typedef struct {
const gchar *key;
guint value;
} GDebugKey;
GVoidFunc ()
Declares a type of function which takes no arguments and has no return value. It is used to specify
the type function passed to g_atexit().
GFreeFunc ()
Declares a type of function which takes an arbitrary data pointer argument and has no return value.
It is not currently used in GLib or GTK+.
g_qsort_with_data ()
This is just like the standard C qsort() function, but the comparison routine accepts a user data argu-
ment.
303
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
g_nullify_pointer ()
Synopsis
#include <glib.h>
GScanner;
GScannerConfig;
GScanner* g_scanner_new (const GScannerConfig *config_templ
void g_scanner_destroy (GScanner *scanner);
304
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
#define G_CSET_a_2_z
#define G_CSET_A_2_Z
#define G_CSET_DIGITS
#define G_CSET_LATINC
#define G_CSET_LATINS
enum GTokenType;
union GTokenValue;
enum GErrorType;
Description
The GScanner and its associated functions provide a general purpose lexical scanner.
Details
GScanner
typedef struct {
/* unused fields */
gpointer user_data;
guint max_parse_errors;
/* quarked data */
GData *qdata;
305
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
/* to be considered private */
GHashTable *symbol_table;
gint input_fd;
const gchar *text;
const gchar *text_end;
gchar *buffer;
guint scope_id;
306
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
gchar *buffer ;
guint scope_id ;
GScannerConfig
typedef struct {
/* Character sets
*/
gchar *cset_skip_characters; /* default: " \t\n" */
gchar *cset_identifier_first;
gchar *cset_identifier_nth;
gchar *cpair_comment_single; /* default: "#\n" */
Specifies the GScanner parser configuration. Most settings can be changed during the parsing phase
and will affect the lexical parsing of the next unpeeked token.
cset_skip_characters specifies which characters should be skipped by the scanner (the default is
the whitespace characters: space, tab, carriage-return and line-feed).
cset_identifier_first specifies the characters which can start identifiers (the default is G_CSET_a_2_z,
"_", and G_CSET_A_2_Z).
cset_identifier_nth specifies the characters which can be used in identifiers, after the first charac-
ter (the default is G_CSET_a_2_z, "_0123456789", G_CSET_A_2_Z, G_CSET_LATINS, G_CSET_LATINC).
cpair_comment_single specifies the characters at the start and end of single-line comments. The
default is "#\n" which means that single-line comments start with a ’#’ and continue until a ’\n’ (end of
line).
case_sensitive specifies if symbols are case sensitive (the default is FALSE).
307
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
skip_comment_multi specifies if multi-line comments are skipped and not returned as tokens (the
default is TRUE).
skip_comment_single specifies if single-line comments are skipped and not returned as tokens (the
default is TRUE).
scan_comment_multi specifies if multi-line comments are recognized (the default is TRUE).
scan_identifier specifies if identifiers are recognized (the default is TRUE).
scan_identifier_1char specifies if single-character identifiers are recognized (the default is FALSE).
scan_identifier_NULL specifies if NULL is reported as G_TOKEN_IDENTIFIER_NULL. (the de-
fault is FALSE).
scan_symbols specifies if symbols are recognized (the default is TRUE).
scan_binary specifies if binary numbers are recognized (the default is FALSE).
scan_octal specifies if octal numbers are recognized (the default is TRUE).
scan_float specifies if floating point numbers are recognized (the default is TRUE).
scan_hex specifies if hexadecimal numbers are recognized (the default is TRUE).
scan_hex_dollar specifies if ’$’ is recognized as a prefix for hexadecimal numbers (the default is
FALSE).
scan_string_sq specifies if strings can be enclosed in single quotes (the default is TRUE).
scan_string_dq specifies if strings can be enclosed in double quotes (the default is TRUE).
numbers_2_int specifies if binary, octal and hexadecimal numbers are reported as G_TOKEN_INT
(the default is TRUE).
int_2_float specifies if all numbers are reported as G_TOKEN_FLOAT (the default is FALSE).
identifier_2_string specifies if identifiers are reported as strings (the default is FALSE).
char_2_token specifies if characters are reported by setting token = ch or as G_TOKEN_CHAR
(the default is TRUE).
symbol_2_token specifies if symbols are reported by setting token = v_symbol or as G_TOKEN_SYMBOL
(the default is FALSE).
scope_0_fallback specifies if a symbol is searched for in the default scope in addition to the current
scope (the default is FALSE).
g_scanner_new ()
Creates a new GScanner. The config_templ structure specifies the initial settings of the scanner,
which are copied into the GScanner config field. If you pass NULL then the default settings are used.
g_scanner_destroy ()
scanner : a GScanner.
g_scanner_input_file ()
scanner : a GScanner.
308
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
g_scanner_sync_file_offset ()
Rewinds the filedescriptor to the current buffer position and blows the file read ahead buffer. This is
useful for third party uses of the scanners filedescriptor, which hooks onto the current scanning position.
scanner : a GScanner.
g_scanner_input_text ()
scanner : a GScanner.
g_scanner_peek_next_token ()
Parses the next token, without removing it from the input stream. The token data is placed in the
next_token, next_value, next_line, and next_position fields of the GScanner structure.
Note that, while the token is not removed from the input stream (i.e. the next call to g_scanner_get_next_token()
will return the same token), it will not be reevaluated. This can lead to surprising results when changing
scope or the scanner configuration after peeking the next token. Getting the next token after switching
the scope or configuration will return whatever was peeked before, regardless of any symbols that may
have been added or removed in the new scope.
scanner : a GScanner.
g_scanner_get_next_token ()
Parses the next token just like g_scanner_peek_next_token() and also removes it from the input
stream. The token data is placed in the token, value, line, and position fields of the GScanner struc-
ture.
scanner : a GScanner.
g_scanner_eof ()
Returns TRUE if the scanner has reached the end of the file or text buffer.
scanner : a GScanner.
Returns : %TRUE if the scanner has reached the end of the file or text buffer.
309
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
g_scanner_cur_line ()
Returns the current line in the input stream (counting from 1). This is the line of the last token parsed
via g_scanner_get_next_token().
scanner : a GScanner.
g_scanner_cur_position ()
Returns the current position in the current line (counting from 0). This is the position of the last token
parsed via g_scanner_get_next_token().
scanner : a GScanner.
g_scanner_cur_token ()
Gets the current token type. This is simply the token field in the GScanner structure.
scanner : a GScanner.
g_scanner_cur_value ()
Gets the current token value. This is simply the value field in the GScanner structure.
scanner : a GScanner.
g_scanner_set_scope ()
scanner : a GScanner.
310
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
g_scanner_scope_add_symbol ()
g_scanner_scope_foreach_symbol ()
Calls the given function for each of the symbol/value pairs in the given scope of the GScanner. The
function is passed the symbol and value of each pair, and the given user_data parameter.
scanner : a GScanner.
g_scanner_scope_lookup_symbol ()
Looks up a symbol in a scope and return its value. If the symbol is not bound in the scope, NULL is
returned.
scanner : a GScanner.
Returns : the value of symbol in the given scope, or NULL if symbol is not bound in the given scope.
g_scanner_scope_remove_symbol ()
311
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
g_scanner_add_symbol()
WARNING
g_scanner_add_symbol has been deprecated since version 2.2 and should not be
used in newly-written code. Use g_scanner_scope_add_symbol() instead.
scanner : a GScanner.
g_scanner_remove_symbol()
WARNING
g_scanner_remove_symbol has been deprecated since version 2.2 and should not
be used in newly-written code. Use g_scanner_scope_remove_symbol() instead.
scanner : a GScanner.
g_scanner_foreach_symbol()
WARNING
scanner : a GScanner.
312
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
g_scanner_freeze_symbol_table()
#define g_scanner_freeze_symbol_table(scanner)
WARNING
scanner : a GScanner.
g_scanner_thaw_symbol_table()
#define g_scanner_thaw_symbol_table(scanner)
WARNING
scanner : a GScanner.
g_scanner_lookup_symbol ()
Looks up a symbol in the current scope and return its value. If the symbol is not bound in the current
scope, NULL is returned.
scanner : a GScanner.
Returns : the value of symbol in the current scope, or NULL if symbol is not bound in the current scope.
g_scanner_warn ()
scanner : a GScanner.
313
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
g_scanner_error ()
scanner : a GScanner.
g_scanner_unexp_token ()
Outputs a message through the scanner’s msg_handler, resulting from an unexpected token in the in-
put stream. Note that you should not call g_scanner_peek_next_token() followed by g_scanner_unexp_token()
without an intermediate call to g_scanner_get_next_token(), as g_scanner_unexp_token() evaluates the
scanner’s current token (not the peeked token) to construct part of the message.
scanner : a GScanner.
identifier_spec : a string describing how the scanner’s user refers to identifiers (NULL defaults to
"identifier"). This is used if expected_token is G_TOKEN_IDENTIFIER or G_TOKEN_IDENTIFIER_NULL.
symbol_spec : a string describing how the scanner’s user refers to symbols (NULL defaults to "sym-
bol"). This is used if expected_token is G_TOKEN_SYMBOL or any token value greater than
G_TOKEN_LAST.
symbol_name : the name of the symbol, if the scanner’s current token is a symbol.
GScannerMsgFunc ()
scanner : a GScanner.
314
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
G_CSET_a_2_z
The set of lowercase ASCII alphabet characters. Used for specifying valid identifier characters in
GScannerConfig.
G_CSET_A_2_Z
The set of uppercase ASCII alphabet characters. Used for specifying valid identifier characters in
GScannerConfig.
G_CSET_DIGITS
The set of digits. Used for specifying valid identifier characters in GScannerConfig.
G_CSET_LATINC
#define G_CSET_LATINC
The set of uppercase ISO 8859-1 alphabet characters which are not ASCII characters. Used for speci-
fying valid identifier characters in GScannerConfig.
G_CSET_LATINS
#define G_CSET_LATINS
The set of lowercase ISO 8859-1 alphabet characters which are not ASCII characters. Used for speci-
fying valid identifier characters in GScannerConfig.
enum GTokenType
typedef enum
{
G_TOKEN_EOF = 0,
G_TOKEN_LEFT_PAREN = ’(’,
G_TOKEN_RIGHT_PAREN = ’)’,
G_TOKEN_LEFT_CURLY = ’{’,
G_TOKEN_RIGHT_CURLY = ’}’,
G_TOKEN_LEFT_BRACE = ’[’,
G_TOKEN_RIGHT_BRACE = ’]’,
G_TOKEN_EQUAL_SIGN = ’=’,
G_TOKEN_COMMA = ’,’,
G_TOKEN_NONE = 256,
G_TOKEN_ERROR,
G_TOKEN_CHAR,
G_TOKEN_BINARY,
G_TOKEN_OCTAL,
G_TOKEN_INT,
G_TOKEN_HEX,
G_TOKEN_FLOAT,
G_TOKEN_STRING,
315
CHAPTER 4. GLIB UTILITIES 4.11. LEXICAL SCANNER
G_TOKEN_SYMBOL,
G_TOKEN_IDENTIFIER,
G_TOKEN_IDENTIFIER_NULL,
G_TOKEN_COMMENT_SINGLE,
G_TOKEN_COMMENT_MULTI,
G_TOKEN_LAST
} GTokenType;
union GTokenValue
union GTokenValue
{
gpointer v_symbol;
gchar *v_identifier;
gulong v_binary;
gulong v_octal;
gulong v_int;
guint64 v_int64;
gdouble v_float;
gulong v_hex;
gchar *v_string;
gchar *v_comment;
guchar v_char;
guint v_error;
};
enum GErrorType
typedef enum
{
G_ERR_UNKNOWN,
G_ERR_UNEXP_EOF,
G_ERR_UNEXP_EOF_IN_STRING,
G_ERR_UNEXP_EOF_IN_COMMENT,
G_ERR_NON_DIGIT_IN_CONST,
G_ERR_DIGIT_RADIX,
G_ERR_FLOAT_RADIX,
G_ERR_FLOAT_MALFORMED
} GErrorType;
The possible errors, used in the v_error field of GTokenValue, when the token is a G_TOKEN_ERROR.
G_ERR_UNKNOWN unknown error.
G_ERR_UNEXP_EOF unexpected end of file.
G_ERR_UNEXP_EOF_IN_STRING unterminated string constant.
G_ERR_UNEXP_EOF_IN_COMMENT unterminated comment.
G_ERR_NON_DIGIT_IN_CONST non-digit character in a number.
316
CHAPTER 4. GLIB UTILITIES 4.12. AUTOMATIC STRING COMPLETION
Synopsis
#include <glib.h>
GCompletion;
GCompletion* g_completion_new (GCompletionFunc func);
gchar * (*GCompletionFunc) (gpointer );
void g_completion_add_items (GCompletion *cmp,
GList *items);
void g_completion_remove_items (GCompletion *cmp,
GList *items);
void g_completion_clear_items (GCompletion *cmp);
GList* g_completion_complete (GCompletion *cmp,
const gchar *prefix,
gchar **new_prefix);
GList* g_completion_complete_utf8 (GCompletion *cmp,
const gchar *prefix,
gchar **new_prefix);
void g_completion_set_compare (GCompletion *cmp,
GCompletionStrncmpFunc strncm
gint (*GCompletionStrncmpFunc) (const gchar *s1,
const gchar *s2,
gsize n);
void g_completion_free (GCompletion *cmp);
Description
GCompletion provides support for automatic completion of a string using any group of target strings.
It is typically used for file name completion as is common in many UNIX shells.
A GCompletion is created using g_completion_new(). Target items are added and removed with
g_completion_add_items(), g_completion_remove_items() and g_completion_clear_items(). A comple-
tion attempt is requested with g_completion_complete() or g_completion_complete_utf8(). When no
longer needed, the GCompletion is freed with g_completion_free().
Items in the completion can be simple strings (e.g. filenames), or pointers to arbitrary data struc-
tures. If data structures are used you must provide a GCompletionFunc in g_completion_new(), which
retrieves the item’s string from the data structure. You can change the way in which strings are com-
pared by setting a different GCompletionStrncmpFunc in g_completion_set_compare().
Details
GCompletion
typedef struct {
GList* items;
GCompletionFunc func;
317
CHAPTER 4. GLIB UTILITIES 4.12. AUTOMATIC STRING COMPLETION
gchar* prefix;
GList* cache;
GCompletionStrncmpFunc strncmp_func;
} GCompletion;
GCompletionFunc func; function which is called to get the string associated with a target item. It is
NULL if the target items are strings.
GCompletionStrncmpFunc strncmp_func; The function to use when comparing strings. Use g_completion_set_compare()
to modify this function.
g_completion_new ()
func : the function to be called to return the string representing an item in the GCompletion, or NULL
if strings are going to be used as the GCompletion items.
GCompletionFunc ()
Specifies the type of the function passed to g_completion_new(). It should return the string corre-
sponding to the given target item. This is used when you use data structures as GCompletion items.
g_completion_add_items ()
g_completion_remove_items ()
318
CHAPTER 4. GLIB UTILITIES 4.12. AUTOMATIC STRING COMPLETION
g_completion_clear_items ()
g_completion_complete ()
Attempts to complete the string prefix using the GCompletion target items.
prefix : the prefix string, typically typed by the user, which is compared with each of the items.
new_prefix : if non-NULL, returns the longest prefix which is common to all items that matched pre-
fix , or NULL if no items matched prefix . This string should be freed when no longer needed.
Returns : the list of items whose strings begin with prefix . This should not be changed.
g_completion_complete_utf8 ()
Attempts to complete the string prefix using the GCompletion target items. In contrast to g_completion_complete
this function returns the largest common prefix that is a valid UTF-8 string, omitting a possible common
partial character.
You should use this function instead of g_completion_complete() if your items are UTF-8 strings.
prefix : the prefix string, typically used by the user, which is compared with each of the items
new_prefix : if non-NULL, returns the longest prefix which is common to all items that matched pre-
fix , or NULL if no items matched prefix . This string should be freed when no longer needed.
Returns : the list of items whose strings begin with prefix . This should not be changed.
Since 2.4
g_completion_set_compare ()
Sets the function to use for string comparisons. The default string comparison function is strncmp().
cmp : a GCompletion.
319
CHAPTER 4. GLIB UTILITIES 4.13. TIMERS
GCompletionStrncmpFunc ()
Specifies the type of the function passed to g_completion_set_compare(). This is used when you use
strings as GCompletion items.
s1 : string to compare with s2.
Returns : an integer less than, equal to, or greater than zero if the first n bytes of s1 is found, respectively,
to be less than, to match, or to be greater than the first n bytes of s2.
g_completion_free ()
4.13 Timers
Name
Timers – keep track of elapsed time
Synopsis
#include <glib.h>
GTimer;
GTimer* g_timer_new (void);
void g_timer_start (GTimer *timer);
void g_timer_stop (GTimer *timer);
void g_timer_continue (GTimer *timer);
gdouble g_timer_elapsed (GTimer *timer,
gulong *microseconds);
void g_timer_reset (GTimer *timer);
void g_timer_destroy (GTimer *timer);
Description
GTimer records a start time, and counts microseconds elapsed since that time. This is done somewhat
differently on different platforms, and can be tricky to get exactly right, so GTimer provides a portable/-
convenient interface.
N OTE
GTimer uses a higher-quality clock when thread support is available. Therefore, calling
g_thread_init() while timers are running may lead to unreliable results. It is best to call
g_thread_init() before starting any timers, if you are using threads at all.
320
CHAPTER 4. GLIB UTILITIES 4.13. TIMERS
Details
GTimer
g_timer_new ()
Creates a new timer, and starts timing (i.e. g_timer_start() is implicitly called for you).
Returns : a new GTimer.
g_timer_start ()
Marks a start time, so that future calls to g_timer_elapsed() will report the time since g_timer_start()
was called. g_timer_new() automatically marks the start time, so no need to call g_timer_start() imme-
diately after creating the timer.
timer : a GTimer.
g_timer_stop ()
Marks an end time, so calls to g_timer_elapsed() will return the difference between this end time and
the start time.
timer : a GTimer.
g_timer_continue ()
Resumes a timer that has previously been stopped with g_timer_stop(). g_timer_stop() must be
called before using this function.
timer : a GTimer.
Since 2.4
g_timer_elapsed ()
If timer has been started but not stopped, obtains the time since the timer was started. If timer has
been stopped, obtains the elapsed time between the time it was started and the time it was stopped. The
return value is the number of seconds elapsed, including any fractional part. The microseconds out
parameter is essentially useless.
WARNING
321
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
timer : a GTimer.
microseconds : return location for the fractional part of seconds elapsed, in microseconds (that is, the
total number of microseconds elapsed, modulo 1000000), or NULL
Returns : seconds elapsed as a floating point value, including any fractional part.
g_timer_reset ()
This function is useless; it’s fine to call g_timer_start() on an already-started timer to reset the start
time, so g_timer_reset() serves no purpose.
timer : a GTimer.
g_timer_destroy ()
Synopsis
#include <glib.h>
enum GSpawnError;
#define G_SPAWN_ERROR
enum GSpawnFlags;
void (*GSpawnChildSetupFunc) (gpointer user_data);
gboolean g_spawn_async_with_pipes (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
GPid *child_pid,
gint *standard_input,
gint *standard_output,
gint *standard_error,
GError **error);
gboolean g_spawn_async (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
GPid *child_pid,
GError **error);
gboolean g_spawn_sync (const gchar *working_directory,
322
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_se
gpointer user_data,
gchar **standard_output,
gchar **standard_error,
gint *exit_status,
GError **error);
gboolean g_spawn_command_line_async (const gchar *command_line,
GError **error);
gboolean g_spawn_command_line_sync (const gchar *command_line,
gchar **standard_output,
gchar **standard_error,
gint *exit_status,
GError **error);
void g_spawn_close_pid (GPid pid);
Description
Details
enum GSpawnError
typedef enum
{
G_SPAWN_ERROR_FORK, /* fork failed due to lack of memory */
G_SPAWN_ERROR_READ, /* read or select on pipes failed */
G_SPAWN_ERROR_CHDIR, /* changing to working dir failed */
G_SPAWN_ERROR_ACCES, /* execv() returned EACCES */
G_SPAWN_ERROR_PERM, /* execv() returned EPERM */
G_SPAWN_ERROR_2BIG, /* execv() returned E2BIG */
G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
G_SPAWN_ERROR_NAMETOOLONG, /* "" "" ENAMETOOLONG */
G_SPAWN_ERROR_NOENT, /* "" "" ENOENT */
G_SPAWN_ERROR_NOMEM, /* "" "" ENOMEM */
G_SPAWN_ERROR_NOTDIR, /* "" "" ENOTDIR */
G_SPAWN_ERROR_LOOP, /* "" "" ELOOP */
G_SPAWN_ERROR_TXTBUSY, /* "" "" ETXTBUSY */
G_SPAWN_ERROR_IO, /* "" "" EIO */
G_SPAWN_ERROR_NFILE, /* "" "" ENFILE */
G_SPAWN_ERROR_MFILE, /* "" "" EMFLE */
G_SPAWN_ERROR_INVAL, /* "" "" EINVAL */
G_SPAWN_ERROR_ISDIR, /* "" "" EISDIR */
G_SPAWN_ERROR_LIBBAD, /* "" "" ELIBBAD */
G_SPAWN_ERROR_FAILED /* other fatal failure, error->message
* should explain
*/
} GSpawnError;
323
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
G_SPAWN_ERROR
Error domain for spawning processes. Errors in this domain will be from the GSpawnError enumer-
ation. See GError for information on error domains.
enum GSpawnFlags
typedef enum
{
G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
G_SPAWN_DO_NOT_REAP_CHILD = 1 << 1,
/* look for argv[0] in the path i.e. use execvp() */
G_SPAWN_SEARCH_PATH = 1 << 2,
/* Dump output to /dev/null */
G_SPAWN_STDOUT_TO_DEV_NULL = 1 << 3,
G_SPAWN_STDERR_TO_DEV_NULL = 1 << 4,
G_SPAWN_CHILD_INHERITS_STDIN = 1 << 5,
G_SPAWN_FILE_AND_ARGV_ZERO = 1 << 6
} GSpawnFlags;
324
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
G_SPAWN_CHILD_INHERITS_STDIN the child will inherit the parent’s standard input (by default, the
child’s standard input is attached to /dev/null).
G_SPAWN_FILE_AND_ARGV_ZERO the first element of argv is the file to execute, while the remaining
elements are the actual argument vector to pass to the file. Normally g_spawn_async_with_pipes()
uses argv[0] as the file to execute, and passes all of argv to the child.
GSpawnChildSetupFunc ()
Specifies the type of the setup function passed to g_spawn_async(), g_spawn_sync() and g_spawn_async_with_pip
On POSIX platforms it is called in the child after GLib has performed all the setup it plans to perform
but before calling exec(). On POSIX actions taken in this function will thus only affect the child, not the
parent.
Note that POSIX allows only async-signal-safe functions (see signal(7)) to be called in the child be-
tween fork() and exec(), which drastically limits the usefulness of child setup functions.
Also note that modifying the environment from the child setup function may not have the intended
effect, since it will get overridden by a non-NULL env argument to the g_spawn... functions.
On Windows the function is called in the parent. Its usefulness on Windows is thus questionable. In
many cases executing the child setup function in the parent can have ill effects, and you should be very
careful when porting software to Windows that uses child setup functions.
g_spawn_async_with_pipes ()
Executes a child program asynchronously (your program will not block waiting for the child to exit).
The child program is specified by the only argument that must be provided, argv . argv should be a
NULL-terminated array of strings, to be passed as the argument vector for the child. The first string in
argv is of course the name of the program to execute. By default, the name of the program must be a
full path; the PATH shell variable will only be searched if you pass the G_SPAWN_SEARCH_PATH flag.
On Windows, note that all the string or string vector arguments to this function and the other
g_spawn*() functions are in UTF-8, the GLib file name encoding. Unicode characters that are not part
of the system codepage passed in these arguments will be correctly available in the spawned program
only if it uses wide character API to retrieve its command line. For C programs built with Microsoft’s
tools it is enough to make the program have a wmain() instead of main(). wmain() has a wide character
argument vector as parameter.
At least currently, mingw doesn’t support wmain(), so if you use mingw to develop the spawned
program, it will have to call the undocumented function __wgetmainargs() to get the wide character ar-
gument vector and environment. See gspawn-win32-helper.c in the GLib sources or init.c in the mingw
runtime sources for a prototype for that function. Alternatively, you can retrieve the Win32 system
level wide character command line passed to the spawned program using the GetCommandLineW()
function.
On Windows the low-level child process creation API CreateProcess() doesn’t use argument
vectors, but a command line. The C runtime library’s spawn*() family of functions (which g_spawn_async_with_pip
325
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
eventually calls) paste the argument vector elements together into a command line, and the C runtime
startup code does a corresponding reconstruction of an argument vector from the command line, to be
passed to main(). Complications arise when you have argument vector elements that contain spaces of
double quotes. The spawn*() functions don’t do any quoting or escaping, but on the other hand the
startup code does do unquoting and unescaping in order to enable receiving arguments with embedded
spaces or double quotes. To work around this asymmetry, g_spawn_async_with_pipes() will do quoting
and escaping on argument vector elements that need it before calling the C runtime spawn() function.
The returned child_pid on Windows is a handle to the child process, not its identifier. Process
handles and process identifiers are different concepts on Windows.
envp is a NULL-terminated array of strings, where each string has the form KEY=VALUE. This will
become the child’s environment. If envp is NULL, the child inherits its parent’s environment.
flags should be the bitwise OR of any flags you want to affect the function’s behaviour. The
G_SPAWN_DO_NOT_REAP_CHILD means that the child will not automatically be reaped; you must
use a GChildWatch source to be notified about the death of the child process. Eventually you must call
g_spawn_close_pid() on the child_pid , in order to free resources which may be associated with the
child process. (On Unix, using a GChildWatch source is equivalent to calling waitpid() or handling the
SIGCHLD signal manually. On Windows, calling g_spawn_close_pid() is equivalent to calling Close-
Handle() on the process handle returned in child_pid ).
G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent’s open file descriptors will be in-
herited by the child; otherwise all descriptors except stdin/stdout/stderr will be closed before calling
exec() in the child. G_SPAWN_SEARCH_PATH means that argv[0] need not be an absolute path, it
will be looked for in the user’s PATH. G_SPAWN_STDOUT_TO_DEV_NULL means that the child’s stan-
dard output will be discarded, instead of going to the same location as the parent’s standard output. If
you use this flag, standard_output must be NULL. G_SPAWN_STDERR_TO_DEV_NULL means that
the child’s standard error will be discarded, instead of going to the same location as the parent’s stan-
dard error. If you use this flag, standard_error must be NULL. G_SPAWN_CHILD_INHERITS_STDIN
means that the child will inherit the parent’s standard input (by default, the child’s standard input is at-
tached to /dev/null). If you use this flag, standard_input must be NULL. G_SPAWN_FILE_AND_ARGV_ZERO
means that the first element of argv is the file to execute, while the remaining elements are the actual
argument vector to pass to the file. Normally g_spawn_async_with_pipes() uses argv [0] as the file to
execute, and passes all of argv to the child.
child_setup and user_data are a function and user data. On POSIX platforms, the function is
called in the child after GLib has performed all the setup it plans to perform (including creating pipes,
closing file descriptors, etc.) but before calling exec(). That is, child_setup is called just before calling
exec() in the child. Obviously actions taken in this function will only affect the child, not the parent.
On Windows, there is no separate fork() and exec() functionality. Child processes are created and
run with a single API call, CreateProcess(). There is no sensible thing child_setup could be used for on
Windows so it is ignored and not called.
If non-NULL, child_pid will on Unix be filled with the child’s process ID. You can use the pro-
cess ID to send signals to the child, or to use g_child_watch_add() (or waitpid()) if you specified the
G_SPAWN_DO_NOT_REAP_CHILD flag. On Windows, child_pid will be filled with a handle to the
child process only if you specified the G_SPAWN_DO_NOT_REAP_CHILD flag. You can then access the
child process using the Win32 API, for example wait for its termination with the WaitFor*() functions,
or examine its exit code with GetExitCodeProcess(). You should close the handle with CloseHandle() or
g_spawn_close_pid() when you no longer need it.
If non-NULL, the standard_input, standard_output, standard_error locations will be filled with
file descriptors for writing to the child’s standard input or reading from its standard output or standard
error. The caller of g_spawn_async_with_pipes() must close these file descriptors when they are no
longer in use. If these parameters are NULL, the corresponding pipe won’t be created.
If standard_input is NULL, the child’s standard input is attached to /dev/null unless G_SPAWN_CHILD_INHERITS_S
is set.
If standard_error is NULL, the child’s standard error goes to the same location as the parent’s
standard error unless G_SPAWN_STDERR_TO_DEV_NULL is set.
If standard_output is NULL, the child’s standard output goes to the same location as the parent’s
standard output unless G_SPAWN_STDOUT_TO_DEV_NULL is set.
error can be NULL to ignore errors, or non-NULL to report errors. If an error is set, the function
returns FALSE. Errors are reported even if they occur in the child (for example if the executable in a-
rgv[0] is not found). Typically the message field of returned errors should be displayed to users.
Possible errors are those from the G_SPAWN_ERROR domain.
326
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
N OTE
If you are writing a GTK+ application, and the program you are spawning is a graphical
application, too, then you may want to use gdk_spawn_on_screen_with_pipes() instead
to ensure that the spawned program opens its windows on the right screen.
working_directory : child’s current working directory, or NULL to inherit parent’s, in the GLib file
name encoding
argv : child’s argument vector, in the GLib file name encoding
envp : child’s environment, or NULL to inherit parent’s, in the GLib file name encoding
standard_input : return location for file descriptor to write to child’s stdin, or NULL
standard_output : return location for file descriptor to read child’s stdout, or NULL
standard_error : return location for file descriptor to read child’s stderr, or NULL
g_spawn_async ()
See g_spawn_async_with_pipes() for a full description; this function simply calls the g_spawn_async_with_pipes()
without any pipes.
You should call g_spawn_close_pid() on the returned child process reference when you don’t need
it any more.
N OTE
If you are writing a GTK+ application, and the program you are spawning is a graphical
application, too, then you may want to use gdk_spawn_on_screen() instead to ensure that
the spawned program opens its windows on the right screen.
327
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
N OTE
Note that the returned child_pid on Windows is a handle to the child process and not
its identifier. Process handles and process identifiers are different concepts on Windows.
g_spawn_sync ()
Executes a child synchronously (waits for the child to exit before returning). All output from the child
is stored in standard_output and standard_error , if those parameters are non-NULL. Note that you
must set the G_SPAWN_STDOUT_TO_DEV_NULL and G_SPAWN_STDERR_TO_DEV_NULL flags
when passing NULL for standard_output and standard_error . If exit_status is non-NULL, the exit
status of the child is stored there as it would be returned by waitpid(); standard UNIX macros such as
WIFEXITED() and WEXITSTATUS() must be used to evaluate the exit status. Note that this function call
waitpid() even if exit_status is NULL, and does not accept the G_SPAWN_DO_NOT_REAP_CHILD
flag. If an error occurs, no data is returned in standard_output, standard_error , or exit_status.
This function calls g_spawn_async_with_pipes() internally; see that function for full details on the
other parameters and details on how these functions work on Windows.
328
CHAPTER 4. GLIB UTILITIES 4.14. SPAWNING PROCESSES
exit_status : return location for child exit status, as returned by waitpid(), or NULL
g_spawn_command_line_async ()
A simple version of g_spawn_async() that parses a command line with g_shell_parse_argv() and
passes it to g_spawn_async(). Runs a command line in the background. Unlike g_spawn_async(), the
G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note that G_SPAWN_SEARCH_PATH
can have security implications, so consider using g_spawn_async() directly if appropriate. Possible
errors are those from g_shell_parse_argv() and g_spawn_async().
The same concerns on Windows apply as for g_spawn_command_line_sync().
command_line : a command line
g_spawn_command_line_sync ()
A simple version of g_spawn_sync() with little-used parameters removed, taking a command line
instead of an argument vector. See g_spawn_sync() for full details. command_line will be parsed by
g_shell_parse_argv(). Unlike g_spawn_sync(), the G_SPAWN_SEARCH_PATH flag is enabled. Note
that G_SPAWN_SEARCH_PATH can have security implications, so consider using g_spawn_sync() di-
rectly if appropriate. Possible errors are those from g_spawn_sync() and those from g_shell_parse_argv().
If exit_status is non-NULL, the exit status of the child is stored there as it would be returned by
waitpid(); standard UNIX macros such as WIFEXITED() and WEXITSTATUS() must be used to evaluate
the exit status.
On Windows, please note the implications of g_shell_parse_argv() parsing command_line. Pars-
ing is done according to Unix shell rules, not Windows command interpreter rules. Space is a separa-
tor, and backslashes are special. Thus you cannot simply pass a command_line containing canonical
Windows paths, like "c:\\program files\\app\\app.exe", as the backslashes will be eaten, and the
space will act as a separator. You need to enclose such paths with single quotes, like "’c:\\program
files\\app\\app.exe’ ’e:\\folder\\argument.txt’".
command_line : a command line
329
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
g_spawn_close_pid ()
On some platforms, notably Windows, the GPid type represents a resource which must be closed to
prevent resource leaking. g_spawn_close_pid() is provided for this purpose. It should be used on all
platforms, even though it doesn’t do anything under UNIX.
Synopsis
#include <glib.h>
#include <glib/gstdio.h>
enum GFileError;
#define G_FILE_ERROR
enum GFileTest;
GFileError g_file_error_from_errno (gint err_no);
gboolean g_file_get_contents (const gchar *filename,
gchar **contents,
gsize *length,
GError **error);
gboolean g_file_set_contents (const gchar *filename,
const gchar *contents,
gssize length,
GError **error);
gboolean g_file_test (const gchar *filename,
GFileTest test);
gint g_mkstemp (gchar *tmpl);
gint g_file_open_tmp (const gchar *tmpl,
gchar **name_used,
GError **error);
gchar * g_file_read_link (const gchar *filename,
GError **error);
int g_mkdir_with_parents (const gchar *pathname,
int mode);
GDir;
GDir * g_dir_open (const gchar *path,
guint flags,
GError **error);
const gchar * g_dir_read_name (GDir *dir);
void g_dir_rewind (GDir *dir);
void g_dir_close (GDir *dir);
GMappedFile;
GMappedFile * g_mapped_file_new (const gchar *filename,
gboolean writable,
GError **error);
void g_mapped_file_free (GMappedFile *file);
gsize g_mapped_file_get_length (GMappedFile *file);
330
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
Description
There is a group of functions which wrap the common POSIX functions dealing with filenames (g_open(),
g_rename(), g_mkdir(), g_stat(), g_unlink(), g_remove(), g_fopen(), g_freopen()). The point of these
wrappers is to make it possible to handle file names with any Unicode characters in them on Windows
without having to use ifdefs and the wide character API in the application code.
The pathname argument should be in the GLib file name encoding. On POSIX this is the actual
on-disk encoding which might correspond to the locale settings of the process (or the G_FILENAME_E-
NCODING environment variable), or not.
On Windows the GLib file name encoding is UTF-8. Note that the Microsoft C library does not
use UTF-8, but has separate APIs for current system code page and wide characters (UTF-16). The
GLib wrappers call the wide character API if present (on modern Windows systems), otherwise convert
to/from the system code page.
Another group of functions allows to open and read directories in the GLib file name encoding.
These are g_dir_open(), g_dir_read_name(), g_dir_rewind(), g_dir_close().
Details
enum GFileError
typedef enum
{
G_FILE_ERROR_EXIST,
G_FILE_ERROR_ISDIR,
G_FILE_ERROR_ACCES,
G_FILE_ERROR_NAMETOOLONG,
G_FILE_ERROR_NOENT,
G_FILE_ERROR_NOTDIR,
331
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
G_FILE_ERROR_NXIO,
G_FILE_ERROR_NODEV,
G_FILE_ERROR_ROFS,
G_FILE_ERROR_TXTBSY,
G_FILE_ERROR_FAULT,
G_FILE_ERROR_LOOP,
G_FILE_ERROR_NOSPC,
G_FILE_ERROR_NOMEM,
G_FILE_ERROR_MFILE,
G_FILE_ERROR_NFILE,
G_FILE_ERROR_BADF,
G_FILE_ERROR_INVAL,
G_FILE_ERROR_PIPE,
G_FILE_ERROR_AGAIN,
G_FILE_ERROR_INTR,
G_FILE_ERROR_IO,
G_FILE_ERROR_PERM,
G_FILE_ERROR_NOSYS,
G_FILE_ERROR_FAILED
} GFileError;
Values corresponding to errno codes returned from file operations on UNIX. Unlike errno codes,
GFileError values are available on all systems, even Windows. The exact meaning of each code depends
on what sort of file operation you were performing; the UNIX documentation gives more details. The
following error code descriptions come from the GNU C Library manual, and are under the copyright
of that manual.
It’s not very portable to make detailed assumptions about exactly which errors will be returned from
a given operation. Some errors don’t occur on some systems, etc., sometimes there are subtle differences
in when a system will report a given error, etc.
G_FILE_ERROR_EXIST Operation not permitted; only the owner of the file (or other resource) or pro-
cesses with special privileges can perform the operation.
G_FILE_ERROR_ISDIR File is a directory; you cannot open a directory for writing, or create or remove
hard links to it.
G_FILE_ERROR_ACCES Permission denied; the file permissions do not allow the attempted operation.
G_FILE_ERROR_NAMETOOLONG Filename too long.
G_FILE_ERROR_NOENT No such file or directory. This is a "file doesn’t exist" error for ordinary files
that are referenced in contexts where they are expected to already exist.
G_FILE_ERROR_NOTDIR A file that isn’t a directory was specified when a directory is required.
G_FILE_ERROR_NXIO No such device or address. The system tried to use the device represented by a
file you specified, and it couldn’t find the device. This can mean that the device file was installed
incorrectly, or that the physical device is missing or not correctly attached to the computer.
G_FILE_ERROR_NODEV This file is of a type that doesn’t support mapping.
G_FILE_ERROR_ROFS The directory containing the new link can’t be modified because it’s on a read-
only file system.
G_FILE_ERROR_TXTBSY Text file busy.
G_FILE_ERROR_FAULT You passed in a pointer to bad memory. (GLib won’t reliably return this, don’t
pass in pointers to bad memory.)
G_FILE_ERROR_LOOP Too many levels of symbolic links were encountered in looking up a file name.
This often indicates a cycle of symbolic links.
G_FILE_ERROR_NOSPC No space left on device; write operation on a file failed because the disk is full.
G_FILE_ERROR_NOMEM No memory available. The system cannot allocate more virtual memory be-
cause its capacity is full.
332
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
G_FILE_ERROR_MFILE The current process has too many files open and can’t open any more. Dupli-
cate descriptors do count toward this limit.
G_FILE_ERROR_NFILE There are too many distinct file openings in the entire system.
G_FILE_ERROR_BADF Bad file descriptor; for example, I/O on a descriptor that has been closed or
reading from a descriptor open only for writing (or vice versa).
G_FILE_ERROR_INVAL Invalid argument. This is used to indicate various kinds of problems with
passing the wrong argument to a library function.
G_FILE_ERROR_PIPE Broken pipe; there is no process reading from the other end of a pipe. Every
library function that returns this error code also generates a ‘SIGPIPE’ signal; this signal terminates
the program if not handled or blocked. Thus, your program will never actually see this code unless
it has handled or blocked ‘SIGPIPE’.
G_FILE_ERROR_AGAIN Resource temporarily unavailable; the call might work if you try again later.
G_FILE_ERROR_INTR Interrupted function call; an asynchronous signal occurred and prevented com-
pletion of the call. When this happens, you should try the call again.
G_FILE_ERROR_IO Input/output error; usually used for physical read or write errors. i.e. the disk or
other physical device hardware is returning errors.
G_FILE_ERROR_PERM Operation not permitted; only the owner of the file (or other resource) or pro-
cesses with special privileges can perform the operation.
G_FILE_ERROR_NOSYS Function not implemented; this indicates that the system is missing some
functionality.
G_FILE_ERROR_FAILED Does not correspond to a UNIX error code; this is the standard "failed for
unspecified reason" error code present in all GError error code enumerations. Returned if no
specific code applies.
G_FILE_ERROR
Error domain for file operations. Errors in this domain will be from the GFileError enumeration. See
GError for information on error domains.
enum GFileTest
typedef enum
{
G_FILE_TEST_IS_REGULAR = 1 << 0,
G_FILE_TEST_IS_SYMLINK = 1 << 1,
G_FILE_TEST_IS_DIR = 1 << 2,
G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
G_FILE_TEST_EXISTS = 1 << 4
} GFileTest;
G_FILE_TEST_IS_REGULAR %TRUE if the file is a regular file (not a directory). Note that this test will
also return TRUE if the tested file is a symlink to a regular file.
G_FILE_TEST_EXISTS %TRUE if the file exists. It may or may not be a regular file.
333
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
g_file_error_from_errno ()
Gets a GFileError constant based on the passed-in errno. For example, if you pass in EEXIST
this function returns G_FILE_ERROR_EXIST. Unlike errno values, you can portably assume that all
GFileError values will exist.
Normally a GFileError value goes into a GError returned from a function that manipulates files. So
you would use g_file_error_from_errno() when constructing a GError.
g_file_get_contents ()
Reads an entire file into allocated memory, with good error checking.
If the call was successful, it returns TRUE and sets contents to the file contents and length to the
length of the file contents in bytes. The string stored in contents will be nul-terminated, so for text
files you can pass NULL for the length argument. If the call was not successful, it returns FALSE
and sets error . The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError
enumeration. In the error case, contents is set to NULL and length is set to zero.
filename : name of a file to read contents from, in the GLib file name encoding
contents : location to store an allocated string, use g_free() to free the returned string
g_file_set_contents ()
Writes all of contents to a file named filename, with good error checking. If a file called filename
already exists it will be overwritten.
This write is atomic in the sense that it is first written to a temporary file which is then renamed to
the final name. Notes:
• On Unix, if filename already exists hard links to filename will break. Also since the file is recre-
ated, existing permissions, access control lists, metadata etc. may be lost. If filename is a symbolic
link, the link itself will be replaced, not the linked file.
• On Windows renaming a file will not remove an existing file with the new name, so on Windows
there is a race condition between the existing file being removed and the temporary file being
renamed.
• On Windows there is no way to remove a file that is open to some process, or mapped into memory.
Thus, this function will fail if filename already exists and is open.
If the call was sucessful, it returns TRUE. If the call was not successful, it returns FALSE and sets er-
ror . The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError enumeration.
334
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
filename : name of a file to write contents to, in the GLib file name encoding
Since 2.8
g_file_test ()
Returns TRUE if any of the tests in the bitfield test are TRUE. For example, (G_FILE_TEST_EXI-
STS | G_FILE_TEST_IS_DIR) will return TRUE if the file exists; the check whether it’s a directory
doesn’t matter since the existence test is TRUE. With the current set of available tests, there’s no point
passing in more than one test at a time.
Apart from G_FILE_TEST_IS_SYMLINK all tests follow symbolic links, so for a symbolic link to a
regular file g_file_test() will return TRUE for both G_FILE_TEST_IS_SYMLINK and G_FILE_TEST_IS_REGULAR.
Note, that for a dangling symbolic link g_file_test() will return TRUE for G_FILE_TEST_IS_SYMLINK
and FALSE for all other flags.
You should never use g_file_test() to test whether it is safe to perform an operation, because there is
always the possibility of the condition changing before you actually perform the operation. For example,
you might think you could use G_FILE_TEST_IS_SYMLINK to know whether it is safe to write to a file
without being tricked into writing into a different location. It doesn’t work!
/* DON’T DO THIS */
if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK))
{
fd = g_open (filename, O_WRONLY);
/* write to fd */
}
g_mkstemp ()
Opens a temporary file. See the mkstemp() documentation on most UNIX-like systems.
The parameter is a string that should follow the rules for mkstemp() templates, i.e. contain the string
"XXXXXX". g_mkstemp() is slightly more flexible than mkstemp() in that the sequence does not have to
occur at the very end of the template. The X string will be modified to form the name of a file that didn’t
exist. The string should be in the GLib file name encoding. Most importantly, on Windows it should be
in UTF-8.
335
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
Returns : A file handle (as from open()) to the file opened for reading and writing. The file is opened
in binary mode on platforms where there is a difference. The file handle should be closed with
close(). In case of errors, -1 is returned.
g_file_open_tmp ()
Opens a file for writing in the preferred directory for temporary files (as returned by g_get_tmp_dir()).
tmpl should be a string in the GLib file name encoding containing a sequence of six ’X’ characters,
as the parameter to g_mkstemp(). However, unlike these functions, the template should only be a base-
name, no directory components are allowed. If template is NULL, a default template is used.
Note that in contrast to g_mkstemp() (and mkstemp()) tmpl is not modified, and might thus be a
read-only literal string.
The actual name used is returned in name_used if non-NULL. This string should be freed with
g_free() when not needed any longer. The returned name is in the GLib file name encoding.
tmpl : Template for file name, as in g_mkstemp(), basename only, or NULL, to a default template
Returns : A file handle (as from open()) to the file opened for reading and writing. The file is opened
in binary mode on platforms where there is a difference. The file handle should be closed with
close(). In case of errors, -1 is returned and error will be set.
g_file_read_link ()
Reads the contents of the symbolic link filename like the POSIX readlink() function. The returned
string is in the encoding used for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
Returns : A newly-allocated string with the contents of the symbolic link, or NULL if an error occurred.
Since 2.4
g_mkdir_with_parents ()
Create a directory if it doesn’t already exist. Create intermediate parent directories as needed, too.
Returns : 0 if the directory already exists, or was successfully created. Returns -1 if an error occurred,
with errno set.
Since 2.8
336
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
GDir
g_dir_open ()
Opens a directory for reading. The names of the files in the directory can then be retrieved using
g_dir_read_name().
path : the path to the directory you are interested in. On Unix in the on-disk encoding. On Windows in
UTF-8
error : return location for a GError, or NULL. If non-NULL, an error will be set if and only if g_dir_open()
fails.
Returns : a newly allocated GDir on success, NULL on failure. If non-NULL, you must free the result
with g_dir_close() when you are finished with it.
g_dir_read_name ()
Retrieves the name of the next entry in the directory. The ’.’ and ’..’ entries are omitted. On Windows,
the returned name is in UTF-8. On Unix, it is in the on-disk encoding.
Returns : The entry’s name or NULL if there are no more entries. The return value is owned by GLib
and must not be modified or freed.
g_dir_rewind ()
Resets the given directory. The next call to g_dir_read_name() will return the first entry again.
g_dir_close ()
GMappedFile
The GMappedFile represents a file mapping created with g_mapped_file_new(). It has only private
members and should not be accessed directly.
337
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
g_mapped_file_new ()
Maps a file into memory. On UNIX, this is using the mmap() function.
If writable is TRUE, the mapped buffer may be modified, otherwise it is an error to modify the
mapped buffer. Modifications to the buffer are not visible to other processes mapping the same file, and
are not written back to the file.
Note that modifications of the underlying file might affect the contents of the GMappedFile. There-
fore, mapping should only be used if the file will not be modified, or if all modifications of the file are
done atomically (e.g. using g_file_set_contents()).
filename : The path of the file to load, in the GLib filename encoding
Returns : a newly allocated GMappedFile which must be freed with g_mapped_file_free(), or NULL if
the mapping failed.
Since 2.8
g_mapped_file_free ()
file : a GMappedFile
Since 2.8
g_mapped_file_get_length ()
file : a GMappedFile
Since 2.8
g_mapped_file_get_contents ()
file : a GMappedFile
Since 2.8
338
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
g_open ()
A wrapper for the POSIX open() function. The open() function is used to convert a pathname into a
file descriptor.
On POSIX systems file descriptors are implemented by the operating system. On Windows, it’s the
C library that implements open() and file descriptors. The actual Win32 API for opening files is quite
different, see MSDN documentation for CreateFile(). The Win32 API uses file handles, which are more
randomish integers, not small integers like file descriptors.
Because file descriptors are specific to the C library on Windows, the file descriptor returned by
this function makes sense only to functions in the same C library. Thus if the GLib-using code uses a
different C library than GLib does, the file descriptor returned by this function cannot be passed to C
library functions like write() or read().
See your C library manual for more details about open().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
flags : as in open()
mode : as in open()
Returns : a new file descriptor, or -1 if an error occurred. The return value can be used exactly like the
return value from open().
Since 2.6
g_rename ()
A wrapper for the POSIX rename() function. The rename() function renames a file, moving it between
directories if required.
See your C library manual for more details about how rename() works on your system. It is not
possible in general on Windows to rename a file that is open to some process.
oldfilename : a pathname in the GLib file name encoding (UTF-8 on Windows)
g_mkdir ()
A wrapper for the POSIX mkdir() function. The mkdir() function attempts to create a directory with
the given name and permissions. The mode argument is ignored on Windows.
See your C library manual for more details about mkdir().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
339
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
g_stat ()
A wrapper for the POSIX stat() function. The stat() function returns information about a file. On
Windows the stat() function in the C library checks only the FAT-style READONLY attribute and does
not look at the ACL at all. Thus on Windows the protection bits in the st_mode field are a fabrication of
little use.
See your C library manual for more details about stat().
buf : a pointer to a stat struct, which will be filled with the file information
Since 2.6
g_lstat ()
A wrapper for the POSIX lstat() function. The lstat() function is like stat() except that in the case of
symbolic links, it returns information about the symbolic link itself and not the file that it refers to. If the
system does not support symbolic links g_lstat() is identical to g_stat().
See your C library manual for more details about lstat().
buf : a pointer to a stat struct, which will be filled with the file information
Since 2.6
g_unlink ()
A wrapper for the POSIX unlink() function. The unlink() function deletes a name from the filesystem.
If this was the last link to the file and no processes have it opened, the diskspace occupied by the file is
freed.
See your C library manual for more details about unlink(). Note that on Windows, it is in general
not possible to delete files that are open to some process, or mapped into memory.
Since 2.6
g_remove ()
A wrapper for the POSIX remove() function. The remove() function deletes a name from the filesys-
tem.
See your C library manual for more details about how remove() works on your system. On Unix,
remove() removes also directories, as it calls unlink() for files and rmdir() for directories. On Windows,
although remove() in the C library only works for files, this function tries first remove() and then if that
340
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
fails rmdir(), and thus works for both files and directories. Note however, that on Windows, it is in
general not possible to remove a file that is open to some process, or mapped into memory.
If this function fails on Windows you can’t infer too much from the errno value. rmdir() is tried
regardless of what caused remove() to fail. Any errno value set by remove() will be overwritten by that
set by rmdir().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
g_rmdir ()
A wrapper for the POSIX rmdir() function. The rmdir() function deletes a directory from the filesys-
tem.
See your C library manual for more details about how rmdir() works on your system.
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
g_fopen ()
A wrapper for the stdio fopen() function. The fopen() function opens a file and associates a new
stream with it.
Because file descriptors are specific to the C library on Windows, and a file descriptor is partof the
FILE struct, the FILE pointer returned by this function makes sense only to functions in the same C
library. Thus if the GLib-using code uses a different C library than GLib does, the FILE pointer returned
by this function cannot be passed to C library functions like fprintf() or fread().
See your C library manual for more details about fopen().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
mode : a string describing the mode in which the file should be opened
Returns : A FILE pointer if the file was successfully opened, or NULL if an error occurred
Since 2.6
g_freopen ()
A wrapper for the POSIX freopen() function. The freopen() function opens a file and associates it
with an existing stream.
See your C library manual for more details about freopen().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
mode : a string describing the mode in which the file should be opened
Returns : A FILE pointer if the file was successfully opened, or NULL if an error occurred.
Since 2.6
341
CHAPTER 4. GLIB UTILITIES 4.15. FILE UTILITIES
g_chmod ()
A wrapper for the POSIX chmod() function. The chmod() function is used to set the permissions of
a file system object.
On Windows the file protection mechanism is not at all POSIX-like, and the underlying chmod()
function in the C library just sets or clears the FAT-style READONLY attribute. It does not touch any
ACL. Software that needs to manage file permissions on Windows exactly should use the Win32 API.
See your C library manual for more details about chmod().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
mode : as in chmod()
g_access ()
A wrapper for the POSIX access() function. This function is used to test a pathname for one or several
of read, write or execute permissions, or just existence.
On Windows, the file protection mechanism is not at all POSIX-like, and the underlying function in
the C library only checks the FAT-style READONLY attribute, and does not look at the ACL of a file
at all. This function is this in practise almost useless on Windows. Software that needs to handle file
permissions on Windows more exactly should use the Win32 API.
See your C library manual for more details about access().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
mode : as in access()
Returns : zero if the pathname refers to an existing file system object that has all the tested permissions,
or -1 otherwise or on error.
Since 2.8
g_creat ()
A wrapper for the POSIX creat() function. The creat() function is used to convert a pathname into a
file descriptor, creating a file if necessary.
On POSIX systems file descriptors are implemented by the operating system. On Windows, it’s
the C library that implements creat() and file descriptors. The actual Windows API for opening files is
different, see MSDN documentation for CreateFile(). The Win32 API uses file handles, which are more
randomish integers, not small integers like file descriptors.
Because file descriptors are specific to the C library on Windows, the file descriptor returned by
this function makes sense only to functions in the same C library. Thus if the GLib-using code uses a
different C library than GLib does, the file descriptor returned by this function cannot be passed to C
library functions like write() or read().
See your C library manual for more details about creat().
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
mode : as in creat()
Returns : a new file descriptor, or -1 if an error occurred. The return value can be used exactly like the
return value from creat().
Since 2.8
342
CHAPTER 4. GLIB UTILITIES 4.16. URI FUNCTIONS
g_chdir ()
A wrapper for the POSIX chdir() function. The function changes the current directory of the process
to path.
See your C library manual for more details about chdir().
path : a pathname in the GLib file name encoding (UTF-8 on Windows)
g_utime ()
A wrapper for the POSIX utime() function. The utime() function sets the access and modification
timestamps of a file.
See your C library manual for more details about how utime() works on your system.
filename : a pathname in the GLib file name encoding (UTF-8 on Windows)
Synopsis
#include <glib.h>
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT
#define G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO
#define G_URI_RESERVED_CHARS_GENERIC_DELIMITERS
#define G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS
char * g_uri_parse_scheme (const char *uri);
char * g_uri_escape_string (const char *unescaped,
const char *reserved_chars_al
gboolean allow_utf8);
char * g_uri_unescape_string (const char *escaped_string,
const char *illegal_character
char * g_uri_unescape_segment (const char *escaped_string,
const char *escaped_string_en
const char *illegal_character
Description
Functions for manipulating Universal Resource Identifiers (URIs) as defined by RFC 3986. It is highly
recommended that you have read and understand RFC 3986 for understanding this API.
343
CHAPTER 4. GLIB UTILITIES 4.16. URI FUNCTIONS
Details
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH ←-
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/"
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT ←-
G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":@"
G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO
#define G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO ←-
G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":"
G_URI_RESERVED_CHARS_GENERIC_DELIMITERS
G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS
g_uri_parse_scheme ()
Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
Returns : The "Scheme" component of the URI, or NULL on error. The returned string should be freed
when no longer needed.
Since 2.16
g_uri_escape_string ()
344
CHAPTER 4. GLIB UTILITIES 4.16. URI FUNCTIONS
Returns : an escaped version of unescaped . The returned string should be freed when no longer needed.
Since 2.16
g_uri_unescape_string ()
Returns : an unescaped version of escaped_string . The returned string should be freed when no
longer needed.
Since 2.16
g_uri_unescape_segment ()
escaped_string : a string.
escaped_string_end : a string.
Returns : an unescaped version of escaped_string or NULL on error. The returned string should be
freed when no longer needed.
Since 2.16
345
CHAPTER 4. GLIB UTILITIES 4.17. SHELL-RELATED UTILITIES
Synopsis
#include <glib.h>
enum GShellError;
#define G_SHELL_ERROR
gboolean g_shell_parse_argv (const gchar *command_line,
gint *argcp,
gchar ***argvp,
GError **error);
gchar* g_shell_quote (const gchar *unquoted_string);
gchar* g_shell_unquote (const gchar *quoted_string,
GError **error);
Description
Details
enum GShellError
typedef enum
{
/* mismatched or otherwise mangled quoting */
G_SHELL_ERROR_BAD_QUOTING,
/* string to be parsed was empty */
G_SHELL_ERROR_EMPTY_STRING,
G_SHELL_ERROR_FAILED
} GShellError;
G_SHELL_ERROR
Error domain for shell functions. Errors in this domain will be from the GShellError enumeration.
See GError for information on error domains.
g_shell_parse_argv ()
346
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
Parses a command line into an argument vector, in much the same way the shell would, but with-
out many of the expansions the shell would perform (variable expansion, globs, operators, filename
expansion, etc. are not supported). The results are defined to be the same as those you would get from
a UNIX98 /bin/sh, as long as the input contains none of the unsupported shell expansions. If the in-
put does contain such expansions, they are passed through literally. Possible errors are those from the
G_SHELL_ERROR domain. Free the returned vector with g_strfreev().
command_line : command line to parse
g_shell_quote ()
Quotes a string so that the shell (/bin/sh) will interpret the quoted string to mean unquoted_str-
ing . If you pass a filename to the shell, for example, you should first quote it with this function. The
return value must be freed with g_free(). The quoting style used is undefined (single or double quotes
may be used).
unquoted_string : a literal string
g_shell_unquote ()
Unquotes a string as the shell (/bin/sh) would. Only handles quotes; if a string contains file globs,
arithmetic operators, variables, backticks, redirections, or other special-to-the-shell features, the result
will be different from the result a real shell would produce (the variables, backticks, etc. will be passed
through literally instead of being expanded). This function is guaranteed to succeed if applied to the re-
sult of g_shell_quote(). If it fails, it returns NULL and sets the error. The quoted_string need not
actually contain quoted or escaped text; g_shell_unquote() simply goes through the string and un-
quotes/unescapes anything that the shell would. Both single and double quotes are handled, as are
escapes including escaped newlines. The return value must be freed with g_free(). Possible errors are in
the G_SHELL_ERROR domain.
Shell quoting rules are a bit strange. Single quotes preserve the literal string exactly. escape se-
quences are not allowed; not even \’ - if you want a ’ in the quoted text, you have to do something like
’foo’\”bar’. Double quotes allow $, ‘, ", \, and newline to be escaped with backslash. Otherwise double
quotes preserve things literally.
quoted_string : shell-quoted string
347
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
Synopsis
#include <glib.h>
enum GOptionError;
#define G_OPTION_ERROR
gboolean (*GOptionArgFunc) (const gchar *option_name,
const gchar *value,
gpointer data,
GError **error);
GOptionContext;
GOptionContext * g_option_context_new (const gchar *parameter_string);
void g_option_context_set_summary (GOptionContext *context,
const gchar *summary);
const gchar * g_option_context_get_summary (GOptionContext *context);
void g_option_context_set_description (GOptionContext *context,
const gchar *description);
const gchar * g_option_context_get_description (GOptionContext *context);
const gchar * (*GTranslateFunc) (const gchar *str,
gpointer data);
void g_option_context_set_translate_func (GOptionContext *context,
GTranslateFunc func,
gpointer data,
GDestroyNotify destroy_notify);
void g_option_context_set_translation_domain
(GOptionContext *context,
const gchar *domain);
void g_option_context_free (GOptionContext *context);
gboolean g_option_context_parse (GOptionContext *context,
gint *argc,
gchar ***argv,
GError **error);
void g_option_context_set_help_enabled (GOptionContext *context,
gboolean help_enabled);
gboolean g_option_context_get_help_enabled (GOptionContext *context);
void g_option_context_set_ignore_unknown_options
(GOptionContext *context,
gboolean ignore_unknown);
gboolean g_option_context_get_ignore_unknown_options
(GOptionContext *context);
gchar * g_option_context_get_help (GOptionContext *context,
gboolean main_help,
GOptionGroup *group);
enum GOptionArg;
enum GOptionFlags;
#define G_OPTION_REMAINING
GOptionEntry;
void g_option_context_add_main_entries (GOptionContext *context,
const GOptionEntry *entries,
const gchar *translation_domain);
GOptionGroup;
void g_option_context_add_group (GOptionContext *context,
GOptionGroup *group);
void g_option_context_set_main_group (GOptionContext *context,
GOptionGroup *group);
GOptionGroup * g_option_context_get_main_group (GOptionContext *context);
GOptionGroup * g_option_group_new (const gchar *name,
const gchar *description,
348
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
Description
The GOption commandline parser is intended to be a simpler replacement for the popt library. It sup-
ports short and long commandline options, as shown in the following example:
testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2
The example demonstrates a number of features of the GOption commandline parser
• Options can be single letters, prefixed by a single dash. Multiple short options can be grouped
behind a single dash.
• Options can have an extra argument, which can be a number, a string or a filename. For long
options, the extra argument can be appended with an equals sign after the option name.
• An argument consisting solely of two dashes turns off further parsing, any remaining arguments
(even those starting with a dash) are returned to the application as rest arguments.
Another important feature of GOption is that it can automatically generate nicely formatted help
output. Unless it is explicitly turned off with g_option_context_set_help_enabled(), GOption will recog-
nize the --help, -?, --help-all and --help-groupname options (where groupname is the name of
a GOptionGroup) and write a text similar to the one shown in the following example to stdout.
Usage:
testtreemodel [OPTION...] - test tree model performance
Help Options:
-?, --help Show help options
--help-all Show all help options
--help-gtk Show GTK+ Options
Application Options:
-r, --repeats=N Average over N repetitions
349
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
GOption groups options in GOptionGroups, which makes it easy to incorporate options from multi-
ple sources. The intended use for this is to let applications collect option groups from the libraries it uses,
add them to their GOptionContext, and parse all options by a single call to g_option_context_parse().
See gtk_get_option_group() for an example.
If an option is declared to be of type string or filename, GOption takes care of converting it to the
right encoding; strings are returned in UTF-8, filenames are returned in the GLib filename encoding.
Note that this only works if setlocale() has been called before g_option_context_parse().
Here is a complete example of setting up GOption to parse the example commandline above and
produce the example help output.
static gint repeats = 2;
static gint max_size = 8;
static gboolean verbose = FALSE;
static gboolean beep = FALSE;
static gboolean rand = FALSE;
int
main (int argc, char *argv[])
{
GError *error = NULL;
GOptionContext *context;
// ...
Details
enum GOptionError
typedef enum
{
G_OPTION_ERROR_UNKNOWN_OPTION,
G_OPTION_ERROR_BAD_VALUE,
G_OPTION_ERROR_FAILED
350
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
} GOptionError;
G_OPTION_ERROR
Error domain for option parsing. Errors in this domain will be from the GOptionError enumeration.
See GError for information on error domains.
GOptionArgFunc ()
data : User data added to the GOptionGroup containing the option when it was created with g_option_group_new()
error : A return location for errors. The error code G_OPTION_ERROR_FAILED is intended to be used
for errors in GOptionArgFunc callbacks.
Returns : TRUE if the option was successfully parsed, FALSE if an error occurred, in which case error
should be set with g_set_error()
GOptionContext
A GOptionContext struct defines which options are accepted by the commandline option parser. The
struct has only private fields and should not be directly accessed.
g_option_context_new ()
351
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
parameter_string : a string which is displayed in the first line of --help output, after the usage
summary programname [OPTION...]
Returns : a newly created GOptionContext, which must be freed with g_option_context_free() after use.
Since 2.6
g_option_context_set_summary ()
Adds a string to be displayed in --help output before the list of options. This is typically a summary
of the program functionality.
Note that the summary is translated (see g_option_context_set_translate_func() and g_option_context_set_translation_do
context : a GOptionContext
summary : a string to be shown in --help output before the list of options, or NULL
Since 2.12
g_option_context_get_summary ()
context : a GOptionContext
Since 2.12
g_option_context_set_description ()
Adds a string to be displayed in --help output after the list of options. This text often includes a
bug reporting address.
Note that the summary is translated (see g_option_context_set_translate_func()).
context : a GOptionContext
description : a string to be shown in --help output after the list of options, or NULL
Since 2.12
g_option_context_get_description ()
context : a GOptionContext
Since 2.12
352
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
GTranslateFunc ()
The type of functions which are used to translate user-visible strings, for --help output.
str : the untranslated string
data : user data specified when installing the function, e.g. in g_option_group_set_translate_func()
Returns : a translation of the string for the current locale. The returned string is owned by GLib and
must not be freed.
g_option_context_set_translate_func ()
Sets the function which is used to translate the contexts user-visible strings, for --help output. If
func is NULL, strings are not translated.
Note that option groups have their own translation functions, this function only affects the para-
meter_string (see g_option_context_new()), the summary (see g_option_context_set_summary()) and
the description (see g_option_context_set_description()).
If you are using gettext(), you only need to set the translation domain, see g_option_context_set_translation_domai
context : a GOptionContext
Since 2.12
g_option_context_set_translation_domain ()
void g_option_context_set_translation_domain
(GOptionContext *context,
const gchar *domain);
Since 2.12
g_option_context_free ()
Frees context and all the groups which have been added to it.
Please note that parsed arguments need to be freed separately (see GOptionEntry).
context : a GOptionContext
Since 2.6
353
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
g_option_context_parse ()
Parses the command line arguments, recognizing options which have been added to context. A
side-effect of calling this function is that g_set_prgname() will be called.
If the parsing is successful, any parsed arguments are removed from the array and argc and argv
are updated accordingly. A ’--’ option is stripped from argv unless there are unparsed options before
and after it, or some of the options after it start with ’-’. In case of an error, argc and argv are left
unmodified.
If automatic --help support is enabled (see g_option_context_set_help_enabled()), and the argv
array contains one of the recognized help options, this function will produce help output to stdout and
call exit (0).
Note that function depends on the current locale for automatic character set conversion of string and
filename arguments.
context : a GOptionContext
Since 2.6
g_option_context_set_help_enabled ()
context : a GOptionContext
Since 2.6
g_option_context_get_help_enabled ()
Returns whether automatic --help generation is turned on for context. See g_option_context_set_help_enabled().
context : a GOptionContext
Since 2.6
354
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
g_option_context_set_ignore_unknown_options ()
void g_option_context_set_ignore_unknown_options
(GOptionContext *context,
gboolean ignore_unknown) ←-
;
Sets whether to ignore unknown options or not. If an argument is ignored, it is left in the argv array
after parsing. By default, g_option_context_parse() treats unknown options as error.
This setting does not affect non-option arguments (i.e. arguments which don’t start with a dash). But
note that GOption cannot reliably determine whether a non-option belongs to a preceding unknown
option.
context : a GOptionContext
ignore_unknown : TRUE to ignore unknown options, FALSE to produce an error when unknown op-
tions are met
Since 2.6
g_option_context_get_ignore_unknown_options ()
gboolean g_option_context_get_ignore_unknown_options
(GOptionContext *context) ←-
;
g_option_context_get_help ()
Returns a formatted, translated help text for the given context. To obtain the text produced by --he-
lp, call g_option_context_get_help (context, TRUE, NULL). To obtain the text produced by
--help-all, call g_option_context_get_help (context, FALSE, NULL). To obtain the help
text for an option group, call g_option_context_get_help (context, FALSE, group).
context : a GOptionContext
enum GOptionArg
typedef enum
{
G_OPTION_ARG_NONE,
G_OPTION_ARG_STRING,
G_OPTION_ARG_INT,
G_OPTION_ARG_CALLBACK,
G_OPTION_ARG_FILENAME,
355
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
G_OPTION_ARG_STRING_ARRAY,
G_OPTION_ARG_FILENAME_ARRAY,
G_OPTION_ARG_DOUBLE,
G_OPTION_ARG_INT64
} GOptionArg;
The GOptionArg enum values determine which type of extra argument the options expect to find. If
an option expects an extra argument, it can be specified in several ways; with a short option: -x arg,
with a long option: --name arg or combined in a single argument: --name=arg.
G_OPTION_ARG_NONE No extra argument. This is useful for simple flags.
G_OPTION_ARG_STRING The option takes a string argument.
G_OPTION_ARG_INT The option takes an integer argument.
G_OPTION_ARG_CALLBACK The option provides a callback to parse the extra argument.
G_OPTION_ARG_FILENAME The option takes a filename as argument.
G_OPTION_ARG_STRING_ARRAY The option takes a string argument, multiple uses of the option are
collected into an array of strings.
G_OPTION_ARG_FILENAME_ARRAY The option takes a filename as argument, multiple uses of the op-
tion are collected into an array of strings.
G_OPTION_ARG_DOUBLE The option takes a double argument. The argument can be formatted either
for the user’s locale or for the "C" locale. Since 2.12
G_OPTION_ARG_INT64 The option takes a 64-bit integer. Like G_OPTION_ARG_INT but for larger
numbers. The number can be in decimal base, or in hexadecimal (when prefixed with 0x, for
example, 0xffffffff). Since 2.12
enum GOptionFlags
typedef enum
{
G_OPTION_FLAG_HIDDEN = 1 << 0,
G_OPTION_FLAG_IN_MAIN = 1 << 1,
G_OPTION_FLAG_REVERSE = 1 << 2,
G_OPTION_FLAG_NO_ARG = 1 << 3,
G_OPTION_FLAG_FILENAME = 1 << 4,
G_OPTION_FLAG_OPTIONAL_ARG = 1 << 5,
G_OPTION_FLAG_NOALIAS = 1 << 6
} GOptionFlags;
356
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
G_OPTION_FLAG_NOALIAS This flag turns off the automatic conflict resolution which prefixes long
option names with groupname- if there is a conflict. This option should only be used in situations
where aliasing is necessary to model some legacy commandline interface. It is not safe to use this
option, unless all option groups are under your direct control. Since 2.8.
G_OPTION_REMAINING
If a long option in the main group has this name, it is not treated as a regular option. Instead it
collects all non-option arguments which would otherwise be left in argv. The option must be of type
G_OPTION_ARG_CALLBACK, G_OPTION_ARG_STRING_ARRAY or G_OPTION_ARG_FILENAME_ARRAY.
Using G_OPTION_REMAINING instead of simply scanning argv for leftover arguments has the
advantage that GOption takes care of necessary encoding conversions for strings or filenames.
Since 2.6
GOptionEntry
typedef struct {
const gchar *long_name;
gchar short_name;
gint flags;
GOptionArg arg;
gpointer arg_data;
A GOptionEntry defines a single option. To have an effect, they must be added to a GOptionGroup
with g_option_context_add_main_entries() or g_option_group_add_entries().
const gchar *long_name; The long name of an option can be used to specify it in a commandline as --
long_name. Every option must have a long name. To resolve conflicts if multiple option groups
contain the same long name, it is also possible to specify the option as --groupname-long_name.
gchar short_name; If an option has a short name, it can be specified -short_name in a commandline.
short_name must be a printable ASCII character different from ’-’, or zero if the option has no
short name.
gpointer arg_data; If the arg type is G_OPTION_ARG_CALLBACK, then arg_data must point to a
GOptionArgFunc callback function, which will be called to handle the extra argument. Otherwise,
arg_data is a pointer to a location to store the value, the required type of the location depends on
the arg type:
G_OPTION_ARG_NONE gboolean
G_OPTION_ARG_STRING gchar*
G_OPTION_ARG_INT gint
G_OPTION_ARG_FILENAME gchar*
G_OPTION_ARG_STRING_ARRAY gchar**
G_OPTION_ARG_FILENAME_ARRAY gchar**
G_OPTION_ARG_DOUBLE gdouble
357
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
const gchar *description; the description for the option in --help output. The description is trans-
lated using the translate_func of the group, see g_option_group_set_translation_domain().
const gchar *arg_description; The placeholder to use for the extra argument parsed by the option in
--help output. The arg_description is translated using the translate_func of the group, see
g_option_group_set_translation_domain().
g_option_context_add_main_entries ()
A convenience function which creates a main group if it doesn’t exist, adds the entries to it and sets
the translation domain.
context : a GOptionContext
translation_domain : a translation domain to use for translating the --help output for the options
in entries with gettext(), or NULL
Since 2.6
GOptionGroup
A GOptionGroup struct defines the options in a single group. The struct has only private fields and
should not be directly accessed.
All options in a group share the same translation function. Libaries which need to parse command-
line options are expected to provide a function for getting a GOptionGroup holding their options, which
the application can then add to its GOptionContext.
g_option_context_add_group ()
Adds a GOptionGroup to the context, so that parsing with context will recognize the options in
the group. Note that the group will be freed together with the context when g_option_context_free() is
called, so you must not free the group yourself after adding it to a context.
context : a GOptionContext
Since 2.6
358
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
g_option_context_set_main_group ()
Sets a GOptionGroup as main group of the context. This has the same effect as calling g_option_context_add_grou
the only difference is that the options in the main group are treated differently when generating --help
output.
context : a GOptionContext
g_option_context_get_main_group ()
Returns : the main group of context, or NULL if context doesn’t have a main group. Note that group
belongs to context and should not be modified or freed.
Since 2.6
g_option_group_new ()
Returns : a newly created option group. It should be added to a GOptionContext or freed with g_option_group_free()
Since 2.6
g_option_group_free ()
Frees a GOptionGroup. Note that you must not free groups which have been added to a GOption-
Context.
group : a GOptionGroup
Since 2.6
359
CHAPTER 4. GLIB UTILITIES 4.18. COMMANDLINE OPTION PARSER
g_option_group_add_entries ()
Since 2.6
GOptionParseFunc ()
The type of function that can be called before and after parsing.
context : The active GOptionContext
data : User data added to the GOptionGroup containing the option when it was created with g_option_group_new()
Returns : TRUE if the function completed successfully, FALSE if an error occurred, in which case error
should be set with g_set_error()
g_option_group_set_parse_hooks ()
Associates two functions with group which will be called from g_option_context_parse() before the
first option is parsed and after the last option has been parsed, respectively.
Note that the user data to be passed to pre_parse_func and post_parse_func can be specified
when constructing the group with g_option_group_new().
group : a GOptionGroup
Since 2.6
GOptionErrorFunc ()
360
CHAPTER 4. GLIB UTILITIES 4.19. GLOB-STYLE PATTERN MATCHING
data : User data added to the GOptionGroup containing the option when it was created with g_option_group_new()
g_option_group_set_error_hook ()
Associates a function with group which will be called from g_option_context_parse() when an error
occurs.
Note that the user data to be passed to error_func can be specified when constructing the group
with g_option_group_new().
group : a GOptionGroup
Since 2.6
g_option_group_set_translate_func ()
Sets the function which is used to translate user-visible strings, for --help output. Different groups
can use different GTranslateFuncs. If func is NULL, strings are not translated.
If you are using gettext(), you only need to set the translation domain, see g_option_group_set_translation_domain
group : a GOptionGroup
Since 2.6
g_option_group_set_translation_domain ()
void g_option_group_set_translation_domain
(GOptionGroup *group,
const gchar *domain);
Since 2.6
361
CHAPTER 4. GLIB UTILITIES 4.19. GLOB-STYLE PATTERN MATCHING
Synopsis
#include <glib.h>
GPatternSpec;
GPatternSpec* g_pattern_spec_new (const gchar *pattern);
void g_pattern_spec_free (GPatternSpec *pspec);
gboolean g_pattern_spec_equal (GPatternSpec *pspec1,
GPatternSpec *pspec2);
gboolean g_pattern_match (GPatternSpec *pspec,
guint string_length,
const gchar *string,
const gchar *string_reversed);
gboolean g_pattern_match_string (GPatternSpec *pspec,
const gchar *string);
gboolean g_pattern_match_simple (const gchar *pattern,
const gchar *string);
Description
The g_pattern_match* functions match a string against a pattern containing ’*’ and ’?’ wildcards
with similar semantics as the standard glob() function: ’*’ matches an arbitrary, possibly empty, string,
’?’ matches an arbitrary character.
Note that in contrast to glob(), the ’/’ character can be matched by the wildcards, there are no ’[...]’
character ranges and ’*’ and ’?’ can not be escaped to include them literally in a pattern.
When multiple strings must be matched against the same pattern, it is better to compile the pattern to
a GPatternSpec using g_pattern_spec_new() and use g_pattern_match_string() instead of g_pattern_match_simple().
This avoids the overhead of repeated pattern compilation.
Details
GPatternSpec
A GPatternSpec is the ’compiled’ form of a pattern. This structure is opaque and its fields cannot be
accessed directly.
g_pattern_spec_new ()
g_pattern_spec_free ()
pspec : a GPatternSpec
362
CHAPTER 4. GLIB UTILITIES 4.19. GLOB-STYLE PATTERN MATCHING
g_pattern_spec_equal ()
Compares two compiled pattern specs and returns whether they will match the same set of strings.
pspec1 : a GPatternSpec
g_pattern_match ()
Matches a string against a compiled pattern. Passing the correct length of the string given is manda-
tory. The reversed string can be omitted by passing NULL, this is more efficient if the reversed version
of the string to be matched is not at hand, as g_pattern_match() will only construct it if the compiled
pattern requires reverse matches.
Note that, if the user code will (possibly) match a string against a multitude of patterns containing
wildcards, chances are high that some patterns will require a reversed string. In this case, it’s more
efficient to provide the reversed string to avoid multiple constructions thereof in the various calls to
g_pattern_match().
Note also that the reverse of a UTF-8 encoded string can in general not be obtained by g_strreverse().
This works only if the string doesn’t contain any multibyte characters. GLib offers the g_utf8_strreverse()
function to reverse UTF-8 encoded strings.
pspec : a GPatternSpec
string_length : the length of string (in bytes, i.e. strlen(), not g_utf8_strlen())
g_pattern_match_string ()
Matches a string against a compiled pattern. If the string is to be matched against more than one
pattern, consider using g_pattern_match() instead while supplying the reversed string.
pspec : a GPatternSpec
363
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
g_pattern_match_simple ()
Matches a string against a pattern given as a string. If this function is to be called in a loop, it’s
more efficient to compile the pattern once with g_pattern_spec_new() and call g_pattern_match_string()
repeatedly.
pattern : the UTF-8 encoded pattern
Synopsis
#include <glib.h>
enum GRegexError;
#define G_REGEX_ERROR
enum GRegexCompileFlags;
enum GRegexMatchFlags;
GRegex;
gboolean (*GRegexEvalCallback) (const GMatchInfo *match_info,
GString *result,
gpointer user_data);
GRegex * g_regex_new (const gchar *pattern,
GRegexCompileFlags compile_options
GRegexMatchFlags match_options,
GError **error);
GRegex * g_regex_ref (GRegex *regex);
void g_regex_unref (GRegex *regex);
const gchar * g_regex_get_pattern (const GRegex *regex);
gint g_regex_get_max_backref (const GRegex *regex);
gint g_regex_get_capture_count (const GRegex *regex);
gint g_regex_get_string_number (const GRegex *regex,
const gchar *name);
gchar * g_regex_escape_string (const gchar *string,
gint length);
gboolean g_regex_match_simple (const gchar *pattern,
const gchar *string,
GRegexCompileFlags compile_options
GRegexMatchFlags match_options);
gboolean g_regex_match (const GRegex *regex,
const gchar *string,
GRegexMatchFlags match_options,
GMatchInfo **match_info);
gboolean g_regex_match_full (const GRegex *regex,
const gchar *string,
gssize string_len,
gint start_position,
GRegexMatchFlags match_options,
364
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
GMatchInfo **match_info,
GError **error);
gboolean g_regex_match_all (const GRegex *regex,
const gchar *string,
GRegexMatchFlags match_option
GMatchInfo **match_info);
gboolean g_regex_match_all_full (const GRegex *regex,
const gchar *string,
gssize string_len,
gint start_position,
GRegexMatchFlags match_option
GMatchInfo **match_info,
GError **error);
gchar ** g_regex_split_simple (const gchar *pattern,
const gchar *string,
GRegexCompileFlags compile_op
GRegexMatchFlags match_option
gchar ** g_regex_split (const GRegex *regex,
const gchar *string,
GRegexMatchFlags match_option
gchar ** g_regex_split_full (const GRegex *regex,
const gchar *string,
gssize string_len,
gint start_position,
GRegexMatchFlags match_option
gint max_tokens,
GError **error);
gchar * g_regex_replace (const GRegex *regex,
const gchar *string,
gssize string_len,
gint start_position,
const gchar *replacement,
GRegexMatchFlags match_option
GError **error);
gchar * g_regex_replace_literal (const GRegex *regex,
const gchar *string,
gssize string_len,
gint start_position,
const gchar *replacement,
GRegexMatchFlags match_option
GError **error);
gchar * g_regex_replace_eval (const GRegex *regex,
const gchar *string,
gssize string_len,
gint start_position,
GRegexMatchFlags match_option
GRegexEvalCallback eval,
gpointer user_data,
GError **error);
gboolean g_regex_check_replacement (const gchar *replacement,
gboolean *has_references,
GError **error);
GMatchInfo;
GRegex * g_match_info_get_regex (const GMatchInfo *match_info)
const gchar * g_match_info_get_string (const GMatchInfo *match_info)
void g_match_info_free (GMatchInfo *match_info);
gboolean g_match_info_matches (const GMatchInfo *match_info)
gboolean g_match_info_next (GMatchInfo *match_info,
GError **error);
365
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
Description
The g_regex_*() functions implement regular expression pattern matching using syntax and seman-
tics similar to Perl regular expression.
Some functions accept a start_position argument, setting it differs from just passing over a short-
ened string and setting G_REGEX_MATCH_NOTBOL in the case of a pattern that begins with any kind
of lookbehind assertion. For example, consider the pattern "\Biss\B" which finds occurrences of "iss" in
the middle of words. ("\B" matches only if the current position in the subject is not a word boundary.)
When applied to the string "Mississipi" from the fourth byte, namely "issipi", it does not match, because
"\B" is always false at the start of the subject, which is deemed to be a word boundary. However, if the
entire string is passed , but with start_position set to 4, it finds the second occurrence of "iss" because
it is able to look behind the starting point to discover that it is preceded by a letter.
Note that, unless you set the G_REGEX_RAW flag, all the strings passed to these functions must be
encoded in UTF-8. The lengths and the positions inside the strings are in bytes and not in characters,
so, for instance, "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a single character. If you
set G_REGEX_RAW the strings can be non-valid UTF-8 strings and a byte is treated as a character, so
"\xc3\xa0" is two bytes and two characters long.
When matching a pattern, "\n" matches only against a "\n" character in the string, and "\r" matches
only a "\r" character. To match any newline sequence use "\R". This particular group matches either the
two-character sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed, U+000A, "\n"),
VT (vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"), CR (carriage return, U+000D, "\r"), NEL
(next line, U+0085), LS (line separator, U+2028), or PS (paragraph separator, U+2029).
The behaviour of the dot, circumflex, and dollar metacharacters are affected by newline characters,
the default is to recognize any newline character (the same characters recognized by "\R"). This can be
changed with G_REGEX_NEWLINE_CR, G_REGEX_NEWLINE_LF and G_REGEX_NEWLINE_CRLF
compile options, and with G_REGEX_MATCH_NEWLINE_ANY, G_REGEX_MATCH_NEWLINE_CR,
G_REGEX_MATCH_NEWLINE_LF and G_REGEX_MATCH_NEWLINE_CRLF match options. These
settings are also relevant when compiling a pattern if G_REGEX_EXTENDED is set, and an unescaped
"#" outside a character class is encountered. This indicates a comment that lasts until after the next
newline.
Creating and manipulating the same GRegex structure from different threads is not a problem as
GRegex does not modify its internal state between creation and destruction, on the other hand GMatch-
Info is not threadsafe.
The regular expressions low level functionalities are obtained through the excellent PCRE library
written by Philip Hazel.
Details
enum GRegexError
366
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
typedef enum
{
G_REGEX_ERROR_COMPILE,
G_REGEX_ERROR_OPTIMIZE,
G_REGEX_ERROR_REPLACE,
G_REGEX_ERROR_MATCH,
G_REGEX_ERROR_INTERNAL,
367
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
368
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
Since 2.14
G_REGEX_ERROR
Error domain for regular expressions. Errors in this domain will be from the GRegexError enumera-
tion. See GError for information on error domains.
Since 2.14
enum GRegexCompileFlags
typedef enum
{
G_REGEX_CASELESS = 1 << 0,
G_REGEX_MULTILINE = 1 << 1,
G_REGEX_DOTALL = 1 << 2,
G_REGEX_EXTENDED = 1 << 3,
G_REGEX_ANCHORED = 1 << 4,
G_REGEX_DOLLAR_ENDONLY = 1 << 5,
G_REGEX_UNGREEDY = 1 << 9,
G_REGEX_RAW = 1 << 11,
G_REGEX_NO_AUTO_CAPTURE = 1 << 12,
G_REGEX_OPTIMIZE = 1 << 13,
G_REGEX_DUPNAMES = 1 << 19,
G_REGEX_NEWLINE_CR = 1 << 20,
G_REGEX_NEWLINE_LF = 1 << 21,
G_REGEX_NEWLINE_CRLF = G_REGEX_NEWLINE_CR | G_REGEX_NEWLINE_LF
} GRegexCompileFlags;
G_REGEX_CASELESS Letters in the pattern match both upper and lower case letters. It be changed
within a pattern by a "(?i)" option setting.
G_REGEX_MULTILINE By default, GRegex treats the strings as consisting of a single line of charac-
ters (even if it actually contains newlines). The "start of line" metacharacter ("ˆ") matches only
at the start of the string, while the "end of line" metacharacter ("$") matches only at the end of
the string, or before a terminating newline (unless G_REGEX_DOLLAR_ENDONLY is set). When
G_REGEX_MULTILINE is set, the "start of line" and "end of line" constructs match immediately
following or immediately before any newline in the string, respectively, as well as at the very start
and end. This can be changed within a pattern by a "(?m)" option setting.
G_REGEX_DOTALL A dot metacharater (".") in the pattern matches all characters, including newlines.
Without it, newlines are excluded. This option can be changed within a pattern by a ("?s") option
setting.
369
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
G_REGEX_EXTENDED Whitespace data characters in the pattern are totally ignored except when es-
caped or inside a character class. Whitespace does not include the VT character (code 11). In addi-
tion, characters between an unescaped "#" outside a character class and the next newline character,
inclusive, are also ignored. This can be changed within a pattern by a "(?x)" option setting.
G_REGEX_ANCHORED The pattern is forced to be "anchored", that is, it is constrained to match only at
the first matching point in the string that is being searched. This effect can also be achieved by
appropriate constructs in the pattern itself such as the "ˆ" metacharater.
G_REGEX_DOLLAR_ENDONLY A dollar metacharacter ("$") in the pattern matches only at the end of the
string. Without this option, a dollar also matches immediately before the final character if it is a
newline (but not before any other newlines). This option is ignored if G_REGEX_MULTILINE is
set.
G_REGEX_UNGREEDY Inverts the "greediness" of the quantifiers so that they are not greedy by default,
but become greedy if followed by "?". It can also be set by a "(?U)" option setting within the pattern.
G_REGEX_RAW Usually strings must be valid UTF-8 strings, using this flag they are considered as a raw
sequence of bytes.
G_REGEX_OPTIMIZE Optimize the regular expression. If the pattern will be used many times, then it
may be worth the effort to optimize it to improve the speed of matches.
G_REGEX_DUPNAMES Names used to identify capturing subpatterns need not be unique. This can be
helpful for certain types of pattern when it is known that only one instance of the named subpat-
tern can ever be matched.
G_REGEX_NEWLINE_CR Usually any newline character is recognized, if this option is set, the only rec-
ognized newline character is ’\r’.
G_REGEX_NEWLINE_LF Usually any newline character is recognized, if this option is set, the only rec-
ognized newline character is ’\n’.
G_REGEX_NEWLINE_CRLF Usually any newline character is recognized, if this option is set, the only
recognized newline character sequence is ’\r\n’.
Since 2.14
enum GRegexMatchFlags
typedef enum
{
G_REGEX_MATCH_ANCHORED = 1 << 4,
G_REGEX_MATCH_NOTBOL = 1 << 7,
G_REGEX_MATCH_NOTEOL = 1 << 8,
G_REGEX_MATCH_NOTEMPTY = 1 << 10,
G_REGEX_MATCH_PARTIAL = 1 << 15,
G_REGEX_MATCH_NEWLINE_CR = 1 << 20,
G_REGEX_MATCH_NEWLINE_LF = 1 << 21,
G_REGEX_MATCH_NEWLINE_CRLF = G_REGEX_MATCH_NEWLINE_CR | ←-
G_REGEX_MATCH_NEWLINE_LF,
G_REGEX_MATCH_NEWLINE_ANY = 1 << 22
} GRegexMatchFlags;
370
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
G_REGEX_MATCH_NOTBOL Specifies that first character of the string is not the beginning of a line, so the
circumflex metacharacter should not match before it. Setting this without G_REGEX_MULTILINE
(at compile time) causes circumflex never to match. This option affects only the behaviour of the
circumflex metacharacter, it does not affect "\A".
G_REGEX_MATCH_NOTEOL Specifies that the end of the subject string is not the end of a line, so the
dollar metacharacter should not match it nor (except in multiline mode) a newline immediately
before it. Setting this without G_REGEX_MULTILINE (at compile time) causes dollar never to
match. This option affects only the behaviour of the dollar metacharacter, it does not affect "\Z"
or "\z".
G_REGEX_MATCH_NOTEMPTY An empty string is not considered to be a valid match if this option is set.
If there are alternatives in the pattern, they are tried. If all the alternatives match the empty string,
the entire match fails. For example, if the pattern "a?b?" is applied to a string not beginning with
"a" or "b", it matches the empty string at the start of the string. With this flag set, this match is not
valid, so GRegex searches further into the string for occurrences of "a" or "b".
G_REGEX_MATCH_PARTIAL Turns on the partial matching feature, for more documentation on partial
matching see g_match_info_is_partial_match().
G_REGEX_MATCH_NEWLINE_CR Overrides the newline definition set when creating a new GRegex,
setting the ’\r’ character as line terminator.
G_REGEX_MATCH_NEWLINE_LF Overrides the newline definition set when creating a new GRegex,
setting the ’\n’ character as line terminator.
G_REGEX_MATCH_NEWLINE_CRLF Overrides the newline definition set when creating a new GRegex,
setting the ’\r\n’ characters as line terminator.
G_REGEX_MATCH_NEWLINE_ANY Overrides the newline definition set when creating a new GRegex,
any newline character or character sequence is recognized.
Since 2.14
GRegex
A GRegex is the "compiled" form of a regular expression pattern. This structure is opaque and its
fields cannot be accessed directly.
Since 2.14
GRegexEvalCallback ()
Specifies the type of the function passed to g_regex_replace_eval(). It is called for each occurance
of the pattern in the string passed to g_regex_replace_eval(), and it should append the replacement to
result.
match_info : the GMatchInfo generated by the match. Use g_match_info_get_regex() and g_match_info_get_string()
if you need the GRegex or the matched string.
Since 2.14
371
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
g_regex_new ()
Compiles the regular expression to an internal form, and does the initial setup of the GRegex struc-
ture.
Returns : a GRegex structure. Call g_regex_unref() when you are done with it
Since 2.14
g_regex_ref ()
regex : a GRegex
Returns : regex
Since 2.14
g_regex_unref ()
Decreases reference count of regex by 1. When reference count drops to zero, it frees all the memory
associated with the regex structure.
regex : a GRegex
Since 2.14
g_regex_get_pattern ()
Gets the pattern string associated with regex , i.e. a copy of the string passed to g_regex_new().
Since 2.14
372
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
g_regex_get_max_backref ()
Returns the number of the highest back reference in the pattern, or 0 if the pattern does not contain
back references.
regex : a GRegex
Since 2.14
g_regex_get_capture_count ()
regex : a GRegex
Since 2.14
g_regex_get_string_number ()
Since 2.14
g_regex_escape_string ()
Escapes the special characters used for regular expressions in string , for instance "a.b*c" becomes
"a\.b\*c". This function is useful to dynamically generate regular expressions.
string can contain nul characters that are replaced with "\0", in this case remember to specify the
correct length of string in length.
Since 2.14
373
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
g_regex_match_simple ()
g_regex_match ()
Scans for a match in string for the pattern in regex . The match_options are combined with the
match options specified when the regex structure was created, letting you have more flexibility in
reusing GRegex structures.
A GMatchInfo structure, used to get information on the match, is stored in match_info if not NULL.
Note that if match_info is not NULL then it is created even if the function returns FALSE, i.e. you must
free it regardless if regular expression actually matched.
To retrieve all the non-overlapping matches of the pattern in string you can use g_match_info_next().
static void
print_uppercase_words (const gchar *string)
{
/* Print all uppercase-only words. */
GRegex *regex;
GMatchInfo *match_info;
~
regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
g_regex_match (regex, string, 0, &match_info);
while (g_match_info_matches (match_info))
{
gchar *word = g_match_info_fetch (match_info, 0);
g_print ("Found: %s\n", word);
g_free (word);
g_match_info_next (match_info, NULL);
}
g_match_info_free (match_info);
g_regex_unref (regex);
}
374
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
string is not copied and is used in GMatchInfo internally. If you use any GMatchInfo method
(except g_match_info_free()) after freeing or modifying string then the behaviour is undefined.
match_info : pointer to location where to store the GMatchInfo, or NULL if you do not need it
Since 2.14
g_regex_match_full ()
Scans for a match in string for the pattern in regex . The match_options are combined with the
match options specified when the regex structure was created, letting you have more flexibility in
reusing GRegex structures.
Setting start_position differs from just passing over a shortened string and setting G_REGEX_MATCH_NOTBO
in the case of a pattern that begins with any kind of lookbehind assertion, such as "\b".
A GMatchInfo structure, used to get information on the match, is stored in match_info if not NULL.
Note that if match_info is not NULL then it is created even if the function returns FALSE, i.e. you must
free it regardless if regular expression actually matched.
string is not copied and is used in GMatchInfo internally. If you use any GMatchInfo method
(except g_match_info_free()) after freeing or modifying string then the behaviour is undefined.
To retrieve all the non-overlapping matches of the pattern in string you can use g_match_info_next().
static void
print_uppercase_words (const gchar *string)
{
/* Print all uppercase-only words. */
GRegex *regex;
GMatchInfo *match_info;
GError *error = NULL;
~
regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
while (g_match_info_matches (match_info))
{
gchar *word = g_match_info_fetch (match_info, 0);
g_print ("Found: %s\n", word);
g_free (word);
g_match_info_next (match_info, &error);
}
g_match_info_free (match_info);
g_regex_unref (regex);
if (error != NULL)
{
g_printerr ("Error while matching: %s\n", error->message);
g_error_free (error);
}
}
375
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
match_info : pointer to location where to store the GMatchInfo, or NULL if you do not need it
Since 2.14
g_regex_match_all ()
Using the standard algorithm for regular expression matching only the longest match in the string is
retrieved. This function uses a different algorithm so it can retrieve all the possible matches. For more
documentation see g_regex_match_all_full().
A GMatchInfo structure, used to get information on the match, is stored in match_info if not NULL.
Note that if match_info is not NULL then it is created even if the function returns FALSE, i.e. you must
free it regardless if regular expression actually matched.
string is not copied and is used in GMatchInfo internally. If you use any GMatchInfo method
(except g_match_info_free()) after freeing or modifying string then the behaviour is undefined.
match_info : pointer to location where to store the GMatchInfo, or NULL if you do not need it
Since 2.14
g_regex_match_all_full ()
Using the standard algorithm for regular expression matching only the longest match in the string
is retrieved, it is not possibile to obtain all the available matches. For instance matching "<a> <b> <c>"
against the pattern "<.*>" you get "<a> <b> <c>".
This function uses a different algorithm (called DFA, i.e. deterministic finite automaton), so it can
retrieve all the possible matches, all starting at the same point in the string. For instance matching "<a>
376
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
<b> <c>" against the pattern "<.*>" you would obtain three matches: "<a> <b> <c>", "<a> <b>" and
"<a>".
The number of matched strings is retrieved using g_match_info_get_match_count(). To obtain the
matched strings and their position you can use, respectively, g_match_info_fetch() and g_match_info_fetch_pos().
Note that the strings are returned in reverse order of length; that is, the longest matching string is given
first.
Note that the DFA algorithm is slower than the standard one and it is not able to capture substrings,
so backreferences do not work.
Setting start_position differs from just passing over a shortened string and setting G_REGEX_MATCH_NOTBO
in the case of a pattern that begins with any kind of lookbehind assertion, such as "\b".
A GMatchInfo structure, used to get information on the match, is stored in match_info if not NULL.
Note that if match_info is not NULL then it is created even if the function returns FALSE, i.e. you must
free it regardless if regular expression actually matched.
string is not copied and is used in GMatchInfo internally. If you use any GMatchInfo method
(except g_match_info_free()) after freeing or modifying string then the behaviour is undefined.
regex : a GRegex structure from g_regex_new()
match_info : pointer to location where to store the GMatchInfo, or NULL if you do not need it
g_regex_split_simple ()
Breaks the string on the pattern, and returns an array of the tokens. If the pattern contains capturing
parentheses, then the text for each of the substrings will also be returned. If the pattern does not match
anywhere in the string, then the whole string is returned as the first token.
This function is equivalent to g_regex_split() but it does not require to compile the pattern with
g_regex_new(), avoiding some lines of code when you need just to do a split without extracting sub-
strings, capture counts, and so on.
If this function is to be called on the same pattern more than once, it’s more efficient to compile the
pattern once with g_regex_new() and then use g_regex_split().
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing
a single string. The reason for this special case is that being able to represent a empty vector is typically
more useful than consistent handling of empty elements. If you do need to represent empty elements,
you’ll need to check for the empty string before calling this function.
A pattern that can match empty strings splits string into separate characters wherever it matches
the empty string between characters. For example splitting "ab c" using as a separator "\s*", you will
get "a", "b" and "c".
pattern : the regular expression
377
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
Since 2.14
g_regex_split ()
Breaks the string on the pattern, and returns an array of the tokens. If the pattern contains capturing
parentheses, then the text for each of the substrings will also be returned. If the pattern does not match
anywhere in the string, then the whole string is returned as the first token.
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing
a single string. The reason for this special case is that being able to represent a empty vector is typically
more useful than consistent handling of empty elements. If you do need to represent empty elements,
you’ll need to check for the empty string before calling this function.
A pattern that can match empty strings splits string into separate characters wherever it matches
the empty string between characters. For example splitting "ab c" using as a separator "\s*", you will
get "a", "b" and "c".
Since 2.14
g_regex_split_full ()
Breaks the string on the pattern, and returns an array of the tokens. If the pattern contains capturing
parentheses, then the text for each of the substrings will also be returned. If the pattern does not match
anywhere in the string, then the whole string is returned as the first token.
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing
a single string. The reason for this special case is that being able to represent a empty vector is typically
more useful than consistent handling of empty elements. If you do need to represent empty elements,
you’ll need to check for the empty string before calling this function.
A pattern that can match empty strings splits string into separate characters wherever it matches
the empty string between characters. For example splitting "ab c" using as a separator "\s*", you will
get "a", "b" and "c".
Setting start_position differs from just passing over a shortened string and setting G_REGEX_MATCH_NOTBOL
in the case of a pattern that begins with any kind of lookbehind assertion, such as "\b".
378
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
max_tokens : the maximum number of tokens to split string into. If this is less than 1, the string is
split completely
Since 2.14
g_regex_replace ()
Replaces all occurrences of the pattern in regex with the replacement text. Backreferences of the
form ’\number’ or ’\g<number>’ in the replacement text are interpolated by the number-th captured
subexpression of the match, ’\g<name>’ refers to the captured subexpression with the given name. ’\0’
refers to the complete match, but ’\0’ followed by a number is the octal representation of a character.
To include a literal ’\’ in the replacement, write ’\\’. There are also escapes that changes the case of the
following text:
Since 2.14
379
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
g_regex_replace_literal ()
Replaces all occurrences of the pattern in regex with the replacement text. replacement is replaced
literally, to include backreferences use g_regex_replace().
Setting start_position differs from just passing over a shortened string and setting G_REGEX_MATCH_NOTBOL
in the case of a pattern that begins with any kind of lookbehind assertion, such as "\b".
regex : a GRegex structure
g_regex_replace_eval ()
Replaces occurrences of the pattern in regex with the output of eval for that occurrence.
Setting start_position differs from just passing over a shortened string and setting G_REGEX_MATCH_NOTBOL
in the case of a pattern that begins with any kind of lookbehind assertion, such as "\b".
The following example uses g_regex_replace_eval() to replace multiple strings at once:
static gboolean
eval_cb (const GMatchInfo *info,
GString *res,
gpointer data)
{
gchar *match;
gchar *r;
380
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
return FALSE;
}
/* ... */
GRegex *reg;
GHashTable *h;
gchar *res;
/* ... */
Since 2.14
g_regex_check_replacement ()
Checks whether replacement is a valid replacement string (see g_regex_replace()), i.e. that all escape
sequences in it are valid.
If has_references is not NULL then replacement is checked for pattern references. For instance,
replacement text ’foo\n’ does not contain references and may be evaluated without information about
actual match, but ’\0\1’ (whole match followed by first subpattern) requires valid GMatchInfo object.
Since 2.14
381
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
GMatchInfo
GMatchInfo is used to retrieve information about the regular expression match which created it. This
structure is opaque and its fields cannot be accessed directly.
Since 2.14
g_match_info_get_regex ()
Returns GRegex object used in match_info. It belongs to Glib and must not be freed. Use g_regex_ref()
if you need to keep it after you free match_info object.
match_info : a GMatchInfo
Since 2.14
g_match_info_get_string ()
Returns the string searched with match_info. This is the string passed to g_regex_match() or g_regex_replace()
so you may not free it before calling this function.
match_info : a GMatchInfo
Since 2.14
g_match_info_free ()
match_info : a GMatchInfo
Since 2.14
g_match_info_matches ()
Since 2.14
382
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
g_match_info_next ()
Scans for the next match using the same parameters of the previous call to g_regex_match_full() or
g_regex_match() that returned match_info.
The match is done on the string passed to the match function, so you cannot free it before calling this
function.
match_info : a GMatchInfo structure
g_match_info_get_match_count ()
Retrieves the number of matched substrings (including substring 0, that is the whole matched text),
so 1 is returned if the pattern has no substrings in it and 0 is returned if the match failed.
If the last match was obtained using the DFA algorithm, that is using g_regex_match_all() or g_regex_match_all_fu
the retrieved count is not that of the number of capturing parentheses but that of the number of matched
substrings.
match_info : a GMatchInfo structure
g_match_info_is_partial_match ()
Usually if the string passed to g_regex_match*() matches as far as it goes, but is too short to match
the entire pattern, FALSE is returned. There are circumstances where it might be helpful to distinguish
this case from other cases in which there is no match.
Consider, for example, an application where a human is required to type in data for a field with spe-
cific formatting requirements. An example might be a date in the form ddmmmyy, defined by the pat-
tern "ˆ\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$". If the application sees
the user’s keystrokes one by one, and can check that what has been typed so far is potentially valid, it is
able to raise an error as soon as a mistake is made.
GRegex supports the concept of partial matching by means of the G_REGEX_MATCH_PARTIAL
flag. When this is set the return code for g_regex_match() or g_regex_match_full() is, as usual, TRUE
for a complete match, FALSE otherwise. But, when these functions return FALSE, you can check if the
match was partial calling g_match_info_is_partial_match().
When using partial matching you cannot use g_match_info_fetch*().
Because of the way certain internal optimizations are implemented the partial matching algorithm
cannot be used with all patterns. So repeated single characters such as "a{2,4}" and repeated single meta-
sequences such as "\d+" are not permitted if the maximum number of occurrences is greater than one.
Optional items such as "\d?" (where the maximum is one) are permitted. Quantifiers with any values
are permitted after parentheses, so the invalid examples above can be coded thus "(a){2,4}" and "(\d)+".
If G_REGEX_MATCH_PARTIAL is set for a pattern that does not conform to the restrictions, matching
functions return an error.
match_info : a GMatchInfo structure
383
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
g_match_info_expand_references ()
Returns a new string containing the text in string_to_expand with references and escape sequences
expanded. References refer to the last match done with string against regex and have the same syntax
used by g_regex_replace().
The string_to_expand must be UTF-8 encoded even if G_REGEX_RAW was passed to g_regex_new().
The backreferences are extracted from the string passed to the match function, so you cannot call this
function after freeing the string.
match_info may be NULL in which case string_to_expand must not contain references. For in-
stance "foo\n" does not refer to an actual pattern and ’\n’ merely will be replaced with \n character,
while to expand "\0" (whole match) one needs the result of a match. Use g_regex_check_replacement()
to find out whether string_to_expand contains references.
Since 2.14
g_match_info_fetch ()
Retrieves the text matching the match_num’th capturing parentheses. 0 is the full text of the match, 1
is the first paren set, 2 the second, and so on.
If match_num is a valid sub pattern but it didn’t match anything (e.g. sub pattern 1, matching "b"
against "(a)?b") then an empty string is returned.
If the match was obtained using the DFA algorithm, that is using g_regex_match_all() or g_regex_match_all_full(),
the retrieved string is not that of a set of parentheses but that of a matched substring. Substrings are
matched in reverse order of length, so 0 is the longest match.
The string is fetched from the string passed to the match function, so you cannot call this function
after freeing the string.
Returns : The matched substring, or NULL if an error occurred. You have to free the string yourself
Since 2.14
g_match_info_fetch_pos ()
384
CHAPTER 4. GLIB UTILITIES 4.20. PERL-COMPATIBLE REGULAR . . .
Retrieves the position in bytes of the match_num’th capturing parentheses. 0 is the full text of the
match, 1 is the first paren set, 2 the second, and so on.
If match_num is a valid sub pattern but it didn’t match anything (e.g. sub pattern 1, matching "b"
against "(a)?b") then start_pos and end_pos are set to -1 and TRUE is returned.
If the match was obtained using the DFA algorithm, that is using g_regex_match_all() or g_regex_match_all_full(),
the retrieved position is not that of a set of parentheses but that of a matched substring. Substrings are
matched in reverse order of length, so 0 is the longest match.
match_info : GMatchInfo structure
Returns : TRUE if the position was fetched, FALSE otherwise. If the position cannot be fetched, start-
_pos and end_pos are left unchanged
Since 2.14
g_match_info_fetch_named ()
Returns : The matched substring, or NULL if an error occurred. You have to free the string yourself
Since 2.14
g_match_info_fetch_named_pos ()
Returns : TRUE if the position was fetched, FALSE otherwise. If the position cannot be fetched, start-
_pos and end_pos are left unchanged
Since 2.14
385
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
g_match_info_fetch_all ()
Bundles up pointers to each of the matching substrings from a match and stores them in an array of
gchar pointers. The first element in the returned array is the match number 0, i.e. the entire matched
text.
If a sub pattern didn’t match anything (e.g. sub pattern 1, matching "b" against "(a)?b") then an
empty string is inserted.
If the last match was obtained using the DFA algorithm, that is using g_regex_match_all() or g_regex_match_all_full(),
the retrieved strings are not that matched by sets of parentheses but that of the matched substring. Sub-
strings are matched in reverse order of length, so the first one is the longest match.
The strings are fetched from the string passed to the match function, so you cannot call this function
after freeing the string.
match_info : a GMatchInfo structure
Returns : a NULL-terminated array of gchar * pointers. It must be freed using g_strfreev(). If the previ-
ous match failed NULL is returned
Since 2.14
Synopsis
#include <glib.h>
enum GMarkupError;
#define G_MARKUP_ERROR
enum GMarkupParseFlags;
GMarkupParseContext;
GMarkupParser;
gchar* g_markup_escape_text (const gchar *text,
gssize length);
gchar * g_markup_printf_escaped (const char *format,
...);
gchar * g_markup_vprintf_escaped (const char *format,
va_list args);
gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context,
GError **error);
void g_markup_parse_context_free (GMarkupParseContext *context);
void g_markup_parse_context_get_position (GMarkupParseContext *context,
gint *line_number,
gint *char_number);
const gchar * g_markup_parse_context_get_element (GMarkupParseContext *context);
const GSList * g_markup_parse_context_get_element_stack
(GMarkupParseContext *context);
gpointer g_markup_parse_context_get_user_data
(GMarkupParseContext *context);
GMarkupParseContext * g_markup_parse_context_new (const GMarkupParser *parser,
GMarkupParseFlags flags,
gpointer user_data,
GDestroyNotify user_data_dnotify);
386
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
enum GMarkupCollectType;
gboolean g_markup_collect_attributes (const gchar *element_name,
const gchar **attribute_names
const gchar **attribute_value
GError **error,
GMarkupCollectType first_type
const gchar *first_attr,
...);
Description
The "GMarkup" parser is intended to parse a simple markup format that’s a subset of XML. This is
a small, efficient, easy-to-use parser. It should not be used if you expect to interoperate with other
applications generating full-scale XML. However, it’s very useful for application data files, config files,
etc. where you know your application will be the only one writing the file. Full-scale XML parsers
should be able to parse the subset used by GMarkup, so you can easily migrate to full-scale XML at a
later time if the need arises.
GMarkup is not guaranteed to signal an error on all invalid XML; the parser may accept documents
that an XML parser would not. However, XML documents which are not well-formed5 are not consid-
ered valid GMarkup documents.
Simplifications to XML include:
• No user-defined entities.
• Processing instructions, comments and the doctype declaration are "passed through" but are not
interpreted in any way.
• No DTD or validation.
• Elements
• Attributes
• Character references
Details
enum GMarkupError
typedef enum
{
G_MARKUP_ERROR_BAD_UTF8,
G_MARKUP_ERROR_EMPTY,
G_MARKUP_ERROR_PARSE,
5 Being wellformed is a weaker condition than being valid. See the XML specification for definitions of these terms.
387
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
G_MARKUP_ERROR
Error domain for markup parsing. Errors in this domain will be from the GMarkupError enumera-
tion. See GError for information on error domains.
enum GMarkupParseFlags
typedef enum
{
G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
G_MARKUP_TREAT_CDATA_AS_TEXT = 1 << 1,
G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2
} GMarkupParseFlags;
GMarkupParseContext
A parse context is used to parse a stream of bytes that you expect to contain marked-up text. See
g_markup_parse_context_new(), GMarkupParser, and so on for more details.
388
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
GMarkupParser
typedef struct {
/* Called for open tags <foo bar="baz"> */
void (*start_element) (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
Any of the fields in GMarkupParser can be NULL, in which case they will be ignored. Except for the
error function, any of these callbacks can set an error; in particular the G_MARKUP_ERROR_UNKNOWN_ELEMEN
G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, and G_MARKUP_ERROR_INVALID_CONTENT er-
rors are intended to be set from these callbacks. If you set an error from a callback, g_markup_parse_context_parse()
will report that error back to its caller.
start_element () Callback to invoke when the opening tag of an element is seen.
end_element () Callback to invoke when the closing tag of an element is seen. Note that this is also
called for empty tags like <empty/>.
text () Callback to invoke when some text is seen (text is always inside an element). Note that the text of
an element may be spread over multiple calls of this function. If the G_MARKUP_TREAT_CDATA_AS_TEXT
flag is set, this function is also called for the content of CDATA marked sections.
passthrough () Callback to invoke for comments, processing instructions and doctype declarations; if
you’re re-writing the parsed document, write the passthrough text back out in the same position. If
the G_MARKUP_TREAT_CDATA_AS_TEXT flag is not set, this function is also called for CDATA
marked sections.
error () Callback to invoke when an error occurs.
389
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
g_markup_escape_text ()
Escapes text so that the markup parser will parse it verbatim. Less than, greater than, ampersand,
etc. are replaced with the corresponding entities. This function would typically be used when writing
out a file to be parsed with the markup parser.
Note that this function doesn’t protect whitespace and line endings from being processed according
to the XML rules for normalization of line endings and attribute values.
Note also that if given a string containing them, this function will produce character references in
the range of &x1; .. &x1f; for all control sequences except for tabstop, newline and carriage return. The
character references in this range are not valid XML 1.0, but they are valid XML 1.1 and will be accepted
by the GMarkup parser.
g_markup_printf_escaped ()
Formats arguments according to format, escaping all string and character arguments in the fashion
of g_markup_escape_text(). This is useful when you want to insert literal strings into XML-style markup
output, without having to worry that the strings might themselves contain markup.
const char *store = "Fortnum & Mason";
const char *item = "Tea";
char *output;
~
output = g_markup_printf_escaped ("<purchase>"
"<store>%s</store>"
"<item>%s</item>"
"</purchase>",
store, item);
Returns : newly allocated result from formatting operation. Free with g_free().
Since 2.4
g_markup_vprintf_escaped ()
Formats the data in args according to format, escaping all string and character arguments in the
fashion of g_markup_escape_text(). See g_markup_printf_escaped().
Returns : newly allocated result from formatting operation. Free with g_free().
Since 2.4
390
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
g_markup_parse_context_end_parse ()
Signals to the GMarkupParseContext that all data has been fed into the parse context with g_markup_parse_contex
This function reports an error if the document isn’t complete, for example if elements are still open.
context : a GMarkupParseContext
g_markup_parse_context_free ()
Frees a GMarkupParseContext. Can’t be called from inside one of the GMarkupParser functions.
Can’t be called while a subparser is pushed.
context : a GMarkupParseContext
g_markup_parse_context_get_position ()
Retrieves the current line number and the number of the character on that line. Intended for use in
error messages; there are no strict semantics for what constitutes the "current" line number other than
"the best number we could come up with for error messages."
context : a GMarkupParseContext
g_markup_parse_context_get_element ()
context : a GMarkupParseContext
Since 2.2
391
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
g_markup_parse_context_get_element_stack ()
Retrieves the element stack from the internal state of the parser. The returned GSList is a list of strings
where the first item is the currently open tag (as would be returned by g_markup_parse_context_get_element())
and the next item is its immediate parent.
This function is intended to be used in the start_element and end_element handlers where g_markup_parse_context_get_
would merely return the name of the element that is being processed.
context : a GMarkupParseContext
Since 2.16
g_markup_parse_context_get_user_data ()
gpointer g_markup_parse_context_get_user_data
(GMarkupParseContext * ←-
context);
Returns the user_data associated with context. This will either be the user_data that was provided
to g_markup_parse_context_new() or to the most recent call of g_markup_parse_context_push().
context : a GMarkupParseContext
Returns : the provided user_data. The returned data belongs to the markup context and will be freed
when g_markup_context_free() is called.
Since 2.18
g_markup_parse_context_new ()
Creates a new parse context. A parse context is used to parse marked-up documents. You can feed
any number of documents into a context, as long as no errors occur; once an error occurs, the parse
context can’t continue to parse text (you have to free it and create a new parse context).
parser : a GMarkupParser
user_data_dnotify : user data destroy notifier called when the parse context is freed
392
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
g_markup_parse_context_parse ()
Feed some data to the GMarkupParseContext. The data need not be valid UTF-8; an error will be
signaled if it’s invalid. The data need not be an entire document; you can feed a document into the
parser incrementally, via multiple calls to this function. Typically, as you receive data from a network
connection or file, you feed each received chunk of data into this function, aborting the process if an
error occurs. Once an error is reported, no further data may be fed to the GMarkupParseContext; all
errors are fatal.
context : a GMarkupParseContext
g_markup_parse_context_push ()
static void
counter_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
CounterData *data = user_data;
393
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
data->tag_count++;
}
static void
counter_error (GMarkupParseContext *context,
GError *error,
gpointer user_data)
{
CounterData *data = user_data;
In order to allow this parser to be easily used as a subparser, the following interface is provided:
void
start_counting (GMarkupParseContext *context)
{
CounterData *data = g_slice_new (CounterData);
data->tag_count = 0;
g_markup_parse_context_push (context, &counter_subparser, data);
}
gint
end_counting (GMarkupParseContext *context)
{
CounterData *data = g_markup_parse_context_pop (context);
int result;
result = data->tag_count;
g_slice_free (CounterData, data);
return result;
}
context : a GMarkupParseContext
394
CHAPTER 4. GLIB UTILITIES 4.21. SIMPLE XML SUBSET PARSER
parser : a GMarkupParser
Since 2.18
g_markup_parse_context_pop ()
context : a GMarkupParseContext
Since 2.18
enum GMarkupCollectType
typedef enum
{
G_MARKUP_COLLECT_INVALID,
G_MARKUP_COLLECT_STRING,
G_MARKUP_COLLECT_STRDUP,
G_MARKUP_COLLECT_BOOLEAN,
G_MARKUP_COLLECT_TRISTATE,
A mixed enumerated type and flags field. You must specify one type (string, strdup, boolean, tris-
tate). Additionally, you may optionally bitwise OR the type with the flag G_MARKUP_COLLECT_OPTIONAL.
It is likely that this enum will be extended in the future to support other types.
G_MARKUP_COLLECT_STRING collect the string pointer directly from the attribute_values[] array. Ex-
pects a parameter of type (const char **). If G_MARKUP_COLLECT_OPTIONAL is specified and
the attribute isn’t present then the pointer will be set to NULL.
G_MARKUP_COLLECT_BOOLEAN expects a parameter of type (gboolean *) and parses the attribute value
as a boolean. Sets FALSE if the attribute isn’t present. Valid boolean values consist of (case insen-
sitive) "false", "f", "no", "n", "0" and "true", "t", "yes", "y", "1".
G_MARKUP_COLLECT_OPTIONAL can be bitwise ORed with the other fields. If present, allows the at-
tribute not to appear. A default value is set depending on what value type is used.
395
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_markup_collect_attributes ()
Collects the attributes of the element from the data passed to the GMarkupParser start_element func-
tion, dealing with common error conditions and supporting boolean values.
This utility function is not required to write a parser but can save a lot of typing.
The element_name, attribute_names, attribute_values and error parameters passed to the
start_element callback should be passed unmodified to this function.
Following these arguments is a list of "supported" attributes to collect. It is an error to specify multi-
ple attributes with the same name. If any attribute not in the list appears in the attribute_names array
then an unknown attribute error will result.
The GMarkupCollectType field allows specifying the type of collection to perform and if a given
attribute must appear or is optional.
The attribute name is simply the name of the attribute to collect.
The pointer should be of the appropriate type (see the descriptions under GMarkupCollectType) and
may be NULL in case a particular attribute is to be allowed but ignored.
This function deals with issuing errors for missing attributes (of type G_MARKUP_ERROR_MISSING_ATTRIBUTE),
unknown attributes (of type G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE) and duplicate attributes
(of type G_MARKUP_ERROR_INVALID_CONTENT) as well as parse errors for boolean-valued at-
tributes (again of type G_MARKUP_ERROR_INVALID_CONTENT). In all of these cases FALSE will
be returned and error will be set as appropriate.
... : a pointer to the storage location of the first attribute (or NULL), followed by more types names
and pointers, ending with G_MARKUP_COLLECT_INVALID.
Since 2.16
396
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
Synopsis
#include <glib.h>
GKeyFile;
#define G_KEY_FILE_ERROR
enum GKeyFileError;
enum GKeyFileFlags;
397
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
398
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
#define G_KEY_FILE_DESKTOP_GROUP
#define G_KEY_FILE_DESKTOP_KEY_TYPE
399
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
#define G_KEY_FILE_DESKTOP_KEY_VERSION
#define G_KEY_FILE_DESKTOP_KEY_NAME
#define G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME
#define G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY
#define G_KEY_FILE_DESKTOP_KEY_COMMENT
#define G_KEY_FILE_DESKTOP_KEY_ICON
#define G_KEY_FILE_DESKTOP_KEY_HIDDEN
#define G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN
#define G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN
#define G_KEY_FILE_DESKTOP_KEY_TRY_EXEC
#define G_KEY_FILE_DESKTOP_KEY_EXEC
#define G_KEY_FILE_DESKTOP_KEY_PATH
#define G_KEY_FILE_DESKTOP_KEY_TERMINAL
#define G_KEY_FILE_DESKTOP_KEY_MIME_TYPE
#define G_KEY_FILE_DESKTOP_KEY_CATEGORIES
#define G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY
#define G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS
#define G_KEY_FILE_DESKTOP_KEY_URL
#define G_KEY_FILE_DESKTOP_TYPE_APPLICATION
#define G_KEY_FILE_DESKTOP_TYPE_LINK
#define G_KEY_FILE_DESKTOP_TYPE_DIRECTORY
Description
GKeyFile lets you parse, edit or create files containing groups of key-value pairs, which we call key files
for lack of a better name. Several freedesktop.org specifications use key files now, e.g the Desktop Entry
Specification and the Icon Theme Specification.
The syntax of key files is described in detail in the Desktop Entry Specification, here is a quick sum-
mary: Key files consists of groups of key-value pairs, interspersed with comments.
# this is just an example
# there can be comments before the first group
[First Group]
Name=Key File Example\tthis value shows\nescaping
# localized strings are stored in multiple key-value pairs
Welcome=Hello
Welcome[de]=Hallo
Welcome[fr_FR]=Bonjour
Welcome[it]=Ciao
Welcome[be@latin]=Hello
[Another Group]
Numbers=2;20;-200;0
Booleans=true;false;true;true
Lines beginning with a ’#’ and blank lines are considered comments.
Groups are started by a header line containing the group name enclosed in ’[’ and ’]’, and ended
implicitly by the start of the next group or the end of the file. Each key-value pair must be contained in
a group.
Key-value pairs generally have the form key=value, with the exception of localized strings, which
have the form key[locale]=value, with a locale identifier of the form lang_COUNTRYMODIFIER
where COUNTRY and MODIFIER are optional. Space before and after the ’=’ character are ignored. New-
line, tab, carriage return and backslash characters in value are escaped as \n, \t, \r, and \\, respectively.
To preserve leading spaces in values, these can also be escaped as \s.
Key files can store strings (possibly with localized variants), integers, booleans and lists of these.
Lists are separated by a separator character, typically ’;’ or ’,’. To use the list separator character in a
value in a list, it has to be escaped by prefixing it with a backslash.
This syntax is obviously inspired by the .ini files commonly met on Windows, but there are some
important differences:
• .ini files use the ’;’ character to begin comments, key files use the ’#’ character.
• Key files do not allow for ungrouped keys meaning only comments can precede the first group.
400
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
• Key and Group names are case-sensitive, for example a group called [GROUP] is a different group
from [group].
• .ini files don’t have a strongly typed boolean entry type, they only have GetProfileInt. In
GKeyFile only true and false (in lower case) are allowed.
Note that in contrast to the Desktop Entry Specification, groups in key files may contain the same
key multiple times; the last entry wins. Key files may also contain multiple groups with the same name;
they are merged together. Another difference is that keys and group names in key files are not restricted
to ASCII characters.
Details
GKeyFile
The GKeyFile struct contains only private fields and should not be used directly.
G_KEY_FILE_ERROR
Error domain for key file parsing. Errors in this domain will be from the GKeyFileError enumeration.
See GError for information on error domains.
enum GKeyFileError
typedef enum
{
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
G_KEY_FILE_ERROR_PARSE,
G_KEY_FILE_ERROR_NOT_FOUND,
G_KEY_FILE_ERROR_KEY_NOT_FOUND,
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
G_KEY_FILE_ERROR_INVALID_VALUE
} GKeyFileError;
enum GKeyFileFlags
typedef enum
{
G_KEY_FILE_NONE = 0,
G_KEY_FILE_KEEP_COMMENTS = 1 << 0,
G_KEY_FILE_KEEP_TRANSLATIONS = 1 << 1
} GKeyFileFlags;
401
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_new ()
g_key_file_free ()
Frees a GKeyFile.
key_file : a GKeyFile
Since 2.6
g_key_file_set_list_separator ()
Sets the character which is used to separate values in lists. Typically ’;’ or ’,’ are used as separators.
The default list separator is ’;’.
key_file : a GKeyFile
Since 2.6
g_key_file_load_from_file ()
Loads a key file into an empty GKeyFile structure. If the file could not be loaded then error is set to
either a GFileError or GKeyFileError.
key_file : an empty GKeyFile struct
402
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_load_from_data ()
Loads a key file from memory into an empty GKeyFile structure. If the object cannot be created then
error is set to a GKeyFileError.
Since 2.6
g_key_file_load_from_data_dirs ()
This function looks for a key file named file in the paths returned from g_get_user_data_dir() and
g_get_system_data_dirs(), loads the file into key_file and returns the file’s full path in full_path. If
the file could not be loaded then an error is set to either a GFileError or GKeyFileError.
full_path : return location for a string containing the full path of the file, or NULL
Since 2.6
g_key_file_load_from_dirs ()
This function looks for a key file named file in the paths specified in search_dirs, loads the file
into key_file and returns the file’s full path in full_path. If the file could not be loaded then an error
is set to either a GFileError or GKeyFileError.
403
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
full_path : return location for a string containing the full path of the file, or NULL
Since 2.14
g_key_file_to_data ()
key_file : a GKeyFile
length : return location for the length of the returned string, or NULL
Since 2.6
g_key_file_get_start_group ()
key_file : a GKeyFile
Since 2.6
g_key_file_get_groups ()
Returns all groups in the key file loaded with key_file. The array of returned groups will be NULL-
terminated, so length may optionally be NULL.
key_file : a GKeyFile
Since 2.6
404
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_get_keys ()
Returns all keys for the group name group_name. The array of returned keys will be NULL-terminated,
so length may optionally be NULL. In the event that the group_name cannot be found, NULL is re-
turned and error is set to G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
key_file : a GKeyFile
Since 2.6
g_key_file_has_group ()
key_file : a GKeyFile
Since 2.6
g_key_file_has_key ()
Looks whether the key file has the key key in the group group_name.
key_file : a GKeyFile
Since 2.6
405
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_get_value ()
Returns the raw value associated with key under group_name. Use g_key_file_get_string() to re-
trieve an unescaped UTF-8 string.
In the event the key cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND.
In the event that the group_name cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_GROUP_NO
key_file : a GKeyFile
key : a key
Returns : a newly allocated string or NULL if the specified key cannot be found.
Since 2.6
g_key_file_get_string ()
Returns the string value associated with key under group_name. Unlike g_key_file_get_value(), this
function handles escape sequences like \s.
In the event the key cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND.
In the event that the group_name cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_GROUP_NO
key_file : a GKeyFile
key : a key
Returns : a newly allocated string or NULL if the specified key cannot be found.
Since 2.6
g_key_file_get_locale_string ()
Returns the value associated with key under group_name translated in the given locale if available.
If locale is NULL then the current locale is assumed.
If key cannot be found then NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND.
If the value associated with key cannot be interpreted or no suitable translation can be found then the
untranslated value is returned.
key_file : a GKeyFile
406
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
key : a key
Returns : a newly allocated string or NULL if the specified key cannot be found.
Since 2.6
g_key_file_get_boolean ()
key_file : a GKeyFile
key : a key
Returns : the value associated with the key as a boolean, or FALSE if the key was not found or could
not be parsed.
Since 2.6
g_key_file_get_integer ()
key_file : a GKeyFile
key : a key
Returns : the value associated with the key as an integer, or 0 if the key was not found or could not be
parsed.
Since 2.6
407
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_get_double ()
Returns the value associated with key under group_name as a double. If group_name is NULL, the
start_group is used.
If key cannot be found then 0.0 is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND.
Likewise, if the value associated with key cannot be interpreted as a double then 0.0 is returned and e-
rror is set to G_KEY_FILE_ERROR_INVALID_VALUE.
key_file : a GKeyFile
key : a key
Returns : the value associated with the key as a double, or 0.0 if the key was not found or could not be
parsed.
Since 2.12
g_key_file_get_string_list ()
key_file : a GKeyFile
key : a key
Returns : a NULL-terminated string array or NULL if the specified key cannot be found. The array
should be freed with g_strfreev().
Since 2.6
g_key_file_get_locale_string_list ()
408
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
Returns the values associated with key under group_name translated in the given locale if available.
If locale is NULL then the current locale is assumed.
If key cannot be found then NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND.
If the values associated with key cannot be interpreted or no suitable translations can be found then the
untranslated values are returned. The returned array is NULL-terminated, so length may optionally be
NULL.
key_file : a GKeyFile
key : a key
Returns : a newly allocated NULL-terminated string array or NULL if the key isn’t found. The string
array should be freed with g_strfreev().
Since 2.6
g_key_file_get_boolean_list ()
key_file : a GKeyFile
key : a key
Returns : the values associated with the key as a list of booleans, or NULL if the key was not found or
could not be parsed.
Since 2.6
g_key_file_get_integer_list ()
409
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
key_file : a GKeyFile
key : a key
Returns : the values associated with the key as a list of integers, or NULL if the key was not found or
could not be parsed.
Since 2.6
g_key_file_get_double_list ()
key_file : a GKeyFile
key : a key
Returns : the values associated with the key as a list of doubles, or NULL if the key was not found or
could not be parsed.
Since 2.12
g_key_file_get_comment ()
Retrieves a comment above key from group_name. If key is NULL then comment will be read from
above group_name. If both key and group_name are NULL, then comment will be read from above the
first group in the file.
key_file : a GKeyFile
key : a key
Since 2.6
410
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_set_value ()
key_file : a GKeyFile
key : a key
value : a string
Since 2.6
g_key_file_set_string ()
Associates a new string value with key under group_name. If key cannot be found then it is created.
If group_name cannot be found then it is created. Unlike g_key_file_set_value(), this function handles
characters that need escaping, such as newlines.
key_file : a GKeyFile
key : a key
string : a string
Since 2.6
g_key_file_set_locale_string ()
Associates a string value for key and locale under group_name. If the translation for key cannot be
found then it is created.
key_file : a GKeyFile
key : a key
string : a string
Since 2.6
411
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_set_boolean ()
Associates a new boolean value with key under group_name. If key cannot be found then it is
created.
key_file : a GKeyFile
key : a key
Since 2.6
g_key_file_set_integer ()
Associates a new integer value with key under group_name. If key cannot be found then it is created.
key_file : a GKeyFile
key : a key
Since 2.6
g_key_file_set_double ()
Associates a new double value with key under group_name. If key cannot be found then it is created.
key_file : a GKeyFile
key : a key
Since 2.12
412
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
g_key_file_set_string_list ()
Associates a list of string values for key under group_name. If key cannot be found then it is created.
If group_name cannot be found then it is created.
key_file : a GKeyFile
key : a key
Since 2.6
g_key_file_set_locale_string_list ()
Associates a list of string values for key and locale under group_name. If the translation for key
cannot be found then it is created.
key_file : a GKeyFile
key : a key
Since 2.6
g_key_file_set_boolean_list ()
Associates a list of boolean values with key under group_name. If key cannot be found then it is
created. If group_name is NULL, the start_group is used.
key_file : a GKeyFile
413
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
key : a key
Since 2.6
g_key_file_set_integer_list ()
Associates a list of integer values with key under group_name. If key cannot be found then it is
created.
key_file : a GKeyFile
key : a key
Since 2.6
g_key_file_set_double_list ()
Associates a list of double values with key under group_name. If key cannot be found then it is
created.
key_file : a GKeyFile
key : a key
Since 2.12
g_key_file_set_comment ()
Places a comment above key from group_name. If key is NULL then comment will be written above
group_name. If both key and group_name are NULL, then comment will be written above the first group
in the file.
414
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
key_file : a GKeyFile
key : a key
comment : a comment
Since 2.6
g_key_file_remove_group ()
key_file : a GKeyFile
Since 2.6
g_key_file_remove_key ()
key_file : a GKeyFile
Since 2.6
g_key_file_remove_comment ()
Removes a comment above key from group_name. If key is NULL then comment will be removed
above group_name. If both key and group_name are NULL, then comment will be removed above the
first group in the file.
key_file : a GKeyFile
415
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
key : a key
Since 2.6
G_KEY_FILE_DESKTOP_GROUP
The name of the main group of a desktop entry file, as defined in the Desktop Entry Specification.
Consult the specification for more details about the meanings of the keys below.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_TYPE
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a string giving the type of the desktop
entry. Usually G_KEY_FILE_DESKTOP_TYPE_APPLICATION, G_KEY_FILE_DESKTOP_TYPE_LINK,
or G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_VERSION
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a string giving the version of the
Desktop Entry Specification used for the desktop entry file.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_NAME
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a localized string giving the specific
name of the desktop entry.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a localized string giving the generic
name of the desktop entry.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a boolean stating whether the desk-
top entry should be shown in menus.
Since 2.14
416
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
G_KEY_FILE_DESKTOP_KEY_COMMENT
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a localized string giving the tooltip
for the desktop entry.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_ICON
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a localized string giving the name of
the icon to be displayed for the desktop entry.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_HIDDEN
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a boolean stating whether the desk-
top entry has been deleted by the user.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a list of strings identifying the envi-
ronments that should display the desktop entry.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a list of strings identifying the envi-
ronments that should not display the desktop entry.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_TRY_EXEC
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a string giving the file name of a
binary on disk used to determine if the program is actually installed. It is only valid for desktop entries
with the Application type.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_EXEC
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a string giving the command line to
execute. It is only valid for desktop entries with the Application type.
Since 2.14
417
CHAPTER 4. GLIB UTILITIES 4.22. KEY-VALUE FILE PARSER
G_KEY_FILE_DESKTOP_KEY_PATH
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a string containing the working di-
rectory to run the program in. It is only valid for desktop entries with the Application type.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_TERMINAL
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a boolean stating whether the pro-
gram should be run in a terminal window. It is only valid for desktop entries with the Application
type.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_MIME_TYPE
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a list of strings giving the MIME
types supported by this desktop entry.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_CATEGORIES
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a list of strings giving the categories
in which the desktop entry should be shown in a menu.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a boolean stating whether the appli-
cation supports the Startup Notification Protocol Specification.
Since 2.14
G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS
G_KEY_FILE_DESKTOP_KEY_URL
A key under G_KEY_FILE_DESKTOP_GROUP whose value is a string giving the URL to access. It
is only valid for desktop entries with the Link type.
Since 2.14
418
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
G_KEY_FILE_DESKTOP_TYPE_APPLICATION
The value of the G_KEY_FILE_DESKTOP_KEY_TYPE key for desktop entries representing applica-
tions.
Since 2.14
G_KEY_FILE_DESKTOP_TYPE_LINK
The value of the G_KEY_FILE_DESKTOP_KEY_TYPE key for desktop entries representing links to
documents.
Since 2.14
G_KEY_FILE_DESKTOP_TYPE_DIRECTORY
The value of the G_KEY_FILE_DESKTOP_KEY_TYPE key for desktop entries representing directo-
ries.
Since 2.14
Synopsis
#include <glib.h>
GBookmarkFile;
#define G_BOOKMARK_FILE_ERROR
enum GBookmarkFileError;
GBookmarkFile * g_bookmark_file_new (void);
void g_bookmark_file_free (GBookmarkFile *bookmark);
gboolean g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
const gchar *filename,
GError **error);
gboolean g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
const gchar *data,
gsize length,
GError **error);
gboolean g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
const gchar *file,
gchar **full_path,
GError **error);
gchar * g_bookmark_file_to_data (GBookmarkFile *bookmark,
gsize *length,
GError **error);
gboolean g_bookmark_file_to_file (GBookmarkFile *bookmark,
const gchar *filename,
GError **error);
gboolean g_bookmark_file_has_item (GBookmarkFile *bookmark,
419
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
420
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
Description
GBookmarkFile lets you parse, edit or create files containing bookmarks to URI, along with some meta-
data about the resource pointed by the URI like its MIME type, the application that is registering the
421
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
bookmark and the icon that should be used to represent the bookmark. The data is stored using the
Desktop Bookmark Specification.
The syntax of the bookmark files is described in detail inside the Desktop Bookmark Specification,
here is a quick summary: bookmark files use a sub-class of the XML Bookmark Exchange Language
specification, consisting of valid UTF-8 encoded XML, under the xbel root element; each bookmark is
stored inside a bookmark element, using its URI: no relative paths can be used inside a bookmark file.
The bookmark may have a user defined title and description, to be used instead of the URI. Under the
metadata element, with its owner attribute set to http://freedesktop.org, is stored the meta-
data about a resource pointed by its URI. The meta-data consists of the resource’s MIME type; the
applications that have registered a bookmark; the groups to which a bookmark belongs to; a visibility
flag, used to set the bookmark as "private" to the applications and groups that has it registered; the URI
and MIME type of an icon, to be used when displaying the bookmark inside a GUI.
<?xml version="1.0"?>
<!DOCTYPE xbel PUBLIC
"+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML"
"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">
<xbel version="1.0"
xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"
xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks">
<bookmark href="file:///home/ebassi/bookmark-spec/bookmark-spec.xml">
<title>Desktop Bookmarks Spec</title>
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type>text/xml</mime:mime-type>
<bookmark:applications>
<bookmark:application name="GEdit" count="2" exec="gedit %u" timestamp ←-
="1115726763"/>
<bookmark:application name="GViM" count="7" exec="gvim %f" timestamp ←-
="1115726812"/>
</bookmark:applications>
<bookmark:groups>
<bookmark:group>Editors</bookmark:group>
</bookmark:groups>
</metadata>
</info>
</bookmark>
</xbel>
A bookmark file might contain more than one bookmark; each bookmark is accessed through its
URI.
The important caveat of bookmark files is that when you add a new bookmark you must also add the
application that is registering it, using g_bookmark_file_add_application() or g_bookmark_file_set_app_info().
If a bookmark has no applications then it won’t be dumped when creating the on disk representation,
using g_bookmark_file_to_data() or g_bookmark_file_to_file().
The GBookmarkFile parser was added in GLib 2.12.
Details
GBookmarkFile
The GBookmarkFile struct contains only private data and should not be used directly.
G_BOOKMARK_FILE_ERROR
Error domain for bookmark file parsing. Errors in this domain will be from the GBookmarkFileError
enumeration. See GError for informations on error domains.
422
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
enum GBookmarkFileError
typedef enum
{
G_BOOKMARK_FILE_ERROR_INVALID_URI,
G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
G_BOOKMARK_FILE_ERROR_READ,
G_BOOKMARK_FILE_ERROR_UNKNOWN_ENCODING,
G_BOOKMARK_FILE_ERROR_WRITE,
G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND
} GBookmarkFileError;
g_bookmark_file_new ()
g_bookmark_file_free ()
Frees a GBookmarkFile.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_load_from_file ()
Loads a desktop bookmark file into an empty GBookmarkFile structure. If the file could not be
loaded then error is set to either a GFileError or GBookmarkFileError.
423
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
filename : the path of a filename to load, in the GLib file name encoding
g_bookmark_file_load_from_data ()
Loads a bookmark file from memory into an empty GBookmarkFile structure. If the object cannot be
created then error is set to a GBookmarkFileError.
bookmark : an empty GBookmarkFile struct
g_bookmark_file_load_from_data_dirs ()
This function looks for a desktop bookmark file named file in the paths returned from g_get_user_data_dir()
and g_get_system_data_dirs(), loads the file into bookmark and returns the file’s full path in full_path.
If the file could not be loaded then an error is set to either a GFileError or GBookmarkFileError.
bookmark : a GBookmarkFile
full_path : return location for a string containing the full path of the file, or NULL
g_bookmark_file_to_data ()
length : return location for the length of the returned string, or NULL
424
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_to_file ()
This function outputs bookmark into a file. The write process is guaranteed to be atomic by using
g_file_set_contents() internally.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_has_item ()
Looks whether the desktop bookmark has an item with its URI set to uri.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_has_group ()
Checks whether group appears in the list of groups to which the bookmark for uri belongs to.
In the event the URI cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_N
bookmark : a GBookmarkFile
Since 2.12
425
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_has_application ()
Checks whether the bookmark for uri inside bookmark has been registered by application name.
In the event the URI cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_NOT_F
bookmark : a GBookmarkFile
g_bookmark_file_get_size ()
g_bookmark_file_get_uris ()
Returns all URIs of the bookmarks in the bookmark file bookmark. The array of returned URIs will
be NULL-terminated, so length may optionally be NULL.
bookmark : a GBookmarkFile
Returns : a newly allocated NULL-terminated array of strings. Use g_strfreev() to free it.
Since 2.12
g_bookmark_file_get_title ()
Returns : a newly allocated string or NULL if the specified URI cannot be found.
Since 2.12
426
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_get_description ()
bookmark : a GBookmarkFile
Returns : a newly allocated string or NULL if the specified URI cannot be found.
Since 2.12
g_bookmark_file_get_mime_type ()
bookmark : a GBookmarkFile
Returns : a newly allocated string or NULL if the specified URI cannot be found.
Since 2.12
g_bookmark_file_get_is_private ()
Gets whether the private flag of the bookmark for uri is set.
In the event the URI cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_N
In the event that the private flag cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR
bookmark : a GBookmarkFile
Since 2.12
427
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_get_icon ()
bookmark : a GBookmarkFile
Returns : TRUE if the icon for the bookmark for the URI was found. You should free the returned
strings.
Since 2.12
g_bookmark_file_get_added ()
Gets the time the bookmark for uri was added to bookmark
In the event the URI cannot be found, -1 is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_NOT_FOUN
bookmark : a GBookmarkFile
Returns : a timestamp
Since 2.12
g_bookmark_file_get_modified ()
Gets the time when the bookmark for uri was last modified.
In the event the URI cannot be found, -1 is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_NOT_FOUN
bookmark : a GBookmarkFile
Returns : a timestamp
Since 2.12
428
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_get_visited ()
Gets the time the bookmark for uri was last visited.
In the event the URI cannot be found, -1 is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_NOT_
bookmark : a GBookmarkFile
Returns : a timestamp.
Since 2.12
g_bookmark_file_get_groups ()
bookmark : a GBookmarkFile
length : return location for the length of the returned string, or NULL
Returns : a newly allocated NULL-terminated array of group names. Use g_strfreev() to free it.
Since 2.12
g_bookmark_file_get_applications ()
Retrieves the names of the applications that have registered the bookmark for uri.
In the event the URI cannot be found, NULL is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_N
bookmark : a GBookmarkFile
Returns : a newly allocated NULL-terminated array of strings. Use g_strfreev() to free it.
Since 2.12
429
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_get_app_info ()
Gets the registration informations of app_name for the bookmark for uri. See g_bookmark_file_set_app_info()
for more informations about the returned data.
The string returned in app_exec must be freed.
In the event the URI cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_NOT_F
In the event that no application with name app_name has registered a bookmark for uri, FALSE is re-
turned and error is set to G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that
unquoting the command line fails, an error of the G_SHELL_ERROR domain is set and FALSE is re-
turned.
bookmark : a GBookmarkFile
g_bookmark_file_set_title ()
Sets title as the title of the bookmark for uri inside the bookmark file bookmark.
If uri is NULL, the title of bookmark is set.
If a bookmark for uri cannot be found then it is created.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_set_description ()
430
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
bookmark : a GBookmarkFile
description : a string
Since 2.12
g_bookmark_file_set_mime_type ()
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_set_is_private ()
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_set_icon ()
Sets the icon for the bookmark for uri. If href is NULL, unsets the currently set icon. href can
either be a full URL for the icon file or the icon name following the Icon Naming specification.
If no bookmark for uri is found one is created.
bookmark : a GBookmarkFile
Since 2.12
431
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_set_added ()
Sets the time the bookmark for uri was added into bookmark.
If no bookmark for uri is found then it is created.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_set_groups ()
Sets a list of group names for the item with URI uri. Each previously set group name list is removed.
If uri cannot be found then an item for it is created.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_set_modified ()
Sets the last time the bookmark for uri was last modified.
If no bookmark for uri is found then it is created.
The "modified" time should only be set when the bookmark’s meta-data was actually changed. Every
function of GBookmarkFile that modifies a bookmark also changes the modification time, except for
g_bookmark_file_set_visited().
bookmark : a GBookmarkFile
Since 2.12
432
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_set_visited ()
Sets the time the bookmark for uri was last visited.
If no bookmark for uri is found then it is created.
The "visited" time should only be set if the bookmark was launched, either using the command line
retrieved by g_bookmark_file_get_app_info() or by the default application for the bookmark’s MIME
type, retrieved using g_bookmark_file_get_mime_type(). Changing the "visited" time does not affect
the "modified" time.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_set_app_info ()
Sets the meta-data of application name inside the list of applications that have registered a bookmark
for uri inside bookmark.
You should rarely use this function; use g_bookmark_file_add_application() and g_bookmark_file_remove_applic
instead.
name can be any UTF-8 encoded string used to identify an application. exec can have one of these
two modifiers: "f", which will be expanded as the local file name retrieved from the bookmark’s URI;
"u", which will be expanded as the bookmark’s URI. The expansion is done automatically when retriev-
ing the stored command line using the g_bookmark_file_get_app_info() function. count is the number
of times the application has registered the bookmark; if is < 0, the current registration count will be in-
creased by one, if is 0, the application with name will be removed from the list of registered applications.
stamp is the Unix time of the last registration; if it is -1, the current time will be used.
If you try to remove an application by setting its registration count to zero, and no bookmark for u-
ri is found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND;
similarly, in the event that no application name has registered a bookmark for uri, FALSE is returned and
error is set to G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark for
uri is found, one is created.
bookmark : a GBookmarkFile
433
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_add_group ()
Adds group to the list of groups to which the bookmark for uri belongs to.
If no bookmark for uri is found then it is created.
bookmark : a GBookmarkFile
g_bookmark_file_add_application ()
Adds the application with name and exec to the list of applications that have registered a bookmark
for uri into bookmark.
Every bookmark inside a GBookmarkFile must have at least an application registered. Each appli-
cation must provide a name, a command line useful for launching the bookmark, the number of times
the bookmark has been registered by the application and the last time the application registered this
bookmark.
If name is NULL, the name of the application will be the same returned by g_get_application_name();
if exec is NULL, the command line will be a composition of the program name as returned by g_get_prgname()
and the "u" modifier, which will be expanded to the bookmark’s URI.
This function will automatically take care of updating the registrations count and timestamping in
case an application with the same name had already registered a bookmark for uri inside bookmark.
If no bookmark for uri is found, one is created.
bookmark : a GBookmarkFile
g_bookmark_file_remove_group ()
Removes group from the list of groups to which the bookmark for uri belongs to.
In the event the URI cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_NOT_F
In the event no group was defined, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_INVALID_VALUE
bookmark : a GBookmarkFile
434
CHAPTER 4. GLIB UTILITIES 4.23. BOOKMARK FILE PARSER
g_bookmark_file_remove_application ()
Removes application registered with name from the list of applications that have registered a book-
mark for uri inside bookmark.
In the event the URI cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_N
In the event that no application with name app_name has registered a bookmark for uri, FALSE is re-
turned and error is set to G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_remove_item ()
Removes the bookmark for uri from the bookmark file bookmark.
bookmark : a GBookmarkFile
Since 2.12
g_bookmark_file_move_item ()
Changes the URI of a bookmark item from old_uri to new_uri. Any existing bookmark for new_uri
will be overwritten. If new_uri is NULL, then the bookmark is removed.
In the event the URI cannot be found, FALSE is returned and error is set to G_BOOKMARK_FILE_ERROR_URI_N
bookmark : a GBookmarkFile
Since 2.12
435
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
4.24 Testing
Name
Testing – a test framework
Synopsis
#include <glib.h>
436
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
Description
GLib provides a framework for writing and maintaining unit tests in parallel to the code they are testing.
The API is designed according to established concepts found in the other test frameworks (JUnit, NUnit,
RUnit), which in turn is based on smalltalk unit testing concepts.
Test case Tests (test methods) are grouped together with their fixture into test cases.
Fixture A test fixture consists of fixture data and setup and teardown methods to establish the envi-
ronment for the test functions. We use fresh fixtures, i.e. fixtures are newly set up and torn down
around each test invocation to avoid dependencies between tests.
Test suite Test cases can be grouped into test suites, to allow subsets of the available tests to be run. Test
suites can be grouped into other test suites as well.
The API is designed to handle creation and registration of test suites and test cases implicitly. A simple
call like
g_test_add_func ("/misc/assertions", test_assertions);
creates a test suite called "misc" with a single test case named "assertions", which consists of running the
test_assertions function.
In addition to the traditional g_assert(), the test framework provides an extended set of assertions for
string and numerical comparisons: g_assert_cmpfloat(), g_assert_cmpint(), g_assert_cmpuint(), g_assert_cmphex(),
g_assert_cmpstr(). The advantage of these variants over plain g_assert() is that the assertion messages
can be more elaborate, and include the values of the compared entities.
GLib ships with two utilities called gtester and gtester-report to facilitate running tests and produc-
ing nicely formatted test reports.
Details
g_test_minimized_result ()
437
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
Report the result of a performance or measurement test. The test should generally strive to minimize
the reported quantities (smaller values are better than larger ones), this and minimized_quantity can
determine sorting order for test result reports.
Since 2.16
g_test_maximized_result ()
Report the result of a performance or measurement test. The test should generally strive to maximize
the reported quantities (larger values are better than smaller ones), this and maximized_quantity can
determine sorting order for test result reports.
Since 2.16
g_test_init ()
Initialize the GLib testing framework, e.g. by seeding the test random number generator, the name
for g_get_prgname() and parsing test related command line args. So far, the following arguments are
understood:
--seed=RANDOMSEED provide a random seed to reproduce test runs using random numbers.
438
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
argc : Address of the argc parameter of the main() function. Changed if any arguments were handled.
argv : Address of the argv parameter of main(). Any parameters understood by g_test_init() stripped
before return.
... : Reserved for future extension. Currently, you must pass NULL.
Since 2.16
g_test_quick()
#define g_test_quick()
g_test_slow()
#define g_test_slow()
g_test_thorough()
#define g_test_thorough()
g_test_perf()
#define g_test_perf()
g_test_verbose()
#define g_test_verbose()
g_test_quiet()
#define g_test_quiet()
g_test_run ()
Runs all tests under the toplevel suite which can be retrieved with g_test_get_root(). Similar to
g_test_run_suite(), the test cases to be run are filtered according to test path arguments (-p testpath) as
parsed by g_test_init(). g_test_run_suite() or g_test_run() may only be called once in a program.
Returns : 0 on success
Since 2.16
439
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_test_add_func ()
Create a new test case, similar to g_test_create_case(). However the test is assumed to use no fixture,
and test suites are automatically created on the fly and added to the root fixture, based on the slash-
separated portions of testpath.
Since 2.16
g_test_add_data_func ()
Create a new test case, similar to g_test_create_case(). However the test is assumed to use no fixture,
and test suites are automatically created on the fly and added to the root fixture, based on the slash-
separated portions of testpath. The test_data argument will be passed as first argument to test_f-
unc.
Since 2.16
g_test_add()
Hook up a new test case at testpath, similar to g_test_add_func(). A fixture data structure with
setup and teardown function may be provided though, similar to g_test_create_case(). g_test_add() is
implemented as a macro, so that the fsetup(), ftest() and fteardown() callbacks can expect a Fixture
pointer as first argument in a type safe manner.
Since 2.16
440
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_test_message ()
Since 2.16
g_test_bug_base ()
Since 2.16
g_test_bug ()
This function adds a message to test reports that associates a bug URI with a test case. Bug URIs are
constructed from a base URI set with g_test_bug_base() and bug_uri_snippet.
Since 2.16
g_test_timer_start ()
Start a timing test. Call g_test_timer_elapsed() when the task is supposed to be done. Call this
function again to restart the timer.
Since 2.16
g_test_timer_elapsed ()
Get the time since the last start of the timer with g_test_timer_start().
Returns : the time since the last start of the timer, as a double
Since 2.16
441
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_test_timer_last ()
Since 2.16
g_test_queue_free ()
Enqueue a pointer to be released with g_free() during the next teardown phase. This is equivalent to
calling g_test_queue_destroy() with a destroy callback of g_free().
Since 2.16
g_test_queue_destroy ()
This function enqueus a callback @destroy_func() to be executed during the next test case teardown
phase. This is most useful to auto destruct allocted test resources at the end of a test run. Resources are
released in reverse queue order, that means enqueueing callback A before callback B will cause B() to be
called before A() during teardown.
Since 2.16
g_test_queue_unref()
#define g_test_queue_unref(gobject)
Enqueue an object to be released with g_object_unref() during the next teardown phase. This is
equivalent to calling g_test_queue_destroy() with a destroy callback of g_object_unref().
Since 2.16
enum GTestTrapFlags
typedef enum {
G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
G_TEST_TRAP_INHERIT_STDIN = 1 << 9
} GTestTrapFlags;
Test traps are guards around forked tests. These flags determine what traps to set.
442
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_test_trap_fork ()
Fork the current test program to execute a test case that might not return or that might abort. The
forked test case is aborted and considered failing if its run time exceeds usec_timeout.
The forking behavior can be configured with the GTestTrapFlags flags.
In the following example, the test code forks, the forked child process produces some sample output
and exits successfully. The forking parent process then asserts successful child program termination and
validates child program outputs.
static void
test_fork_patterns (void)
{
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | ←-
G_TEST_TRAP_SILENCE_STDERR))
{
g_print ("some stdout text: somagic17\n");
g_printerr ("some stderr text: semagic43\n");
exit (0); /* successful test run */
}
g_test_trap_assert_passed();
g_test_trap_assert_stdout ("*somagic17*");
g_test_trap_assert_stderr ("*semagic43*");
}
Returns : TRUE for the forked child and FALSE for the executing parent process.
Since 2.16
g_test_trap_has_passed ()
g_test_trap_reached_timeout ()
443
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_test_trap_assert_passed()
#define g_test_trap_assert_passed()
g_test_trap_assert_failed()
#define g_test_trap_assert_failed()
g_test_trap_assert_stdout()
#define g_test_trap_assert_stdout(soutpattern)
Assert that the stdout output of the last forked test matches soutpattern. See g_test_trap_fork().
Since 2.16
g_test_trap_assert_stdout_unmatched()
#define g_test_trap_assert_stdout_unmatched(soutpattern)
Assert that the stdout output of the last forked test does not match soutpattern. See g_test_trap_fork().
Since 2.16
g_test_trap_assert_stderr()
#define g_test_trap_assert_stderr(serrpattern)
Assert that the stderr output of the last forked test matches serrpattern. See g_test_trap_fork().
Since 2.16
g_test_trap_assert_stderr_unmatched()
#define g_test_trap_assert_stderr_unmatched(serrpattern)
Assert that the stderr output of the last forked test does not match serrpattern. See g_test_trap_fork().
Since 2.16
g_test_rand_bit()
#define g_test_rand_bit()
Get a reproducible random bit (0 or 1), see g_test_rand_int() for details on test case random numbers.
Since 2.16
444
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_test_rand_int ()
g_test_rand_int_range ()
Get a reproducible random integer number out of a specified range, see g_test_rand_int() for details
on test case random numbers.
begin : the minimum value returned by this function
g_test_rand_double ()
Get a reproducible random floating point number, see g_test_rand_int() for details on test case ran-
dom numbers.
Returns : a random number from the seeded random number generator.
Since 2.16
g_test_rand_double_range ()
Get a reproducible random floating pointer number out of a specified range, see g_test_rand_int()
for details on test case random numbers.
range_start : the minimum value returned by this function
g_assert()
#define g_assert(expr)
Debugging macro to terminate the application if the assertion fails. If the assertion fails (i.e. the
expression is not true), an error message is logged and the application is terminated.
The macro can be turned off in final releases of code by defining G_DISABLE_ASSERT when com-
piling the application.
expr : the expression to check.
445
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_assert_not_reached()
#define g_assert_not_reached()
Debugging macro to terminate the application if it is ever reached. If it is reached, an error message
is logged and the application is terminated.
The macro can be turned off in final releases of code by defining G_DISABLE_ASSERT when com-
piling the application.
g_assert_cmpstr()
Debugging macro to terminate the application with a warning message if a string comparison fails.
The strings are compared using g_strcmp0().
The effect of g_assert_cmpstr (s1, op, s2) is the same as g_assert (g_strcmp0 (s1-
, s2) op 0). The advantage of this macro is that it can produce a message that includes the actual
values of s1 and s2.
g_assert_cmpstr (mystring, ==, "fubar");
cmp : The comparison operator to use. One of ==, !=, <, >, <=, >=.
Since 2.16
g_assert_cmpint()
Debugging macro to terminate the application with a warning message if an integer comparison
fails.
The effect of g_assert_cmpint (n1, op, n2) is the same as g_assert (n1 op n2). The
advantage of this macro is that it can produce a message that includes the actual values of n1 and n2.
n1 : an integer
cmp : The comparison operator to use. One of ==, !=, <, >, <=, >=.
n2 : another integer
Since 2.16
g_assert_cmpuint()
Debugging macro to terminate the application with a warning message if an unsigned integer com-
parison fails.
The effect of g_assert_cmpuint (n1, op, n2) is the same as g_assert (n1 op n2). The
advantage of this macro is that it can produce a message that includes the actual values of n1 and n2.
n1 : an unsigned integer
cmp : The comparison operator to use. One of ==, !=, <, >, <=, >=.
Since 2.16
446
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
g_assert_cmphex()
Debugging macro to terminate the application with a warning message if an unsigned integer com-
parison fails. This is a variant of g_assert_cmpuint() that displays the numbers in hexadecimal notation
in the message.
n1 : an unsigned integer
cmp : The comparison operator to use. One of ==, !=, <, >, <=, >=.
Since 2.16
g_assert_cmpfloat()
#define g_assert_cmpfloat(n1,cmp,n2)
Debugging macro to terminate the application with a warning message if a floating point number
comparison fails.
The effect of g_assert_cmpfloat (n1, op, n2) is the same as g_assert (n1 op n2). The
advantage of this function is that it can produce a message that includes the actual values of n1 and n2.
n1 : an floating point number
cmp : The comparison operator to use. One of ==, !=, <, >, <=, >=.
Since 2.16
g_assert_no_error()
#define g_assert_no_error(err)
Debugging macro to terminate the application with a warning message if a method has returned a
GError.
The effect of g_assert_no_error (err) is the same as g_assert (err == NULL). The ad-
vantage of this macro is that it can produce a message that includes the error message and code.
err : a GError, possibly NULL
Since 2.20
g_assert_error()
Debugging macro to terminate the application with a warning message if a method has not returned
the correct GError.
The effect of g_assert_error (err, dom, c) is the same as g_assert (err != NULL &&
err->domain == dom && err->code == c). The advantage of this macro is that it can produce a
message that includes the incorrect error message and code.
This can only be used to test for a specific error. If you want to test that err is set, but don’t care
what it’s set to, just use g_assert (err != NULL)
err : a GError, possibly NULL
Since 2.20
447
CHAPTER 4. GLIB UTILITIES 4.24. TESTING
GTestCase
GTestSuite
g_test_create_case ()
Create a new GTestCase, named test_name, this API is fairly low level, calling g_test_add() or
g_test_add_func() is preferable. When this test is executed, a fixture structure of size data_size will
be allocated and filled with 0s. Then data_setup() is called to initialize the fixture. After fixture setup,
the actual test function data_test() is called. Once the test run completed, the fixture structure is torn
down by calling data_teardown() and after that the memory is released.
Splitting up a test run into fixture setup, test function and fixture teardown is most usful if the same
fixture is used for multiple tests. In this cases, g_test_create_case() will be called with the same fixture,
but varying test_name and data_test arguments.
Since 2.16
g_test_create_suite ()
Since 2.16
448
CHAPTER 4. GLIB UTILITIES 4.25. WINDOWS COMPATIBILITY FUNCTIONS
g_test_get_root ()
Get the toplevel test suite for the test path API.
Since 2.16
g_test_suite_add ()
suite : a GTestSuite
test_case : a GTestCase
Since 2.16
g_test_suite_add_suite ()
suite : a GTestSuite
Since 2.16
g_test_run_suite ()
Execute the tests within suite and all nested GTestSuites. The test suites to be executed are fil-
tered according to test path arguments (-p testpath) as parsed by g_test_init(). g_test_run_suite() or
g_test_run() may only be called once in a program.
suite : a GTestSuite
Returns : 0 on success
Since 2.16
See Also
gtester, gtester-report
449
CHAPTER 4. GLIB UTILITIES 4.25. WINDOWS COMPATIBILITY FUNCTIONS
Synopsis
#include <glib.h>
#define MAXPATHLEN
gchar* g_win32_error_message (gint error);
gchar* g_win32_getlocale (void);
gchar* g_win32_get_package_installation_directory
(const gchar *package,
const gchar *dll_name);
gchar* g_win32_get_package_installation_directory_of_module
(gpointer hmodule);
gchar* g_win32_get_package_installation_subdirectory
(const gchar *package,
const gchar *dll_name,
const gchar *subdir);
guint g_win32_get_windows_version (void);
gchar* g_win32_locale_filename_from_utf8 (const gchar *utf8filename);
#define G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
#define G_WIN32_HAVE_WIDECHAR_API ()
#define G_WIN32_IS_NT_BASED ()
Description
These functions provide some level of UNIX emulation on the Windows platform. If your application
really needs the POSIX APIs, we suggest you try the Cygwin project.
Details
MAXPATHLEN
Provided for UNIX emulation on Windows; equivalent to UNIX macro MAXPATHLEN, which is the
maximum length of a filename (including full path).
g_win32_error_message ()
Translate a Win32 error code (as returned by GetLastError()) into the corresponding message. The
message is either language neutral, or in the thread’s language, or the user’s language, the system’s
language, or US English (see docs for FormatMessage()). The returned string is in UTF-8. It should be
deallocated with g_free().
g_win32_getlocale ()
The setlocale() function in the Microsoft C library uses locale names of the form "English_United
States.1252" etc. We want the UNIXish standard form "en_US", "zh_TW" etc. This function gets the
current thread locale from Windows - without any encoding info - and returns it as a string of the above
form for use in forming file names etc. The returned string should be deallocated with g_free().
450
CHAPTER 4. GLIB UTILITIES 4.25. WINDOWS COMPATIBILITY FUNCTIONS
g_win32_get_package_installation_directory ()
gchar* g_win32_get_package_installation_directory
(const gchar *package,
const gchar *dll_name);
WARNING
Returns : a string containing the installation directory for package. The string is in the GLib file name
encoding, i.e. UTF-8. The return value should be freed with g_free() when not needed any longer.
If the function fails NULL is returned. Deprecated :2.18: Pass the HMODULE of a DLL or EXE to
g_win32_get_package_installation_directory_of_module() instead.
g_win32_get_package_installation_directory_of_module ()
gchar* g_win32_get_package_installation_directory_of_module
(gpointer hmodule);
This function tries to determine the installation directory of a software package based on the location
of a DLL of the software package.
hmodule should be the handle of a loaded DLL or NULL. The function looks up the directory that
DLL was loaded from. If hmodule is NULL, the directory the main executable of the current process is
looked up. If that directory’s last component is "bin" or "lib", its parent directory is returned, otherwise
the directory itself.
It thus makes sense to pass only the handle to a "public" DLL of a software package to this function, as
such DLLs typically are known to be installed in a "bin" or occasionally "lib" subfolder of the installation
folder. DLLs that are of the dynamically loaded module or plugin variety are often located in more
451
CHAPTER 4. GLIB UTILITIES 4.25. WINDOWS COMPATIBILITY FUNCTIONS
private locations deeper down in the tree, from which it is impossible for GLib to deduce the root of the
package installation.
The typical use case for this function is to have a DllMain() that saves the handle for the DLL. Then
when code in the DLL needs to construct names of files in the installation tree it calls this function
passing the DLL handle.
hmodule : The Win32 handle for a DLL loaded into the current process, or NULL
Returns : a string containing the guessed installation directory for the software package hmodule is
from. The string is in the GLib file name encoding, i.e. UTF-8. The return value should be freed
with g_free() when not needed any longer. If the function fails NULL is returned.
Since 2.16
g_win32_get_package_installation_subdirectory ()
gchar* g_win32_get_package_installation_subdirectory
(const gchar *package,
const gchar *dll_name,
const gchar *subdir);
WARNING
Returns : a string containing the complete path to subdir inside the installation directory of package.
The returned string is in the GLib file name encoding, i.e. UTF-8. The return value should be freed
with g_free() when no longer needed. If something goes wrong, NULL is returned. Deprecate-
d :2.18: Pass the HMODULE of a DLL or EXE to g_win32_get_package_installation_directory_of_module()
instead, and then construct a subdirectory pathname with g_build_filename().
g_win32_get_windows_version ()
Returns version information for the Windows operating system the code is running on. See MSDN
documentation for the GetVersion() function. To summarize, the most significant bit is one on Win9x,
and zero on NT-based systems. Since version 2.14, GLib works only on NT-based systems, so checking
whether your are running on Win9x in your own software is moot. The least significant byte is 4 on Win-
dows NT 4, and 5 on Windows XP. Software that needs really detailled version and feature information
should use Win32 API like GetVersionEx() and VerifyVersionInfo().
Returns : The version information.
Since 2.6
452
CHAPTER 4. GLIB UTILITIES 4.25. WINDOWS COMPATIBILITY FUNCTIONS
g_win32_locale_filename_from_utf8 ()
Returns : The converted filename, or NULL on conversion failure and lack of short names.
Since 2.8
G_WIN32_DLLMAIN_FOR_DLL_NAME()
WARNING
On Windows, this macro defines a DllMain() function that stores the actual DLL name that the code
being compiled will be included in.
On non-Windows platforms, expands to nothing.
dll_name : the name of the (pointer to the) char array where the DLL name will be stored. If this is
used, you must also include windows.h. If you need a more complex DLL entry point function,
you cannot use this.
G_WIN32_HAVE_WIDECHAR_API()
On Windows, this macro defines an expression which evaluates to TRUE if the code is running on
a version of Windows where the wide character versions of the Win32 API functions, and the wide
chaacter versions of the C library functions work. (They are always present in the DLLs, but don’t work
on Windows 9x and Me.)
On non-Windows platforms, it is not defined.
Since 2.6
453
CHAPTER 4. GLIB UTILITIES 4.25. WINDOWS COMPATIBILITY FUNCTIONS
G_WIN32_IS_NT_BASED()
On Windows, this macro defines an expression which evaluates to TRUE if the code is running on
an NT-based Windows operating system.
On non-Windows platforms, it is not defined.
Since 2.6
454
Chapter 5
Synopsis
#include <glib.h>
Description
Memory slices provide a space-efficient and multi-processing scalable way to allocate equal-sized pieces
of memory, just like the original GMemChunks (from GLib <= 2.8), while avoiding their excessive
memory-waste, scalability and performance problems.
To achieve these goals, the slice allocator uses a sophisticated, layered design that has been inspired
by Bonwick’s slab allocator 1 . It uses posix_memalign() to optimize allocations of many equally-sized
chunks, and has per-thread free lists (the so-called magazine layer) to quickly satisfy allocation requests
of already known structure sizes. This is accompanied by extra caching logic to keep freed memory
around for some time before returning it to the system. Memory that is unused due to alignment con-
straints is used for cache colorization (random distribution of chunk addresses) to improve CPU cache
utilization. The caching layer of the slice allocator adapts itself to high lock contention to improve scal-
ability.
1 [Bonwick94] Jeff Bonwick, The slab allocator: An object-caching kernel memory allocator. USENIX 1994, and [Bonwick01]
Bonwick and Jonathan Adams, Magazines and vmem: Extending the slab allocator to many cpu’s and arbitrary resources.
USENIX 2001
455
CHAPTER 5. GLIB DATA TYPES 5.1. MEMORY SLICES
The slice allocator can allocate blocks as small as two pointers, and unlike malloc(), it does not reserve
extra space per block. For large block sizes, g_slice_new() and g_slice_alloc() will automatically delegate
to the system malloc() implementation. For newly written code it is recommended to use the new g_s-
lice API instead of g_malloc() and friends, as long as objects are not resized during their lifetime and
the object size used at allocation time is still available when freeing.
Details
g_slice_alloc ()
Allocates a block of memory from the slice allocator. The block adress handed out can be expected
to be aligned to at least 1 * sizeof (void*), though in general slices are 2 * sizeof (void*) bytes
aligned, if a malloc() fallback implementation is used instead, the alignment may be reduced in a
libc dependent fashion. Note that the underlying slice allocation mechanism can be changed with the
G_SLICE=always-malloc environment variable.
Since 2.10
g_slice_alloc0 ()
456
CHAPTER 5. GLIB DATA TYPES 5.1. MEMORY SLICES
Allocates a block of memory via g_slice_alloc() and initialize the returned memory to 0. Note that the
underlying slice allocation mechanism can be changed with the G_SLICE=always-malloc environment
variable.
block_size : the number of bytes to allocate
g_slice_copy ()
Allocates a block of memory from the slice allocator and copies block_size bytes into it from mem-
_block .
g_slice_free1 ()
Frees a block of memory. The memory must have been allocated via g_slice_alloc() or g_slice_alloc0()
and the block_size has to match the size specified upon allocation. Note that the exact release be-
haviour can be changed with the G_DEBUG=gc-friendly environment variable, also see G_SLICE for
related debugging options.
block_size : the size of the block
Since 2.10
g_slice_free_chain_with_offset ()
Frees a linked list of memory blocks of structure type type. The memory blocks must be equal-
sized, allocated via g_slice_alloc() or g_slice_alloc0() and linked together by a next pointer (similar to
GSList). The offset of the next field in each block is passed as third argument. Note that the exact release
behaviour can be changed with the G_DEBUG=gc-friendly environment variable, also see G_SLICE for
related debugging options.
block_size : the size of the blocks
Since 2.10
457
CHAPTER 5. GLIB DATA TYPES 5.1. MEMORY SLICES
g_slice_new()
#define g_slice_new(type)
A convenience macro to allocate a block of memory from the slice allocator. It calls g_slice_alloc()
with sizeof (type) and casts the returned pointer to a pointer of the given type, avoiding a type
cast in the source code. Note that the underlying slice allocation mechanism can be changed with the
G_SLICE=always-malloc environment variable.
Since 2.10
g_slice_new0()
#define g_slice_new0(type)
A convenience macro to allocate a block of memory from the slice allocator and set the memory to
0. It calls g_slice_alloc0() with sizeof (type) and casts the returned pointer to a pointer of the given
type, avoiding a type cast in the source code. Note that the underlying slice allocation mechanism can
be changed with the G_SLICE=always-malloc environment variable.
Since 2.10
g_slice_dup()
A convenience macro to duplicate a block of memory using the slice allocator. It calls g_slice_copy()
with sizeof (type) and casts the returned pointer to a pointer of the given type, avoiding a type
cast in the source code. Note that the underlying slice allocation mechanism can be changed with the
G_SLICE=always-malloc environment variable.
Since 2.14
g_slice_free()
A convenience macro to free a block of memory that has been allocated from the slice allocator. It
calls g_slice_free1() using sizeof (type) as the block size. Note that the exact release behaviour
can be changed with the G_DEBUG=gc-friendly environment variable, also see G_SLICE for related
debugging options.
Since 2.10
458
CHAPTER 5. GLIB DATA TYPES 5.2. MEMORY CHUNKS
g_slice_free_chain()
Frees a linked list of memory blocks of structure type type. The memory blocks must be equal-sized,
allocated via g_slice_alloc() or g_slice_alloc0() and linked together by a next pointer (similar to GSList).
The name of the next field in type is passed as third argument. Note that the exact release behaviour
can be changed with the G_DEBUG=gc-friendly environment variable, also see G_SLICE for related
debugging options.
Since 2.10
Synopsis
#include <glib.h>
GMemChunk;
#define G_ALLOC_AND_FREE
#define G_ALLOC_ONLY
Description
Memory chunks provide an space-efficient way to allocate equal-sized pieces of memory, called atoms.
However, due to the administrative overhead (in particular for G_ALLOC_AND_FREE, and when used
459
CHAPTER 5. GLIB DATA TYPES 5.2. MEMORY CHUNKS
from multiple threads), they are in practise often slower than direct use of g_malloc(). Therefore, mem-
ory chunks have been deprecated in favor of the slice allocator, which has been added in 2.10. All
internal uses of memory chunks in GLib have been converted to the g_slice API.
• G_ALLOC_ONLY chunks only allow allocation of atoms. The atoms can never be freed individu-
ally. The memory chunk can only be free in its entirety.
To free an atom use g_mem_chunk_free(), or the convenience macro g_chunk_free(). (Atoms can
only be freed if the memory chunk is created with the type set to G_ALLOC_AND_FREE.)
To free any blocks of memory which are no longer being used, use g_mem_chunk_clean(). To clean
all memory chunks, use g_blow_chunks().
To reset the memory chunk, freeing all of the atoms, use g_mem_chunk_reset().
460
CHAPTER 5. GLIB DATA TYPES 5.2. MEMORY CHUNKS
Details
GMemChunk
WARNING
The GMemChunk struct is an opaque data structure representing a memory chunk. It should be
accessed only through the use of the following functions.
G_ALLOC_AND_FREE
#define G_ALLOC_AND_FREE 2
WARNING
G_ALLOC_ONLY
#define G_ALLOC_ONLY 1
461
CHAPTER 5. GLIB DATA TYPES 5.2. MEMORY CHUNKS
WARNING
g_mem_chunk_new ()
WARNING
g_mem_chunk_new has been deprecated since version 2.10 and should not be used
in newly-written code. Use the slice allocator instead
name : a string to identify the GMemChunk. It is not copied so it should be valid for the lifetime of the
GMemChunk. It is only used in g_mem_chunk_print(), which is used for debugging.
area_size : the size, in bytes, of each block of memory allocated to contain the atoms.
type : the type of the GMemChunk. G_ALLOC_AND_FREE is used if the atoms will be freed individu-
ally. G_ALLOC_ONLY should be used if atoms will never be freed individually. G_ALLOC_ONLY
is quicker, since it does not need to track free atoms, but it obviously wastes memory if you no
longer need many of the atoms.
g_mem_chunk_alloc ()
WARNING
g_mem_chunk_alloc has been deprecated since version 2.10 and should not be
used in newly-written code. Use g_slice_alloc() instead
mem_chunk : a GMemChunk.
462
CHAPTER 5. GLIB DATA TYPES 5.2. MEMORY CHUNKS
g_mem_chunk_alloc0 ()
WARNING
g_mem_chunk_alloc0 has been deprecated since version 2.10 and should not be
used in newly-written code. Use g_slice_alloc0() instead
mem_chunk : a GMemChunk.
g_mem_chunk_free ()
WARNING
g_mem_chunk_free has been deprecated since version 2.10 and should not be used
in newly-written code. Use g_slice_free1() instead
Frees an atom in a GMemChunk. This should only be called if the GMemChunk was created with
G_ALLOC_AND_FREE. Otherwise it will simply return.
mem_chunk : a GMemChunk.
g_mem_chunk_destroy ()
WARNING
g_mem_chunk_destroy has been deprecated since version 2.10 and should not be
used in newly-written code. Use the slice allocator instead
mem_chunk : a GMemChunk.
463
CHAPTER 5. GLIB DATA TYPES 5.2. MEMORY CHUNKS
g_mem_chunk_create()
WARNING
g_mem_chunk_create has been deprecated since version 2.10 and should not be
used in newly-written code. Use the slice allocator instead
A convenience macro for creating a new GMemChunk. It calls g_mem_chunk_new(), using the given
type to create the GMemChunk name. The atom size is determined using sizeof(), and the area size
is calculated by multiplying the pre_alloc parameter with the atom size.
type : the type of the atoms, typically a structure name.
alloc_type : the type of the GMemChunk. G_ALLOC_AND_FREE is used if the atoms will be freed in-
dividually. G_ALLOC_ONLY should be used if atoms will never be freed individually. G_ALLOC_ONLY
is quicker, since it does not need to track free atoms, but it obviously wastes memory if you no
longer need many of the atoms.
Returns : the new GMemChunk.
g_chunk_new()
WARNING
g_chunk_new has been deprecated since version 2.10 and should not be used in
newly-written code. Use g_slice_new() instead
chunk : a GMemChunk.
g_chunk_new0()
WARNING
g_chunk_new0 has been deprecated since version 2.10 and should not be used in
newly-written code. Use g_slice_new0() instead
464
CHAPTER 5. GLIB DATA TYPES 5.2. MEMORY CHUNKS
chunk : a GMemChunk.
g_chunk_free()
WARNING
g_chunk_free has been deprecated since version 2.10 and should not be used in
newly-written code. Use g_slice_free() instead
A convenience macro to free an atom of memory from a GMemChunk. It simply switches the ar-
guments and calls g_mem_chunk_free() It is included simply to complement the other convenience
macros, g_chunk_new() and g_chunk_new0().
mem : a pointer to the atom to be freed.
mem_chunk : a GMemChunk.
g_mem_chunk_reset ()
WARNING
g_mem_chunk_reset has been deprecated since version 2.10 and should not be
used in newly-written code. Use the slice allocator instead
Resets a GMemChunk to its initial state. It frees all of the currently allocated blocks of memory.
mem_chunk : a GMemChunk.
g_mem_chunk_clean ()
WARNING
g_mem_chunk_clean has been deprecated since version 2.10 and should not be
used in newly-written code. Use the slice allocator instead
465
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_blow_chunks ()
WARNING
g_blow_chunks has been deprecated since version 2.10 and should not be used in
newly-written code. Use the slice allocator instead
g_mem_chunk_info ()
WARNING
g_mem_chunk_info has been deprecated since version 2.10 and should not be used
in newly-written code. Use the slice allocator instead
Outputs debugging information for all GMemChunk objects currently in use. It outputs the number
of GMemChunk objects currently allocated, and calls g_mem_chunk_print() to output information on
each one.
g_mem_chunk_print ()
WARNING
g_mem_chunk_print has been deprecated since version 2.10 and should not be
used in newly-written code. Use the slice allocator instead
Outputs debugging information for a GMemChunk. It outputs the name of the GMemChunk (set
with g_mem_chunk_new()), the number of bytes used, and the number of blocks of memory allocated.
mem_chunk : a GMemChunk.
466
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
Synopsis
#include <glib.h>
GList;
467
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
Description
The GList structure and its associated functions provide a standard doubly-linked list data structure.
Each element in the list contains a piece of data, together with pointers which link to the previous and
next elements in the list. Using these pointers it is possible to move through the list in both directions
(unlike the Singly-Linked Lists which only allows movement through the list in the forward direction).
The data contained in each element can be either integer values, by using one of the Type Conversion
Macros, or simply pointers to any type of data.
List elements are allocated from the slice allocator, which is more efficient than allocating elements
individually.
Note that most of the GList functions expect to be passed a pointer to the first element in the list. The
functions which insert elements return the new start of the list, which may have changed.
There is no function to create a GList. NULL is considered to be the empty list so you simply set a
GList* to NULL.
To add elements, use g_list_append(), g_list_prepend(), g_list_insert() and g_list_insert_sorted().
To remove elements, use g_list_remove().
To find elements in the list use g_list_first(), g_list_last(), g_list_next(), g_list_previous(), g_list_nth(),
g_list_nth_data(), g_list_find() and g_list_find_custom().
To find the index of an element use g_list_position() and g_list_index().
To call a function for each element in the list use g_list_foreach().
To free the entire list, use g_list_free().
Details
GList
typedef struct {
gpointer data;
GList *next;
GList *prev;
} GList;
468
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
GList *next; contains the link to the next element in the list.
GList *prev ; contains the link to the previous element in the list.
g_list_append ()
N OTE
The return value is the new start of the list, which may have changed, so make sure you
store the new value.
N OTE
Note that g_list_append() has to traverse the entire list to find the end, which is inefficient
when adding multiple elements. A common idiom to avoid the inefficiency is to prepend
the elements and reverse the list when all elements have been added.
g_list_prepend ()
N OTE
The return value is the new start of the list, which may have changed, so make sure you
store the new value.
469
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_list_insert ()
position : the position to insert the element. If this is negative, or is larger than the number of elements
in the list, the new element is added on to the end of the list.
g_list_insert_before ()
Inserts a new element into the list before the given position.
sibling : the list element before which the new element is inserted or NULL to insert at the end of the
list
g_list_insert_sorted ()
Inserts a new element into the list, using the given comparison function to determine its position.
func : the function to compare elements in the list. It should return a number > 0 if the first parameter
comes after the second parameter in the sort order.
470
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_list_remove ()
Removes an element from a GList. If two elements contain the same data, only the first is removed.
If none of the elements contain the data, the GList is unchanged.
list : a GList
g_list_remove_link ()
Removes an element from a GList, without freeing the element. The removed element’s prev and
next links are set to NULL, so that it becomes a self-contained list with one element.
list : a GList
g_list_delete_link ()
Removes the node link_ from the list and frees it. Compare this to g_list_remove_link() which re-
moves the node without freeing it.
list : a GList
g_list_remove_all ()
Removes all list nodes with data equal to data. Returns the new head of the list. Contrast with
g_list_remove() which removes only the first node matching the given data.
list : a GList
471
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_list_free ()
Frees all of the memory used by a GList. The freed elements are returned to the slice allocator.
N OTE
list : a GList
g_list_alloc ()
Allocates space for one GList element. It is called by g_list_append(), g_list_prepend(), g_list_insert()
and g_list_insert_sorted() and so is rarely used on its own.
g_list_free_1 ()
g_list_free1
#define g_list_free1
g_list_length ()
N OTE
This function iterates over the whole list to count its elements.
list : a GList
472
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_list_copy ()
Copies a GList.
N OTE
Note that this is a "shallow" copy. If the list elements consist of pointers to data, the
pointers are copied but the actual data is not.
list : a GList
g_list_reverse ()
Reverses a GList. It simply switches the next and prev pointers of each element.
list : a GList
g_list_sort ()
list : a GList
compare_func : the comparison function used to sort the GList. This function is passed the data from
2 elements of the GList and should return 0 if they are equal, a negative value if the first element
comes before the second, or a positive value if the first element comes after the second.
GCompareFunc ()
Specifies the type of a comparison function used to compare two values. The function should return
a negative integer if the first value comes before the second, 0 if they are equal, or a positive integer if
the first value comes after the second.
a : a value.
473
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_list_insert_sorted_with_data ()
Inserts a new element into the list, using the given comparison function to determine its position.
list : a pointer to a GList
func : the function to compare elements in the list. It should return a number > 0 if the first parameter
comes after the second parameter in the sort order.
user_data : user data to pass to comparison function.
g_list_sort_with_data ()
Like g_list_sort(), but the comparison function accepts a user data argument.
list : a GList
GCompareDataFunc ()
Specifies the type of a comparison function used to compare two values. The function should return
a negative integer if the first value comes before the second, 0 if they are equal, or a positive integer if
the first value comes after the second.
a : a value.
g_list_concat ()
Adds the second GList onto the end of the first GList. Note that the elements of the second GList are
not copied. They are used directly.
list1 : a GList
474
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_list_foreach ()
GFunc ()
g_list_first ()
Returns : the first element in the GList, or NULL if the GList has no elements
g_list_last ()
Returns : the last element in the GList, or NULL if the GList has no elements
g_list_previous()
#define g_list_previous(list)
g_list_next()
#define g_list_next(list)
475
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
g_list_nth ()
Returns : the element, or NULL if the position is off the end of the GList
g_list_nth_data ()
Returns : the element’s data, or NULL if the position is off the end of the GList
g_list_nth_prev ()
Returns : the element, or NULL if the position is off the end of the GList
g_list_find ()
g_list_find_custom ()
Finds an element in a GList, using a supplied function to find the desired element. It iterates over the
list, calling the given function which should return 0 when the desired element is found. The function
takes two gconstpointer arguments, the GList element’s data as the first argument and the given user
data.
list : a GList
476
CHAPTER 5. GLIB DATA TYPES 5.3. DOUBLY-LINKED LISTS
func : the function to call for each element. It should return 0 when the desired element is found
g_list_position ()
Gets the position of the given element in the GList (starting from 0).
list : a GList
Returns : the position of the element in the GList, or -1 if the element is not found
g_list_index ()
Gets the position of the element containing the given data (starting from 0).
list : a GList
Returns : the index of the element containing the data, or -1 if the data is not found
g_list_push_allocator ()
WARNING
g_list_push_allocator has been deprecated since version 2.10 and should not
be used in newly-written code. It does nothing, since GList has been converted to the
slice allocator
Sets the allocator to use to allocate GList elements. Use g_list_pop_allocator() to restore the previous
allocator.
Note that this function is not available if GLib has been compiled with --disable-mem-pools
allocator : the GAllocator to use when allocating GList elements.
g_list_pop_allocator ()
WARNING
g_list_pop_allocator has been deprecated since version 2.10 and should not be
used in newly-written code. It does nothing, since GList has been converted to the slice
allocator
477
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
Synopsis
#include <glib.h>
GSList;
478
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
GFunc func,
gpointer user_data);
Description
The GSList structure and its associated functions provide a standard singly-linked list data structure.
Each element in the list contains a piece of data, together with a pointer which links to the next
element in the list. Using this pointer it is possible to move through the list in one direction only (unlike
the Doubly-Linked Lists which allow movement in both directions).
The data contained in each element can be either integer values, by using one of the Type Conversion
Macros, or simply pointers to any type of data.
List elements are allocated from the slice allocator, which is more efficient than allocating elements
individually.
Note that most of the GSList functions expect to be passed a pointer to the first element in the list.
The functions which insert elements return the new start of the list, which may have changed.
There is no function to create a GSList. NULL is considered to be the empty list so you simply set a
GSList* to NULL.
To add elements, use g_slist_append(), g_slist_prepend(), g_slist_insert() and g_slist_insert_sorted().
To remove elements, use g_slist_remove().
To find elements in the list use g_slist_last(), g_slist_next(), g_slist_nth(), g_slist_nth_data(), g_slist_find()
and g_slist_find_custom().
To find the index of an element use g_slist_position() and g_slist_index().
To call a function for each element in the list use g_slist_foreach().
To free the entire list, use g_slist_free().
Details
GSList
typedef struct {
gpointer data;
GSList *next;
} GSList;
The GSList struct is used for each element in the singly-linked list.
gpointer data; holds the element’s data, which can be a pointer to any kind of data, or any integer
value using the Type Conversion Macros.
GSList *next; contains the link to the next element in the list.
479
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
g_slist_alloc ()
Allocates space for one GSList element. It is called by the g_slist_append(), g_slist_prepend(), g_slist_insert()
and g_slist_insert_sorted() functions and so is rarely used on its own.
g_slist_append ()
N OTE
The return value is the new start of the list, which may have changed, so make sure you
store the new value.
N OTE
Note that g_slist_append() has to traverse the entire list to find the end, which is inefficient
when adding multiple elements. A common idiom to avoid the inefficiency is to prepend
the elements and reverse the list when all elements have been added.
list : a GSList
g_slist_prepend ()
480
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
N OTE
The return value is the new start of the list, which may have changed, so make sure you
store the new value.
list : a GSList
g_slist_insert ()
list : a GSList
position : the position to insert the element. If this is negative, or is larger than the number of elements
in the list, the new element is added on to the end of the list.
g_slist_insert_before ()
slist : a GSList
g_slist_insert_sorted ()
Inserts a new element into the list, using the given comparison function to determine its position.
list : a GSList
481
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
func : the function to compare elements in the list. It should return a number > 0 if the first parameter
comes after the second parameter in the sort order.
g_slist_remove ()
Removes an element from a GSList. If two elements contain the same data, only the first is removed.
If none of the elements contain the data, the GSList is unchanged.
list : a GSList
g_slist_remove_link ()
Removes an element from a GSList, without freeing the element. The removed element’s next link is
set to NULL, so that it becomes a self-contained list with one element.
list : a GSList
g_slist_delete_link ()
Removes the node link_ from the list and frees it. Compare this to g_slist_remove_link() which
removes the node without freeing it.
list : a GSList
g_slist_remove_all ()
Removes all list nodes with data equal to data. Returns the new head of the list. Contrast with
g_slist_remove() which removes only the first node matching the given data.
list : a GSList
482
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
g_slist_free ()
Frees all of the memory used by a GSList. The freed elements are returned to the slice allocator.
list : a GSList
g_slist_free_1 ()
g_slist_free1
#define g_slist_free1
g_slist_length ()
N OTE
This function iterates over the whole list to count its elements.
list : a GSList
g_slist_copy ()
Copies a GSList.
N OTE
Note that this is a "shallow" copy. If the list elements consist of pointers to data, the
pointers are copied but the actual data isn’t.
list : a GSList
483
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
g_slist_reverse ()
Reverses a GSList.
list : a GSList
g_slist_insert_sorted_with_data ()
Inserts a new element into the list, using the given comparison function to determine its position.
list : a GSList
func : the function to compare elements in the list. It should return a number > 0 if the first parameter
comes after the second parameter in the sort order.
Since 2.10
g_slist_sort ()
list : a GSList
compare_func : the comparison function used to sort the GSList. This function is passed the data from
2 elements of the GSList and should return 0 if they are equal, a negative value if the first element
comes before the second, or a positive value if the first element comes after the second.
g_slist_sort_with_data ()
Like g_slist_sort(), but the sort function accepts a user data argument.
list : a GSList
484
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
g_slist_concat ()
Adds the second GSList onto the end of the first GSList. Note that the elements of the second GSList
are not copied. They are used directly.
list1 : a GSList
g_slist_foreach ()
g_slist_last ()
N OTE
list : a GSList
Returns : the last element in the GSList, or NULL if the GSList has no elements
g_slist_next()
#define g_slist_next(slist)
g_slist_nth ()
Returns : the element, or NULL if the position is off the end of the GSList
485
CHAPTER 5. GLIB DATA TYPES 5.4. SINGLY-LINKED LISTS
g_slist_nth_data ()
list : a GSList
Returns : the element’s data, or NULL if the position is off the end of the GSList
g_slist_find ()
list : a GSList
g_slist_find_custom ()
Finds an element in a GSList, using a supplied function to find the desired element. It iterates over
the list, calling the given function which should return 0 when the desired element is found. The func-
tion takes two gconstpointer arguments, the GSList element’s data as the first argument and the given
user data.
list : a GSList
func : the function to call for each element. It should return 0 when the desired element is found
g_slist_position ()
Gets the position of the given element in the GSList (starting from 0).
list : a GSList
Returns : the position of the element in the GSList, or -1 if the element is not found
486
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_slist_index ()
Gets the position of the element containing the given data (starting from 0).
list : a GSList
Returns : the index of the element containing the data, or -1 if the data is not found
g_slist_push_allocator ()
WARNING
g_slist_push_allocator has been deprecated since version 2.10 and should not
be used in newly-written code. It does nothing, since GSList has been converted to the
slice allocator
Sets the allocator to use to allocate GSList elements. Use g_slist_pop_allocator() to restore the previ-
ous allocator.
Note that this function is not available if GLib has been compiled with --disable-mem-pools
g_slist_pop_allocator ()
WARNING
g_slist_pop_allocator has been deprecated since version 2.10 and should not
be used in newly-written code. It does nothing, since GSList has been converted to the
slice allocator
487
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
Synopsis
#include <glib.h>
GQueue;
GQueue* g_queue_new (void);
void g_queue_free (GQueue *queue);
#define G_QUEUE_INIT
void g_queue_init (GQueue *queue);
void g_queue_clear (GQueue *queue);
gboolean g_queue_is_empty (GQueue *queue);
guint g_queue_get_length (GQueue *queue);
void g_queue_reverse (GQueue *queue);
GQueue * g_queue_copy (GQueue *queue);
void g_queue_foreach (GQueue *queue,
GFunc func,
gpointer user_data);
GList * g_queue_find (GQueue *queue,
gconstpointer data);
GList * g_queue_find_custom (GQueue *queue,
gconstpointer data,
GCompareFunc func);
void g_queue_sort (GQueue *queue,
GCompareDataFunc compare_func,
gpointer user_data);
void g_queue_push_head (GQueue *queue,
gpointer data);
void g_queue_push_tail (GQueue *queue,
gpointer data);
void g_queue_push_nth (GQueue *queue,
gpointer data,
gint n);
gpointer g_queue_pop_head (GQueue *queue);
gpointer g_queue_pop_tail (GQueue *queue);
gpointer g_queue_pop_nth (GQueue *queue,
guint n);
gpointer g_queue_peek_head (GQueue *queue);
gpointer g_queue_peek_tail (GQueue *queue);
gpointer g_queue_peek_nth (GQueue *queue,
guint n);
gint g_queue_index (GQueue *queue,
gconstpointer data);
void g_queue_remove (GQueue *queue,
gconstpointer data);
void g_queue_remove_all (GQueue *queue,
gconstpointer data);
void g_queue_insert_before (GQueue *queue,
GList *sibling,
gpointer data);
void g_queue_insert_after (GQueue *queue,
GList *sibling,
gpointer data);
void g_queue_insert_sorted (GQueue *queue,
gpointer data,
GCompareDataFunc func,
gpointer user_data);
void g_queue_push_head_link (GQueue *queue,
GList *link_);
488
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
Description
The GQueue structure and its associated functions provide a standard queue data structure. Internally,
GQueue uses the same data structure as GList to store elements.
The data contained in each element can be either integer values, by using one of the Type Conversion
Macros, or simply pointers to any type of data.
To create a new GQueue, use g_queue_new().
To initialize a statically-allocated GQueue, use G_QUEUE_INIT or g_queue_init().
To add elements, use g_queue_push_head(), g_queue_push_head_link(), g_queue_push_tail() and
g_queue_push_tail_link().
To remove elements, use g_queue_pop_head() and g_queue_pop_tail().
To free the entire queue, use g_queue_free().
Details
GQueue
typedef struct {
GList *head;
GList *tail;
guint length;
} GQueue;
g_queue_new ()
489
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_queue_free ()
Frees the memory allocated for the GQueue. Only call this function if queue was created with
g_queue_new(). If queue elements contain dynamically-allocated memory, they should be freed first.
queue : a GQueue.
G_QUEUE_INIT
A statically-allocated GQueue must be initialized with this macro before it can be used. This macro
can be used to initialize a variable, but it cannot be assigned to a variable. In that case you have to use
g_queue_init().
GQueue my_queue = G_QUEUE_INIT;
Since 2.14
g_queue_init ()
A statically-allocated GQueue must be initialized with this function before it can be used. Alter-
natively you can initialize it with G_QUEUE_INIT. It is not necessary to initialize queues created with
g_queue_new().
queue : an uninitialized GQueue
Since 2.14
g_queue_clear ()
Removes all the elements in queue. If queue elements contain dynamically-allocated memory, they
should be freed first.
queue : a GQueue
Since 2.14
g_queue_is_empty ()
g_queue_get_length ()
490
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_queue_reverse ()
Since 2.4
g_queue_copy ()
Copies a queue. Note that is a shallow copy. If the elements in the queue consist of pointers to data,
the pointers are copied, but the actual data is not.
queue : a GQueue
g_queue_foreach ()
Calls func for each element in the queue passing user_data to the function.
queue : a GQueue
Since 2.4
g_queue_find ()
g_queue_find_custom ()
Finds an element in a GQueue, using a supplied function to find the desired element. It iterates over
the queue, calling the given function which should return 0 when the desired element is found. The
function takes two gconstpointer arguments, the GQueue element’s data as the first argument and the
given user data as the second argument.
491
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
queue : a GQueue
func : a GCompareFunc to call for each element. It should return 0 when the desired element is found
g_queue_sort ()
compare_func : the GCompareDataFunc used to sort queue. This function is passed two elements of
the queue and should return 0 if they are equal, a negative value if the first comes before the
second, and a positive value if the second comes before the first.
user_data : user data passed to compare_func
Since 2.4
g_queue_push_head ()
g_queue_push_tail ()
g_queue_push_nth ()
n : the position to insert the new element. If n is negative or larger than the number of elements in the
queue, the element is added to the end of the queue.
Since 2.4
492
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_queue_pop_head ()
Returns : the data of the first element in the queue, or NULL if the queue is empty.
g_queue_pop_tail ()
Returns : the data of the last element in the queue, or NULL if the queue is empty.
g_queue_pop_nth ()
g_queue_peek_head ()
Returns : the data of the first element in the queue, or NULL if the queue is empty.
g_queue_peek_tail ()
Returns : the data of the last element in the queue, or NULL if the queue is empty.
g_queue_peek_nth ()
Returns : The data for the n’th element of queue, or NULL if n is off the end of queue.
Since 2.4
493
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_queue_index ()
Returns the position of the first element in queue which contains data.
queue : a GQueue
Returns : The position of the first element in queue which contains data, or -1 if no element in queue
contains data.
Since 2.4
g_queue_remove ()
queue : a GQueue
Since 2.4
g_queue_remove_all ()
queue : a GQueue
Since 2.4
g_queue_insert_before ()
queue : a GQueue
Since 2.4
494
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_queue_insert_after ()
queue : a GQueue
Since 2.4
g_queue_insert_sorted ()
Inserts data into queue using func to determine the new position.
queue : a GQueue
func : the GCompareDataFunc used to compare elements in the queue. It is called with two elements
of the queue and user_data. It should return 0 if the elements are equal, a negative value if the
first element comes before the second, and a positive value if the second element comes before the
first.
Since 2.4
g_queue_push_head_link ()
queue : a GQueue.
link_ : a single GList element, not a list with more than one element.
g_queue_push_tail_link ()
queue : a GQueue.
link_ : a single GList element, not a list with more than one element.
495
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_queue_push_nth_link ()
queue : a GQueue
n : the position to insert the link. If this is negative or larger than the number of elements in queue, the
link is added to the end of queue.
Since 2.4
g_queue_pop_head_link ()
queue : a GQueue.
Returns : the GList element at the head of the queue, or NULL if the queue is empty.
g_queue_pop_tail_link ()
queue : a GQueue.
Returns : the GList element at the tail of the queue, or NULL if the queue is empty.
g_queue_pop_nth_link ()
queue : a GQueue
Since 2.4
g_queue_peek_head_link ()
queue : a GQueue
Since 2.4
496
CHAPTER 5. GLIB DATA TYPES 5.5. DOUBLE-ENDED QUEUES
g_queue_peek_tail_link ()
g_queue_peek_nth_link ()
Returns : The link at the n’th position, or NULL if n is off the end of the list
Since 2.4
g_queue_link_index ()
g_queue_unlink ()
Unlinks link_ so that it will no longer be part of queue. The link is not freed.
link_ must be part of queue,
queue : a GQueue
g_queue_delete_link ()
497
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
5.6 Sequences
Name
Sequences – scalable lists
Synopsis
#include <glib.h>
GSequence;
typedef GSequenceIter;
gint (*GSequenceIterCompareFunc) (GSequenceIter *a,
GSequenceIter *b,
gpointer data);
498
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
Description
The GSequence data structure has the API of a list, but is implemented internally with a balanced binary
tree. This means that it is possible to maintain a sorted list of n elements in time O(n log n). The data
contained in each element can be either integer values, by using of the Type Conversion Macros, or
simply pointers to any type of data.
A GSequence is accessed through iterators, represented by a GSequenceIter. An iterator represents
a position between two elements of the sequence. For example, the begin iterator represents the gap
immediately before the first element of the sequence, and the end iterator represents the gap immediately
after the last element. In an empty sequence, the begin and end iterators are the same.
Some methods on GSequence operate on ranges of items. For example g_sequence_foreach_range()
will call a user-specified function on each element with the given range. The range is delimited by the
gaps represented by the passed-in iterators, so if you pass in the begin and end iterators, the range in
question is the entire sequence.
The function g_sequence_get() is used with an iterator to access the element immediately following
the gap that the iterator represents. The iterator is said to point to that element.
Iterators are stable across most operations on a GSequence. For example an iterator pointing to
some element of a sequence will continue to point to that element even after the sequence is sorted.
Even moving an element to another sequence using for example g_sequence_move_range() will not
invalidate the iterators pointing to it. The only operation that will invalidate an iterator is when the
element it points to is removed from any sequence.
499
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
Details
GSequence
The GSequence struct is an opaque data type representing a Sequence data type.
GSequenceIter
The GSequenceIter struct is an opaque data type representing an iterator pointing into a GSequence.
GSequenceIterCompareFunc ()
b : a GSequenceIter
Returns : zero if the iterators are equal, a negative value if a comes before b, and a positive value if b
comes before a.
g_sequence_new ()
Creates a new GSequence. The data_destroy function, if non-NULL will be called on all items
when the sequence is destroyed and on items that are removed from the sequence.
data_destroy : a GDestroyNotify function, or NULL
g_sequence_free ()
Frees the memory allocated for seq . If seq has a data destroy function associated with it, that func-
tion is called on all items in seq .
seq : a GSequence
Since 2.14
g_sequence_get_length ()
500
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
g_sequence_foreach ()
Calls func for each item in the sequence passing user_data to the function.
seq : a GSequence
Since 2.14
g_sequence_foreach_range ()
Calls func for each item in the range (begin, end ) passing user_data to the function.
begin : a GSequenceIter
end : a GSequenceIter
func : a GFunc
Since 2.14
g_sequence_sort ()
cmp_func : the GCompareDataFunc used to sort seq . This function is passed two items of seq and
should return 0 if they are equal, a negative value fi the first comes before the second, and a
positive value if the second comes before the first.
cmp_data : user data passed to cmp_func
Since 2.14
g_sequence_sort_iter ()
501
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
cmp_func : the GSequenceItercompare used to compare iterators in the sequence. It is called with two
iterators pointing into seq . It should return 0 if the iterators are equal, a negative value if the first
iterator comes before the second, and a positive value if the second iterator comes before the first.
Since 2.14
g_sequence_get_begin_iter ()
seq : a GSequence
Since 2.14
g_sequence_get_end_iter ()
seq : a GSequence
Since 2.14
g_sequence_get_iter_at_pos ()
Returns the iterator at position pos. If pos is negative or larger than the number of items in seq , the
end iterator is returned.
seq : a GSequence
Since 2.14
g_sequence_append ()
seq : a GSequencePointer
Since 2.14
502
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
g_sequence_prepend ()
seq : a GSequence
Since 2.14
g_sequence_insert_before ()
iter : a GSequenceIter
Since 2.14
g_sequence_move ()
Moves the item pointed to by src to the position indicated by dest. After calling this function dest
will point to the position immediately after src. It is allowed for src and dest to point into different
sequences.
Since 2.14
g_sequence_swap ()
Swaps the items pointed to by a and b. It is allowed for a and b to point into difference sequences.
a : a GSequenceIter
b : a GSequenceIter
Since 2.14
503
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
g_sequence_insert_sorted ()
Inserts data into sequence using func to determine the new position. The sequence must already
be sorted according to cmp_func; otherwise the new position of data is undefined.
seq : a GSequence
cmp_func : the GCompareDataFunc used to compare items in the sequence. It is called with two items
of the seq and user_data. It should return 0 if the items are equal, a negative value if the first
item comes before the second, and a positive value if the second item comes before the first.
cmp_data : user data passed to cmp_func.
g_sequence_insert_sorted_iter ()
iter_cmp : the GSequenceItercompare used to compare iterators in the sequence. It is called with two
iterators pointing into seq . It should return 0 if the iterators are equal, a negative value if the first
iterator comes before the second, and a positive value if the second iterator comes before the first.
cmp_data : user data passed to cmp_func
g_sequence_sort_changed ()
Moves the data pointed to a new position as indicated by cmp_func. This function should be called
for items in a sequence already sorted according to cmp_func whenever some aspect of an item changes
so that cmp_func may return different values for that item.
iter : A GSequenceIter
cmp_func : the GCompareDataFunc used to compare items in the sequence. It is called with two items
of the seq and user_data. It should return 0 if the items are equal, a negative value if the first
item comes before the second, and a positive value if the second item comes before the first.
cmp_data : user data passed to cmp_func.
Since 2.14
504
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
g_sequence_sort_changed_iter ()
iter_cmp : the GSequenceItercompare used to compare iterators in the sequence. It is called with two
iterators pointing into seq . It should return 0 if the iterators are equal, a negative value if the first
iterator comes before the second, and a positive value if the second iterator comes before the first.
cmp_data : user data passed to cmp_func
Since 2.14
g_sequence_remove ()
Removes the item pointed to by iter . It is an error to pass the end iterator to this function.
If the sequnce has a data destroy function associated with it, this function is called on the data for
the removed item.
iter : a GSequenceIter
Since 2.14
g_sequence_remove_range ()
end : a GSequenceIter
Since 2.14
g_sequence_move_range ()
Inserts the (begin, end ) range at the destination pointed to by ptr. The begin and end iters must point
into the same sequence. It is allowed for dest to point to a different sequence than the one pointed into
by begin and end .
If dest is NULL, the range indicated by begin and end is removed from the sequence. If dest iter
points to a place within the (begin, end ) range, the range does not move.
dest : a GSequenceIter
begin : a GSequenceIter
end : a GSequenceIter
Since 2.14
505
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
g_sequence_search ()
Returns an iterator pointing to the position where data would be inserted according to cmp_func
and cmp_data.
seq : a GSequence
cmp_func : the GCompareDataFunc used to compare items in the sequence. It is called with two items
of the seq and user_data. It should return 0 if the items are equal, a negative value if the first
item comes before the second, and a positive value if the second item comes before the first.
Returns : an GSequenceIter pointing to the position where data would have been inserted according to
cmp_func and cmp_data.
Since 2.14
g_sequence_search_iter ()
seq : a GSequence
iter_cmp : the GSequenceIterCompare function used to compare iterators in the sequence. It is called
with two iterators pointing into seq . It should return 0 if the iterators are equal, a negative value if
the first iterator comes before the second, and a positive value if the second iterator comes before
the first.
Returns : a GSequenceIter pointing to the position in seq where data would have been inserted accord-
ing to iter_cmp and cmp_data.
Since 2.14
g_sequence_get ()
iter : a GSequenceIter
Since 2.14
506
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
g_sequence_set ()
Changes the data for the item pointed to by iter to be data. If the sequence has a data destroy
function associated with it, that function is called on the existing data that iter pointed to.
iter : a GSequenceIter
Since 2.14
g_sequence_iter_is_begin ()
iter : a GSequenceIter
Since 2.14
g_sequence_iter_is_end ()
iter : a GSequenceIter
Since 2.14
g_sequence_iter_next ()
Returns an iterator pointing to the next position after iter . If iter is the end iterator, the end iterator
is returned.
iter : a GSequenceIter
Since 2.14
g_sequence_iter_prev ()
Returns an iterator pointing to the previous position before iter . If iter is the begin iterator, the
begin iterator is returned.
iter : a GSequenceIter
Since 2.14
507
CHAPTER 5. GLIB DATA TYPES 5.6. SEQUENCES
g_sequence_iter_get_position ()
iter : a GSequenceIter
Since 2.14
g_sequence_iter_move ()
Returns the GSequenceIter which is delta positions away from iter . If iter is closer than -delta
positions to the beginning of the sequence, the begin iterator is returned. If iter is closer than delta
positions to the end of the sequence, the end iterator is returned.
iter : a GSequenceIter
delta : A positive or negative number indicating how many positions away from iter the returned
GSequenceIter will be.
Since 2.14
g_sequence_iter_get_sequence ()
iter : a GSequenceIter
Since 2.14
g_sequence_iter_compare ()
Returns a negative number if a comes before b, 0 if they are equal, and a positive number if a comes
after b.
The a and b iterators must point into the same sequence.
a : a GSequenceIter
b : a GSequenceIter
Returns : A negative number if a comes before b, 0 if they are equal, and a positive number if a comes
after b.
Since 2.14
508
CHAPTER 5. GLIB DATA TYPES 5.7. TRASH STACKS
g_sequence_range_get_midpoint ()
Finds an iterator somewhere in the range (begin, end ). This iterator will be close to the middle of
the range, but is not guaranteed to be exactly in the middle.
The begin and end iterators must both point to the same sequence and begin must come before or
be equal to end in the sequence.
begin : a GSequenceIter
end : a GSequenceIter
Since 2.14
Synopsis
#include <glib.h>
GTrashStack;
void g_trash_stack_push (GTrashStack **stack_p,
gpointer data_p);
gpointer g_trash_stack_pop (GTrashStack **stack_p);
gpointer g_trash_stack_peek (GTrashStack **stack_p);
guint g_trash_stack_height (GTrashStack **stack_p);
Description
A GTrashStack is an efficient way to keep a stack of unused allocated memory chunks. Each memory
chunk is required to be large enough to hold a gpointer. This allows the stack to be maintained without
any space overhead, since the stack pointers can be stored inside the memory chunks.
There is no function to create a GTrashStack. A NULL GTrashStack* is a perfectly valid empty stack.
Details
GTrashStack
typedef struct {
GTrashStack *next;
} GTrashStack;
Each piece of memory that is pushed onto the stack is cast to a GTrashStack*.
GTrashStack *next; pointer to the previous element of the stack, gets stored in the first sizeof (gp-
ointer) bytes of the element.
509
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_trash_stack_push ()
g_trash_stack_pop ()
g_trash_stack_peek ()
g_trash_stack_height ()
Returns the height of a GTrashStack. Note that execution of this function is of O(N) complexity
where N denotes the number of items on the stack.
Synopsis
#include <glib.h>
GHashTable;
GHashTable* g_hash_table_new (GHashFunc hash_func,
GEqualFunc key_equal_func);
GHashTable* g_hash_table_new_full (GHashFunc hash_func,
GEqualFunc key_equal_func,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func)
guint (*GHashFunc) (gconstpointer key);
510
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
511
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
Description
A GHashTable provides associations between keys and values which is optimized so that given a key,
the associated value can be found very quickly.
Note that neither keys nor values are copied when inserted into the GHashTable, so they must exist
for the lifetime of the GHashTable. This means that the use of static strings is OK, but temporary strings
(i.e. those created in buffers and those returned by GTK+ widgets) should be copied with g_strdup()
before being inserted.
If keys or values are dynamically allocated, you must be careful to ensure that they are freed when
they are removed from the GHashTable, and also when they are overwritten by new insertions into
the GHashTable. It is also not advisable to mix static strings and dynamically-allocated strings in a
GHashTable, because it then becomes difficult to determine whether the string should be freed.
To create a GHashTable, use g_hash_table_new().
To insert a key and value into a GHashTable, use g_hash_table_insert().
To lookup a value corresponding to a given key, use g_hash_table_lookup() and g_hash_table_lookup_extended().
To remove a key and value, use g_hash_table_remove().
To call a function for each key and value pair use g_hash_table_foreach() or use a iterator to iterate
over the key/value pairs in the hash table, see GHashTableIter.
To destroy a GHashTable use g_hash_table_destroy().
Details
GHashTable
The GHashTable struct is an opaque data structure to represent a Hash Table. It should only be
accessed via the following functions.
g_hash_table_new ()
512
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_hash_table_new_full ()
Creates a new GHashTable like g_hash_table_new() with a reference count of 1 and allows to specify
functions to free the memory allocated for the key and value that get called when removing the entry
from the GHashTable.
key_destroy_func : a function to free the memory allocated for the key used when removing the entry
from the GHashTable or NULL if you don’t want to supply such a function.
value_destroy_func : a function to free the memory allocated for the value used when removing the
entry from the GHashTable or NULL if you don’t want to supply such a function.
GHashFunc ()
Specifies the type of the hash function which is passed to g_hash_table_new() when a GHashTable
is created.
The function is passed a key and should return a guint hash value. The functions g_direct_hash(),
g_int_hash() and g_str_hash() provide hash functions which can be used when the key is a gpointer,
gint, and gchar* respectively.
The hash values should be evenly distributed over a fairly large range? The modulus is taken with
the hash table size (a prime number) to find the ’bucket’ to place each key into. The function should also
be very fast, since it is called for each key lookup.
key : a key.
GEqualFunc ()
Specifies the type of a function used to test two values for equality. The function should return TRUE
if both values are equal and FALSE otherwise.
a : a value.
513
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_hash_table_insert ()
hash_table : a GHashTable.
g_hash_table_replace ()
Inserts a new key and value into a GHashTable similar to g_hash_table_insert(). The difference is
that if the key already exists in the GHashTable, it gets replaced by the new key. If you supplied a v-
alue_destroy_func when creating the GHashTable, the old value is freed using that function. If you
supplied a key_destroy_func when creating the GHashTable, the old key is freed using that function.
hash_table : a GHashTable.
g_hash_table_size ()
hash_table : a GHashTable.
g_hash_table_lookup ()
Looks up a key in a GHashTable. Note that this function cannot distinguish between a key that
is not present and one which is present and has the value NULL. If you need this distinction, use
g_hash_table_lookup_extended().
hash_table : a GHashTable.
514
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_hash_table_lookup_extended ()
Looks up a key in the GHashTable, returning the original key and the associated value and a gboolean
which is TRUE if the key was found. This is useful if you need to free the memory allocated for the orig-
inal key, for example before calling g_hash_table_remove().
You can actually pass NULL for lookup_key to test whether the NULL key exists.
hash_table : a GHashTable
value : return location for the value associated with the key, or NULL
g_hash_table_foreach ()
Calls the given function for each of the key/value pairs in the GHashTable. The function is passed
the key and value of each pair, and the given user_data parameter. The hash table may not be modified
while iterating over it (you can’t add/remove items). To remove all items matching a predicate, use
g_hash_table_foreach_remove().
See g_hash_table_find() for performance caveats for linear order searches in contrast to g_hash_table_lookup().
hash_table : a GHashTable.
g_hash_table_find ()
Calls the given function for key/value pairs in the GHashTable until predicate returns TRUE. The
function is passed the key and value of each pair, and the given user_data parameter. The hash table
may not be modified while iterating over it (you can’t add/remove items).
Note, that hash tables are really only optimized for forward lookups, i.e. g_hash_table_lookup(). So
code that frequently issues g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of once per
every entry in a hash table) should probably be reworked to use additional or different data structures
for reverse lookups (keep in mind that an O(n) find/foreach operation issued for all n values in a hash
table ends up needing O(n*n) operations).
hash_table : a GHashTable.
Returns : The value of the first key/value pair is returned, for which func evaluates to TRUE. If no pair
with the requested property is found, NULL is returned.
Since 2.4
515
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
GHFunc ()
Specifies the type of the function passed to g_hash_table_foreach(). It is called with each key/value
pair, together with the user_data parameter which is passed to g_hash_table_foreach().
key : a key.
g_hash_table_remove ()
Returns : TRUE if the key was found and removed from the GHashTable.
g_hash_table_steal ()
Removes a key and its associated value from a GHashTable without calling the key and value destroy
functions.
hash_table : a GHashTable.
Returns : TRUE if the key was found and removed from the GHashTable.
g_hash_table_foreach_remove ()
Calls the given function for each key/value pair in the GHashTable. If the function returns TRUE,
then the key/value pair is removed from the GHashTable. If you supplied key or value destroy functions
when creating the GHashTable, they are used to free the memory allocated for the removed keys and
values.
See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.
hash_table : a GHashTable.
516
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_hash_table_foreach_steal ()
Calls the given function for each key/value pair in the GHashTable. If the function returns TRUE,
then the key/value pair is removed from the GHashTable, but no key or value destroy functions are
called.
See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.
hash_table : a GHashTable.
g_hash_table_remove_all ()
g_hash_table_steal_all ()
Removes all keys and their associated values from a GHashTable without calling the key and value
destroy functions.
hash_table : a GHashTable.
Since 2.12
g_hash_table_get_keys ()
Retrieves every key inside hash_table. The returned data is valid until hash_table is modified.
hash_table : a GHashTable
Returns : a GList containing all the keys inside the hash table. The content of the list is owned by the
hash table and should not be modified or freed. Use g_list_free() when done using the list.
Since 2.14
g_hash_table_get_values ()
Retrieves every value inside hash_table. The returned data is valid until hash_table is modified.
hash_table : a GHashTable
Returns : a GList containing all the values inside the hash table. The content of the list is owned by the
hash table and should not be modified or freed. Use g_list_free() when done using the list.
Since 2.14
517
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
GHRFunc ()
Specifies the type of the function passed to g_hash_table_foreach_remove(). It is called with each
key/value pair, together with the user_data parameter passed to g_hash_table_foreach_remove(). It
should return TRUE if the key/value pair should be removed from the GHashTable.
key : a key.
Returns : %TRUE if the key/value pair should be removed from the GHashTable.
g_hash_table_freeze()
#define g_hash_table_freeze(hash_table)
WARNING
This function is deprecated and will be removed in the next major release of GLib. It does nothing.
hash_table : a GHashTable
g_hash_table_thaw()
#define g_hash_table_thaw(hash_table)
WARNING
This function is deprecated and will be removed in the next major release of GLib. It does nothing.
hash_table : a GHashTable
g_hash_table_destroy ()
Destroys all keys and values in the GHashTable and decrements its reference count by 1. If keys
and/or values are dynamically allocated, you should either free them first or create the GHashTable with
destroy notifiers using g_hash_table_new_full(). In the latter case the destroy functions you supplied
will be called on all keys and values during the destruction phase.
hash_table : a GHashTable.
518
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_hash_table_ref ()
Atomically increments the reference count of hash_table by one. This function is MT-safe and may
be called from any thread.
Since 2.10
g_hash_table_unref ()
Atomically decrements the reference count of hash_table by one. If the reference count drops to
0, all keys and values will be destroyed, and all memory allocated by the hash table is released. This
function is MT-safe and may be called from any thread.
Since 2.10
GHashTableIter
typedef struct {
} GHashTableIter;
A GHashTableIter structure represents an iterator that can be used to iterate over the elements of
a GHashTable. GHashTableIter structures are typically allocated on the stack and then initialized with
g_hash_table_iter_init().
g_hash_table_iter_init ()
Initializes a key/value pair iterator and associates it with hash_table. Modifying the hash table
after calling this function invalidates the returned iterator.
GHashTableIter iter;
gpointer key, value;
hash_table : a GHashTable.
Since 2.16
519
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_hash_table_iter_next ()
Advances iter and retrieves the key and/or value that are now pointed to as a result of this ad-
vancement. If FALSE is returned, key and value are not set, and the iterator becomes invalid.
Since 2.16
g_hash_table_iter_get_hash_table ()
Since 2.16
g_hash_table_iter_remove ()
Removes the key/value pair currently pointed to by the iterator from its associated GHashTable.
Can only be called after g_hash_table_iter_next() returned TRUE, and cannot be called more than once
for the same key/value pair.
If the GHashTable was created using g_hash_table_new_full(), the key and value are freed using the
supplied destroy functions, otherwise you have to make sure that any dynamically allocated values are
freed yourself.
Since 2.16
g_hash_table_iter_steal ()
Removes the key/value pair currently pointed to by the iterator from its associated GHashTable,
without calling the key and value destroy functions. Can only be called after g_hash_table_iter_next()
returned TRUE, and cannot be called more than once for the same key/value pair.
Since 2.16
520
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_direct_equal ()
Compares two gpointer arguments and returns TRUE if they are equal. It can be passed to g_hash_table_new()
as the key_equal_func parameter, when using pointers as keys in a GHashTable.
v1 : a key.
g_direct_hash ()
Converts a gpointer to a hash value. It can be passed to g_hash_table_new() as the hash_func pa-
rameter, when using pointers as keys in a GHashTable.
v : a gpointer key
g_int_equal ()
Compares the two gint values being pointed to and returns TRUE if they are equal. It can be passed
to g_hash_table_new() as the key_equal_func parameter, when using pointers to integers as keys in a
GHashTable.
v1 : a pointer to a gint key.
g_int_hash ()
Converts a pointer to a gint to a hash value. It can be passed to g_hash_table_new() as the hash_func
parameter, when using pointers to integers values as keys in a GHashTable.
v : a pointer to a gint key
g_int64_equal ()
Compares the two gint64 values being pointed to and returns TRUE if they are equal. It can be
passed to g_hash_table_new() as the key_equal_func parameter, when using pointers to 64-bit integers
as keys in a GHashTable.
v1 : a pointer to a gint64 key.
521
CHAPTER 5. GLIB DATA TYPES 5.8. HASH TABLES
g_int64_hash ()
Converts a pointer to a gint64 to a hash value. It can be passed to g_hash_table_new() as the hash_-
func parameter, when using pointers to 64-bit integers values as keys in a GHashTable.
v : a pointer to a gint64 key
g_double_equal ()
Compares the two gdouble values being pointed to and returns TRUE if they are equal. It can be
passed to g_hash_table_new() as the key_equal_func parameter, when using pointers to doubles as
keys in a GHashTable.
v1 : a pointer to a gdouble key.
g_double_hash ()
g_str_equal ()
Compares two strings for byte-by-byte equality and returns TRUE if they are equal. It can be passed
to g_hash_table_new() as the key_equal_func parameter, when using strings as keys in a GHashTable.
v1 : a key
g_str_hash ()
Converts a string to a hash value. It can be passed to g_hash_table_new() as the hash_func parame-
ter, when using strings as keys in a GHashTable.
v : a string key
522
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
5.9 Strings
Name
Strings – text buffers which grow automatically as text is added
Synopsis
#include <glib.h>
GString;
GString* g_string_new (const gchar *init);
GString* g_string_new_len (const gchar *init,
gssize len);
GString* g_string_sized_new (gsize dfl_size);
GString* g_string_assign (GString *string,
const gchar *rval);
#define g_string_sprintf
#define g_string_sprintfa
void g_string_vprintf (GString *string,
const gchar *format,
va_list args);
void g_string_append_vprintf (GString *string,
const gchar *format,
va_list args);
void g_string_printf (GString *string,
const gchar *format,
...);
void g_string_append_printf (GString *string,
const gchar *format,
...);
GString* g_string_append (GString *string,
const gchar *val);
GString* g_string_append_c (GString *string,
gchar c);
GString* g_string_append_unichar (GString *string,
gunichar wc);
GString* g_string_append_len (GString *string,
const gchar *val,
gssize len);
GString * g_string_append_uri_escaped (GString *string,
const char *unescaped,
const char *reserved_chars_al
gboolean allow_utf8);
GString* g_string_prepend (GString *string,
const gchar *val);
GString* g_string_prepend_c (GString *string,
gchar c);
GString* g_string_prepend_unichar (GString *string,
gunichar wc);
GString* g_string_prepend_len (GString *string,
const gchar *val,
gssize len);
GString* g_string_insert (GString *string,
gssize pos,
const gchar *val);
GString* g_string_insert_c (GString *string,
gssize pos,
523
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
gchar c);
GString* g_string_insert_unichar (GString *string,
gssize pos,
gunichar wc);
GString* g_string_insert_len (GString *string,
gssize pos,
const gchar *val,
gssize len);
GString* g_string_overwrite (GString *string,
gsize pos,
const gchar *val);
GString* g_string_overwrite_len (GString *string,
gsize pos,
const gchar *val,
gssize len);
GString* g_string_erase (GString *string,
gssize pos,
gssize len);
GString* g_string_truncate (GString *string,
gsize len);
GString* g_string_set_size (GString *string,
gsize len);
gchar* g_string_free (GString *string,
gboolean free_segment);
Description
A GString is similar to a standard C string, except that it grows automatically as text is appended or
inserted. Also, it stores the length of the string, so can be used for binary data with embedded nul bytes.
Details
GString
typedef struct {
gchar *str;
gsize len;
gsize allocated_len;
} GString;
g_string_new ()
524
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
g_string_new_len ()
Creates a new GString with len bytes of the init buffer. Because a length is provided, init need
not be nul-terminated, and can contain embedded nul bytes.
Since this function does not stop at nul bytes, it is the caller’s responsibility to ensure that init has
at least len addressable bytes.
init : initial contents of the string
g_string_sized_new ()
Creates a new GString, with enough space for dfl_size bytes. This is useful if you are going to add
a lot of text to the string and don’t want it to be reallocated too often.
dfl_size : the default size of the space allocated to hold the string
g_string_assign ()
Copies the bytes from a string into a GString, destroying any previous contents. It is rather like the
standard strcpy() function, except that you do not have to worry about having enough space to copy the
string.
string : the destination GString. Its current contents are destroyed.
Returns : string
g_string_sprintf
#define g_string_sprintf
WARNING
Writes a formatted string into a GString. This is similar to the standard sprintf() function, except that
the GString buffer automatically expands to contain the results. The previous contents of the GString
are destroyed.
525
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
string : a GString
g_string_sprintfa
#define g_string_sprintfa
WARNING
Appends a formatted string onto the end of a GString. This function is similar to g_string_sprintf()
except that the text is appended to the GString.
string : a GString
g_string_vprintf ()
Writes a formatted string into a GString. This function is similar to g_string_printf() except that the
arguments to the format string are passed as a va_list.
string : a GString
Since 2.14
g_string_append_vprintf ()
Appends a formatted string onto the end of a GString. This function is similar to g_string_append_printf()
except that the arguments to the format string are passed as a va_list.
string : a GString
Since 2.14
526
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
g_string_printf ()
Writes a formatted string into a GString. This is similar to the standard sprintf() function, except that
the GString buffer automatically expands to contain the results. The previous contents of the GString
are destroyed.
string : a GString
g_string_append_printf ()
Appends a formatted string onto the end of a GString. This function is similar to g_string_printf()
except that the text is appended to the GString.
string : a GString
g_string_append ()
Returns : string
g_string_append_c ()
Returns : string
g_string_append_unichar ()
wc : a Unicode character
Returns : string
527
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
g_string_append_len ()
Appends len bytes of val to string . Because len is provided, val may contain embedded nuls and
need not be nul-terminated.
Since this function does not stop at nul bytes, it is the caller’s responsibility to ensure that val has at
least len addressable bytes.
string : a GString
Returns : string
g_string_append_uri_escaped ()
Appends unescaped to string , escaped any characters that are reserved in URIs using URI-style
escape sequences.
string : a GString
unescaped : a string
allow_utf8 : set TRUE if the escaped string may include UTF8 characters
Returns : string
Since 2.16
g_string_prepend ()
Returns : string
g_string_prepend_c ()
Returns : string
528
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
g_string_prepend_unichar ()
string : a GString
wc : a Unicode character
Returns : string
g_string_prepend_len ()
Prepends len bytes of val to string . Because len is provided, val may contain embedded nuls and
need not be nul-terminated.
Since this function does not stop at nul bytes, it is the caller’s responsibility to ensure that val has at
least len addressable bytes.
string : a GString
Returns : string
g_string_insert ()
string : a GString
Returns : string
g_string_insert_c ()
string : a GString
Returns : string
529
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
g_string_insert_unichar ()
Converts a Unicode character into UTF-8, and insert it into the string at the given position.
string : a GString
pos : the position at which to insert character, or -1 to append at the end of the string
wc : a Unicode character
Returns : string
g_string_insert_len ()
Inserts len bytes of val into string at pos. Because len is provided, val may contain embedded
nuls and need not be nul-terminated. If pos is -1, bytes are inserted at the end of the string.
Since this function does not stop at nul bytes, it is the caller’s responsibility to ensure that val has at
least len addressable bytes.
string : a GString
pos : position in string where insertion should happen, or -1 for at the end
Returns : string
g_string_overwrite ()
string : a GString
val : the string that will overwrite the string starting at pos
Returns : string
Since 2.14
530
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
g_string_overwrite_len ()
Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.
string : a GString
val : the string that will overwrite the string starting at pos
Returns : string
Since 2.14
g_string_erase ()
Removes len bytes from a GString, starting at position pos. The rest of the GString is shifted down
to fill the gap.
string : a GString
Returns : string
g_string_truncate ()
Cuts off the end of the GString, leaving the first len bytes.
string : a GString
Returns : string
g_string_set_size ()
Sets the length of a GString. If the length is less than the current length, the string will be truncated.
If the length is greater than the current length, the contents of the newly added area are undefined.
(However, as always, string->str[string->len] will be a nul byte.)
string : a GString
Returns : string
531
CHAPTER 5. GLIB DATA TYPES 5.9. STRINGS
g_string_free ()
Frees the memory allocated for the GString. If free_segment is TRUE it also frees the character data.
string : a GString
g_string_up ()
WARNING
g_string_up has been deprecated since version 2.2 and should not be used in newly-
written code. This function uses the locale-specific toupper() function, which is almost
never the right thing. Use g_string_ascii_up() or g_utf8_strup() instead.
string : a GString
Returns : string
g_string_down ()
WARNING
g_string_down has been deprecated since version 2.2 and should not be used in
newly-written code. This function uses the locale-specific tolower() function, which is
almost never the right thing. Use g_string_ascii_down() or g_utf8_strdown() instead.
string : a GString
g_string_hash ()
532
CHAPTER 5. GLIB DATA TYPES 5.10. STRING CHUNKS
g_string_equal ()
Compares two strings for equality, returning TRUE if they are equal. For use with GHashTable.
v : a GString
v2 : another GString
Returns : TRUE if they strings are the same length and contain the same bytes
Synopsis
#include <glib.h>
GStringChunk;
GStringChunk* g_string_chunk_new (gsize size);
gchar* g_string_chunk_insert (GStringChunk *chunk,
const gchar *string);
gchar* g_string_chunk_insert_const (GStringChunk *chunk,
const gchar *string);
gchar* g_string_chunk_insert_len (GStringChunk *chunk,
const gchar *string,
gssize len);
void g_string_chunk_clear (GStringChunk *chunk);
void g_string_chunk_free (GStringChunk *chunk);
Description
String chunks are used to store groups of strings. Memory is allocated in blocks, and as strings are
added to the GStringChunk they are copied into the next free position in a block. When a block is full a
new block is allocated.
When storing a large number of strings, string chunks are more efficient than using g_strdup() since
fewer calls to malloc() are needed, and less memory is wasted in memory allocation overheads.
By adding strings with g_string_chunk_insert_const() it is also possible to remove duplicates.
To create a new GStringChunk use g_string_chunk_new().
To add strings to a GStringChunk use g_string_chunk_insert().
To add strings to a GStringChunk, but without duplicating strings which are already in the GStringChunk,
use g_string_chunk_insert_const().
To free the entire GStringChunk use g_string_chunk_free(). It is not possible to free individual
strings.
Details
GStringChunk
An opaque data structure representing String Chunks. It should only be accessed by using the fol-
lowing functions.
533
CHAPTER 5. GLIB DATA TYPES 5.10. STRING CHUNKS
g_string_chunk_new ()
g_string_chunk_insert ()
Adds a copy of string to the GStringChunk. It returns a pointer to the new copy of the string in the
GStringChunk. The characters in the string can be changed, if necessary, though you should not change
anything after the end of the string.
Unlike g_string_chunk_insert_const(), this function does not check for duplicates. Also strings added
with g_string_chunk_insert() will not be searched by g_string_chunk_insert_const() when looking for
duplicates.
chunk : a GStringChunk
g_string_chunk_insert_const ()
Adds a copy of string to the GStringChunk, unless the same string has already been added to the
GStringChunk with g_string_chunk_insert_const().
This function is useful if you need to copy a large number of strings but do not want to waste space
storing duplicates. But you must remember that there may be several pointers to the same string, and
so any changes made to the strings should be done very carefully.
Note that g_string_chunk_insert_const() will not return a pointer to a string added with g_string_chunk_insert(),
even if they do match.
chunk : a GStringChunk
Returns : a pointer to the new or existing copy of string within the GStringChunk
g_string_chunk_insert_len ()
Adds a copy of the first len bytes of string to the GStringChunk. The copy is nul-terminated.
Since this function does not stop at nul bytes, it is the caller’s responsibility to ensure that string
has at least len addressable bytes.
The characters in the returned string can be changed, if necessary, though you should not change
anything after the end of the string.
chunk : a GStringChunk
534
CHAPTER 5. GLIB DATA TYPES 5.11. ARRAYS
g_string_chunk_clear ()
Frees all strings contained within the GStringChunk. After calling g_string_chunk_clear() it is not
safe to access any of the strings which were contained within it.
chunk : a GStringChunk
Since 2.14
g_string_chunk_free ()
Frees all memory allocated by the GStringChunk. After calling g_string_chunk_free() it is not safe to
access any of the strings which were contained within it.
chunk : a GStringChunk
5.11 Arrays
Name
Arrays – arrays of arbitrary elements which grow automatically as elements are added
Synopsis
#include <glib.h>
GArray;
GArray* g_array_new (gboolean zero_terminated,
gboolean clear_,
guint element_size);
GArray* g_array_sized_new (gboolean zero_terminated,
gboolean clear_,
guint element_size,
guint reserved_size);
GArray * g_array_ref (GArray *array);
void g_array_unref (GArray *array);
guint g_array_get_element_size (GArray *array);
#define g_array_append_val (a,v)
GArray* g_array_append_vals (GArray *array,
gconstpointer data,
guint len);
#define g_array_prepend_val (a,v)
GArray* g_array_prepend_vals (GArray *array,
gconstpointer data,
guint len);
#define g_array_insert_val (a,i,v)
GArray* g_array_insert_vals (GArray *array,
guint index_,
gconstpointer data,
535
CHAPTER 5. GLIB DATA TYPES 5.11. ARRAYS
guint len);
GArray* g_array_remove_index (GArray *array,
guint index_);
GArray* g_array_remove_index_fast (GArray *array,
guint index_);
GArray* g_array_remove_range (GArray *array,
guint index_,
guint length);
void g_array_sort (GArray *array,
GCompareFunc compare_func);
void g_array_sort_with_data (GArray *array,
GCompareDataFunc compare_func,
gpointer user_data);
#define g_array_index (a,t,i)
GArray* g_array_set_size (GArray *array,
guint length);
gchar* g_array_free (GArray *array,
gboolean free_segment);
Description
Arrays are similar to standard C arrays, except that they grow automatically as elements are added.
Array elements can be of any size (though all elements of one array are the same size), and the array
can be automatically cleared to ’0’s and zero-terminated.
To create a new array use g_array_new().
To add elements to an array, use g_array_append_val(), g_array_append_vals(), g_array_prepend_val(),
and g_array_prepend_vals().
To access an element of an array, use g_array_index().
To set the size of an array, use g_array_set_size().
To free an array, use g_array_free().
Details
GArray
typedef struct {
gchar *data;
guint len;
} GArray;
536
CHAPTER 5. GLIB DATA TYPES 5.11. ARRAYS
g_array_new ()
zero_terminated : %TRUE if the array should have an extra element at the end which is set to 0.
clear_ : %TRUE if GArray elements should be automatically cleared to 0 when they are allocated.
g_array_sized_new ()
Creates a new GArray with reserved_size elements preallocated and a reference count of 1. This
avoids frequent reallocation, if you are going to add many elements to the array. Note however that the
size of the array is still 0.
zero_terminated : %TRUE if the array should have an extra element at the end with all bits cleared.
g_array_ref ()
Atomically increments the reference count of array by one. This function is MT-safe and may be
called from any thread.
array : A GArray.
Since 2.22
g_array_unref ()
Atomically decrements the reference count of array by one. If the reference count drops to 0, all
memory allocated by the array is released. This function is MT-safe and may be called from any thread.
array : A GArray.
Since 2.22
537
CHAPTER 5. GLIB DATA TYPES 5.11. ARRAYS
g_array_get_element_size ()
g_array_append_val()
#define g_array_append_val(a,v)
Adds the value on to the end of the array. The array will grow in size automatically if necessary.
N OTE
a : a GArray.
g_array_append_vals ()
g_array_prepend_val()
#define g_array_prepend_val(a,v)
Adds the value on to the start of the array. The array will grow in size automatically if necessary.
This operation is slower than g_array_append_val() since the existing elements in the array have to
be moved to make space for the new element.
N OTE
538
CHAPTER 5. GLIB DATA TYPES 5.11. ARRAYS
a : a GArray.
g_array_prepend_vals ()
g_array_insert_val()
#define g_array_insert_val(a,i,v)
N OTE
a : a GArray.
g_array_insert_vals ()
539
CHAPTER 5. GLIB DATA TYPES 5.11. ARRAYS
g_array_remove_index ()
Removes the element at the given index from a GArray. The following elements are moved down
one place.
array : a GArray.
g_array_remove_index_fast ()
Removes the element at the given index from a GArray. The last element in the array is used
to fill in the space, so this function does not preserve the order of the GArray. But it is faster than
g_array_remove_index().
array : a GArray .
g_array_remove_range ()
Removes the given number of elements starting at the given index from a GArray. The following
elements are moved to close the gap.
array : a GArray .
Since 2.4
g_array_sort ()
Sorts a GArray using compare_func which should be a qsort()-style comparison function (returns
less than zero for first arg is less than second arg, zero for equal, greater zero if first arg is greater than
second arg).
If two array elements compare equal, their order in the sorted array is undefined.
array : a GArray.
540
CHAPTER 5. GLIB DATA TYPES 5.11. ARRAYS
g_array_sort_with_data ()
Like g_array_sort(), but the comparison function receives an extra user data argument.
array : a GArray.
g_array_index()
#define g_array_index(a,t,i)
Returns the element of a GArray at the given index. The return value is cast to the given type.
a : a GArray.
g_array_set_size ()
Sets the size of the array, expanding it if necessary. If the array was created with clear_ set to TRUE,
the new elements are set to 0.
array : a GArray.
g_array_free ()
Frees the memory allocated for the GArray. If free_segment is TRUE it frees the memory block
holding the elements as well and also each element if array has a element_free_func set. Pass FALSE
if you want to free the GArray wrapper but preserve the underlying array for use elsewhere. If the
reference count of array is greater than one, the GArray wrapper is preserved but the size of array will
be set to zero.
541
CHAPTER 5. GLIB DATA TYPES 5.12. POINTER ARRAYS
N OTE
array : a GArray.
Returns : the element data if free_segment is FALSE, otherwise NULL. The element data should be
freed using g_free().
Synopsis
#include <glib.h>
GPtrArray;
GPtrArray* g_ptr_array_new (void);
GPtrArray* g_ptr_array_sized_new (guint reserved_size);
GPtrArray* g_ptr_array_new_with_free_func (GDestroyNotify element_free_func);
void g_ptr_array_set_free_func (GPtrArray *array,
GDestroyNotify element_free_func);
GPtrArray* g_ptr_array_ref (GPtrArray *array);
void g_ptr_array_unref (GPtrArray *array);
void g_ptr_array_add (GPtrArray *array,
gpointer data);
gboolean g_ptr_array_remove (GPtrArray *array,
gpointer data);
gpointer g_ptr_array_remove_index (GPtrArray *array,
guint index_);
gboolean g_ptr_array_remove_fast (GPtrArray *array,
gpointer data);
gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
guint index_);
void g_ptr_array_remove_range (GPtrArray *array,
guint index_,
guint length);
void g_ptr_array_sort (GPtrArray *array,
GCompareFunc compare_func);
void g_ptr_array_sort_with_data (GPtrArray *array,
GCompareDataFunc compare_func,
gpointer user_data);
void g_ptr_array_set_size (GPtrArray *array,
gint length);
#define g_ptr_array_index (array,index_)
gpointer* g_ptr_array_free (GPtrArray *array,
gboolean free_seg);
542
CHAPTER 5. GLIB DATA TYPES 5.12. POINTER ARRAYS
Description
Pointer Arrays are similar to Arrays but are used only for storing pointers.
N OTE
If you remove elements from the array, elements at the end of the array are moved into
the space previously occupied by the removed element. This means that you should not
rely on the index of particular elements remaining the same. You should also be careful
when deleting elements while iterating over the array.
Details
GPtrArray
typedef struct {
gpointer *pdata;
guint len;
} GPtrArray;
gpointer *pdata; points to the array of pointers, which may be moved when the array grows.
g_ptr_array_new ()
543
CHAPTER 5. GLIB DATA TYPES 5.12. POINTER ARRAYS
g_ptr_array_sized_new ()
Creates a new GPtrArray with reserved_size pointers preallocated and a reference count of 1. This
avoids frequent reallocation, if you are going to add many pointers to the array. Note however that the
size of the array is still 0.
reserved_size : number of pointers preallocated.
g_ptr_array_new_with_free_func ()
Creates a new GPtrArray with a reference count of 1 and use element_free_func for freeing each
element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called
with free_segment set to TRUE or when removing elements.
element_free_func : A function to free elements with destroy array or NULL.
g_ptr_array_set_free_func ()
Sets a function for freeing each element when array is destroyed either via g_ptr_array_unref(),
when g_ptr_array_free() is called with free_segment set to TRUE or when removing elements.
array : A GPtrArray.
Since 2.22
g_ptr_array_ref ()
Atomically increments the reference count of array by one. This function is MT-safe and may be
called from any thread.
array : A GArray.
g_ptr_array_unref ()
Atomically decrements the reference count of array by one. If the reference count drops to 0, the
effect is the same as calling g_ptr_array_free() with free_segment set to TRUE. This function is MT-safe
and may be called from any thread.
array : A GPtrArray.
Since 2.22
544
CHAPTER 5. GLIB DATA TYPES 5.12. POINTER ARRAYS
g_ptr_array_add ()
Adds a pointer to the end of the pointer array. The array will grow in size automatically if necessary.
array : a GPtrArray.
g_ptr_array_remove ()
Removes the first occurrence of the given pointer from the pointer array. The following elements are
moved down one place. If array has a non-NULL GDestroyNotify function it is called for the removed
element.
It returns TRUE if the pointer was removed, or FALSE if the pointer was not found.
array : a GPtrArray.
Returns : %TRUE if the pointer is removed. FALSE if the pointer is not found in the array.
g_ptr_array_remove_index ()
Removes the pointer at the given index from the pointer array. The following elements are moved
down one place. If array has a non-NULL GDestroyNotify function it is called for the removed element.
array : a GPtrArray.
g_ptr_array_remove_fast ()
Removes the first occurrence of the given pointer from the pointer array. The last element in the array
is used to fill in the space, so this function does not preserve the order of the array. But it is faster than
g_ptr_array_remove(). If array has a non-NULL GDestroyNotify function it is called for the removed
element.
It returns TRUE if the pointer was removed, or FALSE if the pointer was not found.
array : a GPtrArray.
545
CHAPTER 5. GLIB DATA TYPES 5.12. POINTER ARRAYS
g_ptr_array_remove_index_fast ()
Removes the pointer at the given index from the pointer array. The last element in the array is
used to fill in the space, so this function does not preserve the order of the array. But it is faster than
g_ptr_array_remove_index(). If array has a non-NULL GDestroyNotify function it is called for the
removed element.
array : a GPtrArray.
g_ptr_array_remove_range ()
Removes the given number of pointers starting at the given index from a GPtrArray. The following
elements are moved to close the gap. If array has a non-NULL GDestroyNotify function it is called for
the removed elements.
array : a GPtrArray .
Since 2.4
g_ptr_array_sort ()
Sorts the array, using compare_func which should be a qsort()-style comparison function (returns
less than zero for first arg is less than second arg, zero for equal, greater than zero if irst arg is greater
than second arg).
If two array elements compare equal, their order in the sorted array is undefined.
N OTE
The comparison function for g_ptr_array_sort() doesn’t take the pointers from the array
as arguments, it takes pointers to the pointers in the array.
array : a GPtrArray.
546
CHAPTER 5. GLIB DATA TYPES 5.12. POINTER ARRAYS
g_ptr_array_sort_with_data ()
Like g_ptr_array_sort(), but the comparison function has an extra user data argument.
N OTE
The comparison function for g_ptr_array_sort_with_data() doesn’t take the pointers from
the array as arguments, it takes pointers to the pointers in the array.
array : a GPtrArray.
g_ptr_array_set_size ()
Sets the size of the array, expanding it if necessary. New elements are set to NULL.
array : a GPtrArray.
g_ptr_array_index()
#define g_ptr_array_index(array,index_)
g_ptr_array_free ()
Frees the memory allocated for the GPtrArray. If free_seg is TRUE it frees the memory block
holding the elements as well. Pass FALSE if you want to free the GPtrArray wrapper but preserve the
underlying array for use elsewhere. If the reference count of array is greater than one, the GPtrArray
wrapper is preserved but the size of array will be set to zero.
N OTE
547
CHAPTER 5. GLIB DATA TYPES 5.13. BYTE ARRAYS
array : a GPtrArray.
Returns : the pointer array if free_seg is FALSE, otherwise NULL. The pointer array should be freed
using g_free().
g_ptr_array_foreach ()
Since 2.4
Synopsis
#include <glib.h>
GByteArray;
GByteArray* g_byte_array_new (void);
GByteArray* g_byte_array_sized_new (guint reserved_size);
GByteArray * g_byte_array_ref (GByteArray *array);
void g_byte_array_unref (GByteArray *array);
GByteArray* g_byte_array_append (GByteArray *array,
const guint8 *data,
guint len);
GByteArray* g_byte_array_prepend (GByteArray *array,
const guint8 *data,
guint len);
GByteArray* g_byte_array_remove_index (GByteArray *array,
guint index_);
GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
guint index_);
GByteArray* g_byte_array_remove_range (GByteArray *array,
guint index_,
guint length);
void g_byte_array_sort (GByteArray *array,
GCompareFunc compare_func);
void g_byte_array_sort_with_data (GByteArray *array,
GCompareDataFunc compare_func,
gpointer user_data);
GByteArray* g_byte_array_set_size (GByteArray *array,
guint length);
guint8* g_byte_array_free (GByteArray *array,
gboolean free_segment);
548
CHAPTER 5. GLIB DATA TYPES 5.13. BYTE ARRAYS
Description
GByteArray is based on GArray, to provide arrays of bytes which grow automatically as elements are
added.
To create a new GByteArray use g_byte_array_new().
To add elements to a GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
To set the size of a GByteArray, use g_byte_array_set_size().
To free a GByteArray, use g_byte_array_free().
Details
GByteArray
typedef struct {
guint8 *data;
guint len;
} GByteArray;
guint8 *data; a pointer to the element data. The data may be moved as elements are added to the
GByteArray.
g_byte_array_new ()
g_byte_array_sized_new ()
Creates a new GByteArray with reserved_size bytes preallocated. This avoids frequent realloca-
tion, if you are going to add many bytes to the array. Note however that the size of the array is still
0.
549
CHAPTER 5. GLIB DATA TYPES 5.13. BYTE ARRAYS
g_byte_array_ref ()
Atomically increments the reference count of array by one. This function is MT-safe and may be
called from any thread.
array : A GByteArray.
Since 2.22
g_byte_array_unref ()
Atomically decrements the reference count of array by one. If the reference count drops to 0, all
memory allocated by the array is released. This function is MT-safe and may be called from any thread.
array : A GByteArray.
Since 2.22
g_byte_array_append ()
Adds the given bytes to the end of the GByteArray. The array will grow in size automatically if
necessary.
array : a GByteArray.
g_byte_array_prepend ()
Adds the given data to the start of the GByteArray. The array will grow in size automatically if
necessary.
array : a GByteArray.
550
CHAPTER 5. GLIB DATA TYPES 5.13. BYTE ARRAYS
g_byte_array_remove_index ()
Removes the byte at the given index from a GByteArray. The following bytes are moved down one
place.
array : a GByteArray.
g_byte_array_remove_index_fast ()
Removes the byte at the given index from a GByteArray. The last element in the array is used to
fill in the space, so this function does not preserve the order of the GByteArray. But it is faster than
g_byte_array_remove_index().
array : a GByteArray.
g_byte_array_remove_range ()
Removes the given number of bytes starting at the given index from a GByteArray. The following
elements are moved to close the gap.
array : a GByteArray .
Since 2.4
g_byte_array_sort ()
Sorts a byte array, using compare_func which should be a qsort()-style comparison function (returns
less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater
than second arg).
If two array elements compare equal, their order in the sorted array is undefined.
array : a GByteArray.
551
CHAPTER 5. GLIB DATA TYPES 5.14. BALANCED BINARY TREES
g_byte_array_sort_with_data ()
Like g_byte_array_sort(), but the comparison function takes an extra user data argument.
array : a GByteArray.
g_byte_array_set_size ()
g_byte_array_free ()
Frees the memory allocated by the GByteArray. If free_segment is TRUE it frees the actual byte
data. If the reference count of array is greater than one, the GByteArray wrapper is preserved but the
size of array will be set to zero.
array : a GByteArray.
Returns : the element data if free_segment is FALSE, otherwise NULL. The element data should be
freed using g_free().
Synopsis
#include <glib.h>
GTree;
GTree* g_tree_new (GCompareFunc key_compare_func);
GTree* g_tree_new_with_data (GCompareDataFunc key_compare_func,
gpointer key_compare_data);
GTree* g_tree_new_full (GCompareDataFunc key_compare_func,
gpointer key_compare_data,
552
CHAPTER 5. GLIB DATA TYPES 5.14. BALANCED BINARY TREES
GDestroyNotify key_destroy_fu
GDestroyNotify value_destroy_
void g_tree_insert (GTree *tree,
gpointer key,
gpointer value);
void g_tree_replace (GTree *tree,
gpointer key,
gpointer value);
gint g_tree_nnodes (GTree *tree);
gint g_tree_height (GTree *tree);
gpointer g_tree_lookup (GTree *tree,
gconstpointer key);
gboolean g_tree_lookup_extended (GTree *tree,
gconstpointer lookup_key,
gpointer *orig_key,
gpointer *value);
void g_tree_foreach (GTree *tree,
GTraverseFunc func,
gpointer user_data);
void g_tree_traverse (GTree *tree,
GTraverseFunc traverse_func,
GTraverseType traverse_type,
gpointer user_data);
gboolean (*GTraverseFunc) (gpointer key,
gpointer value,
gpointer data);
enum GTraverseType;
gpointer g_tree_search (GTree *tree,
GCompareFunc search_func,
gconstpointer user_data);
gboolean g_tree_remove (GTree *tree,
gconstpointer key);
gboolean g_tree_steal (GTree *tree,
gconstpointer key);
void g_tree_destroy (GTree *tree);
Description
The GTree structure and its associated functions provide a sorted collection of key/value pairs opti-
mized for searching and traversing in order.
To create a new GTree use g_tree_new().
To insert a key/value pair into a GTree use g_tree_insert().
To lookup the value corresponding to a given key, use g_tree_lookup() and g_tree_lookup_extended().
To find out the number of nodes in a GTree, use g_tree_nnodes(). To get the height of a GTree, use
g_tree_height().
To traverse a GTree, calling a function for each node visited in the traversal, use g_tree_foreach().
To remove a key/value pair use g_tree_remove().
To destroy a GTree, use g_tree_destroy().
Details
GTree
The GTree struct is an opaque data structure representing a Balanced Binary Tree. It should be
accessed only by using the following functions.
553
CHAPTER 5. GLIB DATA TYPES 5.14. BALANCED BINARY TREES
g_tree_new ()
key_compare_func : the function used to order the nodes in the GTree. It should return values similar
to the standard strcmp() function - 0 if the two arguments are equal, a negative value if the first
argument comes before the second, or a positive value if the first argument comes after the second.
g_tree_new_with_data ()
Creates a new GTree with a comparison function that accepts user data. See g_tree_new() for more
details.
g_tree_new_full ()
Creates a new GTree like g_tree_new() and allows to specify functions to free the memory allocated
for the key and value that get called when removing the entry from the GTree.
key_destroy_func : a function to free the memory allocated for the key used when removing the entry
from the GTree or NULL if you don’t want to supply such a function.
value_destroy_func : a function to free the memory allocated for the value used when removing the
entry from the GTree or NULL if you don’t want to supply such a function.
g_tree_insert ()
554
CHAPTER 5. GLIB DATA TYPES 5.14. BALANCED BINARY TREES
Inserts a key/value pair into a GTree. If the given key already exists in the GTree its corresponding
value is set to the new value. If you supplied a value_destroy_func when creating the GTree, the old
value is freed using that function. If you supplied a key_destroy_func when creating the GTree, the
passed key is freed using that function.
The tree is automatically ’balanced’ as new key/value pairs are added, so that the distance from the
root to every leaf is as small as possible.
tree : a GTree.
g_tree_replace ()
Inserts a new key and value into a GTree similar to g_tree_insert(). The difference is that if the key
already exists in the GTree, it gets replaced by the new key. If you supplied a value_destroy_func
when creating the GTree, the old value is freed using that function. If you supplied a key_destroy_fu-
nc when creating the GTree, the old key is freed using that function.
The tree is automatically ’balanced’ as new key/value pairs are added, so that the distance from the
root to every leaf is as small as possible.
tree : a GTree.
g_tree_nnodes ()
g_tree_height ()
g_tree_lookup ()
Gets the value corresponding to the given key. Since a GTree is automatically balanced as key/value
pairs are added, key lookup is very fast.
tree : a GTree.
Returns : the value corresponding to the key, or NULL if the key was not found.
555
CHAPTER 5. GLIB DATA TYPES 5.14. BALANCED BINARY TREES
g_tree_lookup_extended ()
Looks up a key in the GTree, returning the original key and the associated value and a gboolean
which is TRUE if the key was found. This is useful if you need to free the memory allocated for the
original key, for example before calling g_tree_remove().
tree : a GTree.
g_tree_foreach ()
Calls the given function for each of the key/value pairs in the GTree. The function is passed the key
and value of each pair, and the given data parameter. The tree is traversed in sorted order.
The tree may not be modified while iterating over it (you can’t add/remove items). To remove all
items matching a predicate, you need to add each item to a list in your GTraverseFunc as you walk over
the tree, then walk the list and remove each item.
tree : a GTree.
func : the function to call for each node visited. If this function returns TRUE, the traversal is stopped.
g_tree_traverse ()
WARNING
g_tree_traverse has been deprecated since version 2.2 and should not be used in
newly-written code. The order of a balanced tree is somewhat arbitrary. If you just want
to visit all nodes in sorted order, use g_tree_foreach() instead. If you really need to visit
nodes in a different order, consider using an N-ary Tree.
tree : a GTree.
556
CHAPTER 5. GLIB DATA TYPES 5.14. BALANCED BINARY TREES
traverse_func : the function to call for each node visited. If this function returns TRUE, the traversal
is stopped.
traverse_type : the order in which nodes are visited, one of G_IN_ORDER, G_PRE_ORDER and
G_POST_ORDER.
user_data : user data to pass to the function.
GTraverseFunc ()
Specifies the type of function passed to g_tree_traverse(). It is passed the key and value of each node,
together with the user_data parameter passed to g_tree_traverse(). If the function returns TRUE, the
traversal is stopped.
key : a key of a GTree node.
enum GTraverseType
typedef enum
{
G_IN_ORDER,
G_PRE_ORDER,
G_POST_ORDER,
G_LEVEL_ORDER
} GTraverseType;
g_tree_search ()
557
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
tree : a GTree.
user_data : the data passed as the second argument to the search_func function.
Returns : the value corresponding to the found key, or NULL if the key was not found.
g_tree_remove ()
tree : a GTree.
Returns : TRUE if the key was found (prior to 2.8, this function returned nothing)
g_tree_steal ()
Removes a key and its associated value from a GTree without calling the key and value destroy
functions.
If the key does not exist in the GTree, the function does nothing.
tree : a GTree.
Returns : TRUE if the key was found (prior to 2.8, this function returned nothing)
g_tree_destroy ()
Destroys the GTree. If keys and/or values are dynamically allocated, you should either free them
first or create the GTree using g_tree_new_full(). In the latter case the destroy functions you supplied
will be called on all keys and values before destroying the GTree.
tree : a GTree.
558
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
Synopsis
#include <glib.h>
GNode;
GNode* g_node_new (gpointer data);
GNode* g_node_copy (GNode *node);
gpointer (*GCopyFunc) (gconstpointer src,
gpointer data);
GNode* g_node_copy_deep (GNode *node,
GCopyFunc copy_func,
gpointer data);
559
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
Description
The GNode struct and its associated functions provide a N-ary tree data structure, where nodes in the
tree can contain arbitrary data.
To create a new tree use g_node_new().
To insert a node into a tree use g_node_insert(), g_node_insert_before(), g_node_append() and g_node_prepend().
To create a new node and insert it into a tree use g_node_insert_data(), g_node_insert_data_before(),
g_node_append_data() and g_node_prepend_data().
To reverse the children of a node use g_node_reverse_children().
To find a node use g_node_get_root(), g_node_find(), g_node_find_child(), g_node_child_index(),
g_node_child_position(), g_node_first_child(), g_node_last_child(), g_node_nth_child(), g_node_first_sibling(),
g_node_prev_sibling(), g_node_next_sibling() or g_node_last_sibling().
To get information about a node or tree use G_NODE_IS_LEAF(), G_NODE_IS_ROOT(), g_node_depth(),
g_node_n_nodes(), g_node_n_children(), g_node_is_ancestor() or g_node_max_height().
To traverse a tree, calling a function for each node visited in the traversal, use g_node_traverse() or
g_node_children_foreach().
To remove a node or subtree from a tree use g_node_unlink() or g_node_destroy().
Details
GNode
typedef struct {
gpointer data;
GNode *next;
GNode *prev;
GNode *parent;
GNode *children;
} GNode;
560
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
GNode *next; points to the node’s next sibling (a sibling is another GNode with the same parent).
GNode *parent; points to the parent of the GNode, or is NULL if the GNode is the root of the tree.
GNode *children; The children field points to the first child of the GNode. The other children are
accessed by using the next pointer of each child.
g_node_new ()
Creates a new GNode containing the given data. Used to create the first node in a tree.
g_node_copy ()
Recursively copies a GNode (but does not deep-copy the data inside the nodes, see g_node_copy_deep()
if you need that).
node : a GNode
GCopyFunc ()
A function of this signature is used to copy the node data when doing a deep-copy of a tree.
Since 2.4
g_node_copy_deep ()
node : a GNode
copy_func : the function which is called to copy the data inside each node, or NULL to use the original
data.
Since 2.4
561
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
g_node_insert ()
position : the position to place node at, with respect to its siblings If position is -1, node is inserted as
the last child of parent
g_node_insert_before ()
sibling : the sibling GNode to place node before. If sibling is NULL, the node is inserted as the last
child of parent.
g_node_insert_after ()
sibling : the sibling GNode to place node after. If sibling is NULL, the node is inserted as the first
child of parent.
g_node_append()
562
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
g_node_prepend ()
g_node_insert_data()
position : the position to place the new GNode at. If position is -1, the new GNode is inserted as the
last child of parent
data : the data for the new GNode
g_node_insert_data_before()
g_node_append_data()
g_node_prepend_data()
563
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
g_node_reverse_children ()
Reverses the order of the children of a GNode. (It doesn’t change the order of the grandchildren.)
node : a GNode.
g_node_traverse ()
Traverses a tree starting at the given root GNode. It calls the given function for each node visited.
The traversal can be halted at any point by returning TRUE from func.
order : the order in which nodes are visited - G_IN_ORDER, G_PRE_ORDER, G_POST_ORDER, or
G_LEVEL_ORDER.
max_depth : the maximum depth of the traversal. Nodes below this depth will not be visited. If
max_depth is -1 all nodes in the tree are visited. If depth is 1, only the root is visited. If depth
is 2, the root and its children are visited. And so on.
enum GTraverseFlags
typedef enum
{
G_TRAVERSE_LEAVES = 1 << 0,
G_TRAVERSE_NON_LEAVES = 1 << 1,
G_TRAVERSE_ALL = G_TRAVERSE_LEAVES | G_TRAVERSE_NON_LEAVES,
G_TRAVERSE_MASK = 0x03,
G_TRAVERSE_LEAFS = G_TRAVERSE_LEAVES,
G_TRAVERSE_NON_LEAFS = G_TRAVERSE_NON_LEAVES
} GTraverseFlags;
Specifies which nodes are visited during several of the tree functions, including g_node_traverse()
and g_node_find().
G_TRAVERSE_LEAVES only leaf nodes should be visited. This name has been introduced in 2.6, for
older version use G_TRAVERSE_LEAFS.
G_TRAVERSE_NON_LEAVES only non-leaf nodes should be visited. This name has been introduced in
2.6, for older version use G_TRAVERSE_NON_LEAFS.
564
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
GNodeTraverseFunc ()
Specifies the type of function passed to g_node_traverse(). The function is called with each of the
nodes visited, together with the user data passed to g_node_traverse(). If the function returns TRUE,
then the traversal is stopped.
node : a GNode.
g_node_children_foreach ()
Calls a function for each of the children of a GNode. Note that it doesn’t descend beneath the child
nodes.
node : a GNode
GNodeForeachFunc ()
Specifies the type of function passed to g_node_children_foreach(). The function is called with each
child node, together with the user data passed to g_node_children_foreach().
node : a GNode.
g_node_get_root ()
node : a GNode
565
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
g_node_find ()
order : the order in which nodes are visited - G_IN_ORDER, G_PRE_ORDER, G_POST_ORDER, or
G_LEVEL_ORDER
g_node_find_child ()
node : a GNode
Returns : the found child GNode, or NULL if the data is not found
g_node_child_index ()
Gets the position of the first child of a GNode which contains the given data.
node : a GNode
Returns : the index of the child of node which contains data, or -1 if the data is not found
g_node_child_position ()
Gets the position of a GNode with respect to its siblings. child must be a child of node. The first
child is numbered 0, the second 1, and so on.
node : a GNode
566
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
g_node_first_child()
#define g_node_first_child(node)
Returns : the first child of node, or NULL if node is NULL or has no children
g_node_last_child ()
g_node_nth_child ()
Gets a child of a GNode, using the given index. The first child is at index 0. If the index is too big,
NULL is returned.
node : a GNode
g_node_first_sibling ()
Gets the first sibling of a GNode. This could possibly be the node itself.
node : a GNode
g_node_next_sibling()
#define g_node_next_sibling(node)
g_node_prev_sibling()
#define g_node_prev_sibling(node)
567
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
g_node_last_sibling ()
Gets the last sibling of a GNode. This could possibly be the node itself.
node : a GNode
G_NODE_IS_LEAF()
G_NODE_IS_ROOT()
#define G_NODE_IS_ROOT(node)
Returns : TRUE if the GNode is the root of a tree (i.e. it has no parent or siblings)
g_node_depth ()
g_node_n_nodes ()
g_node_n_children ()
568
CHAPTER 5. GLIB DATA TYPES 5.15. N-ARY TREES
g_node_is_ancestor ()
Returns TRUE if node is an ancestor of descendant. This is true if node is the parent of descendant,
or if node is the grandparent of descendant etc.
node : a GNode
descendant : a GNode
g_node_max_height ()
Gets the maximum height of all branches beneath a GNode. This is the maximum distance from the
GNode to all leaf nodes.
If root is NULL, 0 is returned. If root has no children, 1 is returned. If root has children, 2 is
returned. And so on.
root : a GNode
g_node_unlink ()
node : the GNode to unlink, which becomes the root of a new tree
g_node_destroy ()
Removes root and its children from the tree, freeing any memory allocated.
g_node_push_allocator ()
WARNING
g_node_push_allocator has been deprecated since version 2.10 and should not
be used in newly-written code. It does nothing, since GNode has been converted to the
slice allocator
Sets the allocator to use to allocate GNode elements. Use g_node_pop_allocator() to restore the
previous allocator.
Note that this function is not available if GLib has been compiled with --disable-mem-pools
569
CHAPTER 5. GLIB DATA TYPES 5.16. QUARKS
g_node_pop_allocator ()
WARNING
g_node_pop_allocator has been deprecated since version 2.10 and should not be
used in newly-written code. It does nothing, since GNode has been converted to the slice
allocator
5.16 Quarks
Name
Quarks – a 2-way association between a string and a unique integer identifier
Synopsis
#include <glib.h>
typedef GQuark;
GQuark g_quark_from_string (const gchar *string);
GQuark g_quark_from_static_string (const gchar *string);
const gchar* g_quark_to_string (GQuark quark);
GQuark g_quark_try_string (const gchar *string);
const gchar* g_intern_string (const gchar *string);
const gchar* g_intern_static_string (const gchar *string);
Description
Quarks are associations between strings and integer identifiers. Given either the string or the GQuark
identifier it is possible to retrieve the other.
Quarks are used for both Datasets and Keyed Data Lists.
To create a new quark from a string, use g_quark_from_string() or g_quark_from_static_string().
To find the string corresponding to a given GQuark, use g_quark_to_string().
To find the GQuark corresponding to a given string, use g_quark_try_string().
Another use for the string pool maintained for the quark functions is string interning, using g_intern_string()
or g_intern_static_string(). An interned string is a canonical representation for a string. One important
advantage of interned strings is that they can be compared for equality by a simple pointer comparision,
rather than using strcmp().
Details
GQuark
A GQuark is a non-zero integer which uniquely identifies a particular string. A GQuark value of
zero is associated to NULL.
570
CHAPTER 5. GLIB DATA TYPES 5.16. QUARKS
g_quark_from_string ()
Gets the GQuark identifying the given string. If the string does not currently have an associated
GQuark, a new GQuark is created, using a copy of the string.
string : a string.
g_quark_from_static_string ()
Gets the GQuark identifying the given (static) string. If the string does not currently have an associ-
ated GQuark, a new GQuark is created, linked to the given string.
Note that this function is identical to g_quark_from_string() except that if a new GQuark is created
the string itself is used rather than a copy. This saves memory, but can only be used if the string will
always exist. It can be used with statically allocated strings in the main program, but not with statically
allocated memory in dynamically loaded modules, if you expect to ever unload the module again (e.g.
do not use this function in GTK+ theme engines).
string : a string.
g_quark_to_string ()
quark : a GQuark.
g_quark_try_string ()
Gets the GQuark associated with the given string, or 0 if string is NULL or it has no associated
GQuark.
If you want the GQuark to be created if it doesn’t already exist, use g_quark_from_string() or g_quark_from_static_
string : a string.
Returns : the GQuark associated with the string, or 0 if string is NULL or there is no GQuark associ-
ated with it.
g_intern_string ()
Returns a canonical representation for string . Interned strings can be compared for equality by
comparing the pointers, instead of using strcmp().
string : a string
Since 2.10
571
CHAPTER 5. GLIB DATA TYPES 5.17. KEYED DATA LISTS
g_intern_static_string ()
Returns a canonical representation for string . Interned strings can be compared for equality by
comparing the pointers, instead of using strcmp(). g_intern_static_string() does not copy the string,
therefore string must not be freed or modified.
string : a static string
Synopsis
#include <glib.h>
GData;
void g_datalist_init (GData **datalist);
Description
Keyed data lists provide lists of arbitrary data elements which can be accessed either with a string or
with a GQuark corresponding to the string.
572
CHAPTER 5. GLIB DATA TYPES 5.17. KEYED DATA LISTS
The GQuark methods are quicker, since the strings have to be converted to GQuarks anyway.
Data lists are used for associating arbitrary data with GObjects, using g_object_set_data() and related
functions.
To create a datalist, use g_datalist_init().
To add data elements to a datalist use g_datalist_id_set_data(), g_datalist_id_set_data_full(), g_datalist_set_data()
and g_datalist_set_data_full().
To get data elements from a datalist use g_datalist_id_get_data() and g_datalist_get_data().
To iterate over all data elements in a datalist use g_datalist_foreach() (not thread-safe).
To remove data elements from a datalist use g_datalist_id_remove_data() and g_datalist_remove_data().
To remove all data elements from a datalist, use g_datalist_clear().
Details
GData
The GData struct is an opaque data structure to represent a Keyed Data List. It should only be
accessed via the following functions.
g_datalist_init ()
Resets the datalist to NULL. It does not free any memory or call any destroy functions.
datalist : a pointer to a pointer to a datalist.
g_datalist_id_set_data()
#define g_datalist_id_set_data(dl, q, d)
Sets the data corresponding to the given GQuark id. Any previous data with the same key is re-
moved, and its destroy function is called.
dl : a datalist.
g_datalist_id_set_data_full ()
Sets the data corresponding to the given GQuark id, and the function to be called when the element
is removed from the datalist. Any previous data with the same key is removed, and its destroy function
is called.
datalist : a datalist.
data : the data element or NULL to remove any previous element corresponding to key_id .
destroy_func : the function to call when the data element is removed. This function will be called
with the data element and can be used to free any memory allocated for it. If data is NULL, then
destroy_func must also be NULL.
573
CHAPTER 5. GLIB DATA TYPES 5.17. KEYED DATA LISTS
g_datalist_id_get_data ()
g_datalist_id_remove_data()
#define g_datalist_id_remove_data(dl, q)
g_datalist_id_remove_no_notify ()
g_datalist_set_data()
#define g_datalist_set_data(dl, k, d)
g_datalist_set_data_full()
#define g_datalist_set_data_full(dl, k, d, f)
Sets the data element corresponding to the given string identifier, and the function to be called when
the data element is removed.
dl : a datalist.
f : the function to call when the data element is removed. This function will be called with the data
element and can be used to free any memory allocated for it. If d is NULL, then f must also be
NULL.
574
CHAPTER 5. GLIB DATA TYPES 5.17. KEYED DATA LISTS
g_datalist_get_data()
#define g_datalist_get_data(dl, k)
Gets a data element, using its string identifer. This is slower than g_datalist_id_get_data() because
the string is first converted to a GQuark.
dl : a datalist.
g_datalist_remove_data()
#define g_datalist_remove_data(dl, k)
Removes an element using its string identifier. The data element’s destroy function is called if it has
been set.
dl : a datalist.
g_datalist_remove_no_notify()
#define g_datalist_remove_no_notify(dl, k)
dl : a datalist.
g_datalist_foreach ()
Calls the given function for each data element of the datalist. The function is called with each data
element’s GQuark id and data, together with the given user_data parameter. Note that this function
is NOT thread-safe. So unless datalist can be protected from any modifications during invocation of
this function, it should not be called.
datalist : a datalist.
g_datalist_clear ()
Frees all the data elements of the datalist. The data elements’ destroy functions are called if they
have been set.
datalist : a datalist.
575
CHAPTER 5. GLIB DATA TYPES 5.18. DATASETS
g_datalist_set_flags ()
Turns on flag values for a data list. This function is used to keep a small number of boolean flags
in an object with a data list without using any additional space. It is not generally useful except in
circumstances where space is very tight. (It is used in the base GObject type, for example.)
flags : the flags to turn on. The values of the flags are restricted by G_DATALIST_FLAGS_MASK
(currently 3; giving two possible boolean flags). A value for flags that doesn’t fit within the mask
is an error.
Since 2.8
g_datalist_unset_flags ()
flags : the flags to turn off. The values of the flags are restricted by G_DATALIST_FLAGS_MASK
(currently 3: giving two possible boolean flags). A value for flags that doesn’t fit within the mask
is an error.
Since 2.8
g_datalist_get_flags ()
Gets flags values packed in together with the datalist. See g_datalist_set_flags().
Since 2.8
G_DATALIST_FLAGS_MASK
A bitmask that restricts the possible flags passed to g_datalist_set_flags(). Passing a flags value where
flags & ~G_DATALIST_FLAGS_MASK != 0 is an error.
5.18 Datasets
Name
Datasets – associate groups of data elements with particular memory locations
576
CHAPTER 5. GLIB DATA TYPES 5.18. DATASETS
Synopsis
#include <glib.h>
Description
Datasets associate groups of data elements with particular memory locations. These are useful if you
need to associate data with a structure returned from an external library. Since you cannot modify the
structure, you use its location in memory as the key into a dataset, where you can associate any number
of data elements with it.
There are two forms of most of the dataset functions. The first form uses strings to identify the data
elements associated with a location. The second form uses GQuark identifiers, which are created with a
call to g_quark_from_string() or g_quark_from_static_string(). The second form is quicker, since it does
not require looking up the string in the hash table of GQuark identifiers.
There is no function to create a dataset. It is automatically created as soon as you add elements to it.
To add data elements to a dataset use g_dataset_id_set_data(), g_dataset_id_set_data_full(), g_dataset_set_data()
and g_dataset_set_data_full().
To get data elements from a dataset use g_dataset_id_get_data() and g_dataset_get_data().
To iterate over all data elements in a dataset use g_dataset_foreach() (not thread-safe).
To remove data elements from a dataset use g_dataset_id_remove_data() and g_dataset_remove_data().
To destroy a dataset, use g_dataset_destroy().
Details
g_dataset_id_set_data()
#define g_dataset_id_set_data(l, k, d)
Sets the data element associated with the given GQuark id. Any previous data with the same key is
removed, and its destroy function is called.
577
CHAPTER 5. GLIB DATA TYPES 5.18. DATASETS
g_dataset_id_set_data_full ()
Sets the data element associated with the given GQuark id, and also the function to call when the
data element is destroyed. Any previous data with the same key is removed, and its destroy function is
called.
destroy_func : the function to call when the data element is removed. This function will be called with
the data element and can be used to free any memory allocated for it.
GDestroyNotify ()
Specifies the type of function which is called when a data element is destroyed. It is passed the
pointer to the data element and should free any memory and resources allocated for it.
g_dataset_id_get_data ()
Returns : the data element corresponding to the GQuark, or NULL if it is not found.
g_dataset_id_remove_data()
#define g_dataset_id_remove_data(l, k)
Removes a data element from a dataset. The data element’s destroy function is called if it has been
set.
578
CHAPTER 5. GLIB DATA TYPES 5.18. DATASETS
g_dataset_id_remove_no_notify ()
g_dataset_set_data()
#define g_dataset_set_data(l, k, d)
g_dataset_set_data_full()
#define g_dataset_set_data_full(l, k, d, f)
Sets the data corresponding to the given string identifier, and the function to call when the data
element is destroyed.
f : the function to call when the data element is removed. This function will be called with the data
element and can be used to free any memory allocated for it.
g_dataset_get_data()
#define g_dataset_get_data(l, k)
Returns : the data element corresponding to the string, or NULL if it is not found.
g_dataset_remove_data()
#define g_dataset_remove_data(l, k)
Removes a data element corresponding to a string. Its destroy function is called if it has been set.
579
CHAPTER 5. GLIB DATA TYPES 5.19. RELATIONS AND TUPLES
g_dataset_remove_no_notify()
#define g_dataset_remove_no_notify(l, k)
g_dataset_foreach ()
Calls the given function for each data element which is associated with the given location. Note that
this function is NOT thread-safe. So unless datalist can be protected from any modifications during
invocation of this function, it should not be called.
GDataForeachFunc ()
Specifies the type of function passed to g_dataset_foreach(). It is called with each GQuark id and
associated data element, together with the user_data parameter supplied to g_dataset_foreach().
g_dataset_destroy ()
Destroys the dataset, freeing all memory allocated, and calling any destroy functions set for data
elements.
580
CHAPTER 5. GLIB DATA TYPES 5.19. RELATIONS AND TUPLES
Synopsis
#include <glib.h>
GRelation;
GRelation* g_relation_new (gint fields);
void g_relation_index (GRelation *relation,
gint field,
GHashFunc hash_func,
GEqualFunc key_equal_func);
void g_relation_insert (GRelation *relation,
...);
gboolean g_relation_exists (GRelation *relation,
...);
gint g_relation_count (GRelation *relation,
gconstpointer key,
gint field);
GTuples* g_relation_select (GRelation *relation,
gconstpointer key,
gint field);
gint g_relation_delete (GRelation *relation,
gconstpointer key,
gint field);
void g_relation_destroy (GRelation *relation);
GTuples;
void g_tuples_destroy (GTuples *tuples);
gpointer g_tuples_index (GTuples *tuples,
gint index_,
gint field);
Description
A GRelation is a table of data which can be indexed on any number of fields, rather like simple database
tables. A GRelation contains a number of records, called tuples. Each record contains a number of fields.
Records are not ordered, so it is not possible to find the record at a particular index.
Note that GRelation tables are currently limited to 2 fields.
To create a GRelation, use g_relation_new().
To specify which fields should be indexed, use g_relation_index(). Note that this must be called
before any tuples are added to the GRelation.
To add records to a GRelation use g_relation_insert().
To determine if a given record appears in a GRelation, use g_relation_exists(). Note that fields are
compared directly, so pointers must point to the exact same position (i.e. different copies of the same
string will not match.)
To count the number of records which have a particular value in a given field, use g_relation_count().
To get all the records which have a particular value in a given field, use g_relation_select(). To access
fields of the resulting records, use g_tuples_index(). To free the resulting records use g_tuples_destroy().
To delete all records which have a particular value in a given field, use g_relation_delete().
To destroy the GRelation, use g_relation_destroy().
To help debug GRelation objects, use g_relation_print().
Details
GRelation
581
CHAPTER 5. GLIB DATA TYPES 5.19. RELATIONS AND TUPLES
The GRelation struct is an opaque data structure to represent a Relation. It should only be accessed
via the following functions.
g_relation_new ()
Creates a new GRelation with the given number of fields. Note that currently the number of fields
must be 2.
g_relation_index ()
Creates an index on the given field. Note that this must be called before any records are added to the
GRelation.
relation : a GRelation.
g_relation_insert ()
relation : a GRelation.
... : the fields of the record to add. These must match the number of fields in the GRelation, and of
type gpointer or gconstpointer.
g_relation_exists ()
Returns TRUE if a record with the given values exists in a GRelation. Note that the values are
compared directly, so that, for example, two copies of the same string will not match.
relation : a GRelation.
... : the fields of the record to compare. The number must match the number of fields in the GRelation.
582
CHAPTER 5. GLIB DATA TYPES 5.19. RELATIONS AND TUPLES
g_relation_count ()
Returns the number of tuples in a GRelation that have the given value in the given field.
relation : a GRelation.
g_relation_select ()
Returns all of the tuples which have the given key in the given field. Use g_tuples_index() to access
the returned records. The returned records should be freed with g_tuples_destroy().
relation : a GRelation.
g_relation_delete ()
Deletes any records from a GRelation that have the given key value in the given field.
relation : a GRelation.
g_relation_destroy ()
Destroys the GRelation, freeing all memory allocated. However, it does not free memory allocated
for the tuple data, so you should free that first if appropriate.
relation : a GRelation.
g_relation_print ()
Outputs information about all records in a GRelation, as well as the indexes. It is for debugging.
relation : a GRelation.
583
CHAPTER 5. GLIB DATA TYPES 5.20. CACHES
GTuples
typedef struct {
guint len;
} GTuples;
The GTuples struct is used to return records (or tuples) from the GRelation by g_relation_select(). It
only contains one public member - the number of records that matched. To access the matched records,
you must use g_tuples_index().
guint len; the number of records that matched.
g_tuples_destroy ()
Frees the records which were returned by g_relation_select(). This should always be called after
g_relation_select() when you are finished with the records. The records are not removed from the GRe-
lation.
tuples : the tuple data to free.
g_tuples_index ()
Gets a field from the records returned by g_relation_select(). It returns the given field of the record
at the given index. The returned value should not be changed.
tuples : the tuple data, returned by g_relation_select().
5.20 Caches
Name
Caches – caches allow sharing of complex data structures to save resources
Synopsis
#include <glib.h>
GCache;
GCache* g_cache_new (GCacheNewFunc value_new_func,
GCacheDestroyFunc value_destroy_fu
GCacheDupFunc key_dup_func,
GCacheDestroyFunc key_destroy_func
GHashFunc hash_key_func,
GHashFunc hash_value_func,
GEqualFunc key_equal_func);
gpointer g_cache_insert (GCache *cache,
gpointer key);
void g_cache_remove (GCache *cache,
584
CHAPTER 5. GLIB DATA TYPES 5.20. CACHES
gconstpointer value);
void g_cache_destroy (GCache *cache);
Description
A GCache allows sharing of complex data structures, in order to save system resources.
GTK+ uses caches for GtkStyles and GdkGCs. These consume a lot of resources, so a GCache is used
to see if a GtkStyle or GdkGC with the required properties already exists. If it does, then the existing
object is used instead of creating a new one.
GCache uses keys and values. A GCache key describes the properties of a particular resource. A
GCache value is the actual resource.
Details
GCache
The GCache struct is an opaque data structure containing information about a GCache. It should
only be accessed via the following functions.
g_cache_new ()
value_new_func : a function to create a new object given a key. This is called by g_cache_insert() if an
object with the given key does not already exist.
key_dup_func : a function to copy a key. It is called by g_cache_insert() if the key does not already exist
in the GCache.
585
CHAPTER 5. GLIB DATA TYPES 5.20. CACHES
key_equal_func : a function to compare two keys. It should return TRUE if the two keys are equiva-
lent.
Returns : a new GCache.
g_cache_insert ()
Gets the value corresponding to the given key, creating it if necessary. It first checks if the value
already exists in the GCache, by using the key_equal_func function passed to g_cache_new(). If it does
already exist it is returned, and its reference count is increased by one. If the value does not currently
exist, if is created by calling the value_new_func. The key is duplicated by calling key_dup_func and
the duplicated key and value are inserted into the GCache.
cache : a GCache.
g_cache_remove ()
Decreases the reference count of the given value. If it drops to 0 then the value and its corresponding
key are destroyed, using the value_destroy_func and key_destroy_func passed to g_cache_new().
cache : a GCache.
g_cache_destroy ()
g_cache_key_foreach ()
Calls the given function for each of the keys in the GCache.
N OTE
func is passed three parameters, the value and key of a cache entry and the user_da-
ta. The order of value and key is different from the order in which g_hash_table_foreach()
passes key-value pairs to its callback function !
586
CHAPTER 5. GLIB DATA TYPES 5.20. CACHES
cache : a GCache.
g_cache_value_foreach ()
WARNING
g_cache_value_foreach has been deprecated since version 2.10 and should not
be used in newly-written code. The reason is that it passes pointers to internal data
structures to func; use g_cache_key_foreach() instead
Calls the given function for each of the values in the GCache.
cache : a GCache.
GCacheDestroyFunc ()
Specifies the type of the value_destroy_func and key_destroy_func functions passed to g_cache_new().
The functions are passed a pointer to the GCache key or GCache value and should free any memory and
other resources associated with it.
GCacheDupFunc ()
Specifies the type of the key_dup_func function passed to g_cache_new(). The function is passed a
key (not a value as the prototype implies) and should return a duplicate of the key.
GCacheNewFunc ()
Specifies the type of the value_new_func function passed to g_cache_new(). It is passed a GCache
key and should create the value corresponding to the key.
587
CHAPTER 5. GLIB DATA TYPES 5.21. MEMORY ALLOCATORS
Synopsis
#include <glib.h>
GAllocator;
GAllocator* g_allocator_new (const gchar *name,
guint n_preallocs);
void g_allocator_free (GAllocator *allocator);
Description
Prior to 2.10, GAllocator was used as an efficient way to allocate small pieces of memory for use with
the GList, GSList and GNode data structures. Since 2.10, it has been completely replaced by the slice
allocator and deprecated.
Details
GAllocator
WARNING
The GAllocator struct contains private data. and should only be accessed using the following func-
tions.
g_allocator_new ()
WARNING
g_allocator_new has been deprecated since version 2.10 and should not be used
in newly-written code. Use the slice allocator instead
588
CHAPTER 5. GLIB DATA TYPES 5.21. MEMORY ALLOCATORS
g_allocator_free ()
WARNING
g_allocator_free has been deprecated since version 2.10 and should not be used
in newly-written code. Use the slice allocator instead
589
Chapter 6
GLib Tools
6.1 glib-gettextize
Name
glib-gettextize – gettext internationalization utility
Synopsis
glib-gettextize [option...] [directory]
Description
glib-gettextize helps to prepare a source package for being internationalized through gettext. It is a
variant of the gettextize that ships with gettext.
glib-gettextize differs from gettextize in that it doesn’t create an intl/ subdirectory and doesn’t
modify po/ChangeLog (note that newer versions of gettextize behave like this when called with the
--no-changelog option).
Options
-f, --force force writing of new files even if old ones exist
See also
gettextize(1)
6.2 gtester
Name
gtester – test running utility
Synopsis
gtester [option...] [testprogram]
591
CHAPTER 6. GLIB TOOLS 6.3. GTESTER-REPORT
Description
gtester is a utility to run unit tests that have been written using the GLib test framework.
When called with the -o option, gtester writes an XML report of the test results, which can be con-
verted into HTML using the gtester-report utility.
Options
-m=MODE run test cases in MODE , which can be perf, slow, thorough or quick. The default mode is quick.
-p=TESTPATH only run test cases matching TESTPATH
--seed=SEEDSTRING run all test cases with random number seed SEEDSTRING
See also
gtester-report(1)
6.3 gtester-report
Name
gtester-report – test report formatting utility
Synopsis
gtester-report [option...] [gtester-log]
Description
gtester-report is a script which converts the XML output generated by gtester into HTML.
Options
See also
gtester(1)
592
Index
A g_assert_cmphex, 447
ABS, 46 g_assert_cmpint, 446
g_access, 342 g_assert_cmpstr, 446
G_ALLOC_AND_FREE, 461 g_assert_cmpuint, 446
G_ALLOC_ONLY, 461 g_assert_error, 447
g_alloca, 151 g_assert_no_error, 447
GAllocator, 588 g_assert_not_reached, 446
g_allocator_free, 589 g_async_queue_length, 140
g_allocator_new, 588 g_async_queue_length_unlocked, 143
GArray, 536 g_async_queue_lock, 140
g_array_append_val, 538 g_async_queue_new, 138
g_array_append_vals, 538 g_async_queue_new_full, 138
g_array_free, 541 g_async_queue_pop, 139
g_array_get_element_size, 538 g_async_queue_pop_unlocked, 142
g_array_index, 541 g_async_queue_push, 138
g_array_insert_val, 539 g_async_queue_push_sorted, 139
g_array_insert_vals, 539 g_async_queue_push_sorted_unlocked, 141
g_array_new, 537 g_async_queue_push_unlocked, 141
g_array_prepend_val, 538 g_async_queue_ref, 138
g_array_prepend_vals, 539 g_async_queue_ref_unlocked, 141
g_array_ref, 537 g_async_queue_sort, 140
g_array_remove_index, 540 g_async_queue_sort_unlocked, 143
g_array_remove_index_fast, 540 g_async_queue_timed_pop, 139
g_array_remove_range, 540 g_async_queue_timed_pop_unlocked, 142
g_array_set_size, 541 g_async_queue_try_pop, 139
g_array_sized_new, 537 g_async_queue_try_pop_unlocked, 142
g_array_sort, 540 g_async_queue_unlock, 140
g_array_sort_with_data, 541 g_async_queue_unref, 138
g_array_unref, 537 g_async_queue_unref_and_unlock, 141
g_ascii_digit_value, 202 GAsyncQueue, 137
g_ascii_dtostr, 208 g_atexit, 302
G_ASCII_DTOSTR_BUF_SIZE, 207 g_atomic_int_add, 74
g_atomic_int_compare_and_exchange, 75
g_ascii_formatd, 208
g_atomic_int_dec_and_test, 76
g_ascii_isalnum, 199
g_atomic_int_exchange_and_add, 75
g_ascii_isalpha, 200
g_atomic_int_get, 74
g_ascii_iscntrl, 200
g_atomic_int_inc, 75
g_ascii_isdigit, 200
g_atomic_int_set, 74
g_ascii_isgraph, 200
g_atomic_pointer_compare_and_exchange, 75
g_ascii_islower, 201
g_atomic_pointer_get, 75
g_ascii_isprint, 201
g_atomic_pointer_set, 75
g_ascii_ispunct, 201
g_ascii_isspace, 201 B
g_ascii_isupper, 202 g_base64_decode, 251
g_ascii_isxdigit, 202 g_base64_decode_inplace, 251
g_ascii_strcasecmp, 202 g_base64_decode_step, 250
g_ascii_strdown, 203 g_base64_encode, 250
g_ascii_strncasecmp, 203 g_base64_encode_close, 250
g_ascii_strtod, 207 g_base64_encode_step, 249
g_ascii_strtoll, 206 g_basename, 298
g_ascii_strtoull, 207 G_BEGIN_DECLS, 65
g_ascii_strup, 203 G_BIG_ENDIAN, 52
g_ascii_tolower, 204 g_bit_nth_lsf, 301
g_ascii_toupper, 204 g_bit_nth_msf, 302
g_ascii_xdigit_value, 202 g_bit_storage, 302
g_assert, 445 g_blow_chunks, 466
g_assert_cmpfloat, 447 g_bookmark_file_add_application, 434
593
INDEX INDEX
594
INDEX INDEX
595
INDEX INDEX
596
INDEX INDEX
597
INDEX INDEX
598
INDEX INDEX
599
INDEX INDEX
g_main_pending, 86 G_MAXSIZE, 43
g_main_quit, 83 G_MAXSSIZE, 43
g_main_run, 83 G_MAXUINT, 41
g_main_set_poll_func, 91 G_MAXUINT16, 42
GMainContext, 84 G_MAXUINT32, 43
GMainLoop, 81 G_MAXUINT64, 43
g_malloc, 150 G_MAXUINT8, 42
g_malloc0, 150 G_MAXULONG, 41
g_mapped_file_free, 338 G_MAXUSHORT, 41
g_mapped_file_get_contents, 338 G_MEM_ALIGN, 47
g_mapped_file_get_length, 338 g_mem_chunk_alloc, 462
g_mapped_file_new, 338 g_mem_chunk_alloc0, 463
GMappedFile, 337 g_mem_chunk_clean, 465
g_markup_collect_attributes, 396 g_mem_chunk_create, 464
G_MARKUP_ERROR, 388 g_mem_chunk_destroy, 463
g_markup_escape_text, 390 g_mem_chunk_free, 463
g_markup_parse_context_end_parse, 391 g_mem_chunk_info, 466
g_markup_parse_context_free, 391 g_mem_chunk_new, 462
g_markup_parse_context_get_element, 391 g_mem_chunk_print, 466
g_markup_parse_context_get_element_stack, 392 g_mem_chunk_reset, 465
g_markup_parse_context_get_position, 391 g_mem_gc_friendly, 151
g_markup_parse_context_get_user_data, 392 g_mem_is_system_malloc, 153
g_markup_parse_context_new, 392 g_mem_profile, 154
g_markup_parse_context_parse, 393 g_mem_set_vtable, 153
g_markup_parse_context_pop, 395 GMemChunk, 461
g_markup_parse_context_push, 393 g_memdup, 152
g_markup_printf_escaped, 390 g_memmove, 152
g_markup_vprintf_escaped, 390 GMemVTable, 153
GMarkupCollectType, 395 g_message, 185
GMarkupError, 387 MIN, 46
GMarkupParseContext, 388 G_MINDOUBLE, 44
GMarkupParseFlags, 388 G_MINFLOAT, 44
GMarkupParser, 389 G_MININT, 41
g_match_info_expand_references, 384 G_MININT16, 42
g_match_info_fetch, 384 G_MININT32, 42
g_match_info_fetch_all, 386 G_MININT64, 43
g_match_info_fetch_named, 385 G_MININT8, 42
g_match_info_fetch_named_pos, 385 G_MINLONG, 41
g_match_info_fetch_pos, 384 G_MINOFFSET, 43
g_match_info_free, 382 G_MINSHORT, 41
g_match_info_get_match_count, 383 G_MINSSIZE, 43
g_match_info_get_regex, 382 g_mkdir, 339
g_match_info_get_string, 382 g_mkdir_with_parents, 336
g_match_info_is_partial_match, 383 g_mkstemp, 335
g_match_info_matches, 382 GModule, 145
g_match_info_next, 383 g_module_build_path, 145
GMatchInfo, 382 g_module_close, 146
MAX, 46 g_module_error, 147
G_MAXDOUBLE, 44 G_MODULE_EXPORT, 147
G_MAXFLOAT, 44 G_MODULE_IMPORT, 147
G_MAXINT, 41 g_module_make_resident, 146
G_MAXINT16, 42 g_module_name, 146
G_MAXINT32, 42 g_module_open, 145
G_MAXINT64, 43 G_MODULE_SUFFIX, 147
G_MAXINT8, 42 g_module_supported, 145
G_MAXLONG, 41 g_module_symbol, 146
G_MAXOFFSET, 43 GModuleCheckInit, 147
MAXPATHLEN, 450 GModuleFlags, 146
G_MAXSHORT, 41 GModuleUnload, 147
600
INDEX INDEX
601
INDEX INDEX
602
INDEX INDEX
603
INDEX INDEX
604
INDEX INDEX
605
INDEX INDEX
606
INDEX INDEX
607
INDEX INDEX
g_utf16_to_utf8, 247
g_utf8_casefold, 243
g_utf8_collate, 244
g_utf8_collate_key, 244
g_utf8_collate_key_for_filename, 245
g_utf8_find_next_char, 240
g_utf8_find_prev_char, 240
g_utf8_get_char, 239
g_utf8_get_char_validated, 239
g_utf8_next_char, 239
g_utf8_normalize, 243
g_utf8_offset_to_pointer, 239
g_utf8_pointer_to_offset, 240
g_utf8_prev_char, 240
g_utf8_strchr, 241
g_utf8_strdown, 243
g_utf8_strlen, 241
g_utf8_strncpy, 241
g_utf8_strrchr, 241
g_utf8_strreverse, 242
g_utf8_strup, 242
g_utf8_to_ucs4, 245
g_utf8_to_ucs4_fast, 246
g_utf8_to_utf16, 245
g_utf8_validate, 242
g_utime, 343
V
G_VA_COPY, 65
g_vasprintf, 199
g_vfprintf, 197
GVoidFunc, 303
g_vprintf, 197
g_vsnprintf, 198
g_vsprintf, 198
W
g_warn_if_fail, 181
g_warn_if_reached, 181
g_warning, 185
G_WIN32_DLLMAIN_FOR_DLL_NAME, 453
g_win32_error_message, 450
g_win32_get_package_installation_directory, 451
g_win32_get_package_installation_directory_of_module,
451
g_win32_get_package_installation_subdirectory, 452
g_win32_get_windows_version, 452
g_win32_getlocale, 450
G_WIN32_HAVE_WIDECHAR_API, 453
G_WIN32_IS_NT_BASED, 454
g_win32_locale_filename_from_utf8, 453
608