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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/* Copyright (c) 2002-2005 krzYszcz and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "m_pd.h"
#include "loud.h"
#ifdef MSW
#define vsnprintf _vsnprintf
#endif
/* The 'shared_' calls do not really belong here,
LATER find them a permanent home. */
int shared_matchignorecase(char *test, char *pattern)
{
char ct, cp;
for (ct = *test, cp = *pattern; ct && cp; ct = *++test, cp = *++pattern)
if (ct != cp
&& ((ct < 'A' || ct > 'z')
|| ((ct > 'Z' || ct + 32 != cp)
&& (ct < 'a' || ct - 32 != cp))))
return (0);
return (ct == cp);
}
struct _loudcontext
{
t_pd *lc_caller; /* an object reporting trouble */
char *lc_callername;
int lc_cnsize;
/* during object creation, use the following: */
t_symbol *lc_selector; /* creation message selector (class name) */
int lc_ac; /* creation message arguments */
t_atom *lc_av; /* void out of creation context */
int lc_andindent;
};
#define LOUD_ERROR_DEFAULT "error (miXed):"
char *loud_ordinal(int n)
{
static char buf[16]; /* assuming 10-digit INT_MAX */
sprintf(buf, "%dth", n);
if (n < 0) n = -n;
n %= 100;
if (n > 20) n %= 10;
if (n && n <= 3)
{
char *ptr = buf + strlen(buf) - 2;
switch (n)
{
case 1: strcpy(ptr, "st"); break;
case 2: strcpy(ptr, "nd"); break;
case 3: strcpy(ptr, "rd"); break;
}
}
return (buf);
}
void loud_error(t_pd *x, char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
if (x)
{
startpost("%s's ", class_getname(*x));
pd_error(x, buf);
}
else post("%s %s", LOUD_ERROR_DEFAULT, buf);
va_end(ap);
}
void loud_errand(t_pd *x, char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
post("%*s%s", (int)(x ? strlen(class_getname(*x)) + 10
: strlen(LOUD_ERROR_DEFAULT) + 1), "", buf);
va_end(ap);
}
void loud_syserror(t_pd *x, char *fmt, ...)
{
if (fmt)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
loud_error(x, "%s (%s)", buf, strerror(errno));
va_end(ap);
}
else loud_error(x, strerror(errno));
}
void loud_nomethod(t_pd *x, t_symbol *s)
{
loud_error(x, "doesn't understand \"%s\"", s->s_name);
}
void loud_messarg(t_pd *x, t_symbol *s)
{
loud_error(x, "bad arguments for message \"%s\"", s->s_name);
}
int loud_checkint(t_pd *x, t_float f, int *valuep, t_symbol *mess)
{
if ((*valuep = (int)f) == f)
return (1);
else
{
static t_symbol *floatsym = 0;
if (!floatsym)
floatsym = gensym("noninteger float");
if (mess == &s_float)
loud_nomethod(x, floatsym);
else if (mess)
loud_error(x, "\"%s\" argument invalid for message \"%s\"",
floatsym->s_name, mess->s_name);
return (0);
}
}
void loud_classarg(t_class *c)
{
loud_error(0, "missing or bad arguments in \"%s\"", class_getname(c));
}
void loud_warning(t_pd *x, char *who, char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
post("warning (%s): %s",
(x ? class_getname(*x) : (who ? who : "miXed")), buf);
va_end(ap);
}
void loud_notimplemented(t_pd *x, char *name)
{
if (name)
loud_warning(x, 0, "\"%s\" method not implemented (yet)", name);
else
loud_warning(x, 0, "not implemented (yet)");
}
int loud_floatarg(t_class *c, int which, int ac, t_atom *av,
t_float *vp, t_float minval, t_float maxval,
int underaction, int overaction, char *what)
{
int result = LOUD_ARGOK;
if (which < ac)
{
av += which;
if (av->a_type == A_FLOAT)
{
t_float f = av->a_w.w_float;
if (f < minval)
{
*vp = (underaction & LOUD_CLIP ? minval : f);
if (underaction)
result = LOUD_ARGUNDER;
}
else if (f > maxval)
{
*vp = (overaction & LOUD_CLIP ? maxval : f);
if (overaction)
result = LOUD_ARGOVER;
}
else *vp = f;
}
else result = LOUD_ARGTYPE;
}
else result = LOUD_ARGMISSING;
if (what)
{
switch (result)
{
case LOUD_ARGUNDER:
if (underaction & LOUD_WARN)
{
if (underaction & LOUD_CLIP)
loud_warning(&c, 0, "%s rounded up to %g", what, minval);
else
loud_warning(&c, 0, "less than %g %s requested",
minval, what);
}
break;
case LOUD_ARGOVER:
if (overaction & LOUD_WARN)
{
if (overaction & LOUD_CLIP)
loud_warning(&c, 0, "%s truncated to %g", what, maxval);
else
loud_warning(&c, 0, "more than %g %s requested",
maxval, what);
}
break;
case LOUD_ARGTYPE:
loud_error(0, "bad argument %d (%s)", which, class_getname(c));
break;
default:;
}
}
return (result);
}
void loudx_error(t_loudcontext *lc, char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
if (lc->lc_caller)
{
startpost("%s's ", (lc->lc_callername ?
lc->lc_callername : class_getname(*lc->lc_caller)));
pd_error(lc->lc_caller, buf);
}
else
{
if (lc->lc_callername)
post("error (%s): %s", lc->lc_callername, buf);
else if (lc->lc_selector)
post("error (%s): %s", lc->lc_selector->s_name, buf);
else
post("%s %s", LOUD_ERROR_DEFAULT, buf);
}
va_end(ap);
}
void loudx_errand(t_loudcontext *lc, char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
post("%*s%s", lc->lc_andindent, "", buf);
va_end(ap);
}
void loudx_nomethod(t_loudcontext *lc, t_symbol *s)
{
loudx_error(lc, "doesn't understand \"%s\"", s->s_name);
}
void loudx_messarg(t_loudcontext *lc, t_symbol *s)
{
loudx_error(lc, "bad arguments for message \"%s\"", s->s_name);
}
void loudx_warning(t_loudcontext *lc, char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
if (lc->lc_callername)
post("warning (%s): %s", lc->lc_callername, buf);
else if (lc->lc_selector)
post("warning (%s): %s", lc->lc_selector->s_name, buf);
else
post("warning (miXed): %s", buf);
va_end(ap);
}
void loudx_setcontext(t_loudcontext *lc, t_pd *caller, char *callername,
t_symbol *s, int ac, t_atom *av)
{
if (lc->lc_callername)
freebytes(lc->lc_callername, lc->lc_cnsize);
lc->lc_caller = caller;
if (callername)
{
lc->lc_cnsize = strlen(callername) + 1;
lc->lc_callername = getbytes(lc->lc_cnsize);
strcpy(lc->lc_callername, callername);
}
else
{
lc->lc_callername = 0;
lc->lc_cnsize = 0;
}
lc->lc_selector = s;
lc->lc_ac = ac;
lc->lc_av = av;
if (callername)
lc->lc_andindent = lc->lc_cnsize + 9;
else if (caller)
lc->lc_andindent = strlen(class_getname(*caller)) + 10;
else if (s)
lc->lc_andindent = strlen(s->s_name) + 10;
else
lc->lc_andindent = strlen(LOUD_ERROR_DEFAULT) + 1;
}
/* must call before going out of creation context */
void loudx_setcaller(t_loudcontext *lc, t_pd *caller, char *callerfmt, ...)
{
va_list ap;
va_start(ap, callerfmt);
if (callerfmt)
{
char buf[MAXPDSTRING];
vsprintf(buf, callerfmt, ap);
loudx_setcontext(lc, caller, buf, lc->lc_selector, 0, 0);
}
else loudx_setcontext(lc, caller, 0, lc->lc_selector, 0, 0);
va_end(ap);
}
t_symbol *loudx_getselector(t_loudcontext *lc)
{
return (lc->lc_selector);
}
t_atom *loudx_getarguments(t_loudcontext *lc, int *acp)
{
*acp = lc->lc_ac;
return (lc->lc_av);
}
void loudx_freecontext(t_loudcontext *lc)
{
if (lc->lc_callername)
freebytes(lc->lc_callername, lc->lc_cnsize);
freebytes(lc, sizeof(*lc));
}
t_loudcontext *loudx_newcontext(t_pd *caller, char *callername,
t_symbol *s, int ac, t_atom *av)
{
t_loudcontext *lc = getbytes(sizeof(*lc));
lc->lc_callername = 0;
loudx_setcontext(lc, caller, callername, s, ac, av);
return (lc);
}
void loudbug_post(char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", buf);
#ifdef MSW
fflush(stderr);
#endif
}
void loudbug_startpost(char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
fputs(buf, stderr);
#ifdef MSW
fflush(stderr);
#endif
}
void loudbug_stringpost(char *s)
{
fputs(s, stderr);
#ifdef MSW
fflush(stderr);
#endif
}
void loudbug_endpost(void)
{
fputs("\n", stderr);
#ifdef MSW
fflush(stderr);
#endif
}
void loudbug_postatom(int ac, t_atom *av)
{
while (ac--)
{
char buf[MAXPDSTRING];
atom_string(av++, buf, MAXPDSTRING);
fprintf(stderr, " %s", buf);
#ifdef MSW
fflush(stderr);
#endif
}
}
void loudbug_postbinbuf(t_binbuf *bb)
{
int ac = binbuf_getnatom(bb);
t_atom *aprev = 0, *ap = binbuf_getvec(bb);
while (ac--)
{
char buf[MAXPDSTRING];
atom_string(ap, buf, MAXPDSTRING);
if (aprev)
{
if (aprev->a_type == A_SEMI)
fprintf(stderr, "\n%s", buf);
else
fprintf(stderr, " %s", buf);
}
else fprintf(stderr, "%s", buf);
#ifdef MSW
fflush(stderr);
#endif
aprev = ap++;
}
if (aprev)
{
fputs("\n", stderr);
#ifdef MSW
fflush(stderr);
#endif
}
}
void loudbug_bug(char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
fprintf(stderr, "miXed consistency check failed: %s\n", buf);
#ifdef MSW
fflush(stderr);
#endif
bug(buf);
}
|