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-03-12 09:14:58 +04:00
|
|
|
#ifdef __APPLE__
|
2013-02-13 08:16:23 +04:00
|
|
|
#include "CocoaUtils.h"
|
2013-03-12 09:14:58 +04:00
|
|
|
#endif
|
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
|
|
|
|
2012-06-24 18:29:28 +04:00
|
|
|
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
|
|
|
*/
|
|
|
|
std::string locate_file(const std::string &filename)
|
2012-02-03 01:50:51 +04:00
|
|
|
{
|
2012-06-24 18:29:28 +04:00
|
|
|
BOOST_FOREACH(const std::string &dir, librarypath) {
|
|
|
|
fs::path usepath = fs::path(dir) / filename;
|
2012-08-18 20:38:24 +04:00
|
|
|
if (fs::exists(usepath) && !fs::is_directory(usepath)) return usepath.string();
|
2012-06-24 18:29:28 +04:00
|
|
|
}
|
|
|
|
return std::string();
|
2012-02-03 01:50:51 +04:00
|
|
|
}
|
|
|
|
|
2011-12-25 18:09:52 +04:00
|
|
|
void parser_init(const std::string &applicationpath)
|
2011-12-25 01:02:37 +04:00
|
|
|
{
|
2013-02-13 08:16:23 +04:00
|
|
|
// Add paths from OPENSCADPATH before adding built-in paths
|
|
|
|
const char *openscadpaths = getenv("OPENSCADPATH");
|
|
|
|
if (openscadpaths) {
|
|
|
|
std::string paths(openscadpaths);
|
|
|
|
typedef boost::split_iterator<std::string::iterator> string_split_iterator;
|
|
|
|
for (string_split_iterator it =
|
|
|
|
make_split_iterator(paths, first_finder(":", 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
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Add ~/.openscad/libraries
|
2013-03-05 23:16:49 +04:00
|
|
|
#if defined(__APPLE__) && !defined(OPENSCAD_TESTING)
|
2013-02-13 08:16:23 +04:00
|
|
|
fs::path docdir(CocoaUtils::documentsPath());
|
|
|
|
add_librarydir(boosty::stringy(docdir / "OpenSCAD" / "libraries"));
|
|
|
|
#endif
|
2012-06-24 18:29:28 +04:00
|
|
|
|
2012-02-03 01:50:51 +04:00
|
|
|
std::string librarydir;
|
2012-06-24 18:29:28 +04:00
|
|
|
fs::path libdir(applicationpath);
|
|
|
|
fs::path tmpdir;
|
2012-08-19 03:08:07 +04:00
|
|
|
#ifdef __APPLE__
|
2011-12-25 02:08:38 +04:00
|
|
|
libdir /= "../Resources"; // Libraries can be bundled
|
|
|
|
if (!is_directory(libdir / "libraries")) libdir /= "../../..";
|
2012-08-19 03:08:07 +04:00
|
|
|
#elif !defined(WIN32)
|
2011-12-25 02:08:38 +04:00
|
|
|
if (is_directory(tmpdir = libdir / "../share/openscad/libraries")) {
|
2012-01-14 08:02:15 +04:00
|
|
|
librarydir = boosty::stringy( tmpdir );
|
2011-12-25 02:08:38 +04:00
|
|
|
} else if (is_directory(tmpdir = libdir / "../../share/openscad/libraries")) {
|
2012-01-14 08:02:15 +04:00
|
|
|
librarydir = boosty::stringy( tmpdir );
|
2011-12-25 02:08:38 +04:00
|
|
|
} else if (is_directory(tmpdir = libdir / "../../libraries")) {
|
2012-01-14 08:02:15 +04:00
|
|
|
librarydir = boosty::stringy( tmpdir );
|
2011-12-25 01:02:37 +04:00
|
|
|
} else
|
|
|
|
#endif
|
2012-01-14 08:02:15 +04:00
|
|
|
if (is_directory(tmpdir = libdir / "libraries")) {
|
|
|
|
librarydir = boosty::stringy( tmpdir );
|
2011-12-25 01:02:37 +04:00
|
|
|
}
|
2012-06-24 18:29:28 +04:00
|
|
|
if (!librarydir.empty()) add_librarydir(librarydir);
|
2011-12-25 01:02:37 +04:00
|
|
|
}
|