source: trunk/libtransmission/utils.h @ 798

Last change on this file since 798 was 798, checked in by joshe, 15 years ago

Allow frontend to change message output level.
Rename tr_setErrorFunction() to tr_setMessageFunction()

  • Property svn:keywords set to Date Rev Author Id
File size: 5.4 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 798 2006-08-20 18:15:25Z joshe $
3 *
4 * Copyright (c) 2005-2006 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#define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a )
29#define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a )
30#define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a )
31void tr_msg  ( int level, char * msg, ... );
32
33int  tr_rand ( int );
34
35void * tr_memmem( const void *, size_t, const void *, size_t );
36
37/***********************************************************************
38 * tr_mkdir
39 ***********************************************************************
40 * Create a directory and any needed parent directories.
41 * Note that the string passed in must be writable!
42 **********************************************************************/
43int tr_mkdir( char * path );
44
45/***********************************************************************
46 * tr_date
47 ***********************************************************************
48 * Returns the current date in milliseconds
49 **********************************************************************/
50static inline uint64_t tr_date()
51{
52    struct timeval tv;
53    gettimeofday( &tv, NULL );
54    return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
55}
56
57/***********************************************************************
58 * tr_wait
59 ***********************************************************************
60 * Wait 'delay' milliseconds
61 **********************************************************************/
62static inline void tr_wait( uint64_t delay )
63{
64#ifdef SYS_BEOS
65    snooze( 1000 * delay );
66#else
67    usleep( 1000 * delay );
68#endif
69}
70
71/***********************************************************************
72 * tr_bitfieldHas
73 **********************************************************************/
74static inline int tr_bitfieldHas( uint8_t * bitfield, int piece )
75{
76    return ( bitfield[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
77}
78
79/***********************************************************************
80 * tr_bitfieldAdd
81 **********************************************************************/
82static inline void tr_bitfieldAdd( uint8_t * bitfield, int piece )
83{
84    bitfield[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) );
85}
86
87static inline void tr_bitfieldRem( uint8_t * bitfield, int piece )
88{
89    bitfield[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) );
90}
91
92#define tr_blockPiece(a) _tr_blockPiece(tor,a)
93static inline int _tr_blockPiece( tr_torrent_t * tor, int block )
94{
95    tr_info_t * inf = &tor->info;
96    return block / ( inf->pieceSize / tor->blockSize );
97}
98
99#define tr_blockSize(a) _tr_blockSize(tor,a)
100static inline int _tr_blockSize( tr_torrent_t * tor, int block )
101{
102    tr_info_t * inf = &tor->info;
103    int dummy;
104
105    if( block != tor->blockCount - 1 ||
106        !( dummy = inf->totalSize % tor->blockSize ) )
107    {
108        return tor->blockSize;
109    }
110
111    return dummy;
112}
113
114#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
115static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block )
116{
117    tr_info_t * inf = &tor->info;
118    return tor->blockSize *
119        ( block % ( inf->pieceSize / tor->blockSize ) );
120}
121
122#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
123static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece )
124{
125    tr_info_t * inf = &tor->info;
126    if( piece < inf->pieceCount - 1 ||
127        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
128    {
129        return inf->pieceSize / tor->blockSize;
130    }
131    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
132}
133
134#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
135static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece )
136{
137    tr_info_t * inf = &tor->info;
138    return piece * ( inf->pieceSize / tor->blockSize );
139}
140
141#define tr_pieceSize(a) _tr_pieceSize(tor,a)
142static inline int _tr_pieceSize( tr_torrent_t * tor, int piece )
143{
144    tr_info_t * inf = &tor->info;
145    if( piece < inf->pieceCount - 1 ||
146        !( inf->totalSize % inf->pieceSize ) )
147    {
148        return inf->pieceSize;
149    }
150    return inf->totalSize % inf->pieceSize;
151}
152
153#define tr_block(a,b) _tr_block(tor,a,b)
154static inline int _tr_block( tr_torrent_t * tor, int index, int begin )
155{
156    tr_info_t * inf = &tor->info;
157    return index * ( inf->pieceSize / tor->blockSize ) +
158        begin / tor->blockSize;
159}
160
161#endif
Note: See TracBrowser for help on using the repository browser.