f434740ec38e930330d5194d5bbbb46c5fdac2c7
[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 gint 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 void  set_rc_dir                        (const gchar *dir);
388 gboolean rc_dir_is_alt                  (void);
389 const gchar *get_mail_base_dir          (void);
390 const gchar *get_news_cache_dir         (void);
391 const gchar *get_imap_cache_dir         (void);
392 const gchar *get_mbox_cache_dir         (void);
393 const gchar *get_mime_tmp_dir           (void);
394 const gchar *get_template_dir           (void);
395 const gchar *get_header_cache_dir       (void);
396 const gchar *get_plugin_dir             (void);
397 const gchar *get_tmp_dir                (void);
398 const gchar *get_locale_dir             (void);
399 gchar *get_tmp_file                     (void);
400 const gchar *get_domain_name            (void);
401
402 /* file / directory handling */
403 off_t get_file_size             (const gchar    *file);
404 off_t get_file_size_as_crlf     (const gchar    *file);
405 off_t get_left_file_size        (FILE           *fp);
406
407 time_t get_file_mtime           (const gchar *file);
408
409 gboolean file_exist             (const gchar    *file,
410                                  gboolean        allow_fifo);
411 gboolean is_relative_filename   (const gchar *file);
412 gboolean is_dir_exist           (const gchar    *dir);
413 gboolean is_file_entry_exist    (const gchar    *file);
414 gboolean dirent_is_regular_file (struct dirent  *d);
415 gboolean dirent_is_directory    (struct dirent  *d);
416
417 #define is_file_exist(file)             file_exist(file, FALSE)
418 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
419
420 gint change_dir                 (const gchar    *dir);
421 gint make_dir                   (const gchar    *dir);
422 gint make_dir_hier              (const gchar    *dir);
423 gint remove_all_files           (const gchar    *dir);
424 gint remove_numbered_files      (const gchar    *dir,
425                                  guint           first,
426                                  guint           last);
427 gint remove_numbered_files_not_in_list(const gchar *dir,
428                                        GSList *numberlist);
429 gint remove_all_numbered_files  (const gchar    *dir);
430 gint remove_expired_files       (const gchar    *dir,
431                                  guint           hours);
432 gint remove_dir_recursive       (const gchar    *dir);
433 gint append_file                (const gchar    *src,
434                                  const gchar    *dest,
435                                  gboolean        keep_backup);
436 gint rename_force               (const gchar    *oldpath,
437                                  const gchar    *newpath);
438 gint copy_file                  (const gchar    *src,
439                                  const gchar    *dest,
440                                  gboolean        keep_backup);
441 gint move_file                  (const gchar    *src,
442                                  const gchar    *dest,
443                                  gboolean        overwrite);
444 gint copy_dir                   (const gchar    *src,
445                                  const gchar    *dest);
446 gint copy_file_part_to_fp       (FILE           *fp,
447                                  off_t           offset,
448                                  size_t          length,
449                                  FILE           *dest_fp);
450 gint copy_file_part             (FILE           *fp,
451                                  off_t           offset,
452                                  size_t          length,
453                                  const gchar    *dest);
454
455 gchar *canonicalize_str         (const gchar    *str);
456 gint canonicalize_file          (const gchar    *src,
457                                  const gchar    *dest);
458 gint canonicalize_file_replace  (const gchar    *file);
459 gint uncanonicalize_file        (const gchar    *src,
460                                  const gchar    *dest);
461 gint uncanonicalize_file_replace(const gchar    *file);
462
463 gchar *normalize_newlines       (const gchar    *str);
464
465 gchar *get_outgoing_rfc2822_str (FILE           *fp);
466
467 gint change_file_mode_rw        (FILE           *fp,
468                                  const gchar    *file);
469 FILE *my_tmpfile                (void);
470 FILE *get_tmpfile_in_dir        (const gchar    *dir,
471                                  gchar         **filename);
472 FILE *str_open_as_stream        (const gchar    *str);
473 gint str_write_to_file          (const gchar    *str,
474                                  const gchar    *file);
475 gchar *file_read_to_str         (const gchar    *file);
476 gchar *file_read_stream_to_str  (FILE           *fp);
477
478 char *fgets_crlf(char *buf, int size, FILE *stream);
479
480 /* process execution */
481 gint execute_command_line       (const gchar    *cmdline,
482                                  gboolean        async);
483 gchar *get_command_output       (const gchar    *cmdline);
484
485 /* open URI with external browser */
486 gint open_uri(const gchar *uri, const gchar *cmdline);
487 /* open file with text editor */
488 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
489
490 /* time functions */
491 time_t remote_tzoffset_sec      (const gchar    *zone);
492 time_t tzoffset_sec             (time_t         *now);
493 gchar *tzoffset                 (time_t         *now);
494 void get_rfc822_date            (gchar          *buf,
495                                  gint            len);
496
497 size_t my_strftime              (gchar                  *s,
498                                  size_t                  max,
499                                  const gchar            *format,
500                                  const struct tm        *tm);
501 size_t fast_strftime            (gchar                  *buf, 
502                                  gint                    buflen, 
503                                  const gchar            *format, 
504                                  struct tm              *lt);
505
506 /* debugging */
507 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
508 const char * debug_srcname (const char *file);
509
510 /* subject threading */
511 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
512 void subject_table_insert(GHashTable *subject_table, gchar * subject,
513                           void * data);
514 void subject_table_remove(GHashTable *subject_table, gchar * subject);
515 gint subject_get_prefix_length (const gchar *subject);
516
517 /* quoting recognition */
518 const gchar * line_has_quote_char       (const gchar *str,
519                                          const gchar *quote_chars);
520 const gchar * line_has_quote_char_last  (const gchar *str,
521                                          const gchar *quote_chars);
522
523 guint g_stricase_hash   (gconstpointer gptr);
524 gint g_stricase_equal   (gconstpointer gptr1, gconstpointer gptr2);
525 gint g_int_compare      (gconstpointer a, gconstpointer b);
526
527 gchar *generate_msgid           (gchar *buf, gint len);
528 gchar *generate_mime_boundary   (const gchar *prefix);
529
530 gint quote_cmd_argument(gchar * result, guint size,
531                         const gchar * path);
532 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
533
534 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
535 void get_hex_str(gchar *out, guchar ch);
536
537 /* auto pointer for containers that support GType system */
538
539 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
540 typedef struct AutoPointer      GAuto;
541 GType g_auto_pointer_register           (void);
542 GAuto *g_auto_pointer_new               (gpointer pointer);
543 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
544                                          GFreeFunc free);
545 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
546 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
547 void g_auto_pointer_free                (GAuto *auto_ptr);
548 void replace_returns                    (gchar *str);
549 gboolean get_uri_part   (const gchar *start,
550                          const gchar *scanpos,
551                          const gchar **bp,
552                          const gchar **ep,
553                          gboolean hdr);
554 gchar *make_uri_string  (const gchar *bp,
555                          const gchar *ep);
556 gboolean get_email_part (const gchar *start, 
557                          const gchar *scanpos,
558                          const gchar **bp, 
559                          const gchar **ep,
560                          gboolean hdr);
561 gchar *make_email_string(const gchar *bp,
562                          const gchar *ep);
563 gchar *make_http_string (const gchar *bp,
564                          const gchar *ep);
565
566 gchar *mailcap_get_command_for_type(const gchar *type, 
567                                     const gchar *file_to_open);
568 void mailcap_update_default        (const gchar *type,
569                                     const gchar *command);
570
571 gboolean file_is_email(const gchar *filename);
572 gboolean sc_g_list_bigger(GList *list, gint max);
573 gboolean sc_g_slist_bigger(GSList *list, gint max);
574 #ifdef __cplusplus
575 }
576 #endif
577
578 #endif /* __UTILS_H__ */