2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <glib-object.h>
33 #include <sys/types.h>
43 /* Wrappers for C library function that take pathname arguments. */
44 #if GLIB_CHECK_VERSION(2, 6, 0)
45 # include <glib/gstdio.h>
49 #define g_rename rename
53 #define g_unlink unlink
54 #define g_remove remove
57 #define g_freopen freopen
59 #endif /* GLIB_CHECK_VERSION */
61 #if !GLIB_CHECK_VERSION(2, 7, 0)
67 gint g_chdir (const gchar *path);
68 gint g_chmod (const gchar *path,
70 #endif /* G_OS_UNIX */
72 #endif /* !GLIB_CHECK_VERSION */
74 /* The AC_CHECK_SIZEOF() in configure fails for some machines.
75 * we provide some fallback values here */
76 #if !SIZEOF_UNSIGNED_SHORT
77 #undef SIZEOF_UNSIGNED_SHORT
78 #define SIZEOF_UNSIGNED_SHORT 2
80 #if !SIZEOF_UNSIGNED_INT
81 #undef SIZEOF_UNSIGNED_INT
82 #define SIZEOF_UNSIGNED_INT 4
84 #if !SIZEOF_UNSIGNED_LONG
85 #undef SIZEOF_UNSIGNED_LONG
86 #define SIZEOF_UNSIGNED_LONG 4
89 #ifndef HAVE_U32_TYPEDEF
90 #undef u32 /* maybe there is a macro with this name */
92 #define HAVE_U32_TYPEDEF
95 #ifndef BIG_ENDIAN_HOST
96 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
97 #define BIG_ENDIAN_HOST 1
101 #define CHDIR_RETURN_IF_FAIL(dir) \
103 if (change_dir(dir) < 0) return; \
106 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
108 if (change_dir(dir) < 0) return val; \
111 #define Xalloca(ptr, size, iffail) \
113 if ((ptr = alloca(size)) == NULL) { \
114 g_warning("can't allocate memory\n"); \
119 #define Xstrdup_a(ptr, str, iffail) \
123 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
124 g_warning("can't allocate memory\n"); \
127 strcpy(__tmp, str); \
132 #define Xstrndup_a(ptr, str, len, iffail) \
136 if ((__tmp = alloca(len + 1)) == NULL) { \
137 g_warning("can't allocate memory\n"); \
140 strncpy(__tmp, str, len); \
147 #define Xstrcat_a(ptr, str1, str2, iffail) \
152 len1 = strlen(str1); \
153 len2 = strlen(str2); \
154 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
155 g_warning("can't allocate memory\n"); \
158 memcpy(__tmp, str1, len1); \
159 memcpy(__tmp + len1, str2, len2 + 1); \
165 #define AUTORELEASE_STR(str, iffail) \
168 Xstrdup_a(__str, str, iffail); \
173 #define FILE_OP_ERROR(file, func) \
175 fprintf(stderr, "%s: ", file); \
180 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
186 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
188 /* debug functions */
189 void debug_set_mode (gboolean mode);
190 gboolean debug_get_mode (void);
193 #define debug_print \
194 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
197 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
198 #define debug_print \
199 debug_print_real("%s:%d:", __FILE__, __LINE__), \
203 /* for macro expansion */
205 #define Xstr(x) Str(x)
207 void list_free_strings (GList *list);
208 void slist_free_strings (GSList *list);
210 void hash_free_strings (GHashTable *table);
211 void hash_free_value_mem (GHashTable *table);
213 gint str_case_equal (gconstpointer v,
215 guint str_case_hash (gconstpointer key);
217 void ptr_array_free_strings (GPtrArray *array);
219 typedef gboolean (*StrFindFunc) (const gchar *haystack,
220 const gchar *needle);
222 gboolean str_find (const gchar *haystack,
223 const gchar *needle);
224 gboolean str_case_find (const gchar *haystack,
225 const gchar *needle);
226 gboolean str_find_equal (const gchar *haystack,
227 const gchar *needle);
228 gboolean str_case_find_equal (const gchar *haystack,
229 const gchar *needle);
231 /* number-string conversion */
232 gint to_number (const gchar *nstr);
233 gchar *itos_buf (gchar *nstr,
235 gchar *itos (gint n);
236 gchar *to_human_readable (off_t size);
238 /* alternative string functions */
239 gint strcmp2 (const gchar *s1,
241 gchar *strstr2 (const gchar *s1,
243 gint path_cmp (const gchar *s1,
245 gchar *strretchomp (gchar *str);
246 gchar *strtailchomp (gchar *str,
248 gchar *strcrchomp (gchar *str);
249 void file_strip_crs (const gchar *file);
250 gchar *strcasestr (const gchar *haystack,
251 const gchar *needle);
252 gpointer my_memmem (gconstpointer haystack,
254 gconstpointer needle,
256 gchar *strncpy2 (gchar *dest,
260 /* wide-character functions */
262 int iswalnum (wint_t wc);
265 int iswspace (wint_t wc);
268 wint_t towlower (wint_t wc);
272 size_t wcslen (const wchar_t *s);
275 wchar_t *wcscpy (wchar_t *dest,
279 wchar_t *wcsncpy (wchar_t *dest,
284 wchar_t *wcsdup (const wchar_t *s);
285 wchar_t *wcsndup (const wchar_t *s,
287 wchar_t *strdup_mbstowcs (const gchar *s);
288 gchar *strdup_wcstombs (const wchar_t *s);
289 gint wcsncasecmp (const wchar_t *s1,
292 wchar_t *wcscasestr (const wchar_t *haystack,
293 const wchar_t *needle);
294 gint get_mbs_len (const gchar *s);
296 gboolean is_next_nonascii (const gchar *s);
297 gint get_next_word_len (const gchar *s);
299 /* functions for string parsing */
300 gint subject_compare (const gchar *s1,
302 gint subject_compare_for_sort (const gchar *s1,
304 void trim_subject_for_compare (gchar *str);
305 void trim_subject_for_sort (gchar *str);
306 void trim_subject (gchar *str);
307 void eliminate_parenthesis (gchar *str,
310 void extract_parenthesis (gchar *str,
314 void extract_parenthesis_with_skip_quote (gchar *str,
319 void eliminate_quote (gchar *str,
321 void extract_quote (gchar *str,
323 void eliminate_address_comment (gchar *str);
324 gchar *strchr_with_skip_quote (const gchar *str,
327 gchar *strrchr_with_skip_quote (const gchar *str,
330 void extract_address (gchar *str);
331 void extract_list_id_str (gchar *str);
333 GSList *slist_concat_unique (GSList *first,
335 GSList *address_list_append (GSList *addr_list,
337 GSList *address_list_append_with_comments(GSList *addr_list,
339 GSList *references_list_prepend (GSList *msgid_list,
341 GSList *references_list_append (GSList *msgid_list,
343 GSList *newsgroup_list_append (GSList *group_list,
346 GList *add_history (GList *list,
349 void remove_return (gchar *str);
350 void remove_space (gchar *str);
351 void unfold_line (gchar *str);
352 void subst_char (gchar *str,
355 void subst_chars (gchar *str,
358 void subst_for_filename (gchar *str);
359 void subst_for_shellsafe_filename (gchar *str);
360 gboolean is_header_line (const gchar *str);
361 gboolean is_ascii_str (const gchar *str);
362 gint get_quote_level (const gchar *str,
363 const gchar *quote_chars);
364 gint check_line_length (const gchar *str,
368 gchar *strstr_with_skip_quote (const gchar *haystack,
369 const gchar *needle);
370 gchar *strchr_parenthesis_close (const gchar *str,
374 gchar **strsplit_parenthesis (const gchar *str,
378 gchar **strsplit_with_quote (const gchar *str,
382 gchar *get_abbrev_newsgroup_name (const gchar *group,
384 gchar *trim_string (const gchar *str,
387 GList *uri_list_extract_filenames (const gchar *uri_list);
388 gboolean is_uri_string (const gchar *str);
389 gchar *get_uri_path (const gchar *uri);
390 gint get_uri_len (const gchar *str);
391 void decode_uri (gchar *decoded_uri,
392 const gchar *encoded_uri);
393 gint scan_mailto_url (const gchar *mailto,
400 /* return static strings */
401 const gchar *get_home_dir (void);
402 const gchar *get_rc_dir (void);
403 const gchar *get_mail_base_dir (void);
404 const gchar *get_news_cache_dir (void);
405 const gchar *get_imap_cache_dir (void);
406 const gchar *get_mbox_cache_dir (void);
407 const gchar *get_mime_tmp_dir (void);
408 const gchar *get_template_dir (void);
409 const gchar *get_header_cache_dir (void);
410 const gchar *get_plugin_dir (void);
411 const gchar *get_tmp_dir (void);
412 gchar *get_tmp_file (void);
413 const gchar *get_domain_name (void);
415 /* file / directory handling */
416 off_t get_file_size (const gchar *file);
417 off_t get_file_size_as_crlf (const gchar *file);
418 off_t get_left_file_size (FILE *fp);
420 gboolean file_exist (const gchar *file,
421 gboolean allow_fifo);
422 gboolean is_relative_filename (const gchar *file);
423 gboolean is_dir_exist (const gchar *dir);
424 gboolean is_file_entry_exist (const gchar *file);
425 gboolean dirent_is_regular_file (struct dirent *d);
426 gboolean dirent_is_directory (struct dirent *d);
428 #define is_file_exist(file) file_exist(file, FALSE)
429 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
431 gint change_dir (const gchar *dir);
432 gint make_dir (const gchar *dir);
433 gint make_dir_hier (const gchar *dir);
434 gint remove_all_files (const gchar *dir);
435 gint remove_numbered_files (const gchar *dir,
438 gint remove_numbered_files_not_in_list(const gchar *dir,
440 gint remove_all_numbered_files (const gchar *dir);
441 gint remove_expired_files (const gchar *dir,
443 gint remove_dir_recursive (const gchar *dir);
444 gint append_file (const gchar *src,
446 gboolean keep_backup);
447 gint rename_force (const gchar *oldpath,
448 const gchar *newpath);
449 gint copy_file (const gchar *src,
451 gboolean keep_backup);
452 gint move_file (const gchar *src,
455 gint copy_dir (const gchar *src,
457 gint copy_file_part_to_fp (FILE *fp,
461 gint copy_file_part (FILE *fp,
466 gchar *canonicalize_str (const gchar *str);
467 gint canonicalize_file (const gchar *src,
469 gint canonicalize_file_replace (const gchar *file);
470 gint uncanonicalize_file (const gchar *src,
472 gint uncanonicalize_file_replace(const gchar *file);
474 gchar *normalize_newlines (const gchar *str);
476 gchar *get_outgoing_rfc2822_str (FILE *fp);
478 gint change_file_mode_rw (FILE *fp,
480 FILE *my_tmpfile (void);
481 FILE *get_tmpfile_in_dir (const gchar *dir,
483 FILE *str_open_as_stream (const gchar *str);
484 gint str_write_to_file (const gchar *str,
486 gchar *file_read_to_str (const gchar *file);
487 gchar *file_read_stream_to_str (FILE *fp);
489 /* process execution */
490 gint execute_async (gchar *const argv[]);
491 gint execute_sync (gchar *const argv[]);
492 gint execute_command_line (const gchar *cmdline,
494 gchar *get_command_output (const gchar *cmdline);
496 /* open URI with external browser */
497 gint open_uri(const gchar *uri, const gchar *cmdline);
498 /* open file with text editor */
499 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
502 time_t remote_tzoffset_sec (const gchar *zone);
503 time_t tzoffset_sec (time_t *now);
504 gchar *tzoffset (time_t *now);
505 void get_rfc822_date (gchar *buf,
508 size_t my_strftime (gchar *s,
511 const struct tm *tm);
514 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
515 const char * debug_srcname (const char *file);
517 /* subject threading */
518 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
519 void subject_table_insert(GHashTable *subject_table, gchar * subject,
521 void subject_table_remove(GHashTable *subject_table, gchar * subject);
522 gint subject_get_prefix_length (const gchar *subject);
524 /* quoting recognition */
525 const gchar * line_has_quote_char (const gchar *str,
526 const gchar *quote_chars);
527 const gchar * line_has_quote_char_last (const gchar *str,
528 const gchar *quote_chars);
530 guint g_stricase_hash (gconstpointer gptr);
531 gint g_stricase_equal (gconstpointer gptr1, gconstpointer gptr2);
532 gint g_int_compare (gconstpointer a, gconstpointer b);
534 gchar *generate_msgid (gchar *buf, gint len);
535 gchar *generate_mime_boundary (const gchar *prefix);
537 gint quote_cmd_argument(gchar * result, guint size,
539 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
541 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
542 void get_hex_str(gchar *out, guchar ch);
544 /* auto pointer for containers that support GType system */
546 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
547 typedef struct AutoPointer GAuto;
548 GType g_auto_pointer_register (void);
549 GAuto *g_auto_pointer_new (gpointer pointer);
550 GAuto *g_auto_pointer_new_with_free (gpointer p,
552 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
553 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
554 void g_auto_pointer_free (GAuto *auto_ptr);
555 void replace_returns (gchar *str);
557 gboolean get_uri_part(const gchar *start, const gchar *scanpos,
558 const gchar **bp, const gchar **ep, gboolean hdr);
559 gchar *make_uri_string(const gchar *bp, const gchar *ep);
560 gboolean get_email_part(const gchar *start, const gchar *scanpos,
561 const gchar **bp, const gchar **ep, gboolean hdr);
562 gchar *make_email_string(const gchar *bp, const gchar *ep);
563 gchar *make_http_string(const gchar *bp, const gchar *ep);
564 gchar *mailcap_get_command_for_type(const gchar *type);
565 gboolean file_is_email (const gchar *filename);
571 #endif /* __UTILS_H__ */