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