2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <sys/types.h>
42 /* The AC_CHECK_SIZEOF() in configure fails for some machines.
43 * we provide some fallback values here */
44 #if !SIZEOF_UNSIGNED_SHORT
45 #undef SIZEOF_UNSIGNED_SHORT
46 #define SIZEOF_UNSIGNED_SHORT 2
48 #if !SIZEOF_UNSIGNED_INT
49 #undef SIZEOF_UNSIGNED_INT
50 #define SIZEOF_UNSIGNED_INT 4
52 #if !SIZEOF_UNSIGNED_LONG
53 #undef SIZEOF_UNSIGNED_LONG
54 #define SIZEOF_UNSIGNED_LONG 4
57 #ifndef HAVE_U32_TYPEDEF
58 #undef u32 /* maybe there is a macro with this name */
60 #define HAVE_U32_TYPEDEF
63 #ifndef BIG_ENDIAN_HOST
64 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
65 #define BIG_ENDIAN_HOST 1
69 #define CHDIR_RETURN_IF_FAIL(dir) \
71 if (change_dir(dir) < 0) return; \
74 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
76 if (change_dir(dir) < 0) return val; \
79 #define Xalloca(ptr, size, iffail) \
81 if ((ptr = alloca(size)) == NULL) { \
82 g_warning("can't allocate memory\n"); \
87 #define Xstrdup_a(ptr, str, iffail) \
91 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
92 g_warning("can't allocate memory\n"); \
100 #define Xstrndup_a(ptr, str, len, iffail) \
104 if ((__tmp = alloca(len + 1)) == NULL) { \
105 g_warning("can't allocate memory\n"); \
108 strncpy(__tmp, str, len); \
115 #define Xstrcat_a(ptr, str1, str2, iffail) \
120 len1 = strlen(str1); \
121 len2 = strlen(str2); \
122 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
123 g_warning("can't allocate memory\n"); \
126 memcpy(__tmp, str1, len1); \
127 memcpy(__tmp + len1, str2, len2 + 1); \
133 #define AUTORELEASE_STR(str, iffail) \
136 Xstrdup_a(__str, str, iffail); \
141 #define FILE_OP_ERROR(file, func) \
143 fprintf(stderr, "%s: ", file); \
151 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
153 /* debug functions */
154 void debug_set_mode (gboolean mode);
155 gboolean debug_get_mode (void);
156 #define debug_print \
157 debug_print_real(__FILE__ ":%d:", __LINE__), \
160 /* for macro expansion */
162 #define Xstr(x) Str(x)
164 void list_free_strings (GList *list);
165 void slist_free_strings (GSList *list);
167 void hash_free_strings (GHashTable *table);
168 void hash_free_value_mem (GHashTable *table);
170 gint str_case_equal (gconstpointer v,
172 guint str_case_hash (gconstpointer key);
174 void ptr_array_free_strings (GPtrArray *array);
176 /* number-string conversion */
177 gint to_number (const gchar *nstr);
178 gchar *itos_buf (gchar *nstr,
180 gchar *itos (gint n);
181 gchar *to_human_readable (off_t size);
183 /* alternative string functions */
184 gint strcmp2 (const gchar *s1,
186 gchar *strstr2 (const gchar *s1,
188 gint path_cmp (const gchar *s1,
190 gchar *strretchomp (gchar *str);
191 gchar *strtailchomp (gchar *str,
193 gchar *strcrchomp (gchar *str);
194 gchar *strcasestr (const gchar *haystack,
195 const gchar *needle);
196 gchar *strncpy2 (gchar *dest,
200 /* wide-character functions */
202 int iswalnum (wint_t wc);
205 int iswspace (wint_t wc);
208 wint_t towlower (wint_t wc);
212 size_t wcslen (const wchar_t *s);
215 wchar_t *wcscpy (wchar_t *dest,
219 wchar_t *wcsncpy (wchar_t *dest,
224 wchar_t *wcsdup (const wchar_t *s);
225 wchar_t *wcsndup (const wchar_t *s,
227 wchar_t *strdup_mbstowcs (const gchar *s);
228 gchar *strdup_wcstombs (const wchar_t *s);
229 gint wcsncasecmp (const wchar_t *s1,
232 wchar_t *wcscasestr (const wchar_t *haystack,
233 const wchar_t *needle);
234 gint get_mbs_len (const gchar *s);
236 gboolean is_next_nonascii (const guchar *s);
237 gint get_next_word_len (const guchar *s);
239 /* functions for string parsing */
240 gint subject_compare (const gchar *s1,
242 gint subject_compare_for_sort (const gchar *s1,
244 void trim_subject_for_compare (gchar *str);
245 void trim_subject_for_sort (gchar *str);
246 void trim_subject (gchar *str);
247 void eliminate_parenthesis (gchar *str,
250 void extract_parenthesis (gchar *str,
254 void extract_parenthesis_with_skip_quote (gchar *str,
259 void eliminate_quote (gchar *str,
261 void extract_quote (gchar *str,
263 void eliminate_address_comment (gchar *str);
264 gchar *strchr_with_skip_quote (const gchar *str,
267 gchar *strrchr_with_skip_quote (const gchar *str,
270 void extract_address (gchar *str);
271 void extract_list_id_str (gchar *str);
273 GSList *slist_concat_unique (GSList *first,
275 GSList *address_list_append (GSList *addr_list,
277 GSList *address_list_append_with_comments(GSList *addr_list,
279 GSList *references_list_append (GSList *msgid_list,
281 GSList *newsgroup_list_append (GSList *group_list,
284 GList *add_history (GList *list,
287 void remove_return (gchar *str);
288 void remove_space (gchar *str);
289 void unfold_line (gchar *str);
290 void subst_char (gchar *str,
293 void subst_chars (gchar *str,
296 void subst_for_filename (gchar *str);
297 void subst_for_shellsafe_filename (gchar *str);
298 gboolean is_header_line (const gchar *str);
299 gboolean is_ascii_str (const guchar *str);
300 gint get_quote_level (const gchar *str,
301 const gchar *quote_chars);
302 gchar *strstr_with_skip_quote (const gchar *haystack,
303 const gchar *needle);
304 gchar *strchr_parenthesis_close (const gchar *str,
308 gchar **strsplit_parenthesis (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 void decode_uri (gchar *decoded_uri,
325 const gchar *encoded_uri);
326 gint scan_mailto_url (const gchar *mailto,
333 /* return static strings */
334 const gchar *get_home_dir (void);
335 const gchar *get_rc_dir (void);
336 const gchar *get_news_cache_dir (void);
337 const gchar *get_imap_cache_dir (void);
338 const gchar *get_mbox_cache_dir (void);
339 const gchar *get_mime_tmp_dir (void);
340 const gchar *get_template_dir (void);
341 const gchar *get_header_cache_dir (void);
342 const gchar *get_tmp_dir (void);
343 gchar *get_tmp_file (void);
344 const gchar *get_domain_name (void);
346 /* file / directory handling */
347 off_t get_file_size (const gchar *file);
348 off_t get_file_size_as_crlf (const gchar *file);
349 off_t get_left_file_size (FILE *fp);
351 gboolean file_exist (const gchar *file,
352 gboolean allow_fifo);
353 gboolean is_dir_exist (const gchar *dir);
354 gboolean is_file_entry_exist (const gchar *file);
355 gboolean dirent_is_regular_file (struct dirent *d);
356 gboolean dirent_is_directory (struct dirent *d);
358 #define is_file_exist(file) file_exist(file, FALSE)
359 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
361 gint change_dir (const gchar *dir);
362 gint make_dir (const gchar *dir);
363 gint make_dir_hier (const gchar *dir);
364 gint remove_all_files (const gchar *dir);
365 gint remove_numbered_files (const gchar *dir,
368 gint remove_numbered_files_not_in_list(const gchar *dir,
370 gint remove_all_numbered_files (const gchar *dir);
371 gint remove_expired_files (const gchar *dir,
373 gint remove_dir_recursive (const gchar *dir);
374 gint append_file (const gchar *src,
376 gboolean keep_backup);
377 gint copy_file (const gchar *src,
379 gboolean keep_backup);
380 gint move_file (const gchar *src,
383 gint copy_file_part_to_fp (FILE *fp,
387 gint copy_file_part (FILE *fp,
392 gchar *canonicalize_str (const gchar *str);
393 gint canonicalize_file (const gchar *src,
395 gint canonicalize_file_replace (const gchar *file);
396 gint uncanonicalize_file (const gchar *src,
398 gint uncanonicalize_file_replace(const gchar *file);
400 gchar *normalize_newlines (const gchar *str);
402 gchar *get_outgoing_rfc2822_str (FILE *fp);
404 gint change_file_mode_rw (FILE *fp,
406 FILE *my_tmpfile (void);
407 FILE *get_tmpfile_in_dir (const gchar *dir,
409 FILE *str_open_as_stream (const gchar *str);
410 gint str_write_to_file (const gchar *str,
412 gchar *file_read_to_str (const gchar *file);
413 gchar *file_read_stream_to_str (FILE *fp);
415 /* process execution */
416 gint execute_async (gchar *const argv[]);
417 gint execute_sync (gchar *const argv[]);
418 gint execute_command_line (const gchar *cmdline,
420 gchar *get_command_output (const gchar *cmdline);
422 /* open URI with external browser */
423 gint open_uri(const gchar *uri, const gchar *cmdline);
426 time_t remote_tzoffset_sec (const gchar *zone);
427 time_t tzoffset_sec (time_t *now);
428 gchar *tzoffset (time_t *now);
429 void get_rfc822_date (gchar *buf,
433 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
435 /* subject threading */
436 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
437 void subject_table_insert(GHashTable *subject_table, gchar * subject,
439 void subject_table_remove(GHashTable *subject_table, gchar * subject);
440 gint subject_get_prefix_length (const gchar *subject);
442 /* quoting recognition */
443 const gchar * line_has_quote_char (const gchar *str,
444 const gchar *quote_chars);
445 const gchar * line_has_quote_char_last (const gchar *str,
446 const gchar *quote_chars);
448 guint g_stricase_hash (gconstpointer gptr);
449 gint g_stricase_equal (gconstpointer gptr1, gconstpointer gptr2);
450 gint g_int_compare (gconstpointer a, gconstpointer b);
452 gchar *generate_msgid (const gchar *address, gchar *buf, gint len);
453 gchar *generate_mime_boundary (const gchar *prefix);
455 gint quote_cmd_argument(gchar * result, guint size,
457 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
463 #endif /* __UTILS_H__ */