Subversion Repositories spk

Rev

Rev 125 | Rev 183 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "DirIO.h"

#include <sys/types.h>  // For stat().
#include <sys/stat.h>   // For stat().
#include <fcntl.h>
#include "File.h"
#include "File_IO.h"

#ifdef _WIN32
#include <windows.h>
#include <io.h>
#include <direct.h>
#define ACCESS _access
#else
#include <dirent.h>
#define ACCESS access
#endif

/////////////////////////////////////////
// STATIC FUNCTIONS
bool CDirIO::Exists(const Utils::String &dir)
{
        return CDirIO(dir).exists();
}


////////////////////////////////////////
// Ctor/Dtor

CDirIO::CDirIO()
{
}

CDirIO::~CDirIO()
{
}

CDirIO::CDirIO(CyString dir)
{
        SetDir(dir);
}

CDirIO::CDirIO ( CFileIO *file )
{
        SetDir(file->dir());
}

/////////////////////////////////////////////////////////
//

void CDirIO::SetDir(CyString dir)
{
        m_sCurrentDir = dir.ToString();
        m_sCurrentDir.toFilename();
}


bool CDirIO::exists() const
{
        Utils::String dir = m_sCurrentDir;

        if (dir.empty())
                return false;

        if (ACCESS(dir.c_str(), 0) == 0)
                return true;

        return false;
}

bool CDirIO::exists(const Utils::String &dir) const
{
        Utils::String d = _parseDir(dir);

        if (d.empty())
                return false;

        if (ACCESS(d.c_str(), 0) == 0)
                return true;

        return false;
}

Utils::String CDirIO::_parseDir(const Utils::String &dir) const
{
        Utils::String sDir = dir.asFilename();
        if ( !m_sCurrentDir.empty() && !sDir.isin(":") )
        {
                if ( sDir.empty() )
                        sDir = m_sCurrentDir;
                else
                        sDir = m_sCurrentDir + "/" + sDir;
        }

        return sDir.asFilename();
}

bool CDirIO::isDir(const Utils::String &sDir) const
{
        Utils::String dir = _parseDir(sDir);
        if (ACCESS(dir.c_str(), 0) != -1)
        {
                struct stat status;
                stat(dir.c_str(), &status);

                if (status.st_mode & S_IFDIR)
                        return true;
        }
        else {
                return !dir.token("/", -1).isin(".");
        }

        return false;
}

bool CDirIO::isDir() const
{
        return isDir(m_sCurrentDir);
}

bool CDirIO::isFile() const
{
        return isFile(m_sCurrentDir);
}
bool CDirIO::isFile(const Utils::String &sDir) const
{
        Utils::String dir = _parseDir(sDir);
        if (ACCESS(dir.c_str(), 0) != -1)
        {
                struct stat status;
                stat(dir.c_str(), &status);

                if (status.st_mode & S_IFDIR)
                        return false;
                else
                        return true;
        }
        else {
                return dir.token("/", -1).isin(".");
        }

        return false;
}

bool CDirIO::Create(CyString sDir)
{
        return create(sDir.ToString());
}
bool CDirIO::create() const
{
        return create(Utils::String::Null());
}
bool CDirIO::create(const Utils::String &sDir) const
{
        Utils::String dir = sDir;
        if ( dir.empty() )
                dir = m_sCurrentDir;
        dir = _parseDir(dir);

        // split up directorys
        int max = 0;
        Utils::String *dirs = dir.findReplace( "/", "\\" ).findReplace( "\\\\", "\\" ).tokenise("\\", &max );

        // check if full dir, or relative
        int start = 1;
        Utils::String curDir;
        
        if ( dirs && max )
                curDir = dirs[0];

        if ( !curDir.isin(":") )
        {
                curDir = m_sCurrentDir;
                start = 0;
        }

        // process each dir
        for ( int i = start; i < max; i++ )
        {
                if ( !curDir.empty() )
                        curDir += "/";
                curDir += dirs[i];

                // check if the directory exists
                if ( !exists(curDir) )
                {
#ifdef _WIN32
                        if ( _mkdir(curDir.c_str()) )
#else
                        if ( mkdir(curDir.c_str(), 0755) )
#endif
                        {
                                CLEANSPLIT(dirs, max);
                                return false;
                        }
                }
        }

        CLEANSPLIT(dirs, max);

        return true;
}

