4e6a3f949bb73373f2e617a6a66b5c3def289e49
[claws.git] / src / common / utils.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
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.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef __UTILS_H__
21 #define __UTILS_H__
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <glib.h>
28 #include <glib-object.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 #include <time.h>
36 #if HAVE_ALLOCA_H
37 #  include <alloca.h>
38 #endif
39 #if HAVE_WCHAR_H
40 #  include <wchar.h>
41 #endif
42
43 /* Wrappers for C library function that take pathname arguments. */
44 #if GLIB_CHECK_VERSION(2, 6, 0)
45 #  include <glib/gstdio.h>
46 #else
47
48 #define g_open          open
49 #define g_rename        rename
50 #define g_mkdir         mkdir
51 #define g_stat          stat
52 #define g_lstat         lstat
53 #define g_unlink        unlink
54 #define g_remove        remove
55 #define g_rmdir         rmdir
56 #define g_fopen         fopen
57 #define g_freopen       freopen
58
59 #endif /* GLIB_CHECK_VERSION */
60
61 #if !GLIB_CHECK_VERSION(2, 7, 0)
62
63 #ifdef G_OS_UNIX
64 #define g_chdir         chdir
65 #define g_chmod         chmod
66 #else
67 gint g_chdir    (const gchar    *path);
68 gint g_chmod    (const gchar    *path,
69                  gint            mode);
70 #endif /* G_OS_UNIX */
71
72 #endif /* !GLIB_CHECK_VERSION */
73
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
79 #endif
80 #if !SIZEOF_UNSIGNED_INT
81   #undef SIZEOF_UNSIGNED_INT
82   #define SIZEOF_UNSIGNED_INT 4
83 #endif
84 #if !SIZEOF_UNSIGNED_LONG
85   #undef SIZEOF_UNSIGNED_LONG
86   #define SIZEOF_UNSIGNED_LONG 4
87 #endif
88
89 #ifndef HAVE_U32_TYPEDEF
90   #undef u32        /* maybe there is a macro with this name */
91   typedef guint32 u32;
92   #define HAVE_U32_TYPEDEF
93 #endif
94
95 #ifndef BIG_ENDIAN_HOST
96   #if (G_BYTE_ORDER == G_BIG_ENDIAN)
97     #define BIG_ENDIAN_HOST 1
98   #endif
99 #endif
100
101 #define CHDIR_RETURN_IF_FAIL(dir) \
102 { \
103         if (change_dir(dir) < 0) return; \
104 }
105
106 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
107 { \
108         if (change_dir(dir) < 0) return val; \
109 }
110
111 #define Xalloca(ptr, size, iffail) \
112 { \
113         if ((ptr = alloca(size)) == NULL) { \
114                 g_warning("can't allocate memory\n"); \
115                 iffail; \
116         } \
117 }
118
119 #define Xstrdup_a(ptr, str, iffail) \
120 { \
121         gchar *__tmp; \
122  \
123         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
124                 g_warning("can't allocate memory\n"); \
125                 iffail; \
126         } else \
127                 strcpy(__tmp, str); \
128  \
129         ptr = __tmp; \
130 }
131
132 #define Xstrndup_a(ptr, str, len, iffail) \
133 { \
134         gchar *__tmp; \
135  \
136         if ((__tmp = alloca(len + 1)) == NULL) { \
137                 g_warning("can't allocate memory\n"); \
138                 iffail; \
139         } else { \
140                 strncpy(__tmp, str, len); \
141                 __tmp[len] = '\0'; \
142         } \
143  \
144         ptr = __tmp; \
145 }
146
147 #define Xstrcat_a(ptr, str1, str2, iffail) \
148 { \
149         gchar *__tmp; \
150         gint len1, len2; \
151  \
152         len1 = strlen(str1); \
153         len2 = strlen(str2); \
154         if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
155                 g_warning("can't allocate memory\n"); \
156                 iffail; \
157         } else { \
158                 memcpy(__tmp, str1, len1); \
159                 memcpy(__tmp + len1, str2, len2 + 1); \
160         } \
161  \
162         ptr = __tmp; \
163 }
164
165 #define AUTORELEASE_STR(str, iffail) \
166 { \
167         gchar *__str; \
168         Xstrdup_a(__str, str, iffail); \
169         g_free(str); \
170         str = __str; \
171 }
172
173 #define FILE_OP_ERROR(file, func) \
174 { \
175         fprintf(stderr, "%s: ", file); \
176         fflush(stderr); \
177         perror(func); \
178 }
179
180 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
181
182 #ifdef __cplusplus
183 extern "C" {
184 #endif
185
186 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
187
188 /* debug functions */
189 void debug_set_mode             (gboolean mode);
190 gboolean debug_get_mode         (void);
191 #define debug_print \
192         debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
193         debug_print_real
194
195 /* for macro expansion */
196 #define Str(x)  #x
197 #define Xstr(x) Str(x)
198
199 void list_free_strings          (GList          *list);
200 void slist_free_strings         (GSList         *list);
201
202 void hash_free_strings          (GHashTable     *table);
203 void hash_free_value_mem        (GHashTable     *table);
204
205 gint str_case_equal             (gconstpointer   v,
206                                  gconstpointer   v2);
207 guint str_case_hash             (gconstpointer   key);
208
209 void ptr_array_free_strings     (GPtrArray      *array);
210
211 typedef gboolean (*StrFindFunc) (const gchar    *haystack,
212                                  const gchar    *needle);
213
214 gboolean str_find               (const gchar    *haystack,
215                                  const gchar    *needle);
216 gboolean str_case_find          (const gchar    *haystack,
217                                  const gchar    *needle);
218 gboolean str_find_equal         (const gchar    *haystack,
219                                  const gchar    *needle);
220 gboolean str_case_find_equal    (const gchar    *haystack,
221                                  const gchar    *needle);
222
223 /* number-string conversion */
224 gint to_number                  (const gchar *nstr);
225 gchar *itos_buf                 (gchar       *nstr,
226                                  gint         n);
227 gchar *itos                     (gint         n);
228 gchar *to_human_readable        (off_t        size);
229
230 /* alternative string functions */
231 gint strcmp2            (const gchar    *s1,
232                          const gchar    *s2);
233 gchar *strstr2          (const gchar    *s1,
234                          const gchar    *s2);
235 gint path_cmp           (const gchar    *s1,
236                          const gchar    *s2);
237 gchar *strretchomp      (gchar          *str);
238 gchar *strtailchomp     (gchar          *str,
239                          gchar           tail_char);
240 gchar *strcrchomp       (gchar          *str);
241 void file_strip_crs     (const gchar    *file);
242 gchar *strcasestr       (const gchar    *haystack,
243                          const gchar    *needle);
244 gpointer my_memmem      (gconstpointer   haystack,
245                          size_t          haystacklen,
246                          gconstpointer   needle,
247                          size_t          needlelen);
248 gchar *strncpy2         (gchar          *dest,
249                          const gchar    *src,
250                          size_t          n);
251
252 /* wide-character functions */
253 #if !HAVE_ISWALNUM
254 int iswalnum            (wint_t wc);
255 #endif
256 #if !HAVE_ISWSPACE
257 int iswspace            (wint_t wc);
258 #endif
259 #if !HAVE_TOWLOWER
260 wint_t towlower         (wint_t wc);
261 #endif
262
263 #if !HAVE_WCSLEN
264 size_t wcslen           (const wchar_t *s);
265 #endif
266 #if !HAVE_WCSCPY
267 wchar_t *wcscpy         (wchar_t       *dest,
268                          const wchar_t *src);
269 #endif
270 #if !HAVE_WCSNCPY
271 wchar_t *wcsncpy        (wchar_t       *dest,
272                          const wchar_t *src,
273                          size_t         n);
274 #endif
275
276 wchar_t *wcsdup                 (const wchar_t *s);
277 wchar_t *wcsndup                (const wchar_t *s,
278                                  size_t         n);
279 wchar_t *strdup_mbstowcs        (const gchar   *s);
280 gchar *strdup_wcstombs          (const wchar_t *s);
281 gint wcsncasecmp                (const wchar_t *s1,
282                                  const wchar_t *s2,
283                                  size_t         n);
284 wchar_t *wcscasestr             (const wchar_t *haystack,
285                                  const wchar_t *needle);
286 gint get_mbs_len                (const gchar    *s);
287
288 gboolean is_next_nonascii       (const gchar *s);
289 gint get_next_word_len          (const gchar *s);
290
291 /* functions for string parsing */
292 gint subject_compare                    (const gchar    *s1,
293                                          const gchar    *s2);
294 gint subject_compare_for_sort           (const gchar    *s1,
295                                          const gchar    *s2);
296 void trim_subject_for_compare           (gchar          *str);
297 void trim_subject_for_sort              (gchar          *str);
298 void trim_subject                       (gchar          *str);
299 void eliminate_parenthesis              (gchar          *str,
300                                          gchar           op,
301                                          gchar           cl);
302 void extract_parenthesis                (gchar          *str,
303                                          gchar           op,
304                                          gchar           cl);
305
306 void extract_parenthesis_with_skip_quote        (gchar          *str,
307                                                  gchar           quote_chr,
308                                                  gchar           op,
309                                                  gchar           cl);
310
311 void eliminate_quote                    (gchar          *str,
312                                          gchar           quote_chr);
313 void extract_quote                      (gchar          *str,
314                                          gchar           quote_chr);
315 void eliminate_address_comment          (gchar          *str);
316 gchar *strchr_with_skip_quote           (const gchar    *str,
317                                          gint            quote_chr,
318                                          gint            c);
319 gchar *strrchr_with_skip_quote          (const gchar    *str,
320                                          gint            quote_chr,
321                                          gint            c);
322 void extract_address                    (gchar          *str);
323 void extract_list_id_str                (gchar          *str);
324
325 GSList *slist_concat_unique             (GSList         *first,
326                                          GSList         *second);
327 GSList *address_list_append             (GSList         *addr_list,
328                                          const gchar    *str);
329 GSList *address_list_append_with_comments(GSList        *addr_list,
330                                          const gchar    *str);
331 GSList *references_list_prepend         (GSList         *msgid_list,
332                                          const gchar    *str);
333 GSList *references_list_append          (GSList         *msgid_list,
334                                          const gchar    *str);
335 GSList *newsgroup_list_append           (GSList         *group_list,
336                                          const gchar    *str);
337
338 GList *add_history                      (GList          *list,
339                                          const gchar    *str);
340
341 void remove_return                      (gchar          *str);
342 void remove_space                       (gchar          *str);
343 void unfold_line                        (gchar          *str);
344 void subst_char                         (gchar          *str,
345                                          gchar           orig,
346                                          gchar           subst);
347 void subst_chars                        (gchar          *str,
348                                          gchar          *orig,
349                                          gchar           subst);
350 void subst_for_filename                 (gchar          *str);
351 void subst_for_shellsafe_filename       (gchar          *str);
352 gboolean is_header_line                 (const gchar    *str);
353 gboolean is_ascii_str                   (const gchar    *str);
354 gint get_quote_level                    (const gchar    *str,
355                                          const gchar    *quote_chars);
356 gint check_line_length                  (const gchar    *str,
357                                          gint            max_chars,
358                                          gint           *line);
359
360 gchar *strstr_with_skip_quote           (const gchar    *haystack,
361                                          const gchar    *needle);
362 gchar *strchr_parenthesis_close         (const gchar    *str,
363                                          gchar           op,
364                                          gchar           cl);
365
366 gchar **strsplit_parenthesis            (const gchar    *str,
367                                          gchar           op,
368                                          gchar           cl,
369                                          gint            max_tokens);
370 gchar **strsplit_with_quote             (const gchar    *str,
371                                          const gchar    *delim,
372                                          gint            max_tokens);
373
374 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
375                                          gint            len);
376 gchar *trim_string                      (const gchar    *str,
377                                          gint            len);
378
379 GList *uri_list_extract_filenames       (const gchar    *uri_list);
380 gboolean is_uri_string                  (const gchar    *str);
381 gchar *get_uri_path                     (const gchar    *uri);
382 gint get_uri_len                        (const gchar    *str);
383 void decode_uri                         (gchar          *decoded_uri,
384                                          const gchar    *encoded_uri);
385 gint scan_mailto_url                    (const gchar    *mailto,
386                                          gchar         **to,
387                                          gchar         **cc,
388                                          gchar         **bcc,
389                                          gchar         **subject,
390                                          gchar         **body);
391
392 /* return static strings */
393 const gchar *get_home_dir               (void);
394 const gchar *get_rc_dir                 (void);
395 const gchar *get_mail_base_dir          (void);
396 const gchar *get_news_cache_dir         (void);
397 const gchar *get_imap_cache_dir         (void);
398 const gchar *get_mbox_cache_dir         (void);
399 const gchar *get_mime_tmp_dir           (void);
400 const gchar *get_template_dir           (void);
401 const gchar *get_header_cache_dir       (void);
402 const gchar *get_plugin_dir             (void);
403 const gchar *get_tmp_dir                (void);
404 gchar *get_tmp_file                     (void);
405 const gchar *get_domain_name            (void);
406
407 /* file / directory handling */
408 off_t get_file_size             (const gchar    *file);
409 off_t get_file_size_as_crlf     (const gchar    *file);
410 off_t get_left_file_size        (FILE           *fp);
411
412 gboolean file_exist             (const gchar    *file,
413                                  gboolean        allow_fifo);
414 gboolean is_relative_filename   (const gchar *file);
415 gboolean is_dir_exist           (const gchar    *dir);
416 gboolean is_file_entry_exist    (const gchar    *file);
417 gboolean dirent_is_regular_file (struct dirent  *d);
418 gboolean dirent_is_directory    (struct dirent  *d);
419
420 #define is_file_exist(file)             file_exist(file, FALSE)
421 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
422
423 gint change_dir                 (const gchar    *dir);
424 gint make_dir                   (const gchar    *dir);
425 gint make_dir_hier              (const gchar    *dir);
426 gint remove_all_files           (const gchar    *dir);
427 gint remove_numbered_files      (const gchar    *dir,
428                                  guint           first,
429                                  guint           last);
430 gint remove_numbered_files_not_in_list(const gchar *dir,
431                                        GSList *numberlist);
432 gint remove_all_numbered_files  (const gchar    *dir);
433 gint remove_expired_files       (const gchar    *dir,
434                                  guint           hours);
435 gint remove_dir_recursive       (const gchar    *dir);
436 gint append_file                (const gchar    *src,
437                                  const gchar    *dest,
438                                  gboolean        keep_backup);
439 gint rename_force               (const gchar    *oldpath,
440                                  const gchar    *newpath);
441 gint copy_file                  (const gchar    *src,
442                                  const gchar    *dest,
443                                  gboolean        keep_backup);
444 gint move_file                  (const gchar    *src,
445                                  const gchar    *dest,
446                                  gboolean        overwrite);
447 gint copy_dir                   (const gchar    *src,
448                                  const gchar    *dest);
449 gint copy_file_part_to_fp       (FILE           *fp,
450                                  off_t           offset,
451                                  size_t          length,
452                                  FILE           *dest_fp);
453 gint copy_file_part             (FILE           *fp,
454                                  off_t           offset,
455                                  size_t          length,
456                                  const gchar    *dest);
457
458 gchar *canonicalize_str         (const gchar    *str);
459 gint canonicalize_file          (const gchar    *src,
460                                  const gchar    *dest);
461 gint canonicalize_file_replace  (const gchar    *file);
462 gint uncanonicalize_file        (const gchar    *src,
463                                  const gchar    *dest);
464 gint uncanonicalize_file_replace(const gchar    *file);
465
466 gchar *normalize_newlines       (const gchar    *str);
467
468 gchar *get_outgoing_rfc2822_str (FILE           *fp);
469
470 gint change_file_mode_rw        (FILE           *fp,
471                                  const gchar    *file);
472 FILE *my_tmpfile                (void);
473 FILE *get_tmpfile_in_dir        (const gchar    *dir,
474                                  gchar         **filename);
475 FILE *str_open_as_stream        (const gchar    *str);
476 gint str_write_to_file          (const gchar    *str,
477                                  const gchar    *file);
478 gchar *file_read_to_str         (const gchar    *file);
479 gchar *file_read_stream_to_str  (FILE           *fp);
480
481 /* process execution */
482 gint execute_async              (gchar *const    argv[]);
483 gint execute_sync               (gchar *const    argv[]);
484 gint execute_command_line       (const gchar    *cmdline,
485                                  gboolean        async);
486 gchar *get_command_output       (const gchar    *cmdline);
487
488 /* open URI with external browser */
489 gint open_uri(const gchar *uri, const gchar *cmdline);
490 /* open file with text editor */
491 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
492
493 /* time functions */
494 time_t remote_tzoffset_sec      (const gchar    *zone);
495 time_t tzoffset_sec             (time_t         *now);
496 gchar *tzoffset                 (time_t         *now);
497 void get_rfc822_date            (gchar          *buf,
498                                  gint            len);
499
500 size_t my_strftime              (gchar                  *s,
501                                  size_t                  max,
502                                  const gchar            *format,
503                                  const struct tm        *tm);
504
505 /* debugging */
506 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
507 const char * debug_srcname (const char *file);
508
509 /* subject threading */
510 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
511 void subject_table_insert(GHashTable *subject_table, gchar * subject,
512                           void * data);
513 void subject_table_remove(GHashTable *subject_table, gchar * subject);
514 gint subject_get_prefix_length (const gchar *subject);
515
516 /* quoting recognition */
517 const gchar * line_has_quote_char       (const gchar *str,
518                                          const gchar *quote_chars);
519 const gchar * line_has_quote_char_last  (const gchar *str,
520                                          const gchar *quote_chars);
521
522 guint g_stricase_hash   (gconstpointer gptr);
523 gint g_stricase_equal   (gconstpointer gptr1, gconstpointer gptr2);
524 gint g_int_compare      (gconstpointer a, gconstpointer b);
525
526 gchar *generate_msgid           (gchar *buf, gint len);
527 gchar *generate_mime_boundary   (const gchar *prefix);
528
529 gint quote_cmd_argument(gchar * result, guint size,
530                         const gchar * path);
531 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
532
533 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
534 void get_hex_str(gchar *out, guchar ch);
535
536 /* auto pointer for containers that support GType system */
537
538 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
539 typedef struct AutoPointer      GAuto;
540 GType g_auto_pointer_register           (void);
541 GAuto *g_auto_pointer_new               (gpointer pointer);
542 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
543                                          GFreeFunc free);
544 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
545 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
546 void g_auto_pointer_free                (GAuto *auto_ptr);
547 void replace_returns                    (gchar *str);
548
549 gboolean get_uri_part(const gchar *start, const gchar *scanpos,
550                              const gchar **bp, const gchar **ep, gboolean hdr);
551 gchar *make_uri_string(const gchar *bp, const gchar *ep);
552 gboolean get_email_part(const gchar *start, const gchar *scanpos,
553                                const gchar **bp, const gchar **ep, gboolean hdr);
554 gchar *make_email_string(const gchar *bp, const gchar *ep);
555 gchar *make_http_string(const gchar *bp, const gchar *ep);
556 gchar *mailcap_get_command_for_type(const gchar *type);
557
558 #ifdef __cplusplus
559 }
560 #endif
561
562 #endif /* __UTILS_H__ */