sync with sylpheed 0.4.65cvs10
[claws.git] / src / utils.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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
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   #if SIZEOF_UNSIGNED_INT == 4
60     typedef unsigned int u32;
61   #elif SIZEOF_UNSIGNED_LONG == 4
62     typedef unsigned long u32;
63   #else
64     #error no typedef for u32
65   #endif
66   #define HAVE_U32_TYPEDEF
67 #endif
68
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 FILE_OP_ERROR(file, func) \
117 { \
118         fprintf(stderr, "%s: ", file); \
119         perror(func); \
120 }
121
122 /* for macro expansion */
123 #define Str(x)  #x
124 #define Xstr(x) Str(x)
125
126 void list_free_strings          (GList          *list);
127 void slist_free_strings         (GSList         *list);
128
129 void hash_free_strings          (GHashTable     *table);
130
131 void ptr_array_free_strings     (GPtrArray      *array);
132
133 /* number-string conversion */
134 gint to_number                  (const gchar *nstr);
135 gchar *itos                     (gint         n);
136 gchar *to_human_readable        (off_t        size);
137
138 /* alternative string functions */
139 gint strcmp2            (const gchar    *s1,
140                          const gchar    *s2);
141 gint path_cmp           (const gchar    *s1,
142                          const gchar    *s2);
143 gchar *strretchomp      (gchar          *str);
144 gchar *strcasestr       (const gchar    *haystack,
145                          const gchar    *needle);
146 gchar *strncpy2         (gchar          *dest,
147                          const gchar    *src,
148                          size_t          n);
149
150 /* wide-character functions */
151 #if !HAVE_ISWALNUM
152 int iswalnum            (wint_t wc);
153 #endif
154 #if !HAVE_ISWSPACE
155 int iswspace            (wint_t wc);
156 #endif
157 #if !HAVE_TOWLOWER
158 wint_t towlower         (wint_t wc);
159 #endif
160
161 #if !HAVE_WCSLEN
162 size_t wcslen           (const wchar_t *s);
163 #endif
164 #if !HAVE_WCSCPY
165 wchar_t *wcscpy         (wchar_t       *dest,
166                          const wchar_t *src);
167 #endif
168 #if !HAVE_WCSNCPY
169 wchar_t *wcsncpy        (wchar_t       *dest,
170                          const wchar_t *src,
171                          size_t         n);
172 #endif
173
174 wchar_t *wcsdup                 (const wchar_t *s);
175 wchar_t *wcsndup                (const wchar_t *s,
176                                  size_t         n);
177 wchar_t *strdup_mbstowcs        (const gchar   *s);
178 gchar *strdup_wcstombs          (const wchar_t *s);
179 gint wcsncasecmp                (const wchar_t *s1,
180                                  const wchar_t *s2,
181                                  size_t         n);
182 wchar_t *wcscasestr             (const wchar_t *haystack,
183                                  const wchar_t *needle);
184
185 gboolean is_next_nonascii       (const wchar_t *s);
186 gboolean is_next_mbs            (const wchar_t *s);
187 wchar_t *find_wspace            (const wchar_t *s);
188
189 /* functions for string parsing */
190 gint subject_compare                    (const gchar    *s1,
191                                          const gchar    *s2);
192 void trim_subject                       (gchar          *str);
193 void eliminate_parenthesis              (gchar          *str,
194                                          gchar           op,
195                                          gchar           cl);
196 void extract_parenthesis                (gchar          *str,
197                                          gchar           op,
198                                          gchar           cl);
199 void extract_parenthesis_with_skip_quote(gchar          *str,
200                                          gchar           quote_chr,
201                                          gchar           op,
202                                          gchar           cl);
203 void eliminate_quote                    (gchar          *str,
204                                          gchar           quote_chr);
205 void extract_quote                      (gchar          *str,
206                                          gchar           quote_chr);
207 void eliminate_address_comment          (gchar          *str);
208 gchar *strchr_with_skip_quote           (const gchar    *str,
209                                          gint            quote_chr,
210                                          gint            c);
211 gchar *strrchr_with_skip_quote          (const gchar    *str,
212                                          gint            quote_chr,
213                                          gint            c);
214 void extract_address                    (gchar          *str);
215 GSList *address_list_append             (GSList         *addr_list,
216                                          const gchar    *str);
217 GSList *references_list_append          (GSList         *msgid_list,
218                                          const gchar    *str);
219 GSList *newsgroup_list_append           (GSList         *group_list,
220                                          const gchar    *str);
221 void remove_return                      (gchar          *str);
222 void remove_space                       (gchar          *str);
223 void subst_char                         (gchar          *str,
224                                          gchar           orig,
225                                          gchar           subst);
226 gboolean is_header_line                 (const gchar    *str);
227 gboolean is_ascii_str                   (const guchar   *str);
228 gint get_quote_level                    (const gchar    *str);
229 GList *uri_list_extract_filenames       (const gchar    *uri_list);
230 gchar *strstr_with_skip_quote           (const gchar    *haystack,
231                                          const gchar    *needle);
232 gchar **strsplit_with_quote             (const gchar    *str,
233                                          const gchar    *delim,
234                                          gint            max_tokens);
235
236 /* return static strings */
237 gchar *get_home_dir             (void);
238 gchar *get_rc_dir               (void);
239 gchar *get_news_cache_dir       (void);
240 gchar *get_imap_cache_dir       (void);
241 gchar *get_mime_tmp_dir         (void);
242 gchar *get_tmp_file             (void);
243 gchar *get_domain_name          (void);
244
245 /* file / directory handling */
246 off_t get_file_size             (const gchar    *file);
247 gboolean file_exist             (const gchar    *file,
248                                  gboolean        allow_fifo);
249 gboolean is_dir_exist           (const gchar    *dir);
250 gint change_dir                 (const gchar    *dir);
251 gint make_dir_hier              (const gchar    *dir);
252 gint remove_all_files           (const gchar    *dir);
253 gint remove_dir_recursive       (const gchar    *dir);
254 gint copy_file                  (const gchar    *src,
255                                  const gchar    *dest);
256 gint change_file_mode_rw        (FILE           *fp,
257                                  const gchar    *file);
258 FILE *my_tmpfile                (void);
259
260 #define is_file_exist(file)             file_exist(file, FALSE)
261 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
262
263 /* process execution */
264 gint execute_async              (gchar *const    argv[]);
265 gint execute_command_line       (const gchar    *cmdline);
266
267 /* open URI with external browser */
268 gint open_uri(const gchar *uri, const gchar *cmdline);
269
270 /* time functions */
271 time_t remote_tzoffset_sec      (const gchar    *zone);
272 time_t tzoffset_sec             (time_t         *now);
273 gchar *tzoffset                 (time_t         *now);
274 void get_rfc822_date            (gchar          *buf,
275                                  gint            len);
276
277 /* logging */
278 void debug_print(const gchar *format, ...) G_GNUC_PRINTF(1, 2);
279 void log_print  (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
280 void log_message(const gchar *format, ...) G_GNUC_PRINTF(1, 2);
281 void log_warning(const gchar *format, ...) G_GNUC_PRINTF(1, 2);
282 void log_error  (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
283
284 #endif /* __UTILS_H__ */