source: trunk/libtransmission/utils.h @ 5193

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

first cut at adding i18n hooks into libtransmission.

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