支持linux file:list

This commit is contained in:
xx
2024-06-16 15:26:43 +08:00
parent 282fbd9a99
commit c8f75fd8ba
2 changed files with 29 additions and 5 deletions

View File

@@ -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<std::string, bool>& list);
#endif
/**
* @brief 取扩展名
* @param path

View File

@@ -23,6 +23,9 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#ifdef _WIN32
#include <io.h>
#include <Windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#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<std::string, bool> &list)
{
list.clear();
@@ -271,10 +274,33 @@ bool ylib::file::list(const std::string &rootPath, std::map<std::string, bool> &
_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