OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::resource_path Namespace Reference

Functions

std::string ascii_lower_copy (const std::string &value)
boost::filesystem::path portable_path_copy (const boost::filesystem::path &value)
int hex_digit_value (char ch)
std::string percent_decode_copy (const std::string &value)
std::string strip_file_uri_prefix_copy (const std::string &value)
bool file_uri_has_remote_authority (const std::string &value)
bool looks_like_windows_absolute_path (const boost::filesystem::path &path)
boost::filesystem::path filename_from_portable_path (const boost::filesystem::path &value)
boost::filesystem::path find_child_case_insensitive (const boost::filesystem::path &directory, const boost::filesystem::path &requested_name, const char *context)
boost::filesystem::path resolve_existing_path_case_insensitive (const boost::filesystem::path &requested_path, const char *context="resource_path")
boost::filesystem::path resolve_existing_relative_path_case_insensitive (const boost::filesystem::path &base_dir, const boost::filesystem::path &resource_path, const char *context="resource_path")
boost::filesystem::path resolve_external_resource_path (const boost::filesystem::path &base_dir, const std::string &raw_path, const char *context="resource_path", bool allow_basename_fallback=true)

Function Documentation

◆ ascii_lower_copy()

std::string Slic3r::resource_path::ascii_lower_copy ( const std::string & value)
inline
20{
21 std::string lowered;
22 lowered.reserve(value.size());
23 for (unsigned char ch : value)
24 lowered.push_back(static_cast<char>(std::tolower(ch)));
25 return lowered;
26}

Referenced by file_uri_has_remote_authority(), find_child_case_insensitive(), and strip_file_uri_prefix_copy().

Here is the caller graph for this function:

◆ file_uri_has_remote_authority()

bool Slic3r::resource_path::file_uri_has_remote_authority ( const std::string & value)
inline
84{
85 const std::string lower = ascii_lower_copy(value);
86 if (lower.rfind("file://", 0) != 0)
87 return false;
88
89 const std::string path = value.substr(7);
90 if (path.empty() || path.front() == '/')
91 return false;
92
93 const std::size_t slash = path.find('/');
94 const std::string authority = path.substr(0, slash);
95 return ascii_lower_copy(authority) != "localhost";
96}
std::string ascii_lower_copy(const std::string &value)
Definition ResourcePathUtils.hpp:19

References ascii_lower_copy().

Referenced by resolve_external_resource_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ filename_from_portable_path()

boost::filesystem::path Slic3r::resource_path::filename_from_portable_path ( const boost::filesystem::path & value)
inline
108{
109 const boost::filesystem::path portable = portable_path_copy(value);
110 return portable.filename();
111}
boost::filesystem::path portable_path_copy(const boost::filesystem::path &value)
Definition ResourcePathUtils.hpp:28

References portable_path_copy().

Referenced by resolve_external_resource_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_child_case_insensitive()

boost::filesystem::path Slic3r::resource_path::find_child_case_insensitive ( const boost::filesystem::path & directory,
const boost::filesystem::path & requested_name,
const char * context )
inline
117{
118 if (!boost::filesystem::exists(directory) || !boost::filesystem::is_directory(directory))
119 return {};
120
121 const std::string requested_lower = ascii_lower_copy(requested_name.filename().string());
122 std::vector<boost::filesystem::path> matches;
123
124 boost::system::error_code ec;
125 for (boost::filesystem::directory_iterator it(directory, ec), end; !ec && it != end; it.increment(ec)) {
126 if (ascii_lower_copy(it->path().filename().string()) == requested_lower)
127 matches.push_back(it->path());
128 }
129
130 if (matches.size() == 1)
131 return matches.front();
132
133 if (matches.size() > 1) {
134 BOOST_LOG_TRIVIAL(warning) << context << ": ambiguous case-insensitive resource match for "
135 << requested_name << " in " << directory;
136 }
137
138 return {};
139}

References ascii_lower_copy().

Referenced by resolve_existing_path_case_insensitive().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hex_digit_value()

