Statistics
| Revision:

root / libsylph / utils.h @ 3386

History | View | Annotate | Download (15.6 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
#if defined(G_OS_WIN32) && !defined(_WIN64) && defined(HAVE_64BIT_TIME_T)
99
  /* for backward binary compatibility. Use only in struct definition and
100
     pointer arguments. */
101
  typedef gint32 stime_t;
102
#else
103
  typedef time_t stime_t;
104
#endif
105

    
106
#ifndef BIG_ENDIAN_HOST
107
  #if (G_BYTE_ORDER == G_BIG_ENDIAN)
108
    #define BIG_ENDIAN_HOST 1
109
  #endif
110
#endif
111

    
112
#define CHDIR_RETURN_IF_FAIL(dir) \
113
{ \
114
        if (change_dir(dir) < 0) return; \
115
}
116

    
117
#define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
118
{ \
119
        if (change_dir(dir) < 0) return val; \
120
}
121

    
122
#define Xalloca(ptr, size, iffail) \
123
{ \
124
        if ((ptr = alloca(size)) == NULL) { \
125
                g_warning("can't allocate memory\n"); \
126
                iffail; \
127
        } \
128
}
129

    
130
#define Xstrdup_a(ptr, str, iffail) \
131
{ \
132
        gchar *__tmp; \
133
 \
134
        if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
135
                g_warning("can't allocate memory\n"); \
136
                iffail; \
137
        } else \
138
                strcpy(__tmp, str); \
139
 \
140
        ptr = __tmp; \
141
}
142

    
143
#define Xstrndup_a(ptr, str, len, iffail) \
144
{ \
145
        gchar *__tmp; \
146
 \
147
        if ((__tmp = alloca(len + 1)) == NULL) { \
148
                g_warning("can't allocate memory\n"); \
149
                iffail; \
150
        } else { \
151
                strncpy(__tmp, str, len); \
152
                __tmp[len] = '\0'; \
153
        } \
154
 \
155
        ptr = __tmp; \
156
}
157

    
158
#define Xstrcat_a(ptr, str1, str2, iffail) \
159
{ \
160
        gchar *__tmp; \
161
        gint len1, len2; \
162
 \
163
        len1 = strlen(str1); \
164
        len2 = strlen(str2); \
165
        if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
166
                g_warning("can't allocate memory\n"); \
167
                iffail; \
168
        } else { \
169
                memcpy(__tmp, str1, len1); \
170
                memcpy(__tmp + len1, str2, len2 + 1); \
171
        } \
172
 \
173
        ptr = __tmp; \
174
}
175

    
176
#define AUTORELEASE_STR(str, iffail) \
177
{ \
178
        gchar *__str; \
179
        Xstrdup_a(__str, str, iffail); \
180
        g_free(str); \
181
        str = __str; \
182
}
183

    
184
#define FILE_OP_ERROR(file, func) \
185
{ \
186
        fprintf(stderr, "%s: ", file); \
187
        fflush(stderr); \
188
        perror(func); \
189
}
190

    
191
typedef void (*UIUpdateFunc)                (void);
192
typedef void (*EventLoopFunc)                (void);
193
typedef void (*ProgressFunc)                (gint                 cur,
194
                                         gint                 total);
195
typedef gchar * (*QueryPasswordFunc)        (const gchar        *server,
196
                                         const gchar        *user);
197
typedef void (*LogFunc)                        (const gchar        *str);
198
typedef void (*LogFlushFunc)                (void);
199

    
200
/* for macro expansion */
201
#define Str(x)        #x
202
#define Xstr(x)        Str(x)
203

    
204
void list_free_strings                (GList                *list);
205
void slist_free_strings                (GSList                *list);
206

    
207
void hash_free_strings                (GHashTable        *table);
208
void hash_free_value_mem        (GHashTable        *table);
209

    
210
gint str_case_equal                (gconstpointer         v,
211
                                 gconstpointer         v2);
