]> sjero.net Git - wget/blob - src/utils.h
Use Gnulib's alloc functions throughout the source.
[wget] / src / utils.h
1 /* Declarations for utils.c.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 This file is part of GNU Wget.
6
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Wget.  If not, see <http://www.gnu.org/licenses/>.
19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work.  */
30
31 #ifndef UTILS_H
32 #define UTILS_H
33
34 #ifdef USE_GNULIB_ALLOC
35
36 /* Constant is using when we don`t know attempted size exactly */
37 #define UNKNOWN_ATTEMPTED_SIZE -3
38
39 /* Macros that interface to malloc, but know about type sizes, and
40    cast the result to the appropriate type.  The casts are not
41    necessary in standard C, but Wget performs them anyway for the sake
42    of pre-standard environments and possibly C++.  */
43
44 #define xnew(type) (xmalloc (sizeof (type)))
45 #define xnew0(type) (xcalloc (1, sizeof (type)))
46 #define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
47 #define xnew0_array(type, len) (xcalloc ((len), sizeof (type)))
48
49 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
50
51 #define xfree free
52 /* Free P if it is non-NULL.  C requires free() to behaves this way by
53    default, but Wget's code is historically careful not to pass NULL
54    to free.  This allows us to assert p!=NULL in xfree to check
55    additional errors.  (But we currently don't do that!)  */
56 #define xfree_null(p) if (!(p)) ; else xfree (p)
57
58 #endif /* USE_GNULIB_ALLOC */
59
60 struct hash_table;
61
62 struct file_memory {
63   char *content;
64   long length;
65   int mmap_p;
66 };
67
68 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
69
70 char *time_str (time_t);
71 char *datetime_str (time_t);
72
73 #ifdef DEBUG_MALLOC
74 void print_malloc_debug_stats ();
75 #endif
76
77 char *xstrdup_lower (const char *);
78
79 char *strdupdelim (const char *, const char *);
80 char **sepstring (const char *);
81 bool subdir_p (const char *, const char *);
82 void fork_to_background (void);
83
84 char *aprintf (const char *, ...) GCC_FORMAT_ATTR (1, 2);
85 char *concat_strings (const char *, ...);
86
87 void touch (const char *, time_t);
88 int remove_link (const char *);
89 bool file_exists_p (const char *);
90 bool file_non_directory_p (const char *);
91 wgint file_size (const char *);
92 int make_directory (const char *);
93 char *unique_name (const char *, bool);
94 FILE *unique_create (const char *, bool, char **);
95 FILE *fopen_excl (const char *, bool);
96 char *file_merge (const char *, const char *);
97
98 int fnmatch_nocase (const char *, const char *, int);
99 bool acceptable (const char *);
100 bool accdir (const char *s);
101 char *suffix (const char *s);
102 bool match_tail (const char *, const char *, bool);
103 bool has_wildcards_p (const char *);
104
105 bool has_html_suffix_p (const char *);
106
107 char *read_whole_line (FILE *);
108 struct file_memory *read_file (const char *);
109 void read_file_free (struct file_memory *);
110
111 void free_vec (char **);
112 char **merge_vecs (char **, char **);
113 char **vec_append (char **, const char *);
114
115 void string_set_add (struct hash_table *, const char *);
116 int string_set_contains (struct hash_table *, const char *);
117 void string_set_to_array (struct hash_table *, char **);
118 void string_set_free (struct hash_table *);
119 void free_keys_and_values (struct hash_table *);
120
121 const char *with_thousand_seps (wgint);
122
123 /* human_readable must be able to accept wgint and SUM_SIZE_INT
124    arguments.  On machines where wgint is 32-bit, declare it to accept
125    double.  */
126 #if SIZEOF_WGINT >= 8
127 # define HR_NUMTYPE wgint
128 #else
129 # define HR_NUMTYPE double
130 #endif
131 char *human_readable (HR_NUMTYPE);
132
133
134 int numdigit (wgint);
135 char *number_to_string (char *, wgint);
136 char *number_to_static_string (wgint);
137
138 int determine_screen_width (void);
139 int random_number (int);
140 double random_float (void);
141
142 bool run_with_timeout (double, void (*) (void *), void *);
143 void xsleep (double);
144
145 /* How many bytes it will take to store LEN bytes in base64.  */
146 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
147
148 int base64_encode (const void *, int, char *);
149 int base64_decode (const char *, void *);
150
151 void stable_sort (void *, size_t, size_t, int (*) (const void *, const void *));
152
153 const char *print_decimal (double);
154
155 #endif /* UTILS_H */