source: trunk/libtransmission/utils.h @ 7797

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

(trunk libT) new macro, tr_assert(). use it to help smoke out #1749

  • Property svn:keywords set to Date Rev Author Id
File size: 12.5 KB
RevLine 
[7632]1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
[1]3 *
[7632]4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
[260]9 *
[7632]10 * $Id: utils.h 7797 2009-01-25 16:14:11Z charles $
11 */
[1]12
13#ifndef TR_UTILS_H
14#define TR_UTILS_H 1
15
[2611]16#include <inttypes.h>
[2341]17#include <stdarg.h>
[7581]18#include <stddef.h> /* size_t */
[3117]19#include <stdio.h> /* FILE* */
[7581]20#include <string.h> /* memcpy()* */
21#include <stdlib.h> /* malloc() */
[6203]22#include <time.h> /* time_t* */
[2341]23
[7581]24#include "transmission.h"
25
[6792]26#ifdef __cplusplus
27extern "C" {
28#endif
29
[5838]30/***
31****
32***/
[815]33
[5843]34#ifndef FALSE
[6795]35 #define FALSE 0
[5843]36#endif
37
38#ifndef TRUE
[6795]39 #define TRUE 1
[5843]40#endif
41
[6949]42#ifndef UNUSED
43 #ifdef __GNUC__
44  #define UNUSED __attribute__ ( ( unused ) )
45 #else
46  #define UNUSED
47 #endif
48#endif
[5838]49
[6949]50#ifndef TR_GNUC_PRINTF
51 #ifdef __GNUC__
52  #define TR_GNUC_PRINTF( fmt,\
53                          args ) __attribute__ ( ( format ( printf, fmt,\
54                                                            args ) ) )
55 #else
56  #define TR_GNUC_PRINTF( fmt, args )
57 #endif
58#endif
59
60#ifndef TR_GNUC_NULL_TERMINATED
61 #if __GNUC__ >= 4
62  #define TR_GNUC_NULL_TERMINATED __attribute__ ( ( __sentinel__ ) )
63 #else
64  #define TR_GNUC_NULL_TERMINATED
65 #endif
66#endif
67
68#if __GNUC__ > 2 || ( __GNUC__ == 2 && __GNUC_MINOR__ >= 96 )
69 #define TR_GNUC_PURE __attribute__ ( ( __pure__ ) )
70 #define TR_GNUC_MALLOC __attribute__ ( ( __malloc__ ) )
71#else
72 #define TR_GNUC_PURE
73 #define TR_GNUC_MALLOC
74#endif
75
76
[5838]77/***
78****
79***/
80
[6795]81#if !defined( _ )
82 #if defined( SYS_DARWIN )
83  #define _( a ) ( a )
84 #elif defined( HAVE_LIBINTL_H )
[5193]85  #include <libintl.h>
[6795]86  #define _( a ) gettext ( a )
87 #else
88  #define _( a ) ( a )
89 #endif
[5193]90#endif
91
[6619]92/* #define DISABLE_GETTEXT */
[6946]93#if defined(TR_EMBEDDED) && !defined(DISABLE_GETTEXT)
94 #define DISABLE_GETTEXT
95#endif
[6619]96#ifdef DISABLE_GETTEXT
[6946]97 const char * tr_strip_positional_args( const char * fmt );
[6795]98 #undef _
99 #define _( a ) tr_strip_positional_args( a )
[6619]100#endif
101
[7529]102/****
103*****
104****/
[5243]105
[7797]106void tr_assertImpl( const char * file, int line, const char * test, const char * fmt, ... ) TR_GNUC_PRINTF( 4, 5 );
107
108#ifdef NDEBUG
109 #define tr_assert( test, fmt, ... )
110#else
111 #define tr_assert( test, fmt, ... ) \
112    do { if( ! ( test ) ) tr_assertImpl( __FILE__, __LINE__, #test, fmt, __VA_ARGS__ ); } while( 0 )
113#endif
114       
[7529]115int            tr_msgLoggingIsActive( int level );
[5243]116
[6795]117void           tr_msg( const char * file,
118                       int          line,
119                       int          level,
120                       const char * torrent,
121                       const char * fmt,
122                       ... ) TR_GNUC_PRINTF( 5, 6 );
[5838]123
[7529]124#define tr_nerr( n, ... ) \
125    do { \
126        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
127            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, __VA_ARGS__ ); \
128    } while( 0 )
129
130#define tr_ninf( n, ... ) \
131    do { \
132        if( tr_msgLoggingIsActive( TR_MSG_INF) ) \
133            tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, __VA_ARGS__ ); \
134    } while( 0 )
135
136#define tr_ndbg( n, ... ) \
137    do { \
138        if( tr_msgLoggingIsActive( TR_MSG_DBG) ) \
139            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, __VA_ARGS__ ); \
140    } while( 0 )
141
142#define tr_torerr( tor, ... ) \
143    do { \
144        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
145            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, __VA_ARGS__ ); \
146    } while( 0 )
147
148#define tr_torinf( tor, ... ) \
149    do { \
150        if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
151            tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, __VA_ARGS__ ); \
152    } while( 0 )
153
154#define tr_tordbg( tor, ... ) \
155    do { \
156        if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
157            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, __VA_ARGS__ ); \
158    } while( 0 )
159
160#define tr_err( ... ) \
161    do { \
162        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
163            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, __VA_ARGS__ ); \
164    } while( 0 )
165
166#define tr_inf( ... ) \
167    do { \
168        if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
169            tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, __VA_ARGS__ ); \
170    } while( 0 )
171
172#define tr_dbg( ... ) \
173    do { \
174        if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
175            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, __VA_ARGS__ ); \
176    } while( 0 )
177
178
179
[6795]180FILE*          tr_getLog( void );
[1]181
[7592]182tr_bool        tr_deepLoggingIsActive( void );
[6961]183
[6795]184void           tr_deepLog( const char * file,
185                           int          line,
186                           const char * name,
187                           const char * fmt,
188                           ... ) TR_GNUC_PRINTF( 4, 5 );
[5689]189
[6795]190char*          tr_getLogTimeStr( char * buf,
191                                 int    buflen );
[3295]192
[7529]193
194int            tr_wildmat( const char * text,
195                           const char * pattern );
196
[6897]197/** a portability wrapper for basename(). */
198char*          tr_basename( const char * path ) TR_GNUC_MALLOC;
199
200/** a portability wrapper for dirname(). */
201char*          tr_dirname( const char * path ) TR_GNUC_MALLOC;
202
[6892]203/**
[4733]204 * a portability wrapper around mkdir().
205 * On WIN32, the `permissions' argument is unused.
206 *
207 * @return zero on success, or -1 if an error occurred
208 * (in which case errno is set appropriately).
209 */
[6795]210int            tr_mkdir( const char * path,
211                         int          permissions );
[4733]212
213/**
214 * Like mkdir, but makes parent directories as needed.
215 *
216 * @return zero on success, or -1 if an error occurred
217 * (in which case errno is set appropriately).
218 */
[6795]219int            tr_mkdirp( const char * path,
220                          int          permissions );
[310]221
[2578]222
[6842]223/**
224 * Loads a file and returns its contents.
225 * On failure, NULL is returned and errno is set.
226 */
[6795]227uint8_t*       tr_loadFile( const char * filename,
228                            size_t *     size ) TR_GNUC_MALLOC;
[3425]229
[920]230
[2154]231/* creates a filename from a series of elements using the
232   correct separator for filenames. */
[6896]233char*          tr_buildPath( const char * first_element, ... )
234                                              TR_GNUC_NULL_TERMINATED
235                                              TR_GNUC_MALLOC;
[920]236
[6907]237struct timeval;
[2154]238
[6907]239void tr_timevalMsec( uint64_t           milliseconds,
240                     struct timeval   * setme );
[2915]241
[6907]242
[2328]243/* return the current date in milliseconds */
[6949]244uint64_t       tr_date( void );
[1]245
[2328]246/* wait the specified number of milliseconds */
[6949]247void           tr_wait( uint64_t delay_milliseconds );
[1]248
[7656]249char*          tr_utf8clean( const char  * str,
[7748]250                             int           max_len,
[7656]251                             tr_bool     * err );
252
253
[2430]254/***
255****
256***/
[1]257
[7549]258struct evbuffer;
259
260/** @brief pool of reusable buffers
261    @see tr_releaseBuffer() */
262struct evbuffer * tr_getBuffer( void );
263
264/** @brief return a buffer to the pool
265    @see tr_getBuffer() */
266void tr_releaseBuffer( struct evbuffer * buf );
267
268
269/***
270****
271***/
272
[2430]273/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
274   case, define those here since we will use them */
275#ifndef MAX
[6795]276 #define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
[2430]277#endif
278#ifndef MIN
[6795]279 #define MIN( a, b ) ( ( a ) > ( b ) ? ( b ) : ( a ) )
[2430]280#endif
[1]281
[2149]282/***
283****
284***/
285
[7663]286static TR_INLINE void* tr_malloc( size_t size )
[7581]287{
288    return size ? malloc( size ) : NULL;
289}
[7663]290static TR_INLINE void* tr_malloc0( size_t size )
[7581]291{
292    return size ? calloc( 1, size ) : NULL;
293}
[7663]294static TR_INLINE void tr_free( void * p )
[7581]295{
296    if( p != NULL )
297        free( p );
298}
[7663]299static TR_INLINE void* tr_memdup( const void * src, int byteCount )
[7581]300{
301    return memcpy( tr_malloc( byteCount ), src, byteCount );
302}
303
[6795]304#define tr_new( struct_type, n_structs )           \
[7581]305    ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
306
[6795]307#define tr_new0( struct_type, n_structs )          \
[7581]308    ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
309
[6795]310#define tr_renew( struct_type, mem, n_structs )    \
[7581]311    ( (struct_type *) realloc ( ( mem ), ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
[2206]312
[7663]313/** @param in is a void* so that callers can pass in both signed & unsigned without a cast */
[7748]314char* tr_strndup( const void * in, int len ) TR_GNUC_MALLOC;
[2206]315
[7663]316/** @param in is a void* so that callers can pass in both signed & unsigned without a cast */
317static TR_INLINE char* tr_strdup( const void * in )
[7581]318{
[7663]319    return tr_strndup( in, in ? strlen( (const char *) in ) : 0 );
[7581]320}
[5860]321
[7609]322/* @brief same argument list as bsearch() */
323int tr_lowerBound( const void * key,
324                   const void * base,
325                   size_t       nmemb,
326                   size_t       size,
327                   int       (* compar)(const void* key, const void* arrayMember),
328                   tr_bool    * exact_match );
329
330
[6795]331char*       tr_strdup_printf( const char * fmt,
332                              ... )  TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC;
333
334char*       tr_base64_encode( const void * input,
335                              int          inlen,
336                              int *        outlen ) TR_GNUC_MALLOC;
337
338char*       tr_base64_decode( const void * input,
339                              int          inlen,
340                              int *        outlen ) TR_GNUC_MALLOC;
341
342size_t      tr_strlcpy( char *       dst,
343                        const void * src,
344                        size_t       siz );
345
346int         tr_snprintf( char *       buf,
347                         size_t       buflen,
348                         const char * fmt,
[6945]349                         ... ) TR_GNUC_PRINTF( 3, 4 );
[6795]350
[6949]351const char* tr_strerror( int );
[5065]352
[6795]353char*       tr_strstrip( char * str );
[6717]354
[2149]355/***
356****
357***/
358
[6795]359typedef void ( tr_set_func )( void * element, void * userData );
[3105]360
[6795]361void        tr_set_compare( const void * a,
362                            size_t aCount,
363                            const void * b,
364                            size_t bCount,
365                            int compare( const void * a, const void * b ),
366                            size_t elementSize,
367                            tr_set_func in_a_cb,
368                            tr_set_func in_b_cb,
369                            tr_set_func in_both_cb,
370                            void * userData );
[6621]371
[6795]372void tr_sha1_to_hex( char *          out,
373                     const uint8_t * sha1 );
[5112]374
[5356]375
[6795]376int  tr_httpIsValidURL( const char * url );
[5673]377
[6795]378int  tr_httpParseURL( const char * url,
379                      int          url_len,
380                      char **      setme_host,
381                      int *        setme_port,
382                      char **      setme_path );
[5356]383
384
[3105]385/***
386****
387***/
388
[7525]389typedef struct tr_bitfield
[2149]390{
[6795]391    uint8_t *  bits;
392    size_t     bitCount;
393    size_t     byteCount;
[7525]394}
395tr_bitfield;
[2149]396
[7525]397tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );
[2149]398
[7581]399tr_bitfield* tr_bitfieldDestruct( tr_bitfield* );
[7525]400
[7663]401static TR_INLINE tr_bitfield* tr_bitfieldNew( size_t bitcount )
[7581]402{
403    return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount );
404}
[6795]405
[7663]406static TR_INLINE void tr_bitfieldFree( tr_bitfield * b )
[7581]407{
408    tr_free( tr_bitfieldDestruct( b ) );
409}
410
[5838]411tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
[2149]412
[6795]413void         tr_bitfieldClear( tr_bitfield* );
[2149]414
[7529]415int          tr_bitfieldAdd( tr_bitfield*, size_t bit );
[3105]416
[7529]417int          tr_bitfieldRem( tr_bitfield*, size_t bit );
[6795]418
[7529]419int          tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
[6795]420
[7529]421int          tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
[6795]422
[7529]423void         tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
[6795]424
425int          tr_bitfieldIsEmpty( const tr_bitfield* );
426
427size_t       tr_bitfieldCountTrueBits( const tr_bitfield* );
428
[7529]429tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
[6795]430
[6058]431/** A stripped-down version of bitfieldHas to be used
432    for speed when you're looping quickly.  This version
433    has none of tr_bitfieldHas()'s safety checks, so you
434    need to call tr_bitfieldTestFast() first before you
435    start looping. */
[7663]436static TR_INLINE tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth )
[7581]437{
438    return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0;
439}
[6058]440
441/** @param high the highest nth bit you're going to access */
[7663]442static TR_INLINE tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )
[7581]443{
444    return ( b != NULL )
445        && ( b->bits != NULL )
446        && ( high < b->bitCount );
447}
[6058]448
[7663]449static TR_INLINE tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth )
[7581]450{
451    return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth );
452}
[6595]453
[7533]454double tr_getRatio( double numerator, double denominator );
[4482]455
[7533]456
457int tr_ptr2int( void* );
458
459void* tr_int2ptr( int );
460
[6792]461#ifdef __cplusplus
462}
[1]463#endif
[6792]464
465#endif
Note: See TracBrowser for help on using the repository browser.