2009-01-07 06:53:29 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2009-01-07 06:53:29 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2009-08-10 20:04:08 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2009-01-07 06:53:29 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#ifndef TR_UTILS_H
|
|
|
|
#define TR_UTILS_H 1
|
|
|
|
|
2007-08-04 00:43:47 +00:00
|
|
|
#include <inttypes.h>
|
2009-01-02 20:12:23 +00:00
|
|
|
#include <stddef.h> /* size_t */
|
2009-11-26 18:47:08 +00:00
|
|
|
#include <time.h> /* time_t */
|
2007-07-14 05:28:35 +00:00
|
|
|
|
2008-09-19 17:17:34 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup utils Utilities
|
2009-08-10 20:04:08 +00:00
|
|
|
* @{
|
2009-05-29 19:17:12 +00:00
|
|
|
*/
|
|
|
|
|
2008-10-23 02:37:21 +00:00
|
|
|
#ifndef UNUSED
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define UNUSED __attribute__ ( ( unused ) )
|
|
|
|
#else
|
|
|
|
#define UNUSED
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TR_GNUC_PRINTF
|
|
|
|
#ifdef __GNUC__
|
2009-11-01 01:35:06 +00:00
|
|
|
#define TR_GNUC_PRINTF( fmt, args ) __attribute__ ( ( format ( printf, fmt, args ) ) )
|
2008-10-23 02:37:21 +00:00
|
|
|
#else
|
|
|
|
#define TR_GNUC_PRINTF( fmt, args )
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-11-01 01:35:06 +00:00
|
|
|
#ifndef TR_GNUC_NONNULL
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define TR_GNUC_NONNULL( ... ) __attribute__((nonnull (__VA_ARGS__)))
|
|
|
|
#else
|
|
|
|
#define TR_GNUC_NONNULL( ... )
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2008-10-23 02:37:21 +00:00
|
|
|
#ifndef TR_GNUC_NULL_TERMINATED
|
2009-11-02 01:10:04 +00:00
|
|
|
#if __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 3 )
|
2008-10-23 02:37:21 +00:00
|
|
|
#define TR_GNUC_NULL_TERMINATED __attribute__ ( ( __sentinel__ ) )
|
2009-11-01 01:35:06 +00:00
|
|
|
#define TR_GNUC_HOT __attribute ( ( hot ) )
|
2008-10-23 02:37:21 +00:00
|
|
|
#else
|
|
|
|
#define TR_GNUC_NULL_TERMINATED
|
2009-11-01 01:35:06 +00:00
|
|
|
#define TR_GNUC_HOT
|
2008-10-23 02:37:21 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __GNUC__ > 2 || ( __GNUC__ == 2 && __GNUC_MINOR__ >= 96 )
|
|
|
|
#define TR_GNUC_MALLOC __attribute__ ( ( __malloc__ ) )
|
|
|
|
#else
|
|
|
|
#define TR_GNUC_MALLOC
|
|
|
|
#endif
|
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-08-22 02:12:58 +00:00
|
|
|
|
2010-06-25 01:13:35 +00:00
|
|
|
const char * tr_strip_positional_args( const char * fmt );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
#if !defined( _ )
|
2009-02-07 14:46:43 +00:00
|
|
|
#if defined( HAVE_LIBINTL_H ) && !defined( SYS_DARWIN )
|
2008-03-04 02:02:25 +00:00
|
|
|
#include <libintl.h>
|
2008-09-23 19:11:04 +00:00
|
|
|
#define _( a ) gettext ( a )
|
|
|
|
#else
|
|
|
|
#define _( a ) ( a )
|
|
|
|
#endif
|
2008-03-05 01:05:42 +00:00
|
|
|
#endif
|
2008-03-04 02:02:25 +00:00
|
|
|
|
2008-08-21 21:04:57 +00:00
|
|
|
/* #define DISABLE_GETTEXT */
|
2010-06-21 16:44:35 +00:00
|
|
|
#ifndef DISABLE_GETTEXT
|
2011-01-27 03:53:02 +00:00
|
|
|
#if defined(WIN32) || defined(TR_LIGHTWEIGHT)
|
2010-06-21 16:44:35 +00:00
|
|
|
#define DISABLE_GETTEXT
|
|
|
|
#endif
|
2008-10-22 17:16:12 +00:00
|
|
|
#endif
|
2008-08-21 21:04:57 +00:00
|
|
|
#ifdef DISABLE_GETTEXT
|
2008-09-23 19:11:04 +00:00
|
|
|
#undef _
|
|
|
|
#define _( a ) tr_strip_positional_args( a )
|
2008-08-21 21:04:57 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-29 18:11:56 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
2008-10-13 22:45:05 +00:00
|
|
|
|
2009-12-02 01:55:09 +00:00
|
|
|
#define TR_MAX_MSG_LOG 10000
|
2009-12-01 23:20:00 +00:00
|
|
|
|
2010-12-31 01:30:35 +00:00
|
|
|
extern tr_msg_level __tr_message_level;
|
|
|
|
|
|
|
|
static inline tr_msg_level tr_getMessageLevel( void )
|
|
|
|
{
|
|
|
|
return __tr_message_level;
|
|
|
|
}
|
2009-11-27 15:25:14 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static inline bool tr_msgLoggingIsActive( tr_msg_level level )
|
2009-11-27 15:25:14 +00:00
|
|
|
{
|
2010-12-31 01:30:35 +00:00
|
|
|
return tr_getMessageLevel() >= level;
|
2009-11-27 15:25:14 +00:00
|
|
|
}
|
2008-05-15 15:44:51 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
void tr_msg( const char * file, int line,
|
2010-06-30 16:40:19 +00:00
|
|
|
tr_msg_level level,
|
2010-01-01 22:13:27 +00:00
|
|
|
const char * torrent,
|
|
|
|
const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 );
|
2008-05-15 15:44:51 +00:00
|
|
|
|
2008-12-29 18:11:56 +00:00
|
|
|
#define tr_nerr( n, ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_ninf( n, ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_INF) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_ndbg( n, ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_DBG) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_torerr( tor, ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_torinf( tor, ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_tordbg( tor, ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_err( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_inf( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
#define tr_dbg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
|
|
|
|
tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-16 18:04:23 +00:00
|
|
|
void* tr_getLog( void );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
/** @brief return true if deep logging has been enabled by the user; false otherwise */
|
2011-03-22 15:19:54 +00:00
|
|
|
bool tr_deepLoggingIsActive( void );
|
2008-10-26 15:39:04 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void tr_deepLog( const char * file,
|
|
|
|
int line,
|
|
|
|
const char * name,
|
|
|
|
const char * fmt,
|
2009-11-01 01:35:06 +00:00
|
|
|
... ) TR_GNUC_PRINTF( 4, 5 ) TR_GNUC_NONNULL(1,4);
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
/** @brief set the buffer with the current time formatted for deep logging. */
|
|
|
|
char* tr_getLogTimeStr( char * buf, int buflen ) TR_GNUC_NONNULL(1);
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2008-12-29 18:11:56 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Rich Salz's classic implementation of shell-style pattern matching for ?, \, [], and * characters.
|
|
|
|
* @return 1 if the pattern matches, 0 if it doesn't, or -1 if an error occured
|
|
|
|
*/
|
2011-03-22 15:19:54 +00:00
|
|
|
bool tr_wildmat( const char * text, const char * pattern ) TR_GNUC_NONNULL(1,2);
|
2008-12-29 18:11:56 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief Portability wrapper for basename() that uses the system implementation if available */
|
|
|
|
char* tr_basename( const char * path ) TR_GNUC_MALLOC;
|
2008-10-14 03:39:16 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief Portability wrapper for dirname() that uses the system implementation if available */
|
|
|
|
char* tr_dirname( const char * path ) TR_GNUC_MALLOC;
|
2008-10-13 22:26:02 +00:00
|
|
|
|
2008-01-18 01:40:41 +00:00
|
|
|
/**
|
2009-05-29 19:17:12 +00:00
|
|
|
* @brief Portability wrapper for mkdir()
|
|
|
|
*
|
2010-01-01 22:13:27 +00:00
|
|
|
* A portability wrapper around mkdir().
|
2008-01-18 01:40:41 +00:00
|
|
|
* On WIN32, the `permissions' argument is unused.
|
|
|
|
*
|
|
|
|
* @return zero on success, or -1 if an error occurred
|
|
|
|
* (in which case errno is set appropriately).
|
|
|
|
*/
|
2010-01-01 22:13:27 +00:00
|
|
|
int tr_mkdir( const char * path, int permissions ) TR_GNUC_NONNULL(1);
|
2008-01-18 01:40:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Like mkdir, but makes parent directories as needed.
|
|
|
|
*
|
|
|
|
* @return zero on success, or -1 if an error occurred
|
|
|
|
* (in which case errno is set appropriately).
|
|
|
|
*/
|
2009-11-01 01:35:06 +00:00
|
|
|
int tr_mkdirp( const char * path, int permissions ) TR_GNUC_NONNULL(1);
|
2007-08-01 00:40:49 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
/**
|
2009-05-29 19:17:12 +00:00
|
|
|
* @brief Loads a file and returns its contents.
|
2008-10-03 04:49:06 +00:00
|
|
|
* On failure, NULL is returned and errno is set.
|
|
|
|
*/
|
2010-01-01 22:13:27 +00:00
|
|
|
uint8_t* tr_loadFile( const char * filename, size_t * size ) TR_GNUC_MALLOC
|
|
|
|
TR_GNUC_NONNULL(1);
|
2007-10-15 20:15:34 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief build a filename from a series of elements using the
|
|
|
|
platform's correct directory separator. */
|
|
|
|
char* tr_buildPath( const char * first_element, ... ) TR_GNUC_NULL_TERMINATED
|
|
|
|
TR_GNUC_MALLOC;
|
2007-06-18 19:39:52 +00:00
|
|
|
|
2009-06-15 03:24:40 +00:00
|
|
|
struct event;
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Convenience wrapper around timer_add() to have a timer wake up in a number of seconds and microseconds
|
|
|
|
* @param timer
|
|
|
|
* @param seconds
|
|
|
|
* @param microseconds
|
|
|
|
*/
|
|
|
|
void tr_timerAdd( struct event * timer, int seconds, int microseconds ) TR_GNUC_NONNULL(1);
|
2009-06-15 03:24:40 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Convenience wrapper around timer_add() to have a timer wake up in a number of milliseconds
|
|
|
|
* @param timer
|
|
|
|
* @param milliseconds
|
|
|
|
*/
|
|
|
|
void tr_timerAddMsec( struct event * timer, int milliseconds ) TR_GNUC_NONNULL(1);
|
2009-12-10 19:04:10 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief return the current date in milliseconds */
|
2010-07-11 20:49:19 +00:00
|
|
|
uint64_t tr_time_msec( void );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief sleep the specified number of milliseconds */
|
2010-07-01 03:59:06 +00:00
|
|
|
void tr_wait_msec( long int delay_milliseconds );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
|
|
|
* @brief make a copy of 'str' whose non-utf8 content has been corrected or stripped
|
2010-01-01 22:13:27 +00:00
|
|
|
* @return a newly-allocated string that must be freed with tr_free()
|
2009-05-29 19:17:12 +00:00
|
|
|
* @param str the string to make a clean copy of
|
2010-12-27 19:18:17 +00:00
|
|
|
* @param len the length of the string to copy. If -1, the entire string is used.
|
2009-05-29 19:17:12 +00:00
|
|
|
*/
|
2010-07-07 16:48:23 +00:00
|
|
|
char* tr_utf8clean( const char * str, int len ) TR_GNUC_MALLOC;
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
|
|
|
2007-07-19 11:54:37 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* Sometimes the system defines MAX/MIN, sometimes not.
|
|
|
|
In the latter case, define those here since we will use them */
|
2007-07-19 11:54:37 +00:00
|
|
|
#ifndef MAX
|
2008-09-23 19:11:04 +00:00
|
|
|
#define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
|
2007-07-19 11:54:37 +00:00
|
|
|
#endif
|
|
|
|
#ifndef MIN
|
2008-09-23 19:11:04 +00:00
|
|
|
#define MIN( a, b ) ( ( a ) > ( b ) ? ( b ) : ( a ) )
|
2007-07-19 11:54:37 +00:00
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Portability wrapper around malloc() in which `0' is a safe argument */
|
2010-10-11 15:41:27 +00:00
|
|
|
void* tr_malloc( size_t size );
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/** @brief Portability wrapper around calloc() in which `0' is a safe argument */
|
2010-10-11 15:41:27 +00:00
|
|
|
void* tr_malloc0( size_t size );
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/** @brief Portability wrapper around free() in which `NULL' is a safe argument */
|
2010-10-11 15:41:27 +00:00
|
|
|
void tr_free( void * p );
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief make a newly-allocated copy of a chunk of memory
|
|
|
|
* @param src the memory to copy
|
|
|
|
* @param byteCount the number of bytes to copy
|
|
|
|
* @return a newly-allocated copy of `src' that can be freed with tr_free()
|
|
|
|
*/
|
2010-10-11 15:41:27 +00:00
|
|
|
void* tr_memdup( const void * src, size_t byteCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-02 20:12:23 +00:00
|
|
|
#define tr_new( struct_type, n_structs ) \
|
2011-02-12 17:27:47 +00:00
|
|
|
( (struct_type *) tr_malloc ( sizeof ( struct_type ) * ( ( size_t) ( n_structs ) ) ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-02 20:12:23 +00:00
|
|
|
#define tr_new0( struct_type, n_structs ) \
|
2011-02-12 17:27:47 +00:00
|
|
|
( (struct_type *) tr_malloc0 ( sizeof ( struct_type ) * ( ( size_t) ( n_structs ) ) ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-02 20:12:23 +00:00
|
|
|
#define tr_renew( struct_type, mem, n_structs ) \
|
2011-02-12 17:27:47 +00:00
|
|
|
( (struct_type *) realloc ( ( mem ), sizeof ( struct_type ) * ( ( size_t) ( n_structs ) ) ) )
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2010-02-23 06:06:45 +00:00
|
|
|
void* tr_valloc( size_t bufLen );
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief make a newly-allocated copy of a substring
|
|
|
|
* @param in is a void* so that callers can pass in both signed & unsigned without a cast
|
2010-12-27 19:18:17 +00:00
|
|
|
* @param len length of the substring to copy. if a length less than zero is passed in, strlen( len ) is used
|
2010-01-01 22:13:27 +00:00
|
|
|
* @return a newly-allocated copy of `in' that can be freed with tr_free()
|
|
|
|
*/
|
2009-01-19 14:05:43 +00:00
|
|
|
char* tr_strndup( const void * in, int len ) TR_GNUC_MALLOC;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief make a newly-allocated copy of a string
|
|
|
|
* @param in is a void* so that callers can pass in both signed & unsigned without a cast
|
|
|
|
* @return a newly-allocated copy of `in' that can be freed with tr_free()
|
|
|
|
*/
|
2010-10-11 15:41:27 +00:00
|
|
|
char* tr_strdup( const void * in );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2011-01-03 03:01:12 +00:00
|
|
|
/**
|
|
|
|
* @brief like strcmp() but gracefully handles NULL strings
|
|
|
|
*/
|
|
|
|
int tr_strcmp0( const char * str1, const char * str2 );
|
|
|
|
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
|
|
|
|
struct evbuffer;
|
|
|
|
|
|
|
|
char* evbuffer_free_to_str( struct evbuffer * buf );
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief similar to bsearch() but returns the index of the lower bound */
|
2009-01-04 16:29:44 +00:00
|
|
|
int tr_lowerBound( const void * key,
|
|
|
|
const void * base,
|
|
|
|
size_t nmemb,
|
|
|
|
size_t size,
|
|
|
|
int (* compar)(const void* key, const void* arrayMember),
|
2011-03-22 15:19:54 +00:00
|
|
|
bool * exact_match ) TR_GNUC_HOT TR_GNUC_NONNULL(1,5,6);
|
2009-01-04 16:29:44 +00:00
|
|
|
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief sprintf() a string into a newly-allocated buffer large enough to hold it
|
|
|
|
* @return a newly-allocated string that can be freed with tr_free()
|
|
|
|
*/
|
|
|
|
char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 )
|
|
|
|
TR_GNUC_MALLOC;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Translate a block of bytes into base64
|
|
|
|
* @return a newly-allocated string that can be freed with tr_free()
|
|
|
|
*/
|
|
|
|
char* tr_base64_encode( const void * input,
|
|
|
|
int inlen,
|
|
|
|
int * outlen ) TR_GNUC_MALLOC;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Translate a block of bytes from base64 into raw form
|
|
|
|
* @return a newly-allocated string that can be freed with tr_free()
|
|
|
|
*/
|
|
|
|
char* tr_base64_decode( const void * input,
|
|
|
|
int inlen,
|
|
|
|
int * outlen ) TR_GNUC_MALLOC;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief Portability wrapper for strlcpy() that uses the system implementation if available */
|
|
|
|
size_t tr_strlcpy( char * dst, const void * src, size_t siz );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief Portability wrapper for snprintf() that uses the system implementation if available */
|
|
|
|
int tr_snprintf( char * buf, size_t buflen,
|
2009-11-01 01:35:06 +00:00
|
|
|
const char * fmt, ... ) TR_GNUC_PRINTF( 3, 4 ) TR_GNUC_NONNULL(1,3);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Convenience wrapper around strerorr() guaranteed to not return NULL
|
|
|
|
@param errno */
|
2008-10-23 02:37:21 +00:00
|
|
|
const char* tr_strerror( int );
|
2008-02-19 04:16:04 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief strips leading and trailing whitspace from a string
|
|
|
|
@return the stripped string */
|
|
|
|
char* tr_strstrip( char * str );
|
2008-09-05 19:11:30 +00:00
|
|
|
|
2010-06-07 14:25:31 +00:00
|
|
|
/** @brief Returns true if the string ends with the specified case-insensitive suffix */
|
2011-03-22 15:19:54 +00:00
|
|
|
bool tr_str_has_suffix( const char *str, const char *suffix );
|
2010-06-07 14:25:31 +00:00
|
|
|
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief Portability wrapper for memmem() that uses the system implementation if available */
|
2009-05-19 04:48:33 +00:00
|
|
|
const char* tr_memmem( const char * haystack, size_t haystack_len,
|
|
|
|
const char * needle, size_t needle_len );
|
|
|
|
|
2010-12-08 14:57:34 +00:00
|
|
|
/** @brief Portability wrapper for strsep() that uses the system implementation if available */
|
|
|
|
char* tr_strsep( char ** str, const char * delim );
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-09-14 06:23:48 +00:00
|
|
|
int compareInt( const void * va, const void * vb );
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
void tr_sha1_to_hex( char * out, const uint8_t * sha1 ) TR_GNUC_NONNULL(1,2);
|
2008-02-25 20:21:22 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
void tr_hex_to_sha1( uint8_t * out, const char * hex ) TR_GNUC_NONNULL(1,2);
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2010-02-01 23:52:46 +00:00
|
|
|
/** @brief convenience function to determine if an address is an IP address (IPv4 or IPv6) */
|
2011-03-22 15:19:54 +00:00
|
|
|
bool tr_addressIsIP( const char * address );
|
2010-02-01 23:52:46 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
/** @brief return true if the url is a http or https url that Transmission understands */
|
|
|
|
bool tr_urlIsValidTracker( const char * url ) TR_GNUC_NONNULL(1);
|
2010-02-20 15:57:05 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
/** @brief return true if the url is a [ http, https, ftp, ftps ] url that Transmission understands */
|
|
|
|
bool tr_urlIsValid( const char * url, int url_len ) TR_GNUC_NONNULL(1);
|
2010-02-20 15:57:05 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief parse a URL into its component parts
|
|
|
|
@return zero on success or an error number if an error occurred */
|
2010-02-20 15:57:05 +00:00
|
|
|
int tr_urlParse( const char * url,
|
|
|
|
int url_len,
|
|
|
|
char ** setme_scheme,
|
|
|
|
char ** setme_host,
|
|
|
|
int * setme_port,
|
|
|
|
char ** setme_path ) TR_GNUC_NONNULL(1);
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/** @brief return TR_RATIO_NA, TR_RATIO_INF, or a number in [0..1]
|
|
|
|
@return TR_RATIO_NA, TR_RATIO_INF, or a number in [0..1] */
|
2010-07-01 05:14:34 +00:00
|
|
|
double tr_getRatio( uint64_t numerator, uint64_t denominator );
|
2008-12-29 21:17:48 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Given a string like "1-4" or "1-4,6,9,14-51", this returns a
|
|
|
|
* newly-allocated array of all the integers in the set.
|
|
|
|
* @return a newly-allocated array of integers that must be freed with tr_free(),
|
|
|
|
* or NULL if a fragment of the string can't be parsed.
|
|
|
|
*
|
|
|
|
* For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4.
|
|
|
|
*/
|
|
|
|
int* tr_parseNumberRange( const char * str,
|
|
|
|
int str_len,
|
|
|
|
int * setmeCount ) TR_GNUC_MALLOC TR_GNUC_NONNULL(1);
|
2008-12-29 21:17:48 +00:00
|
|
|
|
2008-01-04 19:13:30 +00:00
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
/**
|
|
|
|
* @brief truncate a double value at a given number of decimal places.
|
|
|
|
*
|
|
|
|
* this can be used to prevent a printf() call from rounding up:
|
|
|
|
* call with the decimal_places argument equal to the number of
|
|
|
|
* decimal places in the printf()'s precision:
|
|
|
|
*
|
|
|
|
* - printf("%.2f%%", 99.999 ) ==> "100.00%"
|
|
|
|
*
|
|
|
|
* - printf("%.2f%%", tr_truncd(99.999, 2)) ==> "99.99%"
|
|
|
|
* ^ ^
|
|
|
|
* | These should match |
|
|
|
|
* +------------------------+
|
|
|
|
*/
|
2009-07-14 20:09:46 +00:00
|
|
|
double tr_truncd( double x, int decimal_places );
|
|
|
|
|
2010-06-22 22:30:58 +00:00
|
|
|
/* return a percent formatted string of either x.xx, xx.x or xxx */
|
|
|
|
char* tr_strpercent( char * buf, double x, size_t buflen );
|
|
|
|
|
2009-03-04 16:16:02 +00:00
|
|
|
/**
|
|
|
|
* @param buf the buffer to write the string to
|
|
|
|
* @param buflef buf's size
|
|
|
|
* @param ratio the ratio to convert to a string
|
|
|
|
* @param the string represntation of "infinity"
|
|
|
|
*/
|
2009-11-01 01:35:06 +00:00
|
|
|
char* tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity ) TR_GNUC_NONNULL(1,4);
|
2009-03-04 16:16:02 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief Portability wrapper for localtime_r() that uses the system implementation if available */
|
2009-03-25 18:42:39 +00:00
|
|
|
struct tm * tr_localtime_r( const time_t *_clock, struct tm *_result );
|
|
|
|
|
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
/**
|
|
|
|
* @brief move a file
|
|
|
|
* @return 0 on success; otherwise, return -1 and set errno
|
|
|
|
*/
|
2009-11-24 02:16:31 +00:00
|
|
|
int tr_moveFile( const char * oldpath, const char * newpath,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool * renamed ) TR_GNUC_NONNULL(1,2);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2010-12-22 07:04:11 +00:00
|
|
|
/** @brief Test to see if the two filenames point to the same file. */
|
2011-03-22 15:19:54 +00:00
|
|
|
bool tr_is_same_file( const char * filename1, const char * filename2 );
|
2010-12-22 07:04:11 +00:00
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
/** @brief convenience function to remove an item from an array */
|
2010-10-11 15:41:27 +00:00
|
|
|
void tr_removeElementFromArray( void * array,
|
|
|
|
unsigned int index_to_remove,
|
|
|
|
size_t sizeof_element,
|
|
|
|
size_t nmemb );
|
2009-03-04 16:16:02 +00:00
|
|
|
|
2009-11-26 18:47:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Private libtransmission variable that's visible only for inlining in tr_time() */
|
2010-12-31 01:30:35 +00:00
|
|
|
extern time_t __tr_current_time;
|
2009-11-26 18:47:08 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
2010-01-05 23:47:50 +00:00
|
|
|
* @brief very inexpensive form of time(NULL)
|
2010-01-01 22:13:27 +00:00
|
|
|
* @return the current epoch time in seconds
|
|
|
|
*
|
|
|
|
* This function returns a second counter that is updated once per second.
|
|
|
|
* If something blocks the libtransmission thread for more than a second,
|
|
|
|
* that counter may be thrown off, so this function is not guaranteed
|
2010-12-27 19:18:17 +00:00
|
|
|
* to always be accurate. However, it is *much* faster when 100% accuracy
|
2010-01-01 22:13:27 +00:00
|
|
|
* isn't needed
|
|
|
|
*/
|
2010-12-31 01:30:35 +00:00
|
|
|
static inline time_t tr_time( void ) { return __tr_current_time; }
|
2009-11-26 18:47:08 +00:00
|
|
|
|
2010-03-12 19:24:45 +00:00
|
|
|
/** @brief Private libtransmission function to update tr_time()'s counter */
|
2010-12-31 01:30:35 +00:00
|
|
|
static inline void tr_timeUpdate( time_t now ) { __tr_current_time = now; }
|
2009-11-26 18:47:08 +00:00
|
|
|
|
2010-06-21 16:44:35 +00:00
|
|
|
/** @brief Portability wrapper for realpath() that uses the system implementation if available */
|
|
|
|
char* tr_realpath( const char *path, char * resolved_path );
|
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
/** @brief Portability wrapper for htonll() that uses the system implementation if available */
|
|
|
|
uint64_t tr_htonll( uint64_t );
|
|
|
|
|
|
|
|
/** @brief Portability wrapper for htonll() that uses the system implementation if available */
|
|
|
|
uint64_t tr_ntohll( uint64_t );
|
|
|
|
|
2009-11-26 18:47:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-07-06 20:52:54 +00:00
|
|
|
/* example: tr_formatter_size_init( 1024, _("KiB"), _("MiB"), _("GiB"), _("TiB") ); */
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2010-07-06 20:52:54 +00:00
|
|
|
void tr_formatter_size_init( unsigned int kilo, const char * kb, const char * mb,
|
|
|
|
const char * gb, const char * tb );
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2010-07-06 20:52:54 +00:00
|
|
|
void tr_formatter_speed_init( unsigned int kilo, const char * kb, const char * mb,
|
|
|
|
const char * gb, const char * tb );
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2010-07-06 20:52:54 +00:00
|
|
|
void tr_formatter_mem_init( unsigned int kilo, const char * kb, const char * mb,
|
|
|
|
const char * gb, const char * tb );
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
extern unsigned int tr_speed_K;
|
|
|
|
extern unsigned int tr_mem_K;
|
2010-07-09 20:53:38 +00:00
|
|
|
extern unsigned int tr_size_K;
|
|
|
|
|
|
|
|
/* format a speed from KBps into a user-readable string. */
|
|
|
|
char* tr_formatter_speed_KBps( char * buf, double KBps, size_t buflen );
|
|
|
|
|
|
|
|
/* format a memory size from bytes into a user-readable string. */
|
2011-01-19 18:14:01 +00:00
|
|
|
char* tr_formatter_mem_B( char * buf, int64_t bytes, size_t buflen );
|
2010-07-09 20:53:38 +00:00
|
|
|
|
|
|
|
/* format a memory size from MB into a user-readable string. */
|
2010-07-04 06:07:21 +00:00
|
|
|
static inline char* tr_formatter_mem_MB( char * buf, double MBps, size_t buflen ) { return tr_formatter_mem_B( buf, MBps * tr_mem_K * tr_mem_K, buflen ); }
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2010-07-09 20:53:38 +00:00
|
|
|
/* format a file size from bytes into a user-readable string. */
|
2011-01-19 18:14:01 +00:00
|
|
|
char* tr_formatter_size_B( char * buf, int64_t bytes, size_t buflen );
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2010-09-18 23:06:03 +00:00
|
|
|
void tr_formatter_get_units( struct tr_benc * dict );
|
2010-06-22 13:34:46 +00:00
|
|
|
|
2010-06-22 04:34:16 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-09-19 17:17:34 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @} */
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|