replace LOG_MESSAGE with debug_print
[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 FILE_OP_ERROR(file, func) \
115 { \
116         fprintf(stderr, "%s: ", file); \
117         perror(func); \
118 }
119
120 #define debug_print(format, ...) debug_print_real(__FILE__, __LINE__, format , ##__VA_ARGS__)
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 void hash_free_value_mem        (GHashTable     *table);
131
132 void ptr_array_free_strings     (GPtrArray      *array);
133
134 /* number-string conversion */
135 gint to_number                  (const gchar *nstr);
136 gchar *itos_buf                 (gchar       *nstr,
137                                  gint         n);
138 gchar *itos                     (gint         n);
139 gchar *to_human_readable        (off_t        size);
140
141 /* alternative string functions */
142 gint strcmp2            (const gchar    *s1,
143                          const gchar    *s2);
144 gint strstr2            (const gchar    *s1,
145                          const gchar    *s2);
146 gint path_cmp           (const gchar    *s1,
147                          const gchar    *s2);
148 gchar *strretchomp      (gchar          *str);
149 gchar *strtailchomp     (gchar          *str,
150                          gchar           tail_char);
151 gchar *strcasestr       (const gchar    *haystack,
152                          const gchar    *needle);
153 gchar *strncpy2         (gchar          *dest,
154                          const gchar    *src,
155                          size_t          n);
156
157 /* wide-character functions */
158 #if !HAVE_ISWALNUM
159 int iswalnum            (wint_t wc);
160 #endif
161 #if !HAVE_ISWSPACE
162 int iswspace            (wint_t wc);
163 #endif
164 #if !HAVE_TOWLOWER
165 wint_t towlower         (wint_t wc);
166 #endif
167
168 #if !HAVE_WCSLEN
169 size_t wcslen           (const wchar_t *s);
170 #endif
171 #if !HAVE_WCSCPY
172 wchar_t *wcscpy         (wchar_t       *dest,
173                          const wchar_t *src);
174 #endif
175 #if !HAVE_WCSNCPY
176 wchar_t *wcsncpy        (wchar_t       *dest,
177                          const wchar_t *src,
178                          size_t         n);
179 #endif
180
181 wchar_t *wcsdup                 (const wchar_t *s);
182 wchar_t *wcsndup                (const wchar_t *s,
183                                  size_t         n);
184 wchar_t *strdup_mbstowcs        (const gchar   *s);
185 gchar *strdup_wcstombs          (const wchar_t *s);
186 gint wcsncasecmp                (const wchar_t *s1,
187                                  const wchar_t *s2,
188                                  size_t         n);
189 wchar_t *wcscasestr             (const wchar_t *haystack,
190                                  const wchar_t *needle);
191
192 gboolean is_next_nonascii       (const wchar_t *s);
193 gboolean is_next_mbs            (const wchar_t *s);
194 wchar_t *find_wspace            (const wchar_t *s);
195
196 /* functions for string parsing */
197 gint subject_compare                    (const gchar    *s1,
198                                          const gchar    *s2);
199 void trim_subject                       (gchar          *str);
200 void eliminate_parenthesis              (gchar          *str,
201                                          gchar           op,
202                                          gchar           cl);
203 void extract_parenthesis                (gchar          *str,
204                                          gchar           op,
205                                          gchar           cl);
206
207 void extract_one_parenthesis_with_skip_quote    (gchar          *str,
208                                                  gchar           quote_chr,
209                                                  gchar           op,
210                                                  gchar           cl);
211 void extract_parenthesis_with_skip_quote        (gchar          *str,
212                                                  gchar           quote_chr,
213                                                  gchar           op,
214                                                  gchar           cl);
215
216 void eliminate_quote                    (gchar          *str,
217                                          gchar           quote_chr);
218 void extract_quote                      (gchar          *str,
219                                          gchar           quote_chr);
220 void eliminate_address_comment          (gchar          *str);
221 gchar *strchr_with_skip_quote           (const gchar    *str,
222                                          gint            quote_chr,
223                                          gint            c);
224 gchar *strrchr_with_skip_quote          (const gchar    *str,
225                                          gint            quote_chr,
226                                          gint            c);
227 void extract_address                    (gchar          *str);
228 GSList *address_list_append             (GSList         *addr_list,
229                                          const gchar    *str);
230 GSList *references_list_append          (GSList         *msgid_list,
231                                          const gchar    *str);
232 GSList *newsgroup_list_append           (GSList         *group_list,
233                                          const gchar    *str);
234 void remove_return                      (gchar          *str);
235 void remove_space                       (gchar          *str);
236 void unfold_line                        (gchar          *str);
237 void subst_char                         (gchar          *str,
238                                          gchar           orig,
239                                          gchar           subst);
240 gboolean is_header_line                 (const gchar    *str);
241 gboolean is_ascii_str                   (const guchar   *str);
242 gint get_quote_level                    (const gchar    *str);
243 GList *uri_list_extract_filenames       (const gchar    *uri_list);
244 gchar *strstr_with_skip_quote           (const gchar    *haystack,
245                                          const gchar    *needle);
246 gchar **strsplit_with_quote             (const gchar    *str,
247                                          const gchar    *delim,
248                                          gint            max_tokens);
249
250 /* return static strings */
251 gchar *get_home_dir             (void);
252 gchar *get_rc_dir               (void);
253 gchar *get_news_cache_dir       (void);
254 gchar *get_imap_cache_dir       (void);
255 gchar *get_mbox_cache_dir       (void);
256 gchar *get_mime_tmp_dir         (void);
257 gchar *get_tmp_file             (void);
258 gchar *get_domain_name          (void);
259
260 /* file / directory handling */
261 off_t get_file_size             (const gchar    *file);
262 off_t get_left_file_size        (FILE           *fp);
263 gboolean file_exist             (const gchar    *file,
264                                  gboolean        allow_fifo);
265 gboolean is_dir_exist           (const gchar    *dir);
266 gint change_dir                 (const gchar    *dir);
267 gint make_dir_hier              (const gchar    *dir);
268 gint remove_all_files           (const gchar    *dir);
269 gint remove_numbered_files      (const gchar    *dir,
270                                  guint           first,
271                                  guint           last);
272 gint remove_all_numbered_files  (const gchar    *dir);
273 gint remove_dir_recursive       (const gchar    *dir);
274 gint copy_file                  (const gchar    *src,
275                                  const gchar    *dest);
276 gint move_file                  (const gchar    *src,
277                                  const gchar    *dest);
278 gint change_file_mode_rw        (FILE           *fp,
279                                  const gchar    *file);
280 FILE *my_tmpfile                (void);
281
282 #define is_file_exist(file)             file_exist(file, FALSE)
283 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
284
285 /* process execution */
286 gint execute_async              (gchar *const    argv[]);
287 gint execute_sync               (gchar *const    argv[]);
288 gint execute_command_line       (const gchar    *cmdline,
289                                  gboolean        async);
290
291 /* open URI with external browser */
292 gint open_uri(const gchar *uri, const gchar *cmdline);
293
294 /* time functions */
295 time_t remote_tzoffset_sec      (const gchar    *zone);
296 time_t tzoffset_sec             (time_t         *now);
297 gchar *tzoffset                 (time_t         *now);
298 void get_rfc822_date            (gchar          *buf,
299                                  gint            len);
300
301 /* logging */
302 void set_log_file       (const gchar *filename);
303 void close_log_file     (void);
304 void log_verbosity_set  (gboolean verbose);
305 void debug_print_real   (const gchar *file, const guint line, const gchar *format, ...) G_GNUC_PRINTF(3, 4);
306 void log_print          (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
307 void log_message        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
308 void log_warning        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
309 void log_error          (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
310
311 #endif /* __UTILS_H__ */