Statistics
| Revision:

root / libsylph / utils.h @ 3245

History | View | Annotate | Download (15.4 KB)

1
/*
2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2013 Hiroyuki Yamamoto
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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

    
39
/* Wrappers for C library function that take pathname arguments. */
40
#if GLIB_CHECK_VERSION(2, 6, 0)
41
#  include <glib/gstdio.h>
42
#else
43

    
44
#define g_open                open
45
#define g_rename        rename
46
#define g_mkdir                mkdir
47
#define g_stat                stat
48
#define g_lstat                lstat
49
#define g_unlink        unlink
50
#define g_remove        remove
51
#define g_rmdir                rmdir
52
#define g_fopen                fopen
53
#define g_freopen        freopen
54

    
55
#endif /* GLIB_CHECK_VERSION */
56

    
57
#if !GLIB_CHECK_VERSION(2, 7, 0)
58

    
59
#ifdef G_OS_UNIX
60
#define g_chdir                chdir
61
#define g_chmod                chmod
62
#else
63
gint g_chdir        (const gchar        *path);
64
gint g_chmod        (const gchar        *path,
65
                 gint                 mode);
66
#endif /* G_OS_UNIX */
67

    
68
#endif /* !GLIB_CHECK_VERSION */
69

    
70
#ifdef G_OS_UNIX
71
#define syl_link        link
72
#else
73
gint syl_link        (const gchar        *src,
74
                 const gchar        *dest);
75
#endif
76

    
77
/* The AC_CHECK_SIZEOF() in configure fails for some machines.
78
 * we provide some fallback values here */
79
#if !SIZEOF_UNSIGNED_SHORT
80
  #undef SIZEOF_UNSIGNED_SHORT
81
  #define SIZEOF_UNSIGNED_SHORT 2
82
#endif
83
#if !SIZEOF_UNSIGNED_INT
84
  #undef SIZEOF_UNSIGNED_INT
85
  #define SIZEOF_UNSIGNED_INT 4
86
#endif
87
#if !SIZEOF_UNSIGNED_LONG
88
  #undef SIZEOF_UNSIGNED_LONG
89
  #define SIZEOF_UNSIGNED_LONG 4
90
#endif
91

    
92
#ifndef HAVE_U32_TYPEDEF
93
  #undef u32            /* maybe there is a macro with this name */
94
  typedef guint32 u32;
95
  #define HAVE_U32_TYPEDEF
96
#endif
97

    
98
#ifndef BIG_ENDIAN_HOST
99
  #if (G_BYTE_ORDER == G_BIG_ENDIAN)
100
    #define BIG_ENDIAN_HOST 1
101
  #endif
102
#endif
103

    
104
#define CHDIR_RETURN_IF_FAIL(dir) \
105
{ \
106
        if (change_dir(dir) < 0) return; \
107
}
108

    
109
#define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
110
{ \
111
        if (change_dir(dir) < 0) return val; \
112
}
113

    
114
#define Xalloca(ptr, size, iffail) \
115
{ \
116
        if ((ptr = alloca(size)) == NULL) { \
117
                g_warning("can't allocate memory\n"); \
118
                iffail; \
119
        } \
120
}
121

    
122
#define Xstrdup_a(ptr, str, iffail) \
123
{ \
124
        gchar *__tmp; \
125
 \
126
        if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
127
                g_warning("can't allocate memory\n"); \
128
                iffail; \
129
        } else \
130
                strcpy(__tmp, str); \
131
 \
132
        ptr = __tmp; \
133
}
134

    
135
#define Xstrndup_a(ptr, str, len, iffail) \
136
{ \
137
        gchar *__tmp; \
138
 \
139
        if ((__tmp = alloca(len + 1)) == NULL) { \
140
                g_warning("can't allocate memory\n"); \
141
                iffail; \
142
        } else { \
143
                strncpy(__tmp, str, len); \
144
                __tmp[len] = '\0'; \
145
        } \
146
 \
147
        ptr = __tmp; \
148
}
149

    
150
#define Xstrcat_a(ptr, str1, str2, iffail) \
151
{ \
152
        gchar *__tmp; \
153
        gint len1, len2; \
154
 \
155
        len1 = strlen(str1); \
156
        len2 = strlen(str2); \
157
        if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
158
                g_warning("can't allocate memory\n"); \
159
                iffail; \
160
        } else { \
161
                memcpy(__tmp, str1, len1); \
162
                memcpy(__tmp + len1, str2, len2 + 1); \
163
        } \
