#include "Filelist.h"
#include <algorithm>
#ifdef WIN32
# include <io.h>
#else
# include <dirent.h>
# include <cstring>
#endif
using std::vector;
using std::string;
void scanDirectoryAppend(const string& path, const string& ext, vector<string>& filelist)
{
#ifdef WIN32
string pathWithExt = path + "/*" + ext;
_finddata_t dir;
intptr_t fh = _findfirst(pathWithExt.c_str(), &dir);
if (fh == -1L)
{
return;
}
do
{
filelist.push_back(dir.name);
}
while (_findnext(fh, &dir) == 0);
_findclose(fh);
#else
dirent* current = 0;
DIR* dp = opendir(path.c_str());
if (!dp)
{
return;
}
int extLen = strlen(ext.c_str());
while ((current = readdir(dp)) != 0)
{
int len = strlen(current->d_name);
if (len > extLen && strncmp(current->d_name + len - extLen, ext.c_str(), extLen) == 0)
{
filelist.push_back(current->d_name);
}
}
closedir(dp);
#endif
std::sort(filelist.begin(), filelist.end());
}
void scanDirectory(const string& path, const string& ext, vector<string>& filelist)
{
filelist.clear();
scanDirectoryAppend(path, ext, filelist);
}