212
guint str_case_hash                (gconstpointer         key);
213

    
214
void ptr_array_free_strings        (GPtrArray        *array);
215

    
216
typedef gboolean (*StrFindFunc) (const gchar        *haystack,
217
                                 const gchar        *needle);
218

    
219
gboolean str_find                (const gchar        *haystack,
220
                                 const gchar        *needle);
221
gboolean str_case_find                (const gchar        *haystack,
222
                                 const gchar        *needle);
223
gboolean str_find_equal                (const gchar        *haystack,
224
                                 const gchar        *needle);
225
gboolean str_case_find_equal        (const gchar        *haystack,
226
                                 const gchar        *needle);
227

    
228
/* number-string conversion */
229
gint to_number                        (const gchar *nstr);
230
guint to_unumber                (const gchar *nstr);
231
gchar *itos_buf                        (gchar             *nstr,
232
                                 gint              n);
233
gchar *itos                        (gint              n);
234
gchar *utos_buf                        (gchar             *nstr,
235
                                 guint              n);
236
gchar *to_human_readable_buf        (gchar             *buf,
237
                                 size_t              bufsize,
238
                                 gint64              size);
239
gchar *to_human_readable        (gint64              size);
240

    
241
/* alternative string functions */
242
gint strcmp2                (const gchar        *s1,
243
                         const gchar        *s2);
244
gint path_cmp                (const gchar        *s1,
245
                         const gchar        *s2);
246
gboolean is_path_parent        (const gchar        *parent,
247
                         const gchar        *child);
248

    
249
gchar *strretchomp        (gchar                *str);
250
gchar *strtailchomp        (gchar                *str,
251
                         gchar                 tail_char);
252
gchar *strcrchomp        (gchar                *str);
253

    
254
gchar *strcasestr        (const gchar        *haystack,
255
                         const gchar        *needle);
256
gpointer my_memmem        (gconstpointer         haystack,
257
                         size_t                 haystacklen,
258
                         gconstpointer         needle,
259
                         size_t                 needlelen);
260

    
261
gchar *strncpy2                (gchar                *dest,
262
                         const gchar        *src,
263
                         size_t                 n);
264

    
265
gboolean str_has_suffix_case        (const gchar        *str,
266
                                 const gchar        *suffix);
267

    
268
gint str_find_format_times        (const gchar        *haystack,
269
                                 gchar                 ch);
270

    
271
gboolean is_next_nonascii        (const gchar        *s);
272
gint get_next_word_len                (const gchar        *s);
273

    
274
/* functions for string parsing */
275
gint subject_compare                        (const gchar        *s1,
276
                                         const gchar        *s2);
277
gint subject_compare_for_sort                (const gchar        *s1,
278
                                         const gchar        *s2);
279
void trim_subject_for_compare                (gchar                *str);
280
void trim_subject_for_sort                (gchar                *str);
281
void trim_subject                        (gchar                *str);
282
void eliminate_parenthesis                (gchar                *str,
283
                                         gchar                 op,
284
                                         gchar                 cl);
285
void extract_parenthesis                (gchar                *str,
286
                                         gchar                 op,
287
                                         gchar                 cl);
288
void extract_parenthesis_with_escape        (gchar                *str,
289
                                         gchar                 op,
290
                                         gchar                 cl);
291

    
292
void extract_parenthesis_with_skip_quote        (gchar                *str,
293
                                                 gchar                 quote_chr,
294
                                                 gchar                 op,
295
                                                 gchar                 cl);
296

    
297
void eliminate_quote                        (gchar                *str,
298
                                         gchar                 quote_chr);
299
void extract_quote                        (gchar                *str,
300
                                         gchar                 quote_chr);
301
void extract_quote_with_escape                (gchar                *str,
302
                                         gchar                 quote_chr);
303
void eliminate_address_comment                (gchar                *str);
304
gchar *strchr_with_skip_quote                (const gchar        *str,
305
                                         gint                 quote_chr,
306
                                         gint                 c);
