Merge pull request #409 from Alkarex/patch-1
[mono.git] / eglib / src / glib.h
index 0f8f8e74ec92388cd39fc9c149320b46df08e6c6..6d6dadff448c31b9b07cf6ab691b0c4f8a6624f1 100644 (file)
@@ -110,6 +110,9 @@ typedef guint32 gunichar;
 #define FALSE                0
 #define TRUE                 1
 
+#define G_MINSHORT           SHRT_MIN
+#define G_MAXSHORT           SHRT_MAX
+#define G_MAXUSHORT          USHRT_MAX
 #define G_MAXINT             INT_MAX
 #define G_MININT             INT_MIN
 #define G_MAXINT32           INT32_MAX
@@ -126,7 +129,9 @@ typedef guint32 gunichar;
 
 #define G_USEC_PER_SEC  1000000
 
+#ifndef ABS
 #define ABS(a)         ((a) > 0 ? (a) : -(a))
+#endif
 
 #define G_STRUCT_OFFSET(p_type,field) offsetof(p_type,field)
 
@@ -192,60 +197,6 @@ gchar*           g_win32_getlocale(void);
 #define g_return_if_fail(x)  G_STMT_START { if (!(x)) { g_critical ("%s:%d: assertion '%s' failed", __FILE__, __LINE__, #x); return; } } G_STMT_END
 #define g_return_val_if_fail(x,e)  G_STMT_START { if (!(x)) { g_critical ("%s:%d: assertion '%s' failed", __FILE__, __LINE__, #x); return (e); } } G_STMT_END
 
-/*
- * Hashtables
- */
-typedef struct _GHashTable GHashTable;
-typedef struct _GHashTableIter GHashTableIter;
-
-/* Private, but needed for stack allocation */
-struct _GHashTableIter
-{
-       gpointer dummy [8];
-};
-
-typedef void     (*GFunc)          (gpointer data, gpointer user_data);
-typedef gint     (*GCompareFunc)   (gconstpointer a, gconstpointer b);
-typedef gint     (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
-typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
-typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
-typedef void     (*GDestroyNotify) (gpointer data);
-typedef guint    (*GHashFunc)      (gconstpointer key);
-typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
-typedef void     (*GFreeFunc)      (gpointer       data);
-
-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);
-void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
-guint           g_hash_table_size            (GHashTable *hash);
-gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
-gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
-void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
-gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
-gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
-gboolean        g_hash_table_steal           (GHashTable *hash, gconstpointer key);
-void            g_hash_table_remove_all      (GHashTable *hash);
-guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
-guint           g_hash_table_foreach_steal   (GHashTable *hash, GHRFunc func, gpointer user_data);
-void            g_hash_table_destroy         (GHashTable *hash);
-void            g_hash_table_print_stats     (GHashTable *table);
-
-void            g_hash_table_iter_init       (GHashTableIter *iter, GHashTable *hash_table);
-gboolean        g_hash_table_iter_next       (GHashTableIter *iter, gpointer *key, gpointer *value);
-
-guint           g_spaced_primes_closest      (guint x);
-
-#define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
-#define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
-
-gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
-guint    g_direct_hash  (gconstpointer v1);
-gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
-guint    g_int_hash     (gconstpointer v1);
-gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
-guint    g_str_hash     (gconstpointer v1);
-
 /*
  * Errors
  */
