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 FiosIsValidFile(
const std::string &path,
const struct dirent *ent,
struct stat *sb)
83 assert(path.back() == PATHSEPCHAR);
84 if (path.size() > 2) assert(path[path.size() - 2] != PATHSEPCHAR);
85 std::string filename = fmt::format(
"{}{}", path, ent->d_name);
87 return stat(filename.c_str(), sb) == 0;
90 bool FiosIsHiddenFile(
const struct dirent *ent)
92 return ent->d_name[0] ==
'.';
98 #include "../../debug.h"
99 #include "../../string_func.h"
103 #define INTERNALCODE "UTF-8"
110 static const char *GetLocalCode()
112 #if defined(__APPLE__)
117 if (locale !=
nullptr) locale = strchr(locale,
'.');
119 return (locale ==
nullptr) ?
"" : locale + 1;
127 static std::string convert_tofrom_fs(iconv_t convd,
const std::string &name)
132 #ifdef HAVE_NON_CONST_ICONV
133 char *inbuf =
const_cast<char*
>(name.data());
135 const char *inbuf = name.data();
139 size_t inlen = name.size();
140 std::string buf(inlen * 4,
'\0');
142 size_t outlen = buf.size();
143 char *outbuf = buf.data();
144 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
145 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (
size_t)(-1)) {
146 Debug(misc, 0,
"[iconv] error converting '{}'. Errno {}", name, errno);
150 buf.resize(outbuf - buf.data());
159 std::string
OTTD2FS(
const std::string &name)
161 static iconv_t convd = (iconv_t)(-1);
162 if (convd == (iconv_t)(-1)) {
163 const char *env = GetLocalCode();
164 convd = iconv_open(env, INTERNALCODE);
165 if (convd == (iconv_t)(-1)) {
166 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env);
171 return convert_tofrom_fs(convd, name);
179 std::string
FS2OTTD(
const std::string &name)
181 static iconv_t convd = (iconv_t)(-1);
182 if (convd == (iconv_t)(-1)) {
183 const char *env = GetLocalCode();
184 convd = iconv_open(INTERNALCODE, env);
185 if (convd == (iconv_t)(-1)) {
186 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE);
191 return convert_tofrom_fs(convd, name);
196 void ShowInfoI(
const std::string &str)
198 fmt::print(stderr,
"{}\n", str);
201 #if !defined(__APPLE__)
202 void ShowOSErrorBox(
const char *buf,
bool)
205 if (isatty(fileno(stderr))) {
206 fmt::print(stderr,
"\033[1;31mError: {}\033[0;39m\n", buf);
208 fmt::print(stderr,
"Error: {}\n", buf);
217 if (SDL_HasClipboardText() == SDL_FALSE)
return std::nullopt;
219 char *clip = SDL_GetClipboardText();
220 if (clip !=
nullptr) {
221 std::string result = clip;
232 #if defined(__EMSCRIPTEN__)
233 void OSOpenBrowser(
const std::string &url)
236 EM_ASM({
if (window[
"openttd_open_url"]) window.openttd_open_url($0, $1) }, url.c_str(), url.size());
238 #elif !defined( __APPLE__)
239 void OSOpenBrowser(
const std::string &url)
241 pid_t child_pid = fork();
242 if (child_pid != 0)
return;
245 args[0] =
"xdg-open";
246 args[1] = url.c_str();
248 execvp(args[0],
const_cast<char *
const *
>(args));
249 Debug(misc, 0,
"Failed to open url: {}", url);
254 void SetCurrentThreadName([[maybe_unused]]
const char *threadName)
256 #if defined(__GLIBC__)
257 if (threadName) pthread_setname_np(pthread_self(), threadName);
259 #if defined(__APPLE__)
260 MacOSSetThreadName(threadName);