source: trunk/libtransmission/utils.h @ 920

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

Merge nat-traversal branch to trunk.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.7 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 920 2006-09-25 18:37:45Z 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
28void tr_msgInit( void );
29
30#define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a )
31#define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a )
32#define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a )
33void tr_msg  ( int level, char * msg, ... ) PRINTF( 2, 3 );
34
35int  tr_rand ( int );
36
37void * tr_memmem( const void *, size_t, const void *, size_t );
38
39/***********************************************************************
40 * tr_mkdir
41 ***********************************************************************
42 * Create a directory and any needed parent directories.
43 * Note that the string passed in must be writable!
44 **********************************************************************/
45int tr_mkdir( char * path );
46
47/***********************************************************************
48 * tr_strcasecmp
49 ***********************************************************************
50 * A case-insensitive strncmp()
51 **********************************************************************/
52#define tr_strcasecmp( ff, ss ) ( tr_strncasecmp( (ff), (ss), -1 ) )
53int tr_strncasecmp( const char * first, const char * second, int len );
54
55/***********************************************************************
56 * tr_sprintf
57 ***********************************************************************
58 * Appends to the end of a buffer using printf formatting,
59 * growing the buffer if needed
60 **********************************************************************/
61int tr_sprintf( char ** buf, int * used, int * max,
62                const char * format, ... ) PRINTF( 4, 5 );
63/* gee, it sure would be nice if BeOS had va_copy() */
64int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list );
65
66/***********************************************************************
67 * tr_dupstr
68 ***********************************************************************
69 * Creates a nul-terminated string
70 **********************************************************************/
71char * tr_dupstr( const char * base, int len );
72
73/***********************************************************************
74 * tr_date
75 ***********************************************************************
76 * Returns the current date in milliseconds
77 **********************************************************************/
78static inline uint64_t tr_date()
79{
80    struct timeval tv;
81    gettimeofday( &tv, NULL );
82    return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
83}
84
85/***********************************************************************
86 * tr_wait
87 ***********************************************************************
88 * Wait 'delay' milliseconds
89 **********************************************************************/
90static inline void tr_wait( uint64_t delay )
91{
92#ifdef SYS_BEOS
93    snooze( 1000 * delay );
94#else
95    usleep( 1000 * delay );
96#endif
97}
98
99/***********************************************************************
100 * tr_bitfieldHas
101 **********************************************************************/
102static inline int tr_bitfieldHas( uint8_t * bitfield, int piece )
103{
104    return ( bitfield[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
105}
106
107/***********************************************************************
108 * tr_bitfieldAdd
109 **********************************************************************/
110static inline void tr_bitfieldAdd( uint8_t * bitfield, int piece )
111{
112    bitfield[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) );
113}
114
115static inline void tr_bitfieldRem( uint8_t * bitfield, int piece )
116{
117    bitfield[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) );
118}
119
120#define tr_blockPiece(a) _tr_blockPiece(tor,a)
121static inline int _tr_blockPiece( tr_torrent_t * tor, int block )
122{
123    tr_info_t * inf = &tor->info;
124    return block / ( inf->pieceSize / tor->blockSize );
125}
126
127#define tr_blockSize(a) _tr_blockSize(tor,a)
128static inline int _tr_blockSize( tr_torrent_t * tor, int block )
129{
130    tr_info_t * inf = &tor->info;
131    int dummy;
132
133    if( block != tor->blockCount - 1 ||
134        !( dummy = inf->totalSize % tor->blockSize ) )
135    {
136        return tor->blockSize;
137    }
138
139    return dummy;
140}
141
142#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
143static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block )
144{
145    tr_info_t * inf = &tor->info;
146    return tor->blockSize *
147        ( block % ( inf->pieceSize / tor->blockSize ) );
148}
149
150#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
151static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece )
152{
153    tr_info_t * inf = &tor->info;
154    if( piece < inf->pieceCount - 1 ||
155        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
156    {
157        return inf->pieceSize / tor->blockSize;
158    }
159    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
160}
161
162#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
163static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece )
164{
165    tr_info_t * inf = &tor->info;
166    return piece * ( inf->pieceSize / tor->blockSize );
167}
168
169#define tr_pieceSize(a) _tr_pieceSize(tor,a)
170static inline int _tr_pieceSize( tr_torrent_t * tor, int piece )
171{
172    tr_info_t * inf = &tor->info;
173    if( piece < inf->pieceCount - 1 ||
174        !( inf->totalSize % inf->pieceSize ) )
175    {
176        return inf->pieceSize;
177    }
178    return inf->totalSize % inf->pieceSize;
179}
180
181#define tr_block(a,b) _tr_block(tor,a,b)
182static inline int _tr_block( tr_torrent_t * tor, int index, int begin )
183{
184    tr_info_t * inf = &tor->info;
185    return index * ( inf->pieceSize / tor->blockSize ) +
186        begin / tor->blockSize;
187}
188
189#endif
Note: See TracBrowser for help on using the repository browser.