Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filesystem::generic_filepath. #2819

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/include/OpenImageIO/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ OIIO_API std::string parent_path (const std::string &filepath) noexcept;
OIIO_API std::string replace_extension (const std::string &filepath,
const std::string &new_extension) noexcept;

/// Return the filepath in generic format, not any OS-specific conventions.
OIIO_API std::string generic_filepath (string_view filepath) noexcept;

/// Turn a searchpath (multiple directory paths separated by ':' or ';')
/// into a vector<string> containing each individual directory. If
/// validonly is true, only existing and readable directories will end
Expand Down
36 changes: 32 additions & 4 deletions src/libutil/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,23 @@ Filesystem::filename(const std::string& filepath) noexcept
{
// To simplify dealing with platform-specific separators and whatnot,
// just use the Boost routines:
return pathstr(u8path(filepath).filename());
try {
return pathstr(u8path(filepath).filename());
} catch (...) {
return filepath;
}
}



std::string
Filesystem::extension(const std::string& filepath, bool include_dot) noexcept
{
std::string s = pathstr(u8path(filepath).extension());
std::string s;
try {
s = pathstr(u8path(filepath).extension());
} catch (...) {
}
if (!include_dot && !s.empty() && s[0] == '.')
s.erase(0, 1); // erase the first character
return s;
Expand All @@ -118,7 +126,11 @@ Filesystem::extension(const std::string& filepath, bool include_dot) noexcept
std::string
Filesystem::parent_path(const std::string& filepath) noexcept
{
return pathstr(u8path(filepath).parent_path());
try {
return pathstr(u8path(filepath).parent_path());
} catch (...) {
return filepath;
}
}


Expand All @@ -127,7 +139,23 @@ std::string
Filesystem::replace_extension(const std::string& filepath,
const std::string& new_extension) noexcept
{
return pathstr(u8path(filepath).replace_extension(new_extension));
try {
return pathstr(u8path(filepath).replace_extension(new_extension));
} catch (...) {
return filepath;
}
}



std::string
Filesystem::generic_filepath(string_view filepath) noexcept
{
try {
return pathstr(u8path(filepath).generic_string());
} catch (...) {
return filepath;
}
}


Expand Down
8 changes: 8 additions & 0 deletions src/libutil/filesystem_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ test_filename_decomposition()
std::cout << "Testing replace_extension\n";
OIIO_CHECK_EQUAL(Filesystem::replace_extension(test, "foo"),
"/directoryA/directory/filename.foo");

std::cout << "Testing generic_string\n";
#if _WIN32
OIIO_CHECK_EQUAL(Filesystem::generic_filepath("\\x\\y"), "/x/y");
OIIO_CHECK_EQUAL(Filesystem::generic_filepath("c:\\x\\y"), "/c/x/y");
#endif
}


Expand Down Expand Up @@ -287,6 +293,8 @@ test_scan_file_seq_with_views(const char* pattern, const char** views_,
Filesystem::scan_for_matching_filenames(normalized_pattern, views,
frame_numbers, frame_views,
frame_names);
for (auto& f : frame_names)
f = Filesystem::generic_filepath(f);
std::string joined = Strutil::join(frame_names, " ");
std::cout << " " << pattern;
std::cout << " -> " << joined << "\n";
Expand Down