root / libsylph / utils.h @ 1694
History | View | Annotate | Download (14 kB)
| 1 | /*
|
|---|---|
| 2 | * LibSylph -- E-Mail client library |
| 3 | * Copyright (C) 1999-2007 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef __UTILS_H__
|
| 21 | #define __UTILS_H__
|
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H
|
| 24 | # include "config.h" |
| 25 | #endif
|
| 26 | |
| 27 | #include <glib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <dirent.h> |
| 34 | #include <time.h> |
| 35 | #if HAVE_ALLOCA_H
|
| 36 | # include <alloca.h> |
| 37 | #endif
|
| 38 | |
| 39 | /* Wrappers for C library function that take pathname arguments. */
|
| 40 | #if GLIB_CHECK_VERSION(2, 6, 0) |
| 41 | # include <glib/gstdio.h> |
| 42 | #else
|
| 43 | |
| 44 | #define g_open open
|
| 45 | #define g_rename rename
|
| 46 | #define g_mkdir mkdir
|
| 47 | #define g_stat stat
|
| 48 | #define g_lstat lstat
|
| 49 | #define g_unlink unlink
|
| 50 | #define g_remove remove
|
| 51 | #define g_rmdir rmdir
|
| 52 | #define g_fopen fopen
|
| 53 | #define g_freopen freopen
|
| 54 | |
| 55 | #endif /* GLIB_CHECK_VERSION */ |
| 56 | |
| 57 | #if !GLIB_CHECK_VERSION(2, 7, 0) |
| 58 | |
| 59 | #ifdef G_OS_UNIX
|
| 60 | #define g_chdir chdir
|
| 61 | #define g_chmod chmod
|
| 62 | #else
|
| 63 | gint g_chdir (const gchar *path);
|
| 64 | gint g_chmod (const gchar *path,
|
| 65 | gint mode); |
| 66 | #endif /* G_OS_UNIX */ |
| 67 | |
| 68 | #endif /* !GLIB_CHECK_VERSION */ |
| 69 | |
| 70 | /* The AC_CHECK_SIZEOF() in configure fails for some machines.
|
| 71 | * we provide some fallback values here */ |
| 72 | #if !SIZEOF_UNSIGNED_SHORT
|
| 73 | #undef SIZEOF_UNSIGNED_SHORT
|
| 74 | #define SIZEOF_UNSIGNED_SHORT 2 |
| 75 | #endif
|
| 76 | #if !SIZEOF_UNSIGNED_INT
|
| 77 | #undef SIZEOF_UNSIGNED_INT
|
| 78 | #define SIZEOF_UNSIGNED_INT 4 |
| 79 | #endif
|
| 80 | #if !SIZEOF_UNSIGNED_LONG
|
| 81 | #undef SIZEOF_UNSIGNED_LONG
|
| 82 | #define SIZEOF_UNSIGNED_LONG 4 |
| 83 | #endif
|
| 84 | |
| 85 | #ifndef HAVE_U32_TYPEDEF
|
| 86 | #undef u32 /* maybe there is a macro with this name */ |
| 87 | typedef guint32 u32;
|
| 88 | #define HAVE_U32_TYPEDEF
|
| 89 | #endif
|
| 90 | |
| 91 | #ifndef BIG_ENDIAN_HOST
|
| 92 | #if (G_BYTE_ORDER == G_BIG_ENDIAN)
|
| 93 | #define BIG_ENDIAN_HOST 1 |
| 94 | #endif
|
| 95 | #endif
|
| 96 | |
| 97 | #define CHDIR_RETURN_IF_FAIL(dir) \
|
| 98 | { \
|
| 99 | if (change_dir(dir) < 0) return; \ |
| 100 | } |
| 101 | |
| 102 | #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
|
| 103 | { \
|
| 104 | if (change_dir(dir) < 0) return val; \ |
| 105 | } |
| 106 | |
| 107 | #define Xalloca(ptr, size, iffail) \
|
| 108 | { \
|
| 109 | if ((ptr = alloca(size)) == NULL) { \ |
| 110 | g_warning("can't allocate memory\n"); \
|
| 111 | iffail; \ |
| 112 | } \ |
| 113 | } |
| 114 | |
| 115 | #define Xstrdup_a(ptr, str, iffail) \
|
| 116 | { \
|
| 117 | gchar *__tmp; \ |
| 118 | \ |
| 119 | if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \ |
| 120 | g_warning("can't allocate memory\n"); \
|
| 121 | iffail; \ |
| 122 | } else \
|
| 123 | strcpy(__tmp, str); \ |
| 124 | \ |
| 125 | ptr = __tmp; \ |
| 126 | } |
| 127 | |
| 128 | #define Xstrndup_a(ptr, str, len, iffail) \
|
| 129 | { \
|
| 130 | gchar *__tmp; \ |
| 131 | \ |
| 132 | if ((__tmp = alloca(len + 1)) == NULL) { \ |
| 133 | g_warning("can't allocate memory\n"); \
|
| 134 | iffail; \ |
| 135 | } else { \
|
| 136 | strncpy(__tmp, str, len); \ |
| 137 | __tmp[len] = '\0'; \
|
| 138 | } \ |
| 139 | \ |
| 140 | ptr = __tmp; \ |
| 141 | } |
| 142 | |
| 143 | #define Xstrcat_a(ptr, str1, str2, iffail) \
|
| 144 | { \
|
| 145 | gchar *__tmp; \ |
| 146 | gint len1, len2; \ |
| 147 | \ |
| 148 | len1 = strlen(str1); \ |
| 149 | len2 = strlen(str2); \ |
| 150 | if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \ |
| 151 | g_warning("can't allocate memory\n"); \
|
| 152 | iffail; \ |
| 153 | } else { \
|
| 154 | memcpy(__tmp, str1, len1); \ |
| 155 | memcpy(__tmp + len1, str2, len2 + 1); \
|
| 156 | } \ |
| 157 | \ |
| 158 | ptr = __tmp; \ |
| 159 | } |
| 160 | |
| 161 | #define AUTORELEASE_STR(str, iffail) \
|
| 162 | { \
|
| 163 | gchar *__str; \ |
| 164 | Xstrdup_a(__str, str, iffail); \ |
| 165 | g_free(str); \ |
| 166 | str = __str; \ |
| 167 | } |
| 168 | |
| 169 | #define FILE_OP_ERROR(file, func) \
|
| 170 | { \
|
| 171 | fprintf(stderr, "%s: ", file); \
|
| 172 | fflush(stderr); \ |
| 173 | perror(func); \ |
| 174 | } |
| 175 | |
| 176 | typedef void (*UIUpdateFunc) (void); |
| 177 | typedef void (*ProgressFunc) (gint cur, |
| 178 | gint total); |
| 179 | typedef gchar * (*QueryPasswordFunc) (const gchar *server, |
| 180 | const gchar *user);
|
| 181 | typedef void (*LogFunc) (const gchar *str); |
| 182 | |
| 183 | /* for macro expansion */
|
| 184 | #define Str(x) #x |
| 185 | #define Xstr(x) Str(x)
|
| 186 | |
| 187 | void list_free_strings (GList *list);
|
| 188 | void slist_free_strings (GSList *list);
|
| 189 | |
| 190 | void hash_free_strings (GHashTable *table);
|
| 191 | void hash_free_value_mem (GHashTable *table);
|
| 192 | |
| 193 | gint str_case_equal (gconstpointer v, |
| 194 | gconstpointer v2); |
| 195 | guint str_case_hash (gconstpointer key); |
| 196 | |
| 197 | void ptr_array_free_strings (GPtrArray *array);
|
| 198 | |
| 199 | typedef gboolean (*StrFindFunc) (const gchar *haystack, |
| 200 | const gchar *needle);
|
| 201 | |
| 202 | gboolean str_find (const gchar *haystack,
|
| 203 | const gchar *needle);
|
| 204 | gboolean str_case_find (const gchar *haystack,
|
| 205 | const gchar *needle);
|
| 206 | gboolean str_find_equal (const gchar *haystack,
|
| 207 | const gchar *needle);
|
| 208 | gboolean str_case_find_equal (const gchar *haystack,
|
| 209 | const gchar *needle);
|
| 210 | |
| 211 | /* number-string conversion */
|
| 212 | gint to_number (const gchar *nstr);
|
| 213 | gchar *itos_buf (gchar *nstr, |
| 214 | gint n); |
| 215 | gchar *itos (gint n); |
| 216 | gchar *to_human_readable (gint64 size); |
| 217 | |
| 218 | /* alternative string functions */
|
| 219 | gint strcmp2 (const gchar *s1,
|
| 220 | const gchar *s2);
|
| 221 | gint path_cmp (const gchar *s1,
|
| 222 | const gchar *s2);
|
| 223 | gchar *strretchomp (gchar *str); |
| 224 | gchar *strtailchomp (gchar *str, |
| 225 | gchar tail_char); |
| 226 | gchar *strcrchomp (gchar *str); |
| 227 | gchar *strcasestr (const gchar *haystack,
|
| 228 | const gchar *needle);
|
| 229 | gpointer my_memmem (gconstpointer haystack, |
| 230 | size_t haystacklen, |
| 231 | gconstpointer needle, |
| 232 | size_t needlelen); |
| 233 | gchar *strncpy2 (gchar *dest, |
| 234 | const gchar *src,
|
| 235 | size_t n); |
| 236 | |
| 237 | gboolean str_has_suffix_case (const gchar *str,
|
| 238 | const gchar *suffix);
|
| 239 | |
| 240 | gint str_find_format_times (const gchar *haystack,
|
| 241 | gchar ch); |
| 242 | |
| 243 | gboolean is_next_nonascii (const gchar *s);
|
| 244 | gint get_next_word_len (const gchar *s);
|
| 245 | |
| 246 | /* functions for string parsing */
|
| 247 | gint subject_compare (const gchar *s1,
|
| 248 | const gchar *s2);
|
| 249 | gint subject_compare_for_sort (const gchar *s1,
|
| 250 | const gchar *s2);
|
| 251 | void trim_subject_for_compare (gchar *str);
|
| 252 | void trim_subject_for_sort (gchar *str);
|
| 253 | void trim_subject (gchar *str);
|
| 254 | void eliminate_parenthesis (gchar *str,
|
| 255 | gchar op, |
| 256 | gchar cl); |
| 257 | void extract_parenthesis (gchar *str,
|
| 258 | gchar op, |
| 259 | gchar cl); |
| 260 | void extract_parenthesis_with_escape (gchar *str,
|
| 261 | gchar op, |
| 262 | gchar cl); |
| 263 | |
| 264 | void extract_parenthesis_with_skip_quote (gchar *str,
|
| 265 | gchar quote_chr, |
| 266 | gchar op, |
| 267 | gchar cl); |
| 268 | |
| 269 | void eliminate_quote (gchar *str,
|
| 270 | gchar quote_chr); |
| 271 | void extract_quote (gchar *str,
|
| 272 | gchar quote_chr); |
| 273 | void extract_quote_with_escape (gchar *str,
|
| 274 | gchar quote_chr); |
| 275 | void eliminate_address_comment (gchar *str);
|
| 276 | gchar *strchr_with_skip_quote (const gchar *str,
|
| 277 | gint quote_chr, |
| 278 | gint c); |
| 279 | gchar *strrchr_with_skip_quote (const gchar *str,
|
| 280 | gint quote_chr, |
| 281 | gint c); |
| 282 | void extract_address (gchar *str);
|
| 283 | void extract_list_id_str (gchar *str);
|
| 284 | |
| 285 | gchar *normalize_address_field (const gchar *str);
|
| 286 | |
| 287 | gboolean address_equal (const gchar *addr1,
|
| 288 | const gchar *addr2);
|
| 289 | |
| 290 | GSList *address_list_append_orig (GSList *addr_list, |
| 291 | const gchar *str);
|
| 292 | GSList *address_list_append (GSList *addr_list, |
| 293 | const gchar *str);
|
| 294 | GSList *references_list_prepend (GSList *msgid_list, |
| 295 | const gchar *str);
|
| 296 | GSList *references_list_append (GSList *msgid_list, |
| 297 | const gchar *str);
|
| 298 | GSList *newsgroup_list_append (GSList *group_list, |
| 299 | const gchar *str);
|
| 300 | |
| 301 | GList *add_history (GList *list, |
| 302 | const gchar *str);
|
| 303 | |
| 304 | /* modify string */
|
| 305 | void remove_return (gchar *str);
|
| 306 | void remove_space (gchar *str);
|
| 307 | void unfold_line (gchar *str);
|
| 308 | void subst_char (gchar *str,
|
| 309 | gchar orig, |
| 310 | gchar subst); |
| 311 | void subst_chars (gchar *str,
|
| 312 | gchar *orig, |
| 313 | gchar subst); |
| 314 | void subst_null (gchar *str,
|
| 315 | gint len, |
| 316 | gchar subst); |
| 317 | void subst_control (gchar *str,
|
| 318 | gchar subst); |
| 319 | void subst_for_filename (gchar *str);
|
| 320 | |
| 321 | gchar *get_alt_filename (const gchar *filename,
|
| 322 | gint count); |
| 323 | |
| 324 | gboolean is_header_line (const gchar *str);
|
| 325 | gboolean is_ascii_str (const gchar *str);
|
| 326 | |
| 327 | gint get_quote_level (const gchar *str);
|
| 328 | gint check_line_length (const gchar *str,
|
| 329 | gint max_chars, |
| 330 | gint *line); |
| 331 | |
| 332 | gchar *strstr_with_skip_quote (const gchar *haystack,
|
| 333 | const gchar *needle);
|
| 334 | gchar *strchr_parenthesis_close (const gchar *str,
|
| 335 | gchar op, |
| 336 | gchar cl); |
| 337 | |
| 338 | gchar **strsplit_parenthesis (const gchar *str,
|
| 339 | gchar op, |
| 340 | gchar cl, |
| 341 | gint max_tokens); |
| 342 | gchar **strsplit_with_quote (const gchar *str,
|
| 343 | const gchar *delim,
|
| 344 | gint max_tokens); |
| 345 | gchar **strsplit_csv (const gchar *str,
|
| 346 | gchar delim, |
| 347 | gint max_tokens); |
| 348 | |
| 349 | gchar *get_abbrev_newsgroup_name (const gchar *group,
|
| 350 | gint len); |
| 351 | gchar *trim_string (const gchar *str,
|
| 352 | gint len); |
| 353 | gchar *trim_string_before (const gchar *str,
|
| 354 | gint len); |
| 355 | |
| 356 | GList *uri_list_extract_filenames (const gchar *uri_list);
|
| 357 | gboolean is_uri_string (const gchar *str);
|
| 358 | gchar *get_uri_path (const gchar *uri);
|
| 359 | gint get_uri_len (const gchar *str);
|
| 360 | void decode_uri (gchar *decoded_uri,
|
| 361 | const gchar *encoded_uri);
|
| 362 | void decode_xdigit_encoded_str (gchar *decoded,
|
| 363 | const gchar *encoded);
|
| 364 | gchar *encode_uri (const gchar *filename);
|
| 365 | gchar *uriencode_for_filename (const gchar *filename);
|
| 366 | gint scan_mailto_url (const gchar *mailto,
|
| 367 | gchar **to, |
| 368 | gchar **cc, |
| 369 | gchar **bcc, |
| 370 | gchar **subject, |
| 371 | gchar **inreplyto, |
| 372 | gchar **body); |
| 373 | |
| 374 | void set_startup_dir (void); |
| 375 | void set_rc_dir (const gchar *dir); |
| 376 | |
| 377 | /* return static strings */
|
| 378 | const gchar *get_startup_dir (void); |
| 379 | const gchar *get_home_dir (void); |
| 380 | const gchar *get_document_dir (void); |
| 381 | const gchar *get_rc_dir (void); |
| 382 | const gchar *get_old_rc_dir (void); |
| 383 | const gchar *get_mail_base_dir (void); |
| 384 | const gchar *get_news_cache_dir (void); |
| 385 | const gchar *get_imap_cache_dir (void); |
| 386 | const gchar *get_mime_tmp_dir (void); |
| 387 | const gchar *get_template_dir (void); |
| 388 | const gchar *get_tmp_dir (void); |
| 389 | gchar *get_tmp_file (void);
|
| 390 | const gchar *get_domain_name (void); |
| 391 | |
| 392 | /* file / directory handling */
|
| 393 | off_t get_file_size (const gchar *file);
|
| 394 | off_t get_file_size_as_crlf (const gchar *file);
|
| 395 | off_t get_left_file_size (FILE *fp); |
| 396 | |
| 397 | gboolean file_exist (const gchar *file,
|
| 398 | gboolean allow_fifo); |
| 399 | gboolean is_dir_exist (const gchar *dir);
|
| 400 | gboolean is_file_entry_exist (const gchar *file);
|
| 401 | gboolean dirent_is_regular_file (struct dirent *d);
|
| 402 | gboolean dirent_is_directory (struct dirent *d);
|
| 403 | |
| 404 | #define is_file_exist(file) file_exist(file, FALSE)
|
| 405 | #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
|
| 406 | |
| 407 | gint change_dir (const gchar *dir);
|
| 408 | gint make_dir (const gchar *dir);
|
| 409 | gint make_dir_hier (const gchar *dir);
|
| 410 | gint remove_all_files (const gchar *dir);
|
| 411 | gint remove_numbered_files (const gchar *dir,
|
| 412 | guint first, |
| 413 | guint last); |
| 414 | gint remove_all_numbered_files (const gchar *dir);
|
| 415 | gint remove_expired_files (const gchar *dir,
|
| 416 | guint hours); |
| 417 | gint remove_dir_recursive (const gchar *dir);
|
| 418 | gint rename_force (const gchar *oldpath,
|
| 419 | const gchar *newpath);
|
| 420 | gint copy_file (const gchar *src,
|
| 421 | const gchar *dest,
|
| 422 | gboolean keep_backup); |
| 423 | gint copy_dir (const gchar *src,
|
| 424 | const gchar *dest);
|
| 425 | gint move_file (const gchar *src,
|
| 426 | const gchar *dest,
|
| 427 | gboolean overwrite); |
| 428 | gint copy_file_part (FILE *fp, |
| 429 | off_t offset, |
| 430 | size_t length, |
| 431 | const gchar *dest);
|
| 432 | |
| 433 | gchar *canonicalize_str (const gchar *str);
|
| 434 | gint canonicalize_file (const gchar *src,
|
| 435 | const gchar *dest);
|
| 436 | gint canonicalize_file_replace (const gchar *file);
|
| 437 | FILE *canonicalize_file_stream (FILE *fp, |
| 438 | gint *length); |
| 439 | gint uncanonicalize_file (const gchar *src,
|
| 440 | const gchar *dest);
|
| 441 | gint uncanonicalize_file_replace(const gchar *file);
|
| 442 | |
| 443 | gchar *normalize_newlines (const gchar *str);
|
| 444 | gchar *strchomp_all (const gchar *str);
|
| 445 | |
| 446 | FILE *get_outgoing_rfc2822_file (FILE *fp); |
| 447 | gchar *get_outgoing_rfc2822_str (FILE *fp); |
| 448 | gchar *generate_mime_boundary (const gchar *prefix);
|
| 449 | |
| 450 | gint change_file_mode_rw (FILE *fp, |
| 451 | const gchar *file);
|
| 452 | FILE *my_tmpfile (void);
|
| 453 | FILE *str_open_as_stream (const gchar *str);
|
| 454 | gint str_write_to_file (const gchar *str,
|
| 455 | const gchar *file);
|
| 456 | gchar *file_read_to_str (const gchar *file);
|
| 457 | gchar *file_read_stream_to_str (FILE *fp); |
| 458 | |
| 459 | /* process execution */
|
| 460 | gint execute_async (gchar *const argv[]);
|
| 461 | gint execute_sync (gchar *const argv[]);
|
| 462 | gint execute_command_line (const gchar *cmdline,
|
| 463 | gboolean async); |
| 464 | gint execute_open_file (const gchar *file,
|
| 465 | const gchar *content_type);
|
| 466 | gint execute_print_file (const gchar *file);
|
| 467 | gchar *get_command_output (const gchar *cmdline);
|
| 468 | |
| 469 | /* open URI with external browser */
|
| 470 | gint open_uri(const gchar *uri, const gchar *cmdline); |
| 471 | |
| 472 | /* time functions */
|
| 473 | time_t remote_tzoffset_sec (const gchar *zone);
|
| 474 | time_t tzoffset_sec (time_t *now); |
| 475 | gchar *tzoffset (time_t *now); |
| 476 | void get_rfc822_date (gchar *buf,
|
| 477 | gint len); |
| 478 | |
| 479 | size_t my_strftime (gchar *s, |
| 480 | size_t max, |
| 481 | const gchar *format,
|
| 482 | const struct tm *tm); |
| 483 | |
| 484 | /* UI hints */
|
| 485 | void set_ui_update_func (UIUpdateFunc func);
|
| 486 | void ui_update (void); |
| 487 | |
| 488 | void set_progress_func (ProgressFunc func);
|
| 489 | void progress_show (gint cur,
|
| 490 | gint total); |
| 491 | |
| 492 | /* user input */
|
| 493 | void set_input_query_password_func (QueryPasswordFunc func);
|
| 494 | |
| 495 | gchar *input_query_password (const gchar *server,
|
| 496 | const gchar *user);
|
| 497 | |
| 498 | /* logging */
|
| 499 | void set_log_file (const gchar *filename); |
| 500 | void close_log_file (void); |
| 501 | void set_log_verbosity (gboolean verbose);
|
| 502 | gboolean get_debug_mode (void);
|
| 503 | void set_debug_mode (gboolean enable);
|
| 504 | |
| 505 | void set_log_ui_func (LogFunc print_func,
|
| 506 | LogFunc message_func, |
| 507 | LogFunc warning_func, |
| 508 | LogFunc error_func); |
| 509 | |
| 510 | void set_log_show_status_func (LogFunc status_func);
|
| 511 | |
| 512 | void debug_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2); |
| 513 | void status_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2); |
| 514 | |
| 515 | void log_write (const gchar *str, |
| 516 | const gchar *prefix);
|
| 517 | |
| 518 | void log_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2); |
| 519 | void log_message (const gchar *format, ...) G_GNUC_PRINTF(1, 2); |
| 520 | void log_warning (const gchar *format, ...) G_GNUC_PRINTF(1, 2); |
| 521 | void log_error (const gchar *format, ...) G_GNUC_PRINTF(1, 2); |
| 522 | |
| 523 | #endif /* __UTILS_H__ */ |