Statistics
| Revision:

root / src / utils.h @ 1

History | View | Annotate | Download (11.4 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2004 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 <dirent.h>
34
#include <time.h>
35
#if HAVE_ALLOCA_H
36
#  include <alloca.h>
37
#endif
38
#if HAVE_WCHAR_H
39
#  include <wchar.h>
40
#endif
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
  typedef guint32 u32;
60
  #define HAVE_U32_TYPEDEF
61
#endif
62

    
63
#ifndef BIG_ENDIAN_HOST
64
  #if (G_BYTE_ORDER == G_BIG_ENDIAN)
65
    #define BIG_ENDIAN_HOST 1
66
  #endif
67
#endif
68

    
69
#define CHDIR_RETURN_IF_FAIL(dir) \
70
{ \
71
        if (change_dir(dir) < 0) return; \
72
}
73

    
74
#define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
75
{ \
76
        if (change_dir(dir) < 0) return val; \
77
}
78

    
79
#define Xalloca(ptr, size, iffail) \
80
{ \
81
        if ((ptr = alloca(size)) == NULL) { \
82
                g_warning("can't allocate memory\n"); \
83
                iffail; \
84
        } \
85
}
86

    
87
#define Xstrdup_a(ptr, str, iffail) \
88
{ \
89
        gchar *__tmp; \
90
 \
91
        if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
92
                g_warning("can't allocate memory\n"); \
93
                iffail; \
94
        } else \
95
                strcpy(__tmp, str); \
96
 \
97
        ptr = __tmp; \
98
}
99

    
100
#define Xstrndup_a(ptr, str, len, iffail) \
101
{ \
102
        gchar *__tmp; \
103
 \
104
        if ((__tmp = alloca(len + 1)) == NULL) { \
105
                g_warning("can't allocate memory\n"); \
106
                iffail; \
107
        } else { \
108
                strncpy(__tmp, str, len); \
109
                __tmp[len] = '\0'; \
110
        } \
111
 \
112
        ptr = __tmp; \
113
}
114

    
115
#define Xstrcat_a(ptr, str1, str2, iffail) \
116
{ \
117
        gchar *__tmp; \
118
        gint len1, len2; \
119
 \
120
        len1 = strlen(str1); \
121
        len2 = strlen(str2); \
122
        if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
123
                g_warning("can't allocate memory\n"); \
124
                iffail; \
125
        } else { \
126
                memcpy(__tmp, str1, len1); \
127
                memcpy(__tmp + len1, str2, len2 + 1); \
128
        } \
129
 \
130
        ptr = __tmp; \
131
}
132

    
133
#define AUTORELEASE_STR(str, iffail) \
134
{ \
135
        gchar *__str; \
136
        Xstrdup_a(__str, str, iffail); \
137
        g_free(str); \
138
        str = __str; \
139
}
140

    
141
#define FILE_OP_ERROR(file, func) \
142
{ \
143
        fprintf(stderr, "%s: ", file); \
144
        perror(func); \
145
}
146

    
147
/* for macro expansion */
148
#define Str(x)        #x
149
#define Xstr(x)        Str(x)
150

    
151
void list_free_strings                (GList                *list);
152
void slist_free_strings                (GSList                *list);
153

    
154
void hash_free_strings                (GHashTable        *table);
155
void hash_free_value_mem        (GHashTable        *table);
156

    
157
gint str_case_equal                (gconstpointer         v,
158
                                 gconstpointer         v2);
159
guint str_case_hash                (gconstpointer         key);
160

    
161
void ptr_array_free_strings        (GPtrArray        *array);
162

    
163
typedef gboolean (*StrFindFunc) (const gchar        *haystack,
164
                                 const gchar        *needle);
165

    
166
gboolean str_find                (const gchar        *haystack,
167
                                 const gchar        *needle);
168
gboolean str_case_find                (const gchar        *haystack,
169
                                 const gchar        *needle);
170
gboolean str_find_equal                (const gchar        *haystack,
171
                                 const gchar        *needle);
172
gboolean str_case_find_equal        (const gchar        *haystack,
173
                                 const gchar        *needle);
174

    
175
/* number-string conversion */
176
gint to_number                        (const gchar *nstr);
177
gchar *itos_buf                        (gchar             *nstr,
178
                                 gint              n);
179
gchar *itos                        (gint              n);
180
gchar *to_human_readable        (off_t              size);
181

    
182
/* alternative string functions */
183
gint strcmp2                (const gchar        *s1,
184
                         const gchar        *s2);
185
gint path_cmp                (const gchar        *s1,
186
                         const gchar        *s2);
187
gchar *strretchomp        (gchar                *str);
188
gchar *strtailchomp        (gchar                *str,
189
                         gchar                 tail_char);
190
gchar *strcrchomp        (gchar                *str);
191
gchar *strcasestr        (const gchar        *haystack,
192
                         const gchar        *needle);
193
gpointer my_memmem        (gconstpointer         haystack,
194
                         size_t                 haystacklen,
195
                         gconstpointer         needle,
196
                         size_t                 needlelen);
