diff options
author | Don Bright <hugh.m.bright@gmail.com> | 2013-05-27 05:08:31 (GMT) |
---|---|---|
committer | Don Bright <hugh.m.bright@gmail.com> | 2013-05-27 05:08:31 (GMT) |
commit | 37dc9342a14206f5862447eb9d3d5049ec67f638 (patch) | |
tree | 17e5a57dfa154821918094cab47476189771ce00 /src/parsersettings.cc | |
parent | 6742ad55f4cf335fc767ab65784aab81e06b61d0 (diff) | |
parent | 0967a26bff45951d7b86fe628e8b1156e6e40ede (diff) |
Merge branch 'master' of github.com:openscad/openscad into issue125
Diffstat (limited to 'src/parsersettings.cc')
-rw-r--r-- | src/parsersettings.cc | 76 |
1 files changed, 68 insertions, 8 deletions
diff --git a/src/parsersettings.cc b/src/parsersettings.cc index 48a6cf8..8db33a8 100644 --- a/src/parsersettings.cc +++ b/src/parsersettings.cc @@ -18,13 +18,73 @@ void add_librarydir(const std::string &libdir) Searces for the given file in library paths and returns the full path if found. Returns an empty path if file cannot be found or filename is a directory. */ -std::string locate_file(const std::string &filename) +fs::path search_libs(const fs::path &localpath) { BOOST_FOREACH(const std::string &dir, librarypath) { - fs::path usepath = fs::path(dir) / filename; - if (fs::exists(usepath) && !fs::is_directory(usepath)) return usepath.string(); + fs::path usepath = fs::path(dir) / localpath; + if (fs::exists(usepath) && !fs::is_directory(usepath)) { + return usepath.string(); + } + } + return fs::path(); +} + +// files must be 'ordinary' - they must exist and be non-directories +static bool check_valid(const fs::path &p, const std::vector<std::string> *openfilenames) +{ + if (p.empty()) { + PRINTB("WARNING: File path is blank: %s",p); + return false; + } + if (!p.has_parent_path()) { + PRINTB("WARNING: No parent path: %s",p); + return false; + } + if (!fs::exists(p)) { + PRINTB("WARNING: File not found: %s",p); + // searched === + return false; + } + if (fs::is_directory(p)) { + PRINTB("WARNING: %s invalid - points to a directory",p); + return false; + } + std::string fullname = boosty::stringy(p); + // Detect circular includes + if (openfilenames) { + BOOST_FOREACH(const std::string &s, *openfilenames) { + if (s == fullname) { + PRINTB("WARNING: circular include file %s", fullname); + return false; + } + } + } + return true; +} + +/*! + Check if the given filename is valid. + + If the given filename is absolute, do a simple check. + If not, search the applicable paths for a valid file. + + Returns the absolute path to a valid file, or an empty path if no + valid files could be found. +*/ +fs::path find_valid_path(const fs::path &sourcepath, + const fs::path &localpath, + const std::vector<std::string> *openfilenames) +{ + if (boosty::is_absolute(localpath)) { + if (check_valid(localpath, openfilenames)) return boosty::absolute(localpath); + } + else { + fs::path fpath = sourcepath / localpath; + if (check_valid(fpath, openfilenames)) return fpath; + fpath = search_libs(localpath); + if (!fpath.empty() && check_valid(fpath, openfilenames)) return fpath; } - return std::string(); + return fs::path(); } void parser_init(const std::string &applicationpath) @@ -59,15 +119,15 @@ void parser_init(const std::string &applicationpath) if (!is_directory(libdir / "libraries")) libdir /= "../../.."; #elif !defined(WIN32) if (is_directory(tmpdir = libdir / "../share/openscad/libraries")) { - librarydir = boosty::stringy( tmpdir ); + librarydir = boosty::stringy(tmpdir); } else if (is_directory(tmpdir = libdir / "../../share/openscad/libraries")) { - librarydir = boosty::stringy( tmpdir ); + librarydir = boosty::stringy(tmpdir); } else if (is_directory(tmpdir = libdir / "../../libraries")) { - librarydir = boosty::stringy( tmpdir ); + librarydir = boosty::stringy(tmpdir); } else #endif if (is_directory(tmpdir = libdir / "libraries")) { - librarydir = boosty::stringy( tmpdir ); + librarydir = boosty::stringy(tmpdir); } if (!librarydir.empty()) add_librarydir(librarydir); } |