From 07ccce90d3f9d83c5d2fba2a0c34d564bc565d1a Mon Sep 17 00:00:00 2001 From: Epicalert Date: Fri, 2 Jul 2021 23:40:52 +0800 Subject: [PATCH] Use std::filesystem instead of dirent.h --- src/modellist.cpp | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/modellist.cpp b/src/modellist.cpp index 59bc692..b39e529 100644 --- a/src/modellist.cpp +++ b/src/modellist.cpp @@ -1,6 +1,5 @@ #include - -#include +#include #include @@ -8,24 +7,14 @@ // get models from a given directory and add them to a given vector void getModelsFromDir(std::string path, std::vector* vector) { - DIR* dir; - struct dirent* ent; + // return if directory doesnt exist + if (!std::filesystem::exists(path)) return; - if ((dir = opendir(path.c_str())) != NULL) { - while ((ent = readdir(dir)) != NULL) { - std::string filename = ent->d_name; - size_t fmaPos = filename.find(".fma"); // position of ".fma" in filename - - if (fmaPos == std::string::npos) { - // filename does not have ".fma" in it - continue; - } - - filename.erase(fmaPos); - - vector->push_back(filename); + // iterate through all items in this directory + for (auto& p: std::filesystem::directory_iterator(path)) { + if (p.path().extension() == ".fma") { + vector->push_back(p.path().stem().string()); } - closedir(dir); } }