Add Posix directory support.

Former-commit-id: 308d7ce06c694cb869064ab2d2a5628475febf88
This commit is contained in:
Alexandre Janniaux 2013-01-04 18:22:13 +01:00
parent 1b9a86d993
commit cf85e22c30
2 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,101 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Posix/DirectoryImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Debug.hpp>
NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent)
{
NazaraUnused(parent);
}
void NzDirectoryImpl::Close()
{
closedir(m_handle);
}
NzString NzDirectoryImpl::GetResultName() const
{
return NzString::Unicode(m_result.d_name);
}
nzUInt64 NzDirectoryImpl::GetResultSize() const
{
struct stat64 resulststat;
stat64(m_result.d_name, &resulststat);
return static_cast<nzUInt64>(resulststat.st_size);
}
bool NzDirectoryImpl::IsResultDirectory() const
{
return S_ISDIR(m_result.d_name);
}
bool NzDirectoryImpl::NextResult()
{
if (m_result = readdir64(m_handle))
return true;
else
{
if (errno != ENOENT)
NazaraError("Unable to get next result: " + NzGetLastSystemError());
return false;
}
}
bool NzDirectoryImpl::Open(const NzString& dirPath)
{
m_handle = opendir(dirPath.GetConstBuffer());
if (m_handle == NULL)
{
NazaraError("Unable to open directory: " + NzGetLastSystemError());
return false;
}
return true;
}
bool NzDirectoryImpl::Create(const NzString& dirPath)
{
mode_t permissions; // TODO: check permissions
bool success = mkdir(dirPath.GetConstBuffer(), permissions) != -1;
return success;
}
bool NzDirectoryImpl::Exists(const NzString& dirPath)
{
if (S_ISDIR(dirPath.GetConstBuffer()))
return true;
return false;
}
NzString NzDirectoryImpl::GetCurrent()
{
NzString currentPath;
char* path = new char[_PC_PATH_MAX];
if (getcwd(path, _PC_PATH_MAX))
currentPath = NzString::Unicode(path);
else
NazaraError("Unable to get current directory: " + NzGetLastSystemError());
delete[] path;
return currentPath;
}
bool NzDirectoryImpl::Remove(const NzString& dirPath)
{
bool success = rmdir(dirPath.GetConstBuffer()) != -1;
return success;
}

View File

@ -0,0 +1,48 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_DIRECTORYIMPL_HPP
#define NAZARA_DIRECTORYIMPL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/NonCopyable.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
class NzDirectory;
class NzString;
class NzDirectoryImpl : NzNonCopyable
{
public:
NzDirectoryImpl(const NzDirectory* parent);
~NzDirectoryImpl() = default;
void Close();
NzString GetResultName() const;
nzUInt64 GetResultSize() const;
bool IsResultDirectory() const;
bool NextResult();
bool Open(const NzString& dirPath);
static bool Create(const NzString& dirPath);
static bool Exists(const NzString& dirPath);
static NzString GetCurrent();
static bool Remove(const NzString& dirPath);
private:
DIR* m_handle;
dirent64* m_result;
bool m_firstCall;
};
#endif // NAZARA_DIRECTORYIMPL_HPP