~ubuntu-branches/ubuntu/quantal/libkcompactdisc/quantal-updates

« back to all changes in this revision

Viewing changes to wmlib/wm_helpers.c

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-05-26 15:25:35 UTC
  • Revision ID: package-import@ubuntu.com-20120526152535-60v997qsp1solymv
Tags: upstream-4.8.80a
Import upstream version 4.8.80a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of WorkMan, the civilized CD player library
 
3
 * Copyright (C) 1991-1997 by Steven Grimm <koreth@midwinter.com>
 
4
 * Copyright (C) by Dirk Försterling <milliByte@DeathsDoor.com>
 
5
 * Copyright (C) 2004-2006 Alexander Kern <alex.kern@gmx.de>
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the Free
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 *
 
21
 *
 
22
 * Some helpful functions...
 
23
 *
 
24
 */
 
25
 
 
26
#include <stdio.h>
 
27
#include <string.h>
 
28
#include <stdlib.h>
 
29
#include <errno.h>
 
30
#include <stdarg.h>
 
31
#include <sys/time.h>
 
32
#include "include/workman_defs.h"
 
33
#include "include/wm_config.h"
 
34
#include "include/wm_helpers.h"
 
35
#include "include/wm_struct.h"
 
36
 
 
37
#define WM_MSG_CLASS WM_MSG_CLASS_MISC
 
38
 
 
39
int wm_lib_verbosity = WM_MSG_LEVEL_ERROR | WM_MSG_CLASS_ALL;
 
40
 
 
41
/*
 
42
 * Some seleced functions of version reporting follow...
 
43
 */
 
44
 
 
45
int wm_libver_major( void ){return WM_LIBVER_MAJOR;}
 
46
int wm_libver_minor( void ){return WM_LIBVER_MINOR;}
 
47
int wm_libver_pl( void ){return WM_LIBVER_PL;}
 
48
 
 
49
char *wm_libver_name( void )
 
50
{
 
51
        char *s = NULL;
 
52
 
 
53
        wm_strmcat(&s, WM_LIBVER_NAME);
 
54
        return s;
 
55
} /* wm_libver_name() */
 
56
 
 
57
char *wm_libver_number( void )
 
58
{
 
59
        char *s = NULL;
 
60
 
 
61
        s = malloc(10);
 
62
        /* this is not used very often, so don't care about speed...*/
 
63
        sprintf(s, "%d.%d.%d", wm_libver_major(), wm_libver_minor(), wm_libver_pl());
 
64
        return s;
 
65
} /* wm_libver_number() */
 
66
 
 
67
char *wm_libver_date( void )
 
68
{
 
69
        char *s = NULL;
 
70
        wm_strmcat(&s, __DATE__);
 
71
        return s;
 
72
} /* wm_libver_date() */
 
73
 
 
74
char *wm_libver_string( void )
 
75
{
 
76
        char *s = NULL;
 
77
 
 
78
        wm_strmcat( &s, wm_libver_name() );
 
79
        wm_strmcat( &s, " " );
 
80
        wm_strmcat( &s, wm_libver_number() );
 
81
        return s;
 
82
} /* wm_libver_string() */
 
83
 
 
84
 
 
85
/*
 
86
 *
 
87
 * Now for some memory management...
 
88
 *
 
89
 */
 
90
 
 
91
/* Free some memory and set a pointer to null. */
 
92
void freeup( char **x )
 
93
{
 
94
        if (*x != NULL)
 
95
        {
 
96
                free(*x);
 
97
                *x = NULL;
 
98
        }
 
99
} /* freeup() */
 
100
 
 
101
/* Copy into a malloced string. */
 
102
void
 
103
wm_strmcpy( char **t, const char *s )
 
104
{
 
105
        wm_lib_message(WM_MSG_CLASS_MISC | WM_MSG_LEVEL_DEBUG, "wm_strmcpy(%s, '%s')\n", *t, s);
 
106
        if (*t != NULL)
 
107
          {
 
108
            wm_lib_message(WM_MSG_CLASS_MISC | WM_MSG_LEVEL_DEBUG, "wm_strmcpy freeing pointer %p\n", *t);
 
109
            free(*t);
 
110
          }
 
111
 
 
112
        *t = malloc(strlen(s) + 1);
 
113
        if (*t == NULL)
 
114
        {
 
115
                perror("wm_strmcpy");
 
116
                exit(1);
 
117
        }
 
118
 
 
119
        wm_lib_message(WM_MSG_CLASS_MISC | WM_MSG_LEVEL_DEBUG, "wm_strmcpy finally copying (%p, '%s')\n", *t, s);
 
120
        strncpy(*t, s, strlen(s));
 
121
} /* wm_strmcpy() */
 