197
gchar *strncpy2                (gchar                *dest,
198
                         const gchar        *src,
199
                         size_t                 n);
200

    
201
/* wide-character functions */
202
#if !HAVE_ISWALNUM
203
int iswalnum                (wint_t wc);
204
#endif
205
#if !HAVE_ISWSPACE
206
int iswspace                (wint_t wc);
207
#endif
208
#if !HAVE_TOWLOWER
209
wint_t towlower                (wint_t wc);
210
#endif
211

    
212
#if !HAVE_WCSLEN
213
size_t wcslen                (const wchar_t *s);
214
#endif
215
#if !HAVE_WCSCPY
216
wchar_t *wcscpy                (wchar_t       *dest,
217
                         const wchar_t *src);
218
#endif
219
#if !HAVE_WCSNCPY
220
wchar_t *wcsncpy        (wchar_t       *dest,
221
                         const wchar_t *src,
222
                         size_t                n);
223
#endif
224

    
225
wchar_t *wcsdup                        (const wchar_t *s);
226
wchar_t *wcsndup                (const wchar_t *s,
227
                                 size_t                n);
228
wchar_t *strdup_mbstowcs        (const gchar   *s);
229
gchar *strdup_wcstombs                (const wchar_t *s);
230
gint wcsncasecmp                (const wchar_t *s1,
231
                                 const wchar_t *s2,
232
                                 size_t                n);
233
wchar_t *wcscasestr                (const wchar_t *haystack,
234
                                 const wchar_t *needle);
235
gint get_mbs_len                (const gchar        *s);
236

    
237
gboolean is_next_nonascii        (const guchar *s);
238
gint get_next_word_len                (const guchar *s);
239

    
240
/* functions for string parsing */
241
gint subject_compare                        (const gchar        *s1,
242
                                         const gchar        *s2);
243
gint subject_compare_for_sort                (const gchar        *s1,
244
                                         const gchar        *s2);
245
void trim_subject_for_compare                (gchar                *str);
246
void trim_subject_for_sort                (gchar                *str);
247
void trim_subject                        (gchar                *str);
248
void eliminate_parenthesis                (gchar                *str,
249
                                         gchar                 op,
250
                                         gchar                 cl);
251
void extract_parenthesis                (gchar                *str,
252
                                         gchar                 op,
253
                                         gchar                 cl);
254

    
255
void extract_parenthesis_with_skip_quote        (gchar                *str,
256
                                                 gchar                 quote_chr,
257
                                                 gchar                 op,
258
                                                 gchar                 cl);
259

    
260
void eliminate_quote                        (gchar                *str,
261
                                         gchar                 quote_chr);
262
void extract_quote                        (gchar                *str,
263
                                         gchar                 quote_chr);
264
void eliminate_address_comment                (gchar                *str);
265
gchar *strchr_with_skip_quote                (const gchar        *str,
266
                                         gint                 quote_chr,
267
                                         gint                 c);
268
gchar *strrchr_with_skip_quote                (const gchar        *str,
269
                                         gint                 quote_chr,
270
                                         gint                 c);
271
void extract_address                        (gchar                *str);
272
void extract_list_id_str                (gchar                *str);
273

    
274
GSList *address_list_append                (GSList                *addr_list,
275
                                         const gchar        *str);
276
GSList *references_list_append                (GSList                *msgid_list,
277
                                         const gchar        *str);
278
GSList *newsgroup_list_append                (GSList                *group_list,
279
                                         const gchar        *str);
280

    
281
GList *add_history                        (GList                *list,
282
                                         const gchar        *str);
283

    
284
void remove_return                        (gchar                *str);
285
void remove_space                        (gchar                *str);
286
void unfold_line                        (gchar                *str);
287
void subst_char                                (gchar                *str,
288
                                         gchar                 orig,
289
                                         gchar                 subst);
290
void subst_chars                        (gchar                *str,
291
                                         gchar                *orig,
292
                                         gchar                 subst);
293
void subst_for_filename                        (gchar                *str);
294
gboolean is_header_line                        (const gchar        *str);
295
gboolean is_ascii_str                        (const guchar        *str);
296
gint get_quote_level                        (const gchar        *str);
297

    
298
gchar *strstr_with_skip_quote                (const gchar        *haystack,
299
                                         const gchar        *needle);
300
gchar *strchr_parenthesis_close                (const gchar        *str,
301
                                         gchar                 op,
302
                                         gchar                 cl);
303

    
304
gchar **strsplit_parenthesis                (const gchar        *str,
305
                                         gchar                 op,
306
                                         gchar                 cl,
307
                                         gint                 max_tokens);
308
gchar **strsplit_with_quote                (const gchar        *str,
309
                                         const gchar        *delim,
310
                                         gint                 max_tokens);
311

    
312
gchar *get_abbrev_newsgroup_name        (const gchar        *group,
313
                                         gint                 len);
314
gchar *trim_string                        (const gchar        *str,
315
                                         gint                 len);
