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