-
-
Notifications
You must be signed in to change notification settings - Fork 20.8k
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
use .get_file() instead of basename from libc #51429
Conversation
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.
For what I've found basename
and get_basename
are not equivalent:
basename
returns the last component of the path, which could be a file name or a folder name. See http://man.openbsd.org/basename.3get_basename
returns the path without the extension. See https://docs.godotengine.org/en/latest/classes/class_string.html#class-string-method-get-basename
Given the name file_name
, the fact that it will be used like this:
String dest_path = trash_path + "/files/" + file_name;
And as far as I can tell, move_to_trash
does not work for folders.
I think you should use get_file
instead of get_basename
. See https://docs.godotengine.org/en/latest/classes/class_string.html#class-string-method-get-file
The behavior of get_file
differs from the description of basename
in that it will return empty string if the input is empty string or consist only of /
. I quote from http://man.openbsd.org/basename.3:
If path consists entirely of ‘/’ characters, a pointer to the string "/" is returned. If path is a null pointer or the empty string, a pointer to the string "." is returned.
The description of basename
at https://www.man7.org/linux/man-pages/man3/basename.3.html (which that is not BSD) matches the description at http://man.openbsd.org/basename.3 and also provides some test cases:
path dirname basename
/usr/lib /usr lib
/usr/ / usr
usr . usr
/ / /
. . .
.. . ..
Adding get_basename
and get_file
there for comparison, looks like this:
path dirname basename get_basename get_file
/usr/lib /usr lib /usr/lib lib
/usr/ / usr /usr/ <empty string>
usr . usr usr usr
/ / / / <empty string>
. . . <empty string> .
.. . .. . ..
Where <empty string>
denotes the empty string.
You can see that if we assume we have a file path, then get_file
is what we want.
fb79850
to
3d5b333
Compare
Thanks for checking! I've updated the PR. I was confused because the similar naming, apologize. |
3d5b333
to
1799445
Compare
I forgot to update the commit message, here's another try |
Sorry. I had a look for usages of godot/editor/dependency_editor.cpp Lines 528 to 537 in 77721b3
If |
I wish there was a better API for this. However, let us take advantage of this pattern: I found example of it already in the source: godot/editor/plugins/script_editor_plugin.cpp Line 1976 in 58aa020
That would give us the name of the folder (but not of the file). |
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.
This is what I suggest:
String file_name = p_path.get_file();
if (file_name.length() == 0) {
file_name = p_path.get_base_dir().get_file();
}
That way, if p_path
points to a file, p_path.get_file()
gives us the name of the file. Otherwise, we get empty string and enter the conditional, where p_path.get_base_dir().get_file()
gives us the name of the folder.
On OpenBSD the compiler complains that calling basename(3) would lose const qualifier. basename(3) is defined as char *basename(char *); and can, accorgindly to the POSIX.1, modify the passed string. This uses the .get_file() method. The check is necessary because file_name could be a directory, in which case .get_file() would return an empty string. The .get_base_dir().get_file() idiom is already used. The usage of get_file() and the check were suggested by theraot, thanks!
1799445
to
a3384b7
Compare
Thanks for the suggestion! I'm not really comfortable writing C++ yet. Yep, I wish there were better API too. (but this applies also to libc, I've also improved the commit message, hope it's not too long ^^ |
I, of course, approve this. Because of course. |
Thanks! |
Cherry-picked for 3.4. |
This fixes #48938