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);
228 gint str_case_equal (gconstpointer v,
230 guint str_case_hash (gconstpointer key);
232 void ptr_array_free_strings (GPtrArray *array);
234 typedef gboolean (*StrFindFunc) (const gchar *haystack,
235 const gchar *needle);
237 gboolean str_find (const gchar *haystack,
238 const gchar *needle);
239 gboolean str_case_find (const gchar *haystack,
240 const gchar *needle);
241 /* number-string conversion */
242 gint to_number (const gchar *nstr);
243 gchar *itos_buf (gchar *nstr,
245 gchar *itos (gint n);
246 gchar *to_human_readable (off_t size);
248 /* alternative string functions */
249 gint strcmp2 (const gchar *s1,
251 gchar *strstr2 (const gchar *s1,
253 gint path_cmp (const gchar *s1,
255 gchar *strretchomp (gchar *str);
256 gchar *strtailchomp (gchar *str,
258 gchar *strcrchomp (gchar *str);
259 gint file_strip_crs (const gchar *file);
260 gchar *strcasestr (const gchar *haystack,
261 const gchar *needle);
262 gpointer my_memmem (gconstpointer haystack,
264 gconstpointer needle,
266 gchar *strncpy2 (gchar *dest,
270 gboolean is_next_nonascii (const gchar *s);
271 gint get_next_word_len (const gchar *s);
273 /* functions for string parsing */
274 gint subject_compare (const gchar *s1,
276 gint subject_compare_for_sort (const gchar *s1,
278 void trim_subject (gchar *str);
279 void eliminate_parenthesis (gchar *str,
282 void extract_parenthesis (gchar *str,
286 void extract_quote (gchar *str,
288 void eliminate_address_comment (gchar *str);
289 gchar *strchr_with_skip_quote (const gchar *str,
292 void extract_address (gchar *str);
293 void extract_list_id_str (gchar *str);
295 GSList *address_list_append (GSList *addr_list,
297 GSList *address_list_append_with_comments(GSList *addr_list,
299 GSList *references_list_prepend (GSList *msgid_list,
301 GSList *references_list_append (GSList *msgid_list,
303 GSList *newsgroup_list_append (GSList *group_list,
306 GList *add_history (GList *list,
309 void remove_return (gchar *str);
310 void remove_space (gchar *str);
311 void unfold_line (gchar *str);
312 void subst_char (gchar *str,
315 void subst_chars (gchar *str,
318 void subst_for_filename (gchar *str);
319 void subst_for_shellsafe_filename (gchar *str);
320 gboolean is_ascii_str (const gchar *str);
321 gint get_quote_level (const gchar *str,
322 const gchar *quote_chars);
323 gint check_line_length (const gchar *str,
327 gchar **strsplit_with_quote (const gchar *str,
331 gchar *get_abbrev_newsgroup_name (const gchar *group,
333 gchar *trim_string (const gchar *str,
336 GList *uri_list_extract_filenames (const gchar *uri_list);
337 gboolean is_uri_string (const gchar *str);
338 gchar *get_uri_path (const gchar *uri);
339 gint get_uri_len (const gchar *str);
340 void decode_uri (gchar *decoded_uri,
341 const gchar *encoded_uri);
342 void decode_uri_with_plus (gchar *decoded_uri,
343 const gchar *encoded_uri,
345 gint scan_mailto_url (const gchar *mailto,
353 /* return static strings */
354 const gchar *get_home_dir (void);
355 const gchar *get_rc_dir (void);
356 void set_rc_dir (const gchar *dir);
357 gboolean rc_dir_is_alt (void);
358 const gchar *get_mail_base_dir (void);
359 const gchar *get_news_cache_dir (void);
360 const gchar *get_imap_cache_dir (void);
361 const gchar *get_mime_tmp_dir (void);
362 const gchar *get_template_dir (void);
363 const gchar *get_plugin_dir (void);
364 const gchar *get_tmp_dir (void);
365 const gchar *get_locale_dir (void);
366 gchar *get_tmp_file (void);
367 const gchar *get_domain_name (void);
369 /* file / directory handling */
370 off_t get_file_size (const gchar *file);
371 off_t get_file_size_as_crlf (const gchar *file);
373 time_t get_file_mtime (const gchar *file);
375 gboolean file_exist (const gchar *file,
376 gboolean allow_fifo);
377 gboolean is_relative_filename (const gchar *file);
378 gboolean is_dir_exist (const gchar *dir);
379 gboolean is_file_entry_exist (const gchar *file);
380 gboolean dirent_is_regular_file (struct dirent *d);
382 #define is_file_exist(file) file_exist(file, FALSE)
383 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
385 gint change_dir (const gchar *dir);
386 gint make_dir (const gchar *dir);
387 gint make_dir_hier (const gchar *dir);
388 gint remove_all_files (const gchar *dir);
389 gint remove_numbered_files (const gchar *dir,
392 gint remove_numbered_files_not_in_list(const gchar *dir,
394 gint remove_all_numbered_files (const gchar *dir);
395 gint remove_dir_recursive (const gchar *dir);
396 gint append_file (const gchar *src,
398 gboolean keep_backup);
399 gint rename_force (const gchar *oldpath,
400 const gchar *newpath);
401 gint copy_file (const gchar *src,
403 gboolean keep_backup);
404 gint move_file (const gchar *src,
407 gint copy_dir (const gchar *src,
409 gint copy_file_part_to_fp (FILE *fp,
413 gint copy_file_part (FILE *fp,
418 gchar *canonicalize_str (const gchar *str);
419 gint canonicalize_file (const gchar *src,
421 gint canonicalize_file_replace (const gchar *file);
423 gchar *normalize_newlines (const gchar *str);
425 gchar *get_outgoing_rfc2822_str (FILE *fp);
427 gint change_file_mode_rw (FILE *fp,
429 FILE *my_tmpfile (void);
430 FILE *get_tmpfile_in_dir (const gchar *dir,
432 FILE *str_open_as_stream (const gchar *str);
433 gint str_write_to_file (const gchar *str,
435 gchar *file_read_to_str (const gchar *file);
436 gchar *file_read_stream_to_str (FILE *fp);
438 char *fgets_crlf(char *buf, int size, FILE *stream);
440 /* process execution */
441 gint execute_command_line (const gchar *cmdline,
443 gchar *get_command_output (const gchar *cmdline);
445 /* open URI with external browser */
446 gint open_uri(const gchar *uri, const gchar *cmdline);
447 /* open file with text editor */
448 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
451 time_t remote_tzoffset_sec (const gchar *zone);
452 time_t tzoffset_sec (time_t *now);
453 gchar *tzoffset (time_t *now);
454 void get_rfc822_date (gchar *buf,
457 size_t fast_strftime (gchar *buf,
463 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
464 const char * debug_srcname (const char *file);
466 /* subject threading */
467 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
468 void subject_table_insert(GHashTable *subject_table, gchar * subject,
470 void subject_table_remove(GHashTable *subject_table, gchar * subject);
471 gint subject_get_prefix_length (const gchar *subject);
473 /* quoting recognition */
474 const gchar * line_has_quote_char (const gchar *str,
475 const gchar *quote_chars);
477 gint g_int_compare (gconstpointer a, gconstpointer b);
479 gchar *generate_msgid (gchar *buf, gint len);
480 gchar *generate_mime_boundary (const gchar *prefix);
482 gint quote_cmd_argument(gchar * result, guint size,
484 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
486 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
487 void get_hex_str(gchar *out, guchar ch);
489 /* auto pointer for containers that support GType system */
491 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
492 typedef struct AutoPointer GAuto;
493 GType g_auto_pointer_register (void);
494 GAuto *g_auto_pointer_new (gpointer pointer);
495 GAuto *g_auto_pointer_new_with_free (gpointer p,
497 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
498 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
499 void g_auto_pointer_free (GAuto *auto_ptr);
500 void replace_returns (gchar *str);
501 gboolean get_uri_part (const gchar *start,
502 const gchar *scanpos,
506 gchar *make_uri_string (const gchar *bp,
508 gboolean get_email_part (const gchar *start,
509 const gchar *scanpos,
513 gchar *make_email_string(const gchar *bp,
515 gchar *make_http_string (const gchar *bp,
518 gchar *mailcap_get_command_for_type(const gchar *type,
519 const gchar *file_to_open);
520 void mailcap_update_default (const gchar *type,
521 const gchar *command);
523 gboolean file_is_email(const gchar *filename);
524 gboolean sc_g_list_bigger(GList *list, gint max);
525 gboolean sc_g_slist_bigger(GSList *list, gint max);
530 #endif /* __UTILS_H__ */