/*
    PIP - Platform Independent Primitives
    Directory
    Copyright (C) 2014  Ivan Pelipenko peri4ko@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PIDIR_H
#define PIDIR_H

#include "pifile.h"
#if !defined(ANDROID)
#ifdef WINDOWS
#  undef S_IFDIR
#  undef S_IFREG
#  undef S_IFLNK
#  undef S_IFBLK
#  undef S_IFCHR
#  undef S_IFSOCK
#  define S_IFDIR  0x01
#  define S_IFREG  0x02
#  define S_IFLNK  0x04
#  define S_IFBLK  0x08
#  define S_IFCHR  0x10
#  define S_IFSOCK 0x20
#else
#  ifdef ANDROID
#    include <sys/dirent.h>
#  else
#    include <sys/dir.h>
#  endif
#  include <sys/stat.h>
#endif
#define S_IFHDN  0x40

class PIP_EXPORT PIDir
{
public:
	PIDir(const PIString & dir = PIString());
	PIDir(const PIFile & file);
	
	struct DirEntry {
		DirEntry(const PIString & name_ = PIString(), int mode_ = 0, int size_ = 0) {
			name = name_; mode = mode_; size = size_;
		}
		
		PIString name;
		int mode;
		int size;
		
		bool isDir() const {return (mode & S_IFDIR);}
		bool isFile() const {return (mode & S_IFREG);}
		bool isSymbolicLink() const {return (mode & S_IFLNK);}
		bool isBlockDevice() const {return (mode & S_IFBLK);}
		bool isCharacterDevice() const {return (mode & S_IFCHR);}
		bool isSocket() const {return (mode & S_IFSOCK);}
		bool isHidden() const {return (mode & S_IFHDN);}
	};
	
	bool isExists() const {return PIDir::isExists(path_);}
	bool isAbsolute() const;
	bool isRelative() const {return !isAbsolute();}
	
	const PIString & path() const {return path_;}
	PIString absolutePath() const;
	PIDir & cleanPath();
	PIDir cleanedPath() const {PIDir d(path_); d.cleanPath(); return d;}
	PIDir & setDir(const PIString & path);
	bool setCurrent() {return PIDir::setCurrent(path_);}
	
	PIVector<DirEntry> entries();
	
	bool make(bool withParents = true);
	bool remove() {return PIDir::remove(path_);}
	bool rename(const PIString & new_name) {if (!PIDir::rename(path_, new_name)) return false; path_ = new_name; return true;}
	PIDir & cd(const PIString & path);
	PIDir & up() {return cd("..");}
	
	bool operator ==(const PIDir & d) const;
	bool operator !=(const PIDir & d) const {return !((*this) == d);}
	
	
	static const PIChar separator;
	
	static PIDir current();
	static PIDir home();
	static PIDir temporary();
	static bool isExists(const PIString & path);
	static bool make(const PIString & path, bool withParents = true);
	static bool remove(const PIString & path) {return removeDir(path);}
	static bool rename(const PIString & path, const PIString & new_name) {return PIDir::renameDir(path, new_name);}
	static bool setCurrent(const PIString & path);
	static bool setCurrent(const PIDir & dir) {return setCurrent(dir.path_);}
	
private:
	static bool makeDir(const PIString & path);
	static bool removeDir(const PIString & path);
	static bool renameDir(const PIString & path, const PIString & new_name);
	
	PIString path_;
	
};


inline bool operator <(const PIDir::DirEntry & v0, const PIDir::DirEntry & v1) {return (v0.name < v1.name);}
inline bool operator >(const PIDir::DirEntry & v0, const PIDir::DirEntry & v1) {return (v0.name > v1.name);}
inline bool operator ==(const PIDir::DirEntry & v0, const PIDir::DirEntry & v1) {return (v0.name == v1.name);}
inline bool operator !=(const PIDir::DirEntry & v0, const PIDir::DirEntry & v1) {return (v0.name != v1.name);}

inline PICout operator <<(PICout s, const PIDir & v) {s.setControl(0, true); s << "PIDir(\"" << v.path() << "\")"; s.restoreControl(); return s;}
inline PICout operator <<(PICout s, const PIDir::DirEntry & v) {s.setControl(0, true); s << "DirEntry(\"" << v.name << "\", " << v.size << " b, " << PIString::fromNumber(v.mode, 16) << ")"; s.restoreControl(); return s;}


#endif
#endif // PIDIR_H
