Big f***ing cleanup part 1

This commit is contained in:
Lynix
2020-02-23 00:42:22 +01:00
parent 67d0e0a689
commit 3d22321109
178 changed files with 2190 additions and 5113 deletions

View File

@@ -1,52 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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/ConditionVariableImpl.hpp>
#include <Nazara/Core/Posix/MutexImpl.hpp>
#include <sys/time.h>
#include <time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
ConditionVariableImpl::ConditionVariableImpl()
{
pthread_cond_init(&m_cv, nullptr);
}
ConditionVariableImpl::~ConditionVariableImpl()
{
pthread_cond_destroy(&m_cv);
}
void ConditionVariableImpl::Signal()
{
pthread_cond_signal(&m_cv);
}
void ConditionVariableImpl::SignalAll()
{
pthread_cond_broadcast(&m_cv);
}
void ConditionVariableImpl::Wait(MutexImpl* mutex)
{
pthread_cond_wait(&m_cv, &mutex->m_handle);
}
bool ConditionVariableImpl::Wait(MutexImpl* mutex, UInt32 timeout)
{
// get the current time
timeval tv;
gettimeofday(&tv, nullptr);
// construct the time limit (current time + time to wait)
timespec ti;
ti.tv_nsec = (tv.tv_usec + (timeout % 1000)) * 1000000;
ti.tv_sec = tv.tv_sec + (timeout / 1000) + (ti.tv_nsec / 1000000000);
ti.tv_nsec %= 1000000000;
return pthread_cond_timedwait(&m_cv,&mutex->m_handle, &ti) != 0;
}
}

View File

@@ -1,36 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
#pragma once
#ifndef NAZARA_CONDITIONVARIABLEIMPL_HPP
#define NAZARA_CONDITIONVARIABLEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <pthread.h>
namespace Nz
{
class MutexImpl;
class ConditionVariableImpl
{
public:
ConditionVariableImpl();
~ConditionVariableImpl();
void Signal();
void SignalAll();
void Wait(MutexImpl* mutex);
bool Wait(MutexImpl* mutex, UInt32 timeout);
private:
pthread_cond_t m_cv;
};
}
#endif // NAZARA_CONDITIONVARIABLEIMPL_HPP

View File

@@ -1,120 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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/Directory.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StackArray.hpp>
#include <Nazara/Core/String.hpp>
#include <cstring>
#include <errno.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
DirectoryImpl::DirectoryImpl(const Directory* parent) :
m_parent(parent)
{
}
void DirectoryImpl::Close()
{
closedir(m_handle);
}
String DirectoryImpl::GetResultName() const
{
return m_result->d_name;
}
UInt64 DirectoryImpl::GetResultSize() const
{
String path = m_parent->GetPath();
std::size_t pathSize = path.GetSize();
std::size_t resultNameSize = std::strlen(m_result->d_name);
std::size_t fullNameSize = pathSize + 1 + resultNameSize;
StackArray<char> fullName = NazaraStackArrayNoInit(char, fullNameSize + 1);
std::memcpy(&fullName[0], path.GetConstBuffer(), pathSize * sizeof(char));
fullName[pathSize] = '/';
std::memcpy(&fullName[pathSize + 1], m_result->d_name, resultNameSize * sizeof(char));
fullName[fullNameSize] = '\0';
struct stat64 results;
stat64(fullName.data(), &results);
return results.st_size;
}
bool DirectoryImpl::IsResultDirectory() const
{
//TODO: Fix d_type handling (field can be missing or be a symbolic link, both cases which must be handled by calling stat)
return m_result->d_type == DT_DIR;
}
bool DirectoryImpl::NextResult()
{
if ((m_result = readdir64(m_handle)))
return true;
else
{
if (errno == EBADF || errno == EOVERFLOW)
NazaraError("Unable to get next result: " + Error::GetLastSystemError());
return false;
}
}
bool DirectoryImpl::Open(const String& dirPath)
{
m_handle = opendir(dirPath.GetConstBuffer());
if (!m_handle)
{
NazaraError("Unable to open directory: " + Error::GetLastSystemError());
return false;
}
return true;
}
bool DirectoryImpl::Create(const String& dirPath)
{
mode_t permissions = S_IRWXU | S_IRWXG | S_IRWXO; // 777
return mkdir(dirPath.GetConstBuffer(), permissions) != -1;
}
bool DirectoryImpl::Exists(const String& dirPath)
{
struct stat64 filestats;
if (stat64(dirPath.GetConstBuffer(), &filestats) == -1) // error
return false;
return S_ISDIR(filestats.st_mode) || S_ISREG(filestats.st_mode);
}
String DirectoryImpl::GetCurrent()
{
String currentPath;
char path[MAXPATHLEN];
if (getcwd(path, MAXPATHLEN))
currentPath = path;
else
NazaraError("Unable to get current directory: " + Error::GetLastSystemError()); // Bug: initialisation -> if no path for log !
return currentPath;
}
bool DirectoryImpl::Remove(const String& dirPath)
{
bool success = rmdir(dirPath.GetConstBuffer()) != -1;
return success;
}
}

