2014-12-09 21:14:43 +03:00
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
|
|
|
#include <unistd.h>
|
2014-11-25 01:34:57 +03:00
|
|
|
#include <sys/resource.h>
|
2014-12-09 21:14:43 +03:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2014-11-25 01:34:57 +03:00
|
|
|
|
2013-05-22 01:45:24 +04:00
|
|
|
#include "PlatformUtils.h"
|
2013-05-22 02:19:29 +04:00
|
|
|
#include "boosty.h"
|
2013-05-22 01:45:24 +04:00
|
|
|
|
2014-02-17 00:13:21 +04:00
|
|
|
std::string PlatformUtils::pathSeparatorChar()
|
|
|
|
{
|
|
|
|
return ":";
|
|
|
|
}
|
|
|
|
|
2013-05-22 01:45:24 +04:00
|
|
|
std::string PlatformUtils::documentsPath()
|
|
|
|
{
|
2014-01-23 06:27:25 +04:00
|
|
|
const char *home = getenv("HOME");
|
|
|
|
if (home) {
|
|
|
|
fs::path docpath(home);
|
|
|
|
docpath = docpath / ".local" / "share";
|
|
|
|
return boosty::stringy(docpath);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "";
|
|
|
|
}
|
2013-05-22 01:45:24 +04:00
|
|
|
}
|
2014-05-17 09:02:44 +04:00
|
|
|
|
2014-11-02 01:06:36 +03:00
|
|
|
// see http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
std::string PlatformUtils::userConfigPath()
|
|
|
|
{
|
|
|
|
fs::path config_path;
|
|
|
|
|
|
|
|
const char *xdg_env = getenv("XDG_CONFIG_HOME");
|
|
|
|
if (xdg_env && fs::exists(fs::path(xdg_env))) {
|
2014-11-02 18:39:51 +03:00
|
|
|
config_path = fs::path(xdg_env) / OPENSCAD_FOLDER_NAME;
|
2014-11-02 01:06:36 +03:00
|
|
|
} else {
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
if (home) {
|
2014-11-02 18:39:51 +03:00
|
|
|
config_path = fs::path(home) / ".config" / OPENSCAD_FOLDER_NAME;
|
2014-11-02 01:06:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs::is_directory(config_path)) {
|
|
|
|
return boosty::stringy(boosty::absolute(config_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-11-25 01:34:57 +03:00
|
|
|
unsigned long PlatformUtils::stackLimit()
|
|
|
|
{
|
|
|
|
struct rlimit limit;
|
|
|
|
|
|
|
|
int ret = getrlimit(RLIMIT_STACK, &limit);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (limit.rlim_cur > STACK_BUFFER_SIZE) {
|
|
|
|
return limit.rlim_cur - STACK_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
if (limit.rlim_max > STACK_BUFFER_SIZE) {
|
|
|
|
return limit.rlim_max - STACK_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STACK_LIMIT_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2014-12-09 21:14:43 +03:00
|
|
|
/**
|
|
|
|
* Check /etc/os-release as defined by systemd.
|
|
|
|
* @see http://0pointer.de/blog/projects/os-release.html
|
|
|
|
* @see http://www.freedesktop.org/software/systemd/man/os-release.html
|
|
|
|
* @return the PRETTY_NAME from the os-release file or an empty string.
|
|
|
|
*/
|
|
|
|
static std::string checkOsRelease()
|
|
|
|
{
|
|
|
|
std::ifstream os_release_stream("/etc/os-release");
|
|
|
|
std::string os_release((std::istreambuf_iterator<char>(os_release_stream)), std::istreambuf_iterator<char>());
|
|
|
|
|
|
|
|
boost::smatch results;
|
|
|
|
boost::regex pretty_name("^PRETTY_NAME=\"([^\"]+)\"");
|
|
|
|
if (boost::regex_search(os_release, results, pretty_name)) {
|
|
|
|
return results[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string checkEtcIssue()
|
|
|
|
{
|
|
|
|
std::ifstream issue_stream("/etc/issue");
|
|
|
|
std::string issue((std::istreambuf_iterator<char>(issue_stream)), std::istreambuf_iterator<char>());
|
|
|
|
|
|
|
|
boost::regex nl("\n.*$");
|
|
|
|
issue = boost::regex_replace(issue, nl, "");
|
|
|
|
boost::regex esc("\\\\.");
|
|
|
|
issue = boost::regex_replace(issue, esc, "");
|
|
|
|
boost::algorithm::trim(issue);
|
|
|
|
|
|
|
|
return issue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string detectDistribution()
|
|
|
|
{
|
|
|
|
std::string osrelease = checkOsRelease();
|
|
|
|
if (!osrelease.empty()) {
|
|
|
|
return osrelease;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string etcissue = checkEtcIssue();
|
|
|
|
if (!etcissue.empty()) {
|
|
|
|
return etcissue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PlatformUtils::sysinfo()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
struct utsname osinfo;
|
|
|
|
if (uname(&osinfo) == 0) {
|
|
|
|
result += osinfo.sysname;
|
|
|
|
result += " ";
|
|
|
|
result += osinfo.release;
|
|
|
|
result += " ";
|
|
|
|
result += osinfo.version;
|
|
|
|
result += " ";
|
|
|
|
result += osinfo.machine;
|
|
|
|
} else {
|
|
|
|
result += "Unknown Linux";
|
|
|
|
}
|
|
|
|
|
|
|
|
long numcpu = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (numcpu > 0) {
|
|
|
|
result += " ";
|
|
|
|
result += boost::lexical_cast<std::string>(numcpu);
|
|
|
|
result += " CPU";
|
|
|
|
if (numcpu > 1) {
|
|
|
|
result += "s";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long pages = sysconf(_SC_PHYS_PAGES);
|
|
|
|
long pagesize = sysconf(_SC_PAGE_SIZE);
|
|
|
|
if ((pages > 0) && (pagesize > 0)) {
|
|
|
|
result += " ";
|
|
|
|
result += PlatformUtils::toMemorySizeString(pages * pagesize, 2);
|
|
|
|
result += " RAM";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string distribution = detectDistribution();
|
|
|
|
if (!distribution.empty()) {
|
|
|
|
result += " ";
|
|
|
|
result += distribution;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-05-17 09:02:44 +04:00
|
|
|
void PlatformUtils::ensureStdIO(void) {}
|
|
|
|
|