source: trunk/libtransmission/utils.h @ 6627

Last change on this file since 6627 was 6627, checked in by charles, 13 years ago

(libT) remove a little more dead code

  • Property svn:keywords set to Date Rev Author Id
File size: 8.4 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 6627 2008-08-22 15:33:55Z 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/***
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 DISABLE_GETTEXT */
95#ifdef DISABLE_GETTEXT
96const char* tr_strip_positional_args( const char* fmt );
97#undef _
98#define _(a) tr_strip_positional_args(a)
99#endif
100
101#define tr_nerr( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, ## a )
102#define tr_ninf( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, ## a )
103#define tr_ndbg( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, ## a )
104
105#define tr_torerr( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, ## a )
106#define tr_torinf( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, ## a )
107#define tr_tordbg( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, ## a )
108
109#define tr_err( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, ## a )
110#define tr_inf( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, ## a )
111#define tr_dbg( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, ## a )
112
113void tr_msgInit( void );
114
115void tr_msg( const char * file, int line,
116             int level, const char * torrent,
117             const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 );
118
119FILE* tr_getLog( void );
120
121void tr_deepLog( const char * file, int line,
122                 const char * name,
123                 const char * fmt, ... ) TR_GNUC_PRINTF( 4, 5 );
124
125char* tr_getLogTimeStr( char * buf, int buflen );
126
127/**
128 * a portability wrapper around mkdir().
129 * On WIN32, the `permissions' argument is unused.
130 *
131 * @return zero on success, or -1 if an error occurred
132 * (in which case errno is set appropriately).
133 */
134int tr_mkdir( const char * path, int permissions );
135
136/**
137 * Like mkdir, but makes parent directories as needed.
138 *
139 * @return zero on success, or -1 if an error occurred
140 * (in which case errno is set appropriately).
141 */
142int tr_mkdirp( const char * path, int permissions );
143
144
145uint8_t* tr_loadFile( const char * filename, size_t * size ) TR_GNUC_MALLOC;
146
147
148/* creates a filename from a series of elements using the
149   correct separator for filenames. */
150void tr_buildPath( char* buf, size_t buflen,
151                   const char * first_element, ... )
152                                      TR_GNUC_NULL_TERMINATED;
153
154struct timeval tr_timevalMsec( uint64_t milliseconds );
155
156
157int tr_ioErrorFromErrno( int err );
158
159const char * tr_errorString( int code );
160
161/* return the current date in milliseconds */
162uint64_t tr_date( void );
163
164/* wait the specified number of milliseconds */
165void tr_wait( uint64_t delay_milliseconds );
166
167/***
168****
169***/
170
171/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
172   case, define those here since we will use them */
173#ifndef MAX
174#define MAX(a,b) ((a)>(b)?(a):(b))
175#endif
176#ifndef MIN
177#define MIN(a,b) ((a)>(b)?(b):(a))
178#endif
179
180/***
181****
182***/
183
184#define tr_new(struct_type, n_structs)           \
185    ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
186#define tr_new0(struct_type, n_structs)          \
187    ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
188#define tr_renew(struct_type, mem, n_structs)    \
189    ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
190
191void* tr_malloc  ( size_t ) TR_GNUC_MALLOC;
192void* tr_malloc0 ( size_t ) TR_GNUC_MALLOC;
193void  tr_free    ( void* );
194
195char* tr_strdup( const void * str ) TR_GNUC_MALLOC;
196char* tr_strndup( const void * str, int len ) TR_GNUC_MALLOC;
197void* tr_memdup( const void * src, int byteCount ) TR_GNUC_MALLOC;
198char* tr_strdup_printf( const char * fmt, ... )  TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC;
199char* tr_base64_encode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
200char* tr_base64_decode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
201
202size_t tr_strlcpy( char * dst, const void * src, size_t siz );
203int tr_snprintf( char * buf, size_t buflen, const char * fmt, ... );
204
205const char* tr_strerror( int );
206
207/***
208****
209***/
210
211typedef void (tr_set_func)(void * element, void * userData );
212
213void tr_set_compare( const void * a, size_t aCount,
214                     const void * b, size_t bCount,
215                     int compare( const void * a, const void * b ),
216                     size_t elementSize,
217                     tr_set_func in_a_cb,
218                     tr_set_func in_b_cb,
219                     tr_set_func in_both_cb,
220                     void * userData );
221
222void tr_sha1_to_hex( char * out, const uint8_t * sha1 );
223
224
225int tr_httpIsValidURL( const char * url );
226
227int tr_httpParseURL( const char * url,
228                     int          url_len,
229                     char     ** setme_host,
230                     int       * setme_port,
231                     char     ** setme_path );
232
233
234/***
235****
236***/
237
238struct tr_bitfield
239{
240    uint8_t * bits;
241    size_t bitCount;
242    size_t byteCount;
243};
244
245typedef struct tr_bitfield tr_bitfield;
246
247tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
248tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
249void tr_bitfieldFree( tr_bitfield*);
250
251void tr_bitfieldClear( tr_bitfield* );
252int  tr_bitfieldAdd( tr_bitfield*, size_t bit );
253int  tr_bitfieldRem( tr_bitfield*, size_t bit );
254int  tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
255int  tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
256void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
257
258int    tr_bitfieldIsEmpty( const tr_bitfield* );
259size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
260
261tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
262
263/** A stripped-down version of bitfieldHas to be used
264    for speed when you're looping quickly.  This version
265    has none of tr_bitfieldHas()'s safety checks, so you
266    need to call tr_bitfieldTestFast() first before you
267    start looping. */
268#define tr_bitfieldHasFast(bitfield,nth) \
269    (( bitfield->bits[(nth)>>3u] << ((nth)&7u) & 0x80) != 0 )
270
271/** @param high the highest nth bit you're going to access */
272#define tr_bitfieldTestFast(bitfield,high) \
273    ( (bitfield) && ((bitfield)->bits) && ((high)<(bitfield)->bitCount ) )
274
275#define tr_bitfieldHas(bitfield,nth) \
276    ( tr_bitfieldTestFast( bitfield, nth ) && \
277      tr_bitfieldHasFast( bitfield, nth ) )
278
279double tr_getRatio( double numerator, double denominator );
280
281#endif
Note: See TracBrowser for help on using the repository browser.