Blame glib/glib/gutils.h

Packit db3073
/* GLIB - Library of useful routines for C programming
Packit db3073
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
/*
Packit db3073
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit db3073
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit db3073
 * files for a list of changes.  These files are distributed with
Packit db3073
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit db3073
 */
Packit db3073
Packit db3073
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
Packit db3073
#error "Only <glib.h> can be included directly."
Packit db3073
#endif
Packit db3073
Packit db3073
#ifndef __G_UTILS_H__
Packit db3073
#define __G_UTILS_H__
Packit db3073
Packit db3073
#include <glib/gtypes.h>
Packit db3073
#include <stdarg.h>
Packit db3073
Packit db3073
G_BEGIN_DECLS
Packit db3073
Packit db3073
/* Define G_VA_COPY() to do the right thing for copying va_list variables.
Packit db3073
 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
Packit db3073
 */
Packit db3073
#if !defined (G_VA_COPY)
Packit db3073
#  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
Packit db3073
#    define G_VA_COPY(ap1, ap2)	  (*(ap1) = *(ap2))
Packit db3073
#  elif defined (G_VA_COPY_AS_ARRAY)
Packit db3073
#    define G_VA_COPY(ap1, ap2)	  g_memmove ((ap1), (ap2), sizeof (va_list))
Packit db3073
#  else /* va_list is a pointer */
Packit db3073
#    define G_VA_COPY(ap1, ap2)	  ((ap1) = (ap2))
Packit db3073
#  endif /* va_list is a pointer */
Packit db3073
#endif /* !G_VA_COPY */
Packit db3073
Packit db3073
/* inlining hassle. for compilers that don't allow the `inline' keyword,
Packit db3073
 * mostly because of strict ANSI C compliance or dumbness, we try to fall
Packit db3073
 * back to either `__inline__' or `__inline'.
Packit db3073
 * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be 
Packit db3073
 * actually *capable* to do function inlining, in which case inline 
Packit db3073
 * function bodies do make sense. we also define G_INLINE_FUNC to properly 
Packit db3073
 * export the function prototypes if no inlining can be performed.
Packit db3073
 * inline function bodies have to be special cased with G_CAN_INLINE and a
Packit db3073
 * .c file specific macro to allow one compiled instance with extern linkage
Packit db3073
 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
Packit db3073
 */
Packit db3073
#if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
Packit db3073
#  undef inline
Packit db3073
#  define inline __inline__
Packit db3073
#elif !defined (G_HAVE_INLINE)
Packit db3073
#  undef inline
Packit db3073
#  if defined (G_HAVE___INLINE__)
Packit db3073
#    define inline __inline__
Packit db3073
#  elif defined (G_HAVE___INLINE)
Packit db3073
#    define inline __inline
Packit db3073
#  else /* !inline && !__inline__ && !__inline */
Packit db3073
#    define inline  /* don't inline, then */
Packit db3073
#  endif
Packit db3073
#endif
Packit db3073
#ifdef G_IMPLEMENT_INLINES
Packit db3073
#  define G_INLINE_FUNC
Packit db3073
#  undef  G_CAN_INLINE
Packit db3073
#elif defined (__GNUC__) 
Packit db3073
#  define G_INLINE_FUNC static __inline __attribute__ ((unused))
Packit db3073
#elif defined (G_CAN_INLINE) 
Packit db3073
#  define G_INLINE_FUNC static inline
Packit db3073
#else /* can't inline */
Packit db3073
#  define G_INLINE_FUNC
Packit db3073
#endif /* !G_INLINE_FUNC */
Packit db3073
Packit db3073
#ifndef __GTK_DOC_IGNORE__
Packit db3073
#ifdef G_OS_WIN32
Packit db3073
#define g_get_user_name g_get_user_name_utf8
Packit db3073
#define g_get_real_name g_get_real_name_utf8
Packit db3073
#define g_get_home_dir g_get_home_dir_utf8
Packit db3073
#define g_get_tmp_dir g_get_tmp_dir_utf8
Packit db3073
#endif
Packit db3073
#endif
Packit db3073
Packit db3073
const gchar *         g_get_user_name        (void);
Packit db3073
const gchar *         g_get_real_name        (void);
Packit db3073
const gchar *         g_get_home_dir         (void);
Packit db3073
const gchar *         g_get_tmp_dir          (void);
Packit db3073
const gchar *         g_get_host_name	     (void);
Packit db3073
gchar *               g_get_prgname          (void);
Packit db3073
void                  g_set_prgname          (const gchar *prgname);
Packit db3073
const gchar *         g_get_application_name (void);
Packit db3073
void                  g_set_application_name (const gchar *application_name);
Packit db3073
Packit db3073
void      g_reload_user_special_dirs_cache     (void);
Packit db3073
const gchar *         g_get_user_data_dir      (void);
Packit db3073
const gchar *         g_get_user_config_dir    (void);
Packit db3073
const gchar *         g_get_user_cache_dir     (void);
Packit db3073
const gchar * const * g_get_system_data_dirs   (void);
Packit db3073
Packit db3073
#ifdef G_OS_WIN32
Packit db3073
/* This functions is not part of the public GLib API */
Packit db3073
const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
Packit db3073
#endif
Packit db3073
Packit db3073
#if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
Packit db3073
/* This function is not part of the public GLib API either. Just call
Packit db3073
 * g_get_system_data_dirs() in your code, never mind that that is
Packit db3073
 * actually a macro and you will in fact call this inline function.
Packit db3073
 */
