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 /* why is this sometimes undefined !? */
76 typedef gint64 goffset;
77 #define G_MINOFFSET G_MININT64
78 #define G_MAXOFFSET G_MAXINT64
81 #ifndef HAVE_U32_TYPEDEF
82 #undef u32 /* maybe there is a macro with this name */
84 #define HAVE_U32_TYPEDEF
87 #ifndef BIG_ENDIAN_HOST
88 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
89 #define BIG_ENDIAN_HOST 1
93 #define CHDIR_RETURN_IF_FAIL(dir) \
95 if (change_dir(dir) < 0) return; \
98 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
100 if (change_dir(dir) < 0) return val; \
103 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
105 if (change_dir(dir) < 0) { \
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 g_printerr("%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)
208 /* System related stuff. */
210 gboolean superuser_p (void);
213 /* String utilities. */
215 void list_free_strings (GList *list);
216 void slist_free_strings (GSList *list);
218 void hash_free_strings (GHashTable *table);
220 gint str_case_equal (gconstpointer v,
222 guint str_case_hash (gconstpointer key);
224 void ptr_array_free_strings (GPtrArray *array);
226 typedef gboolean (*StrFindFunc) (const gchar *haystack,
227 const gchar *needle);
229 gboolean str_find (const gchar *haystack,
230 const gchar *needle);
231 gboolean str_case_find (const gchar *haystack,
232 const gchar *needle);
233 /* number-string conversion */
234 gint to_number (const gchar *nstr);
235 gchar *itos_buf (gchar *nstr,
237 gchar *itos (gint n);
238 gchar *to_human_readable (goffset size);
240 /* alternative string functions */
241 gint strcmp2 (const gchar *s1,
243 gchar *strstr2 (const gchar *s1,
245 gint path_cmp (const gchar *s1,
247 gchar *strretchomp (gchar *str);
248 gchar *strtailchomp (gchar *str,
250 gchar *strcrchomp (gchar *str);
251 gint file_strip_crs (const gchar *file);
252 gchar *strcasestr (const gchar *haystack,
253 const gchar *needle);
254 gpointer my_memmem (gconstpointer haystack,
256 gconstpointer needle,
258 gchar *strncpy2 (gchar *dest,
262 gboolean is_next_nonascii (const gchar *s);
263 gint get_next_word_len (const gchar *s);
265 /* functions for string parsing */
266 gint subject_compare (const gchar *s1,
268 gint subject_compare_for_sort (const gchar *s1,
270 void trim_subject (gchar *str);
271 void eliminate_parenthesis (gchar *str,
274 void extract_parenthesis (gchar *str,
278 void extract_quote (gchar *str,
280 void eliminate_address_comment (gchar *str);
281 gchar *strchr_with_skip_quote (const gchar *str,
284 void extract_address (gchar *str);
285 void extract_list_id_str (gchar *str);
287 GSList *address_list_append (GSList *addr_list,
289 GSList *address_list_append_with_comments(GSList *addr_list,
291 GSList *references_list_prepend (GSList *msgid_list,
293 GSList *references_list_append (GSList *msgid_list,
295 GSList *newsgroup_list_append (GSList *group_list,
298 GList *add_history (GList *list,
301 void remove_return (gchar *str);
302 void remove_space (gchar *str);
303 void unfold_line (gchar *str);
304 void subst_char (gchar *str,
307 void subst_chars (gchar *str,
310 void subst_for_filename (gchar *str);
311 void subst_for_shellsafe_filename (gchar *str);
312 gboolean is_ascii_str (const gchar *str);
313 gint get_quote_level (const gchar *str,
314 const gchar *quote_chars);
315 gint check_line_length (const gchar *str,
319 gchar **strsplit_with_quote (const gchar *str,
323 gchar *get_abbrev_newsgroup_name (const gchar *group,
325 gchar *trim_string (const gchar *str,
328 GList *uri_list_extract_filenames (const gchar *uri_list);
329 gboolean is_uri_string (const gchar *str);
330 gchar *get_uri_path (const gchar *uri);
331 gint get_uri_len (const gchar *str);
332 void decode_uri (gchar *decoded_uri,
333 const gchar *encoded_uri);
334 void decode_uri_with_plus (gchar *decoded_uri,
335 const gchar *encoded_uri,
337 gint scan_mailto_url (const gchar *mailto,
346 /* return static strings */
347 const gchar *get_home_dir (void);
348 const gchar *get_rc_dir (void);
349 void set_rc_dir (const gchar *dir);
350 gboolean rc_dir_is_alt (void);
351 const gchar *get_mail_base_dir (void);
352 const gchar *get_news_cache_dir (void);
353 const gchar *get_imap_cache_dir (void);
354 const gchar *get_mime_tmp_dir (void);
355 const gchar *get_template_dir (void);
356 const gchar *get_plugin_dir (void);
357 const gchar *get_tmp_dir (void);
358 const gchar *get_locale_dir (void);
359 gchar *get_tmp_file (void);
360 const gchar *get_domain_name (void);
362 /* file / directory handling */
363 off_t get_file_size (const gchar *file);
364 off_t get_file_size_as_crlf (const gchar *file);
366 time_t get_file_mtime (const gchar *file);
368 gboolean file_exist (const gchar *file,
369 gboolean allow_fifo);
370 gboolean is_relative_filename (const gchar *file);
371 gboolean is_dir_exist (const gchar *dir);
372 gboolean is_file_entry_exist (const gchar *file);
373 gboolean dirent_is_regular_file (struct dirent *d);
375 #define is_file_exist(file) file_exist(file, FALSE)
376 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
378 gint change_dir (const gchar *dir);
379 gint make_dir (const gchar *dir);
380 gint make_dir_hier (const gchar *dir);
381 gint remove_all_files (const gchar *dir);
382 gint remove_numbered_files (const gchar *dir,
385 gint remove_numbered_files_not_in_list(const gchar *dir,
387 gint remove_all_numbered_files (const gchar *dir);
388 gint remove_dir_recursive (const gchar *dir);
389 gint append_file (const gchar *src,
391 gboolean keep_backup);
392 gint rename_force (const gchar *oldpath,
393 const gchar *newpath);
394 gint copy_file (const gchar *src,
396 gboolean keep_backup);
397 gint move_file (const gchar *src,
400 gint copy_dir (const gchar *src,
402 gint copy_file_part_to_fp (FILE *fp,
406 gint copy_file_part (FILE *fp,
411 gchar *canonicalize_str (const gchar *str);
412 gint canonicalize_file (const gchar *src,
414 gint canonicalize_file_replace (const gchar *file);
416 gchar *normalize_newlines (const gchar *str);
418 gchar *get_outgoing_rfc2822_str (FILE *fp);
420 gint change_file_mode_rw (FILE *fp,
422 FILE *my_tmpfile (void);
423 FILE *get_tmpfile_in_dir (const gchar *dir,
425 FILE *str_open_as_stream (const gchar *str);
426 gint str_write_to_file (const gchar *str,
428 gchar *file_read_to_str (const gchar *file);
429 gchar *file_read_stream_to_str (FILE *fp);
430 gchar *file_read_to_str_no_recode(const gchar *file);
431 gchar *file_read_stream_to_str_no_recode(FILE *fp);
433 char *fgets_crlf(char *buf, int size, FILE *stream);
435 /* process execution */
436 gint execute_command_line (const gchar *cmdline,
438 gchar *get_command_output (const gchar *cmdline);
440 /* open URI with external browser */
441 gint open_uri(const gchar *uri, const gchar *cmdline);
442 /* open file with text editor */
443 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
446 time_t remote_tzoffset_sec (const gchar *zone);
447 time_t tzoffset_sec (time_t *now);
448 gchar *tzoffset (time_t *now);
449 void get_rfc822_date (gchar *buf,
452 size_t fast_strftime (gchar *buf,
458 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
459 const char * debug_srcname (const char *file);
461 /* subject threading */
462 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
463 void subject_table_insert(GHashTable *subject_table, gchar * subject,
465 void subject_table_remove(GHashTable *subject_table, gchar * subject);
466 void utils_free_regex(void);
467 gint subject_get_prefix_length (const gchar *subject);
469 /* quoting recognition */
470 const gchar * line_has_quote_char (const gchar *str,
471 const gchar *quote_chars);
473 gint g_int_compare (gconstpointer a, gconstpointer b);
475 gchar *generate_msgid (gchar *buf, gint len);
476 gchar *generate_mime_boundary (const gchar *prefix);
478 gint quote_cmd_argument(gchar * result, guint size,
480 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
482 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
483 void get_hex_str(gchar *out, guchar ch);
485 /* auto pointer for containers that support GType system */
487 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
488 typedef struct AutoPointer GAuto;
489 GType g_auto_pointer_register (void);
490 GAuto *g_auto_pointer_new (gpointer pointer);
491 GAuto *g_auto_pointer_new_with_free (gpointer p,
493 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
494 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
495 void g_auto_pointer_free (GAuto *auto_ptr);
496 void replace_returns (gchar *str);
497 gboolean get_uri_part (const gchar *start,
498 const gchar *scanpos,
502 gchar *make_uri_string (const gchar *bp,
504 gboolean get_email_part (const gchar *start,
505 const gchar *scanpos,
509 gchar *make_email_string(const gchar *bp,
511 gchar *make_http_string (const gchar *bp,
514 gchar *mailcap_get_command_for_type(const gchar *type,
515 const gchar *file_to_open);
516 void mailcap_update_default (const gchar *type,
517 const gchar *command);
519 gboolean file_is_email(const gchar *filename);
520 gboolean sc_g_list_bigger(GList *list, gint max);
521 gboolean sc_g_slist_bigger(GSList *list, gint max);
523 int claws_unlink(const gchar *filename);
528 #endif /* __UTILS_H__ */