#include #include "PlatformUtils.h" #include "boosty.h" #include #ifdef USE_SCINTILLA_EDITOR #include #endif extern std::vector librarypath; extern std::vector fontpath; namespace { std::string applicationpath; } void PlatformUtils::registerApplicationPath(const std::string &apppath) { applicationpath = apppath; } std::string PlatformUtils::applicationPath() { return applicationpath; } bool PlatformUtils::createUserLibraryPath() { std::string path = PlatformUtils::userLibraryPath(); bool OK = false; try { if (!fs::exists(fs::path(path))) { //PRINTB("Creating library folder %s", path ); OK = fs::create_directories( path ); } if (!OK) { PRINTB("ERROR: Cannot create %s", path ); } } catch (const fs::filesystem_error& ex) { PRINTB("ERROR: %s",ex.what()); } return OK; } std::string PlatformUtils::userLibraryPath() { fs::path path; try { std::string pathstr = PlatformUtils::documentsPath(); if (pathstr=="") return ""; path = boosty::canonical(fs::path( pathstr )); //PRINTB("path size %i",boosty::stringy(path).size()); //PRINTB("lib path found: [%s]", path ); if (path.empty()) return ""; path /= "OpenSCAD"; path /= "libraries"; //PRINTB("Appended path %s", path ); //PRINTB("Exists: %i", fs::exists(path) ); } catch (const fs::filesystem_error& ex) { PRINTB("ERROR: %s",ex.what()); } return boosty::stringy( path ); } std::string PlatformUtils::backupPath() { fs::path path; try { std::string pathstr = PlatformUtils::documentsPath(); if (pathstr=="") return ""; path = boosty::canonical(fs::path( pathstr )); if (path.empty()) return ""; path /= "OpenSCAD"; path /= "backups"; } catch (const fs::filesystem_error& ex) { PRINTB("ERROR: %s",ex.what()); } return boosty::stringy( path ); } bool PlatformUtils::createBackupPath() { std::string path = PlatformUtils::backupPath(); bool OK = false; try { if (!fs::exists(fs::path(path))) { OK = fs::create_directories( path ); } if (!OK) { PRINTB("ERROR: Cannot create %s", path ); } } catch (const fs::filesystem_error& ex) { PRINTB("ERROR: %s",ex.what()); } return OK; } // This is the built-in read-only resources path std::string PlatformUtils::resourcesPath() { fs::path resourcedir(applicationPath()); fs::path tmpdir; #ifdef __APPLE__ // Resources can be bundled on Mac. If not, fall back to development layout bool isbundle = is_directory(resourcedir / ".." / "Resources"); if (isbundle) { resourcedir /= "../Resources"; // Fall back to dev layout if (!is_directory(resourcedir / "libraries")) resourcedir /= "../../.."; } #elif !defined(WIN32) const char *searchpath[] = { "../share/openscad", "../../share/openscad", ".", "..", "../..", NULL }; for (int a = 0;searchpath[a] != NULL;a++) { tmpdir = resourcedir / searchpath[a]; if (is_directory(tmpdir / "libraries")) { resourcedir = tmpdir; break; } } #endif // resourcedir defaults to applicationPath return boosty::stringy(boosty::canonical(resourcedir)); } int PlatformUtils::setenv(const char *name, const char *value, int overwrite) { #if defined(WIN32) const char *ptr = getenv(name); if ((overwrite == 0) && (ptr != NULL)) { return 0; } char buf[4096]; snprintf(buf, sizeof(buf), "%s=%s", name, value); return _putenv(buf); #else return ::setenv(name, value, overwrite); #endif }