source: trunk/libtransmission/utils.h @ 3164

Last change on this file since 3164 was 3164, checked in by charles, 15 years ago

tweak the bitfield code to be a little faster.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.8 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 3164 2007-09-25 17:43:39Z charles $
3 *
4 * Copyright (c) 2005-2007 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
33void tr_msgInit( void );
34
35#define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a )
36#define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a )
37#define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a )
38void tr_msg  ( int level, char * msg, ... );
39FILE* tr_getLog( void );
40
41int  tr_rand ( int );
42
43void * tr_memmem( const void *, size_t, const void *, size_t );
44
45/***********************************************************************
46 * tr_mkdirp
47 ***********************************************************************
48 * Create a directory and any needed parent directories.
49 * Note that the string passed in must be writable!
50 **********************************************************************/
51int tr_mkdirp( char * path, int permissions );
52
53int tr_mkdir( const char * path, int permissions );
54
55/***********************************************************************
56 * tr_strcasecmp
57 ***********************************************************************
58 * A case-insensitive strncmp()
59 **********************************************************************/
60#define tr_strcasecmp( ff, ss ) ( tr_strncasecmp( (ff), (ss), ULONG_MAX ) )
61int tr_strncasecmp( const char * first, const char * second, size_t len );
62
63/***********************************************************************
64 * tr_sprintf
65 ***********************************************************************
66 * Appends to the end of a buffer using printf formatting,
67 * growing the buffer if needed
68 **********************************************************************/
69int tr_sprintf( char ** buf, int * used, int * max,
70                const char * format, ... );
71/* gee, it sure would be nice if BeOS had va_copy() */
72int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list );
73/* this concatenates some binary data onto the end of a buffer */
74int tr_concat( char ** buf, int * used, int * max,
75               const char * data, int len );
76
77/* creates a filename from a series of elements using the
78   correct separator for filenames. */
79void tr_buildPath ( char* buf, size_t buflen,
80                    const char * first_element, ... );
81
82struct timeval timevalSec ( int seconds );
83struct timeval timevalMsec ( int milliseconds );
84
85
86int    tr_ioErrorFromErrno( void );
87
88char * tr_errorString( int code );
89
90/* return the current date in milliseconds */
91uint64_t tr_date( void );
92
93/* wait the specified number of milliseconds */
94void tr_wait( uint64_t delay_milliseconds );
95
96/***********************************************************************
97 * strlcat_utf8
98 ***********************************************************************
99 * According to the official specification, all strings in the torrent
100 * file are supposed to be UTF-8 encoded. However, there are
101 * non-compliant torrents around... If we encounter an invalid UTF-8
102 * character, we assume it is ISO 8859-1 and convert it to UTF-8.
103 **********************************************************************/
104void strlcat_utf8( void *, const void *, size_t, char );
105size_t bufsize_utf8( const void *, int * );
106
107/***
108****
109***/
110
111/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
112   case, define those here since we will use them */
113#ifndef MAX
114#define MAX(a,b) ((a)>(b)?(a):(b))
115#endif
116#ifndef MIN
117#define MIN(a,b) ((a)>(b)?(b):(a))
118#endif
119
120/***
121****
122***/
123
124#define tr_new(struct_type, n_structs)           \
125    ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
126#define tr_new0(struct_type, n_structs)          \
127    ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
128#define tr_renew(struct_type, mem, n_structs)    \
129    ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
130
131void* tr_malloc  ( size_t );
132void* tr_malloc0 ( size_t );
133void* tr_calloc  ( size_t nmemb, size_t size );
134void  tr_free    ( void* );
135
136char* tr_strdup( const char * str );
137char* tr_strndup( const char * str, int len );
138
139/***
140****
141***/
142
143typedef void (tr_set_func)(void * element, void * userData );
144
145void tr_set_compare( const void * a, size_t aCount,
146                     const void * b, size_t bCount,
147                     int compare( const void * a, const void * b ),
148                     size_t elementSize,
149                     tr_set_func in_a_cb,
150                     tr_set_func in_b_cb,
151                     tr_set_func in_both_cb,
152                     void * userData );
153                   
154int tr_compareUint8 (  uint8_t a,  uint8_t b );
155int tr_compareUint16( uint16_t a, uint16_t b );
156int tr_compareUint32( uint32_t a, uint32_t b );
157int tr_compareUint64( uint64_t a, uint64_t b );
158
159/***
160****
161***/
162
163struct tr_bitfield
164{
165    uint8_t * bits;
166    size_t len;
167};
168
169typedef struct tr_bitfield tr_bitfield;
170typedef struct tr_bitfield tr_bitfield_t;
171
172tr_bitfield* tr_bitfieldNew( size_t bitcount );
173tr_bitfield* tr_bitfieldDup( const tr_bitfield* );
174void tr_bitfieldFree( tr_bitfield*);
175
176void tr_bitfieldClear( tr_bitfield* );
177void tr_bitfieldAdd( tr_bitfield*, size_t bit );
178void tr_bitfieldRem( tr_bitfield*, size_t bit );
179void tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
180void tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
181
182int    tr_bitfieldHas( const tr_bitfield*, size_t bit );
183int    tr_bitfieldIsEmpty( const tr_bitfield* );
184size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
185
186tr_bitfield* tr_bitfieldNegate( tr_bitfield* );
187tr_bitfield* tr_bitfieldAnd( tr_bitfield*, const tr_bitfield* );
188
189#endif
Note: See TracBrowser for help on using the repository browser.