From ab31eafaca6a7b96b0ae0ef559173d886edbf208 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Wed, 14 Dec 2022 17:30:34 +0100 Subject: [PATCH] Some code cleanup. --- es-app/src/FileData.cpp | 86 +++++------ es-app/src/GamelistFileParser.cpp | 108 +++++++------- es-app/src/SystemData.cpp | 216 ++++++++++++++------------- es-core/src/utils/FileSystemUtil.cpp | 182 +++++++++++----------- 4 files changed, 302 insertions(+), 290 deletions(-) diff --git a/es-app/src/FileData.cpp b/es-app/src/FileData.cpp index 80bcf5a08..dea180154 100644 --- a/es-app/src/FileData.cpp +++ b/es-app/src/FileData.cpp @@ -40,6 +40,7 @@ FileData::FileData(FileType type, , mEnvData {envData} , mSystem {system} , mOnlyFolders {false} + , mHasFolders {false} , mUpdateChildrenLastPlayed {false} , mUpdateChildrenMostPlayed {false} , mDeletionFlag {false} @@ -76,7 +77,7 @@ FileData::~FileData() std::string FileData::getDisplayName() const { - std::string stem {Utils::FileSystem::getStem(mPath)}; + const std::string& stem {Utils::FileSystem::getStem(mPath)}; return stem; } @@ -167,7 +168,7 @@ const std::vector FileData::getChildrenRecursive() const const std::string FileData::getROMDirectory() { - std::string romDirSetting {Settings::getInstance()->getString("ROMDirectory")}; + const std::string& romDirSetting {Settings::getInstance()->getString("ROMDirectory")}; std::string romDirPath; if (romDirSetting == "") { @@ -196,7 +197,7 @@ const std::string FileData::getROMDirectory() const std::string FileData::getMediaDirectory() { - std::string mediaDirSetting {Settings::getInstance()->getString("MediaDirectory")}; + const std::string& mediaDirSetting {Settings::getInstance()->getString("MediaDirectory")}; std::string mediaDirPath; if (mediaDirSetting == "") { @@ -233,7 +234,7 @@ const std::string FileData::getMediafilePath(const std::string& subdirectory) co subFolders + "/" + getDisplayName()}; // Look for an image file in the media directory. - for (size_t i = 0; i < extList.size(); ++i) { + for (size_t i {0}; i < extList.size(); ++i) { std::string mediaPath {tempPath + extList[i]}; if (Utils::FileSystem::exists(mediaPath)) return mediaPath; @@ -332,7 +333,7 @@ const std::string FileData::getVideoPath() const getDisplayName()}; // Look for media in the media directory. - for (size_t i = 0; i < extList.size(); ++i) { + for (size_t i {0}; i < extList.size(); ++i) { std::string mediaPath {tempPath + extList[i]}; if (Utils::FileSystem::exists(mediaPath)) return mediaPath; @@ -424,7 +425,7 @@ std::vector FileData::getScrapeFilesRecursive(bool includeFolders, const bool FileData::isArcadeAsset() const { - const std::string stem {Utils::FileSystem::getStem(mPath)}; + const std::string& stem {Utils::FileSystem::getStem(mPath)}; return ((mSystem && (mSystem->hasPlatformId(PlatformIds::ARCADE) || mSystem->hasPlatformId(PlatformIds::SNK_NEO_GEO))) && (MameNames::getInstance().isBios(stem) || MameNames::getInstance().isDevice(stem))); @@ -432,7 +433,7 @@ const bool FileData::isArcadeAsset() const const bool FileData::isArcadeGame() const { - const std::string stem {Utils::FileSystem::getStem(mPath)}; + const std::string& stem {Utils::FileSystem::getStem(mPath)}; return ((mSystem && (mSystem->hasPlatformId(PlatformIds::ARCADE) || mSystem->hasPlatformId(PlatformIds::SNK_NEO_GEO))) && (!MameNames::getInstance().isBios(stem) && !MameNames::getInstance().isDevice(stem))); @@ -444,7 +445,7 @@ void FileData::addChild(FileData* file) if (!mSystem->getFlattenFolders()) assert(file->getParent() == nullptr); - const std::string key = file->getKey(); + const std::string& key {file->getKey()}; if (mChildrenByFilename.find(key) == mChildrenByFilename.cend()) { mChildrenByFilename[key] = file; mChildren.emplace_back(file); @@ -481,7 +482,7 @@ void FileData::sort(ComparisonFunction& comparator, std::vector mChildrenOthers; if (mSystem->isGroupedCustomCollection()) - gameCount = {}; + gameCount = {0, 0}; if (!showHiddenGames) { for (auto it = mChildren.begin(); it != mChildren.end();) { @@ -505,20 +506,20 @@ void FileData::sort(ComparisonFunction& comparator, // The main custom collections view is sorted during startup in CollectionSystemsManager. // The individual collections are however sorted as any normal systems/folders. if (mSystem->isCollection() && mSystem->getFullName() == "collections") { - std::pair tempGameCount {}; + std::pair tempGameCount {0, 0}; for (auto it = mChildren.cbegin(); it != mChildren.cend(); ++it) { if ((*it)->getChildren().size() > 0) (*it)->sort(comparator, gameCount); tempGameCount.first += gameCount.first; tempGameCount.second += gameCount.second; - gameCount = {}; + gameCount = {0, 0}; } gameCount = tempGameCount; return; } if (foldersOnTop) { - for (unsigned int i = 0; i < mChildren.size(); ++i) { + for (unsigned int i {0}; i < mChildren.size(); ++i) { if (mChildren[i]->getType() == FOLDER) { mChildrenFolders.emplace_back(mChildren[i]); } @@ -597,24 +598,24 @@ void FileData::sortFavoritesOnTop(ComparisonFunction& comparator, std::vector mChildrenOthers; if (mSystem->isGroupedCustomCollection()) - gameCount = {}; + gameCount = {0, 0}; // The main custom collections view is sorted during startup in CollectionSystemsManager. // The individual collections are however sorted as any normal systems/folders. if (mSystem->isCollection() && mSystem->getFullName() == "collections") { - std::pair tempGameCount = {}; + std::pair tempGameCount = {0, 0}; for (auto it = mChildren.cbegin(); it != mChildren.cend(); ++it) { if ((*it)->getChildren().size() > 0) (*it)->sortFavoritesOnTop(comparator, gameCount); tempGameCount.first += gameCount.first; tempGameCount.second += gameCount.second; - gameCount = {}; + gameCount = {0, 0}; } gameCount = tempGameCount; return; } - for (unsigned int i = 0; i < mChildren.size(); ++i) { + for (unsigned int i {0}; i < mChildren.size(); ++i) { // If the option to hide hidden games has been set and the game is hidden, // then skip it. Normally games are hidden during loading of the gamelists in // Gamelist::parseGamelist() and this code should only run when a user has marked @@ -745,7 +746,7 @@ void FileData::countGames(std::pair& gameCount) bool isKidMode {(Settings::getInstance()->getString("UIMode") == "kid" || Settings::getInstance()->getBool("ForceKid"))}; - for (unsigned int i = 0; i < mChildren.size(); ++i) { + for (unsigned int i {0}; i < mChildren.size(); ++i) { if (mChildren[i]->getType() == GAME && mChildren[i]->getCountAsGame()) { if (!isKidMode || (isKidMode && mChildren[i]->getKidgame())) { ++gameCount.first; @@ -798,9 +799,9 @@ void FileData::updateMostPlayedList() const FileData::SortType& FileData::getSortTypeFromString(const std::string& desc) const { - std::vector SortTypes = FileSorts::SortTypes; + std::vector SortTypes {FileSorts::SortTypes}; - for (unsigned int i = 0; i < FileSorts::SortTypes.size(); ++i) { + for (unsigned int i {0}; i < FileSorts::SortTypes.size(); ++i) { const FileData::SortType& sort {FileSorts::SortTypes.at(i)}; if (sort.description == desc) return sort; @@ -983,11 +984,11 @@ void FileData::launchGame() command = Utils::FileSystem::expandHomePath(command); // Check that the emulator binary actually exists, and if so, get its path. - std::string binaryPath {findEmulatorPath(command)}; + const std::string& binaryPath {findEmulatorPath(command)}; // Hack to show an error message if there was no emulator entry in es_find_rules.xml. if (binaryPath.substr(0, 18) == "NO EMULATOR RULE: ") { - std::string emulatorEntry {binaryPath.substr(18, binaryPath.size() - 18)}; + const std::string& emulatorEntry {binaryPath.substr(18, binaryPath.size() - 18)}; LOG(LogError) << "Couldn't launch game, either there is no emulator entry for \"" << emulatorEntry << "\" in es_find_rules.xml or there are no rules defined"; LOG(LogError) << "Raw emulator launch command:"; @@ -1051,7 +1052,7 @@ void FileData::launchGame() quotationMarkPos = static_cast(command.find("\"", emuPathPos + 9) - emuPathPos); } - size_t spacePos {command.find(" ", emuPathPos + quotationMarkPos)}; + const size_t spacePos {command.find(" ", emuPathPos + quotationMarkPos)}; std::string coreRaw; std::string coreFile; if (spacePos != std::string::npos) { @@ -1120,7 +1121,7 @@ void FileData::launchGame() // If a %CORE_ find rule entry is used in es_systems.xml for this system, then try to find // the emulator core using the rules defined in es_find_rules.xml. - for (std::string path : emulatorCorePaths) { + for (std::string& path : emulatorCorePaths) { // The position of the %CORE_ variable could have changed as there may have been an // %EMULATOR_ variable that was substituted for the actual emulator binary. coreEntryPos = command.find("%CORE_"); @@ -1210,7 +1211,7 @@ void FileData::launchGame() } std::string startDirectory; - size_t startDirPos {command.find("%STARTDIR%")}; + const size_t startDirPos {command.find("%STARTDIR%")}; if (startDirPos != std::string::npos) { bool invalidEntry {false}; @@ -1233,7 +1234,7 @@ void FileData::launchGame() } } else if (!invalidEntry) { - size_t spacePos {command.find(" ", startDirPos)}; + const size_t spacePos {command.find(" ", startDirPos)}; if (spacePos != std::string::npos) { startDirectory = command.substr(startDirPos + 11, spacePos - startDirPos - 11); command = command.replace(startDirPos, spacePos - startDirPos + 1, ""); @@ -1331,7 +1332,7 @@ void FileData::launchGame() } } else if (!invalidEntry) { - size_t spacePos {command.find(" ", injectPos)}; + const size_t spacePos {command.find(" ", injectPos)}; if (spacePos != std::string::npos) { injectFile = command.substr(injectPos + 9, spacePos - injectPos - 9); command = command.replace(injectPos, spacePos - injectPos + 1, ""); @@ -1407,8 +1408,8 @@ void FileData::launchGame() // The special characters need to be procesed in this order. std::string specialCharacters {"^&()=;,"}; - for (size_t i = 0; i < specialCharacters.size(); ++i) { - std::string special(1, specialCharacters[i]); + for (size_t i {0}; i < specialCharacters.size(); ++i) { + const std::string& special {1, specialCharacters[i]}; if (romPath.find(special) != std::string::npos) { romPath = Utils::String::replace(romPath, special, "^" + special); foundSpecial = true; @@ -1701,15 +1702,15 @@ const std::string FileData::findEmulatorPath(std::string& command) return "NO EMULATOR RULE: " + emulatorEntry; #if defined(_WIN64) - for (std::string path : emulatorWinRegistryPaths) { + for (std::string& path : emulatorWinRegistryPaths) { // Search for the emulator using the App Paths keys in the Windows Registry. - std::string registryKeyPath {"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + - path}; + const std::string& registryKeyPath { + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + path}; HKEY registryKey; LSTATUS keyStatus {-1}; LSTATUS pathStatus {-1}; - char registryPath[1024] {}; + char registryPath[1024] {0}; DWORD pathSize {1024}; // First look in HKEY_CURRENT_USER. @@ -1746,11 +1747,11 @@ const std::string FileData::findEmulatorPath(std::string& command) RegCloseKey(registryKey); } - for (std::string value : emulatorWinRegistryValues) { + for (std::string& value : emulatorWinRegistryValues) { // If the pipe character is found, then the string following this should be appended // to the key value, assuming the key is found. std::string appendString; - size_t pipePos {value.find('|')}; + const size_t pipePos {value.find('|')}; if (pipePos != std::string::npos) { appendString = value.substr(pipePos + 1, std::string::npos); @@ -1758,14 +1759,14 @@ const std::string FileData::findEmulatorPath(std::string& command) } // Search for the defined value in the Windows Registry. - std::string registryValueKey { + const std::string& registryValueKey { Utils::String::replace(Utils::FileSystem::getParent(value), "/", "\\")}; - std::string registryValue {Utils::FileSystem::getFileName(value)}; + const std::string& registryValue {Utils::FileSystem::getFileName(value)}; HKEY registryKey; LSTATUS keyStatus {-1}; LSTATUS pathStatus {-1}; - char path[1024] {}; + char path[1024] {0}; DWORD pathSize {1024}; // First look in HKEY_CURRENT_USER. @@ -1811,7 +1812,7 @@ const std::string FileData::findEmulatorPath(std::string& command) } #endif - for (std::string path : emulatorSystemPaths) { + for (std::string& path : emulatorSystemPaths) { #if defined(_WIN64) std::wstring pathWide {Utils::String::stringToWideString(path)}; // Search for the emulator using the PATH environment variable. @@ -1846,11 +1847,11 @@ const std::string FileData::findEmulatorPath(std::string& command) #endif } - for (std::string path : emulatorStaticPaths) { + for (std::string& path : emulatorStaticPaths) { // If a pipe character is present in the staticpath entry it means we should substitute // the emulator binary with whatever is defined after the pipe character. std::string replaceCommand; - size_t pipePos {path.find('|')}; + const size_t pipePos {path.find('|')}; if (pipePos != std::string::npos) { replaceCommand = path.substr(pipePos + 1); @@ -1899,7 +1900,7 @@ const std::string FileData::findEmulatorPath(std::string& command) // If the first character is a quotation mark, then we need to extract up to the // next quotation mark, otherwise we'll only extract up to the first space character. if (command.front() == '\"') { - std::string emuTemp {command.substr(1, std::string::npos)}; + const std::string& emuTemp {command.substr(1, std::string::npos)}; emuExecutable = emuTemp.substr(0, emuTemp.find('"')); } else { @@ -1917,7 +1918,8 @@ const std::string FileData::findEmulatorPath(std::string& command) #if defined(_WIN64) std::wstring emuExecutableWide {Utils::String::stringToWideString(emuExecutable)}; // Search for the emulator using the PATH environment variable. - DWORD size {SearchPathW(nullptr, emuExecutableWide.c_str(), L".exe", 0, nullptr, nullptr)}; + const DWORD size { + SearchPathW(nullptr, emuExecutableWide.c_str(), L".exe", 0, nullptr, nullptr)}; if (size) { std::vector pathBuffer(static_cast(size) + 1); diff --git a/es-app/src/GamelistFileParser.cpp b/es-app/src/GamelistFileParser.cpp index a6ca3659f..412014095 100644 --- a/es-app/src/GamelistFileParser.cpp +++ b/es-app/src/GamelistFileParser.cpp @@ -32,19 +32,19 @@ namespace GamelistFileParser return nullptr; } - Utils::FileSystem::StringList pathList = Utils::FileSystem::getPathList(relative); + const Utils::FileSystem::StringList& pathList {Utils::FileSystem::getPathList(relative)}; auto path_it = pathList.begin(); - FileData* treeNode = root; - bool found = false; - while (path_it != pathList.end()) { - const std::unordered_map& children = - treeNode->getChildrenByFilename(); + FileData* treeNode {root}; + bool found {false}; - std::string key = *path_it; + while (path_it != pathList.end()) { + const std::unordered_map& children { + treeNode->getChildrenByFilename()}; + + const std::string key {*path_it}; found = children.find(key) != children.cend(); - if (found) { + if (found) treeNode = children.at(key); - } // This is the end. if (path_it == --pathList.end()) { @@ -58,8 +58,8 @@ namespace GamelistFileParser // Handle the special situation where a file exists and has an entry in the // gamelist.xml file but the file extension is not configured in es_systems.xml. - const std::vector extensions = - system->getSystemEnvData()->mSearchExtensions; + const std::vector& extensions { + system->getSystemEnvData()->mSearchExtensions}; if (std::find(extensions.cbegin(), extensions.cend(), Utils::FileSystem::getExtension(path)) == extensions.cend()) { @@ -69,7 +69,7 @@ namespace GamelistFileParser return nullptr; } - FileData* file = new FileData(type, path, system->getSystemEnvData(), system); + FileData* file {new FileData(type, path, system->getSystemEnvData(), system)}; // Skipping arcade assets from gamelist. if (!file->isArcadeAsset()) @@ -103,8 +103,8 @@ namespace GamelistFileParser void parseGamelist(SystemData* system) { - bool trustGamelist = Settings::getInstance()->getBool("ParseGamelistOnly"); - std::string xmlpath = system->getGamelistPath(false); + const bool trustGamelist {Settings::getInstance()->getBool("ParseGamelistOnly")}; + const std::string& xmlpath {system->getGamelistPath(false)}; if (!Utils::FileSystem::exists(xmlpath)) { LOG(LogDebug) << "GamelistFileParser::parseGamelist(): System \"" << system->getName() @@ -121,10 +121,10 @@ namespace GamelistFileParser pugi::xml_document doc; #if defined(_WIN64) - pugi::xml_parse_result result = - doc.load_file(Utils::String::stringToWideString(xmlpath).c_str()); + const pugi::xml_parse_result& result { + doc.load_file(Utils::String::stringToWideString(xmlpath).c_str())}; #else - pugi::xml_parse_result result = doc.load_file(xmlpath.c_str()); + const pugi::xml_parse_result& result {doc.load_file(xmlpath.c_str())}; #endif if (!result) { @@ -133,18 +133,18 @@ namespace GamelistFileParser return; } - pugi::xml_node root = doc.child("gameList"); + const pugi::xml_node& root {doc.child("gameList")}; if (!root) { LOG(LogError) << "Couldn't find node in gamelist \"" << xmlpath << "\""; return; } - pugi::xml_node alternativeEmulator = doc.child("alternativeEmulator"); + const pugi::xml_node& alternativeEmulator {doc.child("alternativeEmulator")}; if (alternativeEmulator) { - std::string label = alternativeEmulator.child("label").text().get(); + const std::string& label {alternativeEmulator.child("label").text().get()}; if (label != "") { - bool validLabel = false; - for (auto command : system->getSystemEnvData()->mLaunchCommands) { + bool validLabel {false}; + for (auto& command : system->getSystemEnvData()->mLaunchCommands) { if (command.second == label) validLabel = true; } @@ -165,18 +165,19 @@ namespace GamelistFileParser } } - std::string relativeTo = system->getStartPath(); - bool showHiddenFiles = Settings::getInstance()->getBool("ShowHiddenFiles"); + const std::string& relativeTo {system->getStartPath()}; + const bool showHiddenFiles {Settings::getInstance()->getBool("ShowHiddenFiles")}; - std::vector tagList = {"game", "folder"}; - FileType typeList[2] = {GAME, FOLDER}; - for (int i = 0; i < 2; ++i) { - std::string tag = tagList[i]; - FileType type = typeList[i]; - for (pugi::xml_node fileNode = root.child(tag.c_str()); fileNode; + const std::vector tagList {"game", "folder"}; + const FileType typeList[2] = {GAME, FOLDER}; + + for (int i {0}; i < 2; ++i) { + std::string tag {tagList[i]}; + FileType type {typeList[i]}; + for (pugi::xml_node fileNode {root.child(tag.c_str())}; fileNode; fileNode = fileNode.next_sibling(tag.c_str())) { - const std::string path = Utils::FileSystem::resolveRelativePath( - fileNode.child("path").text().get(), relativeTo, false); + const std::string& path {Utils::FileSystem::resolveRelativePath( + fileNode.child("path").text().get(), relativeTo, false)}; if (!trustGamelist && !Utils::FileSystem::exists(path)) { #if defined(_WIN64) @@ -199,7 +200,7 @@ namespace GamelistFileParser continue; } - FileData* file = findOrCreateFile(system, path, type); + FileData* file {findOrCreateFile(system, path, type)}; // Don't load entries with the wrong type. This should very rarely (if ever) happen. if (file != nullptr && ((tag == "game" && file->getType() == FOLDER) || @@ -214,7 +215,7 @@ namespace GamelistFileParser continue; } else if (!file->isArcadeAsset()) { - std::string defaultName = file->metadata.get("name"); + const std::string& defaultName {file->metadata.get("name")}; if (file->getType() == FOLDER) { file->metadata = MetaDataList::createFromXML(FOLDER_METADATA, fileNode, relativeTo); @@ -265,7 +266,7 @@ namespace GamelistFileParser SystemData* system) { // Create game and add to parent node. - pugi::xml_node newNode = parent.append_child(tag.c_str()); + pugi::xml_node newNode {parent.append_child(tag.c_str())}; // Write metadata. file->metadata.appendToXML(newNode, true, system->getStartPath()); @@ -303,17 +304,17 @@ namespace GamelistFileParser pugi::xml_document doc; pugi::xml_node root; - std::string xmlReadPath = system->getGamelistPath(false); - bool hasAlternativeEmulatorTag = false; + const std::string& xmlReadPath {system->getGamelistPath(false)}; + bool hasAlternativeEmulatorTag {false}; if (Utils::FileSystem::exists(xmlReadPath)) { // Parse an existing file first. #if defined(_WIN64) - pugi::xml_parse_result result = - doc.load_file(Utils::String::stringToWideString(xmlReadPath).c_str()); + const pugi::xml_parse_result& result { + doc.load_file(Utils::String::stringToWideString(xmlReadPath).c_str())}; #else - pugi::xml_parse_result result = doc.load_file(xmlReadPath.c_str()); + const pugi::xml_parse_result& result {doc.load_file(xmlReadPath.c_str())}; #endif if (!result) { @@ -329,7 +330,7 @@ namespace GamelistFileParser return; } if (updateAlternativeEmulator) { - pugi::xml_node alternativeEmulator = doc.child("alternativeEmulator"); + pugi::xml_node alternativeEmulator {doc.child("alternativeEmulator")}; if (alternativeEmulator) hasAlternativeEmulatorTag = true; @@ -340,7 +341,7 @@ namespace GamelistFileParser alternativeEmulator = doc.child("alternativeEmulator"); } - pugi::xml_node label = alternativeEmulator.child("label"); + const pugi::xml_node& label {alternativeEmulator.child("label")}; if (label && system->getAlternativeEmulator() != alternativeEmulator.child("label").text().get()) { @@ -360,7 +361,7 @@ namespace GamelistFileParser } else { if (updateAlternativeEmulator && system->getAlternativeEmulator() != "") { - pugi::xml_node alternativeEmulator = doc.prepend_child("alternativeEmulator"); + pugi::xml_node alternativeEmulator {doc.prepend_child("alternativeEmulator")}; alternativeEmulator.prepend_child("label").text().set( system->getAlternativeEmulator().c_str()); } @@ -372,14 +373,14 @@ namespace GamelistFileParser // through all our games and add the information from there. FileData* rootFolder {system->getRootFolder()}; if (rootFolder != nullptr) { - int numUpdated = 0; + int numUpdated {0}; // Get only files, no folders. - std::vector files = rootFolder->getFilesRecursive(GAME | FOLDER); + std::vector files {rootFolder->getFilesRecursive(GAME | FOLDER)}; // Iterate through all files, checking if they're already in the XML file. - for (std::vector::const_iterator fit = files.cbegin(); // Line break. + for (std::vector::const_iterator fit {files.cbegin()}; // Line break. fit != files.cend(); ++fit) { - const std::string tag = ((*fit)->getType() == GAME) ? "game" : "folder"; + const std::string& tag {((*fit)->getType() == GAME) ? "game" : "folder"}; // Do not touch if it wasn't changed and is not flagged for deletion. if (!(*fit)->metadata.wasChanged() && !(*fit)->getDeletionFlag()) @@ -387,18 +388,19 @@ namespace GamelistFileParser // Check if the file already exists in the XML file. // If it does, remove the entry before adding it back. - for (pugi::xml_node fileNode = root.child(tag.c_str()); fileNode; + for (pugi::xml_node fileNode {root.child(tag.c_str())}; fileNode; fileNode = fileNode.next_sibling(tag.c_str())) { - pugi::xml_node pathNode = fileNode.child("path"); + const pugi::xml_node& pathNode {fileNode.child("path")}; if (!pathNode) { LOG(LogError) << "<" << tag << "> node contains no child"; continue; } - std::string nodePath = + const std::string& nodePath { Utils::FileSystem::getCanonicalPath(Utils::FileSystem::resolveRelativePath( - pathNode.text().get(), system->getStartPath(), true)); - std::string gamePath = Utils::FileSystem::getCanonicalPath((*fit)->getPath()); + pathNode.text().get(), system->getStartPath(), true))}; + const std::string& gamePath { + Utils::FileSystem::getCanonicalPath((*fit)->getPath())}; if (nodePath == gamePath) { // Found it @@ -420,7 +422,7 @@ namespace GamelistFileParser // Now write the file. if (numUpdated > 0 || updateAlternativeEmulator) { // Make sure the folders leading up to this path exist (or the write will fail). - std::string xmlWritePath(system->getGamelistPath(true)); + const std::string& xmlWritePath {system->getGamelistPath(true)}; Utils::FileSystem::createDirectory(Utils::FileSystem::getParent(xmlWritePath)); if (updateAlternativeEmulator) { diff --git a/es-app/src/SystemData.cpp b/es-app/src/SystemData.cpp index dee6fc7bb..001f644f4 100644 --- a/es-app/src/SystemData.cpp +++ b/es-app/src/SystemData.cpp @@ -40,8 +40,8 @@ FindRules::FindRules() void FindRules::loadFindRules() { - std::string customSystemsDirectory {Utils::FileSystem::getHomePath() + - "/.emulationstation/custom_systems"}; + const std::string& customSystemsDirectory {Utils::FileSystem::getHomePath() + + "/.emulationstation/custom_systems"}; std::string path {customSystemsDirectory + "/es_find_rules.xml"}; @@ -75,9 +75,10 @@ void FindRules::loadFindRules() pugi::xml_document doc; #if defined(_WIN64) - pugi::xml_parse_result res = doc.load_file(Utils::String::stringToWideString(path).c_str()); + const pugi::xml_parse_result& res { + doc.load_file(Utils::String::stringToWideString(path).c_str())}; #else - pugi::xml_parse_result res = doc.load_file(path.c_str()); + const pugi::xml_parse_result& res {doc.load_file(path.c_str())}; #endif if (!res) { @@ -86,7 +87,7 @@ void FindRules::loadFindRules() } // Actually read the file. - pugi::xml_node ruleList = doc.child("ruleList"); + const pugi::xml_node& ruleList {doc.child("ruleList")}; if (!ruleList) { LOG(LogError) << "es_find_rules.xml is missing the tag"; @@ -96,9 +97,9 @@ void FindRules::loadFindRules() EmulatorRules emulatorRules; CoreRules coreRules; - for (pugi::xml_node emulator = ruleList.child("emulator"); emulator; + for (pugi::xml_node emulator {ruleList.child("emulator")}; emulator; emulator = emulator.next_sibling("emulator")) { - std::string emulatorName = emulator.attribute("name").as_string(); + const std::string& emulatorName {emulator.attribute("name").as_string()}; if (emulatorName.empty()) { LOG(LogWarning) << "Found emulator tag without name attribute, skipping entry"; continue; @@ -109,7 +110,7 @@ void FindRules::loadFindRules() continue; } for (pugi::xml_node rule = emulator.child("rule"); rule; rule = rule.next_sibling("rule")) { - std::string ruleType = rule.attribute("type").as_string(); + const std::string& ruleType {rule.attribute("type").as_string()}; if (ruleType.empty()) { LOG(LogWarning) << "Found rule tag without type attribute for emulator \"" << emulatorName << "\", skipping entry"; @@ -125,18 +126,18 @@ void FindRules::loadFindRules() << emulatorName << "\", skipping entry"; continue; } - for (pugi::xml_node entry = rule.child("entry"); entry; + for (pugi::xml_node entry {rule.child("entry")}; entry; entry = entry.next_sibling("entry")) { - std::string entryValue = entry.text().get(); + const std::string& entryValue {entry.text().get()}; if (ruleType == "systempath") - emulatorRules.systemPaths.push_back(entryValue); + emulatorRules.systemPaths.emplace_back(entryValue); else if (ruleType == "staticpath") - emulatorRules.staticPaths.push_back(entryValue); + emulatorRules.staticPaths.emplace_back(entryValue); #if defined(_WIN64) else if (ruleType == "winregistrypath") - emulatorRules.winRegistryPaths.push_back(entryValue); + emulatorRules.winRegistryPaths.emplace_back(entryValue); else if (ruleType == "winregistryvalue") - emulatorRules.winRegistryValues.push_back(entryValue); + emulatorRules.winRegistryValues.emplace_back(entryValue); #endif } } @@ -149,8 +150,8 @@ void FindRules::loadFindRules() #endif } - for (pugi::xml_node core = ruleList.child("core"); core; core = core.next_sibling("core")) { - std::string coreName = core.attribute("name").as_string(); + for (pugi::xml_node core {ruleList.child("core")}; core; core = core.next_sibling("core")) { + const std::string& coreName {core.attribute("name").as_string()}; if (coreName.empty()) { LOG(LogWarning) << "Found core tag without name attribute, skipping entry"; continue; @@ -159,8 +160,8 @@ void FindRules::loadFindRules() LOG(LogWarning) << "Found repeating core tag \"" << coreName << "\", skipping entry"; continue; } - for (pugi::xml_node rule = core.child("rule"); rule; rule = rule.next_sibling("rule")) { - std::string ruleType = rule.attribute("type").as_string(); + for (pugi::xml_node rule {core.child("rule")}; rule; rule = rule.next_sibling("rule")) { + const std::string& ruleType {rule.attribute("type").as_string()}; if (ruleType.empty()) { LOG(LogWarning) << "Found rule tag without type attribute for core \"" << coreName << "\", skipping entry"; @@ -171,11 +172,11 @@ void FindRules::loadFindRules() << coreName << "\", skipping entry"; continue; } - for (pugi::xml_node entry = rule.child("entry"); entry; + for (pugi::xml_node entry {rule.child("entry")}; entry; entry = entry.next_sibling("entry")) { - std::string entryValue = entry.text().get(); + const std::string& entryValue {entry.text().get()}; if (ruleType == "corepath") - coreRules.corePaths.push_back(entryValue); + coreRules.corePaths.emplace_back(entryValue); } } mCores[coreName] = coreRules; @@ -265,13 +266,12 @@ void SystemData::setIsGameSystemStatus() bool SystemData::populateFolder(FileData* folder) { - const std::string& folderPath = folder->getPath(); - std::string filePath; std::string extension; - bool isGame; - bool showHiddenFiles = Settings::getInstance()->getBool("ShowHiddenFiles"); - Utils::FileSystem::StringList dirContent = Utils::FileSystem::getDirContent(folderPath); + const std::string& folderPath {folder->getPath()}; + const bool showHiddenFiles {Settings::getInstance()->getBool("ShowHiddenFiles")}; + const Utils::FileSystem::StringList& dirContent {Utils::FileSystem::getDirContent(folderPath)}; + bool isGame {false}; // If system directory exists but contains no games, return as error. if (dirContent.size() == 0) @@ -290,9 +290,10 @@ bool SystemData::populateFolder(FileData* folder) mFlattenFolders = true; } - for (Utils::FileSystem::StringList::const_iterator it = dirContent.cbegin(); + for (Utils::FileSystem::StringList::const_iterator it {dirContent.cbegin()}; it != dirContent.cend(); ++it) { filePath = *it; + const bool isDirectory {Utils::FileSystem::isDirectory(filePath)}; // Skip any recursive symlinks as those would hang the application at various places. if (Utils::FileSystem::isSymlink(filePath)) { @@ -306,8 +307,7 @@ bool SystemData::populateFolder(FileData* folder) // Skip hidden files and folders. if (!showHiddenFiles && Utils::FileSystem::isHidden(filePath)) { LOG(LogDebug) << "SystemData::populateFolder(): Skipping hidden " - << (Utils::FileSystem::isDirectory(filePath) ? "directory \"" : "file \"") - << filePath << "\""; + << (isDirectory ? "directory \"" : "file \"") << filePath << "\""; continue; } @@ -317,17 +317,18 @@ bool SystemData::populateFolder(FileData* folder) extension = Utils::FileSystem::getExtension(filePath); isGame = false; + if (std::find(mEnvData->mSearchExtensions.cbegin(), mEnvData->mSearchExtensions.cend(), extension) != mEnvData->mSearchExtensions.cend()) { - FileData* newGame = new FileData(GAME, filePath, mEnvData, this); + FileData* newGame {new FileData(GAME, filePath, mEnvData, this)}; // If adding a configured file extension to a directory it will get interpreted as // a regular file. This is useful for displaying multi-file/multi-disc games as single // entries or for emulators that can get directories passed to them as command line // parameters instead of regular files. In these instances we remove the extension // from the metadata name so it does not show up in the gamelists and similar. - if (Utils::FileSystem::isDirectory(filePath)) { - const std::string folderName = newGame->metadata.get("name"); + if (isDirectory) { + const std::string& folderName {newGame->metadata.get("name")}; newGame->metadata.set( "name", folderName.substr(0, folderName.length() - extension.length())); } @@ -343,15 +344,15 @@ bool SystemData::populateFolder(FileData* folder) } // Add directories that also do not match an extension as folders. - if (!isGame && Utils::FileSystem::isDirectory(filePath)) { + if (!isGame && isDirectory) { // Make sure that it's not a recursive symlink pointing to a location higher in the // hierarchy as the application would run forever trying to resolve the link. if (Utils::FileSystem::isSymlink(filePath)) { - const std::string canonicalPath {Utils::FileSystem::getCanonicalPath(filePath)}; - const std::string canonicalStartPath { + const std::string& canonicalPath {Utils::FileSystem::getCanonicalPath(filePath)}; + const std::string& canonicalStartPath { Utils::FileSystem::getCanonicalPath(mEnvData->mStartPath)}; if (canonicalPath.size() >= canonicalStartPath.size()) { - const std::string combinedPath { + const std::string& combinedPath { mEnvData->mStartPath + canonicalPath.substr(canonicalStartPath.size(), canonicalStartPath.size() - canonicalPath.size())}; @@ -384,9 +385,9 @@ bool SystemData::populateFolder(FileData* folder) void SystemData::indexAllGameFilters(const FileData* folder) { - const std::vector& children = folder->getChildren(); + const std::vector& children {folder->getChildren()}; - for (std::vector::const_iterator it = children.cbegin(); // Line break. + for (std::vector::const_iterator it {children.cbegin()}; // Line break. it != children.cend(); ++it) { switch ((*it)->getType()) { case GAME: @@ -405,10 +406,10 @@ std::vector readList(const std::string& str, const std::string& del { std::vector ret; - size_t prevOff = str.find_first_not_of(delims, 0); - size_t off = str.find_first_of(delims, prevOff); + size_t prevOff {str.find_first_not_of(delims, 0)}; + size_t off {str.find_first_of(delims, prevOff)}; while (off != std::string::npos || prevOff != std::string::npos) { - ret.push_back(str.substr(prevOff, off - prevOff)); + ret.emplace_back(str.substr(prevOff, off - prevOff)); prevOff = str.find_first_not_of(delims, off); off = str.find_first_of(delims, prevOff); @@ -430,12 +431,12 @@ bool SystemData::loadConfig() LOG(LogInfo) << "Only parsing the gamelist.xml files, not scanning system directories"; } - std::vector configPaths = getConfigPath(true); - const std::string rompath = FileData::getROMDirectory(); + const std::vector& configPaths {getConfigPath(true)}; + const std::string& rompath {FileData::getROMDirectory()}; - bool onlyProcessCustomFile = false; + bool onlyProcessCustomFile {false}; - for (auto configPath : configPaths) { + for (auto& configPath : configPaths) { // If the loadExclusive tag is present in the custom es_systems.xml file, then skip // processing of the bundled configuration file. if (onlyProcessCustomFile) @@ -450,10 +451,10 @@ bool SystemData::loadConfig() pugi::xml_document doc; #if defined(_WIN64) - pugi::xml_parse_result res = - doc.load_file(Utils::String::stringToWideString(configPath).c_str()); + const pugi::xml_parse_result& res { + doc.load_file(Utils::String::stringToWideString(configPath).c_str())}; #else - pugi::xml_parse_result res = doc.load_file(configPath.c_str()); + const pugi::xml_parse_result& res {doc.load_file(configPath.c_str())}; #endif if (!res) { @@ -461,7 +462,7 @@ bool SystemData::loadConfig() return true; } - pugi::xml_node loadExclusive = doc.child("loadExclusive"); + const pugi::xml_node& loadExclusive {doc.child("loadExclusive")}; if (loadExclusive) { if (configPath == configPaths.front() && configPaths.size() > 1) { LOG(LogInfo) << "Only loading custom file as the tag is present"; @@ -475,14 +476,14 @@ bool SystemData::loadConfig() } // Actually read the file. - pugi::xml_node systemList = doc.child("systemList"); + const pugi::xml_node& systemList {doc.child("systemList")}; if (!systemList) { LOG(LogError) << "es_systems.xml is missing the tag"; return true; } - for (pugi::xml_node system = systemList.child("system"); system; + for (pugi::xml_node system {systemList.child("system")}; system; system = system.next_sibling("system")) { std::string name; std::string fullname; @@ -545,7 +546,7 @@ bool SystemData::loadConfig() if (Utils::FileSystem::isSymlink(path)) { // Make sure that the symlink is not pointing to somewhere higher in the hierarchy // as that would lead to an infite loop, meaning the application would never start. - std::string resolvedRompath = Utils::FileSystem::getCanonicalPath(rompath); + const std::string& resolvedRompath {Utils::FileSystem::getCanonicalPath(rompath)}; if (resolvedRompath.find(Utils::FileSystem::getCanonicalPath(path)) == 0) { LOG(LogWarning) << "Skipping system \"" << name << "\" as the defined ROM directory \"" @@ -561,7 +562,7 @@ bool SystemData::loadConfig() // the label attribute needs to be set on all entries as it's a requirement for the // alternative emulator logic. std::vector> commands; - for (pugi::xml_node entry = system.child("command"); entry; + for (pugi::xml_node entry {system.child("command")}; entry; entry = entry.next_sibling("command")) { if (!entry.attribute("label")) { if (commands.size() == 1) { @@ -589,29 +590,29 @@ bool SystemData::loadConfig() << name << "\""; break; } - commands.push_back( + commands.emplace_back( std::make_pair(entry.text().get(), entry.attribute("label").as_string())); } // Platform ID list - const std::string platformList = - Utils::String::toLower(system.child("platform").text().get()); + const std::string& platformList { + Utils::String::toLower(system.child("platform").text().get())}; if (platformList == "") { LOG(LogWarning) << "No platform defined for system \"" << name << "\", scraper searches will be inaccurate"; } - std::vector platformStrs = readList(platformList); + const std::vector& platformStrs {readList(platformList)}; std::vector platformIds; for (auto it = platformStrs.cbegin(); it != platformStrs.cend(); ++it) { - std::string str = *it; - PlatformIds::PlatformId platformId = PlatformIds::getPlatformId(str); + std::string str {*it}; + const PlatformIds::PlatformId platformId {PlatformIds::getPlatformId(str)}; if (platformId == PlatformIds::PLATFORM_IGNORE) { // When platform is PLATFORM_IGNORE, do not allow other platforms. platformIds.clear(); - platformIds.push_back(platformId); + platformIds.emplace_back(platformId); break; } @@ -621,7 +622,7 @@ bool SystemData::loadConfig() LOG(LogWarning) << "Unknown platform \"" << str << "\" defined for system \"" << name << "\", scraper searches will be inaccurate"; else if (platformId != PlatformIds::PLATFORM_UNKNOWN) - platformIds.push_back(platformId); + platformIds.emplace_back(platformId); } // Theme folder. @@ -662,20 +663,20 @@ bool SystemData::loadConfig() #endif // Create the system runtime environment data. - SystemEnvironmentData* envData = new SystemEnvironmentData; + SystemEnvironmentData* envData {new SystemEnvironmentData}; envData->mStartPath = path; envData->mSearchExtensions = extensions; envData->mLaunchCommands = commands; envData->mPlatformIds = platformIds; - SystemData* newSys = new SystemData(name, fullname, sortName, envData, themeFolder); - bool onlyHidden = false; + SystemData* newSys {new SystemData(name, fullname, sortName, envData, themeFolder)}; + bool onlyHidden {false}; // If the option to show hidden games has been disabled, then check whether all // games for the system are hidden. That will flag the system as empty. if (!Settings::getInstance()->getBool("ShowHiddenGames")) { - std::vector recursiveGames = - newSys->getRootFolder()->getChildrenRecursive(); + std::vector recursiveGames { + newSys->getRootFolder()->getChildrenRecursive()}; onlyHidden = true; for (auto it = recursiveGames.cbegin(); it != recursiveGames.cend(); ++it) { if ((*it)->getType() != FOLDER) { @@ -692,7 +693,7 @@ bool SystemData::loadConfig() delete newSys; } else { - sSystemVector.push_back(newSys); + sSystemVector.emplace_back(newSys); } } } @@ -723,7 +724,7 @@ std::string SystemData::getLaunchCommandFromLabel(const std::string& label) void SystemData::deleteSystems() { - for (unsigned int i = 0; i < sSystemVector.size(); ++i) + for (unsigned int i {0}; i < sSystemVector.size(); ++i) delete sSystemVector.at(i); sSystemVector.clear(); @@ -734,8 +735,8 @@ std::vector SystemData::getConfigPath(bool legacyWarning) std::vector paths; if (legacyWarning) { - std::string legacyConfigFile = - Utils::FileSystem::getHomePath() + "/.emulationstation/es_systems.cfg"; + const std::string& legacyConfigFile {Utils::FileSystem::getHomePath() + + "/.emulationstation/es_systems.cfg"}; if (Utils::FileSystem::exists(legacyConfigFile)) { #if defined(_WIN64) @@ -751,8 +752,8 @@ std::vector SystemData::getConfigPath(bool legacyWarning) } } - std::string customSystemsDirectory = - Utils::FileSystem::getHomePath() + "/.emulationstation/custom_systems"; + const std::string& customSystemsDirectory {Utils::FileSystem::getHomePath() + + "/.emulationstation/custom_systems"}; if (!Utils::FileSystem::exists(customSystemsDirectory)) { LOG(LogInfo) << "Creating custom systems directory \"" << customSystemsDirectory << "\"..."; @@ -766,7 +767,7 @@ std::vector SystemData::getConfigPath(bool legacyWarning) if (Utils::FileSystem::exists(path)) { LOG(LogInfo) << "Found custom systems configuration file"; - paths.push_back(path); + paths.emplace_back(path); } #if defined(_WIN64) @@ -777,14 +778,14 @@ std::vector SystemData::getConfigPath(bool legacyWarning) path = ResourceManager::getInstance().getResourcePath(":/systems/unix/es_systems.xml", true); #endif - paths.push_back(path); + paths.emplace_back(path); return paths; } bool SystemData::createSystemDirectories() { - std::vector configPaths = getConfigPath(true); - const std::string rompath = FileData::getROMDirectory(); + std::vector configPaths {getConfigPath(true)}; + const std::string& rompath {FileData::getROMDirectory()}; bool onlyProcessCustomFile = false; @@ -831,13 +832,13 @@ bool SystemData::createSystemDirectories() // processing of the bundled configuration file. pugi::xml_document doc; #if defined(_WIN64) - pugi::xml_parse_result res = - doc.load_file(Utils::String::stringToWideString(configPaths.front()).c_str()); + const pugi::xml_parse_result& res { + doc.load_file(Utils::String::stringToWideString(configPaths.front()).c_str())}; #else - pugi::xml_parse_result res = doc.load_file(configPaths.front().c_str()); + const pugi::xml_parse_result& res {doc.load_file(configPaths.front().c_str())}; #endif if (res) { - pugi::xml_node loadExclusive = doc.child("loadExclusive"); + const pugi::xml_node& loadExclusive {doc.child("loadExclusive")}; if (loadExclusive) onlyProcessCustomFile = true; } @@ -849,7 +850,7 @@ bool SystemData::createSystemDirectories() std::vector> systemsVector; - for (auto configPath : configPaths) { + for (auto& configPath : configPaths) { // If the loadExclusive tag is present. if (onlyProcessCustomFile && configPath == configPaths.front()) continue; @@ -863,10 +864,10 @@ bool SystemData::createSystemDirectories() pugi::xml_document doc; #if defined(_WIN64) - pugi::xml_parse_result res = - doc.load_file(Utils::String::stringToWideString(configPath).c_str()); + const pugi::xml_parse_result& res { + doc.load_file(Utils::String::stringToWideString(configPath).c_str())}; #else - pugi::xml_parse_result res = doc.load_file(configPath.c_str()); + const pugi::xml_parse_result& res {doc.load_file(configPath.c_str())}; #endif if (!res) { @@ -876,14 +877,14 @@ bool SystemData::createSystemDirectories() } // Actually read the file. - pugi::xml_node systemList = doc.child("systemList"); + const pugi::xml_node& systemList {doc.child("systemList")}; if (!systemList) { LOG(LogError) << "es_systems.xml is missing the tag"; return true; } - for (pugi::xml_node system = systemList.child("system"); system; + for (pugi::xml_node system {systemList.child("system")}; system; system = system.next_sibling("system")) { std::string systemDir; std::string name; @@ -893,7 +894,7 @@ bool SystemData::createSystemDirectories() std::vector commands; std::string platform; std::string themeFolder; - const std::string systemInfoFileName = "/systeminfo.txt"; + const std::string systemInfoFileName {"/systeminfo.txt"}; bool replaceInfoFile = false; std::ofstream systemInfoFile; @@ -901,9 +902,9 @@ bool SystemData::createSystemDirectories() fullname = system.child("fullname").text().get(); path = system.child("path").text().get(); extensions = system.child("extension").text().get(); - for (pugi::xml_node entry = system.child("command"); entry; + for (pugi::xml_node entry {system.child("command")}; entry; entry = entry.next_sibling("command")) { - commands.push_back(entry.text().get()); + commands.emplace_back(entry.text().get()); } platform = Utils::String::toLower(system.child("platform").text().get()); themeFolder = system.child("theme").text().as_string(name.c_str()); @@ -1000,9 +1001,10 @@ bool SystemData::createSystemDirectories() systemsVector.erase(systemIter); if (configPaths.size() != 1 && configPath == configPaths.back()) - systemsVector.push_back(std::make_pair(systemDir + " (custom system)", fullname)); + systemsVector.emplace_back( + std::make_pair(systemDir + " (custom system)", fullname)); else - systemsVector.push_back(std::make_pair(systemDir, fullname)); + systemsVector.emplace_back(std::make_pair(systemDir, fullname)); if (replaceInfoFile) { LOG(LogInfo) << "Replaced existing system information file \"" @@ -1019,8 +1021,8 @@ bool SystemData::createSystemDirectories() // mappings between the system directory names and the full system names. This makes it // easier for the users to identify the correct directories for their games. if (!systemsVector.empty()) { - const std::string systemsFileName = "/systems.txt"; - bool systemsFileSuccess = true; + const std::string& systemsFileName {"/systems.txt"}; + bool systemsFileSuccess {true}; if (Utils::FileSystem::exists(rompath + systemsFileName)) { if (Utils::FileSystem::removeFile(rompath + systemsFileName)) @@ -1133,7 +1135,7 @@ std::string SystemData::getThemePath() const SystemData* SystemData::getRandomSystem(const SystemData* currentSystem) { - unsigned int total = 0; + int total {0}; for (auto it = sSystemVector.cbegin(); it != sSystemVector.cend(); ++it) { if ((*it)->isGameSystem()) ++total; @@ -1142,15 +1144,15 @@ SystemData* SystemData::getRandomSystem(const SystemData* currentSystem) if (total < 2) return nullptr; - SystemData* randomSystem = nullptr; + SystemData* randomSystem {nullptr}; do { // Get a random number in range. std::random_device randDev; // Mersenne Twister pseudorandom number generator. std::mt19937 engine {randDev()}; - std::uniform_int_distribution uniform_dist(0, total - 1); - int target = uniform_dist(engine); + std::uniform_int_distribution uniform_dist {0, total - 1}; + int target {uniform_dist(engine)}; for (auto it = sSystemVector.cbegin(); it != sSystemVector.cend(); ++it) { if ((*it)->isGameSystem()) { @@ -1171,8 +1173,8 @@ SystemData* SystemData::getRandomSystem(const SystemData* currentSystem) FileData* SystemData::getRandomGame(const FileData* currentGame, bool gameSelectorMode) { std::vector gameList; - bool onlyFolders = false; - bool hasFolders = false; + bool onlyFolders {false}; + bool hasFolders {false}; // If we're in the custom collection group list, then get the list of collections, // otherwise get a list of all the folder and file entries in the view. @@ -1203,7 +1205,7 @@ FileData* SystemData::getRandomGame(const FileData* currentGame, bool gameSelect // If this is a mixed view of folders and files, then remove all the folder entries // as we want to exclude them from the random selection. if (!onlyFolders && hasFolders) { - unsigned int i = 0; + unsigned int i {0}; do { if (gameList[i]->getType() == FOLDER) gameList.erase(gameList.begin() + i); @@ -1222,8 +1224,8 @@ FileData* SystemData::getRandomGame(const FileData* currentGame, bool gameSelect if (currentGame && currentGame->getType() == PLACEHOLDER) return nullptr; - unsigned int total = static_cast(gameList.size()); - int target = 0; + int total {static_cast(gameList.size())}; + int target {0}; if (total < 2) return nullptr; @@ -1233,7 +1235,7 @@ FileData* SystemData::getRandomGame(const FileData* currentGame, bool gameSelect std::random_device randDev; // Mersenne Twister pseudorandom number generator. std::mt19937 engine {randDev()}; - std::uniform_int_distribution uniform_dist(0, total - 1); + std::uniform_int_distribution uniform_dist {0, total - 1}; target = uniform_dist(engine); } while (currentGame && gameList.at(target) == currentGame); @@ -1245,7 +1247,7 @@ void SystemData::sortSystem(bool reloadGamelist, bool jumpToFirstRow) if (getName() == "recent") return; - bool favoritesSorting; + bool favoritesSorting {false}; if (this->isCustomCollection() || (this->isCollection() && this->getFullName() == "collections")) { @@ -1255,7 +1257,7 @@ void SystemData::sortSystem(bool reloadGamelist, bool jumpToFirstRow) favoritesSorting = Settings::getInstance()->getBool("FavoritesFirst"); } - FileData* rootFolder = getRootFolder(); + FileData* rootFolder {getRootFolder()}; // Assign the sort type to all grouped custom collections. if (mIsCollectionSystem && mFullName == "collections") { for (auto it = rootFolder->getChildren().begin(); // Line break. @@ -1289,7 +1291,7 @@ void SystemData::loadTheme() { mTheme = std::make_shared(); - std::string path {getThemePath()}; + const std::string& path {getThemePath()}; if (!Utils::FileSystem::exists(path)) { // No theme available for this platform. @@ -1364,7 +1366,7 @@ void SystemData::setupSystemSortType(FileData* rootFolder) { // If DefaultSortOrder is set to something, check that it is actually a valid value. if (Settings::getInstance()->getString("DefaultSortOrder") != "") { - for (unsigned int i = 0; i < FileSorts::SortTypes.size(); ++i) { + for (unsigned int i {0}; i < FileSorts::SortTypes.size(); ++i) { if (FileSorts::SortTypes.at(i).description == Settings::getInstance()->getString("DefaultSortOrder")) { rootFolder->setSortTypeString( diff --git a/es-core/src/utils/FileSystemUtil.cpp b/es-core/src/utils/FileSystemUtil.cpp index 4ac86cd9b..de2341377 100644 --- a/es-core/src/utils/FileSystemUtil.cpp +++ b/es-core/src/utils/FileSystemUtil.cpp @@ -47,14 +47,14 @@ // build environment is broken. #if defined(__unix__) #if defined(ES_INSTALL_PREFIX) -std::string installPrefix = ES_INSTALL_PREFIX; +const std::string installPrefix {ES_INSTALL_PREFIX}; #else #if defined(__linux__) -std::string installPrefix = "/usr"; +const std::string installPrefix {"/usr"}; #elif defined(__NetBSD__) -std::string installPrefix = "/usr/pkg"; +const std::string installPrefix {"/usr/pkg"}; #else -std::string installPrefix = "/usr/local"; +const std::string installPrefix {"/usr/local"}; #endif #endif #endif @@ -63,12 +63,12 @@ namespace Utils { namespace FileSystem { - static std::string homePath = ""; - static std::string exePath = ""; + static std::string homePath; + static std::string exePath; StringList getDirContent(const std::string& path, const bool recursive) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; StringList contentList; // Only parse the directory, if it's a directory. @@ -76,17 +76,19 @@ namespace Utils #if defined(_WIN64) WIN32_FIND_DATAW findData; - std::wstring wildcard = Utils::String::stringToWideString(genericPath) + L"/*"; - HANDLE hFind = FindFirstFileW(wildcard.c_str(), &findData); + const std::wstring& wildcard {Utils::String::stringToWideString(genericPath) + + L"/*"}; + const HANDLE hFind {FindFirstFileW(wildcard.c_str(), &findData)}; if (hFind != INVALID_HANDLE_VALUE) { // Loop over all files in the directory. do { - std::string name = Utils::String::wideStringToString(findData.cFileName); + const std::string& name { + Utils::String::wideStringToString(findData.cFileName)}; // Ignore "." and ".." if ((name != ".") && (name != "..")) { - std::string fullName(getGenericPath(genericPath + "/" + name)); - contentList.push_back(fullName); + const std::string& fullName {getGenericPath(genericPath + "/" + name)}; + contentList.emplace_back(fullName); if (recursive && isDirectory(fullName)) { contentList.sort(); @@ -97,18 +99,18 @@ namespace Utils FindClose(hFind); } #else - DIR* dir = opendir(genericPath.c_str()); + DIR* dir {opendir(genericPath.c_str())}; if (dir != nullptr) { struct dirent* entry; // Loop over all files in the directory. while ((entry = readdir(dir)) != nullptr) { - std::string name(entry->d_name); + const std::string& name(entry->d_name); // Ignore "." and ".." if ((name != ".") && (name != "..")) { - std::string fullName(getGenericPath(genericPath + "/" + name)); - contentList.push_back(fullName); + const std::string& fullName {getGenericPath(genericPath + "/" + name)}; + contentList.emplace_back(fullName); if (recursive && isDirectory(fullName)) { contentList.sort(); @@ -132,13 +134,13 @@ namespace Utils if (entry == std::string::npos) return files; - std::string parent {getParent(pattern)}; + const std::string& parent {getParent(pattern)}; // Don't allow wildcard matching for the parent directory. if (entry <= parent.size()) return files; - StringList dirContent {getDirContent(parent)}; + const StringList& dirContent {getDirContent(parent)}; if (dirContent.size() == 0) return files; @@ -172,19 +174,19 @@ namespace Utils StringList getPathList(const std::string& path) { StringList pathList; - std::string genericPath = getGenericPath(path); - size_t start = 0; - size_t end = 0; + const std::string& genericPath {getGenericPath(path)}; + size_t start {0}; + size_t end {0}; // Split at '/' while ((end = genericPath.find("/", start)) != std::string::npos) { if (end != start) - pathList.push_back(std::string(genericPath, start, end - start)); + pathList.emplace_back(std::string {genericPath, start, end - start}); start = end + 1; } // Add last folder / file to pathList. if (start != genericPath.size()) - pathList.push_back(std::string(genericPath, start, genericPath.size() - start)); + pathList.emplace_back(std::string {genericPath, start, genericPath.size() - start}); return pathList; } @@ -223,7 +225,7 @@ namespace Utils #else if (!homePath.length()) { - std::string envHome = getenv("HOME"); + const std::string& envHome {getenv("HOME")}; if (envHome.length()) homePath = getGenericPath(envHome); } @@ -260,17 +262,18 @@ namespace Utils // Ugly hack to compensate for the Flatpak sandbox restrictions. We traverse // this hardcoded list of paths and use the "which" command to check outside the // sandbox if the emulator binary exists. - std::string pathVariable {"/var/lib/flatpak/exports/bin:/usr/bin:/usr/local/" - "bin:/usr/local/sbin:/usr/sbin:/sbin:/bin:/usr/games:/usr/" - "local/games:/snap/bin:/var/lib/snapd/snap/bin"}; + const std::string& pathVariable { + "/var/lib/flatpak/exports/bin:/usr/bin:/usr/local/" + "bin:/usr/local/sbin:/usr/sbin:/sbin:/bin:/usr/games:/usr/" + "local/games:/snap/bin:/var/lib/snapd/snap/bin"}; - std::vector pathList { + const std::vector& pathList { Utils::String::delimitedStringToVector(pathVariable, ":")}; // Using a temporary file is the only viable solution I've found to communicate // between the sandbox and the outside world. - std::string tempFile {Utils::FileSystem::getHomePath() + "/.emulationstation/" + - ".flatpak_emulator_binary_path.tmp"}; + const std::string& tempFile {Utils::FileSystem::getHomePath() + "/.emulationstation/" + + ".flatpak_emulator_binary_path.tmp"}; std::string emulatorPath; @@ -293,9 +296,9 @@ namespace Utils return emulatorPath; #else - std::string pathVariable {std::string(getenv("PATH"))}; + const std::string& pathVariable {std::string {getenv("PATH")}}; - std::vector pathList { + const std::vector& pathList { Utils::String::delimitedStringToVector(pathVariable, ":")}; std::string pathTest; @@ -313,7 +316,7 @@ namespace Utils void setExePath(const std::string& path) { - constexpr int pathMax = 32767; + constexpr int pathMax {32767}; #if defined(_WIN64) std::wstring result(pathMax, 0); if (GetModuleFileNameW(nullptr, &result[0], pathMax) != 0) @@ -349,20 +352,22 @@ namespace Utils std::string getPreferredPath(const std::string& path) { - std::string preferredPath = path; #if defined(_WIN64) - size_t offset = std::string::npos; + std::string preferredPath {path}; + size_t offset {std::string::npos}; // Convert '/' to '\\' while ((offset = preferredPath.find('/')) != std::string::npos) preferredPath.replace(offset, 1, "\\"); +#else + const std::string& preferredPath {path}; #endif return preferredPath; } std::string getGenericPath(const std::string& path) { - std::string genericPath = path; - size_t offset = std::string::npos; + std::string genericPath {path}; + size_t offset {std::string::npos}; // Remove "\\\\?\\" if ((genericPath.find("\\\\?\\")) == 0) @@ -386,7 +391,7 @@ namespace Utils std::string getEscapedPath(const std::string& path) { - std::string escapedPath = getGenericPath(path); + std::string escapedPath {getGenericPath(path)}; #if defined(_WIN64) // Windows escapes stuff by just putting everything in quotes. @@ -396,12 +401,12 @@ namespace Utils return getPreferredPath(escapedPath); #else // Insert a backslash before most characters that would mess up a bash path. - const char* invalidChars = "\\ '\"!$^&*(){}[]?;<>"; - const char* invalidChar = invalidChars; + const char* invalidChars {"\\ '\"!$^&*(){}[]?;<>"}; + const char* invalidChar {invalidChars}; while (*invalidChar) { - size_t start = 0; - size_t offset = 0; + size_t start {0}; + size_t offset {0}; while ((offset = escapedPath.find(*invalidChar, start)) != std::string::npos) { start = offset + 1; @@ -423,17 +428,17 @@ namespace Utils if ((path[0] == ':') && (path[1] == '/')) return path; - std::string canonicalPath = exists(path) ? getAbsolutePath(path) : getGenericPath(path); + std::string canonicalPath {exists(path) ? getAbsolutePath(path) : getGenericPath(path)}; // Cleanup path. - bool scan = true; + bool scan {true}; while (scan) { - StringList pathList = getPathList(canonicalPath); + const StringList& pathList {getPathList(canonicalPath)}; canonicalPath.clear(); scan = false; - for (StringList::const_iterator it = pathList.cbegin(); it != pathList.cend(); + for (StringList::const_iterator it {pathList.cbegin()}; it != pathList.cend(); ++it) { // Ignore empty. if ((*it).empty()) @@ -458,7 +463,7 @@ namespace Utils #endif if (isSymlink(canonicalPath)) { - std::string resolved = resolveSymlink(canonicalPath); + const std::string& resolved {resolveSymlink(canonicalPath)}; if (resolved.empty()) return ""; @@ -481,8 +486,9 @@ namespace Utils std::string getAbsolutePath(const std::string& path, const std::string& base) { - std::string absolutePath = getGenericPath(path); - std::string baseVar = isAbsolute(base) ? getGenericPath(base) : getAbsolutePath(base); + const std::string& absolutePath {getGenericPath(path)}; + const std::string& baseVar {isAbsolute(base) ? getGenericPath(base) : + getAbsolutePath(base)}; return isAbsolute(absolutePath) ? absolutePath : getGenericPath(baseVar + "/" + absolutePath); @@ -490,8 +496,8 @@ namespace Utils std::string getParent(const std::string& path) { - std::string genericPath = getGenericPath(path); - size_t offset = std::string::npos; + std::string genericPath {getGenericPath(path)}; + size_t offset {std::string::npos}; // Find last '/' and erase it. if ((offset = genericPath.find_last_of('/')) != std::string::npos) @@ -503,13 +509,13 @@ namespace Utils std::string getFileName(const std::string& path) { - std::string genericPath = getGenericPath(path); - size_t offset = std::string::npos; + const std::string& genericPath {getGenericPath(path)}; + size_t offset {std::string::npos}; // Find last '/' and return the filename. if ((offset = genericPath.find_last_of('/')) != std::string::npos) return ((genericPath[offset + 1] == 0) ? "." : - std::string(genericPath, offset + 1)); + std::string {genericPath, offset + 1}); // No '/' found, entire path is a filename. return genericPath; @@ -517,8 +523,8 @@ namespace Utils std::string getStem(const std::string& path) { - std::string fileName = getFileName(path); - size_t offset = std::string::npos; + std::string fileName {getFileName(path)}; + size_t offset {std::string::npos}; // Empty fileName. if (fileName == ".") @@ -536,8 +542,8 @@ namespace Utils std::string getExtension(const std::string& path) { - std::string fileName = getFileName(path); - size_t offset = std::string::npos; + const std::string& fileName {getFileName(path)}; + size_t offset {std::string::npos}; // Empty fileName. if (fileName == ".") @@ -545,7 +551,7 @@ namespace Utils // Find last '.' and return the extension. if ((offset = fileName.find_last_of('.')) != std::string::npos) - return std::string(fileName, offset); + return std::string {fileName, offset}; // No '.' found, filename has no extension. return "."; @@ -561,9 +567,9 @@ namespace Utils const std::string& relativeTo, const bool allowHome) { - std::string genericPath = getGenericPath(path); - std::string relativeToVar = - isDirectory(relativeTo) ? getGenericPath(relativeTo) : getParent(relativeTo); + const std::string& genericPath {getGenericPath(path)}; + const std::string& relativeToVar {isDirectory(relativeTo) ? getGenericPath(relativeTo) : + getParent(relativeTo)}; // Nothing to resolve. if (!genericPath.length()) @@ -585,8 +591,8 @@ namespace Utils const std::string& relativeTo, const bool allowHome) { - bool contains = false; - std::string relativePath = removeCommonPath(path, relativeTo, contains); + bool contains {false}; + std::string relativePath {removeCommonPath(path, relativeTo, contains)}; if (contains) return ("./" + relativePath); @@ -604,9 +610,9 @@ namespace Utils const std::string& commonArg, bool& contains) { - std::string genericPath = getGenericPath(path); - std::string common = - isDirectory(commonArg) ? getGenericPath(commonArg) : getParent(commonArg); + const std::string& genericPath {getGenericPath(path)}; + const std::string& common {isDirectory(commonArg) ? getGenericPath(commonArg) : + getParent(commonArg)}; if (genericPath.find(common) == 0) { contains = true; @@ -619,7 +625,7 @@ namespace Utils std::string resolveSymlink(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; std::string resolved; #if defined(_WIN64) @@ -675,10 +681,10 @@ namespace Utils } #if defined(_WIN64) - std::ifstream sourceFile(Utils::String::stringToWideString(sourcePath).c_str(), - std::ios::binary); + std::ifstream sourceFile {Utils::String::stringToWideString(sourcePath).c_str(), + std::ios::binary}; #else - std::ifstream sourceFile(sourcePath, std::ios::binary); + std::ifstream sourceFile {sourcePath, std::ios::binary}; #endif if (sourceFile.fail()) { @@ -689,10 +695,10 @@ namespace Utils } #if defined(_WIN64) - std::ofstream targetFile(Utils::String::stringToWideString(destinationPath).c_str(), - std::ios::binary); + std::ofstream targetFile {Utils::String::stringToWideString(destinationPath).c_str(), + std::ios::binary}; #else - std::ofstream targetFile(destinationPath, std::ios::binary); + std::ofstream targetFile {destinationPath, std::ios::binary}; #endif if (targetFile.fail()) { @@ -744,7 +750,7 @@ namespace Utils bool removeFile(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; // Don't remove if it doesn't exists. if (!exists(genericPath)) @@ -798,7 +804,7 @@ namespace Utils bool createDirectory(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; if (exists(genericPath)) return true; @@ -812,7 +818,7 @@ namespace Utils #endif // Failed to create directory, try to create the parent. - std::string parent = getParent(genericPath); + const std::string& parent {getParent(genericPath)}; // Only try to create parent if it's not identical to genericPath. if (parent != genericPath) @@ -829,7 +835,7 @@ namespace Utils bool exists(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) struct stat info; @@ -846,7 +852,7 @@ namespace Utils bool driveExists(const std::string& path) { #if defined(_WIN64) - std::string genericPath = getGenericPath(path); + std::string genericPath {getGenericPath(path)}; // Try to add a dot or a backslash and a dot depending on how the drive // letter was defined by the user. if (genericPath.length() == 2 && genericPath.at(1) == ':') @@ -864,7 +870,7 @@ namespace Utils bool isAbsolute(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; #if defined(_WIN64) return ((genericPath.size() > 1) && (genericPath[1] == ':')); @@ -875,7 +881,7 @@ namespace Utils bool isRegularFile(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) struct stat info; @@ -897,7 +903,7 @@ namespace Utils bool isDirectory(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) struct stat info; @@ -919,12 +925,12 @@ namespace Utils bool isSymlink(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; #if defined(_WIN64) // Check for symlink attribute. - const DWORD Attributes = - GetFileAttributesW(Utils::String::stringToWideString(genericPath).c_str()); + const DWORD Attributes { + GetFileAttributesW(Utils::String::stringToWideString(genericPath).c_str())}; if ((Attributes != INVALID_FILE_ATTRIBUTES) && (Attributes & FILE_ATTRIBUTE_REPARSE_POINT)) return true; @@ -952,12 +958,12 @@ namespace Utils bool isHidden(const std::string& path) { - std::string genericPath = getGenericPath(path); + const std::string& genericPath {getGenericPath(path)}; #if defined(_WIN64) // Check for hidden attribute. - const DWORD Attributes = - GetFileAttributesW(Utils::String::stringToWideString(genericPath).c_str()); + const DWORD Attributes { + GetFileAttributesW(Utils::String::stringToWideString(genericPath).c_str())}; if ((Attributes != INVALID_FILE_ATTRIBUTES) && (Attributes & FILE_ATTRIBUTE_HIDDEN)) return true; #endif