openscad/src/PlatformUtils.cc

149 lines
3.4 KiB
C++
Raw Normal View History

#include <stdlib.h>
#include "PlatformUtils.h"
#include "boosty.h"
2014-05-24 23:41:04 +04:00
#include <Eigen/Core>
#ifdef USE_SCINTILLA_EDITOR
#include <Qsci/qsciglobal.h>
#endif
extern std::vector<std::string> librarypath;
2014-07-06 06:28:16 +04:00
extern std::vector<std::string> fontpath;
2014-08-28 00:00:15 +04:00
namespace {
std::string applicationpath;
}
void PlatformUtils::registerApplicationPath(const std::string &apppath)
{
applicationpath = apppath;
}
std::string PlatformUtils::applicationPath()
{
return applicationpath;
}
bool PlatformUtils::createUserLibraryPath()
{
2014-08-28 00:00:15 +04:00
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;
}
2014-08-28 00:00:15 +04:00
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;
}
2014-08-28 00:00:15 +04:00
// 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")) {
2014-08-28 00:00:15 +04:00
resourcedir = tmpdir;
break;
}
2014-08-28 00:00:15 +04:00
}
#endif
// resourcedir defaults to applicationPath
2014-08-28 08:39:20 +04:00
return boosty::stringy(boosty::canonical(resourcedir));
2014-08-28 00:00:15 +04:00
}
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
}