View File

@@ -1,52 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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/Prerequisites.hpp>
#include <dirent.h>
namespace Nz
{
class Directory;
class String;
class DirectoryImpl
{
public:
DirectoryImpl(const Directory* parent);
DirectoryImpl(const DirectoryImpl&) = delete;
DirectoryImpl(DirectoryImpl&&) = delete; ///TODO
~DirectoryImpl() = default;
void Close();
String GetResultName() const;
UInt64 GetResultSize() const;
bool IsResultDirectory() const;
bool NextResult();
bool Open(const String& dirPath);
DirectoryImpl& operator=(const DirectoryImpl&) = delete;
DirectoryImpl& operator=(DirectoryImpl&&) = delete; ///TODO
static bool Create(const String& dirPath);
static bool Exists(const String& dirPath);
static String GetCurrent();
static bool Remove(const String& dirPath);
private:
const Directory* m_parent;
DIR* m_handle;
dirent64* m_result;
};
}
#endif // NAZARA_DIRECTORYIMPL_HPP

View File

@@ -13,13 +13,14 @@
namespace Nz
{
FileImpl::FileImpl(const File* parent) :
m_fileDescriptor(-1),
m_endOfFile(false),
m_endOfFileUpdated(true)
{
NazaraUnused(parent);
}
void FileImpl::Close()
FileImpl::~FileImpl()
{
if (m_fileDescriptor != -1)
close(m_fileDescriptor);
@@ -52,7 +53,7 @@ namespace Nz
return static_cast<UInt64>(position);
}
bool FileImpl::Open(const String& filePath, OpenModeFlags mode)
bool FileImpl::Open(const std::filesystem::path& filePath, OpenModeFlags mode)
{
int flags;
mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
@@ -75,10 +76,10 @@ namespace Nz
if (mode & OpenMode_Truncate)
flags |= O_TRUNC;
m_fileDescriptor = open64(filePath.GetConstBuffer(), flags, permissions);
m_fileDescriptor = open64(filePath.generic_u8string().data(), flags, permissions);
if (m_fileDescriptor == -1)
{
NazaraError("Failed to open \"" + filePath + "\" : " + Error::GetLastSystemError());
NazaraError("Failed to open \"" + filePath.generic_u8string() + "\" : " + Error::GetLastSystemError());
return false;
}
@@ -177,121 +178,4 @@ namespace Nz
return written;
}
bool FileImpl::Copy(const String& sourcePath, const String& targetPath)
{
int fd1 = open64(sourcePath.GetConstBuffer(), O_RDONLY);
if (fd1 == -1)
{
NazaraError("Fail to open input file (" + sourcePath + "): " + Error::GetLastSystemError());
return false;
}
mode_t permissions;
struct stat sb;
if (fstat(fd1, &sb) == -1) // get permission from first file
{
NazaraWarning("Could not get permissions of source file");
permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
}
else
{
permissions = sb.st_mode & ~S_IFMT; // S_IFMT: bit mask for the file type bit field -> ~S_IFMT: general permissions
}
int fd2 = open64(targetPath.GetConstBuffer(), O_WRONLY | O_TRUNC, permissions);
if (fd2 == -1)
{
NazaraError("Fail to open output file (" + targetPath + "): " + Error::GetLastSystemError()); // TODO: more info ?
close(fd1);
return false;
}
char buffer[512];
ssize_t bytes;
do
{
bytes = read(fd1,buffer,512);
if (bytes == -1)
{
close(fd1);
close(fd2);
NazaraError("An error occured from copy : " + Error::GetLastSystemError());
return false;
}
write(fd2,buffer,bytes);
}
while (bytes == 512);
close(fd1);
close(fd2);
return true;
}
bool FileImpl::Delete(const String& filePath)
{
bool success = unlink(filePath.GetConstBuffer()) != -1;
if (success)
return true;
else
{
NazaraError("Failed to delete file (" + filePath + "): " + Error::GetLastSystemError());
return false;
}
}
bool FileImpl::Exists(const String& filePath)
{
const char* path = filePath.GetConstBuffer();
if (access(path, F_OK) != -1)
return true;
return false;
}
time_t FileImpl::GetCreationTime(const String& filePath)
{
NazaraUnused(filePath);
NazaraWarning("Posix has no creation time information");
return 0;
}
time_t FileImpl::GetLastAccessTime(const String& filePath)
{
struct stat64 stats;
stat64(filePath.GetConstBuffer(), &stats);
return stats.st_atime;
}
time_t FileImpl::GetLastWriteTime(const String& filePath)
{
struct stat64 stats;
stat64(filePath.GetConstBuffer(), &stats);
return stats.st_mtime;
}
UInt64 FileImpl::GetSize(const String& filePath)
{
struct stat64 stats;
stat64(filePath.GetConstBuffer(), &stats);
return static_cast<UInt64>(stats.st_size);
}
bool FileImpl::Rename(const String& sourcePath, const String& targetPath)
{
bool success = std::rename(sourcePath.GetConstBuffer(), targetPath.GetConstBuffer()) != -1;
if (success)
return true;
else
{
NazaraError("Unable to rename file: " + Error::GetLastSystemError());
return false;
}
}
}