int Slic3r::resource_path::hex_digit_value ( char ch)
inline
36{
37 if (ch >= '0' && ch <= '9') return ch - '0';
38 if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
39 if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
40 return -1;
41}

Referenced by percent_decode_copy().

Here is the caller graph for this function:

◆ looks_like_windows_absolute_path()

bool Slic3r::resource_path::looks_like_windows_absolute_path ( const boost::filesystem::path & path)
inline
99{
100 const std::string portable = portable_path_copy(path).string();
101 return portable.size() >= 3
102 && std::isalpha(static_cast<unsigned char>(portable[0]))
103 && portable[1] == ':'
104 && portable[2] == '/';
105}

References portable_path_copy().

Referenced by resolve_external_resource_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ percent_decode_copy()

std::string Slic3r::resource_path::percent_decode_copy ( const std::string & value)
inline
47{
48 std::string decoded;
49 decoded.reserve(value.size());
50 for (std::size_t i = 0; i < value.size(); ++i) {
51 if (value[i] == '%' && i + 2 < value.size()) {
52 const int hi = hex_digit_value(value[i + 1]);
53 const int lo = hex_digit_value(value[i + 2]);
54 if (hi >= 0 && lo >= 0) {
55 decoded.push_back(static_cast<char>((hi << 4) | lo));
56 i += 2;
57 continue;
58 }
59 }
60 decoded.push_back(value[i]);
61 }
62 return decoded;
63}
int hex_digit_value(char ch)
Definition ResourcePathUtils.hpp:35

References hex_digit_value().

Referenced by resolve_external_resource_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ portable_path_copy()

boost::filesystem::path Slic3r::resource_path::portable_path_copy ( const boost::filesystem::path & value)
inline
29{
30 std::string portable = value.string();
31 std::replace(portable.begin(), portable.end(), '\\', '/');
32 return boost::filesystem::path(portable);
33}

Referenced by filename_from_portable_path(), looks_like_windows_absolute_path(), resolve_existing_path_case_insensitive(), and resolve_external_resource_path().

Here is the caller graph for this function:

◆ resolve_existing_path_case_insensitive()

boost::filesystem::path Slic3r::resource_path::resolve_existing_path_case_insensitive ( const boost::filesystem::path & requested_path,
const char * context = "resource_path" )
inline
144{
145 const boost::filesystem::path normalized_path = portable_path_copy(requested_path);
146
147 if (normalized_path.empty())
148 return {};
149
150 if (boost::filesystem::exists(normalized_path))
151 return normalized_path;
152
153 boost::filesystem::path current;
154 bool initialized = false;
155
156 for (const boost::filesystem::path& part : normalized_path) {
157 if (part == normalized_path.root_name() || part == normalized_path.root_directory()) {
158 current /= part;
159 initialized = true;
160 continue;
161 }
162
163 if (!initialized) {
164 current = boost::filesystem::current_path();
165 initialized = true;
166 }
167
168 boost::filesystem::path exact = current / part;
169 if (boost::filesystem::exists(exact)) {
170 current = exact;
171 continue;
172 }
173
174 boost::filesystem::path matched = find_child_case_insensitive(current, part, context);
175 if (matched.empty())
176 return {};
177
178 BOOST_LOG_TRIVIAL(info) << context << ": resolved resource path case-insensitively from "
179 << exact << " to " << matched;
180 current = matched;
181 }
182
183 return boost::filesystem::exists(current) ? current : boost::filesystem::path();
184}
static bool initialized
Definition minilzo_extension.cpp:10
boost::filesystem::path find_child_case_insensitive(const boost::filesystem::path &directory, const boost::filesystem::path &requested_name, const char *context)
Definition ResourcePathUtils.hpp:113

References find_child_case_insensitive(), initialized, and portable_path_copy().

Referenced by resolve_existing_relative_path_case_insensitive(), and resolve_external_resource_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resolve_existing_relative_path_case_insensitive()

