source: branches/0.9x/libtransmission/utils.h @ 3649

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

part 1 of the data corruption fix: plug the leaky abstraction in fdlimit

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