# HG changeset patch # User David Demelier # Date 1385801928 -3600 # Node ID 08eb7b00b950e3ec61f2e65d4120997f0872c823 # Parent 01294ed877f6d185db515669e32d76eb8620bf33 Directory: support for Linux and BSD complete diff -r 01294ed877f6 -r 08eb7b00b950 C++/Directory.cpp --- a/C++/Directory.cpp Sat Nov 30 09:41:21 2013 +0100 +++ b/C++/Directory.cpp Sat Nov 30 09:58:48 2013 +0100 @@ -14,20 +14,26 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include "Directory.h" - -#if defined(_MSC_VER) -# include -#endif - -#if defined(_MSC_VER) - -namespace { - + */ + +#include + +#include "Directory.h" + +#if defined(_MSC_VER) +# include +#else +# include +# include + +# include +# include +#endif + +#if defined(_MSC_VER) + +namespace { + std::string systemError() { LPSTR error = nullptr; @@ -93,40 +99,75 @@ #else -// UNIX STUFF +void Directory::systemLoad(const std::string &path, int flags) +{ + DIR *dp; + struct dirent *ent; + + if ((dp = opendir(path.c_str())) == nullptr) + throw std::runtime_error(strerror(errno)); + + while ((ent = readdir(dp)) != nullptr) { + Entry entry; + + entry.name = ent->d_name; + if ((flags & Directory::NotDot) && entry.name == ".") + continue; + if ((flags & Directory::NotDotDot) && entry.name == "..") + continue; + + switch (ent->d_type) { + case DT_DIR: + entry.type = Dir; + break; + case DT_REG: + entry.type = File; + break; + case DT_LNK: + entry.type = Link; + break; + default: + break; + } + + m_list.push_back(entry); + } + + closedir(dp); +} #endif -Directory::Entry::Entry() - : type(Unknown) -{ +Directory::Entry::Entry() + : type(Unknown) +{ +} + +Directory::Directory() +{ +} + +Directory::Directory(const std::string &path, int flags) +{ + systemLoad(path, flags); } - -Directory::Directory() -{ -} - -Directory::Directory(const std::string &path, int flags) -{ - 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(); -} \ No newline at end of file + +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(); +} diff -r 01294ed877f6 -r 08eb7b00b950 C++/Directory.h --- a/C++/Directory.h Sat Nov 30 09:41:21 2013 +0100 +++ b/C++/Directory.h Sat Nov 30 09:58:48 2013 +0100 @@ -14,103 +14,103 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _DIRECTORY_H_ -#define _DIRECTORY_H_ - -#include -#include -#include - -/** - * @class Directory - * @brief class to manipulate directories - * - * This class allow the user to iterate directories in a for range based - * loop using iterators. - */ -class Directory { -public: - /** - * @enum Flags - * @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 - }; - - /** - * @struct Entry - * @brief entry in the directory list - */ - struct Entry { - std::string name; //! name of entry (base name) - Type type; //! type of file - - Entry(); - }; - - using List = std::vector; - -private: - List m_list; - - void systemLoad(const std::string &path, int flags); - -public: - /** - * Default constructor, does nothing. - */ - Directory(); - - /** - * Open a directory and read all its content. - * @param path the path - * @param flags the optional flags - */ - 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; -}; - -#endif // !_DIRECTORY_H_ - + */ + +#ifndef _DIRECTORY_H_ +#define _DIRECTORY_H_ + +#include +#include +#include + +/** + * @class Directory + * @brief class to manipulate directories + * + * This class allow the user to iterate directories in a for range based + * loop using iterators. + */ +class Directory { +public: + /** + * @enum Flags + * @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 + }; + + /** + * @struct Entry + * @brief entry in the directory list + */ + struct Entry { + std::string name; //! name of entry (base name) + Type type; //! type of file + + Entry(); + }; + + using List = std::vector; + +private: + List m_list; + + void systemLoad(const std::string &path, int flags); + +public: + /** + * Default constructor, does nothing. + */ + Directory(); + + /** + * Open a directory and read all its content. + * @param path the path + * @param flags the optional flags + */ + 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; +}; + +#endif // !_DIRECTORY_H_ +