122
 
 
123
/* Add to a malloced string. */
 
124
void
 
125
wm_strmcat( char **t, const char *s)
 
126
{
 
127
        int     len = strlen(s) + 1;
 
128
 
 
129
        wm_lib_message(WM_MSG_CLASS_MISC | WM_MSG_LEVEL_DEBUG, "wm_strmcat(%s, %s)\n", *t, s);
 
130
 
 
131
        if (*s == '\0')
 
132
                return;
 
133
 
 
134
        if (*t != NULL)
 
135
        {
 
136
                len += strlen(*t);
 
137
                *t = realloc(*t, len);
 
138
                if (*t == NULL)
 
139
                {
 
140
                        perror("wm_strmcat");
 
141
                        exit(1);
 
142
                }
 
143
                strcat(*t, s);
 
144
        }
 
145
        else
 
146
                wm_strmcpy(t, s);
 
147
} /* wm_strmcat() */
 
148
 
 
149
/* Duplicate a string.  Some systems have this in libc, but not all. */
 
150
char *
 
151
wm_strdup( char *s )
 
152
{
 
153
        char    *new;
 
154
 
 
155
        new = malloc(strlen(s) + 1);
 
156
        if (new)
 
157
                strcpy(new, s);
 
158
        return (new);
 
159
} /* wm_strdup() */
 
160
 
 
161
 
 
162
/*
 
163
 * set and get verbosity level.
 
164
 */
 
165
void wm_lib_set_verbosity( int level )
 
166
{
 
167
        int l = level & WM_MSG_LEVEL_ALL;
 
168
        int c = level & WM_MSG_CLASS_ALL;
 
169
        if( WM_MSG_LEVEL_NONE <= l && l <= WM_MSG_LEVEL_DEBUG )
 
170
        {
 
171
                wm_lib_verbosity = l | c;
 
172
                wm_lib_message(WM_MSG_CLASS_MISC | WM_MSG_LEVEL_DEBUG, "Verbosity set to 0x%x|0x%x\n", l, c);
 
173
        }
 
174
} /* wm_lib_set_verbosity */
 
175
 
 
176
int wm_lib_get_verbosity( void )
 
177
{
 
178
        return wm_lib_verbosity;
 
179
}
 
180
 
 
181
/*
 
182
 * wm_lib_message().
 
183
 *
 
184
 * any message that falls into allowed classes and has at least
 
185
 * verbosity level wm_lib_verbosity & 0xf will be printed.
 
186
 *
 
187
 * Usage:
 
188
 *
 
189
 * wm_lib_message( WM_MSG_LEVEL | WM_MSG_CLASS, "format", contents);
 
190
 *
 
191
 * To simplify the usage, you may simply use WM_MSG_CLASS. It should be
 
192
 * defined in each module to reflect the correct message class.
 
193
 *
 
194
 */
 
195
void wm_lib_message( unsigned int level, const char *fmt, ... )
 
196
{
 
197
        va_list ap;
 
198
        
 
199
        unsigned int l, c, vl, vc;
 
200
        /* verbosity level */
 
201
        vl = wm_lib_verbosity & WM_MSG_LEVEL_ALL;
 
202
        /* allowed classes */
 
203
        vc = wm_lib_verbosity & WM_MSG_CLASS_ALL;
 
204
 
 
205
        l = level & WM_MSG_LEVEL_ALL;
 
206
        c = level & WM_MSG_CLASS_ALL;
 
207
 
 
208
        /*
 
209
     * print it only if level and class are allowed.
 
210
         */
 
211
        if( (l <= vl) && (vc & c) )
 
212
        {
 
213
                fprintf(stderr, "libWorkMan: ");
 
214
                va_start(ap, fmt);
 
215
                vfprintf(stderr, fmt, ap);
 
216
                va_end(ap);
 
217
        }
 
218
} /* wm_lib_message() */
 
219
 
 
220
/*
 
221
 * Simulate usleep() using select().
 
222
 */
 
223
int
 
224
wm_susleep( int usec )
 
225
{
 
226
        struct timeval  tv;
 
227
 
 
228
        timerclear(&tv);
 
229
        tv.tv_sec = usec / 1000000;
 
230
        tv.tv_usec = usec % 1000000;
 
231
        return (select(0, NULL, NULL, NULL, &tv));
 
232
} /* wm_susleep() */
 
233
 
 
234