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