10#include "../../stdafx.h"
11#include "../../textbuf_gui.h"
12#include "../../debug.h"
13#include "../../string_func.h"
14#include "../../fios.h"
15#include "../../thread.h"
33# include <emscripten.h>
37# include <sys/mount.h>
38#elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
42#if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
47#include <sys/statvfs.h>
51#include <sys/sysctl.h>
55# include "../macosx/macos.h"
58#include "../../safeguards.h"
60bool FiosIsRoot(
const std::string &path)
62 return path == PATHSEP;
70std::optional<uint64_t> FiosGetDiskFreeSpace(
const std::string &path)
75 if (statfs(path.c_str(), &s) == 0)
return static_cast<uint64_t
>(s.f_bsize) * s.f_bavail;
76#elif defined(HAS_STATVFS)
79 if (statvfs(path.c_str(), &s) == 0)
return static_cast<uint64_t
>(s.f_frsize) * s.f_bavail;
84bool FiosIsHiddenFile(
const std::filesystem::path &path)
86 return path.filename().string().starts_with(
".");
93#define INTERNALCODE "UTF-8"
100static std::string GetLocalCode()
102#if defined(__APPLE__)
107 if (!locale.has_value())
return "";
108 auto pos = locale->find(
'.');
109 if (pos == std::string_view::npos)
return "";
110 locale.erase(0, pos + 1);
119static std::string convert_tofrom_fs(iconv_t convd, std::string_view name)
124#ifdef HAVE_NON_CONST_ICONV
125 char *inbuf =
const_cast<char *
>(name.data());
127 const char *inbuf = name.data();
131 size_t inlen = name.size();
132 std::string buf(inlen * 4,
'\0');
134 size_t outlen = buf.size();
135 char *outbuf = buf.data();
136 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
137 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == SIZE_MAX) {
138 Debug(misc, 0,
"[iconv] error converting '{}'. Errno {}", name, errno);
139 return std::string{name};
142 buf.resize(outbuf - buf.data());
149static std::optional<iconv_t> OpenIconv(std::string from, std::string to)
151 iconv_t convd = iconv_open(from.c_str(), to.c_str());
152 if (convd ==
reinterpret_cast<iconv_t
>(-1)) {
153 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", from, to);
164std::string
OTTD2FS(std::string_view name)
166 static const auto convd = OpenIconv(GetLocalCode(), INTERNALCODE);
167 if (!convd.has_value())
return std::string{name};
169 return convert_tofrom_fs(*convd, name);
177std::string
FS2OTTD(std::string_view name)
179 static const auto convd = OpenIconv(INTERNALCODE, GetLocalCode());
180 if (!convd.has_value())
return std::string{name};
182 return convert_tofrom_fs(*convd, name);
187void ShowInfoI(std::string_view str)
189 fmt::print(stderr,
"{}\n", str);
192#if !defined(__APPLE__)
193void ShowOSErrorBox(std::string_view buf,
bool)
196 if (isatty(fileno(stderr))) {
197 fmt::print(stderr,
"\033[1;31mError: {}\033[0;39m\n", buf);
199 fmt::print(stderr,
"Error: {}\n", buf);
208 if (SDL_HasClipboardText() == SDL_FALSE)
return std::nullopt;
210 char *clip = SDL_GetClipboardText();
211 if (clip !=
nullptr) {
212 std::string result = clip;
223#if defined(__EMSCRIPTEN__)
224void OSOpenBrowser(
const std::string &url)
227 EM_ASM({
if (window[
"openttd_open_url"]) window.openttd_open_url($0, $1) }, url.data(), url.size());
229#elif !defined( __APPLE__)
230void OSOpenBrowser(
const std::string &url)
232 pid_t child_pid = fork();
233 if (child_pid != 0)
return;
236 args[0] =
"xdg-open";
237 args[1] = url.c_str();
239 execvp(args[0],
const_cast<char *
const *
>(args));
240 Debug(misc, 0,
"Failed to open url: {}", url);
247#if defined(__GLIBC__)
248 pthread_setname_np(pthread_self(), thread_name.c_str());
250#if defined(__APPLE__)
251 MacOSSetThreadName(thread_name);
List of file information.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
void SetCurrentThreadName(const std::string &thread_name)
Name the thread this function is called on for the debugger.
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
std::optional< std::string > GetCurrentLocale(const char *)
Determine the current user's locale.