164
 \
165
        ptr = __tmp; \
166
}
167

    
168
#define AUTORELEASE_STR(str, iffail) \
169
{ \
170
        gchar *__str; \
171
        Xstrdup_a(__str, str, iffail); \
172
        g_free(str); \
173
        str = __str; \
174
}
175

    
176
#define FILE_OP_ERROR(file, func) \
177
{ \
178
        fprintf(stderr, "%s: ", file); \
179
        fflush(stderr); \
180
        perror(func); \
181
}
182

    
183
typedef void (*UIUpdateFunc)                (void);
184
typedef void (*EventLoopFunc)                (void);
185
typedef void (*ProgressFunc)                (gint                 cur,
186
                                         gint                 total);
187
typedef gchar * (*QueryPasswordFunc)        (const gchar        *server,
188
                                         const gchar        *user);
189
typedef void (*LogFunc)                        (const gchar        *str);
190
typedef void (*LogFlushFunc)                (void);
191

    
192
/* for macro expansion */
193
#define Str(x)        #x
194
#define Xstr(x)        Str(x)
195

    
196
void list_free_strings                (GList                *list);
197
void slist_free_strings                (GSList                *list);
198

    
199
void hash_free_strings                (GHashTable        *table);
200
void hash_free_value_mem        (GHashTable        *table);
201

    
202
gint str_case_equal                (gconstpointer         v,
203
                                 gconstpointer         v2);
204
guint str_case_hash                (gconstpointer         key);
205

    
206
void ptr_array_free_strings        (GPtrArray        *array);
207

    
208
typedef gboolean (*StrFindFunc) (const gchar        *haystack,
209
                                 const gchar        *needle);
210

    
211
gboolean str_find                (const gchar        *haystack,
212
                                 const gchar        *needle);
213
gboolean str_case_find                (const gchar        *haystack,
214
                                 const gchar        *needle);
215
gboolean str_find_equal                (const gchar        *haystack,
216
                                 const gchar        *needle);
217
gboolean str_case_find_equal        (const gchar        *haystack,
218
                                 const gchar        *needle);
219

    
220
/* number-string conversion */
221
gint to_number                        (const gchar *nstr);
222
guint to_unumber                (const gchar *nstr);
223
gchar *itos_buf                        (gchar             *nstr,
224
                                 gint              n);
225
gchar *itos                        (gint              n);
226
gchar *utos_buf                        (gchar             *nstr,
227
                                 guint              n);
228
gchar *to_human_readable_buf        (gchar             *buf,
229
                                 size_t              bufsize,
230
                                 gint64              size);
231
gchar *to_human_readable        (gint64              size);
232

    
233
/* alternative string functions */
234
gint strcmp2                (const gchar        *s1,
235
                         const gchar        *s2);
236
gint path_cmp                (const gchar        *s1,
237
                         const gchar        *s2);
238
gboolean is_path_parent        (const gchar        *parent,
239
                         const gchar        *child);
240

    
241
gchar *strretchomp        (gchar                *str);
242
gchar *strtailchomp        (gchar                *str,
243
                         gchar                 tail_char);
244
gchar *strcrchomp        (gchar                *str);
245

    
246
gchar *strcasestr        (const gchar        *haystack,
247
                         const gchar        *needle);
248
gpointer my_memmem        (gconstpointer         haystack,
249
                         size_t                 haystacklen,
250
                         gconstpointer         needle,
251
                         size_t                 needlelen);
252

    
253
gchar *strncpy2                (gchar                *dest,
254
                         const gchar        *src,
255
                         size_t                 n);
256

    
257
gboolean str_has_suffix_case        (const gchar        *str,
258
                                 const gchar        *suffix);
259

    
260
gint str_find_format_times        (const gchar        *haystack,
261
                                 gchar                 ch);
262

    
263
gboolean is_next_nonascii        (const gchar        *s);
264
gint get_next_word_len                (const gchar        *s);
265

    
266
/* functions for string parsing */
267
gint subject_compare                        (const gchar        *s1,
268
                                         const gchar        *s2);
269
gint subject_compare_for_sort                (const gchar        *s1,
270
                                         const gchar        *s2);