@@ -301,11 +252,16 @@ gint         g_snprintf        (gchar *string, gulong n, gchar const *format, ..
 #define g_vsnprintf vsnprintf
 #define g_vasprintf vasprintf
 
-gsize       g_strlcpy          (gchar *dest, const gchar *src, gsize dest_size);
+gsize   g_strlcpy            (gchar *dest, const gchar *src, gsize dest_size);
+gchar  *g_stpcpy             (gchar *dest, const char *src);
+
 
 gchar   g_ascii_tolower      (gchar c);
+gchar   g_ascii_toupper      (gchar c);
 gchar  *g_ascii_strdown      (const gchar *str, gssize len);
+gchar  *g_ascii_strup        (const gchar *str, gssize len);
 gint    g_ascii_strncasecmp  (const gchar *s1, const gchar *s2, gsize n);
+gint    g_ascii_strcasecmp   (const gchar *s1, const gchar *s2);
 gint    g_ascii_xdigit_value (gchar c);
 #define g_ascii_isspace(c)   (isspace (c) != 0)
 #define g_ascii_isalpha(c)   (isalpha (c) != 0)
@@ -315,12 +271,10 @@ gint    g_ascii_xdigit_value (gchar c);
 /* FIXME: g_strcasecmp supports utf8 unicode stuff */
 #ifdef _MSC_VER
 #define g_strcasecmp stricmp
-#define g_ascii_strcasecmp stricmp
 #define g_strncasecmp strnicmp
 #define g_strstrip(a) g_strchug (g_strchomp (a))
 #else
 #define g_strcasecmp strcasecmp
-#define g_ascii_strcasecmp strcasecmp
 #define g_ascii_strtoull strtoull
 #define g_strncasecmp strncasecmp
 #define g_strstrip(a) g_strchug (g_strchomp (a))
@@ -359,6 +313,16 @@ GString     *g_string_erase         (GString *string, gssize pos, gssize len);
 
 #define g_string_sprintfa g_string_append_printf
 
+typedef void     (*GFunc)          (gpointer data, gpointer user_data);
+typedef gint     (*GCompareFunc)   (gconstpointer a, gconstpointer b);
+typedef gint     (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
+typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
+typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
+typedef void     (*GDestroyNotify) (gpointer data);
+typedef guint    (*GHashFunc)      (gconstpointer key);
+typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
+typedef void     (*GFreeFunc)      (gpointer       data);
+
 /*
  * Lists
  */
@@ -470,6 +434,52 @@ GList *g_list_insert_before (GList         *list,
 GList *g_list_sort          (GList         *sort,
                             GCompareFunc   func);
 
+/*
+ * Hashtables
+ */
+typedef struct _GHashTable GHashTable;
+typedef struct _GHashTableIter GHashTableIter;
+
+/* Private, but needed for stack allocation */
+struct _GHashTableIter
+{
+       gpointer dummy [8];
+};
+
+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);
+void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
+guint           g_hash_table_size            (GHashTable *hash);
+GList          *g_hash_table_get_keys        (GHashTable *hash);
+GList          *g_hash_table_get_values      (GHashTable *hash);
+gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
+gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
+void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
+gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
+gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
+gboolean        g_hash_table_steal           (GHashTable *hash, gconstpointer key);
+void            g_hash_table_remove_all      (GHashTable *hash);
+guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
+guint           g_hash_table_foreach_steal   (GHashTable *hash, GHRFunc func, gpointer user_data);
+void            g_hash_table_destroy         (GHashTable *hash);
+void            g_hash_table_print_stats     (GHashTable *table);
+
+void            g_hash_table_iter_init       (GHashTableIter *iter, GHashTable *hash_table);
+gboolean        g_hash_table_iter_next       (GHashTableIter *iter, gpointer *key, gpointer *value);
+
+guint           g_spaced_primes_closest      (guint x);
+
+#define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
+#define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
+
+gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
+guint    g_direct_hash  (gconstpointer v1);
+gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
+guint    g_int_hash     (gconstpointer v1);
+gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
+guint    g_str_hash     (gconstpointer v1);
+
 /*
  * ByteArray
  */
@@ -738,12 +748,14 @@ typedef enum {
 gchar     *g_utf8_strup (const gchar *str, gssize len);
 gchar     *g_utf8_strdown (const gchar *str, gssize len);
 gint       g_unichar_to_utf8 (gunichar c, gchar *outbuf);
-gunichar*  g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written);
-gunichar2 *g_utf8_to_utf16 (const gchar *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);
-gunichar  *g_utf16_to_ucs4 (const gunichar2 *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);
-gunichar2 *g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **error);
+gunichar  *g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written);
+gunichar  *g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err);
+gunichar2 *g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err);
+gunichar2 *eg_utf8_to_utf16_with_nuls (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err);
+gchar     *g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err);
+gunichar  *g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err);
+gchar     *g_ucs4_to_utf8  (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err);
+gunichar2 *g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err);
 
 #define u8to16(str) g_utf8_to_utf16(str, (glong)strlen(str), NULL, NULL, NULL)
 