Packit db3073
static inline const gchar * const *
Packit db3073
_g_win32_get_system_data_dirs (void)
Packit db3073
{
Packit db3073
  return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
Packit db3073
}
Packit db3073
#define g_get_system_data_dirs _g_win32_get_system_data_dirs
Packit db3073
#endif
Packit db3073
Packit db3073
const gchar * const * g_get_system_config_dirs (void);
Packit db3073
Packit db3073
const gchar * g_get_user_runtime_dir (void);
Packit db3073
Packit db3073
/**
Packit db3073
 * GUserDirectory:
Packit db3073
 * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
Packit db3073
 * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
Packit db3073
 * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
Packit db3073
 * @G_USER_DIRECTORY_MUSIC: the user's Music directory
Packit db3073
 * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
Packit db3073
 * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
Packit db3073
 * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
Packit db3073
 * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
Packit db3073
 * @G_USER_N_DIRECTORIES: the number of enum values
Packit db3073
 *
Packit db3073
 * These are logical ids for special directories which are defined
Packit db3073
 * depending on the platform used. You should use g_get_user_special_dir()
Packit db3073
 * to retrieve the full path associated to the logical id.
Packit db3073
 *
Packit db3073
 * The #GUserDirectory enumeration can be extended at later date. Not
Packit db3073
 * every platform has a directory for every logical id in this
Packit db3073
 * enumeration.
Packit db3073
 *
Packit db3073
 * Since: 2.14
Packit db3073
 */
Packit db3073
typedef enum {
Packit db3073
  G_USER_DIRECTORY_DESKTOP,
Packit db3073
  G_USER_DIRECTORY_DOCUMENTS,
Packit db3073
  G_USER_DIRECTORY_DOWNLOAD,
Packit db3073
  G_USER_DIRECTORY_MUSIC,
Packit db3073
  G_USER_DIRECTORY_PICTURES,
Packit db3073
  G_USER_DIRECTORY_PUBLIC_SHARE,
Packit db3073
  G_USER_DIRECTORY_TEMPLATES,
Packit db3073
  G_USER_DIRECTORY_VIDEOS,
Packit db3073
Packit db3073
  G_USER_N_DIRECTORIES
Packit db3073
} GUserDirectory;
Packit db3073
Packit db3073
const gchar * g_get_user_special_dir (GUserDirectory directory);
Packit db3073
Packit db3073
/**
Packit db3073
 * GDebugKey:
Packit db3073
 * @key: the string
Packit db3073
 * @value: the flag
Packit db3073
 *
Packit db3073
 * Associates a string with a bit flag.
Packit db3073
 * Used in g_parse_debug_string().
Packit db3073
 */
Packit db3073
typedef struct _GDebugKey GDebugKey;
Packit db3073
struct _GDebugKey
Packit db3073
{
Packit db3073
  const gchar *key;
Packit db3073
  guint	       value;
Packit db3073
};
Packit db3073
Packit db3073
/* Miscellaneous utility functions
Packit db3073
 */
Packit db3073
guint                 g_parse_debug_string (const gchar     *string,
Packit db3073
					    const GDebugKey *keys,
Packit db3073
					    guint            nkeys);
