From d82ebce770a1dd53092d4fc070b9dc7f00731049 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Sun, 17 Aug 2014 22:23:17 +0200 Subject: [PATCH] Create LinkApp objects in Menu::readLinksOfSection() This avoids having to store the paths in an intermediate vector. Also it allows us to do the writability check once per directory instead of once per file. --- src/menu.cpp | 64 ++++++++++++++++++++++++---------------------------- src/menu.h | 2 +- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/menu.cpp b/src/menu.cpp index 0fc4d1a..ddd0a81 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -791,21 +791,6 @@ void Menu::removePackageLink(std::string path) #endif #endif -void Menu::readLinksOfSection(std::string path, std::vector &linkfiles) -{ - DIR *dirp; - struct dirent *dptr; - - if ((dirp = opendir(path.c_str())) == NULL) return; - - while ((dptr = readdir(dirp))) { - if (dptr->d_type != DT_REG) continue; - linkfiles.emplace_back(path + "/" + dptr->d_name); - } - - closedir(dirp); -} - static bool compare_links(Link *a, Link *b) { LinkApp *app1 = dynamic_cast(a); @@ -829,39 +814,48 @@ void Menu::orderLinks() void Menu::readLinks() { - vector linkfiles; - iLink = 0; iFirstDispRow = 0; for (uint i=0; isections.size() ? iSection : i); + string const& section = sections[correct]; - readLinksOfSection(GMENU2X_SYSTEM_DIR "/sections/" - + sections[correct], linkfiles); - - readLinksOfSection(GMenu2X::getHome() + "/sections/" - + sections[correct], linkfiles); - - for (auto const& linkfile : linkfiles) { - // Check whether the link file could be deleted. - bool deletable = access(parentDir(linkfile).c_str(), W_OK) == 0; - - LinkApp *link = new LinkApp(gmenu2x, linkfile, deletable); - link->setSize(gmenu2x->skinConfInt["linkWidth"], gmenu2x->skinConfInt["linkHeight"]); - if (link->targetExists()) - links[i].push_back(link); - else - delete link; - } + readLinksOfSection(GMENU2X_SYSTEM_DIR "/sections/" + section, i); + readLinksOfSection(GMenu2X::getHome() + "/sections/" + section, i); } orderLinks(); } +void Menu::readLinksOfSection(std::string const& path, uint i) +{ + DIR *dirp = opendir(path.c_str()); + if (!dirp) return; + + // Check whether link files in this directory could be deleted. + bool deletable = access(path.c_str(), W_OK) == 0; + + while (struct dirent *dptr = readdir(dirp)) { + if (dptr->d_type != DT_REG) continue; + string linkfile = path + '/' + dptr->d_name; + + LinkApp *link = new LinkApp(gmenu2x, linkfile, deletable); + if (link->targetExists()) { + link->setSize( + gmenu2x->skinConfInt["linkWidth"], + gmenu2x->skinConfInt["linkHeight"]); + links[i].push_back(link); + } else { + delete link; + } + } + + closedir(dirp); +} + void Menu::renameSection(int index, const string &name) { sections[index] = name; } diff --git a/src/menu.h b/src/menu.h index 8e84607..d3dfc8b 100644 --- a/src/menu.h +++ b/src/menu.h @@ -89,7 +89,7 @@ private: #endif // Load all the links on the given section directory. - void readLinksOfSection(std::string path, std::vector &linkfiles); + void readLinksOfSection(std::string const& path, uint i); void decSectionIndex(); void incSectionIndex();