source: trunk/libtransmission/utils.h @ 7549

Last change on this file since 7549 was 7549, checked in by charles, 12 years ago

(trunk libT) have a pool of reusable evbuffers

  • Property svn:keywords set to Date Rev Author Id
File size: 12.4 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 7549 2008-12-30 20:32:00Z charles $
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#ifdef __cplusplus
35extern "C" {
36#endif
37
38/***
39****
40***/
41
42#ifndef FALSE
43 #define FALSE 0
44#endif
45
46#ifndef TRUE
47 #define TRUE 1
48#endif
49
50#ifndef UNUSED
51 #ifdef __GNUC__
52  #define UNUSED __attribute__ ( ( unused ) )
53 #else
54  #define UNUSED
55 #endif
56#endif
57
58#ifndef TR_GNUC_PRINTF
59 #ifdef __GNUC__
60  #define TR_GNUC_PRINTF( fmt,\
61                          args ) __attribute__ ( ( format ( printf, fmt,\
62                                                            args ) ) )
63 #else
64  #define TR_GNUC_PRINTF( fmt, args )
65 #endif
66#endif
67
68#ifndef TR_GNUC_NULL_TERMINATED
69 #if __GNUC__ >= 4
70  #define TR_GNUC_NULL_TERMINATED __attribute__ ( ( __sentinel__ ) )
71 #else
72  #define TR_GNUC_NULL_TERMINATED
73 #endif
74#endif
75
76#if __GNUC__ > 2 || ( __GNUC__ == 2 && __GNUC_MINOR__ >= 96 )
77 #define TR_GNUC_PURE __attribute__ ( ( __pure__ ) )
78 #define TR_GNUC_MALLOC __attribute__ ( ( __malloc__ ) )
79#else
80 #define TR_GNUC_PURE
81 #define TR_GNUC_MALLOC
82#endif
83
84
85/***
86****
87***/
88
89#if !defined( _ )
90 #if defined( SYS_DARWIN )
91  #define _( a ) ( a )
92 #elif defined( HAVE_LIBINTL_H )
93  #include <libintl.h>
94  #define _( a ) gettext ( a )
95 #else
96  #define _( a ) ( a )
97 #endif
98#endif
99
100/* #define DISABLE_GETTEXT */
101#if defined(TR_EMBEDDED) && !defined(DISABLE_GETTEXT)
102 #define DISABLE_GETTEXT
103#endif
104#ifdef DISABLE_GETTEXT
105 const char * tr_strip_positional_args( const char * fmt );
106 #undef _
107 #define _( a ) tr_strip_positional_args( a )
108#endif
109
110/****
111*****
112****/
113
114int            tr_msgLoggingIsActive( int level );
115
116void           tr_msg( const char * file,
117                       int          line,
118                       int          level,
119                       const char * torrent,
120                       const char * fmt,
121                       ... ) TR_GNUC_PRINTF( 5, 6 );
122
123#define tr_nerr( n, ... ) \
124    do { \
125        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
126            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, __VA_ARGS__ ); \
127    } while( 0 )
128
129#define tr_ninf( n, ... ) \
130    do { \
131        if( tr_msgLoggingIsActive( TR_MSG_INF) ) \
132            tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, __VA_ARGS__ ); \
133    } while( 0 )
134
135#define tr_ndbg( n, ... ) \
136    do { \
137        if( tr_msgLoggingIsActive( TR_MSG_DBG) ) \
138            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, __VA_ARGS__ ); \
139    } while( 0 )
140
141#define tr_torerr( tor, ... ) \
142    do { \
143        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
144            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, __VA_ARGS__ ); \
145    } while( 0 )
146
147#define tr_torinf( tor, ... ) \
148    do { \
149        if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
150            tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, __VA_ARGS__ ); \
151    } while( 0 )
152
153#define tr_tordbg( tor, ... ) \
154    do { \
155        if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
156            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, __VA_ARGS__ ); \
157    } while( 0 )
158
159#define tr_err( ... ) \
160    do { \
161        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
162            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, __VA_ARGS__ ); \
163    } while( 0 )
164
165#define tr_inf( ... ) \
166    do { \
167        if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
168            tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, __VA_ARGS__ ); \
169    } while( 0 )
170
171#define tr_dbg( ... ) \
172    do { \
173        if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
174            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, __VA_ARGS__ ); \
175    } while( 0 )
176
177
178
179FILE*          tr_getLog( void );
180
181int            tr_deepLoggingIsActive( void );
182
183void           tr_deepLog( const char * file,
184                           int          line,
185                           const char * name,
186                           const char * fmt,
187                           ... ) TR_GNUC_PRINTF( 4, 5 );
188
189char*          tr_getLogTimeStr( char * buf,
190                                 int    buflen );
191
192
193int            tr_wildmat( const char * text,
194                           const char * pattern );
195
196/** a portability wrapper for basename(). */
197char*          tr_basename( const char * path ) TR_GNUC_MALLOC;
198
199/** a portability wrapper for dirname(). */
200char*          tr_dirname( const char * path ) TR_GNUC_MALLOC;
201
202/**
203 * a portability wrapper around mkdir().
204 * On WIN32, the `permissions' argument is unused.
205 *
206 * @return zero on success, or -1 if an error occurred
207 * (in which case errno is set appropriately).
208 */
209int            tr_mkdir( const char * path,
210                         int          permissions );
211
212/**
213 * Like mkdir, but makes parent directories as needed.
214 *
215 * @return zero on success, or -1 if an error occurred
216 * (in which case errno is set appropriately).
217 */
218int            tr_mkdirp( const char * path,
219                          int          permissions );
220
221
222/**
223 * Loads a file and returns its contents.
224 * On failure, NULL is returned and errno is set.
225 */
226uint8_t*       tr_loadFile( const char * filename,
227                            size_t *     size ) TR_GNUC_MALLOC;
228
229
230/* creates a filename from a series of elements using the
231   correct separator for filenames. */
232char*          tr_buildPath( const char * first_element, ... )
233                                              TR_GNUC_NULL_TERMINATED
234                                              TR_GNUC_MALLOC;
235
236struct timeval;
237
238void tr_timevalMsec( uint64_t           milliseconds,
239                     struct timeval   * setme );
240
241
242/* return the current date in milliseconds */
243uint64_t       tr_date( void );
244
245/* wait the specified number of milliseconds */
246void           tr_wait( uint64_t delay_milliseconds );
247
248/***
249****
250***/
251
252struct evbuffer;
253
254/** @brief pool of reusable buffers
255    @see tr_releaseBuffer() */
256struct evbuffer * tr_getBuffer( void );
257
258/** @brief return a buffer to the pool
259    @see tr_getBuffer() */
260void tr_releaseBuffer( struct evbuffer * buf );
261
262
263/***
264****
265***/
266
267/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
268   case, define those here since we will use them */
269#ifndef MAX
270 #define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
271#endif
272#ifndef MIN
273 #define MIN( a, b ) ( ( a ) > ( b ) ? ( b ) : ( a ) )
274#endif
275
276/***
277****
278***/
279
280#define tr_new( struct_type, n_structs )           \
281    ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( (\
282                                                                             size_t) (\
283                                                                             n_structs ) ) ) )
284#define tr_new0( struct_type, n_structs )          \
285    ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( (\
286                                                                              size_t) (\
287                                                                              n_structs ) ) ) )
288#define tr_renew( struct_type, mem, n_structs )    \
289    ( (struct_type *) realloc ( ( mem ),\
290                               ( (size_t) sizeof ( struct_type ) ) * ( (\
291                                                                          size_t) (\
292                                                                          n_structs ) ) ) )
293
294void*       tr_malloc( size_t ) TR_GNUC_MALLOC;
295
296void*       tr_malloc0( size_t ) TR_GNUC_MALLOC;
297
298void        tr_free( void* );
299
300char*       tr_strdup( const void * str ) TR_GNUC_MALLOC;
301
302char*       tr_strndup( const void * str,
303                        int          len ) TR_GNUC_MALLOC;
304
305void*       tr_memdup( const void * src,
306                       int          byteCount ) TR_GNUC_MALLOC;
307
308char*       tr_strdup_printf( const char * fmt,
309                              ... )  TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC;
310
311char*       tr_base64_encode( const void * input,
312                              int          inlen,
313                              int *        outlen ) TR_GNUC_MALLOC;
314
315char*       tr_base64_decode( const void * input,
316                              int          inlen,
317                              int *        outlen ) TR_GNUC_MALLOC;
318
319size_t      tr_strlcpy( char *       dst,
320                        const void * src,
321                        size_t       siz );
322
323int         tr_snprintf( char *       buf,
324                         size_t       buflen,
325                         const char * fmt,
326                         ... ) TR_GNUC_PRINTF( 3, 4 );
327
328const char* tr_strerror( int );
329
330char*       tr_strstrip( char * str );
331
332/***
333****
334***/
335
336typedef void ( tr_set_func )( void * element, void * userData );
337
338void        tr_set_compare( const void * a,
339                            size_t aCount,
340                            const void * b,
341                            size_t bCount,
342                            int compare( const void * a, const void * b ),
343                            size_t elementSize,
344                            tr_set_func in_a_cb,
345                            tr_set_func in_b_cb,
346                            tr_set_func in_both_cb,
347                            void * userData );
348
349void tr_sha1_to_hex( char *          out,
350                     const uint8_t * sha1 );
351
352
353int  tr_httpIsValidURL( const char * url );
354
355int  tr_httpParseURL( const char * url,
356                      int          url_len,
357                      char **      setme_host,
358                      int *        setme_port,
359                      char **      setme_path );
360
361
362/***
363****
364***/
365
366typedef struct tr_bitfield
367{
368    uint8_t *  bits;
369    size_t     bitCount;
370    size_t     byteCount;
371}
372tr_bitfield;
373
374tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );
375
376void         tr_bitfieldDestruct( tr_bitfield* );
377
378tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
379
380tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
381
382void         tr_bitfieldFree( tr_bitfield* );
383
384void         tr_bitfieldClear( tr_bitfield* );
385
386int          tr_bitfieldAdd( tr_bitfield*, size_t bit );
387
388int          tr_bitfieldRem( tr_bitfield*, size_t bit );
389
390int          tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
391
392int          tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
393
394void         tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
395
396int          tr_bitfieldIsEmpty( const tr_bitfield* );
397
398size_t       tr_bitfieldCountTrueBits( const tr_bitfield* );
399
400tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
401
402/** A stripped-down version of bitfieldHas to be used
403    for speed when you're looping quickly.  This version
404    has none of tr_bitfieldHas()'s safety checks, so you
405    need to call tr_bitfieldTestFast() first before you
406    start looping. */
407#define tr_bitfieldHasFast( bitfield, nth ) \
408    ( ( (bitfield)->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 )
409
410/** @param high the highest nth bit you're going to access */
411#define tr_bitfieldTestFast( bitfield, high ) \
412    ( ( bitfield ) && ( ( bitfield )->bits )\
413    && ( ( high ) < ( bitfield )->bitCount ) )
414
415#define tr_bitfieldHas( bitfield, nth ) \
416    ( tr_bitfieldTestFast( bitfield, nth )    \
417    && tr_bitfieldHasFast( bitfield, nth ) )
418
419double tr_getRatio( double numerator, double denominator );
420
421
422int tr_ptr2int( void* );
423
424void* tr_int2ptr( int );
425
426#ifdef __cplusplus
427}
428#endif
429
430#endif
Note: See TracBrowser for help on using the repository browser.