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