source: trunk/libtransmission/utils.c @ 798

Last change on this file since 798 was 798, checked in by joshe, 16 years ago

Allow frontend to change message output level.
Rename tr_setErrorFunction() to tr_setMessageFunction()

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