31
31
#include <stdio.h> /* FILE* */
32
32
#include <time.h> /* time_t* */
48
#define UNUSED __attribute__((unused))
52
#define UNUSED __attribute__ ( ( unused ) )
54
58
#ifndef TR_GNUC_PRINTF
56
#define TR_GNUC_PRINTF( fmt, args ) __attribute__((format (printf, fmt, args)))
58
#define TR_GNUC_PRINTF( fmt, args )
60
#define TR_GNUC_PRINTF( fmt,\
61
args ) __attribute__ ( ( format ( printf, fmt,\
64
#define TR_GNUC_PRINTF( fmt, args )
62
68
#ifndef TR_GNUC_NULL_TERMINATED
64
#define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
66
#define TR_GNUC_NULL_TERMINATED
70
#define TR_GNUC_NULL_TERMINATED __attribute__ ( ( __sentinel__ ) )
72
#define TR_GNUC_NULL_TERMINATED
70
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
71
#define TR_GNUC_PURE __attribute__((__pure__))
72
#define TR_GNUC_MALLOC __attribute__((__malloc__))
76
#if __GNUC__ > 2 || ( __GNUC__ == 2 && __GNUC_MINOR__ >= 96 )
77
#define TR_GNUC_PURE __attribute__ ( ( __pure__ ) )
78
#define TR_GNUC_MALLOC __attribute__ ( ( __malloc__ ) )
75
#define TR_GNUC_MALLOC
81
#define TR_GNUC_MALLOC
84
#if defined(SYS_DARWIN)
86
#elif defined(HAVE_LIBINTL_H)
90
#if defined( SYS_DARWIN )
92
#elif defined( HAVE_LIBINTL_H )
87
93
#include <libintl.h>
88
#define _(a) gettext (a)
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 )
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 )
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 )
106
void tr_msgInit( void );
108
void tr_msg( const char * file, int line,
109
int level, const char * torrent,
110
const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 );
112
FILE* tr_getLog( void );
114
void tr_deepLog( const char * file, int line,
116
const char * fmt, ... ) TR_GNUC_PRINTF( 4, 5 );
118
char* tr_getLogTimeStr( char * buf, int buflen );
120
/** Returns a random number in the range of [0...n) */
121
int tr_rand ( int n );
94
#define _( a ) gettext ( a )
100
/* #define DISABLE_GETTEXT */
101
#if defined(TR_EMBEDDED) && !defined(DISABLE_GETTEXT)
102
#define DISABLE_GETTEXT
104
#ifdef DISABLE_GETTEXT
105
const char * tr_strip_positional_args( const char * fmt );
107
#define _( a ) tr_strip_positional_args( a )
110
#define tr_nerr( n, ... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, __VA_ARGS__ )
111
#define tr_ninf( n, ... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, __VA_ARGS__ )
112
#define tr_ndbg( n, ... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, __VA_ARGS__ )
114
#define tr_torerr( tor, ... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, __VA_ARGS__ )
115
#define tr_torinf( tor, ... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, __VA_ARGS__ )
116
#define tr_tordbg( tor, ... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, __VA_ARGS__ )
118
#define tr_err( ... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, __VA_ARGS__ )
119
#define tr_inf( ... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, __VA_ARGS__ )
120
#define tr_dbg( ... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, __VA_ARGS__ )
122
int tr_wildmat( const char * text,
123
const char * pattern );
125
void tr_msgInit( void );
127
void tr_msg( const char * file,
130
const char * torrent,
132
... ) TR_GNUC_PRINTF( 5, 6 );
134
FILE* tr_getLog( void );
136
int tr_deepLoggingIsActive( void );
138
void tr_deepLog( const char * file,
142
... ) TR_GNUC_PRINTF( 4, 5 );
144
char* tr_getLogTimeStr( char * buf,
147
/** a portability wrapper for getcwd(). */
148
char* tr_getcwd( void ) TR_GNUC_MALLOC;
150
/** a portability wrapper for basename(). */
151
char* tr_basename( const char * path ) TR_GNUC_MALLOC;
153
/** a portability wrapper for dirname(). */
154
char* tr_dirname( const char * path ) TR_GNUC_MALLOC;
124
157
* a portability wrapper around mkdir().
135
169
* @return zero on success, or -1 if an error occurred
136
170
* (in which case errno is set appropriately).
138
int tr_mkdirp( const char * path, int permissions );
141
uint8_t* tr_loadFile( const char * filename, size_t * size ) TR_GNUC_MALLOC;
172
int tr_mkdirp( const char * path,
177
* Loads a file and returns its contents.
178
* On failure, NULL is returned and errno is set.
180
uint8_t* tr_loadFile( const char * filename,
181
size_t * size ) TR_GNUC_MALLOC;
144
184
/* creates a filename from a series of elements using the
145
185
correct separator for filenames. */
146
void tr_buildPath( char* buf, size_t buflen,
147
const char * first_element, ... )
148
TR_GNUC_NULL_TERMINATED;
150
struct timeval tr_timevalMsec( uint64_t milliseconds );
153
int tr_ioErrorFromErrno( int err );
155
const char * tr_errorString( int code );
186
char* tr_buildPath( const char * first_element, ... )
187
TR_GNUC_NULL_TERMINATED
192
void tr_timevalMsec( uint64_t milliseconds,
193
struct timeval * setme );
157
196
/* return the current date in milliseconds */
158
uint64_t tr_date( void );
197
uint64_t tr_date( void );
160
199
/* wait the specified number of milliseconds */
161
void tr_wait( uint64_t delay_milliseconds );
200
void tr_wait( uint64_t delay_milliseconds );
167
206
/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
168
207
case, define those here since we will use them */
170
#define MAX(a,b) ((a)>(b)?(a):(b))
209
#define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
173
#define MIN(a,b) ((a)>(b)?(b):(a))
212
#define MIN( a, b ) ( ( a ) > ( b ) ? ( b ) : ( a ) )
180
#define tr_new(struct_type, n_structs) \
181
((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
182
#define tr_new0(struct_type, n_structs) \
183
((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
184
#define tr_renew(struct_type, mem, n_structs) \
185
((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
187
void* tr_malloc ( size_t ) TR_GNUC_MALLOC;
188
void* tr_malloc0 ( size_t ) TR_GNUC_MALLOC;
189
void* tr_calloc ( size_t nmemb, size_t size ) TR_GNUC_MALLOC;
190
void tr_free ( void* );
192
char* tr_strdup( const void * str ) TR_GNUC_MALLOC;
193
char* tr_strndup( const void * str, int len ) TR_GNUC_MALLOC;
194
void* tr_memdup( const void * src, int byteCount ) TR_GNUC_MALLOC;
195
char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC;
196
char* tr_base64_encode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
197
char* tr_base64_decode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
199
size_t tr_strlcpy( char * dst, const char * src, size_t siz );
200
int tr_snprintf( char * buf, size_t buflen, const char * fmt, ... );
202
int tr_stringEndsWith( const char * string, const char * end );
219
#define tr_new( struct_type, n_structs ) \
220
( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( (\
223
#define tr_new0( struct_type, n_structs ) \
224
( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( (\
227
#define tr_renew( struct_type, mem, n_structs ) \
228
( (struct_type *) realloc ( ( mem ),\
229
( (size_t) sizeof ( struct_type ) ) * ( (\
233
void* tr_malloc( size_t ) TR_GNUC_MALLOC;
235
void* tr_malloc0( size_t ) TR_GNUC_MALLOC;
237
void tr_free( void* );
239
char* tr_strdup( const void * str ) TR_GNUC_MALLOC;
241
char* tr_strndup( const void * str,
242
int len ) TR_GNUC_MALLOC;
244
void* tr_memdup( const void * src,
245
int byteCount ) TR_GNUC_MALLOC;
247
char* tr_strdup_printf( const char * fmt,
248
... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC;
250
char* tr_base64_encode( const void * input,
252
int * outlen ) TR_GNUC_MALLOC;
254
char* tr_base64_decode( const void * input,
256
int * outlen ) TR_GNUC_MALLOC;
258
size_t tr_strlcpy( char * dst,
262
int tr_snprintf( char * buf,
265
... ) TR_GNUC_PRINTF( 3, 4 );
204
267
const char* tr_strerror( int );
269
char* tr_strstrip( char * str );
210
typedef void (tr_set_func)(void * element, void * userData );
212
void tr_set_compare( const void * a, size_t aCount,
213
const void * b, size_t bCount,
214
int compare( const void * a, const void * b ),
218
tr_set_func in_both_cb,
221
int tr_compareUint16( uint16_t a, uint16_t b );
222
int tr_compareUint32( uint32_t a, uint32_t b );
223
int tr_compareUint64( uint64_t a, uint64_t b );
224
int tr_compareDouble( double a, double b );
226
int tr_strcmp( const void * a, const void * b );
227
int tr_strcasecmp( const char * a, const char * b );
229
void tr_sha1_to_hex( char * out, const uint8_t * sha1 );
232
int tr_httpIsValidURL( const char * url );
234
int tr_httpParseURL( const char * url,
238
char ** setme_path );
275
typedef void ( tr_set_func )( void * element, void * userData );
277
void tr_set_compare( const void * a,
281
int compare( const void * a, const void * b ),
285
tr_set_func in_both_cb,
288
void tr_sha1_to_hex( char * out,
289
const uint8_t * sha1 );
292
int tr_httpIsValidURL( const char * url );
294
int tr_httpParseURL( const char * url,
298
char ** setme_path );
245
305
struct tr_bitfield
252
312
typedef struct tr_bitfield tr_bitfield;
254
314
tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
255
316
tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
256
void tr_bitfieldFree( tr_bitfield*);
258
void tr_bitfieldClear( tr_bitfield* );
259
int tr_bitfieldAdd( tr_bitfield*, size_t bit );
260
int tr_bitfieldRem( tr_bitfield*, size_t bit );
261
int tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
262
int tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
263
void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
265
int tr_bitfieldHas( const tr_bitfield*, size_t bit );
266
int tr_bitfieldIsEmpty( const tr_bitfield* );
267
size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
269
tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
318
void tr_bitfieldFree( tr_bitfield* );
320
void tr_bitfieldClear( tr_bitfield* );
322
int tr_bitfieldAdd( tr_bitfield*,
325
int tr_bitfieldRem( tr_bitfield*,
328
int tr_bitfieldAddRange( tr_bitfield *,
332
int tr_bitfieldRemRange( tr_bitfield*,
336
void tr_bitfieldDifference( tr_bitfield *,
337
const tr_bitfield * );
339
int tr_bitfieldIsEmpty( const tr_bitfield* );
341
size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
343
tr_bitfield* tr_bitfieldOr( tr_bitfield*,
344
const tr_bitfield* );
271
346
/** A stripped-down version of bitfieldHas to be used
272
347
for speed when you're looping quickly. This version
273
348
has none of tr_bitfieldHas()'s safety checks, so you
274
349
need to call tr_bitfieldTestFast() first before you
275
350
start looping. */
276
#define tr_bitfieldHasFast(bitfield,nth) \
277
(( bitfield->bits[(nth)>>3u] << ((nth)&7u) & 0x80) != 0 )
351
#define tr_bitfieldHasFast( bitfield, nth ) \
352
( ( bitfield->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 )
279
354
/** @param high the highest nth bit you're going to access */
280
#define tr_bitfieldTestFast(bitfield,high) \
281
( (bitfield) && ((bitfield)->bits) && ((high)<(bitfield)->bitCount ) )
283
double tr_getRatio( double numerator, double denominator );
355
#define tr_bitfieldTestFast( bitfield, high ) \
356
( ( bitfield ) && ( ( bitfield )->bits )\
357
&& ( ( high ) < ( bitfield )->bitCount ) )
359
#define tr_bitfieldHas( bitfield, nth ) \
360
( tr_bitfieldTestFast( bitfield, nth ) \
361
&& tr_bitfieldHasFast( bitfield, nth ) )
363
double tr_getRatio( double numerator,
364
double denominator );