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"
30# include <emscripten.h>
34# include <sys/mount.h>
35#elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
39#if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
44#include <sys/statvfs.h>
48#include <sys/sysctl.h>
52# include "../macosx/macos.h"
55#include "../../safeguards.h"
57bool FiosIsRoot(
const std::string &path)
59 return path == PATHSEP;
67std::optional<uint64_t> FiosGetDiskFreeSpace(
const std::string &path)
72 if (statfs(path.c_str(), &s) == 0)
return static_cast<uint64_t
>(s.f_bsize) * s.f_bavail;
73#elif defined(HAS_STATVFS)
76 if (statvfs(path.c_str(), &s) == 0)
return static_cast<uint64_t
>(s.f_frsize) * s.f_bavail;
81bool FiosIsHiddenFile(
const std::filesystem::path &path)
83 return path.filename().string().starts_with(
".");
89#include "../../debug.h"
90#include "../../string_func.h"
94#define INTERNALCODE "UTF-8"
101static std::string GetLocalCode()
103#if defined(__APPLE__)
108 if (!locale.has_value())
return "";
109 auto pos = locale->find(
'.');
110 if (pos == std::string_view::npos)
return "";
111 locale.erase(0, pos + 1);
120static std::string convert_tofrom_fs(iconv_t convd, std::string_view name)
125#ifdef HAVE_NON_CONST_ICONV
126 char *inbuf =
const_cast<char *
>(name.data());
128 const char *inbuf = name.data();
132 size_t inlen = name.size();
133 std::string buf(inlen * 4,
'\0');
135 size_t outlen = buf.size();
136 char *outbuf = buf.data();
137 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
138 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == SIZE_MAX) {
139 Debug(misc, 0,
"[iconv] error converting '{}'. Errno {}", name, errno);
140 return std::string{name};
143 buf.resize(outbuf - buf.data());
150static std::optional<iconv_t> OpenIconv(std::string from, std::string to)
152 iconv_t convd = iconv_open(from.c_str(), to.c_str());
153 if (convd ==
reinterpret_cast<iconv_t
>(-1)) {
154 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", from, to);
165std::string
OTTD2FS(std::string_view name)
167 static const auto convd = OpenIconv(GetLocalCode(), INTERNALCODE);
168 if (!convd.has_value())
return std::string{name};
170 return convert_tofrom_fs(*convd, name);
178std::string
FS2OTTD(std::string_view name)
180 static const auto convd = OpenIconv(INTERNALCODE, GetLocalCode());
181 if (!convd.has_value())
return std::string{name};
183 return convert_tofrom_fs(*convd, name);
188void ShowInfoI(std::string_view str)
190 fmt::print(stderr,
"{}\n", str);
193#if !defined(__APPLE__)
194void ShowOSErrorBox(std::string_view buf,
bool)
197 if (isatty(fileno(stderr))) {
198 fmt::print(stderr,
"\033[1;31mError: {}\033[0;39m\n", buf);
200 fmt::print(stderr,
"Error: {}\n", buf);
209 if (SDL_HasClipboardText() == SDL_FALSE)
return std::nullopt;
211 char *clip = SDL_GetClipboardText();
212 if (clip !=
nullptr) {
213 std::string result = clip;
224#if defined(__EMSCRIPTEN__)
225void OSOpenBrowser(
const std::string &url)
228 EM_ASM({
if (window[
"openttd_open_url"]) window.openttd_open_url($0, $1) }, url.data(), url.size());
230#elif !defined( __APPLE__)
231void OSOpenBrowser(
const std::string &url)
233 pid_t child_pid = fork();
234 if (child_pid != 0)
return;
237 args[0] =
"xdg-open";
238 args[1] = url.c_str();
240 execvp(args[0],
const_cast<char *
const *
>(args));
241 Debug(misc, 0,
"Failed to open url: {}", url);
248#if defined(__GLIBC__)
249 pthread_setname_np(pthread_self(), thread_name.c_str());
251#if defined(__APPLE__)
252 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.