Util

Description These are portable utility functions.

Members

Static functions

atexit
void atexit(GVoidFunc func)

Specifies a function to be called at normal program termination. Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor macro that maps to a call to the atexit() function in the C library. This means that in case the code that calls g_atexit(), i.e. atexit(), is in a DLL, the function will be called when the DLL is detached from the program. This typically makes more sense than that the function is called when the GLib DLL is detached, which happened earlier when g_atexit() was a function in the GLib DLL. The behaviour of atexit() in the context of dynamically loaded modules is not formally specified and varies wildly. On POSIX systems, calling g_atexit() (or atexit()) in a dynamically loaded module which is unloaded before the program terminates might well cause a crash at program exit. Some POSIX systems implement atexit() like Windows, and have each dynamically loaded module maintain an own atexit chain that is called when the module is unloaded. On other POSIX systems, before a dynamically loaded module is unloaded, the registered atexit functions (if any) residing in that module are called, regardless where the code that registered them resided. This is presumably the most robust approach. As can be seen from the above, for portability it's best to avoid calling g_atexit() (or atexit()) except in the main executable of a program.

basename
string basename(string fileName)

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.

bitNthLsf
int bitNthLsf(gulong mask, int nthBit)

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.

bitNthMsf
int bitNthMsf(gulong mask, int nthBit)

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.

bitStorage
uint bitStorage(gulong number)

Gets the number of bits used to hold number, e.g. if number is 4, 3 bits are needed.

buildFilename
string buildFilename(string[] firstElement)

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.

buildFilenamev
string buildFilenamev(char** args)

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. Since 2.8

buildPath
string buildPath(string separator, string[] firstElement)

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.

buildPathv
string buildPathv(string separator, char** args)

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. Since 2.8

formatSizeForDisplay
string formatSizeForDisplay(long size)

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. Since 2.16

getApplicationName
string getApplicationName()

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

getEnviron
string[] getEnviron()

Gets the list of environment variables for the current process. The list is NULL terminated and each item in the list is of the form 'NAME=VALUE'. This is equivalent to direct access to the 'environ' global variable, except portable. The return value is freshly allocated and it should be freed with g_strfreev() when it is no longer needed. Since 2.28

getHostName
string getHostName()

Return a name for the machine. The returned name is not necessarily a fully-qualified domain name, or even present in DNS or some other name service at all. It need not even be unique on your local network or site, but usually it is. Callers should not rely on the return value having any specific properties like uniqueness for security purposes. Even if the name of the machine is changed while an application is running, the return value from this function does not change. The returned string is owned by GLib and should not be modified or freed. If no name can be determined, a default fixed string "localhost" is returned. Since 2.8

getPrgname
string getPrgname()

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].)

getSystemConfigDirs
string[] getSystemConfigDirs()

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. In this case the list of directories retrieved will be XDG_CONFIG_DIRS. On Windows is the directory that contains application data for all users. A typical path is C:\Documents and Settings\All Users\Application Data. This folder is used for application data that is not user specific. For example, an application can store a spell-check dictionary, a database of clip art, or a log file in the CSIDL_COMMON_APPDATA folder. This information will not roam and is available to anyone using the computer. Since 2.6

getSystemDataDirs
string[] getSystemDataDirs()

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 In this case the list of directories retrieved will be XDG_DATA_DIRS. 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. Since 2.6

getUserCacheDir
string getUserCacheDir()

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. In this case the directory retrieved will be XDG_CACHE_HOME. On Windows is the directory that serves as a common repository for temporary Internet files. A typical path is C:\Documents and Settings\username\Local Settings\Temporary Internet Files. See documentation for CSIDL_INTERNET_CACHE. Since 2.6

getUserConfigDir
string getUserConfigDir()

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. In this case the directory retrieved will be XDG_CONFIG_HOME. On Windows this is the folder to use for local (as opposed to roaming) application data. See documentation for CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as what g_get_user_data_dir() returns. Since 2.6

getUserDataDir
string getUserDataDir()

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. In this case the directory retrieved will be XDG_DATA_HOME. On Windows this is the folder to use for local (as opposed to roaming) application data. See documentation for CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as what g_get_user_config_dir() returns. Since 2.6

getUserRuntimeDir
string getUserRuntimeDir()

Returns a directory that is unique to the current user on the local system. On UNIX platforms this is determined using the mechanisms described in the XDG Base Directory Specification. This is the directory specified in the XDG_RUNTIME_DIR environment variable. In the case that this variable is not set, GLib will issue a warning message to stderr and return the value of g_get_user_cache_dir(). On Windows this is the folder to use for local (as opposed to roaming) application data. See documentation for CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as what g_get_user_config_dir() returns. Since 2.28

getUserSpecialDir
string getUserSpecialDir(GUserDirectory directory)

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. Since 2.14

listenv
string[] listenv()

Gets the names of all variables set in the environment. Since 2.8

nullifyPointer
void nullifyPointer(void** nullifyLocation)

Set the pointer at the specified location to NULL.

parseDebugString
uint parseDebugString(string string, GDebugKey* keys, uint nkeys)

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 variables. 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.

pathGetBasename
string pathGetBasename(string fileName)

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 ".".

pathGetDirname
string pathGetDirname(string fileName)

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.

pathIsAbsolute
int pathIsAbsolute(string fileName)

Returns TRUE if the given file_name is an absolute file name. Note that this is a somewhat vague concept on Windows. On POSIX systems, an absolute file name is well-defined. It always starts from the single root directory. For example "/usr/local". On Windows, the concepts of current drive and drive-specific current directory introduce vagueness. This function interprets as an absolute file name one that either begins with a directory separator such as "\Users\tml" or begins with the root on a drive, for example "C:\Windows". The first case also includes UNC paths such as "\\myserver\docs\foo". In all cases, either slashes or backslashes are accepted. Note that a file name relative to the current drive root does not truly specify a file uniquely over time and across processes, as the current drive is a per-process value and can be changed. File names relative the current directory on some specific drive, such as "D:foo/bar", are not interpreted as absolute by this function, but they obviously are not relative to the normal current directory as returned by getcwd() or g_get_current_dir() either. Such paths should be avoided, or need to be handled using Windows-specific code.

pathSkipRoot
string pathSkipRoot(string fileName)

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.

qsortWithData
void qsortWithData(void* pbase, int totalElems, gsize size, GCompareDataFunc compareFunc, void* userData)

This is just like the standard C qsort() function, but the comparison routine accepts a user data argument.

reloadUserSpecialDirsCache
void reloadUserSpecialDirsCache()

Resets the cache used for g_get_user_special_dir(), so that the latest on-disk version is used. Call this only if you just changed the data on disk yourself. Due to threadsafety issues this may cause leaking of strings that were previously returned from g_get_user_special_dir() that can't be freed. We ensure to only leak the data for the directories that actually changed value though. Since 2.22

setApplicationName
void setApplicationName(string applicationName)

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 application's name in the task list. Since 2.2

setPrgname
void setPrgname(string 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.

spacedPrimesClosest
uint spacedPrimesClosest(uint num)

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.

Meta