Mercurial > code
changeset 355:9f1e9c69c223
Directory:
- Update documentation
- Rename some enums
- Directory now derives from std::vector
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 28 Apr 2015 10:49:26 +0200 |
parents | 5f38366c7654 |
children | 6f0598efad07 |
files | C++/doc/Directory/Home.md C++/doc/Directory/class/Directory.md C++/doc/Directory/class/Directory/Constructor.md C++/doc/Directory/class/DirectoryEntry.md C++/doc/Directory/class/DirectoryEntry/DirectoryEntry.md C++/doc/Directory/class/DirectoryEntry/Type.md C++/modules/Directory/Directory.cpp C++/modules/Directory/Directory.h CMakeLists.txt |
diffstat | 9 files changed, 69 insertions(+), 127 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/doc/Directory/Home.md Tue Apr 28 08:38:38 2015 +0200 +++ b/C++/doc/Directory/Home.md Tue Apr 28 10:49:26 2015 +0200 @@ -6,4 +6,21 @@ ### Classes -- [Directory](Classes/Directory.md) \ No newline at end of file +- [DirectoryEntry](class/DirectoryEntry.md) +- [Directory](class/Directory.md) + +### Tutorial + +This code lists all files that are in the **/tmp** directory. + +````cpp +try { + Directory directory("/tmp"); + + for (const DirectoryEntry &entry : directory) { + std::cout << "/tmp/" << entry.name() << std::endl; + } +} catch (const std::exception &ex) { + std::cerr << "Failed to open /tmp: " << ex.what() << std::endl; +} +````
--- a/C++/doc/Directory/class/Directory.md Tue Apr 28 08:38:38 2015 +0200 +++ b/C++/doc/Directory/class/Directory.md Tue Apr 28 10:49:26 2015 +0200 @@ -2,23 +2,22 @@ The directory wrapper. +This class inherits from [std::vector][vector] so you basically just need to instanciate a +Directory object to your path and iterate over it. + ## Class ````cpp -class Directory; +class Directory : public std::vector<DirectoryEntry>; ```` -### Public member types +### Public member constants -- [Flags](Directory/Flags.md) -- [Type](Directory/Type.md) +- **Dot**: list special "." directory (default not) +- **DotDot**: list special ".." directory (default not) ### Public member functions - [(Constructor)](Directory/Constructor.md) -- [begin](Directory/begin.md) -- [cbegin](Directory/cbegin.md) -- [end](Directory/end.md) -- [cend](Directory/cend.md) -- [count](Directory/count.md) -- [operator==](Directory/OperatorEQ.md) + +[vector]: http://en.cppreference.com/w/cpp/container/vector
--- a/C++/doc/Directory/class/Directory/Constructor.md Tue Apr 28 08:38:38 2015 +0200 +++ b/C++/doc/Directory/class/Directory/Constructor.md Tue Apr 28 10:49:26 2015 +0200 @@ -19,14 +19,14 @@ ### SYNOPSIS ````cpp -Directory(const std::string &path, Flags flags = 0); +Directory(const std::string &path, int flags = 0); ```` ### ARGUMENTS - path, the path to read -- flags, optional flags (see [Flags](Flags.md)) +- flags, optional OR'ed flags (e.g Directory::Dot) ### THROWS -- [std::runtime_error](http://en.cppreference.com/w/cpp/error/runtime_error) on failures. \ No newline at end of file +- [std::runtime_error](http://en.cppreference.com/w/cpp/error/runtime_error) on failures.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/doc/Directory/class/DirectoryEntry.md Tue Apr 28 10:49:26 2015 +0200 @@ -0,0 +1,27 @@ +# DirectoryEntry + +A file or directory entry that holds several information. + +## Class + +### SYNOPSIS + +````cpp +class DirectoryEntry { +public: + enum { + Unknown, + File, + Dir, + Link + } + + std::string name; + int type{Unknown}; +}; +```` + +### FIELDS + +- **name**, the base name of the file +- **type**, the file type
--- a/C++/doc/Directory/class/DirectoryEntry/DirectoryEntry.md Tue Apr 28 08:38:38 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -# Entry - -A file or directory entry that holds several information. - -## Struct - -### SYNOPSIS - -````cpp -struct Entry { - std::string name; - Type type; -}; -```` - -### FIELDS - -- **name**, the base name of the file -- **type**, the file type (see [Type](Type.md)) \ No newline at end of file
--- a/C++/doc/Directory/class/DirectoryEntry/Type.md Tue Apr 28 08:38:38 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -# Type - -The type of a file. Not all values are supported on all systems. - -## Enum - -### SYNOPSIS - -````cpp -enum Type { - Unknown = 0, - File, - Directory, - Link -}; -```` - -### VALUES - -- **Unknown**, the entry is unknown on that system -- **File**, the entry is a standard file -- **Directory**, the entry is a directory -- **Link**, the entry is a link \ No newline at end of file
--- a/C++/modules/Directory/Directory.cpp Tue Apr 28 08:38:38 2015 +0200 +++ b/C++/modules/Directory/Directory.cpp Tue Apr 28 10:49:26 2015 +0200 @@ -55,7 +55,7 @@ return errmsg; } -} +} // !namespace void Directory::systemLoad(const std::string &path, int flags) { @@ -92,7 +92,7 @@ break; } - m_list.push_back(entry); + push_back(entry); } while (FindNextFile(handle, &fdata) != 0); FindClose(handle); @@ -131,7 +131,7 @@ break; } - m_list.push_back(entry); + push_back(entry); } closedir(dp); @@ -153,32 +153,7 @@ systemLoad(path, flags); } -Directory::List::iterator Directory::begin() -{ - return m_list.begin(); -} - -Directory::List::const_iterator Directory::cbegin() const -{ - return m_list.cbegin(); -} - -Directory::List::iterator Directory::end() -{ - return m_list.end(); -} - -Directory::List::const_iterator Directory::cend() const -{ - return m_list.cend(); -} - int Directory::count() const { - return m_list.size(); + return size(); } - -bool operator==(const Directory &d1, const Directory &d2) -{ - return d1.m_list == d2.m_list; -}
--- a/C++/modules/Directory/Directory.h Tue Apr 28 08:38:38 2015 +0200 +++ b/C++/modules/Directory/Directory.h Tue Apr 28 10:49:26 2015 +0200 @@ -33,7 +33,7 @@ * @enum Type * @brief Describe the type of an entry */ - enum Type { + enum { Unknown = 0, File, Dir, @@ -41,7 +41,7 @@ }; std::string name; //! name of entry (base name) - Type type{Unknown}; //! type of file + int type{Unknown}; //! type of file friend bool operator==(const DirectoryEntry &e1, const DirectoryEntry &e2); }; @@ -53,7 +53,7 @@ * This class allow the user to iterate directories in a for range based * loop using iterators. */ -class Directory { +class Directory : public std::vector<DirectoryEntry> { public: /** * @enum Flags @@ -64,16 +64,7 @@ DotDot = (1 << 1) //!< If set, lists ".." too }; - using List = std::vector<DirectoryEntry>; - - // C++ Container compatibility - using value_type = List::value_type; - using iterator = List::iterator; - using const_iterator = List::const_iterator; - private: - List m_list; - void systemLoad(const std::string &path, int flags); public: @@ -90,41 +81,11 @@ Directory(const std::string &path, int flags = 0); /** - * Return an iterator the beginning. - * - * @return the iterator - */ - List::iterator begin(); - - /** - * Return a const iterator the beginning. - * - * @return the iterator - */ - List::const_iterator cbegin() const; - - /** - * Return an iterator to past the end. - * - * @return the iterator - */ - List::iterator end(); - - /** - * Return a const iterator to past the end. - * - * @return the iterator - */ - List::const_iterator cend() const; - - /** * Get the number of entries in the directory. * * @return the number */ int count() const; - - friend bool operator==(const Directory &d1, const Directory &d2); }; #endif // !_DIRECTORY_H_
--- a/CMakeLists.txt Tue Apr 28 08:38:38 2015 +0200 +++ b/CMakeLists.txt Tue Apr 28 10:49:26 2015 +0200 @@ -154,6 +154,11 @@ SOURCES ${code_SOURCE_DIR}/C++/modules/Directory/Directory.cpp ${code_SOURCE_DIR}/C++/modules/Directory/Directory.h + DOCS + ${code_SOURCE_DIR}/C++/doc/Directory/Home.md + ${code_SOURCE_DIR}/C++/doc/Directory/class/Directory.md + ${code_SOURCE_DIR}/C++/doc/Directory/class/DirectoryEntry.md + ${code_SOURCE_DIR}/C++/doc/Directory/class/Directory/Constructor.md ) # ---------------------------------------------------------