Commit e61530b2 authored by nanahira's avatar nanahira

Merge branch 'sort_files_fh' into sort_files

parents bd185a16 b33eb644
......@@ -8,6 +8,8 @@
#ifndef _WIN32
#include <dirent.h>
#include <sys/stat.h>
#include <vector>
#include <algorithm>
#endif
#ifdef _WIN32
......@@ -109,21 +111,42 @@ public:
return MakeDir(dir);
}
struct file_unit {
std::string filename;
bool is_dir;
};
static void TraversalDir(const char* path, const std::function<void(const char*, bool)>& cb) {
DIR* dir = nullptr;
struct dirent* dirp = nullptr;
if((dir = opendir(path)) == nullptr)
return;
struct stat fileStat;
while((dirp = readdir(dir)) != nullptr) {
std::vector<file_unit> file_list;
while ((dirp = readdir(dir)) != nullptr)
{
file_unit funit;
char fname[1024];
strcpy(fname, path);
strcat(fname, "/");
strcat(fname, dirp->d_name);
stat(fname, &fileStat);
cb(dirp->d_name, S_ISDIR(fileStat.st_mode));
funit.filename = std::string(dirp->d_name);
funit.is_dir = S_ISDIR(fileStat.st_mode);
file_list.push_back(funit);
}
closedir(dir);
std::sort(file_list.begin(), file_list.end(), TraversalDirSort);
for (file_unit funit : file_list)
cb(funit.filename.c_str(), funit.is_dir);
}
static bool TraversalDirSort(file_unit file1, file_unit file2) {
if(file1.is_dir != file2.is_dir) {
return file1.is_dir;
} else {
return file1.filename < file2.filename;
}
}
static void TraversalDir(const wchar_t* wpath, const std::function<void(const wchar_t*, bool)>& cb) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment