Statistics
| Branch: | Tag: | Revision:

root / libsylph / utils.h @ aebfd4cc

History | View | Annotate | Download (11.4 KB)

1
/*
2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2011 Hiroyuki Yamamoto
4
 */
5

    
6
#ifndef __UTILS_H__
7
#define __UTILS_H__
8

    
9
#ifdef HAVE_CONFIG_H
10
#  include "config.h"
11
#endif
12

    
13
#include <glib.h>
14
#include <stdio.h>
15
#include <string.h>
16
#include <stdlib.h>
17
#include <unistd.h>
18
#include <sys/types.h>
19
#include <dirent.h>
20
#include <time.h>
21
#if HAVE_ALLOCA_H
22
#  include <alloca.h>
23
#endif
24

    
25
/* Wrappers for C library function that take pathname arguments. */
26
#if GLIB_CHECK_VERSION(2, 6, 0)
27
#  include <glib/gstdio.h>
28
#else
29

    
30
#define g_open                open
31
#define g_rename        rename
32
#define g_mkdir                mkdir
33
#define g_stat                stat
34
#define g_lstat                lstat
35
#define g_unlink        unlink
36
#define g_remove        remove
37
#define g_rmdir                rmdir
38
#define g_fopen                fopen
39
#define g_freopen        freopen
40

    
41
#endif /* GLIB_CHECK_VERSION */
42

    
43
#if !GLIB_CHECK_VERSION(2, 7, 0)
44

    
45
#ifdef G_OS_UNIX
46
#define g_chdir                chdir
47
#define g_chmod                chmod
48
#else
49
gint g_chdir        (const gchar        *path);
50
gint g_chmod        (const gchar        *path,
51
                 gint                 mode);
52
#endif /* G_OS_UNIX */
53

    
54
#endif /* !GLIB_CHECK_VERSION */
55

    
56
#ifdef G_OS_UNIX
57
#define syl_link        link
58
#else
59
gint syl_link        (const gchar        *src,
60
                 const gchar        *dest);
61
#endif
62

    
63
/* The AC_CHECK_SIZEOF() in configure fails for some machines.
64
 * we provide some fallback values here */
65
#if !SIZEOF_UNSIGNED_SHORT
66
  #undef SIZEOF_UNSIGNED_SHORT
67
  #define SIZEOF_UNSIGNED_SHORT 2
68
#endif
69
#if !SIZEOF_UNSIGNED_INT
70
  #undef SIZEOF_UNSIGNED_INT
71
  #define SIZEOF_UNSIGNED_INT 4
72
#endif
73
#if !SIZEOF_UNSIGNED_LONG
74
  #undef SIZEOF_UNSIGNED_LONG
75
  #define SIZEOF_UNSIGNED_LONG 4
76
#endif
77

    
78
#ifndef HAVE_U32_TYPEDEF
79
  #undef u32            /* maybe there is a macro with this name */
80
  typedef guint32 u32;
81
  #define HAVE_U32_TYPEDEF
82
#endif
83

    
84
#ifndef BIG_ENDIAN_HOST
85
  #if (G_BYTE_ORDER == G_BIG_ENDIAN)
86
    #define BIG_ENDIAN_HOST 1
87
  #endif
88
#endif
89

    
90
#define CHDIR_RETURN_IF_FAIL(dir) \
91
{ \
92
        if (change_dir(dir) < 0) return; \
93
}
94

    
95
#define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
96
{ \
97
        if (change_dir(dir) < 0) return val; \
98
}
99

    
100
#define Xalloca(ptr, size, iffail) \
101
{ \
102
        if ((ptr = alloca(size)) == NULL) { \
103
                g_warning("can't allocate memory\n"); \
104
                iffail; \
105
        } \
106
}
107

    
108
#define Xstrdup_a(ptr, str, iffail) \
109
{ \
110
        gchar *__tmp; \
111
 \
112
        if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
113
                g_warning("can't allocate memory\n"); \
114
                iffail; \
115
        } else \
116
                strcpy(__tmp, str); \
117
 \
118
        ptr = __tmp; \
