1 /* fakeglib.c - A shim for applications that require GLib 2 * without the whole kit and kaboodle. 3 * 4 * Copyright (C) 2020 Evan Miller 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #ifndef _mdbfakeglib_h_ 22 #define _mdbfakeglib_h_ 23 24 #include <time.h> 25 #include <locale.h> 26 #include <inttypes.h> 27 #include <strings.h> 28 29 typedef uint16_t guint16; 30 typedef uint32_t guint32; 31 typedef uint64_t guint64; 32 typedef int32_t gint32; 33 typedef char gchar; 34 typedef int gboolean; 35 typedef int gint; 36 typedef unsigned int guint; 37 typedef void * gpointer; 38 typedef const void * gconstpointer; 39 typedef uint8_t guint8; 40 typedef guint32 GQuark; 41 typedef guint32 gunichar; 42 typedef signed long gssize; 43 44 typedef guint (*GHashFunc)(gconstpointer); 45 typedef int (*GCompareFunc)(gconstpointer, gconstpointer); 46 typedef gboolean (*GEqualFunc)(gconstpointer, gconstpointer); 47 typedef void (*GFunc) (gpointer data, gpointer user_data); 48 typedef void (*GHFunc)(gpointer key, gpointer value, gpointer data); 49 typedef gboolean (*GHRFunc)(gpointer key, gpointer value, gpointer data); 50 51 typedef struct GString { 52 gchar *str; 53 size_t len; 54 size_t allocated_len; 55 } GString; 56 57 typedef struct GPtrArray { 58 void **pdata; 59 guint len; 60 } GPtrArray; 61 62 typedef struct GList { 63 gpointer data; 64 struct GList *next; 65 struct GList *prev; 66 } GList; 67 68 typedef struct GHashTable { 69 GEqualFunc compare; 70 GPtrArray *array; 71 } GHashTable; 72 73 typedef struct GError { 74 GQuark domain; 75 gint code; 76 gchar *message; 77 } GError; 78 79 typedef enum GOptionArg { 80 G_OPTION_ARG_NONE, 81 G_OPTION_ARG_STRING, 82 G_OPTION_ARG_INT, 83 G_OPTION_ARG_CALLBACK, 84 G_OPTION_ARG_FILENAME 85 } GOptionArg; 86 87 typedef enum GOptionFlags { 88 G_OPTION_FLAG_NONE, 89 G_OPTION_FLAG_REVERSE 90 } GOptionFlags; 91 92 typedef struct GOptionEntry { 93 const gchar *long_name; 94 gchar short_name; 95 gint flags; 96 97 GOptionArg arg; 98 gpointer arg_data; 99 100 const gchar *description; 101 const gchar *arg_description; 102 } GOptionEntry; 103 104 typedef struct GOptionContext { 105 const char *desc; 106 const GOptionEntry *entries; 107 } GOptionContext; 108 109 #define g_str_hash NULL 110 111 #define G_GUINT32_FORMAT PRIu32 112 113 #define g_return_val_if_fail(a, b) if (!a) { return b; } 114 115 #define g_ascii_strcasecmp strcasecmp 116 #define g_malloc0(len) calloc(1, len) 117 #define g_malloc malloc 118 #define g_free free 119 #define g_realloc realloc 120 121 #define G_STR_DELIMITERS "_-|> <." 122 123 #define g_ptr_array_index(array, i) \ 124 ((void **)array->pdata)[i] 125 126 #define TRUE 1 127 #define FALSE 0 128 129 #define GUINT32_SWAP_LE_BE(l) __builtin_bswap32((uint32_t)(l)) 130 131 /* string functions */ 132 void *g_memdup(const void *src, size_t len); 133 int g_str_equal(const void *str1, const void *str2); 134 char **g_strsplit(const char *haystack, const char *needle, int max_tokens); 135 void g_strfreev(char **dir); 136 char *g_strconcat(const char *first, ...); 137 char *g_strdup(const char *src); 138 char *g_strndup(const char *src, size_t len); 139 char *g_strdup_printf(const char *format, ...); 140 gchar *g_strdelimit(gchar *string, const gchar *delimiters, gchar new_delimiter); 141 void g_printerr(const gchar *format, ...); 142 143 /* conversion */ 144 gint g_unichar_to_utf8(gunichar c, gchar *dst); 145 gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len, 146 size_t *bytes_read, size_t *bytes_written, GError **error); 147 gchar *g_utf8_casefold(const gchar *str, gssize len); 148 gchar *g_utf8_strdown(const gchar *str, gssize len); 149 150 /* GString */ 151 GString *g_string_new(const gchar *init); 152 GString *g_string_assign(GString *string, const gchar *rval); 153 GString * g_string_append (GString *string, const gchar *val); 154 gchar *g_string_free (GString *string, gboolean free_segment); 155 156 /* GHashTable */ 157 void *g_hash_table_lookup(GHashTable *tree, const void *key); 158 gboolean g_hash_table_lookup_extended(GHashTable *table, const void *lookup_key, 159 void **orig_key, void **value); 160 void g_hash_table_insert(GHashTable *tree, void *key, void *value); 161 gboolean g_hash_table_remove(GHashTable *hash_table, const void *key); 162 GHashTable *g_hash_table_new(GHashFunc hashes, GEqualFunc equals); 163 void g_hash_table_foreach(GHashTable *tree, GHFunc function, void *data); 164 void g_hash_table_foreach_remove(GHashTable *tree, GHRFunc function, void *data); 165 void g_hash_table_destroy(GHashTable *tree); 166 167 /* GPtrArray */ 168 void g_ptr_array_sort(GPtrArray *array, GCompareFunc func); 169 void g_ptr_array_foreach(GPtrArray *array, GFunc function, gpointer user_data); 170 GPtrArray *g_ptr_array_new(void); 171 void g_ptr_array_add(GPtrArray *array, void *entry); 172 gboolean g_ptr_array_remove (GPtrArray *array, gpointer data); 173 void g_ptr_array_free(GPtrArray *array, gboolean something); 174 175 /* GList */ 176 GList *g_list_append(GList *list, void *data); 177 GList *g_list_last(GList *list); 178 GList *g_list_remove(GList *list, void *data); 179 void g_list_free(GList *list); 180 181 /* GOption */ 182 GOptionContext *g_option_context_new(const char *description); 183 void g_option_context_add_main_entries (GOptionContext *context, 184 const GOptionEntry *entries, 185 const gchar *translation_domain); 186 gchar *g_option_context_get_help (GOptionContext *context, 187 gboolean main_help, void *group); 188 gboolean g_option_context_parse (GOptionContext *context, 189 gint *argc, gchar ***argv, GError **error); 190 void g_option_context_free (GOptionContext *context); 191 192 #endif 193