Sync with HEAD 0.9.12cvs46
[claws.git] / src / common / utils.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 #ifdef __cplusplus
148 extern "C" {
149 #endif
150
151 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
152
153 /* debug functions */
154 void debug_set_mode             (gboolean mode);
155 gboolean debug_get_mode         (void);
156 #define debug_print \
157         debug_print_real(__FILE__ ":%d:", __LINE__), \
158         debug_print_real
159
160 /* for macro expansion */
161 #define Str(x)  #x
162 #define Xstr(x) Str(x)
163
164 void list_free_strings          (GList          *list);
165 void slist_free_strings         (GSList         *list);
166
167 void hash_free_strings          (GHashTable     *table);
168 void hash_free_value_mem        (GHashTable     *table);
169
170 gint str_case_equal             (gconstpointer   v,
171                                  gconstpointer   v2);
172 guint str_case_hash             (gconstpointer   key);
173
174 void ptr_array_free_strings     (GPtrArray      *array);
175
176 /* number-string conversion */
177 gint to_number                  (const gchar *nstr);
178 gchar *itos_buf                 (gchar       *nstr,
179                                  gint         n);
180 gchar *itos                     (gint         n);
181 gchar *to_human_readable        (off_t        size);
182
183 /* alternative string functions */
184 gint strcmp2            (const gchar    *s1,
185                          const gchar    *s2);
186 gchar *strstr2          (const gchar    *s1,
187                          const gchar    *s2);
188 gint path_cmp           (const gchar    *s1,
189                          const gchar    *s2);
190 gchar *strretchomp      (gchar          *str);
191 gchar *strtailchomp     (gchar          *str,
192                          gchar           tail_char);
193 gchar *strcrchomp       (gchar          *str);
194 gchar *strcasestr       (const gchar    *haystack,
195                          const gchar    *needle);
196 gchar *strncpy2         (gchar          *dest,
197                          const gchar    *src,
198                          size_t          n);
199
200 /* wide-character functions */
201 #if !HAVE_ISWALNUM
202 int iswalnum            (wint_t wc);
203 #endif
204 #if !HAVE_ISWSPACE
205 int iswspace            (wint_t wc);
206 #endif
207 #if !HAVE_TOWLOWER
208 wint_t towlower         (wint_t wc);
209 #endif
210
211 #if !HAVE_WCSLEN
212 size_t wcslen           (const wchar_t *s);
213 #endif
214 #if !HAVE_WCSCPY
215 wchar_t *wcscpy         (wchar_t       *dest,
216                          const wchar_t *src);
217 #endif
218 #if !HAVE_WCSNCPY
219 wchar_t *wcsncpy        (wchar_t       *dest,
220                          const wchar_t *src,
221                          size_t         n);
222 #endif
223
224 wchar_t *wcsdup                 (const wchar_t *s);
225 wchar_t *wcsndup                (const wchar_t *s,
226                                  size_t         n);
227 wchar_t *strdup_mbstowcs        (const gchar   *s);
228 gchar *strdup_wcstombs          (const wchar_t *s);
229 gint wcsncasecmp                (const wchar_t *s1,
230                                  const wchar_t *s2,
231                                  size_t         n);
232 wchar_t *wcscasestr             (const wchar_t *haystack,
233                                  const wchar_t *needle);
234 gint get_mbs_len                (const gchar    *s);
235
236 gboolean is_next_nonascii       (const guchar *s);
237 gint get_next_word_len          (const guchar *s);
238
239 /* functions for string parsing */
240 gint subject_compare                    (const gchar    *s1,
241                                          const gchar    *s2);
242 gint subject_compare_for_sort           (const gchar    *s1,
243                                          const gchar    *s2);
244 void trim_subject_for_compare           (gchar          *str);
245 void trim_subject_for_sort              (gchar          *str);
246 void trim_subject                       (gchar          *str);
247 void eliminate_parenthesis              (gchar          *str,
248                                          gchar           op,
249                                          gchar           cl);
250 void extract_parenthesis                (gchar          *str,
251                                          gchar           op,
252                                          gchar           cl);
253
254 void extract_parenthesis_with_skip_quote        (gchar          *str,
255                                                  gchar           quote_chr,
256                                                  gchar           op,
257                                                  gchar           cl);
258
259 void eliminate_quote                    (gchar          *str,
260                                          gchar           quote_chr);
261 void extract_quote                      (gchar          *str,
262                                          gchar           quote_chr);
263 void eliminate_address_comment          (gchar          *str);
264 gchar *strchr_with_skip_quote           (const gchar    *str,
265                                          gint            quote_chr,
266                                          gint            c);
267 gchar *strrchr_with_skip_quote          (const gchar    *str,
268                                          gint            quote_chr,
269                                          gint            c);
270 void extract_address                    (gchar          *str);
271 void extract_list_id_str                (gchar          *str);
272
273 GSList *slist_concat_unique             (GSList         *first,
274                                          GSList         *second);
275 GSList *address_list_append             (GSList         *addr_list,
276                                          const gchar    *str);
277 GSList *address_list_append_with_comments(GSList        *addr_list,
278                                          const gchar    *str);
279 GSList *references_list_append          (GSList         *msgid_list,
280                                          const gchar    *str);
281 GSList *newsgroup_list_append           (GSList         *group_list,
282                                          const gchar    *str);
283
284 GList *add_history                      (GList          *list,
285                                          const gchar    *str);
286
287 void remove_return                      (gchar          *str);
288 void remove_space                       (gchar          *str);
289 void unfold_line                        (gchar          *str);
290 void subst_char                         (gchar          *str,
291                                          gchar           orig,
292                                          gchar           subst);
293 void subst_chars                        (gchar          *str,
294                                          gchar          *orig,
295                                          gchar           subst);
296 void subst_for_filename                 (gchar          *str);
297 void subst_for_shellsafe_filename       (gchar          *str);
298 gboolean is_header_line                 (const gchar    *str);
299 gboolean is_ascii_str                   (const guchar   *str);
300 gint get_quote_level                    (const gchar    *str,
301                                          const gchar    *quote_chars);
302 gchar *strstr_with_skip_quote           (const gchar    *haystack,
303                                          const gchar    *needle);
304 gchar *strchr_parenthesis_close         (const gchar    *str,
305                                          gchar           op,
306                                          gchar           cl);
307
308 gchar **strsplit_parenthesis            (const gchar    *str,
309                                          gchar           op,
310                                          gchar           cl,
311                                          gint            max_tokens);
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 void decode_uri                         (gchar          *decoded_uri,
325                                          const gchar    *encoded_uri);
326 gint scan_mailto_url                    (const gchar    *mailto,
327                                          gchar         **to,
328                                          gchar         **cc,
329                                          gchar         **bcc,
330                                          gchar         **subject,
331                                          gchar         **body);
332
333 /* return static strings */
334 const gchar *get_home_dir               (void);
335 const gchar *get_rc_dir                 (void);
336 const gchar *get_news_cache_dir         (void);
337 const gchar *get_imap_cache_dir         (void);
338 const gchar *get_mbox_cache_dir         (void);
339 const gchar *get_mime_tmp_dir           (void);
340 const gchar *get_template_dir           (void);
341 const gchar *get_header_cache_dir       (void);
342 const gchar *get_tmp_dir                (void);
343 gchar *get_tmp_file                     (void);
344 const gchar *get_domain_name            (void);
345
346 /* file / directory handling */
347 off_t get_file_size             (const gchar    *file);
348 off_t get_file_size_as_crlf     (const gchar    *file);
349 off_t get_left_file_size        (FILE           *fp);
350
351 gboolean file_exist             (const gchar    *file,
352                                  gboolean        allow_fifo);
353 gboolean is_dir_exist           (const gchar    *dir);
354 gboolean is_file_entry_exist    (const gchar    *file);
355 gboolean dirent_is_regular_file (struct dirent  *d);
356 gboolean dirent_is_directory    (struct dirent  *d);
357
358 #define is_file_exist(file)             file_exist(file, FALSE)
359 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
360
361 gint change_dir                 (const gchar    *dir);
362 gint make_dir                   (const gchar    *dir);
363 gint make_dir_hier              (const gchar    *dir);
364 gint remove_all_files           (const gchar    *dir);
365 gint remove_numbered_files      (const gchar    *dir,
366                                  guint           first,
367                                  guint           last);
368 gint remove_numbered_files_not_in_list(const gchar *dir,
369                                        GSList *numberlist);
370 gint remove_all_numbered_files  (const gchar    *dir);
371 gint remove_expired_files       (const gchar    *dir,
372                                  guint           hours);
373 gint remove_dir_recursive       (const gchar    *dir);
374 gint append_file                (const gchar    *src,
375                                  const gchar    *dest,
376                                  gboolean        keep_backup);
377 gint copy_file                  (const gchar    *src,
378                                  const gchar    *dest,
379                                  gboolean        keep_backup);
380 gint move_file                  (const gchar    *src,
381                                  const gchar    *dest,
382                                  gboolean        overwrite);
383 gint copy_file_part             (FILE           *fp,
384                                  off_t           offset,
385                                  size_t          length,
386                                  const gchar    *dest);
387
388 gchar *canonicalize_str         (const gchar    *str);
389 gint canonicalize_file          (const gchar    *src,
390                                  const gchar    *dest);
391 gint canonicalize_file_replace  (const gchar    *file);
392 gint uncanonicalize_file        (const gchar    *src,
393                                  const gchar    *dest);
394 gint uncanonicalize_file_replace(const gchar    *file);
395
396 gchar *normalize_newlines       (const gchar    *str);
397
398 gchar *get_outgoing_rfc2822_str (FILE           *fp);
399
400 gint change_file_mode_rw        (FILE           *fp,
401                                  const gchar    *file);
402 FILE *my_tmpfile                (void);
403 FILE *get_tmpfile_in_dir        (const gchar    *dir,
404                                  gchar         **filename);
405 FILE *str_open_as_stream        (const gchar    *str);
406 gint str_write_to_file          (const gchar    *str,
407                                  const gchar    *file);
408 gchar *file_read_to_str         (const gchar    *file);
409 gchar *file_read_stream_to_str  (FILE           *fp);
410
411 /* process execution */
412 gint execute_async              (gchar *const    argv[]);
413 gint execute_sync               (gchar *const    argv[]);
414 gint execute_command_line       (const gchar    *cmdline,
415                                  gboolean        async);
416 gchar *get_command_output       (const gchar    *cmdline);
417
418 /* open URI with external browser */
419 gint open_uri(const gchar *uri, const gchar *cmdline);
420
421 /* time functions */
422 time_t remote_tzoffset_sec      (const gchar    *zone);
423 time_t tzoffset_sec             (time_t         *now);
424 gchar *tzoffset                 (time_t         *now);
425 void get_rfc822_date            (gchar          *buf,
426                                  gint            len);
427
428 /* debugging */
429 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
430
431 /* subject threading */
432 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
433 void subject_table_insert(GHashTable *subject_table, gchar * subject,
434                           void * data);
435 void subject_table_remove(GHashTable *subject_table, gchar * subject);
436 gint subject_get_prefix_length (const gchar *subject);
437
438 /* quoting recognition */
439 const gchar * line_has_quote_char       (const gchar *str,
440                                          const gchar *quote_chars);
441 const gchar * line_has_quote_char_last  (const gchar *str,
442                                          const gchar *quote_chars);
443
444 guint g_stricase_hash   (gconstpointer gptr);
445 gint g_stricase_equal   (gconstpointer gptr1, gconstpointer gptr2);
446 gint g_int_compare      (gconstpointer a, gconstpointer b);
447
448 gchar *generate_msgid           (const gchar *address, gchar *buf, gint len);
449 gchar *generate_mime_boundary   (const gchar *prefix);
450
451 gint quote_cmd_argument(gchar * result, guint size,
452                         const gchar * path);
453 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
454
455 #ifdef __cplusplus
456 }
457 #endif
458
459 #endif /* __UTILS_H__ */