2007-07-11 [colin] 2.10.0cvs16
[claws.git] / src / common / utils.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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
192 #ifndef __CYGWIN__
193 #define debug_print \
194         debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
195         debug_print_real
196 #else
197   /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
198 #define debug_print \
199         debug_print_real("%s:%d:", __FILE__, __LINE__), \
200         debug_print_real
201 #endif
202
203 /* for macro expansion */
204 #define Str(x)  #x
205 #define Xstr(x) Str(x)
206
207
208 /* System related stuff.  */
209
210 gboolean superuser_p (void);
211
212
213 /* String utilities.  */
214
215 void list_free_strings          (GList          *list);
216 void slist_free_strings         (GSList         *list);
217
218 void hash_free_strings          (GHashTable     *table);
219 void hash_free_value_mem        (GHashTable     *table);
220
221 gint str_case_equal             (gconstpointer   v,
222                                  gconstpointer   v2);
223 guint str_case_hash             (gconstpointer   key);
224
225 void ptr_array_free_strings     (GPtrArray      *array);
226
227 typedef gboolean (*StrFindFunc) (const gchar    *haystack,
228                                  const gchar    *needle);
229
230 gboolean str_find               (const gchar    *haystack,
231                                  const gchar    *needle);
232 gboolean str_case_find          (const gchar    *haystack,
233                                  const gchar    *needle);
234 gboolean str_find_equal         (const gchar    *haystack,
235                                  const gchar    *needle);
236 gboolean str_case_find_equal    (const gchar    *haystack,
237                                  const gchar    *needle);
238
239 /* number-string conversion */
240 gint to_number                  (const gchar *nstr);
241 gchar *itos_buf                 (gchar       *nstr,
242                                  gint         n);
243 gchar *itos                     (gint         n);
244 gchar *to_human_readable        (off_t        size);
245
246 /* alternative string functions */
247 gint strcmp2            (const gchar    *s1,
248                          const gchar    *s2);
249 gchar *strstr2          (const gchar    *s1,
250                          const gchar    *s2);
251 gint path_cmp           (const gchar    *s1,
252                          const gchar    *s2);
253 gchar *strretchomp      (gchar          *str);
254 gchar *strtailchomp     (gchar          *str,
255                          gchar           tail_char);
256 gchar *strcrchomp       (gchar          *str);
257 void file_strip_crs     (const gchar    *file);
258 gchar *strcasestr       (const gchar    *haystack,
259                          const gchar    *needle);
260 gpointer my_memmem      (gconstpointer   haystack,
261                          size_t          haystacklen,
262                          gconstpointer   needle,
263                          size_t          needlelen);
264 gchar *strncpy2         (gchar          *dest,
265                          const gchar    *src,
266                          size_t          n);
267
268 gboolean is_next_nonascii       (const gchar *s);
269 gint get_next_word_len          (const gchar *s);
270
271 /* functions for string parsing */
272 gint subject_compare                    (const gchar    *s1,
273                                          const gchar    *s2);
274 gint subject_compare_for_sort           (const gchar    *s1,
275                                          const gchar    *s2);
276 void trim_subject_for_compare           (gchar          *str);
277 void trim_subject_for_sort              (gchar          *str);
278 void trim_subject                       (gchar          *str);
279 void eliminate_parenthesis              (gchar          *str,
280                                          gchar           op,
281                                          gchar           cl);
282 void extract_parenthesis                (gchar          *str,
283                                          gchar           op,
284                                          gchar           cl);
285
286 void extract_parenthesis_with_skip_quote        (gchar          *str,
287                                                  gchar           quote_chr,
288                                                  gchar           op,
289                                                  gchar           cl);
290
291 void eliminate_quote                    (gchar          *str,
292                                          gchar           quote_chr);
293 void extract_quote                      (gchar          *str,
294                                          gchar           quote_chr);
295 void eliminate_address_comment          (gchar          *str);
296 gchar *strchr_with_skip_quote           (const gchar    *str,
297                                          gint            quote_chr,
298                                          gint            c);
299 gchar *strrchr_with_skip_quote          (const gchar    *str,
300                                          gint            quote_chr,
301                                          gint            c);
302 void extract_address                    (gchar          *str);
303 void extract_list_id_str                (gchar          *str);
304
305 GSList *slist_concat_unique             (GSList         *first,
306                                          GSList         *second);
307 GSList *address_list_append             (GSList         *addr_list,
308                                          const gchar    *str);
309 GSList *address_list_append_with_comments(GSList        *addr_list,
310                                          const gchar    *str);
311 GSList *references_list_prepend         (GSList         *msgid_list,
312                                          const gchar    *str);
313 GSList *references_list_append          (GSList         *msgid_list,
314                                          const gchar    *str);
315 GSList *newsgroup_list_append           (GSList         *group_list,
316                                          const gchar    *str);
317
318 GList *add_history                      (GList          *list,
319                                          const gchar    *str);
320
321 void remove_return                      (gchar          *str);
322 void remove_space                       (gchar          *str);
323 void unfold_line                        (gchar          *str);
324 void subst_char                         (gchar          *str,
325                                          gchar           orig,
326                                          gchar           subst);
327 void subst_chars                        (gchar          *str,
328                                          gchar          *orig,
329                                          gchar           subst);
330 void subst_for_filename                 (gchar          *str);
331 void subst_for_shellsafe_filename       (gchar          *str);
332 gboolean is_header_line                 (const gchar    *str);
333 gboolean is_ascii_str                   (const gchar    *str);
334 gint get_quote_level                    (const gchar    *str,
335                                          const gchar    *quote_chars);
336 gint check_line_length                  (const gchar    *str,
337                                          gint            max_chars,
338                                          gint           *line);
339
340 gchar *strstr_with_skip_quote           (const gchar    *haystack,
341                                          const gchar    *needle);
342 gchar *strchr_parenthesis_close         (const gchar    *str,
343                                          gchar           op,
344                                          gchar           cl);
345
346 gchar **strsplit_parenthesis            (const gchar    *str,
347                                          gchar           op,
348                                          gchar           cl,
349                                          gint            max_tokens);
350 gchar **strsplit_with_quote             (const gchar    *str,
351                                          const gchar    *delim,
352                                          gint            max_tokens);
353
354 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
355                                          gint            len);
356 gchar *trim_string                      (const gchar    *str,
357                                          gint            len);
358
359 GList *uri_list_extract_filenames       (const gchar    *uri_list);
360 gboolean is_uri_string                  (const gchar    *str);
361 gchar *get_uri_path                     (const gchar    *uri);
362 gint get_uri_len                        (const gchar    *str);
363 void decode_uri                         (gchar          *decoded_uri,
364                                          const gchar    *encoded_uri);
365 void decode_uri_with_plus               (gchar          *decoded_uri, 
366                                          const gchar    *encoded_uri, 
367                                          gboolean        with_plus);
368 gint scan_mailto_url                    (const gchar    *mailto,
369                                          gchar         **to,
370                                          gchar         **cc,
371                                          gchar         **bcc,
372                                          gchar         **subject,
373                                          gchar         **body,
374                                          gchar         **attach);
375
376 /* return static strings */
377 const gchar *get_home_dir               (void);
378 const gchar *get_rc_dir                 (void);
379 const gchar *get_mail_base_dir          (void);
380 const gchar *get_news_cache_dir         (void);
381 const gchar *get_imap_cache_dir         (void);
382 const gchar *get_mbox_cache_dir         (void);
383 const gchar *get_mime_tmp_dir           (void);
384 const gchar *get_template_dir           (void);
385 const gchar *get_header_cache_dir       (void);
386 const gchar *get_plugin_dir             (void);
387 const gchar *get_tmp_dir                (void);
388 const gchar *get_locale_dir             (void);
389 gchar *get_tmp_file                     (void);
390 const gchar *get_domain_name            (void);
391
392 /* file / directory handling */
393 off_t get_file_size             (const gchar    *file);
394 off_t get_file_size_as_crlf     (const gchar    *file);
395 off_t get_left_file_size        (FILE           *fp);
396
397 time_t get_file_mtime           (const gchar *file);
398
399 gboolean file_exist             (const gchar    *file,
400                                  gboolean        allow_fifo);
401 gboolean is_relative_filename   (const gchar *file);
402 gboolean is_dir_exist           (const gchar    *dir);
403 gboolean is_file_entry_exist    (const gchar    *file);
404 gboolean dirent_is_regular_file (struct dirent  *d);
405 gboolean dirent_is_directory    (struct dirent  *d);
406
407 #define is_file_exist(file)             file_exist(file, FALSE)
408 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
409
410 gint change_dir                 (const gchar    *dir);
411 gint make_dir                   (const gchar    *dir);
412 gint make_dir_hier              (const gchar    *dir);
413 gint remove_all_files           (const gchar    *dir);
414 gint remove_numbered_files      (const gchar    *dir,
415                                  guint           first,
416                                  guint           last);
417 gint remove_numbered_files_not_in_list(const gchar *dir,
418                                        GSList *numberlist);
419 gint remove_all_numbered_files  (const gchar    *dir);
420 gint remove_expired_files       (const gchar    *dir,
421                                  guint           hours);
422 gint remove_dir_recursive       (const gchar    *dir);
423 gint append_file                (const gchar    *src,
424                                  const gchar    *dest,
425                                  gboolean        keep_backup);
426 gint rename_force               (const gchar    *oldpath,
427                                  const gchar    *newpath);
428 gint copy_file                  (const gchar    *src,
429                                  const gchar    *dest,
430                                  gboolean        keep_backup);
431 gint move_file                  (const gchar    *src,
432                                  const gchar    *dest,
433                                  gboolean        overwrite);
434 gint copy_dir                   (const gchar    *src,
435                                  const gchar    *dest);
436 gint copy_file_part_to_fp       (FILE           *fp,
437                                  off_t           offset,
438                                  size_t          length,
439                                  FILE           *dest_fp);
440 gint copy_file_part             (FILE           *fp,
441                                  off_t           offset,
442                                  size_t          length,
443                                  const gchar    *dest);
444
445 gchar *canonicalize_str         (const gchar    *str);
446 gint canonicalize_file          (const gchar    *src,
447                                  const gchar    *dest);
448 gint canonicalize_file_replace  (const gchar    *file);
449 gint uncanonicalize_file        (const gchar    *src,
450                                  const gchar    *dest);
451 gint uncanonicalize_file_replace(const gchar    *file);
452
453 gchar *normalize_newlines       (const gchar    *str);
454
455 gchar *get_outgoing_rfc2822_str (FILE           *fp);
456
457 gint change_file_mode_rw        (FILE           *fp,
458                                  const gchar    *file);
459 FILE *my_tmpfile                (void);
460 FILE *get_tmpfile_in_dir        (const gchar    *dir,
461                                  gchar         **filename);
462 FILE *str_open_as_stream        (const gchar    *str);
463 gint str_write_to_file          (const gchar    *str,
464                                  const gchar    *file);
465 gchar *file_read_to_str         (const gchar    *file);
466 gchar *file_read_stream_to_str  (FILE           *fp);
467
468 char *fgets_crlf(char *buf, int size, FILE *stream);
469
470 /* process execution */
471 gint execute_command_line       (const gchar    *cmdline,
472                                  gboolean        async);
473 gchar *get_command_output       (const gchar    *cmdline);
474
475 /* open URI with external browser */
476 gint open_uri(const gchar *uri, const gchar *cmdline);
477 /* open file with text editor */
478 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
479
480 /* time functions */
481 time_t remote_tzoffset_sec      (const gchar    *zone);
482 time_t tzoffset_sec             (time_t         *now);
483 gchar *tzoffset                 (time_t         *now);
484 void get_rfc822_date            (gchar          *buf,
485                                  gint            len);
486
487 size_t my_strftime              (gchar                  *s,
488                                  size_t                  max,
489                                  const gchar            *format,
490                                  const struct tm        *tm);
491 size_t fast_strftime            (gchar                  *buf, 
492                                  gint                    buflen, 
493                                  const gchar            *format, 
494                                  struct tm              *lt);
495
496 /* debugging */
497 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
498 const char * debug_srcname (const char *file);
499
500 /* subject threading */
501 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
502 void subject_table_insert(GHashTable *subject_table, gchar * subject,
503                           void * data);
504 void subject_table_remove(GHashTable *subject_table, gchar * subject);
505 gint subject_get_prefix_length (const gchar *subject);
506
507 /* quoting recognition */
508 const gchar * line_has_quote_char       (const gchar *str,
509                                          const gchar *quote_chars);
510 const gchar * line_has_quote_char_last  (const gchar *str,
511                                          const gchar *quote_chars);
512
513 guint g_stricase_hash   (gconstpointer gptr);
514 gint g_stricase_equal   (gconstpointer gptr1, gconstpointer gptr2);
515 gint g_int_compare      (gconstpointer a, gconstpointer b);
516
517 gchar *generate_msgid           (gchar *buf, gint len);
518 gchar *generate_mime_boundary   (const gchar *prefix);
519
520 gint quote_cmd_argument(gchar * result, guint size,
521                         const gchar * path);
522 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
523
524 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
525 void get_hex_str(gchar *out, guchar ch);
526
527 /* auto pointer for containers that support GType system */
528
529 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
530 typedef struct AutoPointer      GAuto;
531 GType g_auto_pointer_register           (void);
532 GAuto *g_auto_pointer_new               (gpointer pointer);
533 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
534                                          GFreeFunc free);
535 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
536 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
537 void g_auto_pointer_free                (GAuto *auto_ptr);
538 void replace_returns                    (gchar *str);
539 gboolean get_uri_part   (const gchar *start,
540                          const gchar *scanpos,
541                          const gchar **bp,
542                          const gchar **ep,
543                          gboolean hdr);
544 gchar *make_uri_string  (const gchar *bp,
545                          const gchar *ep);
546 gboolean get_email_part (const gchar *start, 
547                          const gchar *scanpos,
548                          const gchar **bp, 
549                          const gchar **ep,
550                          gboolean hdr);
551 gchar *make_email_string(const gchar *bp,
552                          const gchar *ep);
553 gchar *make_http_string (const gchar *bp,
554                          const gchar *ep);
555
556 gchar *mailcap_get_command_for_type(const gchar *type, 
557                                     const gchar *file_to_open);
558 void mailcap_update_default        (const gchar *type,
559                                     const gchar *command);
560
561 gboolean file_is_email(const gchar *filename);
562 gboolean sc_g_list_bigger(GList *list, gint max);
563 gboolean sc_g_slist_bigger(GSList *list, gint max);
564 #ifdef __cplusplus
565 }
566 #endif
567
568 #endif /* __UTILS_H__ */