diff --git a/src/Nazara/Core/Posix/DirectoryImpl.cpp b/src/Nazara/Core/Posix/DirectoryImpl.cpp new file mode 100644 index 000000000..c0cce52e7 --- /dev/null +++ b/src/Nazara/Core/Posix/DirectoryImpl.cpp @@ -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 +#include +#include + + +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(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; +} diff --git a/src/Nazara/Core/Posix/DirectoryImpl.hpp b/src/Nazara/Core/Posix/DirectoryImpl.hpp new file mode 100644 index 000000000..ffffc68d7 --- /dev/null +++ b/src/Nazara/Core/Posix/DirectoryImpl.hpp @@ -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 +#include +#include +#include +#include +#include + +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