119
}
120

    
121
#define Xstrndup_a(ptr, str, len, iffail) \
122
{ \
123
        gchar *__tmp; \
124
 \
125
        if ((__tmp = alloca(len + 1)) == NULL) { \
126
                g_warning("can't allocate memory\n"); \
127
                iffail; \
128
        } else { \
129
                strncpy(__tmp, str, len); \
130
                __tmp[len] = '\0'; \
131
        } \
132
 \
133
        ptr = __tmp; \
134
}
135

    
136
#define Xstrcat_a(ptr, str1, str2, iffail) \
137
{ \
138
        gchar *__tmp; \
139
        gint len1, len2; \
140
 \
141
        len1 = strlen(str1); \
142
        len2 = strlen(str2); \
143
        if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
144
                g_warning("can't allocate memory\n"); \
145
                iffail; \
146
        } else { \
147
                memcpy(__tmp, str1, len1); \
148
                memcpy(__tmp + len1, str2, len2 + 1); \
149
        } \
150
 \
151
        ptr = __tmp; \
152
}
153

    
154
#define AUTORELEASE_STR(str, iffail) \
155
{ \
156
        gchar *__str; \
157
        Xstrdup_a(__str, str, iffail); \
158
        g_free(str); \
159
        str = __str; \
160
}
161

    
162
#define FILE_OP_ERROR(file, func) \
163
{ \
164
        fprintf(stderr, "%s: ", file); \
165
        fflush(stderr); \
166
        perror(func); \
167
}
168

    
169
/* for macro expansion */
170
#define Str(x)        #x
171
#define Xstr(x)        Str(x)
172

    
173
void list_free_strings                (GList                *list);
174
void slist_free_strings                (GSList                *list);
175

    
176
void hash_free_strings                (GHashTable        *table);
177
void hash_free_value_mem        (GHashTable        *table);
178

    
179
gint str_case_equal                (gconstpointer         v,
180
                                 gconstpointer         v2);
181
guint str_case_hash                (gconstpointer         key);
182

    
183
void ptr_array_free_strings        (GPtrArray        *array);
184

    
185
typedef gboolean (*StrFindFunc) (const gchar        *haystack,
186
                                 const gchar        *needle);
187

    
188
gboolean str_find                (const gchar        *haystack,
189
                                 const gchar        *needle);
190
gboolean str_case_find                (const gchar        *haystack,
191
                                 const gchar        *needle);
192
gboolean str_find_equal                (const gchar        *haystack,
193
                                 const gchar        *needle);
194
gboolean str_case_find_equal        (const gchar        *haystack,
195
                                 const gchar        *needle);
196

    
197
/* number-string conversion */
198
gint to_number                        (const gchar *nstr);
199
guint to_unumber                (const gchar *nstr);
200
gchar *itos_buf                        (gchar             *nstr,
201
                                 gint              n);
202
gchar *itos                        (gint              n);
203
gchar *utos_buf                        (gchar             *nstr,
204
                                 guint              n);
205
gchar *to_human_readable_buf        (gchar             *buf,
206
                                 size_t              bufsize,
207
                                 gint64              size);
208
gchar *to_human_readable        (gint64              size);
209

    
210
/* alternative string functions */
211
gint strcmp2                (const gchar        *s1,
212
                         const gchar        *s2);
213
gint path_cmp                (const gchar        *s1,
214
                         const gchar        *s2);
215
gboolean is_path_parent        (const gchar        *parent,
216
                         const gchar        *child);
217

    
218
gchar *strretchomp        (gchar                *str);
219
gchar *strtailchomp        (gchar                *str,
220
                         gchar                 tail_char);
221
gchar *strcrchomp        (gchar                *str);
222

    
223
gchar *strcasestr        (const gchar        *haystack,
224
                         const gchar        *needle);
225
gpointer my_memmem        (gconstpointer         haystack,
226
                         size_t                 haystacklen,
227
                         gconstpointer         needle,
228
                         size_t                 needlelen);
229

    
230
gchar *strncpy2                (gchar                *dest,
231
                         const gchar        *src,
232
                         size_t                 n);
233

    
234
gboolean str_has_suffix_case        (const gchar        *str,
235
                                 const gchar        *suffix);
236

    
237
gint str_find_format_times        (const gchar        *haystack,
238
                                 gchar                 ch);
239

    
240
gboolean is_next_nonascii        (const gchar        *s);
241
gint get_next_word_len                (const gchar        *s);
242

    
243
/* functions for string parsing */
244
gint subject_compare                        (const gchar        *s1,
245
                                         const gchar        *s2);
246
gint subject_compare_for_sort                (const gchar        *s1,
247
                                         const gchar        *s2);
248
void trim_subject_for_compare                (gchar                *str);
249
void trim_subject_for_sort                (gchar                *str);
250
void trim_subject                        (gchar                *str);
251
void eliminate_parenthesis                (gchar                *str,
252
                                         gchar                 op,
253
                                         gchar                 cl);
