mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 13:47:46 +00:00
AK+Everywhere: Add and use static APIs for LexicalPath
The LexicalPath instance methods dirname(), basename(), title() and extension() will be changed to return StringView const& in a further commit. Due to this, users creating temporary LexicalPath objects just to call one of those getters will recieve a StringView const& pointing to a possible freed buffer. To avoid this, static methods for those APIs have been added, which will return a String by value to avoid those problems. All cases where temporary LexicalPath objects have been used as described above haven been changed to use the static APIs.
This commit is contained in:
parent
9b8f35259c
commit
fc6d051dfd
43 changed files with 80 additions and 56 deletions
|
@ -204,7 +204,7 @@ int main(int argc, char** argv)
|
|||
auto& icon_image_widget = *widget.find_descendant_of_type_named<GUI::ImageWidget>("icon");
|
||||
icon_image_widget.set_bitmap(GUI::FileIconProvider::icon_for_executable(executable_path).bitmap_for_size(32));
|
||||
|
||||
auto app_name = LexicalPath(executable_path).basename();
|
||||
auto app_name = LexicalPath::basename(executable_path);
|
||||
auto af = Desktop::AppFile::get_for_app(app_name);
|
||||
if (af->is_valid())
|
||||
app_name = af->name();
|
||||
|
|
|
@ -55,7 +55,7 @@ static void run_file_operation([[maybe_unused]] FileOperation operation, const S
|
|||
perror("dup2");
|
||||
_exit(1);
|
||||
}
|
||||
if (execlp("/bin/FileOperation", "/bin/FileOperation", "Copy", source.characters(), LexicalPath(destination).dirname().characters(), nullptr) < 0) {
|
||||
if (execlp("/bin/FileOperation", "/bin/FileOperation", "Copy", source.characters(), LexicalPath::dirname(destination).characters(), nullptr) < 0) {
|
||||
perror("execlp");
|
||||
_exit(1);
|
||||
}
|
||||
|
@ -609,7 +609,7 @@ void DirectoryView::handle_drop(const GUI::ModelIndex& index, const GUI::DropEve
|
|||
for (auto& url_to_copy : urls) {
|
||||
if (!url_to_copy.is_valid() || url_to_copy.path() == target_node.full_path())
|
||||
continue;
|
||||
auto new_path = String::formatted("{}/{}", target_node.full_path(), LexicalPath(url_to_copy.path()).basename());
|
||||
auto new_path = String::formatted("{}/{}", target_node.full_path(), LexicalPath::basename(url_to_copy.path()));
|
||||
if (url_to_copy.path() == new_path)
|
||||
continue;
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ void delete_paths(const Vector<String>& paths, bool should_confirm, GUI::Window*
|
|||
{
|
||||
String message;
|
||||
if (paths.size() == 1) {
|
||||
message = String::formatted("Really delete {}?", LexicalPath(paths[0]).basename());
|
||||
message = String::formatted("Really delete {}?", LexicalPath::basename(paths[0]));
|
||||
} else {
|
||||
message = String::formatted("Really delete {} files?", paths.size());
|
||||
}
|
||||
|
|
|
@ -187,7 +187,7 @@ void do_paste(const String& target_directory, GUI::Window* window)
|
|||
void do_create_link(const Vector<String>& selected_file_paths, GUI::Window* window)
|
||||
{
|
||||
auto path = selected_file_paths.first();
|
||||
auto destination = String::formatted("{}/{}", Core::StandardPaths::desktop_directory(), LexicalPath { path }.basename());
|
||||
auto destination = String::formatted("{}/{}", Core::StandardPaths::desktop_directory(), LexicalPath::basename(path));
|
||||
if (auto result = Core::File::link_file(destination, path); result.is_error()) {
|
||||
GUI::MessageBox::show(window, String::formatted("Could not create desktop shortcut:\n{}", result.error()), "File Manager",
|
||||
GUI::MessageBox::Type::Error);
|
||||
|
@ -736,7 +736,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
|||
selected = directory_view.selected_file_paths();
|
||||
} else {
|
||||
path = directories_model->full_path(tree_view.selection().first());
|
||||
container_dir_path = LexicalPath(path).basename();
|
||||
container_dir_path = LexicalPath::basename(path);
|
||||
selected = tree_view_selected_file_paths();
|
||||
}
|
||||
|
||||
|
@ -1115,7 +1115,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
|||
for (auto& url_to_copy : urls) {
|
||||
if (!url_to_copy.is_valid() || url_to_copy.path() == directory)
|
||||
continue;
|
||||
auto new_path = String::formatted("{}/{}", directory, LexicalPath(url_to_copy.path()).basename());
|
||||
auto new_path = String::formatted("{}/{}", directory, LexicalPath::basename(url_to_copy.path()));
|
||||
if (url_to_copy.path() == new_path)
|
||||
continue;
|
||||
|
||||
|
|
|
@ -432,7 +432,7 @@ void Image::set_title(String title)
|
|||
void Image::set_path(String path)
|
||||
{
|
||||
m_path = move(path);
|
||||
set_title(LexicalPath(m_path).basename());
|
||||
set_title(LexicalPath::basename(m_path));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ GUI::Variant PlaylistModel::data(const GUI::ModelIndex& index, GUI::ModelRole ro
|
|||
if (role == GUI::ModelRole::Display) {
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
return m_playlist_items[index.row()].extended_info->track_display_title.value_or(LexicalPath(m_playlist_items[index.row()].path).title());
|
||||
return m_playlist_items[index.row()].extended_info->track_display_title.value_or(LexicalPath::title(m_playlist_items[index.row()].path));
|
||||
case 1:
|
||||
return format_duration(m_playlist_items[index.row()].extended_info->track_length_in_seconds.value_or(0));
|
||||
case 2:
|
||||
|
|
|
@ -300,7 +300,7 @@ void SoundPlayerWidgetAdvancedView::try_fill_missing_info(Vector<M3UEntry>& entr
|
|||
}
|
||||
|
||||
if (!entry.extended_info->track_display_title.has_value())
|
||||
entry.extended_info->track_display_title = LexicalPath(entry.path).title();
|
||||
entry.extended_info->track_display_title = LexicalPath::title(entry.path);
|
||||
if (!entry.extended_info->track_length_in_seconds.has_value()) {
|
||||
if (auto reader = Audio::Loader::create(entry.path); !reader->has_error())
|
||||
entry.extended_info->track_length_in_seconds = reader->total_samples() / reader->sample_rate();
|
||||
|
|
|
@ -300,7 +300,7 @@ Result<void, String> ExportDialog::make_and_run_for(StringView mime, Core::File&
|
|||
} else {
|
||||
auto page = GUI::WizardPage::construct(
|
||||
"Export File Format",
|
||||
String::formatted("Select the format you wish to export to '{}' as", LexicalPath { file.filename() }.basename()));
|
||||
String::formatted("Select the format you wish to export to '{}' as", LexicalPath::basename(file.filename())));
|
||||
|
||||
page->on_next_page = [] { return nullptr; };
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ HelpWindow::HelpWindow(GUI::Window* parent)
|
|||
m_webview->on_link_click = [this](auto& url, auto&, auto&&) {
|
||||
VERIFY(url.protocol() == "spreadsheet");
|
||||
if (url.host() == "example") {
|
||||
auto entry = LexicalPath(url.path()).basename();
|
||||
auto entry = LexicalPath::basename(url.path());
|
||||
auto doc_option = m_docs.get(entry);
|
||||
if (!doc_option.is_object()) {
|
||||
GUI::MessageBox::show_error(this, String::formatted("No documentation entry found for '{}'", url.path()));
|
||||
|
@ -120,7 +120,7 @@ HelpWindow::HelpWindow(GUI::Window* parent)
|
|||
widget.add_sheet(sheet.release_nonnull());
|
||||
window->show();
|
||||
} else if (url.host() == "doc") {
|
||||
auto entry = LexicalPath(url.path()).basename();
|
||||
auto entry = LexicalPath::basename(url.path());
|
||||
m_webview->load(URL::create_with_data("text/html", render(entry)));
|
||||
} else {
|
||||
dbgln("Invalid spreadsheet action domain '{}'", url.host());
|
||||
|
|
|
@ -252,7 +252,7 @@ Result<NonnullRefPtrVector<Sheet>, String> ImportDialog::make_and_run_for(String
|
|||
} else {
|
||||
auto page = GUI::WizardPage::construct(
|
||||
"Import File Format",
|
||||
String::formatted("Select the format you wish to import '{}' as", LexicalPath { file.filename() }.basename()));
|
||||
String::formatted("Select the format you wish to import '{}' as", LexicalPath::basename(file.filename())));
|
||||
|
||||
page->on_next_page = [] { return nullptr; };
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue