Base: apply clang format

This commit is contained in:
wmayer
2023-11-10 18:27:44 +01:00
committed by WandererFan
parent bb333d9a74
commit 985def3416
154 changed files with 11874 additions and 9872 deletions

View File

@@ -25,18 +25,18 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <algorithm>
# include <cassert>
# include <codecvt>
# include <cstring>
# include <locale>
# if defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
# include <dirent.h>
# include <unistd.h>
# elif defined (FC_OS_WIN32)
# include <io.h>
# include <Windows.h>
# endif
#include <algorithm>
#include <cassert>
#include <codecvt>
#include <cstring>
#include <locale>
#if defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#include <dirent.h>
#include <unistd.h>
#elif defined(FC_OS_WIN32)
#include <io.h>
#include <Windows.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#endif
@@ -49,16 +49,16 @@
using namespace Base;
#ifndef R_OK
#define R_OK 4 /* Test for read permission */
#define R_OK 4 /* Test for read permission */
#endif
#ifndef W_OK
#define W_OK 2 /* Test for write permission */
#define W_OK 2 /* Test for write permission */
#endif
#ifndef X_OK
#define X_OK 1 /* Test for execute permission */
#define X_OK 1 /* Test for execute permission */
#endif
#ifndef F_OK
#define F_OK 0 /* Test for existence */
#define F_OK 0 /* Test for existence */
#endif
//**********************************************************************************
@@ -67,11 +67,11 @@ using namespace Base;
#ifdef FC_OS_WIN32
std::string ConvertFromWideString(const std::wstring& string)
{
int neededSize = WideCharToMultiByte(CP_UTF8, 0, string.c_str(), -1, 0, 0,0,0);
char * CharString = new char[static_cast<size_t>(neededSize)];
WideCharToMultiByte(CP_UTF8, 0, string.c_str(), -1, CharString, neededSize,0,0);
int neededSize = WideCharToMultiByte(CP_UTF8, 0, string.c_str(), -1, 0, 0, 0, 0);
char* CharString = new char[static_cast<size_t>(neededSize)];
WideCharToMultiByte(CP_UTF8, 0, string.c_str(), -1, CharString, neededSize, 0, 0);
std::string String(CharString);
delete [] CharString;
delete[] CharString;
CharString = NULL;
return String;
}
@@ -82,7 +82,7 @@ std::wstring ConvertToWideString(const std::string& string)
wchar_t* wideCharString = new wchar_t[static_cast<size_t>(neededSize)];
MultiByteToWideChar(CP_UTF8, 0, string.c_str(), -1, wideCharString, neededSize);
std::wstring wideString(wideCharString);
delete [] wideCharString;
delete[] wideCharString;
wideCharString = NULL;
return wideString;
}
@@ -93,38 +93,40 @@ std::wstring ConvertToWideString(const std::string& string)
// FileInfo
FileInfo::FileInfo (const char* _FileName)
FileInfo::FileInfo(const char* _FileName)
{
setFile(_FileName);
}
FileInfo::FileInfo (const std::string &_FileName)
FileInfo::FileInfo(const std::string& _FileName)
{
setFile(_FileName.c_str());
}
const std::string &FileInfo::getTempPath()
const std::string& FileInfo::getTempPath()
{
static std::string tempPath;
if (tempPath == "") {
#ifdef FC_OS_WIN32
wchar_t buf[MAX_PATH + 2];
GetTempPathW(MAX_PATH + 1,buf);
GetTempPathW(MAX_PATH + 1, buf);
int neededSize = WideCharToMultiByte(CP_UTF8, 0, buf, -1, 0, 0, 0, 0);
char* dest = new char[static_cast<size_t>(neededSize)];
WideCharToMultiByte(CP_UTF8, 0, buf, -1,dest, neededSize, 0, 0);
WideCharToMultiByte(CP_UTF8, 0, buf, -1, dest, neededSize, 0, 0);
tempPath = dest;
delete [] dest;
delete[] dest;
#else
const char* tmp = getenv("TMPDIR");
if (tmp && tmp[0] != '\0') {
tempPath = tmp;
FileInfo fi(tempPath);
if (tempPath.empty() || !fi.isDir()) // still empty or non-existent
if (tempPath.empty() || !fi.isDir()) { // still empty or non-existent
tempPath = "/tmp/";
else if (tempPath.at(tempPath.size()-1)!='/')
}
else if (tempPath.at(tempPath.size() - 1) != '/') {
tempPath.append("/");
}
}
else {
tempPath = "/tmp/";
@@ -137,28 +139,32 @@ const std::string &FileInfo::getTempPath()
std::string FileInfo::getTempFileName(const char* FileName, const char* Path)
{
//FIXME: To avoid race conditions we should rather return a file pointer
//than a file name.
// FIXME: To avoid race conditions we should rather return a file pointer
// than a file name.
#ifdef FC_OS_WIN32
wchar_t buf[MAX_PATH + 2];
// Path where the file is located
std::wstring path;
if (Path)
if (Path) {
path = ConvertToWideString(std::string(Path));
else
}
else {
path = ConvertToWideString(getTempPath());
}
// File name in the path
std::wstring file;
if (FileName)
if (FileName) {
file = ConvertToWideString(std::string(FileName));
else
}
else {
file = L"FCTempFile";
}
// this already creates the file
GetTempFileNameW(path.c_str(),file.c_str(),0,buf);
GetTempFileNameW(path.c_str(), file.c_str(), 0, buf);
DeleteFileW(buf);
return std::string(ConvertFromWideString(std::wstring(buf)));
@@ -166,10 +172,12 @@ std::string FileInfo::getTempFileName(const char* FileName, const char* Path)
std::string buf;
// Path where the file is located
if (Path)
if (Path) {
buf = Path;
else
}
else {
buf = getTempPath();
}
// File name in the path
if (FileName) {
@@ -190,7 +198,7 @@ std::string FileInfo::getTempFileName(const char* FileName, const char* Path)
if (id > -1) {
FILE* file = fdopen(id, "w");
fclose(file);
vec.pop_back(); // remove '\0'
vec.pop_back(); // remove '\0'
std::string str(vec.begin(), vec.end());
buf.swap(str);
unlink(buf.c_str());
@@ -230,25 +238,27 @@ void FileInfo::setFile(const char* name)
FileName = name;
// keep the UNC paths intact
if (FileName.substr(0,2) == std::string("\\\\"))
std::replace(FileName.begin()+2, FileName.end(), '\\', '/');
else
if (FileName.substr(0, 2) == std::string("\\\\")) {
std::replace(FileName.begin() + 2, FileName.end(), '\\', '/');
}
else {
std::replace(FileName.begin(), FileName.end(), '\\', '/');
}
}
std::string FileInfo::filePath () const
std::string FileInfo::filePath() const
{
return FileName;
}
std::string FileInfo::fileName() const
{
return FileName.substr(FileName.find_last_of('/')+1);
return FileName.substr(FileName.find_last_of('/') + 1);
}
std::string FileInfo::dirPath() const
{
std::size_t last_pos{};
std::size_t last_pos {};
std::string retval;
last_pos = FileName.find_last_of('/');
if (last_pos != std::string::npos) {
@@ -260,7 +270,7 @@ std::string FileInfo::dirPath() const
GetCurrentDirectoryW(MAX_PATH, buf);
retval = std::string(ConvertFromWideString(std::wstring(buf)));
#else
char buf[PATH_MAX+1];
char buf[PATH_MAX + 1];
const char* cwd = getcwd(buf, PATH_MAX);
retval = std::string(cwd ? cwd : ".");
#endif
@@ -273,10 +283,12 @@ std::string FileInfo::fileNamePure() const
std::string temp = fileName();
std::string::size_type pos = temp.find_last_of('.');
if (pos != std::string::npos)
return temp.substr(0,pos);
else
if (pos != std::string::npos) {
return temp.substr(0, pos);
}
else {
return temp;
}
}
std::wstring FileInfo::toStdWString() const
@@ -294,25 +306,27 @@ std::wstring FileInfo::toStdWString() const
std::string FileInfo::extension() const
{
std::string::size_type pos = FileName.find_last_of('.');
if (pos == std::string::npos)
if (pos == std::string::npos) {
return {};
return FileName.substr(pos+1);
}
return FileName.substr(pos + 1);
}
std::string FileInfo::completeExtension() const
{
std::string::size_type pos = FileName.find_first_of('.');
if (pos == std::string::npos)
if (pos == std::string::npos) {
return {};
return FileName.substr(pos+1);
}
return FileName.substr(pos + 1);
}
bool FileInfo::hasExtension(const char* Ext) const
{
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
return _stricmp(Ext, extension().c_str()) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return strcasecmp(Ext,extension().c_str()) == 0;
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return strcasecmp(Ext, extension().c_str()) == 0;
#endif
}
@@ -328,30 +342,30 @@ bool FileInfo::hasExtension(std::initializer_list<const char*> Exts) const
bool FileInfo::exists() const
{
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
return _waccess(wstr.c_str(), F_OK) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return access(FileName.c_str(), F_OK) == 0;
#endif
}
bool FileInfo::isReadable() const
{
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
return _waccess(wstr.c_str(), R_OK) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return access(FileName.c_str(), R_OK) == 0;
#endif
}
bool FileInfo::isWritable() const
{
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
return _waccess(wstr.c_str(), W_OK) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return access(FileName.c_str(), W_OK) == 0;
#endif
}
@@ -360,17 +374,20 @@ bool FileInfo::setPermissions(Permissions perms)
{
int mode = 0;
if (perms & FileInfo::ReadOnly)
if (perms & FileInfo::ReadOnly) {
mode |= S_IREAD;
if (perms & FileInfo::WriteOnly)
}
if (perms & FileInfo::WriteOnly) {
mode |= S_IWRITE;
}
if (mode == 0) // bad argument
if (mode == 0) { // bad argument
return false;
#if defined (FC_OS_WIN32)
}
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
return _wchmod(wstr.c_str(), mode) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return chmod(FileName.c_str(), mode) == 0;
#endif
}
@@ -383,12 +400,15 @@ bool FileInfo::isFile() const
std::wstring wstr = toStdWString();
FILE* fd = _wfopen(wstr.c_str(), L"rb");
bool ok = (fd != 0);
if (fd) fclose(fd);
if (fd) {
fclose(fd);
}
return ok;
#else
struct stat st;
if(stat(FileName.c_str(), &st) != 0)
if (stat(FileName.c_str(), &st) != 0) {
return false;
}
return S_ISREG(st.st_mode);
#endif
}
@@ -402,16 +422,19 @@ bool FileInfo::isDir() const
if (exists()) {
// if we can chdir then it must be a directory, otherwise we assume it
// is a file (which doesn't need to be true for any cases)
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
struct _stat st;
if (_wstat(wstr.c_str(), &st) != 0)
if (_wstat(wstr.c_str(), &st) != 0) {
return false;
}
return ((st.st_mode & _S_IFDIR) != 0);
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
struct stat st{};
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
struct stat st
{
};
if (stat(FileName.c_str(), &st) != 0) {
return false;
}
@@ -420,14 +443,15 @@ bool FileInfo::isDir() const
return false;
#endif
}
else
else {
return false;
}
// TODO: Check for valid path name
//return true;
// return true;
}
unsigned int FileInfo::size () const
unsigned int FileInfo::size() const
{
// not implemented
assert(0);
@@ -439,20 +463,21 @@ TimeInfo FileInfo::lastModified() const
TimeInfo ti = TimeInfo::null();
if (exists()) {
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
struct _stat st;
if (_wstat(wstr.c_str(), &st) == 0) {
ti.setTime_t(st.st_mtime);
}
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
struct stat st{};
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
struct stat st
{
};
if (stat(FileName.c_str(), &st) == 0) {
ti.setTime_t(st.st_mtime);
}
#endif
}
return ti;
}
@@ -462,47 +487,48 @@ TimeInfo FileInfo::lastRead() const
TimeInfo ti = TimeInfo::null();
if (exists()) {
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
struct _stat st;
if (_wstat(wstr.c_str(), &st) == 0) {
ti.setTime_t(st.st_atime);
}
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
struct stat st{};
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
struct stat st
{
};
if (stat(FileName.c_str(), &st) == 0) {
ti.setTime_t(st.st_atime);
}
#endif
}
return ti;
}
bool FileInfo::deleteFile() const
{
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
return ::_wremove(wstr.c_str()) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return (::remove(FileName.c_str())==0);
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return (::remove(FileName.c_str()) == 0);
#else
# error "FileInfo::deleteFile() not implemented for this platform!"
#error "FileInfo::deleteFile() not implemented for this platform!"
#endif
}
bool FileInfo::renameFile(const char* NewName)
{
bool res{};
#if defined (FC_OS_WIN32)
bool res {};
#if defined(FC_OS_WIN32)
std::wstring oldname = toStdWString();
std::wstring newname = ConvertToWideString(NewName);
res = ::_wrename(oldname.c_str(),newname.c_str()) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
res = ::rename(FileName.c_str(),NewName) == 0;
res = ::_wrename(oldname.c_str(), newname.c_str()) == 0;
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
res = ::rename(FileName.c_str(), NewName) == 0;
#else
# error "FileInfo::renameFile() not implemented for this platform!"
#error "FileInfo::renameFile() not implemented for this platform!"
#endif
if (!res) {
int code = errno;
@@ -517,11 +543,11 @@ bool FileInfo::renameFile(const char* NewName)
bool FileInfo::copyTo(const char* NewName) const
{
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring oldname = toStdWString();
std::wstring newname = ConvertToWideString(NewName);
return CopyFileW(oldname.c_str(),newname.c_str(),true) != 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return CopyFileW(oldname.c_str(), newname.c_str(), true) != 0;
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
FileInfo fi1(FileName);
FileInfo fi2(NewName);
Base::ifstream file(fi1, std::ios::in | std::ios::binary);
@@ -530,19 +556,19 @@ bool FileInfo::copyTo(const char* NewName) const
file >> copy.rdbuf();
return file.is_open() && copy.is_open();
#else
# error "FileInfo::copyTo() not implemented for this platform!"
#error "FileInfo::copyTo() not implemented for this platform!"
#endif
}
bool FileInfo::createDirectory() const
{
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
return _wmkdir(wstr.c_str()) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return mkdir(FileName.c_str(), 0777) == 0;
#else
# error "FileInfo::createDirectory() not implemented for this platform!"
#error "FileInfo::createDirectory() not implemented for this platform!"
#endif
}
@@ -550,8 +576,9 @@ bool FileInfo::createDirectories() const
{
try {
boost::filesystem::path path(stringToPath(FileName));
if (boost::filesystem::exists(path))
if (boost::filesystem::exists(path)) {
return true;
}
boost::filesystem::create_directories(path);
return true;
}
@@ -562,22 +589,24 @@ bool FileInfo::createDirectories() const
bool FileInfo::deleteDirectory() const
{
if (!isDir())
if (!isDir()) {
return false;
#if defined (FC_OS_WIN32)
}
#if defined(FC_OS_WIN32)
std::wstring wstr = toStdWString();
return _wrmdir(wstr.c_str()) == 0;
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
return rmdir(FileName.c_str()) == 0;
#else
# error "FileInfo::rmdir() not implemented for this platform!"
#error "FileInfo::rmdir() not implemented for this platform!"
#endif
}
bool FileInfo::deleteDirectoryRecursive() const
{
if (!isDir())
if (!isDir()) {
return false;
}
std::vector<Base::FileInfo> List = getDirectoryContent();
for (Base::FileInfo& fi : List) {
@@ -595,7 +624,8 @@ bool FileInfo::deleteDirectoryRecursive() const
fi.deleteFile();
}
else {
throw Base::FileException("FileInfo::deleteDirectoryRecursive(): Unknown object Type in directory!");
throw Base::FileException(
"FileInfo::deleteDirectoryRecursive(): Unknown object Type in directory!");
}
}
return deleteDirectory();
@@ -604,7 +634,7 @@ bool FileInfo::deleteDirectoryRecursive() const
std::vector<Base::FileInfo> FileInfo::getDirectoryContent() const
{
std::vector<Base::FileInfo> List;
#if defined (FC_OS_WIN32)
#if defined(FC_OS_WIN32)
struct _wfinddata_t dentry;
intptr_t hFile;
@@ -613,32 +643,35 @@ std::vector<Base::FileInfo> FileInfo::getDirectoryContent() const
std::wstring wstr = toStdWString();
wstr += L"/*";
if ((hFile = _wfindfirst( wstr.c_str(), &dentry)) == -1L)
if ((hFile = _wfindfirst(wstr.c_str(), &dentry)) == -1L) {
return List;
}
while (_wfindnext(hFile, &dentry) == 0)
if (wcscmp(dentry.name,L"..") != 0)
List.push_back(FileInfo(FileName + "/" +ConvertFromWideString(std::wstring(dentry.name))));
while (_wfindnext(hFile, &dentry) == 0) {
if (wcscmp(dentry.name, L"..") != 0) {
List.push_back(
FileInfo(FileName + "/" + ConvertFromWideString(std::wstring(dentry.name))));
}
}
_findclose(hFile);
#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#elif defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
DIR* dp(nullptr);
struct dirent* dentry(nullptr);
if (!(dp = opendir(FileName.c_str())))
{
if (!(dp = opendir(FileName.c_str()))) {
return List;
}
while ((dentry = readdir(dp)))
{
while ((dentry = readdir(dp))) {
std::string dir = dentry->d_name;
if (dir != "." && dir != "..")
if (dir != "." && dir != "..") {
List.emplace_back(FileName + "/" + dir);
}
}
closedir(dp);
#else
# error "FileInfo::getDirectoryContent() not implemented for this platform!"
#error "FileInfo::getDirectoryContent() not implemented for this platform!"
#endif
return List;
}