Skip to content

Commit 4dedb5b

Browse files
committed
load_pat.cpp: fix several issues with processing of timidity.cfg
- skips leading whitespace - detect bank directive - warn on soundfont directive - implement support for dir directive - only appends .pat extension if isn't already there The patch was originally authored by @rofl0r back in the day for icculus/SDL_sound#16.
1 parent 8d0b03a commit 4dedb5b

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/load_pat.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ void pat_init_patnames(void)
387387
isdrumset = 0;
388388
_mm_fgets(mmcfg, line, PATH_MAX);
389389
while( !_mm_feof(mmcfg) ) {
390-
if( isdigit(line[0]) || (isblank(line[0]) && isdigit(line[1])) ) {
391-
p = line;
390+
p = line;
391+
while ( isspace(*p) ) p ++;
392+
if( isdigit(p[0]) ) {
392393
// get pat number
393-
while ( isspace(*p) ) p ++;
394394
i = atoi(p);
395395
while ( isdigit(*p) ) p ++;
396396
while ( isspace(*p) ) p ++;
@@ -418,10 +418,25 @@ void pat_init_patnames(void)
418418
*q++ = '\0';
419419
}
420420
}
421-
if( !strncmp(line,"drumset",7) ) isdrumset = 1;
422-
if( !strncmp(line,"source",6) && nsources < 5 ) {
421+
else if( !strncmp(p,"bank",4) ) isdrumset = 0;
422+
else if( !strncmp(p,"drumset",7) ) isdrumset = 1;
423+
else if( !strncmp(p,"soundfont",9) ) {
424+
fprintf(stderr, "warning: soundfont directive unsupported!\n");
425+
}
426+
else if( !strncmp(p,"dir",3) ) {
427+
p += 3;
428+
while ( isspace(*p) ) p ++;
429+
q = p + strlen(p);
430+
if(q > p) {
431+
--q;
432+
while ( q > p && isspace(*q) ) *(q--) = 0;
433+
strncpy(pathforpat, p, PATH_MAX - 1);
434+
pathforpat[PATH_MAX - 1] = 0;
435+
}
436+
}
437+
else if( !strncmp(p,"source",6) && nsources < 5 ) {
423438
q = cfgsources[nsources];
424-
p = &line[7];
439+
p += 6;
425440
while ( isspace(*p) ) p ++;
426441
pfnlen = 0;
427442
while ( *p && *p != '#' && !isspace(*p) && pfnlen < 128 ) {
@@ -459,17 +474,19 @@ void pat_init_patnames(void)
459474

460475
static char *pat_build_path(char *fname, int pat)
461476
{
462-
char *ps;
477+
char *ps, *p;
463478
char *patfile = midipat[pat];
464-
int isabspath = (patfile[0] == '/');
479+
int has_ext = 0, isabspath = (patfile[0] == '/');
465480
if ( isabspath ) patfile ++;
466481
ps = strrchr(patfile, ':');
467482
if( ps ) {
468483
sprintf(fname, "%s%c%s", isabspath ? "" : pathforpat, DIRDELIM, patfile);
469484
strcpy(strrchr(fname, ':'), ".pat");
470485
return ps;
471486
}
472-
sprintf(fname, "%s%c%s.pat", isabspath ? "" : pathforpat, DIRDELIM, patfile);
487+
p = strrchr(patfile, '.');
488+
if(p && !strcasecmp(p, ".pat")) has_ext = 1;
489+
sprintf(fname, "%s%c%s%s", isabspath ? "" : pathforpat, DIRDELIM, patfile, has_ext ? "" : ".pat");
473490
return 0;
474491
}
475492

0 commit comments

Comments
 (0)