1d3729fec3628a15e6ba346cdabf5f1ecdc8907b
[claws.git] / src / utils.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 Xstrconcat_a(ptr, str1, str2, iffail) \
115 { \
116         gchar *__tmp; \
117  \
118         if ((__tmp = alloca(strlen(str1) + strlen(str2) + 1)) == NULL) { \
119                 g_warning("can't allocate memory\n"); \
120                 iffail; \
121         } else { \
122                 strcpy(__tmp, str1); \
123                 strcat(__tmp, str2); \
124         } \
125  \
126         ptr = __tmp; \
127 }
128
129 #define FILE_OP_ERROR(file, func) \
130 { \
131         fprintf(stderr, "%s: ", file); \
132         perror(func); \
133 }
134
135 #define debug_print \
136         debug_print_real(__FILE__ ":%d:", __LINE__), \
137         debug_print_real
138
139 /* for macro expansion */
140 #define Str(x)  #x
141 #define Xstr(x) Str(x)
142
143 void list_free_strings          (GList          *list);
144 void slist_free_strings         (GSList         *list);
145
146 void hash_free_strings          (GHashTable     *table);
147 void hash_free_value_mem        (GHashTable     *table);
148
149 void ptr_array_free_strings     (GPtrArray      *array);
150
151 /* number-string conversion */
152 gint to_number                  (const gchar *nstr);
153 gchar *itos_buf                 (gchar       *nstr,
154                                  gint         n);
155 gchar *itos                     (gint         n);
156 gchar *to_human_readable        (off_t        size);
157
158 /* alternative string functions */
159 gint strcmp2            (const gchar    *s1,
160                          const gchar    *s2);
161 gchar *strstr2          (const gchar    *s1,
162                          const gchar    *s2);
163 gint path_cmp           (const gchar    *s1,
164                          const gchar    *s2);
165 gchar *strretchomp      (gchar          *str);
166 gchar *strtailchomp     (gchar          *str,
167                          gchar           tail_char);
168 gchar *strcrchomp       (gchar          *str);
169 gchar *strcasestr       (const gchar    *haystack,
170                          const gchar    *needle);
171 gchar *strncpy2         (gchar          *dest,
172                          const gchar    *src,
173                          size_t          n);
174
175 /* wide-character functions */
176 #if !HAVE_ISWALNUM
177 int iswalnum            (wint_t wc);
178 #endif
179 #if !HAVE_ISWSPACE
180 int iswspace            (wint_t wc);
181 #endif
182 #if !HAVE_TOWLOWER
183 wint_t towlower         (wint_t wc);
184 #endif
185
186 #if !HAVE_WCSLEN
187 size_t wcslen           (const wchar_t *s);
188 #endif
189 #if !HAVE_WCSCPY
190 wchar_t *wcscpy         (wchar_t       *dest,
191                          const wchar_t *src);
192 #endif
193 #if !HAVE_WCSNCPY
194 wchar_t *wcsncpy        (wchar_t       *dest,
195                          const wchar_t *src,
196                          size_t         n);
197 #endif
198
199 wchar_t *wcsdup                 (const wchar_t *s);
200 wchar_t *wcsndup                (const wchar_t *s,
201                                  size_t         n);
202 wchar_t *strdup_mbstowcs        (const gchar   *s);
203 gchar *strdup_wcstombs          (const wchar_t *s);
204 gint wcsncasecmp                (const wchar_t *s1,
205                                  const wchar_t *s2,
206                                  size_t         n);
207 wchar_t *wcscasestr             (const wchar_t *haystack,
208                                  const wchar_t *needle);
209
210 gboolean is_next_nonascii       (const wchar_t *s);
211 gboolean is_next_mbs            (const wchar_t *s);
212 wchar_t *find_wspace            (const wchar_t *s);
213
214 /* functions for string parsing */
215 gint subject_compare                    (const gchar    *s1,
216                                          const gchar    *s2);
217 void trim_subject                       (gchar          *str);
218 void eliminate_parenthesis              (gchar          *str,
219                                          gchar           op,
220                                          gchar           cl);
221 void extract_parenthesis                (gchar          *str,
222                                          gchar           op,
223                                          gchar           cl);
224
225 void extract_one_parenthesis_with_skip_quote    (gchar          *str,
226                                                  gchar           quote_chr,
227                                                  gchar           op,
228                                                  gchar           cl);
229 void extract_parenthesis_with_skip_quote        (gchar          *str,
230                                                  gchar           quote_chr,
231                                                  gchar           op,
232                                                  gchar           cl);
233
234 void eliminate_quote                    (gchar          *str,
235                                          gchar           quote_chr);
236 void extract_quote                      (gchar          *str,
237                                          gchar           quote_chr);
238 void eliminate_address_comment          (gchar          *str);
239 gchar *strchr_with_skip_quote           (const gchar    *str,
240                                          gint            quote_chr,
241                                          gint            c);
242 gchar *strrchr_with_skip_quote          (const gchar    *str,
243                                          gint            quote_chr,
244                                          gint            c);
245 void extract_address                    (gchar          *str);
246
247 GSList *address_list_append             (GSList         *addr_list,
248                                          const gchar    *str);
249 GSList *references_list_append          (GSList         *msgid_list,
250                                          const gchar    *str);
251 GSList *newsgroup_list_append           (GSList         *group_list,
252                                          const gchar    *str);
253
254 GList *add_history                      (GList          *list,
255                                          const gchar    *str);
256
257 void remove_return                      (gchar          *str);
258 void remove_space                       (gchar          *str);
259 void unfold_line                        (gchar          *str);
260 void subst_char                         (gchar          *str,
261                                          gchar           orig,
262                                          gchar           subst);
263 gboolean is_header_line                 (const gchar    *str);
264 gboolean is_ascii_str                   (const guchar   *str);
265 gint get_quote_level                    (const gchar    *str);
266 gchar *strstr_with_skip_quote           (const gchar    *haystack,
267                                          const gchar    *needle);
268 gchar **strsplit_with_quote             (const gchar    *str,
269                                          const gchar    *delim,
270                                          gint            max_tokens);
271
272 GList *uri_list_extract_filenames       (const gchar    *uri_list);
273 void decode_uri                         (gchar          *decoded_uri,
274                                          const gchar    *encoded_uri);
275
276 /* return static strings */
277 gchar *get_home_dir             (void);
278 gchar *get_rc_dir               (void);
279 gchar *get_news_cache_dir       (void);
280 gchar *get_imap_cache_dir       (void);
281 gchar *get_mbox_cache_dir       (void);
282 gchar *get_mime_tmp_dir         (void);
283 gchar *get_template_dir         (void);
284 gchar *get_tmp_file             (void);
285 gchar *get_domain_name          (void);
286
287 /* file / directory handling */
288 off_t get_file_size             (const gchar    *file);
289 off_t get_left_file_size        (FILE           *fp);
290 gboolean file_exist             (const gchar    *file,
291                                  gboolean        allow_fifo);
292 gboolean is_dir_exist           (const gchar    *dir);
293 gint change_dir                 (const gchar    *dir);
294 gint make_dir_hier              (const gchar    *dir);
295 gint remove_all_files           (const gchar    *dir);
296 gint remove_numbered_files      (const gchar    *dir,
297                                  guint           first,
298                                  guint           last);
299 gint remove_all_numbered_files  (const gchar    *dir);
300 gint remove_dir_recursive       (const gchar    *dir);
301 gint copy_file                  (const gchar    *src,
302                                  const gchar    *dest);
303 gint move_file                  (const gchar    *src,
304                                  const gchar    *dest);
305 gint change_file_mode_rw        (FILE           *fp,
306                                  const gchar    *file);
307 FILE *my_tmpfile                (void);
308
309 #define is_file_exist(file)             file_exist(file, FALSE)
310 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
311
312 /* process execution */
313 gint execute_async              (gchar *const    argv[]);
314 gint execute_sync               (gchar *const    argv[]);
315 gint execute_command_line       (const gchar    *cmdline,
316                                  gboolean        async);
317
318 /* open URI with external browser */
319 gint open_uri(const gchar *uri, const gchar *cmdline);
320
321 /* time functions */
322 time_t remote_tzoffset_sec      (const gchar    *zone);
323 time_t tzoffset_sec             (time_t         *now);
324 gchar *tzoffset                 (time_t         *now);
325 void get_rfc822_date            (gchar          *buf,
326                                  gint            len);
327
328 /* logging */
329 void set_log_file       (const gchar *filename);
330 void close_log_file     (void);
331 void log_verbosity_set  (gboolean verbose);
332 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
333 void log_print          (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
334 void log_message        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
335 void log_warning        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
336 void log_error          (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
337
338 /* subject threading */
339 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
340 void subject_table_insert(GHashTable *subject_table, gchar * subject,
341                           void * data);
342 void subject_table_remove(GHashTable *subject_table, gchar * subject);
343 gboolean subject_is_reply(const gchar *subject);
344
345
346 #endif /* __UTILS_H__ */