| 1 | /* GLib compatibility functions for C++ migration |
| 2 | * |
| 3 | * This header provides portable replacements for commonly used GLib functions |
| 4 | * to facilitate removal of GLib dependency from FontForge core. |
| 5 | * |
| 6 | * Function declarations are C-compatible; implementations in ffglib_compat.cpp |
| 7 | * use C++ standard library for portability. |
| 8 | */ |
| 9 | |
| 10 | #ifndef FONTFORGE_FFGLIB_COMPAT_H |
| 11 | #define FONTFORGE_FFGLIB_COMPAT_H |
| 12 | |
| 13 | #include <stddef.h> |
| 14 | |
| 15 | /* GLib boolean constants */ |
| 16 | #ifndef FALSE |
| 17 | #define FALSE 0 |
| 18 | #endif |
| 19 | #ifndef TRUE |
| 20 | #define TRUE 1 |
| 21 | #endif |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | /* ============================================================================ |
| 28 | * String functions |
| 29 | * ============================================================================ |
| 30 | */ |
| 31 | |
| 32 | /* Count UTF-8 characters in a string. |
| 33 | * maxlen: maximum bytes to scan (-1 for NUL-terminated) |
| 34 | * Returns: number of UTF-8 characters (not bytes) |
| 35 | */ |
| 36 | size_t ff_utf8_strlen(const char* str, int maxlen); |
| 37 | |
| 38 | /* Locale-independent string to double conversion for PostScript data. |
| 39 | * Always uses '.' as decimal separator regardless of locale. |
| 40 | * Callers must strip leading whitespace; no leading '+' is expected. |
| 41 | */ |
| 42 | double ff_strtod(const char* nptr, char** endptr); |
| 43 | |
| 44 | /* Locale-independent double to string conversion. |
| 45 | * Always uses '.' as decimal separator regardless of locale. |
| 46 | * Compatible with g_ascii_formatd(). |
| 47 | * Returns: dest |
| 48 | */ |
| 49 | char* ff_ascii_formatd(char* dest, size_t dest_len, const char* format, |
| 50 | double value); |
| 51 | |
| 52 | /* Strip leading and trailing whitespace in-place. |
| 53 | * Compatible with g_strstrip(). |
| 54 | * Returns: str (modified in place) |
| 55 | */ |
| 56 | char* ff_strstrip(char* str); |
| 57 | |
| 58 | /* Case-insensitive ASCII string comparison. |
| 59 | * Compatible with g_ascii_strcasecmp(). |
| 60 | */ |
| 61 | int ff_ascii_strcasecmp(const char* s1, const char* s2); |
| 62 | |
| 63 | /* Convert ASCII string to lowercase. |
| 64 | * Compatible with g_ascii_strdown(). |
| 65 | * Returns: newly allocated lowercase string (caller must free) |
| 66 | */ |
| 67 | char* ff_ascii_strdown(const char* str, int len); |
| 68 | |
| 69 | /* Decode percent-encoded URI string. |
| 70 | * Compatible with g_uri_unescape_string(). |
| 71 | * Returns: newly allocated string (caller must free) |
| 72 | */ |
| 73 | char* ff_uri_unescape_string(const char* escaped, |
| 74 | const char* illegal_characters); |
| 75 | |
| 76 | /* ============================================================================ |
| 77 | * Random number functions |
| 78 | * ============================================================================ |
| 79 | */ |
| 80 | |
| 81 | /* Seed the random number generator */ |
| 82 | void ff_random_set_seed(unsigned int seed); |
| 83 | |
| 84 | /* Return a random double in [0.0, 1.0) */ |
| 85 | double ff_random_double(void); |
| 86 | |
| 87 | /* Return a random 32-bit integer */ |
| 88 | int ff_random_int(void); |
| 89 | |
| 90 | /* Return a random integer in [begin, end) */ |
| 91 | int ff_random_int_range(int begin, int end); |
| 92 | |
| 93 | /* ============================================================================ |
| 94 | * System/locale functions |
| 95 | * ============================================================================ |
| 96 | */ |
| 97 | |
| 98 | /* Get the system character set. |
| 99 | * Sets *charset to the charset name (static buffer, do not free). |
| 100 | * Returns: 1 if charset is UTF-8, 0 otherwise. |
| 101 | * Compatible with g_get_charset(). |
| 102 | */ |
| 103 | int ff_get_charset(const char** charset); |
| 104 | |
| 105 | /* Get UTC offset in seconds for local time. |
| 106 | * Replaces GDateTime timezone handling. |
| 107 | */ |
| 108 | long ff_get_utc_offset(long timestamp); |
| 109 | |
| 110 | /* Get the real name of the current user. |
| 111 | * Compatible with g_get_real_name(). |
| 112 | * Returns: static buffer containing user's real name (do not free) |
| 113 | */ |
| 114 | const char* ff_get_real_name(void); |
| 115 | |
| 116 | /* Get the system temporary directory. |
| 117 | * Compatible with g_get_tmp_dir(). |
| 118 | * Returns: static buffer containing temp directory path (do not free) |
| 119 | */ |
| 120 | const char* ff_get_tmp_dir(void); |
| 121 | |
| 122 | /* ============================================================================ |
| 123 | * POSIX compatibility for MSVC |
| 124 | * ============================================================================ |
| 125 | */ |
| 126 | |
| 127 | #ifdef _MSC_VER |
| 128 | /* MSVC uses strtok_s instead of strtok_r (same signature) */ |
| 129 | #define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr) |
| 130 | |
| 131 | /* MSVC uses _stricmp instead of strcasecmp */ |
| 132 | #define strcasecmp _stricmp |
| 133 | #define strncasecmp _strnicmp |
| 134 | #endif |
| 135 | |
| 136 | /* ============================================================================ |
| 137 | * Filesystem functions (portable wrappers using std::filesystem) |
| 138 | * ============================================================================ |
| 139 | */ |
| 140 | |
| 141 | /* access() mode flags - match POSIX values */ |
| 142 | #ifndef F_OK |
| 143 | #define F_OK 0 /* Test for existence */ |
| 144 | #define X_OK 1 /* Test for execute permission */ |
| 145 | #define W_OK 2 /* Test for write permission */ |
| 146 | #define R_OK 4 /* Test for read permission */ |
| 147 | #endif |
| 148 | |
| 149 | /* Check file accessibility. Returns 0 on success, -1 on failure. |
| 150 | * Replaces POSIX access(). |
| 151 | */ |
| 152 | int ff_access(const char* path, int mode); |
| 153 | |
| 154 | /* Delete a file. Returns 0 on success, -1 on failure. |
| 155 | * Replaces POSIX unlink(). |
| 156 | */ |
| 157 | int ff_unlink(const char* path); |
| 158 | |
| 159 | /* Remove an empty directory. Returns 0 on success, -1 on failure. |
| 160 | * Replaces POSIX rmdir(). |
| 161 | */ |
| 162 | int ff_rmdir(const char* path); |
| 163 | |
| 164 | /* Create a directory. Returns 0 on success, -1 on failure. |
| 165 | * Replaces POSIX mkdir(). |
| 166 | */ |
| 167 | int ff_mkdir(const char* path, int mode); |
| 168 | |
| 169 | /* Change current working directory. Returns 0 on success, -1 on failure. |
| 170 | * Replaces POSIX chdir(). |
| 171 | */ |
| 172 | int ff_chdir(const char* path); |
| 173 | |
| 174 | /* Get current working directory. Returns buffer on success, NULL on failure. |
| 175 | * Replaces POSIX getcwd(). |
| 176 | */ |
| 177 | char* ff_getcwd(char* buf, size_t size); |
| 178 | |
| 179 | /* Get canonical (absolute, normalized) path. |
| 180 | * Replaces g_canonicalize_filename(). |
| 181 | * Returns: newly allocated string (caller must free), or NULL on error. |
| 182 | */ |
| 183 | char* ff_canonical_path(const char* path); |
| 184 | |
| 185 | /* Remove a file or directory recursively. |
| 186 | * Replaces g_remove() + directory iteration for recursive delete. |
| 187 | * Returns: number of files/directories removed, or -1 on error. |
| 188 | */ |
| 189 | int ff_remove_all(const char* path); |
| 190 | |
| 191 | /* ============================================================================ |
| 192 | * Dynamic array (replaces GArray) |
| 193 | * ============================================================================ |
| 194 | */ |
| 195 | |
| 196 | typedef struct FFArray FFArray; |
| 197 | |
| 198 | /* Create a new array with specified element size and initial capacity */ |
| 199 | FFArray* ff_array_sized_new(int zero_terminated, int clear, size_t element_size, |
| 200 | size_t initial_capacity); |
| 201 | |
| 202 | /* Append a value to the array. |
| 203 | * Usage: ff_array_append(arr, &value); |
| 204 | */ |
| 205 | void ff_array_append(FFArray* arr, const void* val); |
| 206 | |
| 207 | /* Get pointer to array data */ |
| 208 | void* ff_array_data(FFArray* arr); |
| 209 | |
| 210 | /* Get number of elements in array */ |
| 211 | size_t ff_array_len(FFArray* arr); |
| 212 | |
| 213 | /* Free the array and optionally its data, nulls the pointer */ |
| 214 | void ff_array_free(FFArray** p_arr, int free_data); |
| 215 | |
| 216 | /* ============================================================================ |
| 217 | * Linked list (replaces GList) |
| 218 | * ============================================================================ |
| 219 | */ |
| 220 | |
| 221 | typedef struct FFList { |
| 222 | void* data; |
| 223 | struct FFList* next; |
| 224 | } FFList; |
| 225 | |
| 226 | /* Append data to end of list. Returns new head of list. |
| 227 | * Compatible with g_list_append(). |
| 228 | */ |
| 229 | FFList* ff_list_append(FFList* list, void* data); |
| 230 | |
| 231 | /* Free list structure (not the data). |
| 232 | * Compatible with g_list_free(). |
| 233 | */ |
| 234 | void ff_list_free(FFList* list); |
| 235 | |
| 236 | /* Count number of elements in list. |
| 237 | * Compatible with g_list_length(). |
| 238 | */ |
| 239 | unsigned int ff_list_length(FFList* list); |
| 240 | |
| 241 | /* Set pointer to NULL and return old value. |
| 242 | * Compatible with g_steal_pointer(). |
| 243 | */ |
| 244 | void* ff_steal_pointer_impl(void** pp); |
| 245 | |
| 246 | /* ============================================================================ |
| 247 | * Regex functions |
| 248 | * ============================================================================ |
| 249 | */ |
| 250 | |
| 251 | /* Check if string matches regex pattern (case-insensitive). |
| 252 | * Returns 1 if match, 0 if no match or error. |
| 253 | */ |
| 254 | int ff_regex_match(const char* str, const char* pattern); |
| 255 | |
| 256 | /* ============================================================================ |
| 257 | * Sorted string set (case-insensitive, no duplicates) |
| 258 | * ============================================================================ |
| 259 | */ |
| 260 | |
| 261 | typedef struct FFStringSet FFStringSet; |
| 262 | |
| 263 | /* Create a new string set */ |
| 264 | FFStringSet* ff_stringset_new(void); |
| 265 | |
| 266 | /* Insert a string (duplicates ignored, case-insensitive) */ |
| 267 | void ff_stringset_insert(FFStringSet* ss, const char* str); |
| 268 | |
| 269 | /* Iterate over strings in sorted order */ |
| 270 | void ff_stringset_foreach(FFStringSet* ss, void (*fn)(const char*, void*), |
| 271 | void* user_data); |
| 272 | |
| 273 | /* Free the string set, nulls the pointer */ |
| 274 | void ff_stringset_free(FFStringSet** p_ss); |
| 275 | |
| 276 | #ifdef __cplusplus |
| 277 | } |
| 278 | |
| 279 | /* C++ type-safe template version */ |
| 280 | template <typename T> |
| 281 | inline T* ff_steal_pointer(T** pp) { |
| 282 | T* tmp = *pp; |
| 283 | *pp = nullptr; |
| 284 | return tmp; |
| 285 | } |
| 286 | |
| 287 | #else |
| 288 | /* C macro version */ |
| 289 | #define ff_steal_pointer(pp) ff_steal_pointer_impl((void**)(pp)) |
| 290 | #endif |
| 291 | |
| 292 | #endif /* FONTFORGE_FFGLIB_COMPAT_H */ |