2008-05-24 [colin] 3.4.0cvs68
[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
228 gint str_case_equal             (gconstpointer   v,
229                                  gconstpointer   v2);
230 guint str_case_hash             (gconstpointer   key);
231
232 void ptr_array_free_strings     (GPtrArray      *array);
233
234 typedef gboolean (*StrFindFunc) (const gchar    *haystack,
235                                  const gchar    *needle);
236
237 gboolean str_find               (const gchar    *haystack,
238                                  const gchar    *needle);
239 gboolean str_case_find          (const gchar    *haystack,
240                                  const gchar    *needle);
241 /* number-string conversion */
242 gint to_number                  (const gchar *nstr);
243 gchar *itos_buf                 (gchar       *nstr,
244                                  gint         n);
245 gchar *itos                     (gint         n);
246 gchar *to_human_readable        (size_t       size);
247
248 /* alternative string functions */
249 gint strcmp2            (const gchar    *s1,
250                          const gchar    *s2);
251 gchar *strstr2          (const gchar    *s1,
252                          const gchar    *s2);
253 gint path_cmp           (const gchar    *s1,
254                          const gchar    *s2);
255 gchar *strretchomp      (gchar          *str);
256 gchar *strtailchomp     (gchar          *str,
257                          gchar           tail_char);
258 gchar *strcrchomp       (gchar          *str);
259 gint file_strip_crs     (const gchar    *file);
260 gchar *strcasestr       (const gchar    *haystack,
261                          const gchar    *needle);
262 gpointer my_memmem      (gconstpointer   haystack,
263                          size_t          haystacklen,
264                          gconstpointer   needle,
265                          size_t          needlelen);
266 gchar *strncpy2         (gchar          *dest,
267                          const gchar    *src,
268                          size_t          n);
269
270 gboolean is_next_nonascii       (const gchar *s);
271 gint get_next_word_len          (const gchar *s);
272
273 /* functions for string parsing */
274 gint subject_compare                    (const gchar    *s1,
275                                          const gchar    *s2);
276 gint subject_compare_for_sort           (const gchar    *s1,
277                                          const gchar    *s2);
278 void trim_subject                       (gchar          *str);
279 void eliminate_parenthesis              (gchar          *str,
280                                          gchar           op,
281                                          gchar           cl);
282 void extract_parenthesis                (gchar          *str,
283                                          gchar           op,
284                                          gchar           cl);
285
286 void extract_quote                      (gchar          *str,
287                                          gchar           quote_chr);
288 void eliminate_address_comment          (gchar          *str);
289 gchar *strchr_with_skip_quote           (const gchar    *str,
290                                          gint            quote_chr,
291                                          gint            c);
292 void extract_address                    (gchar          *str);
293 void extract_list_id_str                (gchar          *str);
294
295 GSList *address_list_append             (GSList         *addr_list,
296                                          const gchar    *str);
297 GSList *address_list_append_with_comments(GSList        *addr_list,
298                                          const gchar    *str);
299 GSList *references_list_prepend         (GSList         *msgid_list,
300                                          const gchar    *str);
301 GSList *references_list_append          (GSList         *msgid_list,
302                                          const gchar    *str);
303 GSList *newsgroup_list_append           (GSList         *group_list,
304                                          const gchar    *str);
305
306 GList *add_history                      (GList          *list,
307                                          const gchar    *str);
308
309 void remove_return                      (gchar          *str);
310 void remove_space                       (gchar          *str);
311 void unfold_line                        (gchar          *str);
312 void subst_char                         (gchar          *str,
313                                          gchar           orig,
314                                          gchar           subst);
315 void subst_chars                        (gchar          *str,   
316                                          gchar          *orig, 
317                                          gchar          subst);
318 void subst_for_filename                 (gchar          *str);
319 void subst_for_shellsafe_filename       (gchar          *str);
320 gboolean is_ascii_str                   (const gchar    *str);
321 gint get_quote_level                    (const gchar    *str,
322                                          const gchar    *quote_chars);
323 gint check_line_length                  (const gchar    *str,
324                                          gint            max_chars,
325                                          gint           *line);
326
327 gchar **strsplit_with_quote             (const gchar    *str,
328                                          const gchar    *delim,
329                                          gint            max_tokens);
330
331 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
332                                          gint            len);
333 gchar *trim_string                      (const gchar    *str,
334                                          gint            len);
335
336 GList *uri_list_extract_filenames       (const gchar    *uri_list);
337 gboolean is_uri_string                  (const gchar    *str);
338 gchar *get_uri_path                     (const gchar    *uri);
339 gint get_uri_len                        (const gchar    *str);
340 void decode_uri                         (gchar          *decoded_uri,
341                                          const gchar    *encoded_uri);
342 void decode_uri_with_plus               (gchar          *decoded_uri, 
343                                          const gchar    *encoded_uri, 
344                                          gboolean        with_plus);
345 gint scan_mailto_url                    (const gchar    *mailto,
346                                          gchar         **from,
347                                          gchar         **to,
348                                          gchar         **cc,
349                                          gchar         **bcc,
350                                          gchar         **subject,
351                                          gchar         **body,
352                                          gchar         ***attach);
353
354 /* return static strings */
355 const gchar *get_home_dir               (void);
356 const gchar *get_rc_dir                 (void);
357 void  set_rc_dir                        (const gchar *dir);
358 gboolean rc_dir_is_alt                  (void);
359 const gchar *get_mail_base_dir          (void);
360 const gchar *get_news_cache_dir         (void);
361 const gchar *get_imap_cache_dir         (void);
362 const gchar *get_mime_tmp_dir           (void);
363 const gchar *get_template_dir           (void);
364 const gchar *get_plugin_dir             (void);
365 const gchar *get_tmp_dir                (void);
366 const gchar *get_locale_dir             (void);
367 gchar *get_tmp_file                     (void);
368 const gchar *get_domain_name            (void);
369
370 /* file / directory handling */
371 off_t get_file_size             (const gchar    *file);
372 off_t get_file_size_as_crlf     (const gchar    *file);
373
374 time_t get_file_mtime           (const gchar *file);
375
376 gboolean file_exist             (const gchar    *file,
377                                  gboolean        allow_fifo);
378 gboolean is_relative_filename   (const gchar *file);
379 gboolean is_dir_exist           (const gchar    *dir);
380 gboolean is_file_entry_exist    (const gchar    *file);
381 gboolean dirent_is_regular_file (struct dirent  *d);
382
383 #define is_file_exist(file)             file_exist(file, FALSE)
384 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
385
386 gint change_dir                 (const gchar    *dir);
387 gint make_dir                   (const gchar    *dir);
388 gint make_dir_hier              (const gchar    *dir);
389 gint remove_all_files           (const gchar    *dir);
390 gint remove_numbered_files      (const gchar    *dir,
391                                  guint           first,
392                                  guint           last);
393 gint remove_numbered_files_not_in_list(const gchar *dir,
394                                        GSList *numberlist);
395 gint remove_all_numbered_files  (const gchar    *dir);
396 gint remove_dir_recursive       (const gchar    *dir);
397 gint append_file                (const gchar    *src,
398                                  const gchar    *dest,
399                                  gboolean        keep_backup);
400 gint rename_force               (const gchar    *oldpath,
401                                  const gchar    *newpath);
402 gint copy_file                  (const gchar    *src,
403                                  const gchar    *dest,
404                                  gboolean        keep_backup);
405 gint move_file                  (const gchar    *src,
406                                  const gchar    *dest,
407                                  gboolean        overwrite);
408 gint copy_dir                   (const gchar    *src,
409                                  const gchar    *dest);
410 gint copy_file_part_to_fp       (FILE           *fp,
411                                  off_t           offset,
412                                  size_t          length,
413                                  FILE           *dest_fp);
414 gint copy_file_part             (FILE           *fp,
415                                  off_t           offset,
416                                  size_t          length,
417                                  const gchar    *dest);
418
419 gchar *canonicalize_str         (const gchar    *str);
420 gint canonicalize_file          (const gchar    *src,
421                                  const gchar    *dest);
422 gint canonicalize_file_replace  (const gchar    *file);
423
424 gchar *normalize_newlines       (const gchar    *str);
425
426 gchar *get_outgoing_rfc2822_str (FILE           *fp);
427
428 gint change_file_mode_rw        (FILE           *fp,
429                                  const gchar    *file);
430 FILE *my_tmpfile                (void);
431 FILE *get_tmpfile_in_dir        (const gchar    *dir,
432                                  gchar         **filename);
433 FILE *str_open_as_stream        (const gchar    *str);
434 gint str_write_to_file          (const gchar    *str,
435                                  const gchar    *file);
436 gchar *file_read_to_str         (const gchar    *file);
437 gchar *file_read_stream_to_str  (FILE           *fp);
438 gchar *file_read_to_str_no_recode(const gchar *file);
439 gchar *file_read_stream_to_str_no_recode(FILE *fp);
440
441 char *fgets_crlf(char *buf, int size, FILE *stream);
442
443 /* process execution */
444 gint execute_command_line       (const gchar    *cmdline,
445                                  gboolean        async);
446 gchar *get_command_output       (const gchar    *cmdline);
447
448 /* open URI with external browser */
449 gint open_uri(const gchar *uri, const gchar *cmdline);
450 /* open file with text editor */
451 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
452
453 /* time functions */
454 time_t remote_tzoffset_sec      (const gchar    *zone);
455 time_t tzoffset_sec             (time_t         *now);
456 gchar *tzoffset                 (time_t         *now);
457 void get_rfc822_date            (gchar          *buf,
458                                  gint            len);
459
460 size_t fast_strftime            (gchar                  *buf, 
461                                  gint                    buflen, 
462                                  const gchar            *format, 
463                                  struct tm              *lt);
464
465 /* debugging */
466 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
467 const char * debug_srcname (const char *file);
468
469 /* subject threading */
470 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
471 void subject_table_insert(GHashTable *subject_table, gchar * subject,
472                           void * data);
473 void subject_table_remove(GHashTable *subject_table, gchar * subject);
474 void utils_free_regex(void);
475 gint subject_get_prefix_length (const gchar *subject);
476
477 /* quoting recognition */
478 const gchar * line_has_quote_char       (const gchar *str,
479                                          const gchar *quote_chars);
480
481 gint g_int_compare      (gconstpointer a, gconstpointer b);
482
483 gchar *generate_msgid           (gchar *buf, gint len);
484 gchar *generate_mime_boundary   (const gchar *prefix);
485
486 gint quote_cmd_argument(gchar * result, guint size,
487                         const gchar * path);
488 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
489
490 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
491 void get_hex_str(gchar *out, guchar ch);
492
493 /* auto pointer for containers that support GType system */
494
495 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
496 typedef struct AutoPointer      GAuto;
497 GType g_auto_pointer_register           (void);
498 GAuto *g_auto_pointer_new               (gpointer pointer);
499 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
500                                          GFreeFunc free);
501 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
502 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
503 void g_auto_pointer_free                (GAuto *auto_ptr);
504 void replace_returns                    (gchar *str);
505 gboolean get_uri_part   (const gchar *start,
506                          const gchar *scanpos,
507                          const gchar **bp,
508                          const gchar **ep,
509                          gboolean hdr);
510 gchar *make_uri_string  (const gchar *bp,
511                          const gchar *ep);
512 gboolean get_email_part (const gchar *start, 
513                          const gchar *scanpos,
514                          const gchar **bp, 
515                          const gchar **ep,
516                          gboolean hdr);
517 gchar *make_email_string(const gchar *bp,
518                          const gchar *ep);
519 gchar *make_http_string (const gchar *bp,
520                          const gchar *ep);
521
522 gchar *mailcap_get_command_for_type(const gchar *type, 
523                                     const gchar *file_to_open);
524 void mailcap_update_default        (const gchar *type,
525                                     const gchar *command);
526
527 gboolean file_is_email(const gchar *filename);
528 gboolean sc_g_list_bigger(GList *list, gint max);
529 gboolean sc_g_slist_bigger(GSList *list, gint max);
530
531 int claws_unlink(const gchar *filename);
532 #ifdef __cplusplus
533 }
534 #endif
535
536 #endif /* __UTILS_H__ */