Packit db3073
Packit db3073
gint                  g_snprintf           (gchar       *string,
Packit db3073
					    gulong       n,
Packit db3073
					    gchar const *format,
Packit db3073
					    ...) G_GNUC_PRINTF (3, 4);
Packit db3073
gint                  g_vsnprintf          (gchar       *string,
Packit db3073
					    gulong       n,
Packit db3073
					    gchar const *format,
Packit db3073
					    va_list      args);
Packit db3073
Packit db3073
void                  g_nullify_pointer    (gpointer    *nullify_location);
Packit db3073
Packit db3073
typedef enum
Packit db3073
{
Packit db3073
  G_FORMAT_SIZE_DEFAULT     = 0,
Packit db3073
  G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
Packit db3073
  G_FORMAT_SIZE_IEC_UNITS   = 1 << 1
Packit db3073
} GFormatSizeFlags;
Packit db3073
Packit db3073
GLIB_AVAILABLE_IN_2_30
Packit db3073
gchar *g_format_size_full   (guint64          size,
Packit db3073
                             GFormatSizeFlags flags);
Packit db3073
GLIB_AVAILABLE_IN_2_30
Packit db3073
gchar *g_format_size        (guint64          size);
Packit db3073
Packit db3073
GLIB_DEPRECATED_FOR(g_format_size)
Packit db3073
gchar *g_format_size_for_display (goffset size);
Packit db3073
Packit db3073
#ifndef G_DISABLE_DEPRECATED
Packit db3073
/**
Packit db3073
 * GVoidFunc:
Packit db3073
 *
Packit db3073
 * Declares a type of function which takes no arguments
Packit db3073
 * and has no return value. It is used to specify the type
Packit db3073
 * function passed to g_atexit().
Packit db3073
 */
Packit db3073
typedef void (*GVoidFunc) (void);
Packit db3073
#ifndef ATEXIT
Packit db3073
# define ATEXIT(proc) g_ATEXIT(proc)
Packit db3073
#else
Packit db3073
# define G_NATIVE_ATEXIT
Packit db3073
#endif /* ATEXIT */
Packit db3073
/* we use a GLib function as a replacement for ATEXIT, so
Packit db3073
 * the programmer is not required to check the return value
Packit db3073
 * (if there is any in the implementation) and doesn't encounter
Packit db3073
 * missing include files.
Packit db3073
 */
Packit db3073
GLIB_DEPRECATED
Packit db3073
void	g_atexit		(GVoidFunc    func);
Packit db3073
Packit db3073
#ifdef G_OS_WIN32
Packit db3073
/* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
Packit db3073
 * atexit(), the function will be called when the GLib DLL is detached
Packit db3073
 * from the program, which is not what the caller wants. The caller
Packit db3073
 * wants the function to be called when it *itself* exits (or is
Packit db3073
 * detached, in case the caller, too, is a DLL).
Packit db3073
 */
Packit db3073
#if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
Packit db3073
int atexit (void (*)(void));
Packit db3073
#endif
Packit db3073
#define g_atexit(func) atexit(func)
Packit db3073
#endif
Packit db3073
Packit db3073
#endif  /* G_DISABLE_DEPRECATED */
Packit db3073
Packit db3073
#ifndef __GTK_DOC_IGNORE__
Packit db3073
#ifdef G_OS_WIN32
Packit db3073
#define g_find_program_in_path g_find_program_in_path_utf8
Packit db3073
#endif
Packit db3073
#endif
Packit db3073
Packit db3073
/* Look for an executable in PATH, following execvp() rules */
Packit db3073
gchar*  g_find_program_in_path  (const gchar *program);
Packit db3073
Packit db3073
/* Bit tests
Packit db3073
 */
Packit db3073
G_INLINE_FUNC gint	g_bit_nth_lsf (gulong  mask,
Packit db3073
				       gint    nth_bit) G_GNUC_CONST;
Packit db3073
G_INLINE_FUNC gint	g_bit_nth_msf (gulong  mask,
Packit db3073
				       gint    nth_bit) G_GNUC_CONST;
Packit db3073
G_INLINE_FUNC guint	g_bit_storage (gulong  number) G_GNUC_CONST;
Packit db3073
Packit db3073
/* inline function implementations
Packit db3073
 */
Packit db3073
#if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
Packit db3073
G_INLINE_FUNC gint
Packit db3073
g_bit_nth_lsf (gulong mask,
Packit db3073
	       gint   nth_bit)
