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
149 lines (124 loc) · 4.42 KB
/
Copy pathpath.cpp
File metadata and controls
149 lines (124 loc) · 4.42 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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/fs.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/range/distance.hpp>
namespace {
static const char *tokens[] = {
"?audio",
"?data",
"?dictionary",
"?local",
"?script",
"?state",
"?temp",
"?user",
"?video"
};
int find_token(const char *str, size_t len) {
if (len < 5 || str[0] != '?') return -1;
int idx;
switch (str[2] + str[4]) {
case 'u' + 'i': idx = 0; break;
case 'a' + 'a': idx = 1; break;
case 'i' + 't': idx = 2; break;
case 'o' + 'a': idx = 3; break;
case 'c' + 'i': idx = 4; break;
case 't' + 't': idx = 5; break;
case 'e' + 'p': idx = 6; break;
case 's' + 'r': idx = 7; break;
case 'i' + 'e': idx = 8; break;
default: return -1;
}
return strncmp(str, tokens[idx], strlen(tokens[idx])) == 0 ? idx : -1;
}
}
namespace agi {
Path::Path() {
static_assert(sizeof(paths) / sizeof(paths[0]) == sizeof(tokens) / sizeof(tokens[0]),
"Token and path arrays need to be the same size");
FillPlatformSpecificPaths();
}
fs::path Path::Decode(std::string const& path) const {
int idx = find_token(path.c_str(), path.size());
if (idx == -1 || paths[idx].empty())
return fs::path(path).make_preferred();
return (paths[idx]/path.substr(strlen(tokens[idx]))).make_preferred();
}
fs::path Path::MakeRelative(fs::path const& path, std::string const& token) const {
int idx = find_token(token.c_str(), token.size());
if (idx == -1) throw agi::InternalError("Bad token: " + token);
return MakeRelative(path, paths[idx]);
}
fs::path Path::MakeRelative(fs::path const& path, fs::path const& base) const {
if (path.empty() || base.empty()) return path;
const auto str = path.string();
if (boost::starts_with(str, "?dummy") || boost::starts_with(str, "dummy-audio:"))
return path;
// Paths on different volumes can't be made relative to each other
if (path.has_root_name() && path.root_name() != base.root_name())
return path.string();
auto path_it = path.begin();
auto ref_it = base.begin();
for (; path_it != path.end() && ref_it != base.end() && *path_it == *ref_it; ++path_it, ++ref_it) ;
agi::fs::path result;
for (; ref_it != base.end(); ++ref_it)
result /= "..";
for (; path_it != path.end(); ++path_it)
result /= *path_it;
return result;
}
fs::path Path::MakeAbsolute(fs::path path, std::string const& token) const {
if (path.empty()) return path;
int idx = find_token(token.c_str(), token.size());
if (idx == -1) throw agi::InternalError("Bad token: " + token);
path.make_preferred();
const auto str = path.string();
if (boost::starts_with(str, "?dummy") || boost::starts_with(str, "dummy-audio:"))
return path;
return (paths[idx].empty() || path.is_absolute()) ? path : paths[idx]/path;
}
std::string Path::Encode(fs::path const& path) const {
// Find the shortest encoding of path made relative to each token
std::string shortest = path.string();
size_t length = boost::distance(path);
for (size_t i = 0; i < paths.size(); ++i) {
if (paths[i].empty()) continue;
const auto p = MakeRelative(path, tokens[i]);
const size_t d = boost::distance(p);
if (d < length) {
length = d;
shortest = (tokens[i]/p).string();
}
}
return shortest;
}
void Path::SetToken(const char *token_name, fs::path const& token_value) {
int idx = find_token(token_name, strlen(token_name));
if (idx == -1) throw agi::InternalError("Bad token: " + std::string(token_name));
if (token_value.empty())
paths[idx] = token_value;
else if (!token_value.is_absolute())
paths[idx].clear();
else {
paths[idx] = token_value;
paths[idx].make_preferred();
if (fs::FileExists(paths[idx]))
paths[idx] = paths[idx].parent_path();
}
}
}