Files
OpenRCT2-Web/src/openrct2/platform/Platform.Posix.cpp
T

311 lines
8.0 KiB
C++
Raw Normal View History

2017-12-01 00:54:03 +00:00
/*****************************************************************************
* Copyright (c) 2014-2021 OpenRCT2 developers
2018-06-15 14:07:34 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2017-12-01 00:54:03 +00:00
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
2022-01-10 13:49:44 +01:00
# include "platform.h"
2021-03-25 23:51:33 -03:00
# include "../core/Memory.hpp"
2021-03-26 00:07:53 -03:00
# include "../core/Path.hpp"
2018-07-21 16:17:06 +02:00
# include "../core/String.hpp"
2022-01-10 12:45:36 +01:00
# include "../localisation/Date.h"
2018-07-21 16:17:06 +02:00
# include "Platform2.h"
2017-12-01 00:54:03 +00:00
2022-01-08 15:42:27 +01:00
# include <cerrno>
# include <clocale>
2018-07-21 16:17:06 +02:00
# include <cstdlib>
# include <cstring>
# include <ctime>
2021-03-26 00:07:53 -03:00
# include <dirent.h>
# include <fnmatch.h>
2022-01-10 12:45:36 +01:00
# include <locale>
2018-07-21 16:17:06 +02:00
# include <pwd.h>
2020-11-23 00:23:07 -03:00
# include <sys/stat.h>
2018-06-22 23:04:38 +02:00
2022-01-08 13:57:29 +01:00
# define FILE_BUFFER_SIZE 4096
2017-12-01 00:54:03 +00:00
namespace Platform
{
uint32_t GetTicks()
2017-12-01 00:54:03 +00:00
{
return platform_get_ticks();
}
2022-01-08 13:57:29 +01:00
std::string GetEnvironmentVariable(std::string_view name)
2017-12-01 00:54:03 +00:00
{
2022-01-08 13:57:29 +01:00
return String::ToStd(getenv(std::string(name).c_str()));
2017-12-01 00:54:03 +00:00
}
2018-06-22 23:04:38 +02:00
std::string GetEnvironmentPath(const char* name)
2017-12-01 00:54:03 +00:00
{
auto value = getenv(name);
if (value == nullptr)
{
return std::string();
}
2021-09-15 22:22:15 +02:00
auto colon = std::strchr(value, ':');
if (colon == nullptr)
2017-12-01 00:54:03 +00:00
{
2021-09-15 22:22:15 +02:00
return std::string(value);
2017-12-01 00:54:03 +00:00
}
2021-09-15 22:22:15 +02:00
return std::string(value, colon);
2017-12-01 00:54:03 +00:00
}
std::string GetHomePath()
{
std::string path;
auto pw = getpwuid(getuid());
if (pw != nullptr)
{
path = pw->pw_dir;
}
else
{
path = GetEnvironmentVariable("HOME");
}
if (path.empty())
{
path = "/";
}
return path;
}
std::string FormatShortDate(std::time_t timestamp)
{
setlocale(LC_TIME, "");
char date[20];
std::strftime(date, sizeof(date), "%x", std::localtime(&timestamp));
return std::string(date);
}
std::string FormatTime(std::time_t timestamp)
{
setlocale(LC_TIME, "");
char time[20];
std::strftime(time, sizeof(time), "%X", std::localtime(&timestamp));
return std::string(time);
}
2018-03-12 18:00:35 +00:00
bool IsColourTerminalSupported()
{
static bool hasChecked = false;
static bool isSupported = false;
if (!hasChecked)
{
auto term = GetEnvironmentVariable("TERM");
2018-06-22 23:04:38 +02:00
isSupported = term != "cons25" && term != "dumb" && term != "emacs";
2018-03-12 18:00:35 +00:00
hasChecked = true;
}
return isSupported;
}
bool IsRunningInWine()
{
return false;
}
2020-10-22 12:17:28 -07:00
2022-01-08 13:57:29 +01:00
bool FindApp(std::string_view app, std::string* output)
2020-10-22 12:17:28 -07:00
{
2022-01-08 13:57:29 +01:00
return Execute(String::StdFormat("which %s 2> /dev/null", std::string(app).c_str()), output) == 0;
2020-10-22 12:17:28 -07:00
}
2022-01-08 13:57:29 +01:00
int32_t Execute(std::string_view command, std::string* output)
2020-10-22 12:17:28 -07:00
{
# ifndef __EMSCRIPTEN__
2022-01-08 13:57:29 +01:00
log_verbose("executing \"%s\"...", std::string(command).c_str());
FILE* fpipe = popen(std::string(command).c_str(), "r");
2020-10-22 12:17:28 -07:00
if (fpipe == nullptr)
{
return -1;
}
if (output != nullptr)
{
// Read output into buffer
std::vector<char> outputBuffer;
char buffer[1024];
size_t readBytes;
while ((readBytes = fread(buffer, 1, sizeof(buffer), fpipe)) > 0)
{
outputBuffer.insert(outputBuffer.begin(), buffer, buffer + readBytes);
}
// Trim line breaks
size_t outputLength = outputBuffer.size();
for (size_t i = outputLength - 1; i != SIZE_MAX; i--)
{
if (outputBuffer[i] == '\n')
{
outputLength = i;
}
else
{
break;
}
}
// Convert to string
*output = std::string(outputBuffer.data(), outputLength);
}
else
{
fflush(fpipe);
}
// Return exit code
return pclose(fpipe);
# else
log_warning("Emscripten cannot execute processes. The commandline was '%s'.", command.c_str());
return -1;
# endif // __EMSCRIPTEN__
}
2020-11-23 00:23:07 -03:00
2022-01-08 13:57:29 +01:00
uint64_t GetLastModified(std::string_view path)
2020-11-23 00:23:07 -03:00
{
uint64_t lastModified = 0;
struct stat statInfo
{
};
2022-01-08 13:57:29 +01:00
if (stat(std::string(path).c_str(), &statInfo) == 0)
2020-11-23 00:23:07 -03:00
{
lastModified = statInfo.st_mtime;
}
return lastModified;
}
2021-03-25 23:45:01 -03:00
uint64_t GetFileSize(std::string_view path)
{
uint64_t size = 0;
struct stat statInfo
{
};
if (stat(std::string(path).c_str(), &statInfo) == 0)
{
size = statInfo.st_size;
}
return size;
}
2021-03-25 23:55:45 -03:00
bool ShouldIgnoreCase()
{
return false;
}
2021-03-25 23:45:01 -03:00
bool IsPathSeparator(char c)
{
return c == '/';
}
2021-03-25 23:51:33 -03:00
2022-01-08 13:57:29 +01:00
std::string ResolveCasing(std::string_view path, bool fileExists)
2021-03-26 00:07:53 -03:00
{
std::string result;
if (fileExists)
{
// Windows is case insensitive so it will exist and that is all that matters
// for now. We can properly resolve the casing if we ever need to.
result = path;
}
else
{
std::string fileName = Path::GetFileName(path);
std::string directory = Path::GetDirectory(path);
struct dirent** files;
auto count = scandir(directory.c_str(), &files, nullptr, alphasort);
if (count != -1)
{
// Find a file which matches by name (case insensitive)
for (int32_t i = 0; i < count; i++)
{
if (String::Equals(files[i]->d_name, fileName.c_str(), true))
{
result = Path::Combine(directory, std::string(files[i]->d_name));
break;
}
}
// Free memory
for (int32_t i = 0; i < count; i++)
{
free(files[i]);
}
free(files);
}
}
return result;
}
2021-03-26 00:24:55 -03:00
bool RequireNewWindow(bool openGL)
{
return true;
}
2022-01-05 14:42:46 +01:00
std::string GetUsername()
{
std::string result;
auto pw = getpwuid(getuid());
if (pw != nullptr)
{
result = std::string(pw->pw_name);
}
return result;
}
2022-01-10 12:45:36 +01:00
uint8_t GetLocaleDateFormat()
{
const std::time_base::dateorder dateorder = std::use_facet<std::time_get<char>>(std::locale()).date_order();
switch (dateorder)
{
case std::time_base::mdy:
return DATE_FORMAT_MONTH_DAY_YEAR;
case std::time_base::ymd:
return DATE_FORMAT_YEAR_MONTH_DAY;
case std::time_base::ydm:
return DATE_FORMAT_YEAR_DAY_MONTH;
default:
return DATE_FORMAT_DAY_MONTH_YEAR;
}
}
TemperatureUnit GetLocaleTemperatureFormat()
{
// LC_MEASUREMENT is GNU specific.
# ifdef LC_MEASUREMENT
const char* langstring = setlocale(LC_MEASUREMENT, "");
# else
const char* langstring = setlocale(LC_ALL, "");
# endif
if (langstring != nullptr)
{
if (!fnmatch("*_US*", langstring, 0) || !fnmatch("*_BS*", langstring, 0) || !fnmatch("*_BZ*", langstring, 0)
|| !fnmatch("*_PW*", langstring, 0))
{
return TemperatureUnit::Fahrenheit;
}
}
return TemperatureUnit::Celsius;
}
2022-01-10 13:02:11 +01:00
bool ProcessIsElevated()
{
# ifndef __EMSCRIPTEN__
return (geteuid() == 0);
# else
return false;
# endif // __EMSCRIPTEN__
}
2018-06-22 23:04:38 +02:00
} // namespace Platform
2017-12-01 00:54:03 +00:00
#endif