7689acc0c7ad9e4be69f01750e99eb92c2945742
[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        (off_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_for_filename                 (gchar          *str);
316 void subst_for_shellsafe_filename       (gchar          *str);
317 gboolean is_ascii_str                   (const gchar    *str);
318 gint get_quote_level                    (const gchar    *str,
319                                          const gchar    *quote_chars);
320 gint check_line_length                  (const gchar    *str,
321                                          gint            max_chars,
322                                          gint           *line);
323
324 gchar **strsplit_with_quote             (const gchar    *str,
325                                          const gchar    *delim,
326                                          gint            max_tokens);
327
328 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
329                                          gint            len);
330 gchar *trim_string                      (const gchar    *str,
331                                          gint            len);
332
333 GList *uri_list_extract_filenames       (const gchar    *uri_list);
334 gboolean is_uri_string                  (const gchar    *str);
335 gchar *get_uri_path                     (const gchar    *uri);
336 gint get_uri_len                        (const gchar    *str);
337 void decode_uri                         (gchar          *decoded_uri,
338                                          const gchar    *encoded_uri);
339 void decode_uri_with_plus               (gchar          *decoded_uri, 
340                                          const gchar    *encoded_uri, 
341                                          gboolean        with_plus);
342 gint scan_mailto_url                    (const gchar    *mailto,
343                                          gchar         **to,
344                                          gchar         **cc,
345                                          gchar         **bcc,
346                                          gchar         **subject,
347                                          gchar         **body,
348                                          gchar         **attach);
349
350 /* return static strings */
351 const gchar *get_home_dir               (void);
352 const gchar *get_rc_dir                 (void);
353 void  set_rc_dir                        (const gchar *dir);
354 gboolean rc_dir_is_alt                  (void);
355 const gchar *get_mail_base_dir          (void);
356 const gchar *get_news_cache_dir         (void);
357 const gchar *get_imap_cache_dir         (void);
358 const gchar *get_mime_tmp_dir           (void);
359 const gchar *get_template_dir           (void);
360 const gchar *get_plugin_dir             (void);
361 const gchar *get_tmp_dir                (void);
362 const gchar *get_locale_dir             (void);
363 gchar *get_tmp_file                     (void);
364 const gchar *get_domain_name            (void);
365
366 /* file / directory handling */
367 off_t get_file_size             (const gchar    *file);
368 off_t get_file_size_as_crlf     (const gchar    *file);
369
370 time_t get_file_mtime           (const gchar *file);
371
372 gboolean file_exist             (const gchar    *file,
373                                  gboolean        allow_fifo);
374 gboolean is_relative_filename   (const gchar *file);
375 gboolean is_dir_exist           (const gchar    *dir);
376 gboolean is_file_entry_exist    (const gchar    *file);
377 gboolean dirent_is_regular_file (struct dirent  *d);
378
379 #define is_file_exist(file)             file_exist(file, FALSE)
380 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
381
382 gint change_dir                 (const gchar    *dir);
383 gint make_dir                   (const gchar    *dir);
384 gint make_dir_hier              (const gchar    *dir);
385 gint remove_all_files           (const gchar    *dir);
386 gint remove_numbered_files      (const gchar    *dir,
387                                  guint           first,
388                                  guint           last);
389 gint remove_numbered_files_not_in_list(const gchar *dir,
390                                        GSList *numberlist);
391 gint remove_all_numbered_files  (const gchar    *dir);
392 gint remove_dir_recursive       (const gchar    *dir);
393 gint append_file                (const gchar    *src,
394                                  const gchar    *dest,
395                                  gboolean        keep_backup);
396 gint rename_force               (const gchar    *oldpath,
397                                  const gchar    *newpath);
398 gint copy_file                  (const gchar    *src,
399                                  const gchar    *dest,
400                                  gboolean        keep_backup);
401 gint move_file                  (const gchar    *src,
402                                  const gchar    *dest,
403                                  gboolean        overwrite);
404 gint copy_dir                   (const gchar    *src,
405                                  const gchar    *dest);
406 gint copy_file_part_to_fp       (FILE           *fp,
407                                  off_t           offset,
408                                  size_t          length,
409                                  FILE           *dest_fp);
410 gint copy_file_part             (FILE           *fp,
411                                  off_t           offset,
412                                  size_t          length,
413                                  const gchar    *dest);
414
415 gchar *canonicalize_str         (const gchar    *str);
416 gint canonicalize_file          (const gchar    *src,
417                                  const gchar    *dest);
418 gint canonicalize_file_replace  (const gchar    *file);
419
420 gchar *normalize_newlines       (const gchar    *str);
421
422 gchar *get_outgoing_rfc2822_str (FILE           *fp);
423
424 gint change_file_mode_rw        (FILE           *fp,
425                                  const gchar    *file);
426 FILE *my_tmpfile                (void);
427 FILE *get_tmpfile_in_dir        (const gchar    *dir,
428                                  gchar         **filename);
429 FILE *str_open_as_stream        (const gchar    *str);
430 gint str_write_to_file          (const gchar    *str,
431                                  const gchar    *file);
432 gchar *file_read_to_str         (const gchar    *file);
433 gchar *file_read_stream_to_str  (FILE           *fp);
434
435 char *fgets_crlf(char *buf, int size, FILE *stream);
436
437 /* process execution */
438 gint execute_command_line       (const gchar    *cmdline,
439                                  gboolean        async);
440 gchar *get_command_output       (const gchar    *cmdline);
441
442 /* open URI with external browser */
443 gint open_uri(const gchar *uri, const gchar *cmdline);
444 /* open file with text editor */
445 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
446
447 /* time functions */
448 time_t remote_tzoffset_sec      (const gchar    *zone);
449 time_t tzoffset_sec             (time_t         *now);
450 gchar *tzoffset                 (time_t         *now);
451 void get_rfc822_date            (gchar          *buf,
452                                  gint            len);
453
454 size_t fast_strftime            (gchar                  *buf, 
455                                  gint                    buflen, 
456                                  const gchar            *format, 
457                                  struct tm              *lt);
458
459 /* debugging */
460 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
461 const char * debug_srcname (const char *file);
462
463 /* subject threading */
464 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
465 void subject_table_insert(GHashTable *subject_table, gchar * subject,
466                           void * data);
467 void subject_table_remove(GHashTable *subject_table, gchar * subject);
468 gint subject_get_prefix_length (const gchar *subject);
469
470 /* quoting recognition */
471 const gchar * line_has_quote_char       (const gchar *str,
472                                          const gchar *quote_chars);
473
474 gint g_int_compare      (gconstpointer a, gconstpointer b);
475
476 gchar *generate_msgid           (gchar *buf, gint len);
477 gchar *generate_mime_boundary   (const gchar *prefix);
478
479 gint quote_cmd_argument(gchar * result, guint size,
480                         const gchar * path);
481 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
482
483 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
484 void get_hex_str(gchar *out, guchar ch);
485
486 /* auto pointer for containers that support GType system */
487
488 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
489 typedef struct AutoPointer      GAuto;
490 GType g_auto_pointer_register           (void);
491 GAuto *g_auto_pointer_new               (gpointer pointer);
492 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
493                                          GFreeFunc free);
494 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
495 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
496 void g_auto_pointer_free                (GAuto *auto_ptr);
497 void replace_returns                    (gchar *str);
498 gboolean get_uri_part   (const gchar *start,
499                          const gchar *scanpos,
500                          const gchar **bp,
501                          const gchar **ep,
502                          gboolean hdr);
503 gchar *make_uri_string  (const gchar *bp,
504                          const gchar *ep);
505 gboolean get_email_part (const gchar *start, 
506                          const gchar *scanpos,
507                          const gchar **bp, 
508                          const gchar **ep,
509                          gboolean hdr);
510 gchar *make_email_string(const gchar *bp,
511                          const gchar *ep);
512 gchar *make_http_string (const gchar *bp,
513                          const gchar *ep);
514
515 gchar *mailcap_get_command_for_type(const gchar *type, 
516                                     const gchar *file_to_open);
517 void mailcap_update_default        (const gchar *type,
518                                     const gchar *command);
519
520 gboolean file_is_email(const gchar *filename);
521 gboolean sc_g_list_bigger(GList *list, gint max);
522 gboolean sc_g_slist_bigger(GSList *list, gint max);
523 #ifdef __cplusplus
524 }
525 #endif
526
527 #endif /* __UTILS_H__ */