source: trunk/libtransmission/utils.h @ 6595

Last change on this file since 6595 was 6595, checked in by muks, 13 years ago

Make tr_bitfieldHas() a macro

  • Property svn:keywords set to Date Rev Author Id
File size: 9.2 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 6595 2008-08-20 13:45:52Z muks $
3 *
4 * Copyright (c) 2005-2008 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#include <time.h> /* time_t* */
33
34/***
35****
36***/
37
38#ifndef FALSE
39#define FALSE 0
40#endif
41
42#ifndef TRUE
43#define TRUE 1
44#endif
45
46#ifndef UNUSED
47#ifdef __GNUC__
48#define UNUSED __attribute__((unused))
49#else
50#define UNUSED
51#endif
52#endif
53
54#ifndef TR_GNUC_PRINTF
55#ifdef __GNUC__
56#define TR_GNUC_PRINTF( fmt, args ) __attribute__((format (printf, fmt, args)))
57#else
58#define TR_GNUC_PRINTF( fmt, args )
59#endif
60#endif
61
62#ifndef TR_GNUC_NULL_TERMINATED
63#if __GNUC__ >= 4
64#define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
65#else
66#define TR_GNUC_NULL_TERMINATED
67#endif
68#endif
69
70#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
71#define TR_GNUC_PURE __attribute__((__pure__))
72#define TR_GNUC_MALLOC __attribute__((__malloc__))
73#else
74#define TR_GNUC_PURE
75#define TR_GNUC_MALLOC
76#endif
77
78
79/***
80****
81***/
82
83#if !defined(_)
84#if defined(SYS_DARWIN)
85  #define _(a) (a)
86#elif defined(HAVE_LIBINTL_H)
87  #include <libintl.h>
88  #define _(a) gettext (a)
89#else
90  #define _(a) (a)
91#endif
92#endif
93
94#define tr_nerr( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, ## a )
95#define tr_ninf( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, ## a )
96#define tr_ndbg( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, ## a )
97
98#define tr_torerr( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, ## a )
99#define tr_torinf( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, ## a )
100#define tr_tordbg( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, ## a )
101
102#define tr_err( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, ## a )
103#define tr_inf( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, ## a )
104#define tr_dbg( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, ## a )
105
106void tr_msgInit( void );
107
108void tr_msg( const char * file, int line,
109             int level, const char * torrent,
110             const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 );
111
112FILE* tr_getLog( void );
113
114void tr_deepLog( const char * file, int line,
115                 const char * name,
116                 const char * fmt, ... ) TR_GNUC_PRINTF( 4, 5 );
117
118char* tr_getLogTimeStr( char * buf, int buflen );
119
120/**
121 * a portability wrapper around mkdir().
122 * On WIN32, the `permissions' argument is unused.
123 *
124 * @return zero on success, or -1 if an error occurred
125 * (in which case errno is set appropriately).
126 */
127int tr_mkdir( const char * path, int permissions );
128
129/**
130 * Like mkdir, but makes parent directories as needed.
131 *
132 * @return zero on success, or -1 if an error occurred
133 * (in which case errno is set appropriately).
134 */
135int tr_mkdirp( const char * path, int permissions );
136
137
138uint8_t* tr_loadFile( const char * filename, size_t * size ) TR_GNUC_MALLOC;
139
140
141/* creates a filename from a series of elements using the
142   correct separator for filenames. */
143void tr_buildPath( char* buf, size_t buflen,
144                   const char * first_element, ... )
145                                      TR_GNUC_NULL_TERMINATED;
146
147struct timeval tr_timevalMsec( uint64_t milliseconds );
148
149
150int tr_ioErrorFromErrno( int err );
151
152const char * tr_errorString( int code );
153
154/* return the current date in milliseconds */
155uint64_t tr_date( void );
156
157/* wait the specified number of milliseconds */
158void tr_wait( uint64_t delay_milliseconds );
159
160/***
161****
162***/
163
164/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
165   case, define those here since we will use them */
166#ifndef MAX
167#define MAX(a,b) ((a)>(b)?(a):(b))
168#endif
169#ifndef MIN
170#define MIN(a,b) ((a)>(b)?(b):(a))
171#endif
172
173/***
174****
175***/
176
177#define tr_new(struct_type, n_structs)           \
178    ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
179#define tr_new0(struct_type, n_structs)          \
180    ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
181#define tr_renew(struct_type, mem, n_structs)    \
182    ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
183
184void* tr_malloc  ( size_t ) TR_GNUC_MALLOC;
185void* tr_malloc0 ( size_t ) TR_GNUC_MALLOC;
186void* tr_calloc  ( size_t nmemb, size_t size ) TR_GNUC_MALLOC;
187void  tr_free    ( void* );
188
189char* tr_strdup( const void * str ) TR_GNUC_MALLOC;
190char* tr_strndup( const void * str, int len ) TR_GNUC_MALLOC;
191void* tr_memdup( const void * src, int byteCount ) TR_GNUC_MALLOC;
192char* tr_strdup_printf( const char * fmt, ... )  TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC;
193char* tr_base64_encode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
194char* tr_base64_decode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
195
196size_t tr_strlcpy( char * dst, const char * src, size_t siz );
197int tr_snprintf( char * buf, size_t buflen, const char * fmt, ... );
198
199int   tr_stringEndsWith( const char * string, const char * end );
200
201const char* tr_strerror( int );
202
203/***
204****
205***/
206
207typedef void (tr_set_func)(void * element, void * userData );
208
209void tr_set_compare( const void * a, size_t aCount,
210                     const void * b, size_t bCount,
211                     int compare( const void * a, const void * b ),
212                     size_t elementSize,
213                     tr_set_func in_a_cb,
214                     tr_set_func in_b_cb,
215                     tr_set_func in_both_cb,
216                     void * userData );
217                   
218int tr_compareUint16( uint16_t a, uint16_t b );
219int tr_compareUint32( uint32_t a, uint32_t b );
220int tr_compareUint64( uint64_t a, uint64_t b );
221int tr_compareDouble( double a, double b );
222int tr_compareTime( time_t a, time_t b );
223
224int tr_strcmp( const void * a, const void * b );
225int tr_strcasecmp( const char * a, const char * b );
226
227void tr_sha1_to_hex( char * out, const uint8_t * sha1 );
228
229
230int tr_httpIsValidURL( const char * url );
231
232int tr_httpParseURL( const char * url,
233                     int          url_len,
234                     char     ** setme_host,
235                     int       * setme_port,
236                     char     ** setme_path );
237
238
239/***
240****
241***/
242
243struct tr_bitfield
244{
245    uint8_t * bits;
246    size_t bitCount;
247    size_t byteCount;
248};
249
250typedef struct tr_bitfield tr_bitfield;
251typedef struct tr_bitfield tr_bitfield_t;
252
253tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
254tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
255void tr_bitfieldFree( tr_bitfield*);
256
257void tr_bitfieldClear( tr_bitfield* );
258int  tr_bitfieldAdd( tr_bitfield*, size_t bit );
259int  tr_bitfieldRem( tr_bitfield*, size_t bit );
260int  tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
261int  tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
262void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
263
264int    tr_bitfieldIsEmpty( const tr_bitfield* );
265size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
266
267tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
268
269#if 0
270/** @brief finds the first true bit in the bitfield, starting at `startPos'
271    @param setmePos the position of the true bit, if found, is set here.
272    @return nonzero if a true bit was found */
273int tr_bitfieldFindTrue( const tr_bitfield  * bitfield,
274                         size_t               startPos,
275                         size_t             * setmePos );
276#endif
277
278
279/** A stripped-down version of bitfieldHas to be used
280    for speed when you're looping quickly.  This version
281    has none of tr_bitfieldHas()'s safety checks, so you
282    need to call tr_bitfieldTestFast() first before you
283    start looping. */
284#define tr_bitfieldHasFast(bitfield,nth) \
285    (( bitfield->bits[(nth)>>3u] << ((nth)&7u) & 0x80) != 0 )
286
287/** @param high the highest nth bit you're going to access */
288#define tr_bitfieldTestFast(bitfield,high) \
289    ( (bitfield) && ((bitfield)->bits) && ((high)<(bitfield)->bitCount ) )
290
291#define tr_bitfieldHas(bitfield,nth) \
292    ( tr_bitfieldTestFast( bitfield, nth ) && \
293      tr_bitfieldHasFast( bitfield, nth ) )
294
295double tr_getRatio( double numerator, double denominator );
296
297#endif
Note: See TracBrowser for help on using the repository browser.