wmclockmon: update change-log
[dockapps.git] / ascd / libworkman / wm_helpers.c
blobab1e2c24e0f56cb5106d674c4fb35a9f7f45fdf9
1 /*
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 $";
31 #include <stdio.h>
32 #include <string.h>
33 #include <malloc.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <sys/time.h>
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 )
54 char *s = NULL;
56 wm_strmcat(&s, WM_LIBVER_NAME);
57 return s;
58 } /* wm_libver_name() */
60 char *wm_libver_number( void )
62 char *s = NULL;
64 s = malloc(10);
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());
67 return s;
68 } /* wm_libver_number() */
70 char *wm_libver_date( void )
72 char *s = NULL;
73 wm_strmcat(&s, __DATE__);
74 return s;
75 } /* wm_libver_date() */
77 char *wm_libver_string( void )
79 char *s = NULL;
81 wm_strmcat( &s, wm_libver_name() );
82 wm_strmcat( &s, " " );
83 wm_strmcat( &s, wm_libver_number() );
84 return s;
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 )
97 if (*x != NULL)
99 free(*x);
100 *x = NULL;
102 } /* freeup() */
104 /* Copy into a malloced string. */
105 void
106 wm_strmcpy( char **t, char *s )
108 if (*t != NULL)
109 free(*t);
111 *t = malloc(strlen(s) + 1);
112 if (*t == NULL)
114 perror("wm_strmcpy");
115 exit(1);
118 (void) strcpy(*t, s);
119 } /* wm_strmcpy() */
121 /* Add to a malloced string. */
122 void
123 wm_strmcat( char **t, char *s)
125 int len = strlen(s) + 1;
127 if (*s == '\0')
128 return;
130 if (*t != NULL)
132 len += strlen(*t);
133 *t = realloc(*t, len);
134 if (*t == NULL)
136 perror("strmcat");
137 exit(1);
139 strcat(*t, s);
141 else
142 wm_strmcpy(t, s);
143 } /* wm_strmcat() */
145 /* Duplicate a string. Some systems have this in libc, but not all. */
146 char *
147 wm_strdup( char *s )
149 char *new;
151 new = malloc(strlen(s) + 1);
152 if (new)
153 strcpy(new, s);
154 return (new);
155 } /* wm_strdup() */
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;
175 * wm_lib_message().
177 * any message that falls into allowed classes and has at least
178 * verbosity level wm_lib_verbosity & 0xf will be printed.
180 * Usage:
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, ... )
190 va_list ap;
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
199 level &= 0xf;
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) )
209 va_start(ap, fmt);
210 vfprintf(stderr, fmt, ap);
211 va_end(ap);
213 } /* wm_lib_message() */
216 * Simulate usleep() using select().
219 wm_susleep( int usec )
221 struct timeval tv;
223 timerclear(&tv);
224 tv.tv_sec = usec / 1000000;
225 tv.tv_usec = usec % 1000000;
226 return (select(0, NULL, NULL, NULL, &tv));
227 } /* wm_susleep() */