2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-03-23 08:28:01 +00:00
|
|
|
* Copyright (c) 2005-2007 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef TR_UTILS_H
|
|
|
|
#define TR_UTILS_H 1
|
|
|
|
|
2007-08-04 00:43:47 +00:00
|
|
|
#include <inttypes.h>
|
2007-07-14 05:28:35 +00:00
|
|
|
#include <stdarg.h>
|
2007-08-14 20:45:23 +00:00
|
|
|
#include <stddef.h> /* for size_t */
|
2007-09-20 23:33:46 +00:00
|
|
|
#include <stdio.h> /* FILE* */
|
2007-07-14 05:28:35 +00:00
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
void tr_msgInit( void );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a )
|
|
|
|
#define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a )
|
|
|
|
#define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a )
|
2007-07-19 11:54:37 +00:00
|
|
|
void tr_msg ( int level, char * msg, ... );
|
2007-09-20 23:33:46 +00:00
|
|
|
FILE* tr_getLog( void );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
int tr_rand ( int );
|
|
|
|
|
|
|
|
void * tr_memmem( const void *, size_t, const void *, size_t );
|
|
|
|
|
|
|
|
/***********************************************************************
|
2007-08-01 00:40:49 +00:00
|
|
|
* tr_mkdirp
|
2006-07-16 19:39:23 +00:00
|
|
|
***********************************************************************
|
|
|
|
* Create a directory and any needed parent directories.
|
|
|
|
* Note that the string passed in must be writable!
|
|
|
|
**********************************************************************/
|
2007-08-01 00:40:49 +00:00
|
|
|
int tr_mkdirp( char * path, int permissions );
|
|
|
|
|
|
|
|
int tr_mkdir( const char * path, int permissions );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_strcasecmp
|
|
|
|
***********************************************************************
|
|
|
|
* A case-insensitive strncmp()
|
|
|
|
**********************************************************************/
|
2007-06-18 03:40:41 +00:00
|
|
|
#define tr_strcasecmp( ff, ss ) ( tr_strncasecmp( (ff), (ss), ULONG_MAX ) )
|
|
|
|
int tr_strncasecmp( const char * first, const char * second, size_t len );
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_sprintf
|
|
|
|
***********************************************************************
|
|
|
|
* Appends to the end of a buffer using printf formatting,
|
|
|
|
* growing the buffer if needed
|
|
|
|
**********************************************************************/
|
|
|
|
int tr_sprintf( char ** buf, int * used, int * max,
|
2007-07-19 11:54:37 +00:00
|
|
|
const char * format, ... );
|
2006-09-25 18:37:45 +00:00
|
|
|
/* gee, it sure would be nice if BeOS had va_copy() */
|
|
|
|
int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list );
|
2007-01-28 23:26:57 +00:00
|
|
|
/* this concatenates some binary data onto the end of a buffer */
|
|
|
|
int tr_concat( char ** buf, int * used, int * max,
|
|
|
|
const char * data, int len );
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
/* creates a filename from a series of elements using the
|
|
|
|
correct separator for filenames. */
|
|
|
|
void tr_buildPath ( char* buf, size_t buflen,
|
|
|
|
const char * first_element, ... );
|
|
|
|
|
2007-08-23 20:33:01 +00:00
|
|
|
struct timeval timevalSec ( int seconds );
|
|
|
|
struct timeval timevalMsec ( int milliseconds );
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-04-20 02:05:07 +00:00
|
|
|
int tr_ioErrorFromErrno( void );
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
char * tr_errorString( int code );
|
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
/* return the current date in milliseconds */
|
|
|
|
uint64_t tr_date( void );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
/* wait the specified number of milliseconds */
|
|
|
|
void tr_wait( uint64_t delay_milliseconds );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-15 23:02:56 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* strlcat_utf8
|
|
|
|
***********************************************************************
|
|
|
|
* According to the official specification, all strings in the torrent
|
|
|
|
* file are supposed to be UTF-8 encoded. However, there are
|
|
|
|
* non-compliant torrents around... If we encounter an invalid UTF-8
|
|
|
|
* character, we assume it is ISO 8859-1 and convert it to UTF-8.
|
|
|
|
**********************************************************************/
|
|
|
|
void strlcat_utf8( void *, const void *, size_t, char );
|
|
|
|
size_t bufsize_utf8( const void *, int * );
|
|
|
|
|
2007-07-19 11:54:37 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-19 11:54:37 +00:00
|
|
|
/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
|
|
|
|
case, define those here since we will use them */
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) ((a)>(b)?(b):(a))
|
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
#define tr_new(struct_type, n_structs) \
|
|
|
|
((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
|
|
|
|
#define tr_new0(struct_type, n_structs) \
|
|
|
|
((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
|
2007-06-29 05:45:17 +00:00
|
|
|
#define tr_renew(struct_type, mem, n_structs) \
|
|
|
|
((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
|
2007-06-27 05:14:38 +00:00
|
|
|
|
|
|
|
void* tr_malloc ( size_t );
|
|
|
|
void* tr_malloc0 ( size_t );
|
|
|
|
void* tr_calloc ( size_t nmemb, size_t size );
|
|
|
|
void tr_free ( void* );
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
char* tr_strdup( const char * str );
|
|
|
|
char* tr_strndup( const char * str, int len );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
typedef void (tr_set_func)(void * element, void * userData );
|
|
|
|
|
|
|
|
void tr_set_compare( const void * a, size_t aCount,
|
|
|
|
const void * b, size_t bCount,
|
|
|
|
int compare( const void * a, const void * b ),
|
|
|
|
size_t elementSize,
|
|
|
|
tr_set_func in_a_cb,
|
|
|
|
tr_set_func in_b_cb,
|
|
|
|
tr_set_func in_both_cb,
|
|
|
|
void * userData );
|
|
|
|
|
|
|
|
int tr_compareUint8 ( uint8_t a, uint8_t b );
|
|
|
|
int tr_compareUint16( uint16_t a, uint16_t b );
|
|
|
|
int tr_compareUint32( uint32_t a, uint32_t b );
|
|
|
|
int tr_compareUint64( uint64_t a, uint64_t b );
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct tr_bitfield
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
uint8_t * bits;
|
|
|
|
size_t len;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct tr_bitfield tr_bitfield;
|
|
|
|
typedef struct tr_bitfield tr_bitfield_t;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield* tr_bitfieldNew( size_t bitcount );
|
|
|
|
tr_bitfield* tr_bitfieldDup( const tr_bitfield* );
|
|
|
|
void tr_bitfieldFree( tr_bitfield*);
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_bitfieldClear( tr_bitfield* );
|
|
|
|
void tr_bitfieldAdd( tr_bitfield*, size_t bit );
|
|
|
|
void tr_bitfieldRem( tr_bitfield*, size_t bit );
|
|
|
|
void tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
|
|
|
|
void tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-25 17:43:39 +00:00
|
|
|
int tr_bitfieldHas( const tr_bitfield*, size_t bit );
|
2007-09-20 16:32:01 +00:00
|
|
|
int tr_bitfieldIsEmpty( const tr_bitfield* );
|
|
|
|
size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield* tr_bitfieldNegate( tr_bitfield* );
|
|
|
|
tr_bitfield* tr_bitfieldAnd( tr_bitfield*, const tr_bitfield* );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|