bool CDirIO::move(const Utils::String &sTo)
{
        Utils::String to = _parseDir(sTo);

        if (exists(to))
                return false;

        if (!rename(m_sCurrentDir.c_str(), to.c_str()))
        {
                m_sCurrentDir = to;
                return true;
        }

        return false;
}


bool CDirIO::Move(CyString sFrom, CyString sTo)
{
        Utils::String from = _parseDir(sFrom.ToString());
        Utils::String to = _parseDir(sTo.ToString());

        if ( !exists(to) )
        {
                if ( !Create(to) )
                        return false;
        }

        if ( !rename(from.c_str(), to.c_str()) )
                return true;

        return false;
}

bool CDirIO::CheckEmptyDir(CyStringList *dirList)
{
        // no pointer, most likly empty
        if ( !dirList )
                return true;
        // not found any files, most likly empty
        if ( !dirList->Count() )
                return true;
        // check for any valid files

        for ( SStringList *str = dirList->Head(); str; str = str->next )
        {
                CyString d = str->str;
                if ( d == "." || d == ".." )
                        continue;

                // found something
                return false;
        }

        return true;
}

bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList *errors)
{
        // check if the dir is empty
        CyStringList *dirList = DirList(dir);
        if ( CheckEmptyDir(dirList) )
        {
                CyString remDir = _parseDir(dir.ToString());
                #ifdef _WIN32
                if ( _rmdir(remDir.c_str()) == 0 )
                #else
                if ( rmdir(remDir.c_str()) == 0 )
                #endif
                {
                        if ( errors )
                                errors->PushBack(remDir);
                }
                return true;
        }

        // not empty
        if ( doFiles || recursive )
        {
                for ( SStringList *str = dirList->Head(); str; str = str->next )
                {
                        CyString d = str->str;
                        if ( d == "." || d == ".." )
                                continue;

                        // if its a file
                        Utils::String fullFile = (dir + "\\" + d).ToString();
                        if ( doFiles && isFile(fullFile) )
                        {
                                CyString remFile = _parseDir(fullFile);
                                if ( remove(remFile.c_str()) == 0 )
                                {
                                        if ( errors )
                                                errors->PushBack(remFile);
                                }
                        }
                        else if ( recursive && isDir(fullFile) )
                                RemoveDir(fullFile, doFiles, recursive, errors);
                }
        }

        // now check if its empty
        delete dirList;
        dirList = DirList(dir);
        if ( CheckEmptyDir(dirList) )
        {
                CyString remDir = _parseDir(dir.ToString());
                #ifdef _WIN32
                if ( _rmdir(remDir.c_str()) == 0 )
                #else
                if ( rmdir(remDir.c_str()) == 0 )
                #endif
                {
                        if ( errors )
                                errors->PushBack(remDir);
                }
                return true;
        }

        return false;
}

