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