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 #ifndef HAVE_U32_TYPEDEF
75 #undef u32 /* maybe there is a macro with this name */
77 #define HAVE_U32_TYPEDEF
80 #ifndef BIG_ENDIAN_HOST
81 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
82 #define BIG_ENDIAN_HOST 1
86 #define CHDIR_RETURN_IF_FAIL(dir) \
88 if (change_dir(dir) < 0) return; \
91 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
93 if (change_dir(dir) < 0) return val; \
96 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
98 if (change_dir(dir) < 0) { \
104 #define Xalloca(ptr, size, iffail) \
106 if ((ptr = alloca(size)) == NULL) { \
107 g_warning("can't allocate memory\n"); \
112 #define Xstrdup_a(ptr, str, iffail) \
116 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
117 g_warning("can't allocate memory\n"); \
120 strcpy(__tmp, str); \
125 #define Xstrndup_a(ptr, str, len, iffail) \
129 if ((__tmp = alloca(len + 1)) == NULL) { \
130 g_warning("can't allocate memory\n"); \
133 strncpy(__tmp, str, len); \
140 #define Xstrcat_a(ptr, str1, str2, iffail) \
145 len1 = strlen(str1); \
146 len2 = strlen(str2); \
147 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
148 g_warning("can't allocate memory\n"); \
151 memcpy(__tmp, str1, len1); \
152 memcpy(__tmp + len1, str2, len2 + 1); \
158 #define AUTORELEASE_STR(str, iffail) \
161 Xstrdup_a(__str, str, iffail); \
166 #define FILE_OP_ERROR(file, func) \
168 g_printerr("%s: ", file); \
173 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
179 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
181 /* debug functions */
182 void debug_set_mode (gboolean mode);
183 gboolean debug_get_mode (void);
186 #define debug_print \
187 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
190 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
191 #define debug_print \
192 debug_print_real("%s:%d:", __FILE__, __LINE__), \
196 /* for macro expansion */
198 #define Xstr(x) Str(x)
201 /* System related stuff. */
203 gboolean superuser_p (void);
206 /* String utilities. */
208 void list_free_strings (GList *list);
209 void slist_free_strings (GSList *list);
211 void hash_free_strings (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 /* number-string conversion */
227 gint to_number (const gchar *nstr);
228 gchar *itos_buf (gchar *nstr,
230 gchar *itos (gint n);
231 gchar *to_human_readable (goffset size);
233 /* alternative string functions */
234 gint strcmp2 (const gchar *s1,
236 gchar *strstr2 (const gchar *s1,
238 gint path_cmp (const gchar *s1,
240 gchar *strretchomp (gchar *str);
241 gchar *strtailchomp (gchar *str,
243 gchar *strcrchomp (gchar *str);
244 gint file_strip_crs (const gchar *file);
245 gchar *strcasestr (const gchar *haystack,
246 const gchar *needle);
247 gpointer my_memmem (gconstpointer haystack,
249 gconstpointer needle,
251 gchar *strncpy2 (gchar *dest,
255 gboolean is_next_nonascii (const gchar *s);
256 gint get_next_word_len (const gchar *s);
258 /* functions for string parsing */
259 gint subject_compare (const gchar *s1,
261 gint subject_compare_for_sort (const gchar *s1,
263 void trim_subject (gchar *str);
264 void eliminate_parenthesis (gchar *str,
267 void extract_parenthesis (gchar *str,
271 void extract_quote (gchar *str,
273 void eliminate_address_comment (gchar *str);
274 gchar *strchr_with_skip_quote (const gchar *str,
277 void extract_address (gchar *str);
278 void extract_list_id_str (gchar *str);
280 GSList *address_list_append (GSList *addr_list,
282 GSList *address_list_append_with_comments(GSList *addr_list,
284 GSList *references_list_prepend (GSList *msgid_list,
286 GSList *references_list_append (GSList *msgid_list,
288 GSList *newsgroup_list_append (GSList *group_list,
291 GList *add_history (GList *list,
294 void remove_return (gchar *str);
295 void remove_space (gchar *str);
296 void unfold_line (gchar *str);
297 void subst_char (gchar *str,
300 void subst_chars (gchar *str,
303 void subst_for_filename (gchar *str);
304 void subst_for_shellsafe_filename (gchar *str);
305 gboolean is_ascii_str (const gchar *str);
306 gint get_quote_level (const gchar *str,
307 const gchar *quote_chars);
308 gint check_line_length (const gchar *str,
312 gchar **strsplit_with_quote (const gchar *str,
316 gchar *get_abbrev_newsgroup_name (const gchar *group,
318 gchar *trim_string (const gchar *str,
321 GList *uri_list_extract_filenames (const gchar *uri_list);
322 gboolean is_uri_string (const gchar *str);
323 gchar *get_uri_path (const gchar *uri);
324 gint get_uri_len (const gchar *str);
325 void decode_uri (gchar *decoded_uri,
326 const gchar *encoded_uri);
327 void decode_uri_with_plus (gchar *decoded_uri,
328 const gchar *encoded_uri,
330 gint scan_mailto_url (const gchar *mailto,
339 /* return static strings */
340 const gchar *get_home_dir (void);
341 const gchar *get_rc_dir (void);
342 void set_rc_dir (const gchar *dir);
343 gboolean rc_dir_is_alt (void);
344 const gchar *get_mail_base_dir (void);
345 const gchar *get_news_cache_dir (void);
346 const gchar *get_imap_cache_dir (void);
347 const gchar *get_mime_tmp_dir (void);
348 const gchar *get_template_dir (void);
349 const gchar *get_plugin_dir (void);
350 const gchar *get_tmp_dir (void);
351 const gchar *get_locale_dir (void);
352 gchar *get_tmp_file (void);
353 const gchar *get_domain_name (void);
355 /* file / directory handling */
356 off_t get_file_size (const gchar *file);
357 off_t get_file_size_as_crlf (const gchar *file);
359 time_t get_file_mtime (const gchar *file);
361 gboolean file_exist (const gchar *file,
362 gboolean allow_fifo);
363 gboolean is_relative_filename (const gchar *file);
364 gboolean is_dir_exist (const gchar *dir);
365 gboolean is_file_entry_exist (const gchar *file);
366 gboolean dirent_is_regular_file (struct dirent *d);
368 #define is_file_exist(file) file_exist(file, FALSE)
369 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
371 gint change_dir (const gchar *dir);
372 gint make_dir (const gchar *dir);
373 gint make_dir_hier (const gchar *dir);
374 gint remove_all_files (const gchar *dir);
375 gint remove_numbered_files (const gchar *dir,
378 gint remove_numbered_files_not_in_list(const gchar *dir,
380 gint remove_all_numbered_files (const gchar *dir);
381 gint remove_dir_recursive (const gchar *dir);
382 gint append_file (const gchar *src,
384 gboolean keep_backup);
385 gint rename_force (const gchar *oldpath,
386 const gchar *newpath);
387 gint copy_file (const gchar *src,
389 gboolean keep_backup);
390 gint move_file (const gchar *src,
393 gint copy_dir (const gchar *src,
395 gint copy_file_part_to_fp (FILE *fp,
399 gint copy_file_part (FILE *fp,
404 gchar *canonicalize_str (const gchar *str);
405 gint canonicalize_file (const gchar *src,
407 gint canonicalize_file_replace (const gchar *file);
409 gchar *normalize_newlines (const gchar *str);
411 gchar *get_outgoing_rfc2822_str (FILE *fp);
413 gint change_file_mode_rw (FILE *fp,
415 FILE *my_tmpfile (void);
416 FILE *get_tmpfile_in_dir (const gchar *dir,
418 FILE *str_open_as_stream (const gchar *str);
419 gint str_write_to_file (const gchar *str,
421 gchar *file_read_to_str (const gchar *file);
422 gchar *file_read_stream_to_str (FILE *fp);
423 gchar *file_read_to_str_no_recode(const gchar *file);
424 gchar *file_read_stream_to_str_no_recode(FILE *fp);
426 char *fgets_crlf(char *buf, int size, FILE *stream);
428 /* process execution */
429 gint execute_command_line (const gchar *cmdline,
431 gchar *get_command_output (const gchar *cmdline);
433 /* open URI with external browser */
434 gint open_uri(const gchar *uri, const gchar *cmdline);
435 /* open file with text editor */
436 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
439 time_t remote_tzoffset_sec (const gchar *zone);
440 time_t tzoffset_sec (time_t *now);
441 gchar *tzoffset (time_t *now);
442 void get_rfc822_date (gchar *buf,
445 size_t fast_strftime (gchar *buf,
451 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
452 const char * debug_srcname (const char *file);
454 /* subject threading */
455 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
456 void subject_table_insert(GHashTable *subject_table, gchar * subject,
458 void subject_table_remove(GHashTable *subject_table, gchar * subject);
459 void utils_free_regex(void);
460 gint subject_get_prefix_length (const gchar *subject);
462 /* quoting recognition */
463 const gchar * line_has_quote_char (const gchar *str,
464 const gchar *quote_chars);
466 gint g_int_compare (gconstpointer a, gconstpointer b);
468 gchar *generate_msgid (gchar *buf, gint len);
469 gchar *generate_mime_boundary (const gchar *prefix);
471 gint quote_cmd_argument(gchar * result, guint size,
473 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
475 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
476 void get_hex_str(gchar *out, guchar ch);
478 /* auto pointer for containers that support GType system */
480 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
481 typedef struct AutoPointer GAuto;
482 GType g_auto_pointer_register (void);
483 GAuto *g_auto_pointer_new (gpointer pointer);
484 GAuto *g_auto_pointer_new_with_free (gpointer p,
486 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
487 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
488 void g_auto_pointer_free (GAuto *auto_ptr);
489 void replace_returns (gchar *str);
490 gboolean get_uri_part (const gchar *start,
491 const gchar *scanpos,
495 gchar *make_uri_string (const gchar *bp,
497 gboolean get_email_part (const gchar *start,
498 const gchar *scanpos,
502 gchar *make_email_string(const gchar *bp,
504 gchar *make_http_string (const gchar *bp,
507 gchar *mailcap_get_command_for_type(const gchar *type,
508 const gchar *file_to_open);
509 void mailcap_update_default (const gchar *type,
510 const gchar *command);
512 gboolean file_is_email(const gchar *filename);
513 gboolean sc_g_list_bigger(GList *list, gint max);
514 gboolean sc_g_slist_bigger(GSList *list, gint max);
516 int claws_unlink(const gchar *filename);
521 #endif /* __UTILS_H__ */