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