summaryrefslogtreecommitdiff
path: root/libsylph/utils.h
diff options
context:
space:
mode:
authorSimeon Simeonov2018-02-26 11:23:00 +0100
committerSimeon Simeonov2018-02-26 11:23:00 +0100
commit0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch)
treeaf916a30553c78ce9d4f2d8658656a175c5b6921 /libsylph/utils.h
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'libsylph/utils.h')
-rw-r--r--libsylph/utils.h599
1 files changed, 599 insertions, 0 deletions
diff --git a/libsylph/utils.h b/libsylph/utils.h
new file mode 100644
index 0000000..9ac65cf
--- /dev/null
+++ b/libsylph/utils.h
@@ -0,0 +1,599 @@
1/*
2 * LibSylph -- E-Mail client library
3 * Copyright (C) 1999-2017 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
63gint g_chdir (const gchar *path);
64gint 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
73gint 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#if !GLIB_CHECK_VERSION(2, 26, 0)
107#if (defined (_MSC_VER) || defined (__MINGW32__)) && !defined(_WIN64)
108 typedef struct _stat32 GStatBuf;
109#else
110 typedef struct stat GStatBuf;
111#endif
112#endif
113
114#ifndef BIG_ENDIAN_HOST
115 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
116 #define BIG_ENDIAN_HOST 1
117 #endif
118#endif
119
120#define CHDIR_RETURN_IF_FAIL(dir) \
121{ \
122 if (change_dir(dir) < 0) return; \
123}
124
125#define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
126{ \
127 if (change_dir(dir) < 0) return val; \
128}
129
130#define Xalloca(ptr, size, iffail) \
131{ \
132 if ((ptr = alloca(size)) == NULL) { \
133 g_warning("can't allocate memory\n"); \
134 iffail; \
135 } \
136}
137
138#define Xstrdup_a(ptr, str, iffail) \
139{ \
140 gchar *__tmp; \
141 \
142 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
143 g_warning("can't allocate memory\n"); \
144 iffail; \
145 } else \
146 strcpy(__tmp, str); \
147 \
148 ptr = __tmp; \
149}
150
151#define Xstrndup_a(ptr, str, len, iffail) \
152{ \
153 gchar *__tmp; \
154 \
155 if ((__tmp = alloca(len + 1)) == NULL) { \
156 g_warning("can't allocate memory\n"); \
157 iffail; \
158 } else { \
159 strncpy(__tmp, str, len); \
160 __tmp[len] = '\0'; \
161 } \
162 \
163 ptr = __tmp; \
164}
165
166#define Xstrcat_a(ptr, str1, str2, iffail) \
167{ \
168 gchar *__tmp; \
169 gint len1, len2; \
170 \
171 len1 = strlen(str1); \
172 len2 = strlen(str2); \
173 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
174 g_warning("can't allocate memory\n"); \
175 iffail; \
176 } else { \
177 memcpy(__tmp, str1, len1); \
178 memcpy(__tmp + len1, str2, len2 + 1); \
179 } \
180 \
181 ptr = __tmp; \
182}
183
184#define AUTORELEASE_STR(str, iffail) \
185{ \
186 gchar *__str; \
187 Xstrdup_a(__str, str, iffail); \
188 g_free(str); \
189 str = __str; \
190}
191
192#define FILE_OP_ERROR(file, func) \
193{ \
194 fprintf(stderr, "%s: ", file); \
195 fflush(stderr); \
196 perror(func); \
197}
198
199typedef void (*UIUpdateFunc) (void);
200typedef void (*EventLoopFunc) (void);
201typedef void (*ProgressFunc) (gint cur,
202 gint total);
203typedef gchar * (*QueryPasswordFunc) (const gchar *server,
204 const gchar *user);
205typedef void (*LogFunc) (const gchar *str);
206typedef void (*LogFlushFunc) (void);
207
208/* for macro expansion */
209#define Str(x) #x
210#define Xstr(x) Str(x)
211
212void list_free_strings (GList *list);
213void slist_free_strings (GSList *list);
214
215void hash_free_strings (GHashTable *table);
216void hash_free_value_mem (GHashTable *table);
217
218gint str_case_equal (gconstpointer v,
219 gconstpointer v2);
220guint str_case_hash (gconstpointer key);
221
222void ptr_array_free_strings (GPtrArray *array);
223
224typedef gboolean (*StrFindFunc) (const gchar *haystack,
225 const gchar *needle);
226
227gboolean str_find (const gchar *haystack,
228 const gchar *needle);
229gboolean str_case_find (const gchar *haystack,
230 const gchar *needle);
231gboolean str_find_equal (const gchar *haystack,
232 const gchar *needle);
233gboolean str_case_find_equal (const gchar *haystack,
234 const gchar *needle);
235
236/* number-string conversion */
237gint to_number (const gchar *nstr);
238guint to_unumber (const gchar *nstr);
239gchar *itos_buf (gchar *nstr,
240 gint n);
241gchar *itos (gint n);
242gchar *utos_buf (gchar *nstr,
243 guint n);
244gchar *to_human_readable_buf (gchar *buf,
245 size_t bufsize,
246 gint64 size);
247gchar *to_human_readable (gint64 size);
248
249/* alternative string functions */
250gint strcmp2 (const gchar *s1,
251 const gchar *s2);
252gint path_cmp (const gchar *s1,
253 const gchar *s2);
254gboolean is_path_parent (const gchar *parent,
255 const gchar *child);
256
257gchar *strretchomp (gchar *str);
258gchar *strtailchomp (gchar *str,
259 gchar tail_char);
260gchar *strcrchomp (gchar *str);
261
262gchar *strcasestr (const gchar *haystack,
263 const gchar *needle);
264gpointer my_memmem (gconstpointer haystack,
265 size_t haystacklen,
266 gconstpointer needle,
267 size_t needlelen);
268
269gchar *strncpy2 (gchar *dest,
270 const gchar *src,
271 size_t n);
272
273gboolean str_has_suffix_case (const gchar *str,
274 const gchar *suffix);
275
276gint str_find_format_times (const gchar *haystack,
277 gchar ch);
278
279gboolean is_next_nonascii (const gchar *s);
280gint get_next_word_len (const gchar *s);
281
282/* functions for string parsing */
283gint subject_compare (const gchar *s1,
284 const gchar *s2);
285gint subject_compare_for_sort (const gchar *s1,
286 const gchar *s2);
287void trim_subject_for_compare (gchar *str);
288void trim_subject_for_sort (gchar *str);
289void trim_subject (gchar *str);
290void eliminate_parenthesis (gchar *str,
291 gchar op,
292 gchar cl);
293void extract_parenthesis (gchar *str,
294 gchar op,
295 gchar cl);
296void extract_parenthesis_with_escape (gchar *str,
297 gchar op,
298 gchar cl);
299
300void extract_parenthesis_with_skip_quote (gchar *str,
301 gchar quote_chr,
302 gchar op,
303 gchar cl);
304
305void eliminate_quote (gchar *str,
306 gchar quote_chr);
307void extract_quote (gchar *str,
308 gchar quote_chr);
309void extract_quote_with_escape (gchar *str,
310 gchar quote_chr);
311void eliminate_address_comment (gchar *str);
312gchar *strchr_with_skip_quote (const gchar *str,
313 gint quote_chr,
314 gint c);
315gchar *strrchr_with_skip_quote (const gchar *str,
316 gint quote_chr,
317 gint c);
318void extract_address (gchar *str);
319void extract_list_id_str (gchar *str);
320
321gchar *extract_addresses (const gchar *str);
322
323gchar *normalize_address_field (const gchar *str);
324
325gboolean address_equal (const gchar *addr1,
326 const gchar *addr2);
327
328GSList *address_list_append_orig (GSList *addr_list,
329 const gchar *str);
330GSList *address_list_append (GSList *addr_list,
331 const gchar *str);
332GSList *references_list_prepend (GSList *msgid_list,
333 const gchar *str);
334GSList *references_list_append (GSList *msgid_list,
335 const gchar *str);
336GSList *newsgroup_list_append (GSList *group_list,
337 const gchar *str);
338
339GList *add_history (GList *list,
340 const gchar *str);
341
342/* modify string */
343void remove_return (gchar *str);
344void remove_space (gchar *str);
345void unfold_line (gchar *str);
346void subst_char (gchar *str,
347 gchar orig,
348 gchar subst);
349void subst_chars (gchar *str,
350 gchar *orig,
351 gchar subst);
352void subst_null (gchar *str,
353 gint len,
354 gchar subst);
355void subst_control (gchar *str,
356 gchar subst);
357void subst_for_filename (gchar *str);
358
359gchar *get_alt_filename (const gchar *filename,
360 gint count);
361
362gboolean is_header_line (const gchar *str);
363gboolean is_ascii_str (const gchar *str);
364
365gint get_quote_level (const gchar *str);
366gint check_line_length (const gchar *str,
367 gint max_chars,
368 gint *line);
369
370gchar *strstr_with_skip_quote (const gchar *haystack,
371 const gchar *needle);
372gchar *strcasestr_with_skip_quote (const gchar *haystack,
373 const gchar *needle);
374gchar *strchr_parenthesis_close (const gchar *str,
375 gchar op,
376 gchar cl);
377
378gchar **strsplit_parenthesis (const gchar *str,
379 gchar op,
380 gchar cl,
381 gint max_tokens);
382gchar **strsplit_with_quote (const gchar *str,
383 const gchar *delim,
384 gint max_tokens);
385gchar **strsplit_csv (const gchar *str,
386 gchar delim,
387 gint max_tokens);
388
389gchar *strconcat_csv (gchar delim,
390 const gchar *field1,
391 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
392
393gchar *get_abbrev_newsgroup_name (const gchar *group,
394 gint len);
395gchar *trim_string (const gchar *str,
396 gint len);
397gchar *trim_string_before (const gchar *str,
398 gint len);
399
400GList *uri_list_extract_filenames (const gchar *uri_list);
401gboolean is_uri_string (const gchar *str);
402gchar *get_uri_path (const gchar *uri);
403gint get_uri_len (const gchar *str);
404void decode_uri (gchar *decoded_uri,
405 const gchar *encoded_uri);
406void decode_xdigit_encoded_str (gchar *decoded,
407 const gchar *encoded);
408gchar *encode_uri (const gchar *filename);
409gchar *uriencode_for_filename (const gchar *filename);
410gchar *uriencode_for_mailto (const gchar *mailto);
411gint scan_mailto_url (const gchar *mailto,
412 gchar **to,
413 gchar **cc,
414 gchar **bcc,
415 gchar **subject,
416 gchar **inreplyto,
417 gchar **body);
418
419void set_startup_dir (void);
420void set_rc_dir (const gchar *dir);
421
422/* return static strings */
423const gchar *get_startup_dir (void);
424const gchar *get_home_dir (void);
425const gchar *get_document_dir (void);
426const gchar *get_rc_dir (void);
427const gchar *get_old_rc_dir (void);
428const gchar *get_mail_base_dir (void);
429const gchar *get_news_cache_dir (void);
430const gchar *get_imap_cache_dir (void);
431const gchar *get_mime_tmp_dir (void);
432const gchar *get_template_dir (void);
433const gchar *get_tmp_dir (void);
434gchar *get_tmp_file (void);
435const gchar *get_domain_name (void);
436
437/* file / directory handling */
438off_t get_file_size (const gchar *file);
439off_t get_file_size_as_crlf (const gchar *file);
440off_t get_left_file_size (FILE *fp);
441
442gint get_last_empty_line_size (FILE *fp,
443 off_t size);
444
445gboolean file_exist (const gchar *file,
446 gboolean allow_fifo);
447gboolean is_dir_exist (const gchar *dir);
448gboolean is_file_entry_exist (const gchar *file);
449gboolean dirent_is_regular_file (struct dirent *d);
450gboolean dirent_is_directory (struct dirent *d);
451
452#define is_file_exist(file) file_exist(file, FALSE)
453#define is_file_or_fifo_exist(file) file_exist(file, TRUE)
454
455gint change_dir (const gchar *dir);
456gint make_dir (const gchar *dir);
457gint make_dir_hier (const gchar *dir);
458gint remove_all_files (const gchar *dir);
459gint remove_numbered_files (const gchar *dir,
460 guint first,
461 guint last);
462gint remove_all_numbered_files (const gchar *dir);
463gint remove_expired_files (const gchar *dir,
464 guint hours);
465gint remove_dir_recursive (const gchar *dir);
466gint rename_force (const gchar *oldpath,
467 const gchar *newpath);
468gint copy_file (const gchar *src,
469 const gchar *dest,
470 gboolean keep_backup);
471gint copy_dir (const gchar *src,
472 const gchar *dest);
473gint move_file (const gchar *src,
474 const gchar *dest,
475 gboolean overwrite);
476
477gint append_file_part (FILE *fp,
478 off_t offset,
479 size_t length,
480 FILE *dest_fp);
481gint copy_file_part (FILE *fp,
482 off_t offset,
483 size_t length,
484 const gchar *dest);
485
486gint copy_file_stream (FILE *fp,
487 FILE *dest_fp);
488
489gchar *canonicalize_str (const gchar *str);
490gint canonicalize_file (const gchar *src,
491 const gchar *dest);
492gint canonicalize_file_replace (const gchar *file);
493FILE *canonicalize_file_stream (FILE *fp,
494 gint *length);
495gint uncanonicalize_file (const gchar *src,
496 const gchar *dest);
497gint uncanonicalize_file_replace(const gchar *file);
498
499gchar *normalize_newlines (const gchar *str);
500gchar *strchomp_all (const gchar *str);
501
502FILE *get_outgoing_rfc2822_file (FILE *fp);
503gchar *get_outgoing_rfc2822_str (FILE *fp);
504gchar *generate_mime_boundary (const gchar *prefix);
505
506gint change_file_mode_rw (FILE *fp,
507 const gchar *file);
508FILE *my_tmpfile (void);
509FILE *str_open_as_stream (const gchar *str);
510gint str_write_to_file (const gchar *str,
511 const gchar *file);
512gchar *file_read_to_str (const gchar *file);
513gchar *file_read_stream_to_str (FILE *fp);
514
515/* process execution */
516gint execute_async (gchar *const argv[]);
517gint execute_sync (gchar *const argv[]);
518gint execute_command_line (const gchar *cmdline,
519 gboolean async);
520gint execute_command_line_async_wait
521 (const gchar *cmdline);
522
523gint execute_open_file (const gchar *file,
524 const gchar *content_type);
525gint execute_print_file (const gchar *file);
526gchar *get_command_output (const gchar *cmdline);
527
528/* open URI with external browser */
529gint open_uri (const gchar *uri,
530 const gchar *cmdline);
531
532/* play sound */
533gint play_sound (const gchar *file,
534 gboolean async);
535
536/* time functions */
537stime_t remote_tzoffset_sec (const gchar *zone);
538stime_t tzoffset_sec (stime_t *now);
539gchar *tzoffset_buf (gchar *buf,
540 stime_t *now);
541gchar *tzoffset (stime_t *now);
542void get_rfc822_date (gchar *buf,
543 gint len);
544
545size_t my_strftime (gchar *s,
546 size_t max,
547 const gchar *format,
548 const struct tm *tm);
549
550/* UI hints */
551void set_ui_update_func (UIUpdateFunc func);
552void ui_update (void);
553
554void set_event_loop_func (EventLoopFunc func);
555void event_loop_iterate (void);
556
557void set_progress_func (ProgressFunc func);
558void progress_show (gint cur,
559 gint total);
560
561/* user input */
562void set_input_query_password_func (QueryPasswordFunc func);
563
564gchar *input_query_password (const gchar *server,
565 const gchar *user);
566
567/* logging */
568void set_log_file (const gchar *filename);
569void close_log_file (void);
570void set_log_verbosity (gboolean verbose);
571gboolean get_debug_mode (void);
572void set_debug_mode (gboolean enable);
573
574void set_log_ui_func (LogFunc print_func,
575 LogFunc message_func,
576 LogFunc warning_func,
577 LogFunc error_func);
578void set_log_ui_func_full (LogFunc print_func,
579 LogFunc message_func,
580 LogFunc warning_func,
581 LogFunc error_func,
582 LogFlushFunc flush_func);
583
584void set_log_show_status_func (LogFunc status_func);
585
586void debug_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
587void status_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
588
589void log_write (const gchar *str,
590 const gchar *prefix);
591
592void log_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
593void log_message (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
594void log_warning (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
595void log_error (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
596
597void log_flush (void);
598
599#endif /* __UTILS_H__ */