source: trunk/libtransmission/utils.c @ 626

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

Allow frontend to supply a custom function for displaying libtransmission errors.

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