boost::filesystem::path Slic3r::resource_path::resolve_existing_relative_path_case_insensitive ( const boost::filesystem::path & base_dir,
const boost::filesystem::path & resource_path,
const char * context = "resource_path" )
inline
190{
191 const boost::filesystem::path requested = resource_path.is_absolute() ? resource_path : base_dir / resource_path;
192 return resolve_existing_path_case_insensitive(requested, context);
193}
Definition ResourcePathUtils.hpp:17
boost::filesystem::path resolve_existing_path_case_insensitive(const boost::filesystem::path &requested_path, const char *context="resource_path")
Definition ResourcePathUtils.hpp:141

References resolve_existing_path_case_insensitive().

Referenced by resolve_external_resource_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resolve_external_resource_path()

boost::filesystem::path Slic3r::resource_path::resolve_external_resource_path ( const boost::filesystem::path & base_dir,
const std::string & raw_path,
const char * context = "resource_path",
bool allow_basename_fallback = true )
inline
211{
212 if (raw_path.empty())
213 return {};
214
215 const bool remote_file_uri = file_uri_has_remote_authority(raw_path);
216 const std::string decoded_path = percent_decode_copy(strip_file_uri_prefix_copy(raw_path));
217 const boost::filesystem::path requested = portable_path_copy(boost::filesystem::path(decoded_path));
218
219 boost::filesystem::path resolved = (requested.is_absolute() || looks_like_windows_absolute_path(requested)) ?
220 resolve_existing_path_case_insensitive(requested, context) :
221 resolve_existing_relative_path_case_insensitive(base_dir, requested, context);
222 if (!resolved.empty())
223 return resolved;
224
225 if (!allow_basename_fallback || remote_file_uri)
226 return {};
227
228 const boost::filesystem::path basename = filename_from_portable_path(requested);
229 if (basename.empty())
230 return {};
231
232 resolved = resolve_existing_relative_path_case_insensitive(base_dir, basename, context);
233 if (!resolved.empty()) {
234 BOOST_LOG_TRIVIAL(info) << context << ": resolved resource by basename from "
235 << requested << " to " << resolved;
236 }
237 return resolved;
238}
bool file_uri_has_remote_authority(const std::string &value)
Definition ResourcePathUtils.hpp:83
bool looks_like_windows_absolute_path(const boost::filesystem::path &path)
Definition ResourcePathUtils.hpp:98
std::string percent_decode_copy(const std::string &value)
Definition ResourcePathUtils.hpp:46
boost::filesystem::path filename_from_portable_path(const boost::filesystem::path &value)
Definition ResourcePathUtils.hpp:107
boost::filesystem::path resolve_existing_relative_path_case_insensitive(const boost::filesystem::path &base_dir, const boost::filesystem::path &resource_path, const char *context="resource_path")
Definition ResourcePathUtils.hpp:186
std::string strip_file_uri_prefix_copy(const std::string &value)
Definition ResourcePathUtils.hpp:65

References file_uri_has_remote_authority(), filename_from_portable_path(), looks_like_windows_absolute_path(), percent_decode_copy(), portable_path_copy(), resolve_existing_path_case_insensitive(), resolve_existing_relative_path_case_insensitive(), and strip_file_uri_prefix_copy().

Referenced by Slic3r::anonymous_namespace{AssimpImport.cpp}::collect_materials().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strip_file_uri_prefix_copy()

std::string Slic3r::resource_path::strip_file_uri_prefix_copy ( const std::string & value)
inline
66{
67 const std::string lower = ascii_lower_copy(value);
68 if (lower.rfind("file://", 0) != 0)
69 return value;
70
71 std::string path = value.substr(7);
72 if (ascii_lower_copy(path).rfind("localhost/", 0) == 0)
73 path.erase(0, std::string("localhost").size());
74 else if (!path.empty() && path.front() != '/')
75 path = "//" + path;
76
77 // file:///C:/... should become C:/..., while file:///tmp/... keeps /tmp/...
78 if (path.size() >= 3 && path[0] == '/' && std::isalpha(static_cast<unsigned char>(path[1])) && path[2] == ':')
79 path.erase(path.begin());
80 return path;
81}

References ascii_lower_copy().

Referenced by resolve_external_resource_path().

Here is the call graph for this function:
Here is the caller graph for this function: