diff --git a/include/util/file.h b/include/util/file.h index d80215d..9aa3e87 100644 --- a/include/util/file.h +++ b/include/util/file.h @@ -52,7 +52,6 @@ namespace ylib */ bool write(const std::string& filepath, const ylib::buffer& data); bool write(const std::string& filepath, const char* data, size_t len); -#ifdef _WIN32 /** * @brief 列出目录文件 * @param rootPath @@ -60,7 +59,6 @@ namespace ylib * @return */ bool list(const std::string& rootPath, std::map& list); -#endif /** * @brief 取扩展名 * @param path diff --git a/src/util/file.cpp b/src/util/file.cpp index 727fbce..b739c44 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -23,6 +23,9 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web #ifdef _WIN32 #include #include +#else +#include +#include #endif #include "util/strutils.h" #include "util/system.h" @@ -244,7 +247,7 @@ bool ylib::file::write(const std::string &filepath, const char *data, size_t len return f.write(data,len); } -#ifdef _WIN32 + bool ylib::file::list(const std::string &rootPath, std::map &list) { list.clear(); @@ -271,10 +274,33 @@ bool ylib::file::list(const std::string &rootPath, std::map & _findclose(handle); // 关闭搜索句柄 return true; #else - return false; + DIR* dir; + struct dirent* entry; + struct stat info; + + if ((dir = opendir(rootPath.c_str())) == NULL) + { + list.insert(std::make_pair(rootPath, false)); + return false; + } + + while ((entry = readdir(dir)) != NULL) + { + std::string fullPath = rootPath + "/" + entry->d_name; + if (stat(fullPath.c_str(), &info) != 0) + { + list.insert(std::make_pair(entry->d_name, false)); + } + else + { + bool isDirectory = S_ISDIR(info.st_mode); + list.insert(std::make_pair(entry->d_name, isDirectory)); + } + } + closedir(dir); + return true; #endif // _WIN32 } -#endif bool ylib::file::remove_dir(const std::string &dirpath #if _WIN32 , bool recycle