-
Notifications
You must be signed in to change notification settings - Fork 78
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
✨Add list_files function #615 #615
base: develop-pros-4
Are you sure you want to change the base?
Changes from 1 commit
7fa0947
40aa222
90e0bba
a8ed32b
4983f3b
c80a4aa
ba4a55b
da47f28
3d41ca6
4247fb1
8df65db
f4aa620
db8c2ee
a4bc889
490a3ca
b7b773e
1c42992
fc2a72f
b36e01c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,19 @@ std::vector<std::string> list_files(const char* path) { | |
return files; | ||
} | ||
} | ||
|
||
// Check path user passed in | ||
std::string_view path_sv(path); | ||
size_t found = path_sv.find("usd"); | ||
if (found == 0 || found == 1) { | ||
// Deal with when user prepends path with usd | ||
// as either "usd/..." or "/usd/..." | ||
path_sv.remove_prefix(3); | ||
} | ||
|
||
// set path to path_sv.data() | ||
path = path_sv.data(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// Call the C function | ||
int32_t success = usd_list_files_raw(path, buffer, buffer_size); | ||
// Check if call successful, if error return vector containing error state | ||
|
@@ -76,7 +89,7 @@ std::vector<std::string> list_files(const char* path) { | |
file_name = std::string_view(str.data() + index, delimiter_pos - index); | ||
// This is point where content of the std::string_view file name is copied to its | ||
// own std::string and added to the files vector | ||
files.emplace_back(file_name); | ||
files.emplace_back("/usd" + std::string(path) + "/" + std::string(file_name)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't do it like this, this creates 7 different files.emplace_back(std::format("/{}/{}", path, file_name)); if not i would recommend something more like const size_t path_size = 1 + usd_prefix.length() + path.length() + 1 + file_name.length(); // +1's for the slashes
std::string buf;
buf.reserve(path_size); // Reserve enough space in the internal array that we can snprintf right into it
// The %.*s format specifier allows passing the size of a format arg before the value of the arg, so it lets
// us use the string_view into `str` with the C-style snprintf function even though it does not have the
// null terminator that would usually be required to know when a string ends.
// We also add 1 to path_size for the `n` argument for the null terminator, the space for which is
// implicitly included by the std::string::reserve call
std::snprintf(buf.data(), path_size + 1, "/%s/%.*s", path, static_cast<int>(file_name.length()), file_name.data());
files.push_back(std::move(buf)); This retains the property of the old version that it only had to allocate the string once. (EDIT: removed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sadly it does not :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Gracelu128 Ty but
You could also just subtract 1 from the param to the |
||
// Increment index to start substr from | ||
index = delimiter_pos + 1; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does hardcoding the
3
here deal with both cases? Wouldn't it be3 + found
?Also to avoid the magic-number-ness of this 3, I'd probably write this as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also what happens here if they don't include the usd prefix? We just make it work the same as if they did? May be weird, since we also always return paths prefixed with
/usd/
.My preference here would be to return a "path not found" error, as just assuming they wanted an implicit
/usd/
makes doing anything with a VFS outside of/usd/
in the future into a backwards compatibility break. It also improves consistency, since nothing else in PROS would accept that path.