Files
pip/pidir.h

93 lines
2.5 KiB
C++

/*
PIP - Platform Independent Primitives
Directory
Copyright (C) 2013 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"
#include "pistring.h"
#if !defined(WINDOWS) && !defined(ANDROID)
#ifdef ANDROID
# include <sys/dirent.h>
#else
# include <sys/dir.h>
#endif
#include <sys/stat.h>
class PIP_EXPORT PIDir
{
public:
PIDir();
PIDir(const PIString & dir);
~PIDir() {close();}
struct DirEntry {
DirEntry() {;}
DirEntry(const PIString & name_, const int & mode_, const int & size_) {
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 isExists() const {return (dir_ != 0);}
bool isAbsolute() const {if (path_.size() == 0) return false; return (path_[0] == separator);}
const PIString & path() const {return path_;}
PIDir & cleanPath();
PIDir cleanedPath() {PIDir d(path_); d.cleanPath(); return d;}
PIString absolutePath();
bool mkDir(bool withParents = true);
PIVector<DirEntry> entries();
PIDir & cd(const PIString & path);
PIDir & up() {return cd("..");}
bool operator ==(const PIDir & d) const;
static const PIChar separator;
static PIDir current();
static PIDir home();
static PIDir temporary();
static bool mkDir(const PIString & path, bool withParents = true);
static bool rmDir(const PIString & path);
static bool isExists(const PIString & path) {return PIDir(path).isExists();}
//static bool rmDir(const PIString & path);
private:
bool open();
bool close();
PIString path_;
DIR * dir_;
};
#endif
#endif // PIDIR_H