source: trunk/libtransmission/utils.c @ 803

Last change on this file since 803 was 803, checked in by joshe, 15 years ago

Make message level available to message handler callback.

  • Property svn:keywords set to Date Rev Author Id
File size: 4.0 KB
Line 
1/******************************************************************************
2 * $Id: utils.c 803 2006-08-20 22:45:04Z joshe $
3 *
4 * Copyright (c) 2005-2006 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#include "transmission.h"
26
27static void (*messageFunc)( int, const char * );
28
29static int verboseLevel = 0;
30
31void tr_setMessageFunction( void (*func)( int, const char * ) )
32{
33    messageFunc = func;
34}
35
36void tr_setMessageLevel( int level )
37{
38    verboseLevel = MAX( 0, level );
39}
40
41int tr_getMessageLevel( void )
42{
43    return verboseLevel;
44}
45
46void tr_msg( int level, char * msg, ... )
47{
48    char         string[256];
49    va_list      args;
50
51    if( !verboseLevel )
52    {
53        char * env;
54        env          = getenv( "TR_DEBUG" );
55        verboseLevel = ( env ? atoi( env ) : 0 ) + 1;
56        verboseLevel = MAX( 1, verboseLevel );
57    }
58
59    if( verboseLevel < level )
60    {
61        return;
62    }
63
64    va_start( args, msg );
65    vsnprintf( string, sizeof( string ), msg, args );
66    va_end( args );
67
68    if( NULL == messageFunc )
69    {
70        fprintf( stderr, "%s\n", string );
71    }
72    else
73    {
74        messageFunc( level, string );
75    }
76}
77
78int tr_rand( int sup )
79{
80    static int init = 0;
81    if( !init )
82    {
83        srand( tr_date() );
84        init = 1;
85    }
86    return rand() % sup;
87}
88
89void * tr_memmem( const void *vbig, size_t big_len,
90                  const void *vlittle, size_t little_len )
91{
92    const char *big = vbig;
93    const char *little = vlittle;
94    size_t ii, jj;
95
96    if( 0 == big_len || 0 == little_len )
97    {
98        return NULL;
99    }
100
101    for( ii = 0; ii + little_len <= big_len; ii++ )
102    {
103        for( jj = 0; jj < little_len; jj++ )
104        {
105            if( big[ii + jj] != little[jj] )
106            {
107                break;
108            }
109        }
110        if( jj == little_len )
111        {
112            return (char*)big + ii;
113        }
114    }
115
116    return NULL;
117}
118
119int tr_mkdir( char * path )
120{
121    char      * p, * pp;
122    struct stat sb;
123    int done;
124
125    p = path;
126    while( '/' == *p )
127      p++;
128    pp = p;
129    done = 0;
130    while( ( p = strchr( pp, '/' ) ) || ( p = strchr( pp, '\0' ) ) )
131    {
132        if( '\0' == *p)
133        {
134            done = 1;
135        }
136        else
137        {
138            *p = '\0';
139        }
140        if( stat( path, &sb ) )
141        {
142            /* Folder doesn't exist yet */
143            if( mkdir( path, 0777 ) )
144            {
145                tr_err( "Could not create directory %s (%s)", path,
146                        strerror( errno ) );
147                *p = '/';
148                return 1;
149            }
150        }
151        else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
152        {
153            /* Node exists but isn't a folder */
154            tr_err( "Remove %s, it's in the way.", path );
155            *p = '/';
156            return 1;
157        }
158        if( done )
159        {
160            break;
161        }
162        *p = '/';
163        p++;
164        pp = p;
165    }
166
167    return 0;
168}
Note: See TracBrowser for help on using the repository browser.