- hooking in the PathExpander.

This commit is contained in:
Christoph Oelckers 2018-02-19 22:48:13 +01:00
parent 576932c47f
commit 06a1730b8a
7 changed files with 75 additions and 24 deletions

View file

@ -30,10 +30,14 @@
#include <ctype.h>
#include "m_random.h"
#include "common.h"
#include "pathexpander.h"
#include "cmdlib.h"
namespace TimidityPlus
{
static PathExpander tppPathExpander;
/* This'll allocate memory or die. */
void *safe_malloc(size_t count)
@ -174,17 +178,50 @@ int PathList::pathcmp(const char *p1, const char *p2, int ignore_case)
return c1 - c2;
}
std::pair<FileReader *, std::string> PathList::openFile(const char *name)
FileReader *PathList::tryOpenPath(const char *name, bool ismain)
{
FileReader *fp;
int lumpnum;
if (ismain)
{
tppPathExpander.openmode = PathExpander::OM_FILEORLUMP;
tppPathExpander.clearPathlist();
#ifdef _WIN32
tppPathExpander.addToPathlist("C:\\TIMIDITY");
tppPathExpander.addToPathlist("\\TIMIDITY");
tppPathExpander.addToPathlist(progdir);
#else
tppPathExpander.addToPathlist("/usr/local/lib/timidity");
tppPathExpander.addToPathlist("/etc/timidity");
tppPathExpander.addToPathlist("/etc");
#endif
}
if (!(fp = tppPathExpander.openFileReader(name, &lumpnum)))
return NULL;
if (ismain)
{
if (lumpnum > 0)
{
tppPathExpander.openmode = PathExpander::OM_LUMP;
tppPathExpander.clearPathlist(); // when reading from a PK3 we don't want to use any external path
}
else
{
tppPathExpander.openmode = PathExpander::OM_FILE;
}
}
return fp;
}
std::pair<FileReader *, std::string> PathList::openFile(const char *name, bool ismainfile)
{
if (name && *name)
{
/* First try the given name */
FileReader *fr = new FileReader;
if (fr->Open(name))
{
return std::make_pair(fr, std::string(name));
}
if (!isAbsPath(name))
{
@ -197,11 +234,21 @@ std::pair<FileReader *, std::string> PathList::openFile(const char *name)
s += '/';
}
s += name;
if (fr->Open(s.c_str())) return std::make_pair(fr, s);
auto fr = tryOpenPath(name, ismainfile);
if (fr!= nullptr) return std::make_pair(fr, s);
}
}
else
{
// an absolute path is never looked up.
FileReader *fr = new FileReader;
if (fr->Open(name))
{
return std::make_pair(fr, std::string(name));
}
delete fr;
}
}
return std::make_pair(nullptr, std::string());
}
@ -218,9 +265,10 @@ int PathList::isAbsPath(const char *name)
return 0;
}
struct timidity_file *open_file(const char *name, int decompress, int noise_mode, PathList &pathList)
struct timidity_file *open_file(const char *name, bool ismainfile, PathList &pathList)
{
auto file = pathList.openFile(name);
auto file = pathList.openFile(name, ismainfile);
if (!file.first) return nullptr;
auto tf = new timidity_file;
tf->url = file.first;

View file

@ -37,6 +37,7 @@ class PathList
std::vector<std::string> paths;
int isAbsPath(const char *name);
FileReader *tryOpenPath(const char *name, bool ismain);
public:
PathList();
@ -47,7 +48,7 @@ public:
paths.resize(1);
}
static int pathcmp(const char *p1, const char *p2, int ignore_case);
std::pair<FileReader *, std::string> openFile(const char *name);
std::pair<FileReader *, std::string> openFile(const char *name, bool ismainfile);
};
@ -66,7 +67,7 @@ enum
};
extern struct timidity_file *open_file(const char *name, int decompress, int noise_mode, PathList &);
extern struct timidity_file *open_file(const char *name, bool, PathList &);
extern void close_file(struct timidity_file *tf);
extern void skip(struct timidity_file *tf, size_t len);
extern char *tf_gets(char *buff, int n, struct timidity_file *tf);

View file

