b585cba91799c8ac1f2826256fbf15e3ec35c511
[claws.git] / src / common / utils.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 /* The AC_CHECK_SIZEOF() in configure fails for some machines.
44  * we provide some fallback values here */
45 #if !SIZEOF_UNSIGNED_SHORT
46   #undef SIZEOF_UNSIGNED_SHORT
47   #define SIZEOF_UNSIGNED_SHORT 2
48 #endif
49 #if !SIZEOF_UNSIGNED_INT
50   #undef SIZEOF_UNSIGNED_INT
51   #define SIZEOF_UNSIGNED_INT 4
52 #endif
53 #if !SIZEOF_UNSIGNED_LONG
54   #undef SIZEOF_UNSIGNED_LONG
55   #define SIZEOF_UNSIGNED_LONG 4
56 #endif
57
58 #ifndef HAVE_U32_TYPEDEF
59   #undef u32        /* maybe there is a macro with this name */
60   typedef guint32 u32;
61   #define HAVE_U32_TYPEDEF
62 #endif
63
64 #ifndef BIG_ENDIAN_HOST
65   #if (G_BYTE_ORDER == G_BIG_ENDIAN)
66     #define BIG_ENDIAN_HOST 1
67   #endif
68 #endif
69
70 #define CHDIR_RETURN_IF_FAIL(dir) \
71 { \
72         if (change_dir(dir) < 0) return; \
73 }
74
75 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
76 { \
77         if (change_dir(dir) < 0) return val; \
78 }
79
80 #define Xalloca(ptr, size, iffail) \
81 { \
82         if ((ptr = alloca(size)) == NULL) { \
83                 g_warning("can't allocate memory\n"); \
84                 iffail; \
85         } \
86 }
87
88 #define Xstrdup_a(ptr, str, iffail) \
89 { \
90         gchar *__tmp; \
91  \
92         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
93                 g_warning("can't allocate memory\n"); \
94                 iffail; \
95         } else \
96                 strcpy(__tmp, str); \
97  \
98         ptr = __tmp; \
99 }
100
101 #define Xstrndup_a(ptr, str, len, iffail) \
102 { \
103         gchar *__tmp; \
104  \
105         if ((__tmp = alloca(len + 1)) == NULL) { \
106                 g_warning("can't allocate memory\n"); \
107                 iffail; \
108         } else { \
109                 strncpy(__tmp, str, len); \
110                 __tmp[len] = '\0'; \
111         } \
112  \
113         ptr = __tmp; \
114 }
115
116 #define Xstrcat_a(ptr, str1, str2, iffail) \
117 { \
118         gchar *__tmp; \
119         gint len1, len2; \
120  \
121         len1 = strlen(str1); \
122         len2 = strlen(str2); \
123         if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
124                 g_warning("can't allocate memory\n"); \
125                 iffail; \
126         } else { \
127                 memcpy(__tmp, str1, len1); \
128                 memcpy(__tmp + len1, str2, len2 + 1); \
129         } \
130  \
131         ptr = __tmp; \
132 }
133
134 #define AUTORELEASE_STR(str, iffail) \
135 { \
136         gchar *__str; \
137         Xstrdup_a(__str, str, iffail); \
138         g_free(str); \
139         str = __str; \
140 }
141
142 #define FILE_OP_ERROR(file, func) \
143 { \
144         fprintf(stderr, "%s: ", file); \
145         perror(func); \
146 }
147
148 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
149
150 #ifdef __cplusplus
151 extern "C" {
152 #endif
153
154 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
155
156 /* debug functions */
157 void debug_set_mode             (gboolean mode);
158 gboolean debug_get_mode         (void);
159 #define debug_print \
160         debug_print_real(__FILE__ ":%d:", __LINE__), \
161         debug_print_real
162
163 /* for macro expansion */
164 #define Str(x)  #x
165 #define Xstr(x) Str(x)
166
167 void list_free_strings          (GList          *list);
168 void slist_free_strings         (GSList         *list);
169
170 void hash_free_strings          (GHashTable     *table);
171 void hash_free_value_mem        (GHashTable     *table);
172
173 gint str_case_equal             (gconstpointer   v,
174                                  gconstpointer   v2);
175 guint str_case_hash             (gconstpointer   key);
176
177 void ptr_array_free_strings     (GPtrArray      *array);
178
179 typedef gboolean (*StrFindFunc) (const gchar    *haystack,
180                                  const gchar    *needle);
181
182 gboolean str_find               (const gchar    *haystack,
183                                  const gchar    *needle);
184 gboolean str_case_find          (const gchar    *haystack,
185                                  const gchar    *needle);
186 gboolean str_find_equal         (const gchar    *haystack,
187                                  const gchar    *needle);
188 gboolean str_case_find_equal    (const gchar    *haystack,
189                                  const gchar    *needle);
190
191 /* number-string conversion */
192 gint to_number                  (const gchar *nstr);
193 gchar *itos_buf                 (gchar       *nstr,
194                                  gint         n);
195 gchar *itos                     (gint         n);
196 gchar *to_human_readable        (off_t        size);
197
198 /* alternative string functions */
199 gint strcmp2            (const gchar    *s1,
200                          const gchar    *s2);
201 gchar *strstr2          (const gchar    *s1,
202                          const gchar    *s2);
203 gint path_cmp           (const gchar    *s1,
204                          const gchar    *s2);
205 gchar *strretchomp      (gchar          *str);
206 gchar *strtailchomp     (gchar          *str,
207                          gchar           tail_char);
208 gchar *strcrchomp       (gchar          *str);
209 gchar *strcasestr       (const gchar    *haystack,
210                          const gchar    *needle);
211 gpointer my_memmem      (gconstpointer   haystack,
212                          size_t          haystacklen,
213                          gconstpointer   needle,
214                          size_t          needlelen);
215 gchar *strncpy2         (gchar          *dest,
216                          const gchar    *src,
217                          size_t          n);
218
219 /* wide-character functions */
220 #if !HAVE_ISWALNUM
221 int iswalnum            (wint_t wc);
222 #endif
223 #if !HAVE_ISWSPACE
224 int iswspace            (wint_t wc);
225 #endif
226 #if !HAVE_TOWLOWER
227 wint_t towlower         (wint_t wc);
228 #endif
229
230 #if !HAVE_WCSLEN
231 size_t wcslen           (const wchar_t *s);
232 #endif
233 #if !HAVE_WCSCPY
234 wchar_t *wcscpy         (wchar_t       *dest,
235                          const wchar_t *src);
236 #endif
237 #if !HAVE_WCSNCPY
238 wchar_t *wcsncpy        (wchar_t       *dest,
239                          const wchar_t *src,
240                          size_t         n);
241 #endif
242
243 wchar_t *wcsdup                 (const wchar_t *s);
244 wchar_t *wcsndup                (const wchar_t *s,
245                                  size_t         n);
246 wchar_t *strdup_mbstowcs        (const gchar   *s);
247 gchar *strdup_wcstombs          (const wchar_t *s);
248 gint wcsncasecmp                (const wchar_t *s1,
249                                  const wchar_t *s2,
250                                  size_t         n);
251 wchar_t *wcscasestr             (const wchar_t *haystack,
252                                  const wchar_t *needle);
253 gint get_mbs_len                (const gchar    *s);
254
255 gboolean is_next_nonascii       (const guchar *s);
256 gint get_next_word_len          (const guchar *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_for_compare           (gchar          *str);
264 void trim_subject_for_sort              (gchar          *str);
265 void trim_subject                       (gchar          *str);
266 void eliminate_parenthesis              (gchar          *str,
267                                          gchar           op,
268                                          gchar           cl);
269 void extract_parenthesis                (gchar          *str,
270                                          gchar           op,
271                                          gchar           cl);
272
273 void extract_parenthesis_with_skip_quote        (gchar          *str,
274                                                  gchar           quote_chr,
275                                                  gchar           op,
276                                                  gchar           cl);
277
278 void eliminate_quote                    (gchar          *str,
279                                          gchar           quote_chr);
280 void extract_quote                      (gchar          *str,
281                                          gchar           quote_chr);
282 void eliminate_address_comment          (gchar          *str);
283 gchar *strchr_with_skip_quote           (const gchar    *str,
284                                          gint            quote_chr,
285                                          gint            c);
286 gchar *strrchr_with_skip_quote          (const gchar    *str,
287                                          gint            quote_chr,
288                                          gint            c);
289 void extract_address                    (gchar          *str);
290 void extract_list_id_str                (gchar          *str);
291
292 GSList *slist_concat_unique             (GSList         *first,
293                                          GSList         *second);
294 GSList *address_list_append             (GSList         *addr_list,
295                                          const gchar    *str);
296 GSList *address_list_append_with_comments(GSList        *addr_list,
297                                          const gchar    *str);
298 GSList *references_list_prepend         (GSList         *msgid_list,
299                                          const gchar    *str);
300 GSList *references_list_append          (GSList         *msgid_list,
301                                          const gchar    *str);
302 GSList *newsgroup_list_append           (GSList         *group_list,
303                                          const gchar    *str);
304
305 GList *add_history                      (GList          *list,
306                                          const gchar    *str);
307
308 void remove_return                      (gchar          *str);
309 void remove_space                       (gchar          *str);
310 void unfold_line                        (gchar          *str);
311 void subst_char                         (gchar          *str,
312                                          gchar           orig,
313                                          gchar           subst);
314 void subst_chars                        (gchar          *str,
315                                          gchar          *orig,
316                                          gchar           subst);
317 void subst_for_filename                 (gchar          *str);
318 void subst_for_shellsafe_filename       (gchar          *str);
319 gboolean is_header_line                 (const gchar    *str);
320 gboolean is_ascii_str                   (const guchar   *str);
321 gint get_quote_level                    (const gchar    *str,
322                                          const gchar    *quote_chars);
323 gchar *strstr_with_skip_quote           (const gchar    *haystack,
324                                          const gchar    *needle);
325 gchar *strchr_parenthesis_close         (const gchar    *str,
326                                          gchar           op,
327                                          gchar           cl);
328
329 gchar **strsplit_parenthesis            (const gchar    *str,
330                                          gchar           op,
331                                          gchar           cl,
332                                          gint            max_tokens);
333 gchar **strsplit_with_quote             (const gchar    *str,
334                                          const gchar    *delim,
335                                          gint            max_tokens);
336
337 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
338                                          gint            len);
339 gchar *trim_string                      (const gchar    *str,
340                                          gint            len);
341
342 GList *uri_list_extract_filenames       (const gchar    *uri_list);
343 gboolean is_uri_string                  (const gchar    *str);
344 gchar *get_uri_path                     (const gchar    *uri);
345 gint get_uri_len                        (const gchar    *str);
346 void decode_uri                         (gchar          *decoded_uri,
347                                          const gchar    *encoded_uri);
348 gint scan_mailto_url                    (const gchar    *mailto,
349                                          gchar         **to,
350                                          gchar         **cc,
351                                          gchar         **bcc,
352                                          gchar         **subject,
353                                          gchar         **body);
354
355 /* return static strings */
356 const gchar *get_home_dir               (void);
357 const gchar *get_rc_dir                 (void);
358 const gchar *get_news_cache_dir         (void);
359 const gchar *get_imap_cache_dir         (void);
360 const gchar *get_mbox_cache_dir         (void);
361 const gchar *get_mime_tmp_dir           (void);
362 const gchar *get_template_dir           (void);
363 const gchar *get_header_cache_dir       (void);
364 const gchar *get_tmp_dir                (void);
365 gchar *get_tmp_file                     (void);
366 const gchar *get_domain_name            (void);
367
368 /* file / directory handling */
369 off_t get_file_size             (const gchar    *file);
370 off_t get_file_size_as_crlf     (const gchar    *file);
371 off_t get_left_file_size        (FILE           *fp);
372
373 gboolean file_exist             (const gchar    *file,
374                                  gboolean        allow_fifo);
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 gboolean dirent_is_directory    (struct dirent  *d);
379
380 #define is_file_exist(file)             file_exist(file, FALSE)
381 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
382
383 gint change_dir                 (const gchar    *dir);
384 gint make_dir                   (const gchar    *dir);
385 gint make_dir_hier              (const gchar    *dir);
386 gint remove_all_files           (const gchar    *dir);
387 gint remove_numbered_files      (const gchar    *dir,
388                                  guint           first,
389                                  guint           last);
390 gint remove_numbered_files_not_in_list(const gchar *dir,
391                                        GSList *numberlist);
392 gint remove_all_numbered_files  (const gchar    *dir);
393 gint remove_expired_files       (const gchar    *dir,
394                                  guint           hours);
395 gint remove_dir_recursive       (const gchar    *dir);
396 gint append_file                (const gchar    *src,
397                                  const gchar    *dest,
398                                  gboolean        keep_backup);
399 gint copy_file                  (const gchar    *src,
400                                  const gchar    *dest,
401                                  gboolean        keep_backup);
402 gint move_file                  (const gchar    *src,
403                                  const gchar    *dest,
404                                  gboolean        overwrite);
405 gint copy_file_part_to_fp       (FILE           *fp,
406                                  off_t           offset,
407                                  size_t          length,
408                                  FILE           *dest_fp);
409 gint copy_file_part             (FILE           *fp,
410                                  off_t           offset,
411                                  size_t          length,
412                                  const gchar    *dest);
413
414 gchar *canonicalize_str         (const gchar    *str);
415 gint canonicalize_file          (const gchar    *src,
416                                  const gchar    *dest);
417 gint canonicalize_file_replace  (const gchar    *file);
418 gint uncanonicalize_file        (const gchar    *src,
419                                  const gchar    *dest);
420 gint uncanonicalize_file_replace(const gchar    *file);
421
422 gchar *normalize_newlines       (const gchar    *str);
423
424 gchar *get_outgoing_rfc2822_str (FILE           *fp);
425
426 gint change_file_mode_rw        (FILE           *fp,
427                                  const gchar    *file);
428 FILE *my_tmpfile                (void);
429 FILE *get_tmpfile_in_dir        (const gchar    *dir,
430                                  gchar         **filename);
431 FILE *str_open_as_stream        (const gchar    *str);
432 gint str_write_to_file          (const gchar    *str,
433                                  const gchar    *file);
434 gchar *file_read_to_str         (const gchar    *file);
435 gchar *file_read_stream_to_str  (FILE           *fp);
436
437 /* process execution */
438 gint execute_async              (gchar *const    argv[]);
439 gint execute_sync               (gchar *const    argv[]);
440 gint execute_command_line       (const gchar    *cmdline,
441                                  gboolean        async);
442 gchar *get_command_output       (const gchar    *cmdline);
443
444 /* open URI with external browser */
445 gint open_uri(const gchar *uri, 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 /* debugging */
455 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
456
457 /* subject threading */
458 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
459 void subject_table_insert(GHashTable *subject_table, gchar * subject,
460                           void * data);
461 void subject_table_remove(GHashTable *subject_table, gchar * subject);
462 gint subject_get_prefix_length (const gchar *subject);
463
464 /* quoting recognition */
465 const gchar * line_has_quote_char       (const gchar *str,
466                                          const gchar *quote_chars);
467 const gchar * line_has_quote_char_last  (const gchar *str,
468                                          const gchar *quote_chars);
469
470 guint g_stricase_hash   (gconstpointer gptr);
471 gint g_stricase_equal   (gconstpointer gptr1, gconstpointer gptr2);
472 gint g_int_compare      (gconstpointer a, gconstpointer b);
473
474 gchar *generate_msgid           (gchar *buf, gint len);
475 gchar *generate_mime_boundary   (const gchar *prefix);
476
477 gint quote_cmd_argument(gchar * result, guint size,
478                         const gchar * path);
479 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
480
481 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
482 void get_hex_str(gchar *out, guchar ch);
483
484 /* auto pointer for containers that support GType system */
485
486 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
487 typedef struct AutoPointer      GAuto;
488 GType g_auto_pointer_register           (void);
489 GAuto *g_auto_pointer_new               (gpointer pointer);
490 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
491                                          GFreeFunc free);
492 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
493 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
494 void g_auto_pointer_free                (GAuto *auto_ptr);
495
496 #ifdef __cplusplus
497 }
498 #endif
499
500 #endif /* __UTILS_H__ */