diff --git a/libaegisub/meson.build b/libaegisub/meson.build index 7689d31aeb..3b0e4964fc 100644 --- a/libaegisub/meson.build +++ b/libaegisub/meson.build @@ -55,6 +55,7 @@ libaegisub_src = [ if host_machine.system() == 'darwin' libaegisub_src += [ 'osx/dispatch.mm', + 'osx/path.mm', 'osx/spellchecker.mm', 'osx/util.mm', ] @@ -76,9 +77,12 @@ else 'unix/access.cpp', 'unix/fs.cpp', 'unix/log.cpp', - 'unix/path.cpp', 'unix/util.cpp', ] + + if host_machine.system() != 'darwin' + libaegisub_src += 'unix/path.cpp' + endif endif libaegisub_cpp_pch = ['include/lagi_pre.h'] diff --git a/libaegisub/osx/path.mm b/libaegisub/osx/path.mm new file mode 100644 index 0000000000..e8c2509e50 --- /dev/null +++ b/libaegisub/osx/path.mm @@ -0,0 +1,31 @@ +// Copyright (c) 2013, Thomas Goyne +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// +// Aegisub Project http://www.aegisub.org/ + +#include +#include + +namespace sfs = std::filesystem; + +namespace agi { +void Path::FillPlatformSpecificPaths() { + agi::fs::path user_dir = agi::fs::path(sfs::path(agi::util::GetApplicationSupportDirectory()))/"Aegisub"; + SetToken("?user", user_dir); + SetToken("?local", user_dir); + SetToken("?data", agi::util::GetBundleSharedSupportDirectory()); + SetToken("?dictionary", Decode("?data/dictionaries")); + SetToken("?temp", agi::fs::path(sfs::temp_directory_path())); +} +} diff --git a/libaegisub/unix/path.cpp b/libaegisub/unix/path.cpp index 5dada5b06b..19c078ab16 100644 --- a/libaegisub/unix/path.cpp +++ b/libaegisub/unix/path.cpp @@ -15,82 +15,59 @@ // Aegisub Project http://www.aegisub.org/ #include - #include -#include #include - -#ifndef __APPLE__ -#include #include -#include -#endif + +namespace sfs = std::filesystem; namespace { -#ifndef __APPLE__ -std::string home_dir() { +agi::fs::path home_dir() { const char *env = getenv("HOME"); - if (env) return env; + if (env) return agi::fs::path(sfs::path(env)); if ((env = getenv("USER")) || (env = getenv("LOGNAME"))) { if (passwd *user_info = getpwnam(env)) - return user_info->pw_dir; + return agi::fs::path(sfs::path(user_info->pw_dir)); } throw agi::EnvironmentError("Could not get home directory. Make sure HOME is set."); } #ifdef APPIMAGE_BUILD -std::string exe_dir() { - char *exe, *dir; - std::string data = ""; - -#ifdef __FreeBSD__ - exe = realpath("/proc/self/file", NULL); -#else - exe = realpath("/proc/self/exe", NULL); -#endif - +agi::fs::path data_dir() { + char *exe = realpath("/proc/self/exe", NULL); if (!exe) return ""; - if ((dir = dirname(exe)) && strlen(dir) > 0) { - data = dir; - } - + sfs::path p = sfs::path(exe).parent_path(); free(exe); - return data; + if (p.filename() == "bin") { + // assume unix prefix layout + return agi::fs::path(p.parent_path()/"share"); + } + + return agi::fs::path(p); } -#endif /* APPIMAGE_BUILD */ -#endif /* !__APPLE__ */ +#endif } namespace agi { void Path::FillPlatformSpecificPaths() { -#ifndef __APPLE__ - agi::fs::path home = home_dir(); - SetToken("?user", home/".aegisub"); - SetToken("?local", home/".aegisub"); + agi::fs::path dotdir = home_dir()/".aegisub"; + SetToken("?user", dotdir); + SetToken("?local", dotdir); #ifdef APPIMAGE_BUILD - agi::fs::path data = exe_dir(); - if (data == "") data = home/".aegisub"; - SetToken("?data", data); + agi::fs::path data = data_dir(); + SetToken("?data", (data == "") ? dotdir : data); SetToken("?dictionary", Decode("?data/dictionaries")); #else SetToken("?data", P_DATA); SetToken("?dictionary", "/usr/share/hunspell"); #endif -#else - agi::fs::path app_support = agi::util::GetApplicationSupportDirectory(); - SetToken("?user", app_support/"Aegisub"); - SetToken("?local", app_support/"Aegisub"); - SetToken("?data", agi::util::GetBundleSharedSupportDirectory()); - SetToken("?dictionary", Decode("?data/dictionaries")); -#endif - SetToken("?temp", agi::fs::path(std::filesystem::temp_directory_path())); + SetToken("?temp", agi::fs::path(sfs::temp_directory_path())); } - }