Utils::CStringList CDirIO::dirList(Utils::String dir, Utils::String filePattern) const
{
        Utils::CStringList files;
        dirList(files, dir, filePattern);
        return files;
}
bool CDirIO::dirList(Utils::CStringList &files, Utils::String dir, Utils::String filePattern) const
{
        dir = _parseDir(dir);
        if ( dir.empty() )
                return false;

        dir = dir.findReplace("\\", "/");
        if (filePattern.empty())
                dir += "/*";
        else
        {
                dir += "/";
                dir += filePattern;
        }
        dir = dir.findReplace("//", "/");

#ifdef _WIN32
        dir = dir.findReplace("/", "\\");

        WIN32_FIND_DATA data;
        TCHAR buf[5000];
        wsprintf(buf, L"%hs", dir.c_str());

        HANDLE h = FindFirstFile(buf, &data);
        if ( h != INVALID_HANDLE_VALUE)
        {
                std::wstring ws(data.cFileName);
                std::string s(ws.begin(), ws.end());

                Utils::String checkFile(s);
                if ( !checkFile.Compare(".") && !checkFile.Compare("..") )
                        files.pushBack(checkFile);

                while ( FindNextFile(h, &data) )
                {
                        std::wstring ws(data.cFileName);
                        std::string s(ws.begin(), ws.end());

                        Utils::String checkFile(s);
                        if ( checkFile != "." && checkFile != ".." )
                                files.pushBack(checkFile);
                }

                FindClose(h);
                return true;
        }
        return false;
#else
        DIR *dir;
        struct dirent *ent;
        if ((dir = opendir(dir.c_str())) != NULL) {
                while ((ent = readdir(dir)) != NULL) {
                        Utils::String checkFile(ent->d_name);
                        if (checkFile != "." && checkFile != "..")
                                files.pushBack(checkFile);
                }
                closedir(dir);
        }
        return true;
#endif//_WIN32

}

CyStringList *CDirIO::DirList(CyString dir, CyString filepattern)
{
        dir = _parseDir(dir.ToString());
        if ( dir.Empty() )
                return 0;

        CyStringList *files = new CyStringList;

#ifdef _WIN32
        dir = dir.FindReplace("/", "\\");
        if ( filepattern.Empty() )
                dir += "\\*";
        else
        {
                dir += "\\";
                dir += filepattern;
        }
        dir = dir.FindReplace("\\\\", "\\");

        WIN32_FIND_DATA data;
        TCHAR buf[5000];
        wsprintf(buf, L"%hs", dir.c_str());

        HANDLE h = FindFirstFile(buf, &data);
        if ( h != INVALID_HANDLE_VALUE)
        {
                CyString checkFile(data.cFileName);
                if ( !checkFile.Compare(".") && !checkFile.Compare("..") )
                        files->PushBack(checkFile);
                while ( FindNextFile(h, &data) )
                {
                        CyString checkFile(data.cFileName);
                        if ( checkFile != "." && checkFile != ".." )
                                files->PushBack(checkFile);
                }

                FindClose(h);
        }
#else

#endif//_WIN32

        return files;
}

CyString CDirIO::File(CyString filename)
{
        if (m_sCurrentDir.empty())
                return filename;

        return CyString(m_sCurrentDir) + "/" + filename;
}

Utils::String CDirIO::file(const Utils::String &filename) const
{
        if (m_sCurrentDir.empty())
                return filename;

        return m_sCurrentDir + "/" + filename;
}

Utils::String CDirIO::dir(const Utils::String &sDir) const
{
        return _parseDir(sDir);
}

const Utils::String &CDirIO::dir() const
{
        return m_sCurrentDir;
}

CyString CDirIO::Dir(CyString dir)
{
        return _parseDir(dir.ToString());
}

bool CDirIO::cd(const Utils::String &sDir)
{
        if ( m_sCurrentDir.empty() )
                m_sCurrentDir = sDir;
        else
                m_sCurrentDir = m_sCurrentDir + "/" + sDir;
        m_sCurrentDir = m_sCurrentDir.asFilename();

        return exists();
}

bool CDirIO::CreateAndChange(CyString dir)
{
        if ( Create(dir) )
                return cd(dir.ToString());
        return false;
}


Utils::String CDirIO::topDir() const
{
        if ( m_sCurrentDir.empty() )
                return Utils::String("");

        return m_sCurrentDir.token("/", -1);
}

const Utils::String &CDirIO::moveBack()
{
        m_sCurrentDir = m_sCurrentDir.tokens("/", 1, -2);
        return m_sCurrentDir;
}
Utils::String CDirIO::back() const
{
        return m_sCurrentDir.tokens("/", 1, -2);
}