Packit db3073
{
Packit db3073
  if (G_UNLIKELY (nth_bit < -1))
Packit db3073
    nth_bit = -1;
Packit db3073
  while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
Packit db3073
    {
Packit db3073
      nth_bit++;
Packit db3073
      if (mask & (1UL << nth_bit))
Packit db3073
	return nth_bit;
Packit db3073
    }
Packit db3073
  return -1;
Packit db3073
}
Packit db3073
G_INLINE_FUNC gint
Packit db3073
g_bit_nth_msf (gulong mask,
Packit db3073
	       gint   nth_bit)
Packit db3073
{
Packit db3073
  if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
Packit db3073
    nth_bit = GLIB_SIZEOF_LONG * 8;
Packit db3073
  while (nth_bit > 0)
Packit db3073
    {
Packit db3073
      nth_bit--;
Packit db3073
      if (mask & (1UL << nth_bit))
Packit db3073
	return nth_bit;
Packit db3073
    }
Packit db3073
  return -1;
Packit db3073
}
Packit db3073
G_INLINE_FUNC guint
Packit db3073
g_bit_storage (gulong number)
Packit db3073
{
Packit db3073
#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Packit db3073
  return G_LIKELY (number) ?
Packit db3073
	   ((GLIB_SIZEOF_LONG * 8U - 1) ^ (guint) __builtin_clzl(number)) + 1 : 1;
Packit db3073
#else
Packit db3073
  register guint n_bits = 0;
Packit db3073
  
Packit db3073
  do
Packit db3073
    {
Packit db3073
      n_bits++;
Packit db3073
      number >>= 1;
Packit db3073
    }
Packit db3073
  while (number);
Packit db3073
  return n_bits;
Packit db3073
#endif
Packit db3073
}
Packit db3073
#endif  /* G_CAN_INLINE || __G_UTILS_C__ */
Packit db3073
Packit db3073
G_END_DECLS
Packit db3073
Packit db3073
#ifndef G_DISABLE_DEPRECATED
Packit db3073
Packit db3073
/*
Packit db3073
 * This macro is deprecated. This DllMain() is too complex. It is
Packit db3073
 * recommended to write an explicit minimal DLlMain() that just saves
Packit db3073
 * the handle to the DLL and then use that handle instead, for
Packit db3073
 * instance passing it to
Packit db3073
 * g_win32_get_package_installation_directory_of_module().
Packit db3073
 *
Packit db3073
 * On Windows, this macro defines a DllMain function that stores the
Packit db3073
 * actual DLL name that the code being compiled will be included in.
Packit db3073
 * STATIC should be empty or 'static'. DLL_NAME is the name of the
Packit db3073
 * (pointer to the) char array where the DLL name will be stored. If
Packit db3073
 * this is used, you must also include <windows.h>. If you need a more complex
Packit db3073
 * DLL entry point function, you cannot use this.
Packit db3073
 *
Packit db3073
 * On non-Windows platforms, expands to nothing.
Packit db3073
 */
Packit db3073
Packit db3073
#ifndef G_PLATFORM_WIN32
Packit db3073
# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
Packit db3073
#else
Packit db3073
# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)			\
Packit db3073
static char *dll_name;							\
Packit db3073
									\
Packit db3073
BOOL WINAPI								\
Packit db3073
DllMain (HINSTANCE hinstDLL,						\
Packit db3073
	 DWORD     fdwReason,						\
Packit db3073
	 LPVOID    lpvReserved)						\
Packit db3073
{									\
Packit db3073
  wchar_t wcbfr[1000];							\
Packit db3073
  char *tem;								\
Packit db3073
  switch (fdwReason)							\
Packit db3073
    {									\
Packit db3073
    case DLL_PROCESS_ATTACH:						\
Packit db3073
      GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
Packit db3073
      tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);		\
Packit db3073
      dll_name = g_path_get_basename (tem);				\
Packit db3073
      g_free (tem);							\
Packit db3073
      break;								\
Packit db3073
    }									\
Packit db3073
									\
Packit db3073
  return TRUE;								\
Packit db3073
}
Packit db3073
Packit db3073
#endif	/* !G_DISABLE_DEPRECATED */
Packit db3073
Packit db3073
#endif /* G_PLATFORM_WIN32 */
Packit db3073
Packit db3073
#endif /* __G_UTILS_H__ */