1 | /****************************************************************************** |
---|
2 | * $Id: utils.h 3117 2007-09-20 23:33:46Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #ifndef TR_UTILS_H |
---|
26 | #define TR_UTILS_H 1 |
---|
27 | |
---|
28 | #include <inttypes.h> |
---|
29 | #include <stdarg.h> |
---|
30 | #include <stddef.h> /* for size_t */ |
---|
31 | #include <stdio.h> /* FILE* */ |
---|
32 | |
---|
33 | void tr_msgInit( void ); |
---|
34 | |
---|
35 | #define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a ) |
---|
36 | #define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a ) |
---|
37 | #define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a ) |
---|
38 | void tr_msg ( int level, char * msg, ... ); |
---|
39 | FILE* tr_getLog( void ); |
---|
40 | |
---|
41 | int tr_rand ( int ); |
---|
42 | |
---|
43 | void * tr_memmem( const void *, size_t, const void *, size_t ); |
---|
44 | |
---|
45 | /*********************************************************************** |
---|
46 | * tr_mkdirp |
---|
47 | *********************************************************************** |
---|
48 | * Create a directory and any needed parent directories. |
---|
49 | * Note that the string passed in must be writable! |
---|
50 | **********************************************************************/ |
---|
51 | int tr_mkdirp( char * path, int permissions ); |
---|
52 | |
---|
53 | int tr_mkdir( const char * path, int permissions ); |
---|
54 | |
---|
55 | /*********************************************************************** |
---|
56 | * tr_strcasecmp |
---|
57 | *********************************************************************** |
---|
58 | * A case-insensitive strncmp() |
---|
59 | **********************************************************************/ |
---|
60 | #define tr_strcasecmp( ff, ss ) ( tr_strncasecmp( (ff), (ss), ULONG_MAX ) ) |
---|
61 | int tr_strncasecmp( const char * first, const char * second, size_t len ); |
---|
62 | |
---|
63 | /*********************************************************************** |
---|
64 | * tr_sprintf |
---|
65 | *********************************************************************** |
---|
66 | * Appends to the end of a buffer using printf formatting, |
---|
67 | * growing the buffer if needed |
---|
68 | **********************************************************************/ |
---|
69 | int tr_sprintf( char ** buf, int * used, int * max, |
---|
70 | const char * format, ... ); |
---|
71 | /* gee, it sure would be nice if BeOS had va_copy() */ |
---|
72 | int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list ); |
---|
73 | /* this concatenates some binary data onto the end of a buffer */ |
---|
74 | int tr_concat( char ** buf, int * used, int * max, |
---|
75 | const char * data, int len ); |
---|
76 | |
---|
77 | /* creates a filename from a series of elements using the |
---|
78 | correct separator for filenames. */ |
---|
79 | void tr_buildPath ( char* buf, size_t buflen, |
---|
80 | const char * first_element, ... ); |
---|
81 | |
---|
82 | struct timeval timevalSec ( int seconds ); |
---|
83 | struct timeval timevalMsec ( int milliseconds ); |
---|
84 | |
---|
85 | |
---|
86 | int tr_ioErrorFromErrno( void ); |
---|
87 | |
---|
88 | char * tr_errorString( int code ); |
---|
89 | |
---|
90 | /* return the current date in milliseconds */ |
---|
91 | uint64_t tr_date( void ); |
---|
92 | |
---|
93 | /* wait the specified number of milliseconds */ |
---|
94 | void tr_wait( uint64_t delay_milliseconds ); |
---|
95 | |
---|
96 | /*********************************************************************** |
---|
97 | * strlcat_utf8 |
---|
98 | *********************************************************************** |
---|
99 | * According to the official specification, all strings in the torrent |
---|
100 | * file are supposed to be UTF-8 encoded. However, there are |
---|
101 | * non-compliant torrents around... If we encounter an invalid UTF-8 |
---|
102 | * character, we assume it is ISO 8859-1 and convert it to UTF-8. |
---|
103 | **********************************************************************/ |
---|
104 | void strlcat_utf8( void *, const void *, size_t, char ); |
---|
105 | size_t bufsize_utf8( const void *, int * ); |
---|
106 | |
---|
107 | /*** |
---|
108 | **** |
---|
109 | ***/ |
---|
110 | |
---|
111 | /* Sometimes the system defines MAX/MIN, sometimes not. In the latter |
---|
112 | case, define those here since we will use them */ |
---|
113 | #ifndef MAX |
---|
114 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
---|
115 | #endif |
---|
116 | #ifndef MIN |
---|
117 | #define MIN(a,b) ((a)>(b)?(b):(a)) |
---|
118 | #endif |
---|
119 | |
---|
120 | /*** |
---|
121 | **** |
---|
122 | ***/ |
---|
123 | |
---|
124 | #define tr_new(struct_type, n_structs) \ |
---|
125 | ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) |
---|
126 | #define tr_new0(struct_type, n_structs) \ |
---|
127 | ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) |
---|
128 | #define tr_renew(struct_type, mem, n_structs) \ |
---|
129 | ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) |
---|
130 | |
---|
131 | void* tr_malloc ( size_t ); |
---|
132 | void* tr_malloc0 ( size_t ); |
---|
133 | void* tr_calloc ( size_t nmemb, size_t size ); |
---|
134 | void tr_free ( void* ); |
---|
135 | |
---|
136 | char* tr_strdup( const char * str ); |
---|
137 | char* tr_strndup( const char * str, int len ); |
---|
138 | |
---|
139 | /*** |
---|
140 | **** |
---|
141 | ***/ |
---|
142 | |
---|
143 | typedef void (tr_set_func)(void * element, void * userData ); |
---|
144 | |
---|
145 | void tr_set_compare( const void * a, size_t aCount, |
---|
146 | const void * b, size_t bCount, |
---|
147 | int compare( const void * a, const void * b ), |
---|
148 | size_t elementSize, |
---|
149 | tr_set_func in_a_cb, |
---|
150 | tr_set_func in_b_cb, |
---|
151 | tr_set_func in_both_cb, |
---|
152 | void * userData ); |
---|
153 | |
---|
154 | int tr_compareUint8 ( uint8_t a, uint8_t b ); |
---|
155 | int tr_compareUint16( uint16_t a, uint16_t b ); |
---|
156 | int tr_compareUint32( uint32_t a, uint32_t b ); |
---|
157 | int tr_compareUint64( uint64_t a, uint64_t b ); |
---|
158 | |
---|
159 | /*** |
---|
160 | **** |
---|
161 | ***/ |
---|
162 | |
---|
163 | struct tr_bitfield |
---|
164 | { |
---|
165 | uint8_t * bits; |
---|
166 | size_t len; |
---|
167 | }; |
---|
168 | |
---|
169 | typedef struct tr_bitfield tr_bitfield; |
---|
170 | typedef struct tr_bitfield tr_bitfield_t; |
---|
171 | |
---|
172 | tr_bitfield* tr_bitfieldNew( size_t bitcount ); |
---|
173 | tr_bitfield* tr_bitfieldDup( const tr_bitfield* ); |
---|
174 | void tr_bitfieldFree( tr_bitfield*); |
---|
175 | |
---|
176 | void tr_bitfieldClear( tr_bitfield* ); |
---|
177 | void tr_bitfieldAdd( tr_bitfield*, size_t bit ); |
---|
178 | void tr_bitfieldRem( tr_bitfield*, size_t bit ); |
---|
179 | void tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end ); |
---|
180 | void tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end ); |
---|
181 | |
---|
182 | int tr_bitfieldIsEmpty( const tr_bitfield* ); |
---|
183 | size_t tr_bitfieldCountTrueBits( const tr_bitfield* ); |
---|
184 | |
---|
185 | tr_bitfield* tr_bitfieldNegate( tr_bitfield* ); |
---|
186 | tr_bitfield* tr_bitfieldAnd( tr_bitfield*, const tr_bitfield* ); |
---|
187 | |
---|
188 | #define tr_bitfieldHas(bitfield,nth) \ |
---|
189 | ( ( bitfield ) && ( (bitfield)->bits[(nth)>>3] & 128 >>( (nth) & 7 ) ) ) |
---|
190 | |
---|
191 | #endif |
---|