@ -617,7 +617,7 @@ char *Instruments::expand_variables(char *string, MBlockList *varbuf, const char
}
int Instruments::read_config_file(const char *name, int self, int allow_missing_file)
int Instruments::read_config_file(const char *name, int self, int allow_missing_file, bool ismainfile)
{
struct timidity_file *tf;
char buf[1024], *tmp, *w[MAXWORDS + 1], *cp;
@ -637,7 +637,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_
return READ_CONFIG_RECURSION;
}
tf = open_file(name, 1, allow_missing_file ? OF_NORMAL : OF_VERBOSE, pathlist);
tf = open_file(name, ismainfile, pathlist);
if (tf == NULL)
return allow_missing_file ? READ_CONFIG_FILE_NOT_FOUND :
READ_CONFIG_ERROR;

View file

@ -54,7 +54,7 @@ Instruments::Instruments()
bool Instruments::load(const char *config)
{
if (read_config_file(config, 0, 0))
if (read_config_file(config, 0, 0, true))
{
init_load_soundfont();
set_default_instrument();
@ -615,7 +615,7 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr,
return ip;
}
/* Open patch file */
if (!(tf = open_file(name, 2, OF_NORMAL, pathlist))) {
if (!(tf = open_file(name, false, pathlist))) {
#ifdef PATCH_EXT_LIST
int name_len, ext_len;
static const char *patch_ext[] = PATCH_EXT_LIST;
@ -633,7 +633,8 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr,
continue; /* duplicated ext. */
strcpy((char *)tmp, name);
strcat((char *)tmp, patch_ext[i]);
if ((tf = open_file((char *)tmp, 1, OF_NORMAL, pathlist))) {
if ((tf = open_file((char *)tmp, false, pathlist)))
{
noluck = 0;
break;
}

View file

@ -546,7 +546,7 @@ public:
void PrecacheInstruments(const uint16_t *instruments, int count);
int read_config_file(const char *name, int self, int allow_missing_file);
int read_config_file(const char *name, int self, int allow_missing_file, bool ismainfile = false);
void set_default_instrument()
{

View file

@ -278,7 +278,7 @@ int Instruments::import_wave_discriminant(char *sample_file)
struct timidity_file *tf;
char buf[12];
if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL)
if ((tf = open_file(sample_file, false, pathlist)) == NULL)
return 1;
if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0)
@ -309,7 +309,7 @@ int Instruments::import_wave_load(char *sample_file, Instrument *inst)
WAVSamplerChunk samplerc = {0,};
GeneralInstrumentInfo instc;
if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL)
if ((tf = open_file(sample_file, false, pathlist)) == NULL)
return 1;
if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0)
@ -553,7 +553,7 @@ int Instruments::import_aiff_discriminant(char *sample_file)
struct timidity_file *tf;
char buf[12];
if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL)
if ((tf = open_file(sample_file, false, pathlist)) == NULL)
return 1;
if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0
@ -593,7 +593,7 @@ int Instruments::import_aiff_load(char *sample_file, Instrument *inst)
AIFFLoopInfo loop_info = {0,0,0};
AIFFMarkerData *marker_data;
if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL)
if ((tf = open_file(sample_file, false, pathlist)) == NULL)
return 1;
if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0

View file

@ -265,7 +265,7 @@ void Instruments::init_sf(SFInsts *rec)
SFInfo sfinfo;
int i;
if ((rec->tf = open_file(rec->fname, 1, OF_VERBOSE, pathlist)) == NULL) {
if ((rec->tf = open_file(rec->fname, false, pathlist)) == NULL) {
ctl_cmsg(CMSG_ERROR, VERB_NORMAL,
"Can't open soundfont file %s", rec->fname);
end_soundfont(rec);
@ -362,7 +362,8 @@ Instrument *Instruments::try_load_soundfont(SFInsts *rec, int order, int bank,in
if (rec->tf == NULL) {
if (rec->fname == NULL)
return NULL;
if ((rec->tf = open_file(rec->fname, 1, OF_VERBOSE, pathlist)) == NULL) {
if ((rec->tf = open_file(rec->fname, false, pathlist)) == NULL)
{
ctl_cmsg(CMSG_ERROR, VERB_NORMAL,
"Can't open soundfont file %s", rec->fname);
end_soundfont(rec);