allow to use absolute file paths

This commit is contained in:
2021-07-05 17:22:54 +02:00
parent a06006eade
commit fe1ac364ab
2 changed files with 54 additions and 42 deletions

View File

@ -461,8 +461,7 @@ drvInit()
#endif #endif
if (!path) if (!path)
fprintf(stderr, fprintf(stderr,
"drvStreamInit: Warning! STREAM_PROTOCOL_PATH not set. " "drvStreamInit: Warning! STREAM_PROTOCOL_PATH not set.\n");
"Defaults to \"%s\"\n", StreamProtocolParser::path);
else else
StreamProtocolParser::path = path; StreamProtocolParser::path = path;
debug("StreamProtocolParser::path = %s\n", debug("StreamProtocolParser::path = %s\n",

View File

@ -53,7 +53,7 @@ class StreamProtocolParser::Protocol::Variable
// StreamProtocolParser // StreamProtocolParser
StreamProtocolParser* StreamProtocolParser::parsers = NULL; StreamProtocolParser* StreamProtocolParser::parsers = NULL;
const char* StreamProtocolParser::path = "."; const char* StreamProtocolParser::path = NULL;
static const char* specialChars = " ,;{}=()$'\"+-*/"; static const char* specialChars = " ,;{}=()$'\"+-*/";
// Client destructor // Client destructor
@ -157,57 +157,70 @@ this after protocol arguments have been replaced.
StreamProtocolParser* StreamProtocolParser:: StreamProtocolParser* StreamProtocolParser::
readFile(const char* filename) readFile(const char* filename)
{ {
FILE* file; FILE* file = NULL;
StreamProtocolParser* parser; StreamProtocolParser* parser;
const char *p; const char *p;
size_t n; size_t n;
StreamBuffer dir; StreamBuffer dir;
// look for filename in every dir in search path // no path or absolute file name
for (p = path; *p; p += n) if (!path || filename[0] == '/'
{
dir.clear();
// allow ':' or ';' for OS independence
// we need to be careful with drive letters though
n = strcspn(p, ":;");
#ifdef _WIN32 #ifdef _WIN32
if (n == 1 && p[1] == ':' && isalpha(p[0])) || filename[0] == '\\' || (isalpha(filename[0]) && filename[1] == ':')
{
// driver letter
n = 2 + strcspn(p+2, ":;");
}
#endif #endif
dir.append(p, n); ) {
// append / after everything except empty path [or drive letter] // absolute file name
// Windows is fine with / as well file = fopen(filename, "r");
if (n) { if (file) {
debug("StreamProtocolParser::readFile: found '%s'\n", filename);
} else {
error("Can't find readable file '%s'\n", filename);
return NULL;
}
} else {
// look for filename in every dir in search path
for (p = path; *p; p += n)
{
dir.clear();
// allow ':' or ';' for OS independence
// we need to be careful with drive letters though
n = strcspn(p, ":;");
#ifdef _WIN32 #ifdef _WIN32
if (n != 2 || p[1] != ':' || !isalpha(p[0])) if (n == 1 && p[1] == ':' && isalpha(p[0]))
{
// driver letter
n = 2 + strcspn(p+2, ":;");
}
#endif #endif
dir.append('/'); dir.append(p, n);
// append / after everything except empty path [or drive letter]
// Windows is fine with / as well
if (n) {
#ifdef _WIN32
if (n != 2 || p[1] != ':' || !isalpha(p[0]))
#endif
dir.append('/');
}
if (p[n]) n++; // skip the path separator
dir.append(filename);
// try to read the file
debug("StreamProtocolParser::readFile: try '%s'\n", dir());
file = fopen(dir(), "r");
if (file) {
debug("StreamProtocolParser::readFile: found '%s'\n", dir());
break;
}
} }
if (p[n]) n++; // skip the path separator if (!file) {
dir.append(filename); error("Can't find readable file '%s' in '%s'\n", filename, path);
// try to read the file return NULL;
debug("StreamProtocolParser::readFile: try '%s'\n", dir());
file = fopen(dir(), "r");
if (file)
{
// file found; create a parser to read it
debug("StreamProtocolParser::readFile: found '%s'\n", dir());
parser = new StreamProtocolParser(file, filename);
fclose(file);
if (!parser->valid) return NULL;
// printf(
// "/---------------------------------------------------------------------\\\n");
// parser->report();
// printf(
// "\\---------------------------------------------------------------------/\n");
return parser;
} }
} }
error("Can't find readable file '%s' in '%s'\n", filename, path); // file found; create a parser to read it
return NULL; parser = new StreamProtocolParser(file, filename);
fclose(file);
if (!parser->valid) return NULL;
return parser;
} }
/* /*