2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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.
28 #include <glib-object.h>
33 #include <sys/types.h>
43 /* The AC_CHECK_SIZEOF() in configure fails for some machines.
44 * we provide some fallback values here */
45 #if !SIZEOF_UNSIGNED_SHORT
46 #undef SIZEOF_UNSIGNED_SHORT
47 #define SIZEOF_UNSIGNED_SHORT 2
49 #if !SIZEOF_UNSIGNED_INT
50 #undef SIZEOF_UNSIGNED_INT
51 #define SIZEOF_UNSIGNED_INT 4
53 #if !SIZEOF_UNSIGNED_LONG
54 #undef SIZEOF_UNSIGNED_LONG
55 #define SIZEOF_UNSIGNED_LONG 4
58 #ifndef HAVE_U32_TYPEDEF
59 #undef u32 /* maybe there is a macro with this name */
61 #define HAVE_U32_TYPEDEF
64 #ifndef BIG_ENDIAN_HOST
65 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
66 #define BIG_ENDIAN_HOST 1
70 #define CHDIR_RETURN_IF_FAIL(dir) \
72 if (change_dir(dir) < 0) return; \
75 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
77 if (change_dir(dir) < 0) return val; \
80 #define Xalloca(ptr, size, iffail) \
82 if ((ptr = alloca(size)) == NULL) { \
83 g_warning("can't allocate memory\n"); \
88 #define Xstrdup_a(ptr, str, iffail) \
92 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
93 g_warning("can't allocate memory\n"); \
101 #define Xstrndup_a(ptr, str, len, iffail) \
105 if ((__tmp = alloca(len + 1)) == NULL) { \
106 g_warning("can't allocate memory\n"); \
109 strncpy(__tmp, str, len); \
116 #define Xstrcat_a(ptr, str1, str2, iffail) \
121 len1 = strlen(str1); \
122 len2 = strlen(str2); \
123 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
124 g_warning("can't allocate memory\n"); \
127 memcpy(__tmp, str1, len1); \
128 memcpy(__tmp + len1, str2, len2 + 1); \
134 #define AUTORELEASE_STR(str, iffail) \
137 Xstrdup_a(__str, str, iffail); \
142 #define FILE_OP_ERROR(file, func) \
144 fprintf(stderr, "%s: ", file); \
148 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
154 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
156 /* debug functions */
157 void debug_set_mode (gboolean mode);
158 gboolean debug_get_mode (void);
159 #define debug_print \
160 debug_print_real(__FILE__ ":%d:", __LINE__), \
163 /* for macro expansion */
165 #define Xstr(x) Str(x)
167 void list_free_strings (GList *list);
168 void slist_free_strings (GSList *list);
170 void hash_free_strings (GHashTable *table);
171 void hash_free_value_mem (GHashTable *table);
173 gint str_case_equal (gconstpointer v,
175 guint str_case_hash (gconstpointer key);
177 void ptr_array_free_strings (GPtrArray *array);
179 typedef gboolean (*StrFindFunc) (const gchar *haystack,
180 const gchar *needle);
182 gboolean str_find (const gchar *haystack,
183 const gchar *needle);
184 gboolean str_case_find (const gchar *haystack,
185 const gchar *needle);
186 gboolean str_find_equal (const gchar *haystack,
187 const gchar *needle);
188 gboolean str_case_find_equal (const gchar *haystack,
189 const gchar *needle);
191 /* number-string conversion */
192 gint to_number (const gchar *nstr);
193 gchar *itos_buf (gchar *nstr,
195 gchar *itos (gint n);
196 gchar *to_human_readable (off_t size);
198 /* alternative string functions */
199 gint strcmp2 (const gchar *s1,
201 gchar *strstr2 (const gchar *s1,
203 gint path_cmp (const gchar *s1,
205 gchar *strretchomp (gchar *str);
206 gchar *strtailchomp (gchar *str,
208 gchar *strcrchomp (gchar *str);
209 gchar *strcasestr (const gchar *haystack,
210 const gchar *needle);
211 gpointer my_memmem (gconstpointer haystack,
213 gconstpointer needle,
215 gchar *strncpy2 (gchar *dest,
219 /* wide-character functions */
221 int iswalnum (wint_t wc);
224 int iswspace (wint_t wc);
227 wint_t towlower (wint_t wc);
231 size_t wcslen (const wchar_t *s);
234 wchar_t *wcscpy (wchar_t *dest,
238 wchar_t *wcsncpy (wchar_t *dest,
243 wchar_t *wcsdup (const wchar_t *s);
244 wchar_t *wcsndup (const wchar_t *s,
246 wchar_t *strdup_mbstowcs (const gchar *s);
247 gchar *strdup_wcstombs (const wchar_t *s);
248 gint wcsncasecmp (const wchar_t *s1,
251 wchar_t *wcscasestr (const wchar_t *haystack,
252 const wchar_t *needle);
253 gint get_mbs_len (const gchar *s);
255 gboolean is_next_nonascii (const guchar *s);
256 gint get_next_word_len (const guchar *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_for_compare (gchar *str);
264 void trim_subject_for_sort (gchar *str);
265 void trim_subject (gchar *str);
266 void eliminate_parenthesis (gchar *str,
269 void extract_parenthesis (gchar *str,
273 void extract_parenthesis_with_skip_quote (gchar *str,
278 void eliminate_quote (gchar *str,
280 void extract_quote (gchar *str,
282 void eliminate_address_comment (gchar *str);
283 gchar *strchr_with_skip_quote (const gchar *str,
286 gchar *strrchr_with_skip_quote (const gchar *str,
289 void extract_address (gchar *str);
290 void extract_list_id_str (gchar *str);
292 GSList *slist_concat_unique (GSList *first,
294 GSList *address_list_append (GSList *addr_list,
296 GSList *address_list_append_with_comments(GSList *addr_list,
298 GSList *references_list_prepend (GSList *msgid_list,
300 GSList *references_list_append (GSList *msgid_list,
302 GSList *newsgroup_list_append (GSList *group_list,
305 GList *add_history (GList *list,
308 void remove_return (gchar *str);
309 void remove_space (gchar *str);
310 void unfold_line (gchar *str);
311 void subst_char (gchar *str,
314 void subst_chars (gchar *str,
317 void subst_for_filename (gchar *str);
318 void subst_for_shellsafe_filename (gchar *str);
319 gboolean is_header_line (const gchar *str);
320 gboolean is_ascii_str (const guchar *str);
321 gint get_quote_level (const gchar *str,
322 const gchar *quote_chars);
323 gchar *strstr_with_skip_quote (const gchar *haystack,
324 const gchar *needle);
325 gchar *strchr_parenthesis_close (const gchar *str,
329 gchar **strsplit_parenthesis (const gchar *str,
333 gchar **strsplit_with_quote (const gchar *str,
337 gchar *get_abbrev_newsgroup_name (const gchar *group,
339 gchar *trim_string (const gchar *str,
342 GList *uri_list_extract_filenames (const gchar *uri_list);
343 gboolean is_uri_string (const gchar *str);
344 gchar *get_uri_path (const gchar *uri);
345 gint get_uri_len (const gchar *str);
346 void decode_uri (gchar *decoded_uri,
347 const gchar *encoded_uri);
348 gint scan_mailto_url (const gchar *mailto,
355 /* return static strings */
356 const gchar *get_home_dir (void);
357 const gchar *get_rc_dir (void);
358 const gchar *get_news_cache_dir (void);
359 const gchar *get_imap_cache_dir (void);
360 const gchar *get_mbox_cache_dir (void);
361 const gchar *get_mime_tmp_dir (void);
362 const gchar *get_template_dir (void);
363 const gchar *get_header_cache_dir (void);
364 const gchar *get_tmp_dir (void);
365 gchar *get_tmp_file (void);
366 const gchar *get_domain_name (void);
368 /* file / directory handling */
369 off_t get_file_size (const gchar *file);
370 off_t get_file_size_as_crlf (const gchar *file);
371 off_t get_left_file_size (FILE *fp);
373 gboolean file_exist (const gchar *file,
374 gboolean allow_fifo);
375 gboolean is_dir_exist (const gchar *dir);
376 gboolean is_file_entry_exist (const gchar *file);
377 gboolean dirent_is_regular_file (struct dirent *d);
378 gboolean dirent_is_directory (struct dirent *d);
380 #define is_file_exist(file) file_exist(file, FALSE)
381 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
383 gint change_dir (const gchar *dir);
384 gint make_dir (const gchar *dir);
385 gint make_dir_hier (const gchar *dir);
386 gint remove_all_files (const gchar *dir);
387 gint remove_numbered_files (const gchar *dir,
390 gint remove_numbered_files_not_in_list(const gchar *dir,
392 gint remove_all_numbered_files (const gchar *dir);
393 gint remove_expired_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 copy_file (const gchar *src,
401 gboolean keep_backup);
402 gint move_file (const gchar *src,
405 gint copy_file_part_to_fp (FILE *fp,
409 gint copy_file_part (FILE *fp,
414 gchar *canonicalize_str (const gchar *str);
415 gint canonicalize_file (const gchar *src,
417 gint canonicalize_file_replace (const gchar *file);
418 gint uncanonicalize_file (const gchar *src,
420 gint uncanonicalize_file_replace(const gchar *file);
422 gchar *normalize_newlines (const gchar *str);
424 gchar *get_outgoing_rfc2822_str (FILE *fp);
426 gint change_file_mode_rw (FILE *fp,
428 FILE *my_tmpfile (void);
429 FILE *get_tmpfile_in_dir (const gchar *dir,
431 FILE *str_open_as_stream (const gchar *str);
432 gint str_write_to_file (const gchar *str,
434 gchar *file_read_to_str (const gchar *file);
435 gchar *file_read_stream_to_str (FILE *fp);
437 /* process execution */
438 gint execute_async (gchar *const argv[]);
439 gint execute_sync (gchar *const argv[]);
440 gint execute_command_line (const gchar *cmdline,
442 gchar *get_command_output (const gchar *cmdline);
444 /* open URI with external browser */
445 gint open_uri(const gchar *uri, const gchar *cmdline);
448 time_t remote_tzoffset_sec (const gchar *zone);
449 time_t tzoffset_sec (time_t *now);
450 gchar *tzoffset (time_t *now);
451 void get_rfc822_date (gchar *buf,
455 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
457 /* subject threading */
458 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
459 void subject_table_insert(GHashTable *subject_table, gchar * subject,
461 void subject_table_remove(GHashTable *subject_table, gchar * subject);
462 gint subject_get_prefix_length (const gchar *subject);
464 /* quoting recognition */
465 const gchar * line_has_quote_char (const gchar *str,
466 const gchar *quote_chars);
467 const gchar * line_has_quote_char_last (const gchar *str,
468 const gchar *quote_chars);
470 guint g_stricase_hash (gconstpointer gptr);
471 gint g_stricase_equal (gconstpointer gptr1, gconstpointer gptr2);
472 gint g_int_compare (gconstpointer a, gconstpointer b);
474 gchar *generate_msgid (gchar *buf, gint len);
475 gchar *generate_mime_boundary (const gchar *prefix);
477 gint quote_cmd_argument(gchar * result, guint size,
479 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
481 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
482 void get_hex_str(gchar *out, guchar ch);
484 /* auto pointer for containers that support GType system */
486 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
487 typedef struct AutoPointer GAuto;
488 GType g_auto_pointer_register (void);
489 GAuto *g_auto_pointer_new (gpointer pointer);
490 GAuto *g_auto_pointer_new_with_free (gpointer p,
492 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
493 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
494 void g_auto_pointer_free (GAuto *auto_ptr);
500 #endif /* __UTILS_H__ */