316
gchar *trim_string_before                (const gchar        *str,
317
                                         gint                 len);
318

    
319
GList *uri_list_extract_filenames        (const gchar        *uri_list);
320
gboolean is_uri_string                        (const gchar        *str);
321
gchar *get_uri_path                        (const gchar        *uri);
322
void decode_uri                                (gchar                *decoded_uri,
323
                                         const gchar        *encoded_uri);
324
gint scan_mailto_url                        (const gchar        *mailto,
325
                                         gchar               **to,
326
                                         gchar               **cc,
327
                                         gchar               **bcc,
328
                                         gchar               **subject,
329
                                         gchar               **body);
330

    
331
/* return static strings */
332
const gchar *get_home_dir                (void);
333
const gchar *get_rc_dir                        (void);
334
const gchar *get_news_cache_dir                (void);
335
const gchar *get_imap_cache_dir                (void);
336
const gchar *get_mime_tmp_dir                (void);
337
const gchar *get_template_dir                (void);
338
const gchar *get_tmp_dir                (void);
339
gchar *get_tmp_file                        (void);
340
const gchar *get_domain_name                (void);
341

    
342
/* file / directory handling */
343
off_t get_file_size                (const gchar        *file);
344
off_t get_file_size_as_crlf        (const gchar        *file);
345
off_t get_left_file_size        (FILE                *fp);
346

    
347
gboolean file_exist                (const gchar        *file,
348
                                 gboolean         allow_fifo);
349
gboolean is_dir_exist                (const gchar        *dir);
350
gboolean is_file_entry_exist        (const gchar        *file);
351
gboolean dirent_is_regular_file        (struct dirent        *d);
352
gboolean dirent_is_directory        (struct dirent        *d);
353

    
354
#define is_file_exist(file)                file_exist(file, FALSE)
355
#define is_file_or_fifo_exist(file)        file_exist(file, TRUE)
356

    
357
gint change_dir                        (const gchar        *dir);
358
gint make_dir                        (const gchar        *dir);
359
gint make_dir_hier                (const gchar        *dir);
360
gint remove_all_files                (const gchar        *dir);
361
gint remove_numbered_files        (const gchar        *dir,
362
                                 guint                 first,
363
                                 guint                 last);
364
gint remove_all_numbered_files        (const gchar        *dir);
365
gint remove_expired_files        (const gchar        *dir,
366
                                 guint                 hours);
367
gint remove_dir_recursive        (const gchar        *dir);
368
gint copy_file                        (const gchar        *src,
369
                                 const gchar        *dest,
370
                                 gboolean         keep_backup);
371
gint move_file                        (const gchar        *src,
372
                                 const gchar        *dest,
373
                                 gboolean         overwrite);
374
gint copy_file_part                (FILE                *fp,
375
                                 off_t                 offset,
376
                                 size_t                 length,
377
                                 const gchar        *dest);
378

    
379
gchar *canonicalize_str                (const gchar        *str);
380
gint canonicalize_file                (const gchar        *src,
381
                                 const gchar        *dest);
382
gint canonicalize_file_replace        (const gchar        *file);
383
gint uncanonicalize_file        (const gchar        *src,
384
                                 const gchar        *dest);
385
gint uncanonicalize_file_replace(const gchar        *file);
386

    
387
gchar *normalize_newlines        (const gchar        *str);
388

    
389
gchar *get_outgoing_rfc2822_str        (FILE                *fp);
390
gchar *generate_mime_boundary        (const gchar        *prefix);
391

    
392
gint change_file_mode_rw        (FILE                *fp,
393
                                 const gchar        *file);
394
FILE *my_tmpfile                (void);
395
FILE *str_open_as_stream        (const gchar        *str);
396
gint str_write_to_file                (const gchar        *str,
397
                                 const gchar        *file);
398
gchar *file_read_to_str                (const gchar        *file);
399
gchar *file_read_stream_to_str        (FILE                *fp);
400

    
401
/* process execution */
402
gint execute_async                (gchar *const         argv[]);
403
gint execute_sync                (gchar *const         argv[]);
404
gint execute_command_line        (const gchar        *cmdline,
405
                                 gboolean         async);
406
gchar *get_command_output        (const gchar        *cmdline);
407

    
408
/* open URI with external browser */
409
gint open_uri(const gchar *uri, const gchar *cmdline);
410

    
411
/* time functions */
412
time_t remote_tzoffset_sec        (const gchar        *zone);
413
time_t tzoffset_sec                (time_t                *now);
414
gchar *tzoffset                        (time_t                *now);
415
void get_rfc822_date                (gchar                *buf,
416
                                 gint                 len);
417

    
418
/* logging */
419
void set_log_file        (const gchar *filename);
420
void close_log_file        (void);
421
void log_verbosity_set        (gboolean verbose);
422
void debug_print        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
423
void log_print                (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
424
void log_message        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
425
void log_warning        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
426
void log_error                (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
427

    
428
#endif /* __UTILS_H__ */