forked from TypesettingTools/Aegisub
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathpath.cpp
More file actions
117 lines (99 loc) · 3.4 KB
/
Copy pathpath.cpp
File metadata and controls
117 lines (99 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
//
// 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 <libaegisub/path.h>
#include <libaegisub/exception.h>
#include <libaegisub/util_osx.h>
#include <boost/filesystem/operations.hpp>
#include <pwd.h>
#ifndef __APPLE__
#include <fstream>
#include <stdlib.h>
#include <libgen.h>
#endif
namespace {
#ifndef __APPLE__
std::string home_dir() {
const char *env = getenv("HOME");
if (env) return env;
if ((env = getenv("USER")) || (env = getenv("LOGNAME"))) {
if (passwd *user_info = getpwnam(env))
return user_info->pw_dir;
}
throw agi::EnvironmentError("Could not get home directory. Make sure HOME is set.");
}
std::string xdg_dir(const std::string &environment_variable,
const std::string &fallback_directory)
{
const char *env = getenv(environment_variable.c_str());
if (env && *env) return env;
return fallback_directory;
}
#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
if (!exe) return "";
if ((dir = dirname(exe)) && strlen(dir) > 0) {
data = dir;
}
free(exe);
return data;
}
#endif /* APPIMAGE_BUILD */
#endif /* !__APPLE__ */
}
namespace agi {
void Path::FillPlatformSpecificPaths() {
#ifndef __APPLE__
agi::fs::path home = home_dir();
agi::fs::path prev_dir = home/".aegisub";
if (!boost::filesystem::exists(prev_dir))
{
agi::fs::path xdg_config_home = xdg_dir("XDG_CONFIG_HOME", (home/".config").string());
agi::fs::path xdg_cache_home = xdg_dir("XDG_CACHE_HOME", (home/".cache").string());
agi::fs::path xdg_state_home = xdg_dir("XDG_STATE_HOME", (home/".local/state").string());
SetToken("?user", xdg_config_home/"Aegisub");
SetToken("?local", xdg_cache_home/"Aegisub");
SetToken("?state", xdg_state_home/"Aegisub");
} else {
SetToken("?user", prev_dir);
SetToken("?local", prev_dir);
SetToken("?state", prev_dir);
}
#ifdef APPIMAGE_BUILD
agi::fs::path exe = exe_dir();
agi::fs::path data_from_bin = agi::fs::path(P_DATA).lexically_relative(P_BIN);
SetToken("?data", (exe != "" ? exe/data_from_bin : home/".aegisub").make_preferred());
#else
SetToken("?data", P_DATA);
#endif
SetToken("?dictionary", "/usr/share/hunspell");
#else
agi::fs::path app_support = agi::util::GetApplicationSupportDirectory();
SetToken("?user", app_support/"Aegisub");
SetToken("?local", app_support/"Aegisub");
SetToken("?state", app_support/"Aegisub");
SetToken("?data", agi::util::GetBundleSharedSupportDirectory());
SetToken("?dictionary", Decode("?data/dictionaries"));
#endif
SetToken("?temp", boost::filesystem::temp_directory_path());
}
}