boxscript-interperter-c/source/dirent.cpp

56 lines
1.3 KiB
C++
Raw Normal View History

2022-12-01 03:27:30 +00:00
#include "parser.hpp"
#include <dirent.h>
#include <sys/stat.h>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
std::vector<std::pair<std::string,bool>> get_entries(std::string path)
{
std::vector<std::pair<std::string,bool>> entries;
#if defined(GEKKO)
DIR* dir = opendir(path.c_str());
if(dir == NULL)
{
return entries;
}
struct dirent* entry;
entry = readdir(dir);
while(entry != NULL)
{
entries.push_back({entry->d_name,entry->d_type == DT_DIR});
entry = readdir(dir);
}
closedir(dir);
#else
for (const auto & entry : fs::directory_iterator(path))
entries.push_back({entry.path().filename().c_str(),entry.is_directory()});
#endif
return entries;
}
std::vector<std::string> get_files(std::string path)
{
std::vector<std::string> files;
for(auto f : get_entries(path))
{
if(f.first!="." && f.first != ".." && !f.second )
{
files.push_back(f.first);
}
}
return files;
}
std::vector<std::string> get_directories(std::string path)
{
std::vector<std::string> files;
for(auto f : get_entries(path))
{
if(f.first!="." && f.first != ".." && f.second )
{
files.push_back(f.first);
}
}
return files;
}