lokinet/llarp/fs.hpp

49 lines
1.0 KiB
C++
Raw Normal View History

2018-04-08 12:18:16 +00:00
#ifndef LLARP_FS_HPP
#define LLARP_FS_HPP
2018-08-26 12:51:22 +00:00
#include <functional>
2018-09-20 19:24:13 +00:00
2018-06-14 07:48:42 +00:00
#if defined(WIN32) || defined(_WIN32)
2018-06-14 19:22:26 +00:00
#define PATH_SEP "\\"
2018-06-14 07:48:42 +00:00
#else
2018-06-14 19:22:26 +00:00
#define PATH_SEP "/"
2018-06-14 07:48:42 +00:00
#endif
#ifdef _WIN32
2018-10-23 11:29:37 +00:00
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
2018-10-23 13:18:48 +00:00
#else
#include "filesystem.h"
namespace fs = cpp17::filesystem;
#endif
2018-09-20 19:24:13 +00:00
2018-09-17 12:02:09 +00:00
#include <dirent.h>
2018-06-14 19:28:27 +00:00
2018-08-26 12:51:22 +00:00
namespace llarp
{
namespace util
{
using PathVisitor = std::function< bool(const fs::path &) >;
using PathIter = std::function< void(const fs::path &, PathVisitor) >;
2018-09-17 12:02:09 +00:00
2018-08-26 12:51:22 +00:00
static PathIter IterDir = [](const fs::path &path, PathVisitor visit) {
2018-09-19 11:50:18 +00:00
DIR *d = opendir(path.string().c_str());
2018-09-14 15:22:44 +00:00
if(d == nullptr)
return;
struct dirent *ent = nullptr;
do
2018-08-26 12:51:22 +00:00
{
2018-09-14 15:22:44 +00:00
ent = readdir(d);
if(!ent)
break;
2018-09-14 17:46:02 +00:00
if(ent->d_name[0] == '.')
continue;
2018-09-14 15:22:44 +00:00
fs::path p = path / fs::path(ent->d_name);
2018-08-26 12:51:22 +00:00
if(!visit(p))
2018-09-14 15:22:44 +00:00
break;
} while(ent);
closedir(d);
2018-08-26 12:51:22 +00:00
};
} // namespace util
} // namespace llarp
2018-07-27 03:41:55 +00:00
#endif // end LLARP_FS_HPP