307
gchar *strrchr_with_skip_quote                (const gchar        *str,
308
                                         gint                 quote_chr,
309
                                         gint                 c);
310
void extract_address                        (gchar                *str);
311
void extract_list_id_str                (gchar                *str);
312

    
313
gchar *extract_addresses                (const gchar        *str);
314

    
315
gchar *normalize_address_field                (const gchar        *str);
316

    
317
gboolean address_equal                        (const gchar        *addr1,
318
                                         const gchar        *addr2);
319

    
320
GSList *address_list_append_orig        (GSList                *addr_list,
321
                                         const gchar        *str);
322
GSList *address_list_append                (GSList                *addr_list,
323
                                         const gchar        *str);
324
GSList *references_list_prepend                (GSList                *msgid_list,
325
                                         const gchar        *str);
326
GSList *references_list_append                (GSList                *msgid_list,
327
                                         const gchar        *str);
328
GSList *newsgroup_list_append                (GSList                *group_list,
329
                                         const gchar        *str);
330

    
331
GList *add_history                        (GList                *list,
332
                                         const gchar        *str);
333

    
334
/* modify string */
335
void remove_return                        (gchar                *str);
336
void remove_space                        (gchar                *str);
337
void unfold_line                        (gchar                *str);
338
void subst_char                                (gchar                *str,
339
                                         gchar                 orig,
340
                                         gchar                 subst);
341
void subst_chars                        (gchar                *str,
342
                                         gchar                *orig,
343
                                         gchar                 subst);
344
void subst_null                                (gchar                *str,
345
                                         gint                 len,
346
                                         gchar                 subst);
347
void subst_control                        (gchar                *str,
348
                                         gchar                 subst);
349
void subst_for_filename                        (gchar                *str);
350

    
351
gchar *get_alt_filename                        (const gchar        *filename,
352
                                         gint                 count);
353

    
354
gboolean is_header_line                        (const gchar        *str);
355
gboolean is_ascii_str                        (const gchar        *str);
356

    
357
gint get_quote_level                        (const gchar        *str);
358
gint check_line_length                        (const gchar        *str,
359
                                         gint                 max_chars,
360
                                         gint                *line);
361

    
362
gchar *strstr_with_skip_quote                (const gchar        *haystack,
363
                                         const gchar        *needle);
364
gchar *strcasestr_with_skip_quote        (const gchar        *haystack,
365
                                         const gchar        *needle);
366
gchar *strchr_parenthesis_close                (const gchar        *str,
367
                                         gchar                 op,
368
                                         gchar                 cl);
369

    
370
gchar **strsplit_parenthesis                (const gchar        *str,
371
                                         gchar                 op,
372
                                         gchar                 cl,
373
                                         gint                 max_tokens);
374
gchar **strsplit_with_quote                (const gchar        *str,
375
                                         const gchar        *delim,
376
                                         gint                 max_tokens);
377
gchar **strsplit_csv                        (const gchar        *str,
378
                                         gchar                 delim,
379
                                         gint                 max_tokens);
380

    
381
gchar *get_abbrev_newsgroup_name        (const gchar        *group,
382
                                         gint                 len);
383
gchar *trim_string                        (const gchar        *str,
384
                                         gint                 len);
385
gchar *trim_string_before                (const gchar        *str,
386
                                         gint                 len);
387

    
388
GList *uri_list_extract_filenames        (const gchar        *uri_list);
389
gboolean is_uri_string                        (const gchar        *str);
390
gchar *get_uri_path                        (const gchar        *uri);
391
gint get_uri_len                        (const gchar        *str);
392
void decode_uri                                (gchar                *decoded_uri,
393
                                         const gchar        *encoded_uri);
394
void decode_xdigit_encoded_str                (gchar                *decoded,
395
                                         const gchar        *encoded);