271
void trim_subject_for_compare                (gchar                *str);
272
void trim_subject_for_sort                (gchar                *str);
273
void trim_subject                        (gchar                *str);
274
void eliminate_parenthesis                (gchar                *str,
275
                                         gchar                 op,
276
                                         gchar                 cl);
277
void extract_parenthesis                (gchar                *str,
278
                                         gchar                 op,
279
                                         gchar                 cl);
280
void extract_parenthesis_with_escape        (gchar                *str,
281
                                         gchar                 op,
282
                                         gchar                 cl);
283

    
284
void extract_parenthesis_with_skip_quote        (gchar                *str,
285
                                                 gchar                 quote_chr,
286
                                                 gchar                 op,
287
                                                 gchar                 cl);
288

    
289
void eliminate_quote                        (gchar                *str,
290
                                         gchar                 quote_chr);
291
void extract_quote                        (gchar                *str,
292
                                         gchar                 quote_chr);
293
void extract_quote_with_escape                (gchar                *str,
294
                                         gchar                 quote_chr);
295
void eliminate_address_comment                (gchar                *str);
296
gchar *strchr_with_skip_quote                (const gchar        *str,
297
                                         gint                 quote_chr,
298
                                         gint                 c);
299
gchar *strrchr_with_skip_quote                (const gchar        *str,
300
                                         gint                 quote_chr,
301
                                         gint                 c);
302
void extract_address                        (gchar                *str);
303
void extract_list_id_str                (gchar                *str);
304

    
305
gchar *extract_addresses                (const gchar        *str);
306

    
307
gchar *normalize_address_field                (const gchar        *str);
308

    
309
gboolean address_equal                        (const gchar        *addr1,
310
                                         const gchar        *addr2);
311

    
312
GSList *address_list_append_orig        (GSList                *addr_list,
313
                                         const gchar        *str);
314
GSList *address_list_append                (GSList                *addr_list,
315
                                         const gchar        *str);
316
GSList *references_list_prepend                (GSList                *msgid_list,
317
                                         const gchar        *str);
318
GSList *references_list_append                (GSList                *msgid_list,
319
                                         const gchar        *str);
320
GSList *newsgroup_list_append                (GSList                *group_list,
321
                                         const gchar        *str);
322

    
323
GList *add_history                        (GList                *list,
324
                                         const gchar        *str);
325

    
326
/* modify string */
327
void remove_return                        (gchar                *str);
328
void remove_space                        (gchar                *str);
329
void unfold_line                        (gchar                *str);
330
void subst_char                                (gchar                *str,
331
                                         gchar                 orig,
332
                                         gchar                 subst);
333
void subst_chars                        (gchar                *str,
334
                                         gchar                *orig,
335
                                         gchar                 subst);
336
void subst_null                                (gchar                *str,
337
                                         gint                 len,
338
                                         gchar                 subst);
339
void subst_control                        (gchar                *str,
340
                                         gchar                 subst);
341
void subst_for_filename                        (gchar                *str);
342

    
343
gchar *get_alt_filename                        (const gchar        *filename,
344
                                         gint                 count);
345

    
346
gboolean is_header_line                        (const gchar        *str);
347
gboolean is_ascii_str                        (const gchar        *str);
348

    
349
gint get_quote_level                        (const gchar        *str);
350
gint check_line_length                        (const gchar        *str,
351
                                         gint                 max_chars,
352
                                         gint                *line);
353

    
354
gchar *strstr_with_skip_quote                (const gchar        *haystack,
355
                                         const gchar        *needle);
356
gchar *strcasestr_with_skip_quote        (const gchar        *haystack,
357
                                         const gchar        *needle);
358
gchar *strchr_parenthesis_close                (const gchar        *str,
359
                                         gchar                 op,
360
                                         gchar                 cl);
361

    
362
gchar **strsplit_parenthesis                (const gchar        *str,
363
                                         gchar                 op,
364
                                         gchar                 cl,
365
                                         gint                 max_tokens);
366
gchar **strsplit_with_quote                (const gchar        *str,
367
                                         const gchar        *delim,
368
                                         gint                 max_tokens);
369
gchar **strsplit_csv                        (const gchar        *str,
370
                                         gchar                 delim,
371
                                         gint                 max_tokens);
372

    
373
gchar *get_abbrev_newsgroup_name        (const gchar        *group,
374
                                         gint                 len);
