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