source: trunk/libtransmission/utils.h @ 346

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

Add support to libtransmission and the MacOS X GUI for saving private copies of

torrent files.

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