Remove strstr2() in favour of g_strstr_len()
[claws.git] / src / common / utils.h
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The code of the g_utf8_substring function below is owned by
19  * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20  * and is got from GLIB 2.30
21  */
22
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
25
26 #ifdef HAVE_CONFIG_H
27 #include "claws-features.h"
28 #endif
29
30 #ifdef HAVE_BACKTRACE
31 #include <execinfo.h>
32 #endif
33
34 #include <glib.h>
35 #include <glib-object.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <dirent.h>
42 #include <time.h>
43 #if HAVE_ALLOCA_H
44 #  include <alloca.h>
45 #endif
46 #if HAVE_WCHAR_H
47 #  include <wchar.h>
48 #endif
49
50 /* The Hurd doesn't have this limit */
51 #ifndef PATH_MAX
52   #define PATH_MAX 4196
53 #endif
54
55 #ifdef G_OS_WIN32
56
57 #define fsync _commit
58
59 #define pipe(phandles)  _pipe (phandles, 4096, _O_BINARY)
60 #endif
61 /* Wrappers for C library function that take pathname arguments. */
62 #  include <glib/gstdio.h>
63
64 /* why is this sometimes undefined !? */
65 #ifndef G_MAXOFFSET
66 typedef gint64 goffset;
67 #define G_MINOFFSET     G_MININT64
68 #define G_MAXOFFSET     G_MAXINT64
69 #endif
70
71 #ifndef BIG_ENDIAN_HOST
72   #if (G_BYTE_ORDER == G_BIG_ENDIAN)
73     #define BIG_ENDIAN_HOST 1
74   #endif
75 #endif
76
77 #define CHDIR_RETURN_IF_FAIL(dir) \
78 { \
79         if (change_dir(dir) < 0) return; \
80 }
81
82 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
83 { \
84         if (change_dir(dir) < 0) return val; \
85 }
86
87 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
88 { \
89         if (change_dir(dir) < 0) { \
90                 code \
91                 return val; \
92         } \
93 }
94
95 #define Xalloca(ptr, size, iffail) \
96 { \
97         if ((ptr = alloca(size)) == NULL) { \
98                 g_warning("can't allocate memory"); \
99                 iffail; \
100         } \
101 }
102
103 #define Xstrdup_a(ptr, str, iffail) \
104 { \
105         gchar *__tmp; \
106  \
107         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
108                 g_warning("can't allocate memory"); \
109                 iffail; \
110         } else \
111                 strcpy(__tmp, str); \
112  \
113         ptr = __tmp; \
114 }
115
116 #define Xstrndup_a(ptr, str, len, iffail) \
117 { \
118         gchar *__tmp; \
119  \
120         if ((__tmp = alloca(len + 1)) == NULL) { \
121                 g_warning("can't allocate memory"); \
122                 iffail; \
123         } else { \
124                 memcpy(__tmp, str, len); \
125                 __tmp[len] = '\0'; \
126         } \
127  \
128         ptr = __tmp; \
129 }
130
131 #define Xstrcat_a(ptr, str1, str2, iffail) \
132 { \
133         gchar *__tmp; \
134         gint len1, len2; \
135  \
136         len1 = strlen(str1); \
137         len2 = strlen(str2); \
138         if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
139                 g_warning("can't allocate memory"); \
140                 iffail; \
141         } else { \
142                 memcpy(__tmp, str1, len1); \
143                 memcpy(__tmp + len1, str2, len2 + 1); \
144         } \
145  \
146         ptr = __tmp; \
147 }
148
149 #define AUTORELEASE_STR(str, iffail) \
150 { \
151         gchar *__str; \
152         Xstrdup_a(__str, str, iffail); \
153         g_free(str); \
154         str = __str; \
155 }
156
157 #define FILE_OP_ERROR(file, func) \
158 { \
159         g_printerr("%s: ", file); \
160         fflush(stderr); \
161         perror(func); \
162 }
163
164 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
165
166 /* from NetworkManager */
167 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
168 #define print_backtrace()                                               \
169 G_STMT_START                                                            \
170 {                                                                       \
171         void *_call_stack[512];                                         \
172         int  _call_stack_size;                                          \
173         char **_symbols;                                                \
174         _call_stack_size = backtrace (_call_stack,                      \
175                                       G_N_ELEMENTS (_call_stack));      \
176         _symbols = backtrace_symbols (_call_stack, _call_stack_size);   \
177         if (_symbols != NULL)                                           \
178         {                                                               \
179                 int _i;                                                 \
180                 _i = 0;                                                 \
181                 g_print ("traceback:\n");                               \
182                 while (_i < _call_stack_size)                           \
183                 {                                                       \
184                         g_print ("%d:\t%s\n", _i, _symbols[_i]);        \
185                         _i++;                                           \
186                 }                                                       \
187                 free (_symbols);                                        \
188         }                                                               \
189 }                                                                       \
190 G_STMT_END
191 #else
192 #define print_backtrace()                                               \
193 G_STMT_START                                                            \
194 {                                                                       \
195 }                                                                       \
196 G_STMT_END
197 #endif
198
199
200 #define cm_return_val_if_fail(expr,val) G_STMT_START {                  \
201         if (!(expr)) {                                                  \
202                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
203                 print_backtrace();                                      \
204                 g_print("\n");                                          \
205                 return val;                                             \
206         }                                                               \
207 } G_STMT_END
208
209 #define cm_return_if_fail(expr) G_STMT_START {                          \
210         if (!(expr)) {                                                  \
211                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
212                 print_backtrace();                                      \
213                 g_print("\n");                                          \
214                 return;                                                 \
215         }                                                               \
216 } G_STMT_END
217
218 #ifndef MIN
219         #define MIN(a, b) ((a) < (b) ? (a) : (b))
220 #endif
221 #ifndef MAX
222         #define MAX(a, b) ((a) > (b) ? (a) : (b))
223 #endif
224
225 #ifdef __cplusplus
226 extern "C" {
227 #endif
228
229 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
230
231 /* debug functions */
232 void debug_set_mode             (gboolean mode);
233 gboolean debug_get_mode         (void);
234
235 #ifndef __CYGWIN__
236 #define debug_print \
237         debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
238         debug_print_real
239 #else
240   /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
241 #define debug_print \
242         debug_print_real("%s:%d:", __FILE__, __LINE__), \
243         debug_print_real
244 #endif
245
246 /* for macro expansion */
247 #define Str(x)  #x
248 #define Xstr(x) Str(x)
249
250 /* List utilities. */
251
252 GSList *slist_copy_deep         (GSList         *list,
253                                  GCopyFunc       func);
254
255 /* String utilities.  */
256
257 void list_free_strings_full             (GList          *list);
258 void slist_free_strings_full    (GSList         *list);
259
260 void hash_free_strings          (GHashTable     *table);
261
262 gint str_case_equal             (gconstpointer   v,
263                                  gconstpointer   v2);
264 guint str_case_hash             (gconstpointer   key);
265
266 /* number-string conversion */
267 gint to_number                  (const gchar *nstr);
268 gchar *itos_buf                 (gchar       *nstr,
269                                  gint         n);
270 gchar *itos                     (gint         n);
271 gchar *to_human_readable        (goffset      size);
272
273 /* alternative string functions */
274 gint path_cmp           (const gchar    *s1,
275                          const gchar    *s2);
276 gchar *strretchomp      (gchar          *str);
277 gchar *strtailchomp     (gchar          *str,
278                          gchar           tail_char);
279 gchar *strcrchomp       (gchar          *str);
280 gchar *strcasestr       (const gchar    *haystack,
281                          const gchar    *needle);
282 gchar *strncasestr      (const gchar    *haystack,
283                          gint            haystack_len,
284                          const gchar    *needle);
285 gpointer my_memmem      (gconstpointer   haystack,
286                          size_t          haystacklen,
287                          gconstpointer   needle,
288                          size_t          needlelen);
289 gchar *strncpy2         (gchar          *dest,
290                          const gchar    *src,
291                          size_t          n);
292
293 gboolean is_next_nonascii       (const gchar *s);
294 gint get_next_word_len          (const gchar *s);
295
296 /* functions for string parsing */
297 gint subject_compare                    (const gchar    *s1,
298                                          const gchar    *s2);
299 gint subject_compare_for_sort           (const gchar    *s1,
300                                          const gchar    *s2);
301 void trim_subject                       (gchar          *str);
302 void eliminate_parenthesis              (gchar          *str,
303                                          gchar           op,
304                                          gchar           cl);
305 void extract_parenthesis                (gchar          *str,
306                                          gchar           op,
307                                          gchar           cl);
308
309 void extract_quote                      (gchar          *str,
310                                          gchar           quote_chr);
311 gchar *escape_internal_quotes           (gchar          *str,
312                                          gchar           quote_chr);
313 void eliminate_address_comment          (gchar          *str);
314 gchar *strchr_with_skip_quote           (const gchar    *str,
315                                          gint            quote_chr,
316                                          gint            c);
317 void extract_address                    (gchar          *str);
318 void extract_list_id_str                (gchar          *str);
319
320 GSList *address_list_append             (GSList         *addr_list,
321                                          const gchar    *str);
322 GSList *address_list_append_with_comments(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 void remove_return                      (gchar          *str);
335 void remove_space                       (gchar          *str);
336 void unfold_line                        (gchar          *str);
337 void subst_char                         (gchar          *str,
338                                          gchar           orig,
339                                          gchar           subst);
340 void subst_chars                        (gchar          *str,   
341                                          gchar          *orig, 
342                                          gchar          subst);
343 void subst_for_filename                 (gchar          *str);
344 void subst_for_shellsafe_filename       (gchar          *str);
345 gboolean is_ascii_str                   (const gchar    *str);
346 gint get_quote_level                    (const gchar    *str,
347                                          const gchar    *quote_chars);
348 gint check_line_length                  (const gchar    *str,
349                                          gint            max_chars,
350                                          gint           *line);
351
352 gchar **strsplit_with_quote             (const gchar    *str,
353                                          const gchar    *delim,
354                                          gint            max_tokens);
355
356 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
357                                          gint            len);
358 gchar *trim_string                      (const gchar    *str,
359                                          gint            len);
360
361 GList *uri_list_extract_filenames       (const gchar    *uri_list);
362 gboolean is_uri_string                  (const gchar    *str);
363 gchar *get_uri_path                     (const gchar    *uri);
364 gint get_uri_len                        (const gchar    *str);
365 void decode_uri                         (gchar          *decoded_uri,
366                                          const gchar    *encoded_uri);
367 void decode_uri_with_plus               (gchar          *decoded_uri, 
368                                          const gchar    *encoded_uri, 
369                                          gboolean        with_plus);
370 gint scan_mailto_url                    (const gchar    *mailto,
371                                          gchar         **from,
372                                          gchar         **to,
373                                          gchar         **cc,
374                                          gchar         **bcc,
375                                          gchar         **subject,
376                                          gchar         **body,
377                                          gchar         ***attach,
378                                          gchar         **inreplyto);
379
380 /* return static strings */
381 const gchar *get_home_dir               (void);
382 const gchar *get_rc_dir                 (void);
383 void  set_rc_dir                        (const gchar *dir);
384 gboolean rc_dir_is_alt                  (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_plugin_dir             (void);
391 const gchar *get_tmp_dir                (void);
392 const gchar *get_locale_dir             (void);
393 gchar *get_tmp_file                     (void);
394 const gchar *get_domain_name            (void);
395 const gchar *get_desktop_file(void);
396 #ifdef G_OS_WIN32
397 const gchar *w32_get_themes_dir    (void);
398 const gchar *w32_get_cert_file          (void);
399 #endif
400 /* file / directory handling */
401 off_t get_file_size             (const gchar    *file);
402 time_t get_file_mtime           (const gchar *file);
403
404 gboolean file_exist             (const gchar    *file,
405                                  gboolean        allow_fifo);
406 gboolean is_relative_filename   (const gchar *file);
407 gboolean is_dir_exist           (const gchar    *dir);
408 gboolean is_file_entry_exist    (const gchar    *file);
409 gboolean dirent_is_regular_file (struct dirent  *d);
410
411 #define is_file_exist(file)             file_exist(file, FALSE)
412 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
413
414 gint change_dir                 (const gchar    *dir);
415 gint make_dir                   (const gchar    *dir);
416 gint make_dir_hier              (const gchar    *dir);
417 gint remove_all_files           (const gchar    *dir);
418 gint remove_numbered_files      (const gchar    *dir,
419                                  guint           first,
420                                  guint           last);
421 gint remove_numbered_files_not_in_list(const gchar *dir,
422                                        GSList *numberlist);
423 gint remove_all_numbered_files  (const gchar    *dir);
424 gint remove_dir_recursive       (const gchar    *dir);
425 gchar *canonicalize_str         (const gchar    *str);
426 gchar *normalize_newlines       (const gchar    *str);
427
428 gchar *get_outgoing_rfc2822_str (FILE           *fp);
429
430 char *fgets_crlf(char *buf, int size, FILE *stream);
431
432 /* process execution */
433 gint execute_command_line       (const gchar    *cmdline,
434                                  gboolean        async,
435                                  const gchar    *working_directory);
436 gchar *get_command_output       (const gchar    *cmdline);
437
438 /* open URI with external browser */
439 gint open_uri(const gchar *uri, const gchar *cmdline);
440 /* open file with text editor */
441 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
442
443 /* time functions */
444 time_t remote_tzoffset_sec      (const gchar    *zone);
445 time_t tzoffset_sec             (time_t         *now);
446 gchar *tzoffset                 (time_t         *now);
447 void get_rfc822_date            (gchar          *buf,
448                                  gint            len);
449 void get_rfc822_date_hide_tz    (gchar          *buf,
450                                  gint            len);
451
452 size_t fast_strftime            (gchar                  *buf, 
453                                  gint                    buflen, 
454                                  const gchar            *format, 
455                                  struct tm              *lt);
456
457 /* debugging */
458 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
459 const char * debug_srcname (const char *file);
460
461 /* subject threading */
462 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
463 void subject_table_insert(GHashTable *subject_table, gchar * subject,
464                           void * data);
465 void subject_table_remove(GHashTable *subject_table, gchar * subject);
466 void utils_free_regex(void);
467 gint subject_get_prefix_length (const gchar *subject);
468
469 /* quoting recognition */
470 const gchar * line_has_quote_char       (const gchar *str,
471                                          const gchar *quote_chars);
472
473 gint g_int_compare      (gconstpointer a, gconstpointer b);
474
475 gchar *generate_mime_boundary   (const gchar *prefix);
476
477 gint quote_cmd_argument(gchar * result, guint size,
478                         const gchar * path);
479 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
480
481 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
482 void get_hex_str(gchar *out, guchar ch);
483
484 /* auto pointer for containers that support GType system */
485
486 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
487 typedef struct AutoPointer      GAuto;
488 GType g_auto_pointer_register           (void);
489 GAuto *g_auto_pointer_new               (gpointer pointer);
490 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
491                                          GFreeFunc free);
492 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
493 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
494 void g_auto_pointer_free                (GAuto *auto_ptr);
495 void replace_returns                    (gchar *str);
496 gboolean get_uri_part   (const gchar *start,
497                          const gchar *scanpos,
498                          const gchar **bp,
499                          const gchar **ep,
500                          gboolean hdr);
501 gchar *make_uri_string  (const gchar *bp,
502                          const gchar *ep);
503 gboolean get_email_part (const gchar *start, 
504                          const gchar *scanpos,
505                          const gchar **bp, 
506                          const gchar **ep,
507                          gboolean hdr);
508 gchar *make_email_string(const gchar *bp,
509                          const gchar *ep);
510 gchar *make_http_string (const gchar *bp,
511                          const gchar *ep);
512
513 gchar *mailcap_get_command_for_type(const gchar *type, 
514                                     const gchar *file_to_open);
515 void mailcap_update_default        (const gchar *type,
516                                     const gchar *command);
517
518 gboolean file_is_email(const gchar *filename);
519 gboolean sc_g_list_bigger(GList *list, gint max);
520 gboolean sc_g_slist_bigger(GSList *list, gint max);
521
522 GMutex *cm_mutex_new(void);
523 void cm_mutex_free(GMutex *mutex);
524
525 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
526
527 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
528
529 #if !GLIB_CHECK_VERSION(2, 30, 0)
530 gchar   *g_utf8_substring         (const gchar *p,
531                                    glong        start_pos,
532                                    glong        end_pos) G_GNUC_MALLOC;
533 #endif
534
535 gboolean get_random_bytes(void *buf, size_t count);
536
537 #ifdef __cplusplus
538 }
539 #endif
540
541 #endif /* __UTILS_H__ */