396
gchar *encode_uri                        (const gchar        *filename);
397
gchar *uriencode_for_filename                (const gchar        *filename);
398
gchar *uriencode_for_mailto                (const gchar        *mailto);
399
gint scan_mailto_url                        (const gchar        *mailto,
400
                                         gchar               **to,
401
                                         gchar               **cc,
402
                                         gchar               **bcc,
403
                                         gchar               **subject,
404
                                         gchar               **inreplyto,
405
                                         gchar               **body);
406

    
407
void set_startup_dir                        (void);
408
void set_rc_dir                                (const gchar        *dir);
409

    
410
/* return static strings */
411
const gchar *get_startup_dir                (void);
412
const gchar *get_home_dir                (void);
413
const gchar *get_document_dir                (void);
414
const gchar *get_rc_dir                        (void);
415
const gchar *get_old_rc_dir                (void);
416
const gchar *get_mail_base_dir                (void);
417
const gchar *get_news_cache_dir                (void);
418
const gchar *get_imap_cache_dir                (void);
419
const gchar *get_mime_tmp_dir                (void);
420
const gchar *get_template_dir                (void);
421
const gchar *get_tmp_dir                (void);
422
gchar *get_tmp_file                        (void);
423
const gchar *get_domain_name                (void);
424

    
425
/* file / directory handling */
426
off_t get_file_size                (const gchar        *file);
427
off_t get_file_size_as_crlf        (const gchar        *file);
428
off_t get_left_file_size        (FILE                *fp);
429

    
430
gint get_last_empty_line_size        (FILE                *fp,
431
                                 off_t                 size);
432

    
433
gboolean file_exist                (const gchar        *file,
434
                                 gboolean         allow_fifo);
435
gboolean is_dir_exist                (const gchar        *dir);
436
gboolean is_file_entry_exist        (const gchar        *file);
437
gboolean dirent_is_regular_file        (struct dirent        *d);
438
gboolean dirent_is_directory        (struct dirent        *d);
439

    
440
#define is_file_exist(file)                file_exist(file, FALSE)
441
#define is_file_or_fifo_exist(file)        file_exist(file, TRUE)
442

    
443
gint change_dir                        (const gchar        *dir);
444
gint make_dir                        (const gchar        *dir);
445
gint make_dir_hier                (const gchar        *dir);
446
gint remove_all_files                (const gchar        *dir);
447
gint remove_numbered_files        (const gchar        *dir,
448
                                 guint                 first,
449
                                 guint                 last);
450
gint remove_all_numbered_files        (const gchar        *dir);
451
gint remove_expired_files        (const gchar        *dir,
452
                                 guint                 hours);
453
gint remove_dir_recursive        (const gchar        *dir);
454
gint rename_force                (const gchar        *oldpath,
455
                                 const gchar        *newpath);
456
gint copy_file                        (const gchar        *src,
457
                                 const gchar        *dest,
458
                                 gboolean         keep_backup);
459
gint copy_dir                        (const gchar        *src,
460
                                 const gchar        *dest);
461
gint move_file                        (const gchar        *src,
462
                                 const gchar        *dest,
463
                                 gboolean         overwrite);
464

    
465
gint append_file_part                (FILE                *fp,
466
                                 off_t                 offset,
467
                                 size_t                 length,
468
                                 FILE                *dest_fp);
469
gint copy_file_part                (FILE                *fp,
470
                                 off_t                 offset,
471
                                 size_t                 length,
472
                                 const gchar        *dest);
473

    
474
gint copy_file_stream                (FILE                *fp,
475
                                 FILE                *dest_fp);
476

    
477
gchar *canonicalize_str                (const gchar        *str);
478
gint canonicalize_file                (const gchar        *src,
479
                                 const gchar        *dest);
480
gint canonicalize_file_replace        (const gchar        *file);
481
FILE *canonicalize_file_stream        (FILE                *fp,
482
                                 gint                *length);
483
gint uncanonicalize_file        (const gchar        *src,
484
                                 const gchar        *dest);
