2 * $Id: wm_helpers.c,v 1.7 1999/03/07 08:36:41 dirk Exp $
4 * This file is part of WorkMan, the civilized CD player library
5 * (c) 1991-1997 by Steven Grimm (original author)
6 * (c) by Dirk Försterling (current 'author' = maintainer)
7 * The maintainer can be contacted by his e-mail address:
8 * milliByte@DeathsDoor.com
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the Free
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Some helpful functions...
29 static char wm_helpers_id
[] = "$Id: wm_helpers.c,v 1.7 1999/03/07 08:36:41 dirk Exp $";
37 #include "include/workman_defs.h"
38 #include "include/wm_config.h"
39 #include "include/wm_helpers.h"
40 #include "include/wm_struct.h"
42 int wm_lib_verbosity
= WM_MSG_LEVEL_NONE
;
45 * Some seleced functions of version reporting follow...
48 int wm_libver_major( void ){return WM_LIBVER_MAJOR
;}
49 int wm_libver_minor( void ){return WM_LIBVER_MINOR
;}
50 int wm_libver_pl( void ){return WM_LIBVER_PL
;}
52 char *wm_libver_name( void )
56 wm_strmcat(&s
, WM_LIBVER_NAME
);
58 } /* wm_libver_name() */
60 char *wm_libver_number( void )
65 /* this is not used very often, so don't care about speed...*/
66 sprintf(s
, "%d.%d.%d", wm_libver_major(), wm_libver_minor(), wm_libver_pl());
68 } /* wm_libver_number() */
70 char *wm_libver_date( void )
73 wm_strmcat(&s
, __DATE__
);
75 } /* wm_libver_date() */
77 char *wm_libver_string( void )
81 wm_strmcat( &s
, wm_libver_name() );
82 wm_strmcat( &s
, " " );
83 wm_strmcat( &s
, wm_libver_number() );
85 } /* wm_libver_string() */
90 * Now for some memory management...
94 /* Free some memory and set a pointer to null. */
95 void freeup( char **x
)
104 /* Copy into a malloced string. */
106 wm_strmcpy( char **t
, char *s
)
111 *t
= malloc(strlen(s
) + 1);
114 perror("wm_strmcpy");
118 (void) strcpy(*t
, s
);
121 /* Add to a malloced string. */
123 wm_strmcat( char **t
, char *s
)
125 int len
= strlen(s
) + 1;
133 *t
= realloc(*t
, len
);
145 /* Duplicate a string. Some systems have this in libc, but not all. */
151 new = malloc(strlen(s
) + 1);
159 * set and get verbosity level.
161 void wm_lib_set_verbosity( int level
)
163 if( WM_MSG_LEVEL_NONE
<= level
<= WM_MSG_LEVEL_DEBUG
)
165 wm_lib_verbosity
= level
;
167 } /* wm_lib_set_verbosity */
169 int wm_lib_get_verbosity( void )
171 return wm_lib_verbosity
;
177 * any message that falls into allowed classes and has at least
178 * verbosity level wm_lib_verbosity & 0xf will be printed.
182 * wm_lib_message( WM_MSG_LEVEL | WM_MSG_CLASS, "format", contents);
184 * To simplify the usage, you may simply use WM_MSG_CLASS. It should be
185 * defined in each module to reflect the correct message class.
188 void wm_lib_message( unsigned int level
, char *fmt
, ... )
191 /* verbosity level */
192 unsigned int vlevel
= wm_lib_verbosity
& 0xf;
193 /* allowed classes */
194 unsigned int vclass
= (level
& WM_MSG_CLASS_ALL
) & (wm_lib_verbosity
& WM_MSG_CLASS_ALL
);
197 * just give me the level
200 if(level
<= WM_MSG_LEVEL_NONE
)
202 fprintf(stderr
, "LibWorkMan warning: A LibWorkMan programmer specified an invalid message level.\n");
205 * print it only if level and class are allowed.
207 if( (level
<= vlevel
) && (vclass
!= 0) )
210 vfprintf(stderr
, fmt
, ap
);
213 } /* wm_lib_message() */
216 * Simulate usleep() using select().
219 wm_susleep( int usec
)
224 tv
.tv_sec
= usec
/ 1000000;
225 tv
.tv_usec
= usec
% 1000000;
226 return (select(0, NULL
, NULL
, NULL
, &tv
));