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