485
gint uncanonicalize_file_replace(const gchar        *file);
486

    
487
gchar *normalize_newlines        (const gchar        *str);
488
gchar *strchomp_all                (const gchar        *str);
489

    
490
FILE *get_outgoing_rfc2822_file        (FILE                *fp);
491
gchar *get_outgoing_rfc2822_str        (FILE                *fp);
492
gchar *generate_mime_boundary        (const gchar        *prefix);
493

    
494
gint change_file_mode_rw        (FILE                *fp,
495
                                 const gchar        *file);
496
FILE *my_tmpfile                (void);
497
FILE *str_open_as_stream        (const gchar        *str);
498
gint str_write_to_file                (const gchar        *str,
499
                                 const gchar        *file);
500
gchar *file_read_to_str                (const gchar        *file);
501
gchar *file_read_stream_to_str        (FILE                *fp);
502

    
503
/* process execution */
504
gint execute_async                (gchar *const         argv[]);
505
gint execute_sync                (gchar *const         argv[]);
506
gint execute_command_line        (const gchar        *cmdline,
507
                                 gboolean         async);
508
gint execute_command_line_async_wait
509
                                (const gchar        *cmdline);
510

    
511
gint execute_open_file                (const gchar        *file,
512
                                 const gchar        *content_type);
513
gint execute_print_file                (const gchar        *file);
514
gchar *get_command_output        (const gchar        *cmdline);
515

    
516
/* open URI with external browser */
517
gint open_uri                        (const gchar        *uri,
518
                                 const gchar        *cmdline);
519

    
520
/* play sound */
521
gint play_sound                        (const gchar        *file,
522
                                 gboolean         async);
523

    
524
/* time functions */
525
stime_t remote_tzoffset_sec        (const gchar        *zone);
526
stime_t tzoffset_sec                (stime_t        *now);
527
gchar *tzoffset_buf                (gchar                *buf,
528
                                 stime_t        *now);
529
gchar *tzoffset                        (stime_t        *now);
530
void get_rfc822_date                (gchar                *buf,
531
                                 gint                 len);
532

    
533
size_t my_strftime                (gchar                        *s,
534
                                 size_t                         max,
535
                                 const gchar                *format,
536
                                 const struct tm        *tm);
537

    
538
/* UI hints */
539
void set_ui_update_func        (UIUpdateFunc         func);
540
void ui_update                (void);
541

    
542
void set_event_loop_func        (EventLoopFunc         func);
543
void event_loop_iterate                (void);
544

    
545
void set_progress_func        (ProgressFunc         func);
546
void progress_show        (gint                 cur,
547
                         gint                 total);
548

    
549
/* user input */
550
void set_input_query_password_func        (QueryPasswordFunc        func);
551

    
552
gchar *input_query_password        (const gchar        *server,
553
                                 const gchar        *user);
554

    
555
/* logging */
556
void set_log_file        (const gchar        *filename);
557
void close_log_file        (void);
558
void set_log_verbosity        (gboolean         verbose);
559
gboolean get_debug_mode        (void);
560
void set_debug_mode        (gboolean         enable);
561

    
562
void set_log_ui_func                (LogFunc         print_func,
563
                                 LogFunc         message_func,
564
                                 LogFunc         warning_func,
565
                                 LogFunc         error_func);
566
void set_log_ui_func_full        (LogFunc         print_func,
567
                                 LogFunc         message_func,
568
                                 LogFunc         warning_func,
569
                                 LogFunc         error_func,
570
                                 LogFlushFunc         flush_func);
571

    
572
void set_log_show_status_func        (LogFunc         status_func);
573

    
574
void debug_print        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
575
void status_print        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
576

    
577
void log_write                (const gchar        *str,
578
                         const gchar        *prefix);
579

    
580
void log_print                (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
581
void log_message        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
582
void log_warning        (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
583
void log_error                (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
584

    
585
void log_flush                (void);
586

    
587
#endif /* __UTILS_H__ */