Recently engaged in Dlna, found that download source has a lot of glib library use. The glib library was installed in Ubuntu and a few glib library functions were simply tested to increase the understanding of glib.
Overview: The glib library is the most commonly used C-language function library under Linux, which has good portability and practicality. GLib is the basis for GTK + libraries and gnome. GLib can be used on multiple platforms, such as Linux, Unix, Windows, and more. GLib provides a corresponding substitute for many standard, commonly used C language structures. (from the Baidu Encyclopedia)
If you want to use a function in the glib library in your program, you should include the Glib.h header file (Glib.h is already included in the Gtk.h and gnome.h header files)
1 type definition
1. Integer types: gint8, Guint8, Gint16, Guint16, Gint31, Guint32, Gint64, Guint64. Not all platforms are available in 64-bit integer
2. Integer type Gshort, Glong, Gint and short, long, int same
3. Boolean type Gboolean:gboolean can take two values: True and False
4. Character type Gchar and Char are the same
5. Floating-point gfloat and gdouble and float, double are fully equivalent
6. Pointer gpointer corresponds to the void* of standard C
7. Gconstpointer for the const void* of standard C
2 GLib macro
2.1 Common macros
Conversion between integral type and pointer type
1. Gint_to_pointer (a): Convert int type to Gpointer type
2. Gpointer_to_int (a): Convert gpointer type to INT type
3. Guint_to_pointer (a): convert UINT type to Gpointer type
4. Gpointer_to_uint (a): Convert Gpointer type to integral type
5. Definition of a NULL macro: #define NULL (void*) 0 (that is, 0 is an integer data, and Null is a pointer type)
2.2 Debugging macros
Prerequisite Check (when g_disable_checks is defined, the prerequisites check that the macro disappears at compile time)
Assertion (when G_disable_assert is defined, the assertion macro disappears at compile time without adding to the generated program)
All debug macros Use the G_log output warning message, and the G_log warning includes the name of the application or library function where the error occurred.
2.3 Determine if the build is a specified
1. The "Gtk_is_ Component Name" series macro is used to determine whether the component is the corresponding structure
2. Gtk_is_button: Used to determine whether the widget is a button
3. Gtk_is_notebook: Used to determine whether the component is a notebook component
3 Common functions
1. G_warning: Output a warning message on the console
2. G_error: End the execution of the program after outputting an error message from the console
3. G_print: Output A message on the console
4. G_object_unref (Gpointer object): The number of references to object is reduced by 1. When the number of references to object drops to 0 o'clock, delete the object
5. G_file_test: Determine if a file exists
6. G_spawn_sync: Create a child process to run the external program. When the child process is finished, the function returns (synchronously executes a child process)
4 Memory Management
1. G_malloc always returns the Gpointer type. If the request fails, G_malloc exits the program, so you do not have to check the return value
2. G_free ignores any null pointers passed to it
3. The G_realloc function and the ReAlloc function function the same
4. G_MALLOC0: Set 0 of each unit of allocated memory
4.4 Other string conversion functions
1. G_strdup and G_strndup returns a string of allocated memory or a copy of the first n characters of a string
2. G_strconcat used to connect two strings
3. G_strsplit separates the split string at each delimiter and returns a newly assigned string
5 Data structures
5.1 Linked List
GLib provides a common one-way linked list and a doubly linked list, respectively, Gslist and Glist. The data field in a linked list is a gpointer type (the Gint_to_pointer conversion can also be used to make the integer type available in the linked list).
G_list_pervious is used in a doubly linked list to find the previous element of a specified element, which is a unique function in a doubly linked list.
Note: This is only one-way list function, and other doubly linked list functions only need to change the name of the one-way list function to "G_list_ name", also applicable.
G_slist_append used to add an element to a list of unidirectional links
G_slist_remove used to delete an element in a one-way list
G_slist_next to get the next element of the specified element
G_slist_prepend used to insert data into the list header of a linked table
G_slist_reverse is used to reverse the elements in a linked list
G_slist_insert used to insert an element at a specified position
G_slist_remove is used to delete the specified element
G_slist_foreach calls the Gfunc function on each item of the linked list. The GFUNC function is defined as follows: void* Gfunc (gpointer data,gpointer user_data)
5.2 Trees
There are two different trees in glib: gtree is the basic balanced binary tree, which sorts the stored data into binary sort tree by its value
5.3 Hash Table
6 gstring
Gstring is similar to the standard C string type, but gstring can grow automatically, which prevents buffer overflow in the program. Here is the definition of gstring:
struct gstring{
gchar* str; Storage space pointing to string data
Gint Len; The length of the string
}
1. G_string_new (gchar* init): Use a string to create a gstring structure
2. G_string_free (gstring* string,gint free_segment): Releases the memory occupied by gstring
3. G_string_assign (gstring* lval,const gchar* rval): Copy string Rval to Lval, destroy Lval original content
4. G_string_truncate: Intercepts a string to generate a string of the specified length
5. G_string_append: Appends a string to the specified string
6. G_string_append_c: Appends a character to the specified string
7. G_string_prepend: Appends a string to the specified string
8. G_string_prepend_c: Appends a character to the specified string
9. G_string_sprintf: Writes a formatted string to gstring
7. Timer function
The counter function can be used to timing the operation. Use the following procedure:
1. Create a timer using G_timer_new
2. Use G_timer_start to start timing the operation
3. Use G_timer_stop to stop timing
4. Use g_timer_elapsed to determine when the timer is running
5. Using G_timer_destroy to destroy a timer when not in use
6. During use, you can also use the G_timer_reset to reset the timer to zero
8 Error handling function
1. G_strerror uses the error code to get a string that describes the error. The output is generally used: "Program name: the function name where the error occurred: file or description"
2. G_error prints an error message and exits the program. The format is similar to printf, but preceded by the "**error**" in its message
3. g_warning prints a warning message similar to printf, but with "**warning**" in front of its message, and it does not exit the program. Usually used for errors that are not too serious
4. G_message adds "**message**" in front of the string to output a message
5. G_strsignal to obtain a string describing the signal via a signal value
9 Other Utility functions
G_get_prgname: Gets the name of the program
G_set_prgname: Set the name of the program
G_get_user_name: Returns the name of the current user
G_get_real_name: Get the user's real name
G_get_home_dir: Get the user's root directory
G_get_current_dir: Get Current working directory
G_basename: Get the file name (without the preamble part)
G_dirname: Returns the leading directory portion of the file name
G_atexit: Specifies a function to execute when the normal program aborts
G_assert (condition): Determines whether the condition is satisfied, does not meet the output of a warning message after the execution of the exit program
1.g_return_if_fail (condition): If the condition is false, output a warning message and return immediately from the current function
2.g_return_val_if_fail (Condition,retval): If the condition is false, the output of a warning message is returned from the current function, and the return value is retval
First acquaintance glib (1)