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