Use std::filesystem instead of dirent.h

This commit is contained in:
Epicalert 2021-07-02 23:40:52 +08:00
parent 3c94c57558
commit 07ccce90d3
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD

View file

@ -1,6 +1,5 @@
#include <iostream>
#include <dirent.h>
#include <filesystem>
#include <paths.hpp>
@ -8,24 +7,14 @@
// get models from a given directory and add them to a given vector
void getModelsFromDir(std::string path, std::vector<std::string>* 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);
}
}