254
void extract_parenthesis                (gchar                *str,
255
                                         gchar                 op,
256
                                         gchar                 cl);
257
void extract_parenthesis_with_escape        (gchar                *str,
258
                                         gchar                 op,
259
                                         gchar                 cl);
260

    
261
void extract_parenthesis_with_skip_quote        (gchar                *str,
262
                                                 gchar                 quote_chr,
263
                                                 gchar                 op,
264
                                                 gchar                 cl);
265

    
266
void eliminate_quote                        (gchar                *str,
267
                                         gchar                 quote_chr);
268
void extract_quote                        (gchar                *str,
269
                                         gchar                 quote_chr);
270
void extract_quote_with_escape                (gchar                *str,
271
                                         gchar                 quote_chr);
272
void eliminate_address_comment                (gchar                *str);
273
gchar *strchr_with_skip_quote                (const gchar        *str,
274
                                         gint                 quote_chr,
275
                                         gint                 c);
276
gchar *strrchr_with_skip_quote                (const gchar        *str,
277
                                         gint                 quote_chr,
278
                                         gint                 c);
279
void extract_address                        (gchar                *str);
280
void extract_list_id_str                (gchar                *str);
281

    
282
gchar *extract_addresses                (const gchar        *str);
283

    
284
gchar *normalize_address_field                (const gchar        *str);
285

    
286
gboolean address_equal                        (const gchar        *addr1,
287
                                         const gchar        *addr2);
288

    
289
GSList *address_list_append_orig        (GSList                *addr_list,
290
                                         const gchar        *str);
291
GSList *address_list_append                (GSList                *addr_list,
292
                                         const gchar        *str);
293
GSList *references_list_prepend                (GSList                *msgid_list,
294
                                         const gchar        *str);
295
GSList *references_list_append                (GSList                *msgid_list,
296
                                         const gchar        *str);
297
GSList *newsgroup_list_append                (GSList                *group_list,
298
                                         const gchar        *str);
299

    
300
GList *add_history                        (GList                *list,
301
                                         const gchar        *str);
302

    
303
/* modify string */
304
void remove_return                        (gchar                *str);
305
void remove_space                        (gchar                *str);
306
void unfold_line                        (gchar                *str);
307
void subst_char                                (gchar                *str,
308
                                         gchar                 orig,
309
                                         gchar                 subst);
310
void subst_chars                        (gchar                *str,
311
                                         gchar                *orig,
312
                                         gchar                 subst);
313
void subst_null                                (gchar                *str,
314
                                         gint                 len,
315
                                         gchar                 subst);
316
void subst_control                        (gchar                *str,
317
                                         gchar                 subst);
318
void subst_for_filename                        (gchar                *str);
319

    
320
gchar *get_alt_filename                        (const gchar        *filename,
321
                                         gint                 count);
322

    
323
gboolean is_header_line                        (const gchar        *str);
324
gboolean is_ascii_str                        (const gchar        *str);
325

    
326
gint get_quote_level                        (const gchar        *str);
327
gint check_line_length                        (const gchar        *str,
328
                                         gint                 max_chars,
329
                                         gint                *line);
330

    
331
gchar *strstr_with_skip_quote                (const gchar        *haystack,
332
                                         const gchar        *needle);
333
gchar *strcasestr_with_skip_quote        (const gchar        *haystack,
334
                                         const gchar        *needle);
335
gchar *strchr_parenthesis_close                (const gchar        *str,
336
                                         gchar                 op,
337
                                         gchar                 cl);
338

    
339
gchar **strsplit_parenthesis                (const gchar        *str,
340
                                         gchar                 op,
341
                                         gchar                 cl,
342
                                         gint                 max_tokens);
343
gchar **strsplit_with_quote                (const gchar        *str,
344
                                         const gchar        *delim,
345
                                         gint                 max_tokens);
346
gchar **strsplit_csv                        (const gchar        *str,
347
                                         gchar                 delim,
348
                                         gint                 max_tokens);
349

    
350
gchar *get_abbrev_newsgroup_name        (const gchar        *group,
351
                                         gint                 len);
352
gchar *trim_string                        (const gchar        *str,
353
                                         gint                 len);
354
gchar *trim_string_before                (const gchar        *str,
355
                                         gint                 len);