375
gchar *trim_string                        (const gchar        *str,
376
                                         gint                 len);
377
gchar *trim_string_before                (const gchar        *str,
378
                                         gint                 len);
379

    
380
GList *uri_list_extract_filenames        (const gchar        *uri_list);
381
gboolean is_uri_string                        (const gchar        *str);
382
gchar *get_uri_path                        (const gchar        *uri);
383
gint get_uri_len                        (const gchar        *str);
384
void decode_uri                                (gchar                *decoded_uri,
385
                                         const gchar        *encoded_uri);
386
void decode_xdigit_encoded_str                (gchar                *decoded,
387
                                         const gchar        *encoded);
388
gchar *encode_uri                        (const gchar        *filename);
389
gchar *uriencode_for_filename                (const gchar        *filename);
390
gchar *uriencode_for_mailto                (const gchar        *mailto);
391
gint scan_mailto_url                        (const gchar        *mailto,
392
                                         gchar               **to,
393
                                         gchar               **cc,
394
                                         gchar               **bcc,
395
                                         gchar               **subject,
396
                                         gchar               **inreplyto,
397
                                         gchar               **body);
398

    
399
void set_startup_dir                        (void);
400
void set_rc_dir                                (const gchar        *dir);
401

    
402
/* return static strings */
403
const gchar *get_startup_dir                (void);
404
const gchar *get_home_dir                (void);
405
const gchar *get_document_dir                (void);
406
const gchar *get_rc_dir                        (void);
407
const gchar *get_old_rc_dir                (void);
408
const gchar *get_mail_base_dir                (void);
409
const gchar *get_news_cache_dir                (void);
410
const gchar *get_imap_cache_dir                (void);
411
const gchar *get_mime_tmp_dir                (void);
412
const gchar *get_template_dir                (void);
413
const gchar *get_tmp_dir                (void);
414
gchar *get_tmp_file                        (void);
415
const gchar *get_domain_name                (void);
416

    
417
/* file / directory handling */
418
off_t get_file_size                (const gchar        *file);
419
off_t get_file_size_as_crlf        (const gchar        *file);
420
off_t get_left_file_size        (FILE                *fp);
421

    
422
gint get_last_empty_line_size        (FILE                *fp,
423
                                 off_t                 size);
424

    
425
gboolean file_exist                (const gchar        *file,
426
                                 gboolean         allow_fifo);
427
gboolean is_dir_exist                (const gchar        *dir);
428
gboolean is_file_entry_exist        (const gchar        *file);
429
gboolean dirent_is_regular_file        (struct dirent        *d);
430
gboolean dirent_is_directory        (struct dirent        *d);
431

    
432
#define is_file_exist(file)                file_exist(file, FALSE)
433
#define is_file_or_fifo_exist(file)        file_exist(file, TRUE)
434

    
435
gint change_dir                        (const gchar        *dir);
436
gint make_dir                        (const gchar        *dir);
437
gint make_dir_hier                (const gchar        *dir);
438
gint remove_all_files                (const gchar        *dir);
439
gint remove_numbered_files        (const gchar        *dir,
440
                                 guint                 first,
441
                                 guint                 last);
442
gint remove_all_numbered_files        (const gchar        *dir);
443
gint remove_expired_files        (const gchar        *dir,
444
                                 guint                 hours);
445
gint remove_dir_recursive        (const gchar        *dir);
446
gint rename_force                (const gchar        *oldpath,
447
                                 const gchar        *newpath);
448
gint copy_file                        (const gchar        *src,
449
                                 const gchar        *dest,
450
                                 gboolean         keep_backup);
451
gint copy_dir                        (const gchar        *src,
452
                                 const gchar        *dest);
453
gint move_file                        (const gchar        *src,
454
                                 const gchar        *dest,
455
                                 gboolean         overwrite);
456

    
457
gint append_file_part                (FILE                *fp,
458
                                 off_t                 offset,
459
                                 size_t                 length,
460
                                 FILE                *dest_fp);
461
gint copy_file_part                (FILE                *fp,
462
                                 off_t                 offset,
463
                                 size_t                 length,
464
                                 const gchar        *dest);
465

    
466
gint copy_file_stream                (FILE                *fp,
467
                                 FILE                *dest_fp);