View File

@@ -14,6 +14,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Enums.hpp>
#include <ctime>
#include <filesystem>
namespace Nz
{
@@ -28,11 +29,10 @@ namespace Nz
FileImpl(FileImpl&&) = delete; ///TODO
~FileImpl() = default;
void Close();
bool EndOfFile() const;
void Flush();
UInt64 GetCursorPos() const;
bool Open(const String& filePath, OpenModeFlags mode);
bool Open(const std::filesystem::path& filePath, OpenModeFlags mode);
std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(CursorPosition pos, Int64 offset);
bool SetSize(UInt64 size);
@@ -41,15 +41,6 @@ namespace Nz
FileImpl& operator=(const FileImpl&) = delete;
FileImpl& operator=(FileImpl&&) = delete; ///TODO
static bool Copy(const String& sourcePath, const String& targetPath);
static bool Delete(const String& filePath);
static bool Exists(const String& filePath);
static time_t GetCreationTime(const String& filePath);
static time_t GetLastAccessTime(const String& filePath);
static time_t GetLastWriteTime(const String& filePath);
static UInt64 GetSize(const String& filePath);
static bool Rename(const String& sourcePath, const String& targetPath);
private:
int m_fileDescriptor;
mutable bool m_endOfFile;

View File

@@ -1,38 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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/MutexImpl.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
MutexImpl::MutexImpl()
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_handle, &attr);
}
MutexImpl::~MutexImpl()
{
pthread_mutex_destroy(&m_handle);
}
void MutexImpl::Lock()
{
pthread_mutex_lock(&m_handle);
}
bool MutexImpl::TryLock()
{
return pthread_mutex_trylock(&m_handle) == 0;
}
void MutexImpl::Unlock()
{
pthread_mutex_unlock(&m_handle);
}
}

View File

@@ -1,31 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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_MUTEXIMPL_HPP
#define NAZARA_MUTEXIMPL_HPP
#include <pthread.h>
namespace Nz
{
class MutexImpl
{
friend class ConditionVariableImpl;
public:
MutexImpl();
~MutexImpl();
void Lock();
bool TryLock();
void Unlock();
private:
pthread_mutex_t m_handle;
};
}
#endif // NAZARA_MUTEXIMPL_HPP

View File

