source: trunk/libtransmission/utils.h @ 5759

Last change on this file since 5759 was 5759, checked in by charles, 14 years ago

#917: when seeding, drop peers that have everything you have (instead of just dropping seeds)

  • Property svn:keywords set to Date Rev Author Id
File size: 6.4 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 5759 2008-05-06 19:06:46Z 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
33void tr_msgInit( void );
34
35#if !defined(_)
36#if defined(SYS_DARWIN)
37  #define _(a) (a)
38#elif HAVE_LIBINTL
39  #include <libintl.h>
40  #define _(a) gettext (a)
41#else
42  #define _(a) (a)
43#endif
44#endif
45
46#define tr_nerr( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, ## a )
47#define tr_ninf( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, ## a )
48#define tr_ndbg( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, ## a )
49
50#define tr_torerr( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, ## a )
51#define tr_torinf( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, ## a )
52#define tr_tordbg( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, ## a )
53
54#define tr_err( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, ## a )
55#define tr_inf( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, ## a )
56#define tr_dbg( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, ## a )
57void tr_msg  ( const char * file, int line, int level, const char * torrent, const char * fmt, ... );
58FILE* tr_getLog( void );
59
60void tr_deepLog( const char * file, int line, const char * name, const char * fmt, ... ); 
61
62char* tr_getLogTimeStr( char * buf, int buflen );
63
64/** Returns a random number in the range of [0...n) */
65int tr_rand ( int n );
66
67/**
68 * a portability wrapper around mkdir().
69 * On WIN32, the `permissions' argument is unused.
70 *
71 * @return zero on success, or -1 if an error occurred
72 * (in which case errno is set appropriately).
73 */
74int tr_mkdir( const char * path, int permissions );
75
76/**
77 * Like mkdir, but makes parent directories as needed.
78 *
79 * @return zero on success, or -1 if an error occurred
80 * (in which case errno is set appropriately).
81 */
82int tr_mkdirp( const char * path, int permissions );
83
84
85uint8_t* tr_loadFile( const char * filename, size_t * size );
86
87
88/* creates a filename from a series of elements using the
89   correct separator for filenames. */
90void tr_buildPath( char* buf, size_t buflen,
91                   const char * first_element, ... );
92
93struct timeval tr_timevalMsec( uint64_t milliseconds );
94
95
96int tr_ioErrorFromErrno( int err );
97
98const char * tr_errorString( int code );
99
100/* return the current date in milliseconds */
101uint64_t tr_date( void );
102
103/* wait the specified number of milliseconds */
104void tr_wait( uint64_t delay_milliseconds );
105
106/***
107****
108***/
109
110/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
111   case, define those here since we will use them */
112#ifndef MAX
113#define MAX(a,b) ((a)>(b)?(a):(b))
114#endif
115#ifndef MIN
116#define MIN(a,b) ((a)>(b)?(b):(a))
117#endif
118
119/***
120****
121***/
122
123#define tr_new(struct_type, n_structs)           \
124    ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
125#define tr_new0(struct_type, n_structs)          \
126    ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
127#define tr_renew(struct_type, mem, n_structs)    \
128    ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
129
130void* tr_malloc  ( size_t );
131void* tr_malloc0 ( size_t );
132void* tr_calloc  ( size_t nmemb, size_t size );
133void  tr_free    ( void* );
134
135char* tr_strdup( const char * str );
136char* tr_strndup( const char * str, int len );
137
138const char* tr_strerror( int );
139
140/***
141****
142***/
143
144typedef void (tr_set_func)(void * element, void * userData );
145
146void tr_set_compare( const void * a, size_t aCount,
147                     const void * b, size_t bCount,
148                     int compare( const void * a, const void * b ),
149                     size_t elementSize,
150                     tr_set_func in_a_cb,
151                     tr_set_func in_b_cb,
152                     tr_set_func in_both_cb,
153                     void * userData );
154                   
155int tr_compareUint16( uint16_t a, uint16_t b );
156int tr_compareUint32( uint32_t a, uint32_t b );
157
158void tr_sha1_to_hex( char * out, const uint8_t * sha1 );
159
160
161int tr_httpIsValidURL( const char * url );
162
163int tr_httpParseURL( const char * url,
164                     int          url_len,
165                     char     ** setme_host,
166                     int       * setme_port,
167                     char     ** setme_path );
168
169
170/***
171****
172***/
173
174struct tr_bitfield
175{
176    uint8_t * bits;
177    size_t len;
178};
179
180typedef struct tr_bitfield tr_bitfield;
181typedef struct tr_bitfield tr_bitfield_t;
182
183tr_bitfield* tr_bitfieldNew( size_t bitcount );
184tr_bitfield* tr_bitfieldDup( const tr_bitfield* );
185void tr_bitfieldFree( tr_bitfield*);
186
187void tr_bitfieldClear( tr_bitfield* );
188int  tr_bitfieldAdd( tr_bitfield*, size_t bit );
189int  tr_bitfieldRem( tr_bitfield*, size_t bit );
190int  tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
191int  tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
192void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
193
194int    tr_bitfieldHas( const tr_bitfield*, size_t bit );
195int    tr_bitfieldIsEmpty( const tr_bitfield* );
196size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
197
198tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
199
200double tr_getRatio( double numerator, double denominator );
201
202#endif
Note: See TracBrowser for help on using the repository browser.