2011-12-25 01:02:37 +04:00
|
|
|
#include "parsersettings.h"
|
2011-12-25 02:08:38 +04:00
|
|
|
#include <boost/filesystem.hpp>
|
2012-06-24 18:29:28 +04:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include "boosty.h"
|
2013-02-13 08:16:23 +04:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2013-05-22 01:45:24 +04:00
|
|
|
#include "PlatformUtils.h"
|
2011-12-25 01:02:37 +04:00
|
|
|
|
2012-06-24 18:29:28 +04:00
|
|
|
namespace fs = boost::filesystem;
|
2011-12-25 02:08:38 +04:00
|
|
|
|
2012-06-24 18:29:28 +04:00
|
|
|
std::vector<std::string> librarypath;
|
2011-12-25 01:02:37 +04:00
|
|
|
|
2014-10-18 21:25:52 +04:00
|
|
|
static void add_librarydir(const std::string &libdir)
|
2012-02-03 01:50:51 +04:00
|
|
|
{
|
2012-06-24 18:29:28 +04:00
|
|
|
librarypath.push_back(libdir);
|
2012-02-03 01:50:51 +04:00
|
|
|
}
|
|
|
|
|
2012-06-24 18:29:28 +04:00
|
|
|
/*!
|
|
|
|
Searces for the given file in library paths and returns the full path if found.
|
2012-08-18 20:38:24 +04:00
|
|
|
Returns an empty path if file cannot be found or filename is a directory.
|
2012-06-24 18:29:28 +04:00
|
|
|
*/
|
2013-05-26 01:45:13 +04:00
|
|
|
fs::path search_libs(const fs::path &localpath)
|
2012-02-03 01:50:51 +04:00
|
|
|
{
|
2012-06-24 18:29:28 +04:00
|
|
|
BOOST_FOREACH(const std::string &dir, librarypath) {
|
2013-05-26 01:45:13 +04:00
|
|
|
fs::path usepath = fs::path(dir) / localpath;
|
2013-05-20 03:04:51 +04:00
|
|
|
if (fs::exists(usepath) && !fs::is_directory(usepath)) {
|
|
|
|
return usepath.string();
|
|
|
|
}
|
2012-06-24 18:29:28 +04:00
|
|
|
}
|
2013-05-20 00:14:05 +04:00
|
|
|
return fs::path();
|
|
|
|
}
|
|
|
|
|
2013-05-26 23:08:23 +04:00
|
|
|
// files must be 'ordinary' - they must exist and be non-directories
|
2013-05-28 02:28:29 +04:00
|
|
|
// FIXME: We cannot print any output here since these function is called periodically
|
|
|
|
// from "Automatic reload and compile"
|
2013-05-26 01:45:13 +04:00
|
|
|
static bool check_valid(const fs::path &p, const std::vector<std::string> *openfilenames)
|
2013-05-20 00:14:05 +04:00
|
|
|
{
|
2013-05-20 03:04:51 +04:00
|
|
|
if (p.empty()) {
|
2013-05-28 09:25:03 +04:00
|
|
|
//PRINTB("WARNING: File path is blank: %s",p);
|
2013-05-20 03:04:51 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-20 00:14:05 +04:00
|
|
|
if (!p.has_parent_path()) {
|
2013-05-28 09:25:03 +04:00
|
|
|
//PRINTB("WARNING: No parent path: %s",p);
|
2013-05-20 00:14:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!fs::exists(p)) {
|
2013-05-28 09:25:03 +04:00
|
|
|
//PRINTB("WARNING: File not found: %s",p);
|
2013-05-20 00:14:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fs::is_directory(p)) {
|
2013-05-28 09:25:03 +04:00
|
|
|
//PRINTB("WARNING: %s invalid - points to a directory",p);
|
2013-05-20 00:14:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:45:13 +04:00
|
|
|
std::string fullname = boosty::stringy(p);
|
|
|
|
// Detect circular includes
|
|
|
|
if (openfilenames) {
|
|
|
|
BOOST_FOREACH(const std::string &s, *openfilenames) {
|
|
|
|
if (s == fullname) {
|
2013-05-28 02:28:29 +04:00
|
|
|
// PRINTB("WARNING: circular include file %s", fullname);
|
2013-05-26 01:45:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-05-20 03:04:51 +04:00
|
|
|
}
|
2013-05-20 00:14:05 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:55:00 +04:00
|
|
|
/*!
|
|
|
|
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.
|
|
|
|
*/
|
2013-05-26 01:45:13 +04:00
|
|
|
fs::path find_valid_path(const fs::path &sourcepath,
|
|
|
|
const fs::path &localpath,
|
|
|
|
const std::vector<std::string> *openfilenames)
|
2013-05-20 00:14:05 +04:00
|
|
|
{
|
2013-05-26 01:45:13 +04:00
|
|
|
if (boosty::is_absolute(localpath)) {
|
2014-07-31 23:58:59 +04:00
|
|
|
if (check_valid(localpath, openfilenames)) return boosty::canonical(localpath);
|
2013-05-26 01:45:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fs::path fpath = sourcepath / localpath;
|
2014-07-31 23:58:59 +04:00
|
|
|
if (fs::exists(fpath)) fpath = boosty::canonical(fpath);
|
2013-05-26 01:45:13 +04:00
|
|
|
if (check_valid(fpath, openfilenames)) return fpath;
|
|
|
|
fpath = search_libs(localpath);
|
2013-05-26 23:08:23 +04:00
|
|
|
if (!fpath.empty() && check_valid(fpath, openfilenames)) return fpath;
|
2012-06-24 18:29:28 +04:00
|
|
|
}
|
2013-05-20 00:14:05 +04:00
|
|
|
return fs::path();
|
2012-02-03 01:50:51 +04:00
|
|
|
}
|
|
|
|
|
2014-10-19 03:19:53 +04:00
|
|
|
void parser_init()
|
2011-12-25 01:02:37 +04:00
|
|
|
{
|
2014-02-17 00:13:21 +04:00
|
|
|
// Add paths from OPENSCADPATH before adding built-in paths
|
2013-02-13 08:16:23 +04:00
|
|
|
const char *openscadpaths = getenv("OPENSCADPATH");
|
|
|
|
if (openscadpaths) {
|
|
|
|
std::string paths(openscadpaths);
|
2014-02-17 00:13:21 +04:00
|
|
|
std::string sep = PlatformUtils::pathSeparatorChar();
|
|
|
|
typedef boost::split_iterator<std::string::iterator> string_split_iterator;
|
|
|
|
for (string_split_iterator it = boost::make_split_iterator(paths, boost::first_finder(sep, boost::is_iequal())); it != string_split_iterator(); ++it) {
|
|
|
|
add_librarydir(boosty::absolute(fs::path(boost::copy_range<std::string>(*it))).string());
|
|
|
|
}
|
2012-10-24 06:10:15 +04:00
|
|
|
}
|
|
|
|
|
2013-05-22 01:45:24 +04:00
|
|
|
#ifndef OPENSCAD_TESTING
|
2014-08-28 00:00:15 +04:00
|
|
|
add_librarydir(PlatformUtils::userLibraryPath());
|
2013-02-13 08:16:23 +04:00
|
|
|
#endif
|
2012-06-24 18:29:28 +04:00
|
|
|
|
2014-10-19 03:19:53 +04:00
|
|
|
add_librarydir(boosty::absolute(PlatformUtils::resourcePath("libraries")).string());
|
2011-12-25 01:02:37 +04:00
|
|
|
}
|