view C++/modules/Directory/Directory.h @ 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 83ef77841ff1
children 412c3e26bb40
line wrap: on
line source

/*
 * Directory.h -- open and read directories
 *
 * Copyright (c) 2013, 2014 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 Entry
 * @brief entry in the directory list
 */
class DirectoryEntry {
public:
	/**
	 * @enum Type
	 * @brief Describe the type of an entry
	 */
	enum {
		Unknown		= 0,
		File,
		Dir,
		Link
	};

	std::string	name;		//! name of entry (base name)
	int		type{Unknown};	//! type of file

	friend bool operator==(const DirectoryEntry &e1, const DirectoryEntry &e2);
};

/**
 * @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 std::vector<DirectoryEntry> {
public:
	/**
	 * @enum Flags
	 * @brief optional flags to read directories
	 */
	enum Flags {
		Dot	= (1 << 0),	//!< If set, lists "." too
		DotDot	= (1 << 1)	//!< If set, lists ".." too
	};

private:
	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);

	/**
	 * Get the number of entries in the directory.
	 *
	 * @return the number
	 */
	int count() const;
};

#endif // !_DIRECTORY_H_