Blame glib/gutils.h

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#ifndef __G_UTILS_H__
Packit ae235b
#define __G_UTILS_H__
Packit ae235b
Packit ae235b
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
Packit ae235b
#error "Only <glib.h> can be included directly."
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <glib/gtypes.h>
Packit ae235b
#include <stdarg.h>
Packit ae235b
Packit ae235b
G_BEGIN_DECLS
Packit ae235b
Packit ae235b
/* Define G_VA_COPY() to do the right thing for copying va_list variables.
Packit ae235b
 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
Packit ae235b
 */
Packit ae235b
#if !defined (G_VA_COPY)
Packit ae235b
#  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
Packit ae235b
#    define G_VA_COPY(ap1, ap2)	  (*(ap1) = *(ap2))
Packit ae235b
#  elif defined (G_VA_COPY_AS_ARRAY)
Packit ae235b
#    define G_VA_COPY(ap1, ap2)	  memmove ((ap1), (ap2), sizeof (va_list))
Packit ae235b
#  else /* va_list is a pointer */
Packit ae235b
#    define G_VA_COPY(ap1, ap2)	  ((ap1) = (ap2))
Packit ae235b
#  endif /* va_list is a pointer */
Packit ae235b
#endif /* !G_VA_COPY */
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_user_name        (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_real_name        (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_home_dir         (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_tmp_dir          (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_host_name	     (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_prgname          (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void                  g_set_prgname          (const gchar *prgname);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_application_name (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void                  g_set_application_name (const gchar *application_name);
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void      g_reload_user_special_dirs_cache     (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_user_data_dir      (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_user_config_dir    (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar *         g_get_user_cache_dir     (void);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar * const * g_get_system_data_dirs   (void);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
/* This functions is not part of the public GLib API */
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
Packit ae235b
/* This function is not part of the public GLib API either. Just call
Packit ae235b
 * g_get_system_data_dirs() in your code, never mind that that is
Packit ae235b
 * actually a macro and you will in fact call this inline function.
Packit ae235b
 */
Packit ae235b
static inline const gchar * const *
Packit ae235b
_g_win32_get_system_data_dirs (void)
Packit ae235b
{
Packit ae235b
  return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
Packit ae235b
}
Packit ae235b
#define g_get_system_data_dirs _g_win32_get_system_data_dirs
Packit ae235b
#endif
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar * const * g_get_system_config_dirs (void);
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar * g_get_user_runtime_dir (void);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GUserDirectory:
Packit ae235b
 * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
Packit ae235b
 * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
Packit ae235b
 * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
Packit ae235b
 * @G_USER_DIRECTORY_MUSIC: the user's Music directory
Packit ae235b
 * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
Packit ae235b
 * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
Packit ae235b
 * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
Packit ae235b
 * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
Packit ae235b
 * @G_USER_N_DIRECTORIES: the number of enum values
Packit ae235b
 *
Packit ae235b
 * These are logical ids for special directories which are defined
Packit ae235b
 * depending on the platform used. You should use g_get_user_special_dir()
Packit ae235b
 * to retrieve the full path associated to the logical id.
Packit ae235b
 *
Packit ae235b
 * The #GUserDirectory enumeration can be extended at later date. Not
Packit ae235b
 * every platform has a directory for every logical id in this
Packit ae235b
 * enumeration.
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
typedef enum {
Packit ae235b
  G_USER_DIRECTORY_DESKTOP,
Packit ae235b
  G_USER_DIRECTORY_DOCUMENTS,
Packit ae235b
  G_USER_DIRECTORY_DOWNLOAD,
Packit ae235b
  G_USER_DIRECTORY_MUSIC,
Packit ae235b
  G_USER_DIRECTORY_PICTURES,
Packit ae235b
  G_USER_DIRECTORY_PUBLIC_SHARE,
Packit ae235b
  G_USER_DIRECTORY_TEMPLATES,
Packit ae235b
  G_USER_DIRECTORY_VIDEOS,
Packit ae235b
Packit ae235b
  G_USER_N_DIRECTORIES
Packit ae235b
} GUserDirectory;
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
const gchar * g_get_user_special_dir (GUserDirectory directory);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GDebugKey:
Packit ae235b
 * @key: the string
Packit ae235b
 * @value: the flag
Packit ae235b
 *
Packit ae235b
 * Associates a string with a bit flag.
Packit ae235b
 * Used in g_parse_debug_string().
Packit ae235b
 */
Packit ae235b
typedef struct _GDebugKey GDebugKey;
Packit ae235b
struct _GDebugKey
Packit ae235b
{
Packit ae235b
  const gchar *key;
Packit ae235b
  guint	       value;
Packit ae235b
};
Packit ae235b
Packit ae235b
/* Miscellaneous utility functions
Packit ae235b
 */
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
guint                 g_parse_debug_string (const gchar     *string,
Packit ae235b
					    const GDebugKey *keys,
Packit ae235b
					    guint            nkeys);
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gint                  g_snprintf           (gchar       *string,
Packit ae235b
					    gulong       n,
Packit ae235b
					    gchar const *format,
Packit ae235b
					    ...) G_GNUC_PRINTF (3, 4);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gint                  g_vsnprintf          (gchar       *string,
Packit ae235b
					    gulong       n,
Packit ae235b
					    gchar const *format,
Packit ae235b
					    va_list      args)
Packit ae235b
					    G_GNUC_PRINTF(3, 0);
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void                  g_nullify_pointer    (gpointer    *nullify_location);
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  G_FORMAT_SIZE_DEFAULT     = 0,
Packit ae235b
  G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
Packit ae235b
  G_FORMAT_SIZE_IEC_UNITS   = 1 << 1,
Packit ae235b
  G_FORMAT_SIZE_BITS        = 1 << 2
Packit ae235b
} GFormatSizeFlags;
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_2_30
Packit ae235b
gchar *g_format_size_full   (guint64          size,
Packit ae235b
                             GFormatSizeFlags flags);
Packit ae235b
GLIB_AVAILABLE_IN_2_30
Packit ae235b
gchar *g_format_size        (guint64          size);
Packit ae235b
Packit ae235b
GLIB_DEPRECATED_FOR(g_format_size)
Packit ae235b
gchar *g_format_size_for_display (goffset size);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_DEPRECATED
Packit ae235b
/**
Packit ae235b
 * GVoidFunc:
Packit ae235b
 *
Packit ae235b
 * Declares a type of function which takes no arguments
Packit ae235b
 * and has no return value. It is used to specify the type
Packit ae235b
 * function passed to g_atexit().
Packit ae235b
 */
Packit ae235b
typedef void (*GVoidFunc) (void);
Packit ae235b
#define ATEXIT(proc) g_ATEXIT(proc)
Packit ae235b
GLIB_DEPRECATED
Packit ae235b
void	g_atexit		(GVoidFunc    func);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
/* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
Packit ae235b
 * atexit(), the function will be called when the GLib DLL is detached
Packit ae235b
 * from the program, which is not what the caller wants. The caller
Packit ae235b
 * wants the function to be called when it *itself* exits (or is
Packit ae235b
 * detached, in case the caller, too, is a DLL).
Packit ae235b
 */
Packit ae235b
#if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
Packit ae235b
int atexit (void (*)(void));
Packit ae235b
#endif
Packit ae235b
#define g_atexit(func) atexit(func)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
/* Look for an executable in PATH, following execvp() rules */
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gchar*  g_find_program_in_path  (const gchar *program);
Packit ae235b
Packit ae235b
/* Bit tests
Packit ae235b
 *
Packit ae235b
 * These are defined in a convoluted way because we want the compiler to
Packit ae235b
 * be able to inline the code for performance reasons, but for
Packit ae235b
 * historical reasons, we must continue to provide non-inline versions
Packit ae235b
 * on our ABI.
Packit ae235b
 *
Packit ae235b
 * We define these as functions in gutils.c which are just implemented
Packit ae235b
 * as calls to the _impl() versions in order to preserve the ABI.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define g_bit_nth_lsf(mask, nth_bit) g_bit_nth_lsf_impl(mask, nth_bit)
Packit ae235b
#define g_bit_nth_msf(mask, nth_bit) g_bit_nth_msf_impl(mask, nth_bit)
Packit ae235b
#define g_bit_storage(number)        g_bit_storage_impl(number)
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gint    (g_bit_nth_lsf)         (gulong mask,
Packit ae235b
                                 gint   nth_bit);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gint    (g_bit_nth_msf)         (gulong mask,
Packit ae235b
                                 gint   nth_bit);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
guint   (g_bit_storage)         (gulong number);
Packit ae235b
Packit ae235b
static inline gint
Packit ae235b
g_bit_nth_lsf_impl (gulong mask,
Packit ae235b
                    gint   nth_bit)
Packit ae235b
{
Packit ae235b
  if (G_UNLIKELY (nth_bit < -1))
Packit ae235b
    nth_bit = -1;
Packit ae235b
  while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
Packit ae235b
    {
Packit ae235b
      nth_bit++;
Packit ae235b
      if (mask & (1UL << nth_bit))
Packit ae235b
        return nth_bit;
Packit ae235b
    }
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline gint
Packit ae235b
g_bit_nth_msf_impl (gulong mask,
Packit ae235b
                    gint   nth_bit)
Packit ae235b
{
Packit ae235b
  if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
Packit ae235b
    nth_bit = GLIB_SIZEOF_LONG * 8;
Packit ae235b
  while (nth_bit > 0)
Packit ae235b
    {
Packit ae235b
      nth_bit--;
Packit ae235b
      if (mask & (1UL << nth_bit))
Packit ae235b
        return nth_bit;
Packit ae235b
    }
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline guint
Packit ae235b
g_bit_storage_impl (gulong number)
Packit ae235b
{
Packit ae235b
#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Packit ae235b
  return G_LIKELY (number) ?
Packit ae235b
           ((GLIB_SIZEOF_LONG * 8U - 1) ^ (guint) __builtin_clzl(number)) + 1 : 1;
Packit ae235b
#else
Packit ae235b
  guint n_bits = 0;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      n_bits++;
Packit ae235b
      number >>= 1;
Packit ae235b
    }
Packit ae235b
  while (number);
Packit ae235b
  return n_bits;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Crashes the program. */
Packit ae235b
#if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_50
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
#  include <stdlib.h>
Packit ae235b
#  define g_abort() abort ()
Packit ae235b
#else
Packit ae235b
GLIB_AVAILABLE_IN_2_50
Packit ae235b
void g_abort (void) G_GNUC_NORETURN G_ANALYZER_NORETURN;
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_DEPRECATED
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * This macro is deprecated. This DllMain() is too complex. It is
Packit ae235b
 * recommended to write an explicit minimal DLlMain() that just saves
Packit ae235b
 * the handle to the DLL and then use that handle instead, for
Packit ae235b
 * instance passing it to
Packit ae235b
 * g_win32_get_package_installation_directory_of_module().
Packit ae235b
 *
Packit ae235b
 * On Windows, this macro defines a DllMain function that stores the
Packit ae235b
 * actual DLL name that the code being compiled will be included in.
Packit ae235b
 * STATIC should be empty or 'static'. DLL_NAME is the name of the
Packit ae235b
 * (pointer to the) char array where the DLL name will be stored. If
Packit ae235b
 * this is used, you must also include <windows.h>. If you need a more complex
Packit ae235b
 * DLL entry point function, you cannot use this.
Packit ae235b
 *
Packit ae235b
 * On non-Windows platforms, expands to nothing.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#ifndef G_PLATFORM_WIN32
Packit ae235b
# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
Packit ae235b
#else
Packit ae235b
# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)			\
Packit ae235b
static char *dll_name;							\
Packit ae235b
									\
Packit ae235b
BOOL WINAPI								\
Packit ae235b
DllMain (HINSTANCE hinstDLL,						\
Packit ae235b
	 DWORD     fdwReason,						\
Packit ae235b
	 LPVOID    lpvReserved)						\
Packit ae235b
{									\
Packit ae235b
  wchar_t wcbfr[1000];							\
Packit ae235b
  char *tem;								\
Packit ae235b
  switch (fdwReason)							\
Packit ae235b
    {									\
Packit ae235b
    case DLL_PROCESS_ATTACH:						\
Packit ae235b
      GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
Packit ae235b
      tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);		\
Packit ae235b
      dll_name = g_path_get_basename (tem);				\
Packit ae235b
      g_free (tem);							\
Packit ae235b
      break;								\
Packit ae235b
    }									\
Packit ae235b
									\
Packit ae235b
  return TRUE;								\
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif	/* !G_DISABLE_DEPRECATED */
Packit ae235b
Packit ae235b
#endif /* G_PLATFORM_WIN32 */
Packit ae235b
Packit ae235b
G_END_DECLS
Packit ae235b
Packit ae235b
#endif /* __G_UTILS_H__ */