356

    
357
GList *uri_list_extract_filenames        (const gchar        *uri_list);
358
gboolean is_uri_string                        (const gchar        *str);
359
gchar *get_uri_path                        (const gchar        *uri);
360
gint get_uri_len                        (const gchar        *str);
361
void decode_uri                                (gchar                *decoded_uri,
362
                                         const gchar        *encoded_uri);
363
void decode_xdigit_encoded_str                (gchar                *decoded,
364
                                         const gchar        *encoded);
365
gchar *encode_uri                        (const gchar        *filename);
366
gchar *uriencode_for_filename                (const gchar        *filename);
367
gchar *uriencode_for_mailto                (const gchar        *mailto);
368
gint scan_mailto_url                        (const gchar        *mailto,
369
                                         gchar               **to,
370
                                         gchar               **cc,
371
                                         gchar               **bcc,
372
                                         gchar               **subject,
373
                                         gchar               **inreplyto,
374
                                         gchar               **body);
375

    
376
void set_startup_dir                        (void);
377
void set_rc_dir                                (const gchar        *dir);
378

    
379
/* return static strings */
380
const gchar *get_startup_dir                (void);
381
const gchar *get_home_dir                (void);
382
const gchar *get_document_dir                (void);
383
const gchar *get_rc_dir                        (void);
384
const gchar *get_old_rc_dir                (void);
385
const gchar *get_mail_base_dir                (void);
386
const gchar *get_news_cache_dir                (void);
387
const gchar *get_imap_cache_dir                (void);
388
const gchar *get_mime_tmp_dir                (void);
389
const gchar *get_template_dir                (void);
390
const gchar *get_tmp_dir                (void);
391
gchar *get_tmp_file                        (void);
392

    
393
/* file / directory handling */
394
off_t get_file_size                (const gchar        *file);
395
off_t get_file_size_as_crlf        (const gchar        *file);
396
off_t get_left_file_size        (FILE                *fp);
397

    
398
gint get_last_empty_line_size        (FILE                *fp,
399
                                 off_t                 size);
400

    
401
gboolean file_exist                (const gchar        *file,
402
                                 gboolean         allow_fifo);
403
gboolean is_dir_exist                (const gchar        *dir);
404
gboolean is_file_entry_exist        (const gchar        *file);
405
gboolean dirent_is_regular_file        (struct dirent        *d);
406
gboolean dirent_is_directory        (struct dirent        *d);
407

    
408
#define is_file_exist(file)                file_exist(file, FALSE)
409
#define is_file_or_fifo_exist(file)        file_exist(file, TRUE)
410

    
411
gint change_dir                        (const gchar        *dir);
412

    
413
gchar *canonicalize_str                (const gchar        *str);
414
gint canonicalize_file                (const gchar        *src,
415
                                 const gchar        *dest);
416
FILE *canonicalize_file_stream        (FILE                *fp,
417
                                 gint                *length);
418
gint uncanonicalize_file        (const gchar        *src,
419
                                 const gchar        *dest);
420

    
421
gchar *normalize_newlines        (const gchar        *str);
422
gchar *strchomp_all                (const gchar        *str);
423

    
424
FILE *get_outgoing_rfc2822_file        (FILE                *fp);
425
gchar *get_outgoing_rfc2822_str        (FILE                *fp);
426
gchar *generate_mime_boundary        (const gchar        *prefix);
427

    
428
gint change_file_mode_rw        (FILE                *fp,
429
                                 const gchar        *file);
430
FILE *my_tmpfile                (void);
431
FILE *str_open_as_stream        (const gchar        *str);
432
gint str_write_to_file                (const gchar        *str,
433
                                 const gchar        *file);
434
gchar *file_read_to_str                (const gchar        *file);
435
gchar *file_read_stream_to_str        (FILE                *fp);
436

    
437
/* time functions */
438
time_t remote_tzoffset_sec        (const gchar        *zone);
439
time_t tzoffset_sec                (time_t                *now);
440
gchar *tzoffset_buf                (gchar                *buf,
441
                                 time_t                *now);
442
gchar *tzoffset                        (time_t                *now);
443
void get_rfc822_date                (gchar                *buf,
444
                                 gint                 len);
445

    
446
size_t my_strftime                (gchar                        *s,
447
                                 size_t                         max,
448
                                 const gchar                *format,
449
                                 const struct tm        *tm);
450

    
451
/* logging */
452
gboolean get_debug_mode        (void);
453
void set_debug_mode        (gboolean         enable);
454

    
455
void debug_print        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
456

    
457
#endif /* __UTILS_H__ */