@@ -824,6 +836,10 @@ void g_usleep (gulong microseconds);
  * File
  */
 
+gpointer g_file_error_quark (void);
+
+#define G_FILE_ERROR g_file_error_quark ()
+
 typedef enum {
        G_FILE_ERROR_EXIST,
        G_FILE_ERROR_ISDIR,
@@ -861,6 +877,7 @@ typedef enum {
 } GFileTest;
 
 
+gboolean   g_file_set_contents (const gchar *filename, const gchar *contents, gssize length, GError **error);
 gboolean   g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error);
 GFileError g_file_error_from_errno (gint err_no);
 gint       g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error);
@@ -952,14 +969,11 @@ gboolean         g_markup_parse_context_end_parse (GMarkupParseContext *context,
 /*
  * Character set conversion
  */
-/*
-* Index into the table below with the first byte of a UTF-8 sequence to
-* get the number of trailing bytes that are supposed to follow it.
-* Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
-* left as-is for anyone who may want to do such conversion, which was
-* allowed in earlier algorithms.
-*/
-extern const gchar g_trailingBytesForUTF8[256];
+typedef struct _GIConv *GIConv;
+
+gsize g_iconv (GIConv cd, gchar **inbytes, gsize *inbytesleft, gchar **outbytes, gsize *outbytesleft);
+GIConv g_iconv_open (const gchar *to_charset, const gchar *from_charset);
+int g_iconv_close (GIConv cd);
 
 gboolean  g_get_charset        (G_CONST_RETURN char **charset);
 gchar    *g_locale_to_utf8     (const gchar *opsysstring, gssize len,
@@ -972,11 +986,20 @@ gchar    *g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *byte
 gchar    *g_convert            (const gchar *str, gssize len,
                                const gchar *to_codeset, const gchar *from_codeset,
                                gsize *bytes_read, gsize *bytes_written, GError **error);
+
+/*
+ * Unicode manipulation
+ */
+extern const guchar g_utf8_jump_table[256];
+
 gboolean  g_utf8_validate      (const gchar *str, gssize max_len, const gchar **end);
+gunichar  g_utf8_get_char_validated (const gchar *str, gssize max_len);
+gchar    *g_utf8_find_prev_char (const char *str, const char *p);
+gchar    *g_utf8_prev_char     (const char *str);
+#define   g_utf8_next_char(p)  ((p) + g_utf8_jump_table[(guchar)(*p)])
 gunichar  g_utf8_get_char      (const gchar *src);
 glong     g_utf8_strlen        (const gchar *str, gssize max);
-#define   g_utf8_next_char(p) p + (g_trailingBytesForUTF8[(guchar)(*p)] + 1)
-gchar *   g_utf8_offset_to_pointer (const gchar *str, glong offset);
+gchar    *g_utf8_offset_to_pointer (const gchar *str, glong offset);
 glong     g_utf8_pointer_to_offset (const gchar *str, const gchar *pos);
 
 /*
@@ -1013,40 +1036,48 @@ glong     g_utf8_pointer_to_offset (const gchar *str, const gchar *pos);
                                  
  
 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
-#   define GUINT32_TO_LE(x) (x)
-#   define GUINT64_TO_LE(x) (x)
-#   define GUINT16_TO_LE(x) (x)
-#   define GUINT_TO_LE(x)   (x)
-#   define GUINT32_TO_BE(x) GUINT32_SWAP_LE_BE(x)
-#   define GUINT16_FROM_BE(x) GUINT16_SWAP_LE_BE(x)
-#   define GUINT32_FROM_BE(x) GUINT32_SWAP_LE_BE(x)
 #   define GUINT64_FROM_BE(x) GUINT64_SWAP_LE_BE(x)
-#   define GINT16_FROM_BE(x) GUINT16_SWAP_LE_BE(x)
-#   define GINT32_FROM_BE(x) GUINT32_SWAP_LE_BE(x)
-#   define GINT64_FROM_BE(x) GUINT64_SWAP_LE_BE(x)
+#   define GUINT32_FROM_BE(x) GUINT32_SWAP_LE_BE(x)
+#   define GUINT16_FROM_BE(x) GUINT16_SWAP_LE_BE(x)
+#   define GUINT_FROM_BE(x)   GUINT32_SWAP_LE_BE(x)
+#   define GUINT64_FROM_LE(x) (x)
+#   define GUINT32_FROM_LE(x) (x)
+#   define GUINT16_FROM_LE(x) (x)
+#   define GUINT_FROM_LE(x)   (x)
+#   define GUINT64_TO_BE(x)   GUINT64_SWAP_LE_BE(x)
+#   define GUINT32_TO_BE(x)   GUINT32_SWAP_LE_BE(x)
+#   define GUINT16_TO_BE(x)   GUINT16_SWAP_LE_BE(x)
+#   define GUINT_TO_BE(x)     GUINT32_SWAP_LE_BE(x)
+#   define GUINT64_TO_LE(x)   (x)
+#   define GUINT32_TO_LE(x)   (x)
+#   define GUINT16_TO_LE(x)   (x)
+#   define GUINT_TO_LE(x)     (x)
 #else
-#   define GUINT32_TO_LE(x) GUINT32_SWAP_LE_BE(x)
-#   define GUINT64_TO_LE(x) GUINT64_SWAP_LE_BE(x)
-#   define GUINT16_TO_LE(x) GUINT16_SWAP_LE_BE(x)
-#   define GUINT_TO_LE(x)   GUINT32_SWAP_LE_BE(x)
-#   define GUINT32_TO_BE(x) (x)
-#   define GUINT16_FROM_BE(x) (x)
-#   define GUINT32_FROM_BE(x) (x)
 #   define GUINT64_FROM_BE(x) (x)
-#   define GINT16_FROM_BE(x) (x)
-#   define GINT32_FROM_BE(x) (x)
-#   define GINT64_FROM_BE(x) (x)
+#   define GUINT32_FROM_BE(x) (x)
+#   define GUINT16_FROM_BE(x) (x)
+#   define GUINT_FROM_BE(x)   (x)
+#   define GUINT64_FROM_LE(x) GUINT64_SWAP_LE_BE(x)
+#   define GUINT32_FROM_LE(x) GUINT32_SWAP_LE_BE(x)
+#   define GUINT16_FROM_LE(x) GUINT16_SWAP_LE_BE(x)
+#   define GUINT_FROM_LE(x)   GUINT32_SWAP_LE_BE(x)
+#   define GUINT64_TO_BE(x)   (x)
+#   define GUINT32_TO_BE(x)   (x)
+#   define GUINT16_TO_BE(x)   (x)
+#   define GUINT_TO_BE(x)     (x)
+#   define GUINT64_TO_LE(x)   GUINT64_SWAP_LE_BE(x)
+#   define GUINT32_TO_LE(x)   GUINT32_SWAP_LE_BE(x)
+#   define GUINT16_TO_LE(x)   GUINT16_SWAP_LE_BE(x)
+#   define GUINT_TO_LE(x)     GUINT32_SWAP_LE_BE(x)
 #endif
 
+#define GINT64_FROM_BE(x)   (GUINT64_TO_BE (x))
+#define GINT32_FROM_BE(x)   (GUINT32_TO_BE (x))
+#define GINT16_FROM_BE(x)   (GUINT16_TO_BE (x))
 #define GINT64_FROM_LE(x)   (GUINT64_TO_LE (x))
 #define GINT32_FROM_LE(x)   (GUINT32_TO_LE (x))
 #define GINT16_FROM_LE(x)   (GUINT16_TO_LE (x))
 
-#define GUINT32_FROM_LE(x)  (GUINT32_TO_LE (x))
-#define GUINT64_FROM_LE(x)  (GUINT64_TO_LE (x))
-#define GUINT16_FROM_LE(x)  (GUINT16_TO_LE (x))
-#define GUINT_FROM_LE(x)    (GUINT_TO_LE (x))
-
 #define _EGLIB_MAJOR  2
 #define _EGLIB_MIDDLE 4
 #define _EGLIB_MINOR  0