mimic std directory_iterator and directory_entry

This commit is contained in:
qPCR4vir 2015-05-21 15:43:14 +02:00
parent 7117cc03b0
commit 0b63ca51f7

View File

@ -24,7 +24,7 @@
// http://www.boost.org/doc/libs/1_58_0/libs/filesystem/doc/index.htm --- // http://www.boost.org/doc/libs/1_58_0/libs/filesystem/doc/index.htm ---
// http://www.boost.org/doc/libs/1_34_0/libs/filesystem/doc/index.htm // http://www.boost.org/doc/libs/1_34_0/libs/filesystem/doc/index.htm
// http://www.boost.org/doc/libs/1_58_0/boost/filesystem.hpp // http://www.boost.org/doc/libs/1_58_0/boost/filesystem.hpp
// https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x --- Table 1.4. g++ C++ Technical Specifications Implementation Status
#ifndef NANA_FILESYSTEM_HPP #ifndef NANA_FILESYSTEM_HPP
#define NANA_FILESYSTEM_HPP #define NANA_FILESYSTEM_HPP
@ -49,16 +49,18 @@ namespace nana
{ {
namespace filesystem namespace filesystem
{ {
struct fileinfo using path = nana::string;
{
fileinfo();
#ifdef NANA_WINDOWS
fileinfo(const WIN32_FIND_DATA& wfd);
#elif NANA_LINUX
fileinfo(const nana::string& filename, const struct stat &);
#endif
nana::string name;
struct directory_entry
{
directory_entry();
directory_entry(const nana::string& filename, bool is_directory, unsigned long size)
:name{filename}, size{size}, directory{is_directory}
{}
const path& path() const noexcept{return name;}
//operator const path&() const noexcept;
nana::string name;
unsigned long size; unsigned long size;
bool directory; bool directory;
}; };
@ -66,10 +68,10 @@ namespace filesystem
/// an iterator for a sequence of directory_entry elements representing the files in a directory, not an recursive_directory_iterator /// an iterator for a sequence of directory_entry elements representing the files in a directory, not an recursive_directory_iterator
//template<typename FileInfo> //template<typename FileInfo>
class directory_iterator :public std::iterator<std::input_iterator_tag, fileinfo> class directory_iterator :public std::iterator<std::input_iterator_tag, directory_entry>
{ {
public: public:
typedef fileinfo value_type; using value_type = directory_entry ;
directory_iterator():end_(true), handle_(nullptr){} directory_iterator():end_(true), handle_(nullptr){}
@ -135,7 +137,11 @@ namespace filesystem
return; return;
} }
} }
value_ = value_type(wfd_);
value_ = value_type(wfd_.cFileName,
(FILE_ATTRIBUTE_DIRECTORY & wfd_.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY,
wfd_.nFileSizeLow);
#elif defined(NANA_LINUX) #elif defined(NANA_LINUX)
path_ = nana::charset(file_path); path_ = nana::charset(file_path);
if(path_.size() && (path_[path_.size() - 1] != '/')) if(path_.size() && (path_[path_.size() - 1] != '/'))
@ -160,7 +166,7 @@ namespace filesystem
struct stat fst; struct stat fst;
if(stat((path_ + dnt->d_name).c_str(), &fst) == 0) if(stat((path_ + dnt->d_name).c_str(), &fst) == 0)
{ {
value_ = value_type(nana::charset(dnt->d_name), fst); value_ = value_type(nana::charset(dnt->d_name), 0 != S_ISDIR(fst.st_mode), fst.st_size);
} }
else else
{ {
@ -194,7 +200,9 @@ namespace filesystem
return; return;
} }
} }
value_ = value_type(wfd_); value_ = value_type(wfd_.cFileName,
(FILE_ATTRIBUTE_DIRECTORY & wfd_.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY,
wfd_.nFileSizeLow);
} }
else else
end_ = true; end_ = true;
@ -213,7 +221,9 @@ namespace filesystem
} }
struct stat fst; struct stat fst;
if(stat((path_ + "/" + dnt->d_name).c_str(), &fst) == 0) if(stat((path_ + "/" + dnt->d_name).c_str(), &fst) == 0)
value_ = value_type(nana::charset(dnt->d_name), fst); value_ = value_type(wfd_.cFileName,
(FILE_ATTRIBUTE_DIRECTORY & wfd_.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY,
wfd_.nFileSizeLow);
else else
value_.name = nana::charset(dnt->d_name); value_.name = nana::charset(dnt->d_name);
} }
@ -265,7 +275,7 @@ namespace filesystem
return !x.equal(y); return !x.equal(y);
} }
//using directory_iterator = directory_iterator<fileinfo> ; //using directory_iterator = directory_iterator<directory_entry> ;
}//end namespace filesystem }//end namespace filesystem
}//end namespace nana }//end namespace nana