PLaSK library
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#include "system.hpp"
15#include <cstdlib>
16
17#if defined(_MSC_VER) || defined(__MINGW32__)
18# define PLASK_SYSTEM_WINDOWS
20//# include <windows.h>
21//# define BOOST_USE_WINDOWS_H
22#elif defined(__APPLE__)
23# define PLASK_SYSTEM_MACOS
24# include <mach-o/dyld.h>
25# include <limits.h>
26#else
27# include <string>
28# include <limits.h>
29# include <unistd.h>
30#endif
31
32namespace plask {
33
34//comes from: http://www.cplusplus.com/forum/general/11104/
35std::string exePathAndName() {
36#ifdef PLASK_SYSTEM_WINDOWS
37 char result[MAX_PATH];
38 return std::string(result, GetModuleFileName( NULL, result, MAX_PATH ));
39#elif defined(PLASK_SYSTEM_MACOS)
40 char result[PATH_MAX];
41 uint32_t size = sizeof(result);
42 if (_NSGetExecutablePath(result, &size) == 0) {
43 return std::string(result);
44 } else {
45 return std::string();
46 }
47#else
48 char path[PATH_MAX];
49 ssize_t count = readlink( "/proc/self/exe", path, PATH_MAX );
50 return std::string(path, (count > 0) ? count : 0);
51#endif
52}
53
59static std::string dirUp(const std::string& dir) {
60 std::string::size_type last_sep = dir.find_last_of(FILE_PATH_SEPARATOR);
61 return last_sep == std::string::npos ? dir : dir.substr(0, last_sep);
62 //boost::filesystem::absolute(dir).parent_path().string()
63}
64
65std::string exePath() {
66 return dirUp(exePathAndName());
67}
68
69std::string prefixPath() {
70 static std::string prefixPath;
71 if (!prefixPath.empty()) return prefixPath;
72 if (const char* envPath = getenv("PLASK_PREFIX_PATH")) {
73 return prefixPath = envPath;
74 } else {
75 return prefixPath = dirUp(exePath());
76 }
77}
78
79std::string plaskLibPath() {
80 std::string result = prefixPath();
82 result += "lib";
84 result += "plask";
86 return result;
87}
88
89std::string plaskSolversPath(const std::string &category) {
90 std::string result = plaskLibPath();
91 result += "solvers";
93 result += category;
95 return result;
96}
97
98std::string plaskMaterialsPath() {
99 std::string result = plaskLibPath();
100 result += "materials";
102 return result;
103}
104
105} //namespace plask