Added "--enable-maintainer-mode".
[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 #define CHDIR_RETURN_IF_FAIL(dir) \
42 { \
43         if (change_dir(dir) < 0) return; \
44 }
45
46 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
47 { \
48         if (change_dir(dir) < 0) return val; \
49 }
50
51 #define Xalloca(ptr, size, iffail) \
52 { \
53         if ((ptr = alloca(size)) == NULL) { \
54                 g_warning("can't allocate memory\n"); \
55                 iffail; \
56         } \
57 }
58
59 #define Xstrdup_a(ptr, str, iffail) \
60 { \
61         gchar *__tmp; \
62  \
63         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
64                 g_warning("can't allocate memory\n"); \
65                 iffail; \
66         } else \
67                 strcpy(__tmp, str); \
68  \
69         ptr = __tmp; \
70 }
71
72 #define Xstrndup_a(ptr, str, len, iffail) \
73 { \
74         gchar *__tmp; \
75  \
76         if ((__tmp = alloca(len + 1)) == NULL) { \
77                 g_warning("can't allocate memory\n"); \
78                 iffail; \
79         } else { \
80                 strncpy(__tmp, str, len); \
81                 __tmp[len] = '\0'; \
82         } \
83  \
84         ptr = __tmp; \
85 }
86
87 #define FILE_OP_ERROR(file, func) \
88 { \
89         fprintf(stderr, "%s: ", file); \
90         perror(func); \
91 }
92
93 /* for macro expansion */
94 #define Str(x)  #x
95 #define Xstr(x) Str(x)
96
97 void list_free_strings          (GList          *list);
98 void slist_free_strings         (GSList         *list);
99
100 void hash_free_strings          (GHashTable     *table);
101
102 void ptr_array_free_strings     (GPtrArray      *array);
103
104 /* number-string conversion */
105 gint to_number                  (const gchar *nstr);
106 gchar *itos                     (gint         n);
107 gchar *to_human_readable        (off_t        size);
108
109 /* alternative string functions */
110 gint strcmp2            (const gchar    *s1,
111                          const gchar    *s2);
112 gint path_cmp           (const gchar    *s1,
113                          const gchar    *s2);
114 gchar *strretchomp      (gchar          *str);
115 gchar *strcasestr       (const gchar    *haystack,
116                          const gchar    *needle);
117 gchar *strncpy2         (gchar          *dest,
118                          const gchar    *src,
119                          size_t          n);
120
121 /* wide-character functions */
122 #if !HAVE_ISWALNUM
123 int iswalnum            (wint_t wc);
124 #endif
125 #if !HAVE_ISWSPACE
126 int iswspace            (wint_t wc);
127 #endif
128 #if !HAVE_TOWLOWER
129 wint_t towlower         (wint_t wc);
130 #endif
131
132 #if !HAVE_WCSLEN
133 size_t wcslen           (const wchar_t *s);
134 #endif
135 #if !HAVE_WCSCPY
136 wchar_t *wcscpy         (wchar_t       *dest,
137                          const wchar_t *src);
138 #endif
139 #if !HAVE_WCSNCPY
140 wchar_t *wcsncpy        (wchar_t       *dest,
141                          const wchar_t *src,
142                          size_t         n);
143 #endif
144
145 wchar_t *wcsdup                 (const wchar_t *s);
146 wchar_t *wcsndup                (const wchar_t *s,
147                                  size_t         n);
148 wchar_t *strdup_mbstowcs        (const gchar   *s);
149 gchar *strdup_wcstombs          (const wchar_t *s);
150 gint wcsncasecmp                (const wchar_t *s1,
151                                  const wchar_t *s2,
152                                  size_t         n);
153 wchar_t *wcscasestr             (const wchar_t *haystack,
154                                  const wchar_t *needle);
155
156 gboolean is_next_nonascii       (const wchar_t *s);
157 gboolean is_next_mbs            (const wchar_t *s);
158 wchar_t *find_wspace            (const wchar_t *s);
159
160 /* functions for string parsing */
161 gint subject_compare                    (const gchar    *s1,
162                                          const gchar    *s2);
163 void trim_subject                       (gchar          *str);
164 void eliminate_parenthesis              (gchar          *str,
165                                          gchar           op,
166                                          gchar           cl);
167 void extract_parenthesis                (gchar          *str,
168                                          gchar           op,
169                                          gchar           cl);
170 void extract_parenthesis_with_skip_quote(gchar          *str,
171                                          gchar           quote_chr,
172                                          gchar           op,
173                                          gchar           cl);
174 void eliminate_quote                    (gchar          *str,
175                                          gchar           quote_chr);
176 void extract_quote                      (gchar          *str,
177                                          gchar           quote_chr);
178 void eliminate_address_comment          (gchar          *str);
179 gchar *strchr_with_skip_quote           (const gchar    *str,
180                                          gint            quote_chr,
181                                          gint            c);
182 gchar *strrchr_with_skip_quote          (const gchar    *str,
183                                          gint            quote_chr,
184                                          gint            c);
185 void extract_address                    (gchar          *str);
186 GSList *address_list_append             (GSList         *addr_list,
187                                          const gchar    *str);
188 GSList *references_list_append          (GSList         *msgid_list,
189                                          const gchar    *str);
190 GSList *newsgroup_list_append           (GSList         *group_list,
191                                          const gchar    *str);
192 void remove_return                      (gchar          *str);
193 void remove_space                       (gchar          *str);
194 void subst_char                         (gchar          *str,
195                                          gchar           orig,
196                                          gchar           subst);
197 gboolean is_header_line                 (const gchar    *str);
198 gboolean is_ascii_str                   (const guchar   *str);
199 gint get_quote_level                    (const gchar    *str);
200 GList *uri_list_extract_filenames       (const gchar    *uri_list);
201 gchar *strstr_with_skip_quote           (const gchar    *haystack,
202                                          const gchar    *needle);
203 gchar **strsplit_with_quote             (const gchar    *str,
204                                          const gchar    *delim,
205                                          gint            max_tokens);
206
207 /* return static strings */
208 gchar *get_rc_dir               (void);
209 gchar *get_news_cache_dir       (void);
210 gchar *get_imap_cache_dir       (void);
211 gchar *get_mime_tmp_dir         (void);
212 gchar *get_tmp_file             (void);
213 gchar *get_domain_name          (void);
214
215 /* file / directory handling */
216 off_t get_file_size             (const gchar    *file);
217 gboolean file_exist             (const gchar    *file,
218                                  gboolean        allow_fifo);
219 gboolean is_dir_exist           (const gchar    *dir);
220 gint change_dir                 (const gchar    *dir);
221 gint make_dir_hier              (const gchar    *dir);
222 gint remove_all_files           (const gchar    *dir);
223 gint remove_dir_recursive       (const gchar    *dir);
224 gint copy_file                  (const gchar    *src,
225                                  const gchar    *dest);
226 gint change_file_mode_rw        (FILE           *fp,
227                                  const gchar    *file);
228 FILE *my_tmpfile                (void);
229
230 #define is_file_exist(file)             file_exist(file, FALSE)
231 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
232
233 /* process execution */
234 gint execute_async              (gchar *const    argv[]);
235 gint execute_command_line       (const gchar    *cmdline);
236
237 /* open URI with external browser */
238 gint open_uri(const gchar *uri, const gchar *cmdline);
239
240 /* time functions */
241 time_t remote_tzoffset_sec      (const gchar    *zone);
242 time_t tzoffset_sec             (time_t         *now);
243 gchar *tzoffset                 (time_t         *now);
244 void get_rfc822_date            (gchar          *buf,
245                                  gint            len);
246
247 /* logging */
248 void debug_print(const gchar *format, ...) G_GNUC_PRINTF(1, 2);
249 void log_print  (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
250 void log_message(const gchar *format, ...) G_GNUC_PRINTF(1, 2);
251 void log_warning(const gchar *format, ...) G_GNUC_PRINTF(1, 2);
252 void log_error  (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
253
254 #endif /* __UTILS_H__ */