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>
51 #if defined(__APPLE__)
52 # include "../macosx/macos.h"
55 #include "../../safeguards.h"
57 bool FiosIsRoot(
const std::string &path)
59 return path == PATHSEP;
67 std::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;
81 bool 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"
101 static const char *GetLocalCode()
103 #if defined(__APPLE__)
108 if (locale !=
nullptr) locale = strchr(locale,
'.');
110 return (locale ==
nullptr) ?
"" : locale + 1;
118 static std::string convert_tofrom_fs(iconv_t convd,
const std::string &name)
123 #ifdef HAVE_NON_CONST_ICONV
124 char *inbuf =
const_cast<char*
>(name.data());
126 const char *inbuf = name.data();
130 size_t inlen = name.size();
131 std::string buf(inlen * 4,
'\0');
133 size_t outlen = buf.size();
134 char *outbuf = buf.data();
135 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
136 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == SIZE_MAX) {
137 Debug(misc, 0,
"[iconv] error converting '{}'. Errno {}", name, errno);
141 buf.resize(outbuf - buf.data());
150 std::string
OTTD2FS(
const std::string &name)
152 static iconv_t convd = (iconv_t)(-1);
153 if (convd == (iconv_t)(-1)) {
154 const char *env = GetLocalCode();
155 convd = iconv_open(env, INTERNALCODE);
156 if (convd == (iconv_t)(-1)) {
157 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env);
162 return convert_tofrom_fs(convd, name);
170 std::string
FS2OTTD(
const std::string &name)
172 static iconv_t convd = (iconv_t)(-1);
173 if (convd == (iconv_t)(-1)) {
174 const char *env = GetLocalCode();
175 convd = iconv_open(INTERNALCODE, env);
176 if (convd == (iconv_t)(-1)) {
177 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE);
182 return convert_tofrom_fs(convd, name);
187 void ShowInfoI(
const std::string &str)
189 fmt::print(stderr,
"{}\n", str);
192 #if !defined(__APPLE__)
193 void ShowOSErrorBox(
const char *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__)
224 void OSOpenBrowser(
const std::string &url)
227 EM_ASM({
if (window[
"openttd_open_url"]) window.openttd_open_url($0, $1) }, url.c_str(), url.size());
229 #elif !defined( __APPLE__)
230 void 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);
245 void SetCurrentThreadName([[maybe_unused]]
const char *threadName)
247 #if defined(__GLIBC__)
248 if (threadName) pthread_setname_np(pthread_self(), threadName);
250 #if defined(__APPLE__)
251 MacOSSetThreadName(threadName);
List of file information.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
const char * GetCurrentLocale(const char *)
Determine the current user's locale.
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.