Commit b33eb644 authored by nanahira's avatar nanahira

Sort files properly on Linux & Mac

parent e114c992
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#ifndef _WIN32 #ifndef _WIN32
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <vector>
#include <algorithm>
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
...@@ -109,21 +111,42 @@ public: ...@@ -109,21 +111,42 @@ public:
return MakeDir(dir); 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) { static void TraversalDir(const char* path, const std::function<void(const char*, bool)>& cb) {
DIR* dir = nullptr; DIR* dir = nullptr;
struct dirent* dirp = nullptr; struct dirent* dirp = nullptr;
if((dir = opendir(path)) == nullptr) if((dir = opendir(path)) == nullptr)
return; return;
struct stat fileStat; 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]; char fname[1024];
strcpy(fname, path); strcpy(fname, path);
strcat(fname, "/"); strcat(fname, "/");
strcat(fname, dirp->d_name); strcat(fname, dirp->d_name);
stat(fname, &fileStat); 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); 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) { 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