@@ -1,73 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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/SemaphoreImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <time.h>
#include <sys/time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
SemaphoreImpl::SemaphoreImpl(unsigned int count)
{
if (sem_init(&m_semaphore, 0, count) != 0)
NazaraError("Failed to create semaphore: " + Error::GetLastSystemError());
}
SemaphoreImpl::~SemaphoreImpl()
{
sem_destroy(&m_semaphore);
}
unsigned int SemaphoreImpl::GetCount() const
{
int count=0;
sem_getvalue(const_cast<sem_t*>(&m_semaphore), &count);
return static_cast<unsigned int>(count);
}
void SemaphoreImpl::Post()
{
#if NAZARA_CORE_SAFE
if (sem_post(&m_semaphore)==-1)
NazaraError("Failed to release semaphore: " + Error::GetLastSystemError());
#else
sem_post(&m_semaphore);
#endif
}
void SemaphoreImpl::Wait()
{
#if NAZARA_CORE_SAFE
if (sem_wait(&m_semaphore) == -1 )
NazaraError("Failed to wait for semaphore: " + Error::GetLastSystemError());
#else
sem_wait(&m_semaphore);
#endif
}
bool SemaphoreImpl::Wait(UInt32 timeout)
{
timeval tv;
gettimeofday(&tv, nullptr);
timespec ti;
ti.tv_nsec = (tv.tv_usec + (timeout % 1000)) * 1000000;
ti.tv_sec = tv.tv_sec + (timeout / 1000) + (ti.tv_nsec / 1000000000);
ti.tv_nsec %= 1000000000;
#if NAZARA_CORE_SAFE
if (sem_timedwait(&m_semaphore, &ti) != 0)
{
NazaraError("Failed to wait for semaphore: " + Error::GetLastSystemError());
return false;
}
return true;
#else
return sem_timedwait(&m_semaphore, &ti) != 0;
#endif
}
}

View File

@@ -1,31 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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_SEMAPHOREIMPL_HPP
#define NAZARA_SEMAPHOREIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <semaphore.h>
namespace Nz
{
class SemaphoreImpl
{
public:
SemaphoreImpl(unsigned int count);
~SemaphoreImpl();
unsigned int GetCount() const;
void Post();
void Wait();
bool Wait(UInt32 timeout);
private:
sem_t m_semaphore;
};
}
#endif // NAZARA_SEMAPHOREIMPL_HPP

View File

@@ -1,78 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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/ThreadImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Functor.hpp>
#include <Nazara/Core/String.hpp>
#include <errno.h>
#include <sched.h>
#include <time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
ThreadImpl::ThreadImpl(Functor* functor)
{
int error = pthread_create(&m_handle, nullptr, &ThreadImpl::ThreadProc, functor);
if (error != 0)
NazaraInternalError("Failed to create thread: " + Error::GetLastSystemError());
}
void ThreadImpl::Detach()
{
pthread_detach(m_handle);
}
void ThreadImpl::Join()
{
pthread_join(m_handle, nullptr);
}
void ThreadImpl::SetName(const Nz::String& name)
{
#ifdef __GNUC__
pthread_setname_np(m_handle, name.GetConstBuffer());
#else
NazaraWarning("Setting thread name is not supported on this platform");
#endif
}
void ThreadImpl::SetCurrentName(const Nz::String& name)
{
#ifdef __GNUC__
pthread_setname_np(pthread_self(), name.GetConstBuffer());
#else
NazaraWarning("Setting current thread name is not supported on this platform");
#endif
}
void ThreadImpl::Sleep(UInt32 time)
{
if (time == 0)
sched_yield();
else
{
struct timespec ts;
ts.tv_sec = time / 1000;
ts.tv_nsec = (time - ts.tv_sec * 1000) * 1'000'000;
int r;
do
{
r = nanosleep(&ts, &ts);
}
while (r == -1 && errno == EINTR);
}
}
void* ThreadImpl::ThreadProc(void* userdata)
{
Functor* func = static_cast<Functor*>(userdata);
func->Run();
delete func;
return nullptr;
}
}

View File

@@ -1,42 +0,0 @@
// Copyright (C) 2015 Alexandre Janniaux
// 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_THREADIMPL_HPP
#define NAZARA_THREADIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <pthread.h>
namespace Nz
{
struct Functor;
class String;
class ThreadImpl
{
public:
ThreadImpl(Functor* threadFunc);
void Detach();
void Join();
void SetName(const Nz::String& name);
static void SetCurrentName(const Nz::String& name);
static void Sleep(UInt32 time);
private:
static void* ThreadProc(void* userdata);
pthread_t m_handle;
};
}
#endif // NAZARA_THREADIMPL_HPP