468

    
469
gchar *canonicalize_str                (const gchar        *str);
470
gint canonicalize_file                (const gchar        *src,
471
                                 const gchar        *dest);
472
gint canonicalize_file_replace        (const gchar        *file);
473
FILE *canonicalize_file_stream        (FILE                *fp,
474
                                 gint                *length);
475
gint uncanonicalize_file        (const gchar        *src,
476
                                 const gchar        *dest);
477
gint uncanonicalize_file_replace(const gchar        *file);
478

    
479
gchar *normalize_newlines        (const gchar        *str);
480
gchar *strchomp_all                (const gchar        *str);
481

    
482
FILE *get_outgoing_rfc2822_file        (FILE                *fp);
483
gchar *get_outgoing_rfc2822_str        (FILE                *fp);
484
gchar *generate_mime_boundary        (const gchar        *prefix);
485

    
486
gint change_file_mode_rw        (FILE                *fp,
487
                                 const gchar        *file);
488
FILE *my_tmpfile                (void);
489
FILE *str_open_as_stream        (const gchar        *str);
490
gint str_write_to_file                (const gchar        *str,
491
                                 const gchar        *file);
492
gchar *file_read_to_str                (const gchar        *file);
493
gchar *file_read_stream_to_str        (FILE                *fp);
494

    
495
/* process execution */
496
gint execute_async                (gchar *const         argv[]);
497
gint execute_sync                (gchar *const         argv[]);
498
gint execute_command_line        (const gchar        *cmdline,
499
                                 gboolean         async);
500
gint execute_command_line_async_wait
501
                                (const gchar        *cmdline);
502

    
503
gint execute_open_file                (const gchar        *file,
504
                                 const gchar        *content_type);
505
gint execute_print_file                (const gchar        *file);
506
gchar *get_command_output        (const gchar        *cmdline);
507

    
508
/* open URI with external browser */
509
gint open_uri                        (const gchar        *uri,
510
                                 const gchar        *cmdline);
511

    
512
/* play sound */
513
gint play_sound                        (const gchar        *file,
514
                                 gboolean         async);
515

    
516
/* time functions */
517
time_t remote_tzoffset_sec        (const gchar        *zone);
518
time_t tzoffset_sec                (time_t                *now);
519
gchar *tzoffset_buf                (gchar                *buf,
520
                                 time_t                *now);
521
gchar *tzoffset                        (time_t                *now);
522
void get_rfc822_date                (gchar                *buf,
523
                                 gint                 len);
524

    
525
size_t my_strftime                (gchar                        *s,
526
                                 size_t                         max,
527
                                 const gchar                *format,
528
                                 const struct tm        *tm);
529

    
530
/* UI hints */
531
void set_ui_update_func        (UIUpdateFunc         func);
532
void ui_update                (void);
533

    
534
void set_event_loop_func        (EventLoopFunc         func);
535
void event_loop_iterate                (void);
536

    
537
void set_progress_func        (ProgressFunc         func);
538
void progress_show        (gint                 cur,
539
                         gint                 total);
540

    
541
/* user input */
542
void set_input_query_password_func        (QueryPasswordFunc        func);
543

    
544
gchar *input_query_password        (const gchar        *server,
545
                                 const gchar        *user);
546

    
547
/* logging */
548
void set_log_file        (const gchar        *filename);
549
void close_log_file        (void);
550
void set_log_verbosity        (gboolean         verbose);
551
gboolean get_debug_mode        (void);
552
void set_debug_mode        (gboolean         enable);
553

    
554
void set_log_ui_func                (LogFunc         print_func,
555
                                 LogFunc         message_func,
556
                                 LogFunc         warning_func,
557
                                 LogFunc         error_func);
558
void set_log_ui_func_full        (LogFunc         print_func,
559
                                 LogFunc         message_func,
560
                                 LogFunc         warning_func,
561
                                 LogFunc         error_func,
562
                                 LogFlushFunc         flush_func);
563

    
564
void set_log_show_status_func        (LogFunc         status_func);
565

    
566
void debug_print        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
567
void status_print        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
568

    
569
void log_write                (const gchar        *str,
570
                         const gchar        *prefix);
571

    
572
void log_print                (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
573
void log_message        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
574
void log_warning        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
575
void log_error                (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
576

    
577
void log_flush                (void);
578

    
579
#endif /* __UTILS_H__ */