source: trunk/libtransmission/utils.h @ 5065

Last change on this file since 5065 was 5065, checked in by charles, 14 years ago

cleanup #includes for errno & strerror

  • Property svn:keywords set to Date Rev Author Id
File size: 5.4 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 5065 2008-02-19 04:16:04Z charles $
3 *
4 * Copyright (c) 2005-2008 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( __FILE__, __LINE__, TR_MSG_ERR, ## a )
36#define tr_inf( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, ## a )
37#define tr_dbg( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, ## a )
38void tr_msg  ( const char * file, int line, int level, const char * msg, ... );
39FILE* tr_getLog( void );
40
41char* tr_getLogTimeStr( char * buf, int buflen );
42
43/** Returns a random number in the range of [0...n) */
44int tr_rand ( int n );
45
46/**
47 * a portability wrapper around mkdir().
48 * On WIN32, the `permissions' argument is unused.
49 *
50 * @return zero on success, or -1 if an error occurred
51 * (in which case errno is set appropriately).
52 */
53int tr_mkdir( const char * path, int permissions );
54
55/**
56 * Like mkdir, but makes parent directories as needed.
57 *
58 * @return zero on success, or -1 if an error occurred
59 * (in which case errno is set appropriately).
60 */
61int tr_mkdirp( const char * path, int permissions );
62
63
64uint8_t* tr_loadFile( const char * filename, size_t * size );
65
66int tr_vasprintf( char **strp, const char *fmt, va_list ap );
67
68int tr_asprintf( char **strp, const char *fmt, ...);
69
70
71/* creates a filename from a series of elements using the
72   correct separator for filenames. */
73void tr_buildPath( char* buf, size_t buflen,
74                   const char * first_element, ... );
75
76struct timeval timevalMsec( uint64_t milliseconds );
77
78
79int tr_ioErrorFromErrno( int err );
80
81const char * tr_errorString( int code );
82
83/* return the current date in milliseconds */
84uint64_t tr_date( void );
85
86/* wait the specified number of milliseconds */
87void tr_wait( uint64_t delay_milliseconds );
88
89/***
90****
91***/
92
93/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
94   case, define those here since we will use them */
95#ifndef MAX
96#define MAX(a,b) ((a)>(b)?(a):(b))
97#endif
98#ifndef MIN
99#define MIN(a,b) ((a)>(b)?(b):(a))
100#endif
101
102/***
103****
104***/
105
106#define tr_new(struct_type, n_structs)           \
107    ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
108#define tr_new0(struct_type, n_structs)          \
109    ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
110#define tr_renew(struct_type, mem, n_structs)    \
111    ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
112
113void* tr_malloc  ( size_t );
114void* tr_malloc0 ( size_t );
115void* tr_calloc  ( size_t nmemb, size_t size );
116void  tr_free    ( void* );
117
118char* tr_strdup( const char * str );
119char* tr_strndup( const char * str, int len );
120
121const char* tr_strerror( int );
122
123/***
124****
125***/
126
127typedef void (tr_set_func)(void * element, void * userData );
128
129void tr_set_compare( const void * a, size_t aCount,
130                     const void * b, size_t bCount,
131                     int compare( const void * a, const void * b ),
132                     size_t elementSize,
133                     tr_set_func in_a_cb,
134                     tr_set_func in_b_cb,
135                     tr_set_func in_both_cb,
136                     void * userData );
137                   
138int tr_compareUint16( uint16_t a, uint16_t b );
139int tr_compareUint32( uint32_t a, uint32_t b );
140
141/***
142****
143***/
144
145struct tr_bitfield
146{
147    uint8_t * bits;
148    size_t len;
149};
150
151typedef struct tr_bitfield tr_bitfield;
152typedef struct tr_bitfield tr_bitfield_t;
153
154tr_bitfield* tr_bitfieldNew( size_t bitcount );
155tr_bitfield* tr_bitfieldDup( const tr_bitfield* );
156void tr_bitfieldFree( tr_bitfield*);
157
158void tr_bitfieldClear( tr_bitfield* );
159void tr_bitfieldAdd( tr_bitfield*, size_t bit );
160void tr_bitfieldRem( tr_bitfield*, size_t bit );
161void tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
162void tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
163
164int    tr_bitfieldHas( const tr_bitfield*, size_t bit );
165int    tr_bitfieldIsEmpty( const tr_bitfield* );
166size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
167
168tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
169
170double tr_getRatio( double numerator, double denominator );
171
172#endif
Note: See TracBrowser for help on using the repository browser.