Mercurial > code
changeset 341:83ef77841ff1
Directory: dots are not listed by default
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 30 Mar 2015 09:21:01 +0200 |
parents | 1dc2e1adf247 |
children | b32a0d29d97a 8846b46fc435 |
files | C++/modules/Directory/Directory.cpp C++/modules/Directory/Directory.h C++/tests/Directory/main.cpp |
diffstat | 3 files changed, 43 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Directory/Directory.cpp Tue Mar 24 12:47:25 2015 +0100 +++ b/C++/modules/Directory/Directory.cpp Mon Mar 30 09:21:01 2015 +0200 @@ -70,23 +70,23 @@ throw std::runtime_error(systemError()); do { - Entry entry; + DirectoryEntry entry; entry.name = fdata.cFileName; - if ((flags & Directory::NotDot) && entry.name == ".") + if (entry.name == "." && !(flags & Directory::Dot)) continue; - if ((flags & Directory::NotDotDot) && entry.name == "..") + if (entry.name == ".." && !(flags & Directory::DotDot)) continue; switch (fdata.dwFileAttributes) { case FILE_ATTRIBUTE_DIRECTORY: - entry.type = Dir; + entry.type = DirectoryEntry::Dir; break; case FILE_ATTRIBUTE_NORMAL: - entry.type = File; + entry.type = DirectoryEntry::File; break; case FILE_ATTRIBUTE_REPARSE_POINT: - entry.type = Link; + entry.type = DirectoryEntry::Link; break; default: break; @@ -112,20 +112,20 @@ Entry entry; entry.name = ent->d_name; - if ((flags & Directory::NotDot) && entry.name == ".") + if (entry.name == "." && !(flags & Directory::Dot)) continue; - if ((flags & Directory::NotDotDot) && entry.name == "..") + if (entry.name == ".." && !(flags & Directory::DotDot)) continue; switch (ent->d_type) { case DT_DIR: - entry.type = Dir; + entry.type = DirectoryEntry::Dir; break; case DT_REG: - entry.type = File; + entry.type = DirectoryEntry::File; break; case DT_LNK: - entry.type = Link; + entry.type = DirectoryEntry::Link; break; default: break; @@ -139,12 +139,7 @@ #endif -Directory::Entry::Entry() - : type(Unknown) -{ -} - -bool operator==(const Directory::Entry &e1, const Directory::Entry &e2) +bool operator==(const DirectoryEntry &e1, const DirectoryEntry &e2) { return e1.name == e2.name && e1.type == e2.type; }
--- a/C++/modules/Directory/Directory.h Tue Mar 24 12:47:25 2015 +0100 +++ b/C++/modules/Directory/Directory.h Mon Mar 30 09:21:01 2015 +0200 @@ -24,6 +24,29 @@ #include <vector> /** + * @class Entry + * @brief entry in the directory list + */ +class DirectoryEntry { +public: + /** + * @enum Type + * @brief Describe the type of an entry + */ + enum Type { + Unknown = 0, + File, + Dir, + Link + }; + + std::string name; //! name of entry (base name) + Type type{Unknown}; //! type of file + + friend bool operator==(const DirectoryEntry &e1, const DirectoryEntry &e2); +}; + +/** * @class Directory * @brief class to manipulate directories * @@ -37,35 +60,11 @@ * @brief optional flags to read directories */ enum Flags { - NotDot = (1 << 0), - NotDotDot = (1 << 1) - }; - - /** - * @enum Type - * @brief Describe the type of an entry - */ - enum Type { - Unknown = 0, - File, - Dir, - Link + Dot = (1 << 0), //!< If set, lists "." too + DotDot = (1 << 1) //!< If set, lists ".." too }; - /** - * @struct Entry - * @brief entry in the directory list - */ - struct Entry { - std::string name; //! name of entry (base name) - Type type; //! type of file - - Entry(); - - friend bool operator==(const Entry &e1, const Entry &e2); - }; - - using List = std::vector<Entry>; + using List = std::vector<DirectoryEntry>; // C++ Container compatibility using value_type = List::value_type;
--- a/C++/tests/Directory/main.cpp Tue Mar 24 12:47:25 2015 +0100 +++ b/C++/tests/Directory/main.cpp Mon Mar 30 09:21:01 2015 +0200 @@ -23,7 +23,7 @@ TEST(Filter, withDot) { try { - auto flags = Directory::NotDotDot; + int flags = Directory::Dot; Directory directory(".", flags); bool dot(false); bool dotdot(false); @@ -45,7 +45,7 @@ TEST(Filter, withDotDot) { try { - auto flags = Directory::NotDot; + int flags = Directory::DotDot; Directory directory(".", flags); bool dot(false); bool dotdot(false); @@ -67,7 +67,8 @@ TEST(Filter, withBothDots) { try { - Directory directory("."); + int flags = Directory::Dot | Directory::DotDot; + Directory directory(".", flags); bool dot(false); bool dotdot(false); @@ -88,8 +89,7 @@ TEST(Filter, withoutDots) { try { - auto flags = Directory::NotDot | Directory::NotDotDot; - Directory directory(".", flags); + Directory directory("."); bool dot(false); bool dotdot(false);