2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
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 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
113 if (change_dir(dir) < 0) { \
119 #define Xalloca(ptr, size, iffail) \
121 if ((ptr = alloca(size)) == NULL) { \
122 g_warning("can't allocate memory\n"); \
127 #define Xstrdup_a(ptr, str, iffail) \
131 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
132 g_warning("can't allocate memory\n"); \
135 strcpy(__tmp, str); \
140 #define Xstrndup_a(ptr, str, len, iffail) \
144 if ((__tmp = alloca(len + 1)) == NULL) { \
145 g_warning("can't allocate memory\n"); \
148 strncpy(__tmp, str, len); \
155 #define Xstrcat_a(ptr, str1, str2, iffail) \
160 len1 = strlen(str1); \
161 len2 = strlen(str2); \
162 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
163 g_warning("can't allocate memory\n"); \
166 memcpy(__tmp, str1, len1); \
167 memcpy(__tmp + len1, str2, len2 + 1); \
173 #define AUTORELEASE_STR(str, iffail) \
176 Xstrdup_a(__str, str, iffail); \
181 #define FILE_OP_ERROR(file, func) \
183 g_printerr("%s: ", file); \
188 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
194 typedef gpointer (*GNodeMapFunc
) (gpointer nodedata
, gpointer data
);
196 /* debug functions */
197 void debug_set_mode (gboolean mode
);
198 gboolean
debug_get_mode (void);
201 #define debug_print \
202 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
205 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
206 #define debug_print \
207 debug_print_real("%s:%d:", __FILE__, __LINE__), \
211 /* for macro expansion */
213 #define Xstr(x) Str(x)
216 /* System related stuff. */
218 gboolean
superuser_p (void);
221 /* String utilities. */
223 void list_free_strings (GList
*list
);
224 void slist_free_strings (GSList
*list
);
226 void hash_free_strings (GHashTable
*table
);
227 void hash_free_value_mem (GHashTable
*table
);
229 gint
str_case_equal (gconstpointer v
,
231 guint
str_case_hash (gconstpointer key
);
233 void ptr_array_free_strings (GPtrArray
*array
);
235 typedef gboolean (*StrFindFunc
) (const gchar
*haystack
,
236 const gchar
*needle
);
238 gboolean
str_find (const gchar
*haystack
,
239 const gchar
*needle
);
240 gboolean
str_case_find (const gchar
*haystack
,
241 const gchar
*needle
);
242 gboolean
str_find_equal (const gchar
*haystack
,
243 const gchar
*needle
);
244 gboolean
str_case_find_equal (const gchar
*haystack
,
245 const gchar
*needle
);
247 /* number-string conversion */
248 gint
to_number (const gchar
*nstr
);
249 gchar
*itos_buf (gchar
*nstr
,
251 gchar
*itos (gint n
);
252 gchar
*to_human_readable (off_t size
);
254 /* alternative string functions */
255 gint
strcmp2 (const gchar
*s1
,
257 gchar
*strstr2 (const gchar
*s1
,
259 gint
path_cmp (const gchar
*s1
,
261 gchar
*strretchomp (gchar
*str
);
262 gchar
*strtailchomp (gchar
*str
,
264 gchar
*strcrchomp (gchar
*str
);
265 void file_strip_crs (const gchar
*file
);
266 gchar
*strcasestr (const gchar
*haystack
,
267 const gchar
*needle
);
268 gpointer
my_memmem (gconstpointer haystack
,
270 gconstpointer needle
,
272 gchar
*strncpy2 (gchar
*dest
,
276 gboolean
is_next_nonascii (const gchar
*s
);
277 gint
get_next_word_len (const gchar
*s
);
279 /* functions for string parsing */
280 gint
subject_compare (const gchar
*s1
,
282 gint
subject_compare_for_sort (const gchar
*s1
,
284 void trim_subject_for_compare (gchar
*str
);
285 void trim_subject_for_sort (gchar
*str
);
286 void trim_subject (gchar
*str
);
287 void eliminate_parenthesis (gchar
*str
,
290 void extract_parenthesis (gchar
*str
,
294 void extract_parenthesis_with_skip_quote (gchar
*str
,
299 void eliminate_quote (gchar
*str
,
301 void extract_quote (gchar
*str
,
303 void eliminate_address_comment (gchar
*str
);
304 gchar
*strchr_with_skip_quote (const gchar
*str
,
307 gchar
*strrchr_with_skip_quote (const gchar
*str
,
310 void extract_address (gchar
*str
);
311 void extract_list_id_str (gchar
*str
);
313 GSList
*slist_concat_unique (GSList
*first
,
315 GSList
*address_list_append (GSList
*addr_list
,
317 GSList
*address_list_append_with_comments(GSList
*addr_list
,
319 GSList
*references_list_prepend (GSList
*msgid_list
,
321 GSList
*references_list_append (GSList
*msgid_list
,
323 GSList
*newsgroup_list_append (GSList
*group_list
,
326 GList
*add_history (GList
*list
,
329 void remove_return (gchar
*str
);
330 void remove_space (gchar
*str
);
331 void unfold_line (gchar
*str
);
332 void subst_char (gchar
*str
,
335 void subst_chars (gchar
*str
,
338 void subst_for_filename (gchar
*str
);
339 void subst_for_shellsafe_filename (gchar
*str
);
340 gboolean
is_header_line (const gchar
*str
);
341 gboolean
is_ascii_str (const gchar
*str
);
342 gint
get_quote_level (const gchar
*str
,
343 const gchar
*quote_chars
);
344 gint
check_line_length (const gchar
*str
,
348 gchar
*strstr_with_skip_quote (const gchar
*haystack
,
349 const gchar
*needle
);
350 gchar
*strchr_parenthesis_close (const gchar
*str
,
354 gchar
**strsplit_parenthesis (const gchar
*str
,
358 gchar
**strsplit_with_quote (const gchar
*str
,
362 gchar
*get_abbrev_newsgroup_name (const gchar
*group
,
364 gchar
*trim_string (const gchar
*str
,
367 GList
*uri_list_extract_filenames (const gchar
*uri_list
);
368 gboolean
is_uri_string (const gchar
*str
);
369 gchar
*get_uri_path (const gchar
*uri
);
370 gint
get_uri_len (const gchar
*str
);
371 void decode_uri (gchar
*decoded_uri
,
372 const gchar
*encoded_uri
);
373 void decode_uri_with_plus (gchar
*decoded_uri
,
374 const gchar
*encoded_uri
,
376 gint
scan_mailto_url (const gchar
*mailto
,
384 /* return static strings */
385 const gchar
*get_home_dir (void);
386 const gchar
*get_rc_dir (void);
387 const gchar
*get_mail_base_dir (void);
388 const gchar
*get_news_cache_dir (void);
389 const gchar
*get_imap_cache_dir (void);
390 const gchar
*get_mbox_cache_dir (void);
391 const gchar
*get_mime_tmp_dir (void);
392 const gchar
*get_template_dir (void);
393 const gchar
*get_header_cache_dir (void);
394 const gchar
*get_plugin_dir (void);
395 const gchar
*get_tmp_dir (void);
396 const gchar
*get_locale_dir (void);
397 gchar
*get_tmp_file (void);
398 const gchar
*get_domain_name (void);
400 /* file / directory handling */
401 off_t
get_file_size (const gchar
*file
);
402 off_t
get_file_size_as_crlf (const gchar
*file
);
403 off_t
get_left_file_size (FILE *fp
);
405 time_t get_file_mtime (const gchar
*file
);
407 gboolean
file_exist (const gchar
*file
,
408 gboolean allow_fifo
);
409 gboolean
is_relative_filename (const gchar
*file
);
410 gboolean
is_dir_exist (const gchar
*dir
);
411 gboolean
is_file_entry_exist (const gchar
*file
);
412 gboolean
dirent_is_regular_file (struct dirent
*d
);
413 gboolean
dirent_is_directory (struct dirent
*d
);
415 #define is_file_exist(file) file_exist(file, FALSE)
416 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
418 gint
change_dir (const gchar
*dir
);
419 gint
make_dir (const gchar
*dir
);
420 gint
make_dir_hier (const gchar
*dir
);
421 gint
remove_all_files (const gchar
*dir
);
422 gint
remove_numbered_files (const gchar
*dir
,
425 gint
remove_numbered_files_not_in_list(const gchar
*dir
,
427 gint
remove_all_numbered_files (const gchar
*dir
);
428 gint
remove_expired_files (const gchar
*dir
,
430 gint
remove_dir_recursive (const gchar
*dir
);
431 gint
append_file (const gchar
*src
,
433 gboolean keep_backup
);
434 gint
rename_force (const gchar
*oldpath
,
435 const gchar
*newpath
);
436 gint
copy_file (const gchar
*src
,
438 gboolean keep_backup
);
439 gint
move_file (const gchar
*src
,
442 gint
copy_dir (const gchar
*src
,
444 gint
copy_file_part_to_fp (FILE *fp
,
448 gint
copy_file_part (FILE *fp
,
453 gchar
*canonicalize_str (const gchar
*str
);
454 gint
canonicalize_file (const gchar
*src
,
456 gint
canonicalize_file_replace (const gchar
*file
);
457 gint
uncanonicalize_file (const gchar
*src
,
459 gint
uncanonicalize_file_replace(const gchar
*file
);
461 gchar
*normalize_newlines (const gchar
*str
);
463 gchar
*get_outgoing_rfc2822_str (FILE *fp
);
465 gint
change_file_mode_rw (FILE *fp
,
467 FILE *my_tmpfile (void);
468 FILE *get_tmpfile_in_dir (const gchar
*dir
,
470 FILE *str_open_as_stream (const gchar
*str
);
471 gint
str_write_to_file (const gchar
*str
,
473 gchar
*file_read_to_str (const gchar
*file
);
474 gchar
*file_read_stream_to_str (FILE *fp
);
476 char *fgets_crlf(char *buf
, int size
, FILE *stream
);
478 /* process execution */
479 gint
execute_command_line (const gchar
*cmdline
,
481 gchar
*get_command_output (const gchar
*cmdline
);
483 /* open URI with external browser */
484 gint
open_uri(const gchar
*uri
, const gchar
*cmdline
);
485 /* open file with text editor */
486 gint
open_txt_editor(const gchar
*filepath
, const gchar
*cmdline
);
489 time_t remote_tzoffset_sec (const gchar
*zone
);
490 time_t tzoffset_sec (time_t *now
);
491 gchar
*tzoffset (time_t *now
);
492 void get_rfc822_date (gchar
*buf
,
495 size_t my_strftime (gchar
*s
,
498 const struct tm
*tm
);
499 size_t fast_strftime (gchar
*buf
,
505 void debug_print_real (const gchar
*format
, ...) G_GNUC_PRINTF(1, 2);
506 const char * debug_srcname (const char *file
);
508 /* subject threading */
509 void * subject_table_lookup(GHashTable
*subject_table
, gchar
* subject
);
510 void subject_table_insert(GHashTable
*subject_table
, gchar
* subject
,
512 void subject_table_remove(GHashTable
*subject_table
, gchar
* subject
);
513 gint
subject_get_prefix_length (const gchar
*subject
);
515 /* quoting recognition */
516 const gchar
* line_has_quote_char (const gchar
*str
,
517 const gchar
*quote_chars
);
518 const gchar
* line_has_quote_char_last (const gchar
*str
,
519 const gchar
*quote_chars
);
521 guint
g_stricase_hash (gconstpointer gptr
);
522 gint
g_stricase_equal (gconstpointer gptr1
, gconstpointer gptr2
);
523 gint
g_int_compare (gconstpointer a
, gconstpointer b
);
525 gchar
*generate_msgid (gchar
*buf
, gint len
);
526 gchar
*generate_mime_boundary (const gchar
*prefix
);
528 gint
quote_cmd_argument(gchar
* result
, guint size
,
530 GNode
*g_node_map(GNode
*node
, GNodeMapFunc func
, gpointer data
);
532 gboolean
get_hex_value(guchar
*out
, gchar c1
, gchar c2
);
533 void get_hex_str(gchar
*out
, guchar ch
);
535 /* auto pointer for containers that support GType system */
537 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
538 typedef struct AutoPointer GAuto
;
539 GType
g_auto_pointer_register (void);
540 GAuto
*g_auto_pointer_new (gpointer pointer
);
541 GAuto
*g_auto_pointer_new_with_free (gpointer p
,
543 gpointer
g_auto_pointer_get_ptr (GAuto
*auto_ptr
);
544 GAuto
*g_auto_pointer_copy (GAuto
*auto_ptr
);
545 void g_auto_pointer_free (GAuto
*auto_ptr
);
546 void replace_returns (gchar
*str
);
547 gboolean
get_uri_part (const gchar
*start
,
548 const gchar
*scanpos
,
552 gchar
*make_uri_string (const gchar
*bp
,
554 gboolean
get_email_part (const gchar
*start
,
555 const gchar
*scanpos
,
559 gchar
*make_email_string(const gchar
*bp
,
561 gchar
*make_http_string (const gchar
*bp
,
564 gchar
*mailcap_get_command_for_type(const gchar
*type
,
565 const gchar
*file_to_open
);
566 void mailcap_update_default (const gchar
*type
,
567 const gchar
*command
);
569 gboolean
file_is_email(const gchar
*filename
);
570 gboolean
sc_g_list_bigger(GList
*list
, gint max
);
571 gboolean
sc_g_slist_bigger(GSList
*list
, gint max
);
576 #endif /* __UTILS_H__ */