changeset 199:08eb7b00b950

Directory: support for Linux and BSD complete
author David Demelier <markand@malikania.fr>
date Sat, 30 Nov 2013 09:58:48 +0100
parents 01294ed877f6
children 617459270743
files C++/Directory.cpp C++/Directory.h
diffstat 2 files changed, 188 insertions(+), 147 deletions(-) [+]
line wrap: on
line diff
--- 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 <sstream>
-
-#include "Directory.h"
-
-#if defined(_MSC_VER)
-#  include <Windows.h>
-#endif
-
-#if defined(_MSC_VER)
-
-namespace {
-
+ */
+
+#include <sstream>
+
+#include "Directory.h"
+
+#if defined(_MSC_VER)
+#  include <Windows.h>
+#else
+#  include <cstring>
+#  include <cerrno>
+
+#  include <sys/types.h>
+#  include <dirent.h>
+#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();
+}
--- 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 <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_
-
+ */
+
+#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_
+