source: trunk/libtransmission/utils.h @ 6792

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

(wx) get the wxWidgets port building again. :)

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