2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
|
|
|
|
#include "tsystem.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include "tfilepath_io.h"
|
|
|
|
#include "tconvert.h"
|
|
|
|
|
|
|
|
#ifndef TNZCORE_LIGHT
|
|
|
|
|
2016-03-27 13:30:32 +13:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QUuid>
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2017-05-09 00:13:29 +12:00
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QHostInfo>
|
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
#include <shlobj.h>
|
2016-05-13 22:49:17 +12:00
|
|
|
#include <shellapi.h>
|
|
|
|
#include <winnt.h>
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
inline QString toQString(const TFilePath &path) {
|
|
|
|
return QString::fromStdWString(path.getWideString());
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
int HasMainLoop = -1;
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFileStatus::TFileStatus(const TFilePath &path) {
|
|
|
|
m_fileInfo = QFileInfo(QString::fromStdWString(path.getWideString()));
|
|
|
|
m_exist = m_fileInfo.exists();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QString TFileStatus::getGroup() const {
|
|
|
|
if (!m_exist) return QString();
|
|
|
|
return m_fileInfo.group();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QString TFileStatus::getUser() const {
|
|
|
|
if (!m_exist) return QString();
|
|
|
|
return m_fileInfo.owner();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TINT64 TFileStatus::getSize() const {
|
|
|
|
if (!m_exist) return 0;
|
|
|
|
return m_fileInfo.size();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QDateTime TFileStatus::getLastAccessTime() const {
|
|
|
|
if (!m_exist) return QDateTime();
|
|
|
|
return m_fileInfo.lastRead();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QDateTime TFileStatus::getLastModificationTime() const {
|
|
|
|
if (!m_exist) return QDateTime();
|
|
|
|
return m_fileInfo.lastModified();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QDateTime TFileStatus::getCreationTime() const {
|
|
|
|
if (!m_exist) return QDateTime();
|
|
|
|
return m_fileInfo.created();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QFile::Permissions TFileStatus::getPermissions() const {
|
|
|
|
if (!m_exist) return 0;
|
|
|
|
return m_fileInfo.permissions();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TFileStatus::isDirectory() const {
|
|
|
|
if (!m_exist) return 0;
|
|
|
|
return m_fileInfo.isDir();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TFileStatus::isLink() const { return m_fileInfo.isSymLink(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::doHaveMainLoop() {
|
|
|
|
if (HasMainLoop == -1)
|
|
|
|
assert(!"you MUST call the TSystem::hasMainLoop function in the main of the program!");
|
|
|
|
return HasMainLoop == 1;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::hasMainLoop(bool state) {
|
|
|
|
assert(HasMainLoop == -1);
|
|
|
|
HasMainLoop = state ? 1 : 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QString TSystem::getHostName() { return QHostInfo::localHostName(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QString TSystem::getUserName() {
|
|
|
|
QStringList list = QProcess::systemEnvironment();
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < list.size(); j++) {
|
|
|
|
QString value = list.at(j);
|
|
|
|
QString user;
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
if (value.startsWith("USERNAME=")) user = value.right(value.size() - 9);
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
if (value.startsWith("USER=")) user = value.right(value.size() - 5);
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!user.isEmpty()) return user;
|
|
|
|
}
|
|
|
|
return QString("none");
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TSystem::getTempDir() {
|
|
|
|
return TFilePath(QDir::tempPath().toStdString());
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TSystem::getTestDir(string name) {
|
|
|
|
return TFilePath("C:") + TFilePath(name);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QString TSystem::getSystemValue(const TFilePath &name) {
|
|
|
|
QStringList strlist = toQString(name).split("\\", QString::SkipEmptyParts);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
assert(strlist.size() > 3);
|
|
|
|
assert(strlist.at(0) == "SOFTWARE");
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QSettings qs(QSettings::SystemScope, strlist.at(1), strlist.at(2));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int i;
|
|
|
|
QString varName;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
for (i = 3; i < strlist.size(); i++) {
|
|
|
|
varName += strlist.at(i);
|
|
|
|
if (i < strlist.size() - 1) varName += "//";
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return qs.value(varName).toString();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TSystem::getBinDir() {
|
|
|
|
TFilePath fp =
|
|
|
|
TFilePath(QCoreApplication::applicationFilePath().toStdString());
|
|
|
|
return fp.getParentDir();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TSystem::getDllDir() { return getBinDir(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TSystem::getUniqueFile(QString field) {
|
|
|
|
QString uuid = QUuid::createUuid()
|
|
|
|
.toString()
|
|
|
|
.replace("-", "")
|
|
|
|
.replace("{", "")
|
|
|
|
.replace("}", "")
|
|
|
|
.toLatin1()
|
|
|
|
.data();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QString path = QDir::tempPath() + QString("\\") + field + uuid;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return TFilePath(path.toStdString());
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
|
|
|
TFilePathSet getPathsToCreate(const TFilePath &path) {
|
|
|
|
TFilePathSet pathList;
|
|
|
|
if (path.isEmpty()) return pathList;
|
|
|
|
TFilePath parentDir = path;
|
|
|
|
while (!TFileStatus(parentDir).doesExist()) {
|
|
|
|
if (parentDir == parentDir.getParentDir()) return TFilePathSet();
|
|
|
|
pathList.push_back(parentDir);
|
|
|
|
parentDir = parentDir.getParentDir();
|
|
|
|
}
|
|
|
|
return pathList;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void setPathsPermissions(const TFilePathSet &pathSet,
|
|
|
|
QFile::Permissions permissions) {
|
|
|
|
TFilePathSet::const_iterator it;
|
|
|
|
for (it = pathSet.begin(); it != pathSet.end(); it++) {
|
|
|
|
QFile f(toQString(*it));
|
|
|
|
f.setPermissions(permissions);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// gestire exception
|
|
|
|
void TSystem::mkDir(const TFilePath &path) {
|
|
|
|
TFilePathSet pathSet = getPathsToCreate(path);
|
|
|
|
QString qPath = toQString(path);
|
|
|
|
assert(!qPath.contains("+"));
|
|
|
|
if (!QDir::current().mkpath(qPath))
|
|
|
|
throw TSystemException(path, "can't create folder!");
|
|
|
|
|
|
|
|
setPathsPermissions(
|
|
|
|
pathSet, QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
|
|
|
|
QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
|
|
|
|
QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
2016-06-15 18:43:10 +12:00
|
|
|
// gestire exception
|
|
|
|
void TSystem::rmDir(const TFilePath &path) {
|
|
|
|
if (!QDir(toQString(path.getParentDir()))
|
|
|
|
.rmdir(QString::fromStdString(path.getName())))
|
|
|
|
throw TSystemException(path, "can't remove folder!");
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// vinz
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
|
|
|
void rmDirTree(const QString &path) {
|
|
|
|
int i;
|
|
|
|
QFileInfoList fil = QDir(path).entryInfoList();
|
|
|
|
for (i = 0; i < fil.size(); i++) {
|
|
|
|
QFileInfo fi = fil.at(i);
|
|
|
|
if (fi.fileName() == QString(".") || fi.fileName() == QString(".."))
|
|
|
|
continue;
|
|
|
|
QString son = fi.absoluteFilePath();
|
|
|
|
if (QFileInfo(son).isDir())
|
|
|
|
rmDirTree(son);
|
|
|
|
else if (QFileInfo(son).isFile())
|
|
|
|
if (!QFile::remove(son))
|
|
|
|
throw TSystemException("can't remove file" + son.toStdString());
|
|
|
|
}
|
|
|
|
if (!QDir::current().rmdir(path))
|
|
|
|
throw TSystemException("can't remove path!");
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::rmDirTree(const TFilePath &path) { ::rmDirTree(toQString(path)); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2020-11-11 06:44:05 +13:00
|
|
|
void TSystem::copyDir(const TFilePath &dst, const TFilePath &src,
|
|
|
|
bool overwrite) {
|
2016-06-15 18:43:10 +12:00
|
|
|
QFileInfoList fil = QDir(toQString(src)).entryInfoList();
|
|
|
|
|
|
|
|
QDir::current().mkdir(toQString(dst));
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < fil.size(); i++) {
|
|
|
|
QFileInfo fi = fil.at(i);
|
|
|
|
if (fi.fileName() == QString(".") || fi.fileName() == QString(".."))
|
|
|
|
continue;
|
|
|
|
if (fi.isDir()) {
|
|
|
|
TFilePath srcDir = TFilePath(fi.filePath().toStdString());
|
|
|
|
TFilePath dstDir = dst + srcDir.getName();
|
2020-11-11 06:44:05 +13:00
|
|
|
copyDir(dstDir, srcDir, overwrite);
|
2019-06-02 03:38:45 +12:00
|
|
|
} else {
|
|
|
|
TFilePath srcFi = dst + TFilePath(fi.fileName());
|
2020-11-11 06:44:05 +13:00
|
|
|
if (overwrite && QFile::exists(toQString(srcFi)))
|
|
|
|
QFile::remove(toQString(srcFi));
|
2019-06-02 03:38:45 +12:00
|
|
|
QFile::copy(fi.filePath(), toQString(srcFi));
|
|
|
|
}
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
void TSystem::touchFile(const TFilePath &path)
|
|
|
|
{
|
|
|
|
QFile f(toQString(path));
|
|
|
|
|
|
|
|
if (!f.open(QIODevice::ReadWrite))
|
|
|
|
throw TSystemException(path, "can't touch file!");
|
|
|
|
else
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
//------------------------------------------------------------
|
|
|
|
/*
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
wstring getFormattedMessage(DWORD lastError)
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
2016-06-15 18:43:10 +12:00
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
2016-03-19 06:57:51 +13:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
lastError,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
2016-06-15 18:43:10 +12:00
|
|
|
NULL
|
2016-03-19 06:57:51 +13:00
|
|
|
);
|
|
|
|
|
|
|
|
int wSize = MultiByteToWideChar(0,0,(char*)lpMsgBuf,-1,0,0);
|
|
|
|
if(!wSize)
|
|
|
|
return wstring();
|
|
|
|
|
|
|
|
wchar_t* wBuffer = new wchar_t [wSize+1];
|
|
|
|
MultiByteToWideChar(0,0,(char*)lpMsgBuf,-1,wBuffer,wSize);
|
2016-06-15 18:43:10 +12:00
|
|
|
wBuffer[wSize]='\0';
|
2016-03-19 06:57:51 +13:00
|
|
|
wstring wmsg(wBuffer);
|
|
|
|
|
|
|
|
delete []wBuffer;
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
return wmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::copyFile(const TFilePath &dst, const TFilePath &src,
|
|
|
|
bool overwrite) {
|
|
|
|
assert(dst != TFilePath());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (dst == src) return;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2017-09-08 17:26:03 +12:00
|
|
|
// Create the containing folder before trying to copy or it will crash!
|
|
|
|
touchParentDir(dst);
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
const QString &qDst = toQString(dst);
|
|
|
|
if (overwrite && QFile::exists(qDst)) QFile::remove(qDst);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!QFile::copy(toQString(src), qDst))
|
|
|
|
throw TSystemException(dst, "can't copy file!");
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::renameFile(const TFilePath &dst, const TFilePath &src,
|
|
|
|
bool overwrite) {
|
|
|
|
assert(dst != TFilePath());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (dst == src) return;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
const QString &qDst = toQString(dst);
|
|
|
|
if (overwrite && QFile::exists(qDst)) QFile::remove(qDst);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!QFile::rename(toQString(src), qDst))
|
|
|
|
throw TSystemException(dst, "can't rename file!");
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// gestire gli errori con GetLastError?
|
|
|
|
void TSystem::deleteFile(const TFilePath &fp) {
|
2020-09-15 18:07:58 +12:00
|
|
|
if (TSystem::doesExistFileOrLevel(fp)) {
|
|
|
|
if (fp.getQString().contains("cache")) {
|
|
|
|
QFile::remove(toQString(fp));
|
|
|
|
} else if (!QFile::remove(toQString(fp)))
|
|
|
|
throw TSystemException(fp, "can't delete file!");
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::hideFile(const TFilePath &fp) {
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!SetFileAttributesW(fp.getWideString().c_str(), FILE_ATTRIBUTE_HIDDEN))
|
|
|
|
throw TSystemException(fp, "can't hide file!");
|
|
|
|
#else // MACOSX, and others
|
|
|
|
TSystem::renameFile(TFilePath(fp.getParentDir() + L"." + fp.getLevelNameW()),
|
|
|
|
fp);
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class CaselessFilepathLess final
|
2016-06-15 18:43:10 +12:00
|
|
|
: public std::binary_function<TFilePath, TFilePath, bool> {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
bool operator()(const TFilePath &a, const TFilePath &b) const {
|
2016-07-13 22:07:41 +12:00
|
|
|
// Perform case sensitive compare, fallback to case insensitive.
|
|
|
|
const wstring a_str = a.getWideString();
|
|
|
|
const wstring b_str = b.getWideString();
|
|
|
|
|
2016-08-04 19:23:36 +12:00
|
|
|
unsigned int i = 0;
|
2016-07-13 22:07:41 +12:00
|
|
|
int case_compare = -1;
|
|
|
|
while (a_str[i] || b_str[i]) {
|
|
|
|
if (a_str[i] != b_str[i]) {
|
|
|
|
const wchar_t a_wchar = towlower(a_str[i]);
|
|
|
|
const wchar_t b_wchar = towlower(b_str[i]);
|
|
|
|
if (a_wchar < b_wchar) {
|
|
|
|
return true;
|
|
|
|
} else if (a_wchar > b_wchar) {
|
|
|
|
return false;
|
|
|
|
} else if (case_compare == -1) {
|
|
|
|
case_compare = a_str[i] < b_str[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (case_compare == 1);
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
2019-05-22 19:30:39 +12:00
|
|
|
/*! return the folder path list which is readable and executable
|
2016-03-19 06:57:51 +13:00
|
|
|
*/
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::readDirectory_Dir_ReadExe(TFilePathSet &dst,
|
|
|
|
const TFilePath &path) {
|
2019-05-22 19:30:39 +12:00
|
|
|
QStringList dirItems;
|
|
|
|
readDirectory_DirItems(dirItems, path);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2019-05-22 19:30:39 +12:00
|
|
|
for (const QString &item : dirItems) {
|
|
|
|
TFilePath son = path + TFilePath(item.toStdWString());
|
|
|
|
dst.push_back(son);
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2019-05-22 19:30:39 +12:00
|
|
|
//------------------------------------------------------------
|
|
|
|
// return the folder item list which is readable and executable
|
|
|
|
// (returns only names, not full path)
|
|
|
|
void TSystem::readDirectory_DirItems(QStringList &dst, const TFilePath &path) {
|
|
|
|
if (!TFileStatus(path).isDirectory())
|
|
|
|
throw TSystemException(path, " is not a directory");
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2019-05-22 19:30:39 +12:00
|
|
|
QDir dir(toQString(path));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2019-05-22 19:30:39 +12:00
|
|
|
#ifdef _WIN32
|
|
|
|
// equivalent to sorting with QDir::LocaleAware
|
2020-01-14 18:21:32 +13:00
|
|
|
auto const strCompare = [](const QString &s1, const QString &s2) {
|
|
|
|
return QString::localeAwareCompare(s1, s2) < 0;
|
2019-05-22 19:30:39 +12:00
|
|
|
};
|
|
|
|
|
2020-01-14 18:21:32 +13:00
|
|
|
std::set<QString, decltype(strCompare)> entries(strCompare);
|
2019-05-22 19:30:39 +12:00
|
|
|
|
|
|
|
WIN32_FIND_DATA find_dir_data;
|
|
|
|
QString dir_search_path = dir.absolutePath() + "\\*";
|
|
|
|
auto addEntry = [&]() {
|
|
|
|
// QDir::NoDotAndDotDot condition
|
|
|
|
if (wcscmp(find_dir_data.cFileName, L".") != 0 &&
|
|
|
|
wcscmp(find_dir_data.cFileName, L"..") != 0) {
|
|
|
|
// QDir::AllDirs condition
|
|
|
|
if (find_dir_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
|
|
|
|
(find_dir_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == 0) {
|
|
|
|
entries.insert(QString::fromWCharArray(find_dir_data.cFileName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
HANDLE hFind =
|
|
|
|
FindFirstFile((const wchar_t *)dir_search_path.utf16(), &find_dir_data);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
addEntry();
|
|
|
|
while (FindNextFile(hFind, &find_dir_data)) addEntry();
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
2019-05-22 19:30:39 +12:00
|
|
|
for (const QString &name : entries) dst.push_back(QString(name));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2019-05-22 19:30:39 +12:00
|
|
|
#else
|
|
|
|
dst = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Readable,
|
|
|
|
QDir::Name | QDir::LocaleAware);
|
|
|
|
#endif
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
/*! to retrieve the both lists with groupFrames option = on and off.
|
|
|
|
*/
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::readDirectory(TFilePathSet &groupFpSet, TFilePathSet &allFpSet,
|
|
|
|
const TFilePath &path) {
|
|
|
|
if (!TFileStatus(path).isDirectory())
|
|
|
|
throw TSystemException(path, " is not a directory");
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::set<TFilePath, CaselessFilepathLess> fileSet_group;
|
|
|
|
std::set<TFilePath, CaselessFilepathLess> fileSet_all;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2019-05-22 19:30:39 +12:00
|
|
|
QStringList fil;
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FIND_DATA find_dir_data;
|
|
|
|
QString dir_search_path = QDir(toQString(path)).absolutePath() + "\\*";
|
|
|
|
auto addEntry = [&]() {
|
|
|
|
// QDir::NoDotAndDotDot condition
|
|
|
|
if (wcscmp(find_dir_data.cFileName, L".") != 0 &&
|
|
|
|
wcscmp(find_dir_data.cFileName, L"..") != 0) {
|
|
|
|
// QDir::Files condition
|
|
|
|
if ((find_dir_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 &&
|
|
|
|
(find_dir_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == 0) {
|
|
|
|
fil.append(QString::fromWCharArray(find_dir_data.cFileName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
HANDLE hFind =
|
|
|
|
FindFirstFile((const wchar_t *)dir_search_path.utf16(), &find_dir_data);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
addEntry();
|
|
|
|
while (FindNextFile(hFind, &find_dir_data)) addEntry();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fil = QDir(toQString(path))
|
|
|
|
.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
|
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
if (fil.size() == 0) return;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
for (int i = 0; i < fil.size(); i++) {
|
|
|
|
QString fi = fil.at(i);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath son = path + TFilePath(fi.toStdWString());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// store all file paths
|
|
|
|
fileSet_all.insert(son);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// in case of the sequencial files
|
|
|
|
if (son.getDots() == "..") son = son.withFrame();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// store the group. insersion avoids duplication of the item
|
|
|
|
fileSet_group.insert(son);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
groupFpSet.insert(groupFpSet.end(), fileSet_group.begin(),
|
|
|
|
fileSet_group.end());
|
|
|
|
allFpSet.insert(allFpSet.end(), fileSet_all.begin(), fileSet_all.end());
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
|
|
void TSystem::readDirectory(TFilePathSet &dst, const QDir &dir,
|
2016-06-15 18:43:10 +12:00
|
|
|
bool groupFrames) {
|
|
|
|
if (!(dir.exists() && QFileInfo(dir.path()).isDir()))
|
|
|
|
throw TSystemException(TFilePath(dir.path().toStdWString()),
|
|
|
|
" is not a directory");
|
2019-05-22 19:30:39 +12:00
|
|
|
QStringList entries;
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FIND_DATA find_dir_data;
|
|
|
|
QString dir_search_path = dir.absolutePath() + "\\*";
|
|
|
|
QDir::Filters filter = dir.filter();
|
2019-05-28 16:28:11 +12:00
|
|
|
|
|
|
|
// store name filters
|
|
|
|
bool hasNameFilter = false;
|
|
|
|
QList<QRegExp> nameFilters;
|
|
|
|
for (const QString &nameFilter : dir.nameFilters()) {
|
|
|
|
if (nameFilter == "*") {
|
|
|
|
hasNameFilter = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
QRegExp regExp(nameFilter);
|
|
|
|
regExp.setPatternSyntax(QRegExp::Wildcard);
|
|
|
|
nameFilters.append(regExp);
|
|
|
|
hasNameFilter = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto addEntry = [&]() {
|
2019-05-22 19:30:39 +12:00
|
|
|
// QDir::NoDotAndDotDot condition
|
|
|
|
if (wcscmp(find_dir_data.cFileName, L".") != 0 &&
|
|
|
|
wcscmp(find_dir_data.cFileName, L"..") != 0) {
|
|
|
|
// QDir::Files condition
|
|
|
|
if ((filter & QDir::Files) == 0 &&
|
|
|
|
(find_dir_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
|
|
|
return;
|
|
|
|
// QDir::Dirs condition
|
|
|
|
if ((filter & QDir::Dirs) == 0 &&
|
|
|
|
(find_dir_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
return;
|
|
|
|
// QDir::Hidden condition
|
|
|
|
if ((filter & QDir::Hidden) == 0 &&
|
|
|
|
(find_dir_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
|
|
|
|
return;
|
2019-05-28 16:28:11 +12:00
|
|
|
|
|
|
|
QString fileName = QString::fromWCharArray(find_dir_data.cFileName);
|
|
|
|
|
|
|
|
// name filter
|
|
|
|
if (hasNameFilter) {
|
|
|
|
bool matched = false;
|
|
|
|
for (const QRegExp ®Exp : nameFilters) {
|
|
|
|
if (regExp.exactMatch(fileName)) {
|
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!matched) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entries.append(fileName);
|
2019-05-22 19:30:39 +12:00
|
|
|
}
|
|
|
|
};
|
|
|
|
HANDLE hFind =
|
|
|
|
FindFirstFile((const wchar_t *)dir_search_path.utf16(), &find_dir_data);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
addEntry();
|
|
|
|
while (FindNextFile(hFind, &find_dir_data)) addEntry();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
entries = (dir.entryList(dir.filter() | QDir::NoDotAndDotDot));
|
|
|
|
#endif
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath dirPath(dir.path().toStdWString());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::set<TFilePath, CaselessFilepathLess> fpSet;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int e, eCount = entries.size();
|
|
|
|
for (e = 0; e != eCount; ++e) {
|
|
|
|
TFilePath path(dirPath + TFilePath(entries.at(e).toStdWString()));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (groupFrames && path.getDots() == "..") path = path.withFrame();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
fpSet.insert(path);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
dst.insert(dst.end(), fpSet.begin(), fpSet.end());
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::readDirectory(TFilePathSet &dst, const TFilePath &path,
|
|
|
|
bool groupFrames, bool onlyFiles,
|
|
|
|
bool getHiddenFiles) {
|
|
|
|
QDir dir(toQString(path));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QDir::Filters filters(QDir::Files);
|
|
|
|
if (!onlyFiles) filters |= QDir::Dirs;
|
|
|
|
if (getHiddenFiles) filters |= QDir::Hidden;
|
|
|
|
dir.setFilter(filters);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
readDirectory(dst, dir, groupFrames);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::readDirectory(TFilePathSet &dst, const TFilePathSet &pathSet,
|
|
|
|
bool groupFrames, bool onlyFiles,
|
|
|
|
bool getHiddenFiles) {
|
|
|
|
for (TFilePathSet::const_iterator it = pathSet.begin(); it != pathSet.end();
|
|
|
|
it++)
|
|
|
|
readDirectory(dst, *it, groupFrames, onlyFiles);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
|
|
TFilePathSet TSystem::readDirectory(const TFilePath &path, bool groupFrames,
|
2016-06-15 18:43:10 +12:00
|
|
|
bool onlyFiles, bool getHiddenFiles) {
|
|
|
|
TFilePathSet filePathSet;
|
|
|
|
readDirectory(filePathSet, path, groupFrames, onlyFiles, getHiddenFiles);
|
|
|
|
return filePathSet;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePathSet TSystem::readDirectory(const TFilePathSet &pathSet,
|
|
|
|
bool groupFrames, bool onlyFiles,
|
|
|
|
bool getHiddenFiles) {
|
|
|
|
TFilePathSet dst;
|
|
|
|
readDirectory(dst, pathSet, groupFrames, onlyFiles, getHiddenFiles);
|
|
|
|
return dst;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::readDirectoryTree(TFilePathSet &dst, const TFilePath &path,
|
|
|
|
bool groupFrames, bool onlyFiles) {
|
|
|
|
if (!TFileStatus(path).isDirectory())
|
|
|
|
throw TSystemException(path, " is not a directory");
|
|
|
|
|
|
|
|
QFileInfoList fil = QDir(toQString(path)).entryInfoList();
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < fil.size(); i++) {
|
|
|
|
QFileInfo fi = fil.at(i);
|
|
|
|
if (fi.fileName() == QString(".") || fi.fileName() == QString(".."))
|
|
|
|
continue;
|
|
|
|
TFilePath son = TFilePath(fi.filePath().toStdWString());
|
|
|
|
if (TFileStatus(son).isDirectory()) {
|
|
|
|
if (!onlyFiles) dst.push_back(son);
|
|
|
|
readDirectoryTree(dst, son, groupFrames, onlyFiles);
|
|
|
|
} else
|
|
|
|
dst.push_back(son);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::readDirectoryTree(TFilePathSet &dst, const TFilePathSet &pathSet,
|
|
|
|
bool groupFrames, bool onlyFiles) {
|
|
|
|
for (TFilePathSet::const_iterator it = pathSet.begin(); it != pathSet.end();
|
|
|
|
it++)
|
|
|
|
readDirectoryTree(dst, *it, groupFrames, onlyFiles);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePathSet TSystem::readDirectoryTree(const TFilePath &path, bool groupFrames,
|
|
|
|
bool onlyFiles) {
|
|
|
|
TFilePathSet dst;
|
|
|
|
readDirectoryTree(dst, path, groupFrames, onlyFiles);
|
|
|
|
return dst;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePathSet TSystem::readDirectoryTree(const TFilePathSet &pathSet,
|
|
|
|
bool groupFrames, bool onlyFiles) {
|
|
|
|
TFilePathSet dst;
|
|
|
|
readDirectoryTree(dst, pathSet, groupFrames, onlyFiles);
|
|
|
|
return dst;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePathSet TSystem::packLevelNames(const TFilePathSet &fps) {
|
|
|
|
std::set<TFilePath> tmpSet;
|
|
|
|
TFilePathSet::const_iterator cit;
|
|
|
|
for (cit = fps.begin(); cit != fps.end(); ++cit)
|
|
|
|
tmpSet.insert(cit->getParentDir() + cit->getLevelName());
|
|
|
|
|
|
|
|
TFilePathSet fps2;
|
|
|
|
for (std::set<TFilePath>::const_iterator c_sit = tmpSet.begin();
|
|
|
|
c_sit != tmpSet.end(); ++c_sit) {
|
|
|
|
fps2.push_back(*c_sit);
|
|
|
|
}
|
|
|
|
return fps2;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePathSet TSystem::getDisks() {
|
|
|
|
TFilePathSet filePathSet;
|
|
|
|
QFileInfoList fil = QDir::drives();
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < fil.size(); i++)
|
|
|
|
filePathSet.push_back(TFilePath(fil.at(i).filePath().toStdWString()));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return filePathSet;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class LocalThread final : public QThread {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
static LocalThread *currentThread() {
|
|
|
|
return (LocalThread *)QThread::currentThread();
|
|
|
|
}
|
|
|
|
void sleep(TINT64 delay) { msleep(delay); }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::sleep(TINT64 delay) {
|
|
|
|
LocalThread::currentThread()->sleep(delay);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int TSystem::getProcessorCount() { return QThread::idealThreadCount(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::doesExistFileOrLevel(const TFilePath &fp) {
|
|
|
|
if (TFileStatus(fp).doesExist()) return true;
|
|
|
|
|
|
|
|
if (fp.isLevelName()) {
|
|
|
|
const TFilePath &parentDir = fp.getParentDir();
|
|
|
|
if (!TFileStatus(parentDir).doesExist()) return false;
|
|
|
|
|
|
|
|
TFilePathSet files;
|
|
|
|
try {
|
|
|
|
files = TSystem::readDirectory(parentDir, false, true, true);
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
|
|
|
|
TFilePathSet::iterator it, end = files.end();
|
|
|
|
for (it = files.begin(); it != end; ++it) {
|
|
|
|
if (it->getLevelNameW() == fp.getLevelNameW()) return true;
|
2021-02-01 01:15:43 +13:00
|
|
|
// On case insensitive systems like Windows/OSX, we need to
|
|
|
|
// compare using all the same case to confirm it is unique
|
|
|
|
// We'll force this for Linux as well since the project might
|
|
|
|
// be shared on other platforms.
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (_wcsicmp(it->getLevelNameW().c_str(), fp.getLevelNameW().c_str()) ==
|
|
|
|
0)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
if (toLower(it->getLevelNameW()) == toLower(fp.getLevelNameW()))
|
|
|
|
return true;
|
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
|
|
|
} else if (fp.getType() == "psd") {
|
|
|
|
QString name(QString::fromStdWString(fp.getWideName()));
|
|
|
|
name.append(QString::fromStdString(fp.getDottedType()));
|
|
|
|
|
|
|
|
int sepPos = name.indexOf("#");
|
|
|
|
int dotPos = name.indexOf(".", sepPos);
|
|
|
|
int removeChars = dotPos - sepPos;
|
|
|
|
int doubleUnderscorePos = name.indexOf("__", sepPos);
|
|
|
|
if (doubleUnderscorePos > 0) removeChars = doubleUnderscorePos - sepPos;
|
|
|
|
|
|
|
|
name.remove(sepPos, removeChars);
|
|
|
|
|
|
|
|
TFilePath psdpath(fp.getParentDir() + TFilePath(name.toStdWString()));
|
|
|
|
if (TFileStatus(psdpath).doesExist()) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::copyFileOrLevel_throw(const TFilePath &dst,
|
|
|
|
const TFilePath &src) {
|
|
|
|
if (src.isLevelName()) {
|
|
|
|
TFilePathSet files;
|
|
|
|
files = TSystem::readDirectory(src.getParentDir(), false);
|
|
|
|
|
|
|
|
TFilePathSet::iterator it, end = files.end();
|
|
|
|
for (it = files.begin(); it != end; ++it) {
|
|
|
|
if (it->getLevelNameW() == src.getLevelNameW()) {
|
|
|
|
TFilePath src1 = *it;
|
|
|
|
TFilePath dst1 = dst.withFrame(it->getFrame());
|
|
|
|
|
|
|
|
TSystem::copyFile(dst1, src1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
TSystem::copyFile(dst, src);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::renameFileOrLevel_throw(const TFilePath &dst,
|
|
|
|
const TFilePath &src,
|
|
|
|
bool renamePalette) {
|
|
|
|
if (renamePalette && ((src.getType() == "tlv") || (src.getType() == "tzp") ||
|
|
|
|
(src.getType() == "tzu"))) {
|
|
|
|
// Special case: since renames cannot be 'grouped' in the UI, palettes are
|
|
|
|
// automatically
|
|
|
|
// renamed here if required
|
|
|
|
const char *type = (src.getType() == "tlv") ? "tpl" : "plt";
|
|
|
|
|
|
|
|
TFilePath srcpltname(src.withNoFrame().withType(type));
|
|
|
|
TFilePath dstpltname(dst.withNoFrame().withType(type));
|
|
|
|
|
|
|
|
if (TSystem::doesExistFileOrLevel(src) &&
|
|
|
|
TSystem::doesExistFileOrLevel(srcpltname))
|
|
|
|
TSystem::renameFile(dstpltname, srcpltname, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src.isLevelName()) {
|
|
|
|
TFilePathSet files;
|
|
|
|
files = TSystem::readDirectory(src.getParentDir(), false);
|
|
|
|
|
|
|
|
for (TFilePathSet::iterator it = files.begin(); it != files.end(); it++) {
|
|
|
|
if (it->getLevelName() == src.getLevelName()) {
|
|
|
|
TFilePath src1 = *it;
|
|
|
|
TFilePath dst1 = dst.withFrame(it->getFrame());
|
|
|
|
|
|
|
|
TSystem::renameFile(dst1, src1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
TSystem::renameFile(dst, src);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::removeFileOrLevel_throw(const TFilePath &fp) {
|
|
|
|
if (fp.isLevelName()) {
|
|
|
|
TFilePathSet files;
|
|
|
|
files = TSystem::readDirectory(fp.getParentDir(), false, true, true);
|
|
|
|
|
|
|
|
TFilePathSet::iterator it, end = files.end();
|
|
|
|
for (it = files.begin(); it != end; ++it) {
|
|
|
|
if (it->getLevelName() == fp.getLevelName()) TSystem::deleteFile(*it);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
TSystem::deleteFile(fp);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::hideFileOrLevel_throw(const TFilePath &fp) {
|
|
|
|
if (fp.isLevelName()) {
|
|
|
|
TFilePathSet files;
|
|
|
|
files = TSystem::readDirectory(fp.getParentDir(), false);
|
|
|
|
|
|
|
|
TFilePathSet::iterator it, end = files.end();
|
|
|
|
for (it = files.begin(); it != end; ++it) {
|
|
|
|
if (it->getLevelNameW() == fp.getLevelNameW()) TSystem::hideFile(*it);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
TSystem::hideFile(fp);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::moveFileOrLevelToRecycleBin_throw(const TFilePath &fp) {
|
|
|
|
if (fp.isLevelName()) {
|
|
|
|
TFilePathSet files;
|
|
|
|
files = TSystem::readDirectory(fp.getParentDir(), false, true, true);
|
|
|
|
|
|
|
|
TFilePathSet::iterator it, end = files.end();
|
|
|
|
for (it = files.begin(); it != end; ++it) {
|
|
|
|
if (it->getLevelNameW() == fp.getLevelNameW())
|
|
|
|
TSystem::moveFileToRecycleBin(*it);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
TSystem::moveFileToRecycleBin(fp);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::copyFileOrLevel(const TFilePath &dst, const TFilePath &src) {
|
|
|
|
try {
|
|
|
|
copyFileOrLevel_throw(dst, src);
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::renameFileOrLevel(const TFilePath &dst, const TFilePath &src,
|
|
|
|
bool renamePalette) {
|
|
|
|
try {
|
|
|
|
renameFileOrLevel_throw(dst, src, renamePalette);
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::removeFileOrLevel(const TFilePath &fp) {
|
|
|
|
try {
|
|
|
|
removeFileOrLevel_throw(fp);
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::hideFileOrLevel(const TFilePath &fp) {
|
|
|
|
try {
|
|
|
|
hideFileOrLevel_throw(fp);
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::moveFileOrLevelToRecycleBin(const TFilePath &fp) {
|
|
|
|
try {
|
|
|
|
moveFileOrLevelToRecycleBin_throw(fp);
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::touchParentDir(const TFilePath &fp) {
|
|
|
|
TFilePath parentDir = fp.getParentDir();
|
|
|
|
TFileStatus fs(parentDir);
|
|
|
|
if (fs.isDirectory())
|
|
|
|
return true;
|
|
|
|
else if (fs.doesExist())
|
|
|
|
return false;
|
|
|
|
try {
|
|
|
|
mkDir(parentDir);
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TSystem::showDocument(const TFilePath &path) {
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
int ret = (int)ShellExecuteW(0, L"open", path.getWideString().c_str(), 0, 0,
|
|
|
|
SW_SHOWNORMAL);
|
|
|
|
if (ret <= 32) {
|
|
|
|
return false;
|
|
|
|
throw TSystemException(path, "Can't open");
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
string cmd = "open ";
|
|
|
|
string thePath(::to_string(path));
|
|
|
|
UINT pos = 0, count = 0;
|
|
|
|
// string newPath;
|
|
|
|
char newPath[2048];
|
|
|
|
|
|
|
|
while (pos < thePath.size()) {
|
|
|
|
char c = thePath[pos];
|
|
|
|
if (c == ' ') newPath[count++] = '\\';
|
|
|
|
|
|
|
|
newPath[count++] = c;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
newPath[count] = 0;
|
|
|
|
|
|
|
|
cmd = cmd + string(newPath);
|
|
|
|
system(cmd.c_str());
|
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-14 11:42:58 +13:00
|
|
|
QString TSystem::findFileLocation(QStringList folderList, QString fileName) {
|
|
|
|
if (folderList.isEmpty()) return "";
|
|
|
|
|
|
|
|
QStringList::iterator it;
|
|
|
|
for (it = folderList.begin(); it != folderList.end(); it++) {
|
|
|
|
QString path = *it + "/" + fileName;
|
|
|
|
if (TSystem::doesExistFileOrLevel(TFilePath(path))) return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::sleep(TINT64 delay) { Sleep((DWORD)delay); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// gestire gli errori con GetLastError?
|
|
|
|
void TSystem::deleteFile(const TFilePath &fp) { assert(false); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TSystem::rmDirTree(const TFilePath &path) { assert(false); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // TNZCORE_LIGHT
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
|
|
|
TSystemException::TSystemException(const TFilePath &fname, int err)
|
2016-06-15 18:43:10 +12:00
|
|
|
: m_fname(fname)
|
|
|
|
, m_err(err)
|
|
|
|
, m_msg(L"")
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
{}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TSystemException::TSystemException(const TFilePath &fname,
|
|
|
|
const std::string &msg)
|
|
|
|
: m_fname(fname), m_err(-1), m_msg(::to_wstring(msg)) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
|
|
|
TSystemException::TSystemException(const TFilePath &fname, const wstring &msg)
|
2016-06-15 18:43:10 +12:00
|
|
|
: m_fname(fname), m_err(-1), m_msg(msg) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
2016-04-19 19:32:17 +12:00
|
|
|
TSystemException::TSystemException(const std::string &msg)
|
2016-06-15 18:43:10 +12:00
|
|
|
: m_fname(""), m_err(-1), m_msg(::to_wstring(msg)) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
|
|
|
TSystemException::TSystemException(const wstring &msg)
|
2016-06-15 18:43:10 +12:00
|
|
|
: m_fname(""), m_err(-1), m_msg(msg) {}
|