Mercurial > code
changeset 198:01294ed877f6
Directory: support of Windows complete
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 30 Nov 2013 09:41:21 +0100 |
parents | 0b029b53ff55 |
children | 08eb7b00b950 |
files | C++/Directory.cpp C++/Directory.h |
diffstat | 2 files changed, 248 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Directory.cpp Sat Nov 30 09:41:21 2013 +0100 @@ -0,0 +1,132 @@ +/* + * Directory.cpp -- open and read directories + * + * Copyright (c) 2013 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * 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 <sstream> + +#include "Directory.h" + +#if defined(_MSC_VER) +# include <Windows.h> +#endif + +#if defined(_MSC_VER) + +namespace { + +std::string systemError() +{ + LPSTR error = nullptr; + std::string errmsg = "Unknown error"; + + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&error, 0, NULL); + + if (error) { + errmsg = std::string(error); + LocalFree(error); + } + + return errmsg; +} + +} + +void Directory::systemLoad(const std::string &path, int flags) +{ + std::ostringstream oss; + HANDLE handle; + WIN32_FIND_DATA fdata; + + oss << path << "\\*"; + handle = FindFirstFile(oss.str().c_str(), &fdata); + + if (handle == nullptr) + throw std::runtime_error(systemError()); + + do { + Entry entry; + + entry.name = fdata.cFileName; + if ((flags & Directory::NotDot) && entry.name == ".") + continue; + if ((flags & Directory::NotDotDot) && entry.name == "..") + continue; + + switch (fdata.dwFileAttributes) { + case FILE_ATTRIBUTE_DIRECTORY: + entry.type = Dir; + break; + case FILE_ATTRIBUTE_NORMAL: + entry.type = File; + break; + case FILE_ATTRIBUTE_REPARSE_POINT: + entry.type = Link; + break; + default: + break; + } + + m_list.push_back(entry); + } while (FindNextFile(handle, &fdata) != 0); + + FindClose(handle); +} + +#else + +// UNIX STUFF + +#endif + +Directory::Entry::Entry() + : type(Unknown) +{ +} + +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Directory.h Sat Nov 30 09:41:21 2013 +0100 @@ -0,0 +1,116 @@ +/* + * Directory.h -- open and read directories + * + * Copyright (c) 2013 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * 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 <cstddef> +#include <string> +#include <vector> + +/** + * @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<Entry>; + +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_ +