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