This commit was manufactured by cvs2svn to create tag 'LAST_STABLE'.
[claws.git] / src / utils.h
blobbc4e79615668fa924a0b0e52cb432dd6e915c43c
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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.
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.
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.
20 #ifndef __UTILS_H__
21 #define __UTILS_H__
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
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
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
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
62 #ifndef BIG_ENDIAN_HOST
63 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
64 #define BIG_ENDIAN_HOST 1
65 #endif
66 #endif
68 #define CHDIR_RETURN_IF_FAIL(dir) \
69 { \
70 if (change_dir(dir) < 0) return; \
73 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
74 { \
75 if (change_dir(dir) < 0) return val; \
78 #define Xalloca(ptr, size, iffail) \
79 { \
80 if ((ptr = alloca(size)) == NULL) { \
81 g_warning("can't allocate memory\n"); \
82 iffail; \
83 } \
86 #define Xstrdup_a(ptr, str, iffail) \
87 { \
88 gchar *__tmp; \
90 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
91 g_warning("can't allocate memory\n"); \
92 iffail; \
93 } else \
94 strcpy(__tmp, str); \
96 ptr = __tmp; \
99 #define Xstrndup_a(ptr, str, len, iffail) \
101 gchar *__tmp; \
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'; \
111 ptr = __tmp; \
114 #define Xstrcat_a(ptr, str1, str2, iffail) \
116 gchar *__tmp; \
117 gint len1, len2; \
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); \
129 ptr = __tmp; \
132 #define FILE_OP_ERROR(file, func) \
134 fprintf(stderr, "%s: ", file); \
135 perror(func); \
138 #define debug_print \
139 debug_print_real(__FILE__ ":%d:", __LINE__), \
140 debug_print_real
142 /* for macro expansion */
143 #define Str(x) #x
144 #define Xstr(x) Str(x)
146 void list_free_strings (GList *list);
147 void slist_free_strings (GSList *list);
149 void hash_free_strings (GHashTable *table);
150 void hash_free_value_mem (GHashTable *table);
152 void ptr_array_free_strings (GPtrArray *array);
154 /* number-string conversion */
155 gint to_number (const gchar *nstr);
156 gchar *itos_buf (gchar *nstr,
157 gint n);
158 gchar *itos (gint n);
159 gchar *to_human_readable (off_t size);
161 /* alternative string functions */
162 gint strcmp2 (const gchar *s1,
163 const gchar *s2);
164 gchar *strstr2 (const gchar *s1,
165 const gchar *s2);
166 gint path_cmp (const gchar *s1,
167 const gchar *s2);
168 gchar *strretchomp (gchar *str);
169 gchar *strtailchomp (gchar *str,
170 gchar tail_char);
171 gchar *strcrchomp (gchar *str);
172 gchar *strcasestr (const gchar *haystack,
173 const gchar *needle);
174 gchar *strncpy2 (gchar *dest,
175 const gchar *src,
176 size_t n);
178 /* wide-character functions */
179 #if !HAVE_ISWALNUM
180 int iswalnum (wint_t wc);
181 #endif
182 #if !HAVE_ISWSPACE
183 int iswspace (wint_t wc);
184 #endif
185 #if !HAVE_TOWLOWER
186 wint_t towlower (wint_t wc);
187 #endif
189 #if !HAVE_WCSLEN
190 size_t wcslen (const wchar_t *s);
191 #endif
192 #if !HAVE_WCSCPY
193 wchar_t *wcscpy (wchar_t *dest,
194 const wchar_t *src);
195 #endif
196 #if !HAVE_WCSNCPY
197 wchar_t *wcsncpy (wchar_t *dest,
198 const wchar_t *src,
199 size_t n);
200 #endif
202 wchar_t *wcsdup (const wchar_t *s);
203 wchar_t *wcsndup (const wchar_t *s,
204 size_t n);
205 wchar_t *strdup_mbstowcs (const gchar *s);
206 gchar *strdup_wcstombs (const wchar_t *s);
207 gint wcsncasecmp (const wchar_t *s1,
208 const wchar_t *s2,
209 size_t n);
210 wchar_t *wcscasestr (const wchar_t *haystack,
211 const wchar_t *needle);
213 gboolean is_next_nonascii (const wchar_t *s);
214 gboolean is_next_mbs (const wchar_t *s);
215 wchar_t *find_wspace (const wchar_t *s);
217 /* functions for string parsing */
218 gint subject_compare (const gchar *s1,
219 const gchar *s2);
220 void trim_subject (gchar *str);
221 void eliminate_parenthesis (gchar *str,
222 gchar op,
223 gchar cl);
224 void extract_parenthesis (gchar *str,
225 gchar op,
226 gchar cl);
228 void extract_one_parenthesis_with_skip_quote (gchar *str,
229 gchar quote_chr,
230 gchar op,
231 gchar cl);
232 void extract_parenthesis_with_skip_quote (gchar *str,
233 gchar quote_chr,
234 gchar op,
235 gchar cl);
237 void eliminate_quote (gchar *str,
238 gchar quote_chr);
239 void extract_quote (gchar *str,
240 gchar quote_chr);
241 void eliminate_address_comment (gchar *str);
242 gchar *strchr_with_skip_quote (const gchar *str,
243 gint quote_chr,
244 gint c);
245 gchar *strrchr_with_skip_quote (const gchar *str,
246 gint quote_chr,
247 gint c);
248 void extract_address (gchar *str);
250 GSList *address_list_append (GSList *addr_list,
251 const gchar *str);
252 GSList *references_list_append (GSList *msgid_list,
253 const gchar *str);
254 GSList *newsgroup_list_append (GSList *group_list,
255 const gchar *str);
257 GList *add_history (GList *list,
258 const gchar *str);
260 void remove_return (gchar *str);
261 void remove_space (gchar *str);
262 void unfold_line (gchar *str);
263 void subst_char (gchar *str,
264 gchar orig,
265 gchar subst);
266 void subst_chars (gchar *str,
267 gchar *orig,
268 gchar subst);
269 void subst_for_filename (gchar *str);
270 gboolean is_header_line (const gchar *str);
271 gboolean is_ascii_str (const guchar *str);
272 gint get_quote_level (const gchar *str,
273 const gchar *quote_chars);
274 gchar *strstr_with_skip_quote (const gchar *haystack,
275 const gchar *needle);
276 gchar *strchr_parenthesis_close (const gchar *str,
277 gchar op,
278 gchar cl);
280 gchar **strsplit_parenthesis (const gchar *str,
281 gchar op,
282 gchar cl,
283 gint max_tokens);
284 gchar **strsplit_with_quote (const gchar *str,
285 const gchar *delim,
286 gint max_tokens);
288 gchar *get_abbrev_newsgroup_name (const gchar *group);
289 gchar *trim_string (const gchar *str,
290 gint len);
292 GList *uri_list_extract_filenames (const gchar *uri_list);
293 void decode_uri (gchar *decoded_uri,
294 const gchar *encoded_uri);
296 /* return static strings */
297 gchar *get_home_dir (void);
298 gchar *get_rc_dir (void);
299 gchar *get_news_cache_dir (void);
300 gchar *get_imap_cache_dir (void);
301 gchar *get_mbox_cache_dir (void);
302 gchar *get_mime_tmp_dir (void);
303 gchar *get_template_dir (void);
304 gchar *get_header_cache_dir (void);
305 gchar *get_tmp_file (void);
306 gchar *get_domain_name (void);
308 /* file / directory handling */
309 off_t get_file_size (const gchar *file);
310 off_t get_file_size_as_crlf (const gchar *file);
311 off_t get_left_file_size (FILE *fp);
313 gboolean file_exist (const gchar *file,
314 gboolean allow_fifo);
315 gboolean is_dir_exist (const gchar *dir);
316 gboolean is_file_entry_exist (const gchar *file);
318 #define is_file_exist(file) file_exist(file, FALSE)
319 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
321 gint change_dir (const gchar *dir);
322 gint make_dir (const gchar *dir);
323 gint make_dir_hier (const gchar *dir);
324 gint remove_all_files (const gchar *dir);
325 gint remove_numbered_files (const gchar *dir,
326 guint first,
327 guint last);
328 gint remove_all_numbered_files (const gchar *dir);
329 gint remove_dir_recursive (const gchar *dir);
330 gint copy_file (const gchar *src,
331 const gchar *dest);
332 gint move_file (const gchar *src,
333 const gchar *dest);
334 gint change_file_mode_rw (FILE *fp,
335 const gchar *file);
336 FILE *my_tmpfile (void);
337 FILE *str_open_as_stream (const gchar *str);
339 /* process execution */
340 gint execute_async (gchar *const argv[]);
341 gint execute_sync (gchar *const argv[]);
342 gint execute_command_line (const gchar *cmdline,
343 gboolean async);
345 /* open URI with external browser */
346 gint open_uri(const gchar *uri, const gchar *cmdline);
348 /* time functions */
349 time_t remote_tzoffset_sec (const gchar *zone);
350 time_t tzoffset_sec (time_t *now);
351 gchar *tzoffset (time_t *now);
352 void get_rfc822_date (gchar *buf,
353 gint len);
355 /* logging */
356 void set_log_file (const gchar *filename);
357 void close_log_file (void);
358 void log_verbosity_set (gboolean verbose);
359 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
360 void log_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
361 void log_message (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
362 void log_warning (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
363 void log_error (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
365 /* subject threading */
366 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
367 void subject_table_insert(GHashTable *subject_table, gchar * subject,
368 void * data);
369 void subject_table_remove(GHashTable *subject_table, gchar * subject);
370 gboolean subject_is_reply(const gchar *subject);
372 /* quoting recognition */
373 const gchar * line_has_quote_char (const gchar *str,
374 const gchar *quote_chars);
375 const gchar * line_has_quote_char_last (const gchar *str,
376 const gchar *quote_chars);
379 #endif /* __UTILS_H__ */