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
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <qdesktopservices></qdesktopservices>
Toshihiro Shimizu 890ddd
#include <qhostinfo></qhostinfo>
Toshihiro Shimizu 890ddd
#include <shlobj.h></shlobj.h>
Toshihiro Shimizu 890ddd
#else
Campbell Barton d0e335
#include <qdesktopservices></qdesktopservices>
Campbell Barton d0e335
#include <qhostinfo></qhostinfo>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qstringlist></qstringlist>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline QString toQString(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QString::fromStdWString(path.getWideString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int HasMainLoop = -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFileStatus::TFileStatus(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_fileInfo = QFileInfo(QString::fromStdWString(path.getWideString()));
Toshihiro Shimizu 890ddd
	m_exist = m_fileInfo.exists();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TFileStatus::getGroup() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return QString();
Toshihiro Shimizu 890ddd
	return m_fileInfo.group();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TFileStatus::getUser() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return QString();
Toshihiro Shimizu 890ddd
	return m_fileInfo.owner();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TINT64 TFileStatus::getSize() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return m_fileInfo.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QDateTime TFileStatus::getLastAccessTime() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return QDateTime();
Toshihiro Shimizu 890ddd
	return m_fileInfo.lastRead();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QDateTime TFileStatus::getLastModificationTime() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return QDateTime();
Toshihiro Shimizu 890ddd
	return m_fileInfo.lastModified();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QDateTime TFileStatus::getCreationTime() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return QDateTime();
Toshihiro Shimizu 890ddd
	return m_fileInfo.created();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QFile::Permissions TFileStatus::getPermissions() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return m_fileInfo.permissions();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFileStatus::isDirectory() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_exist)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return m_fileInfo.isDir();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFileStatus::isLink() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_fileInfo.isSymLink();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::doHaveMainLoop()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (HasMainLoop == -1)
Toshihiro Shimizu 890ddd
		assert(!"you MUST call the TSystem::hasMainLoop function in the main of the program!");
Toshihiro Shimizu 890ddd
	return HasMainLoop == 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::hasMainLoop(bool state)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(HasMainLoop == -1);
Toshihiro Shimizu 890ddd
	HasMainLoop = state ? 1 : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TSystem::getHostName()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QHostInfo::localHostName();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TSystem::getUserName()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QStringList list = QProcess::systemEnvironment();
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	for (j = 0; j < list.size(); j++) {
Toshihiro Shimizu 890ddd
		QString value = list.at(j);
Toshihiro Shimizu 890ddd
		QString user;
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
		if (value.startsWith("USERNAME="))
Toshihiro Shimizu 890ddd
			user = value.right(value.size() - 9);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		if (value.startsWith("USER="))
Toshihiro Shimizu 890ddd
			user = value.right(value.size() - 5);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		if (!user.isEmpty())
Toshihiro Shimizu 890ddd
			return user;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return QString("none");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TSystem::getTempDir()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TFilePath(QDir::tempPath().toStdString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TSystem::getTestDir(string name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TFilePath("C:") + TFilePath(name);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TSystem::getSystemValue(const TFilePath &name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QStringList strlist = toQString(name).split("\\", QString::SkipEmptyParts);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(strlist.size() > 3);
Toshihiro Shimizu 890ddd
	assert(strlist.at(0) == "SOFTWARE");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QSettings qs(QSettings::SystemScope, strlist.at(1), strlist.at(2));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	QString varName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 3; i < strlist.size(); i++) {
Toshihiro Shimizu 890ddd
		varName += strlist.at(i);
Toshihiro Shimizu 890ddd
		if (i < strlist.size() - 1)
Toshihiro Shimizu 890ddd
			varName += "//";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return qs.value(varName).toString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TSystem::getBinDir()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp = TFilePath(QCoreApplication::applicationFilePath().toStdString());
Toshihiro Shimizu 890ddd
	return fp.getParentDir();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TSystem::getDllDir()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getBinDir();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TSystem::getUniqueFile(QString field)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString uuid = QUuid::createUuid().toString().replace("-", "").replace("{", "").replace("}", "").toLatin1().data();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString path = QDir::tempPath() +
Toshihiro Shimizu 890ddd
				   QString("\\") +
Toshihiro Shimizu 890ddd
				   field +
Toshihiro Shimizu 890ddd
				   uuid;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TFilePath(path.toStdString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
TFilePathSet getPathsToCreate(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet pathList;
Toshihiro Shimizu 890ddd
	if (path.isEmpty())
Toshihiro Shimizu 890ddd
		return pathList;
Toshihiro Shimizu 890ddd
	TFilePath parentDir = path;
Toshihiro Shimizu 890ddd
	while (!TFileStatus(parentDir).doesExist()) {
Toshihiro Shimizu 890ddd
		if (parentDir == parentDir.getParentDir())
Toshihiro Shimizu 890ddd
			return TFilePathSet();
Toshihiro Shimizu 890ddd
		pathList.push_back(parentDir);
Toshihiro Shimizu 890ddd
		parentDir = parentDir.getParentDir();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return pathList;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void setPathsPermissions(const TFilePathSet &pathSet, QFile::Permissions permissions)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet::const_iterator it;
Toshihiro Shimizu 890ddd
	for (it = pathSet.begin(); it != pathSet.end(); it++) {
Toshihiro Shimizu 890ddd
		QFile f(toQString(*it));
Toshihiro Shimizu 890ddd
		f.setPermissions(permissions);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//gestire exception
Toshihiro Shimizu 890ddd
void TSystem::mkDir(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet pathSet = getPathsToCreate(path);
Toshihiro Shimizu 890ddd
	QString qPath = toQString(path);
Toshihiro Shimizu 890ddd
	assert(!qPath.contains("+"));
Toshihiro Shimizu 890ddd
	if (!QDir::current().mkpath(qPath))
Toshihiro Shimizu 890ddd
		throw TSystemException(path, "can't create folder!");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setPathsPermissions(pathSet, QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
Toshihiro Shimizu 890ddd
									 QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
Toshihiro Shimizu 890ddd
									 QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
//gestire exception
Toshihiro Shimizu 890ddd
void TSystem::rmDir(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!QDir(toQString(path.getParentDir())).rmdir(QString::fromStdString(path.getName())))
Toshihiro Shimizu 890ddd
		throw TSystemException(path, "can't remove folder!");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//vinz
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void rmDirTree(const QString &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	QFileInfoList fil = QDir(path).entryInfoList();
Toshihiro Shimizu 890ddd
	for (i = 0; i < fil.size(); i++) {
Toshihiro Shimizu 890ddd
		QFileInfo fi = fil.at(i);
Toshihiro Shimizu 890ddd
		if (fi.fileName() == QString(".") || fi.fileName() == QString(".."))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		QString son = fi.absoluteFilePath();
Toshihiro Shimizu 890ddd
		if (QFileInfo(son).isDir())
Toshihiro Shimizu 890ddd
			rmDirTree(son);
Toshihiro Shimizu 890ddd
		else if (QFileInfo(son).isFile())
Toshihiro Shimizu 890ddd
			if (!QFile::remove(son))
Toshihiro Shimizu 890ddd
				throw TSystemException("can't remove file" + son.toStdString());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!QDir::current().rmdir(path))
Toshihiro Shimizu 890ddd
		throw TSystemException("can't remove path!");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::rmDirTree(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	::rmDirTree(toQString(path));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::copyDir(const TFilePath &dst, const TFilePath &src)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QFileInfoList fil = QDir(toQString(src)).entryInfoList();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QDir::current().mkdir(toQString(dst));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < fil.size(); i++) {
Toshihiro Shimizu 890ddd
		QFileInfo fi = fil.at(i);
Toshihiro Shimizu 890ddd
		if (fi.fileName() == QString(".") || fi.fileName() == QString(".."))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		if (fi.isDir()) {
Toshihiro Shimizu 890ddd
			TFilePath srcDir = TFilePath(fi.filePath().toStdString());
Toshihiro Shimizu 890ddd
			TFilePath dstDir = dst + srcDir.getName();
Toshihiro Shimizu 890ddd
			copyDir(dstDir, srcDir);
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			QFile::copy(fi.filePath(), toQString(dst) + QString("\\") + fi.fileName());
Toshihiro Shimizu 890ddd
	}
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;
Toshihiro Shimizu 890ddd
FormatMessage( 
Toshihiro Shimizu 890ddd
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
Toshihiro Shimizu 890ddd
    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,
Toshihiro Shimizu 890ddd
    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);
Toshihiro Shimizu 890ddd
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
Toshihiro Shimizu 890ddd
void TSystem::copyFile(const TFilePath &dst, const TFilePath &src, bool overwrite)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(dst != TFilePath());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dst == src)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const QString &qDst = toQString(dst);
Toshihiro Shimizu 890ddd
	if (overwrite && QFile::exists(qDst))
Toshihiro Shimizu 890ddd
		QFile::remove(qDst);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!QFile::copy(toQString(src), qDst))
Toshihiro Shimizu 890ddd
		throw TSystemException(dst, "can't copy file!");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::renameFile(const TFilePath &dst, const TFilePath &src, bool overwrite)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(dst != TFilePath());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dst == src)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const QString &qDst = toQString(dst);
Toshihiro Shimizu 890ddd
	if (overwrite && QFile::exists(qDst))
Toshihiro Shimizu 890ddd
		QFile::remove(qDst);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!QFile::rename(toQString(src), qDst))
Toshihiro Shimizu 890ddd
		throw TSystemException(dst, "can't rename file!");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//gestire gli errori con GetLastError?
Toshihiro Shimizu 890ddd
void TSystem::deleteFile(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!QFile::remove(toQString(fp)))
Toshihiro Shimizu 890ddd
		throw TSystemException(fp, "can't delete file!");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::hideFile(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	if (!SetFileAttributesW(fp.getWideString().c_str(), FILE_ATTRIBUTE_HIDDEN))
Toshihiro Shimizu 890ddd
		throw TSystemException(fp, "can't hide file!");
Toshihiro Shimizu 890ddd
#else // MACOSX, and others
Toshihiro Shimizu 890ddd
	TSystem::renameFile(TFilePath(fp.getParentDir() + L"." + fp.getLevelNameW()), fp);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class CaselessFilepathLess : public std::binary_function<tfilepath, bool="" tfilepath,=""></tfilepath,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	bool operator()(const TFilePath &a, const TFilePath &b) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		wstring aa = toLower(a.getWideString());
Toshihiro Shimizu 890ddd
		wstring bb = toLower(b.getWideString());
Toshihiro Shimizu 890ddd
		if (aa == bb)
Toshihiro Shimizu 890ddd
			return a < b;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			return aa < bb;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! return the file list which is readable and executable
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void TSystem::readDirectory_Dir_ReadExe(TFilePathSet &dst, const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!TFileStatus(path).isDirectory())
Toshihiro Shimizu 890ddd
		throw TSystemException(path, " is not a directory");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::set<tfilepath, caselessfilepathless=""> fileSet;</tfilepath,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList fil = QDir(toQString(path)).entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < fil.size(); i++) {
Toshihiro Shimizu 890ddd
		QString fi = fil.at(i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath son = path + TFilePath(fi.toStdWString());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		fileSet.insert(son);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dst.insert(dst.end(), fileSet.begin(), fileSet.end());
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
*/
Toshihiro Shimizu 890ddd
void TSystem::readDirectory(TFilePathSet &groupFpSet, TFilePathSet &allFpSet, const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!TFileStatus(path).isDirectory())
Toshihiro Shimizu 890ddd
		throw TSystemException(path, " is not a directory");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::set<tfilepath, caselessfilepathless=""> fileSet_group;</tfilepath,>
Toshihiro Shimizu 890ddd
	std::set<tfilepath, caselessfilepathless=""> fileSet_all;</tfilepath,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList fil = QDir(toQString(path)).entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fil.size() == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < fil.size(); i++) {
Toshihiro Shimizu 890ddd
		QString fi = fil.at(i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath son = path + TFilePath(fi.toStdWString());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//store all file paths
Toshihiro Shimizu 890ddd
		fileSet_all.insert(son);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//in case of the sequencial files
Toshihiro Shimizu 890ddd
		if (son.getDots() == "..")
Toshihiro Shimizu 890ddd
			son = son.withFrame();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//store the group. insersion avoids duplication of the item
Toshihiro Shimizu 890ddd
		fileSet_group.insert(son);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	groupFpSet.insert(groupFpSet.end(), fileSet_group.begin(), fileSet_group.end());
Toshihiro Shimizu 890ddd
	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,
Toshihiro Shimizu 890ddd
							bool groupFrames)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!(dir.exists() && QFileInfo(dir.path()).isDir()))
Toshihiro Shimizu 890ddd
		throw TSystemException(TFilePath(dir.path().toStdWString()), " is not a directory");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList entries(dir.entryList(dir.filter() | QDir::NoDotAndDotDot));
Toshihiro Shimizu 890ddd
	TFilePath dirPath(dir.path().toStdWString());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::set<tfilepath, caselessfilepathless=""> fpSet;</tfilepath,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int e, eCount = entries.size();
Toshihiro Shimizu 890ddd
	for (e = 0; e != eCount; ++e) {
Toshihiro Shimizu 890ddd
		TFilePath path(dirPath + TFilePath(entries.at(e).toStdWString()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (groupFrames && path.getDots() == "..")
Toshihiro Shimizu 890ddd
			path = path.withFrame();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		fpSet.insert(path);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dst.insert(dst.end(), fpSet.begin(), fpSet.end());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::readDirectory(TFilePathSet &dst, const TFilePath &path, bool groupFrames,
Toshihiro Shimizu 890ddd
							bool onlyFiles, bool getHiddenFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QDir dir(toQString(path));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QDir::Filters filters(QDir::Files);
Toshihiro Shimizu 890ddd
	if (!onlyFiles)
Toshihiro Shimizu 890ddd
		filters |= QDir::Dirs;
Toshihiro Shimizu 890ddd
	if (getHiddenFiles)
Toshihiro Shimizu 890ddd
		filters |= QDir::Hidden;
Toshihiro Shimizu 890ddd
	dir.setFilter(filters);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	readDirectory(dst, dir, groupFrames);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::readDirectory(TFilePathSet &dst, const TFilePathSet &pathSet, bool groupFrames, bool onlyFiles, bool getHiddenFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (TFilePathSet::const_iterator it = pathSet.begin(); it != pathSet.end(); it++)
Toshihiro Shimizu 890ddd
		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,
Toshihiro Shimizu 890ddd
									bool onlyFiles, bool getHiddenFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet filePathSet;
Toshihiro Shimizu 890ddd
	readDirectory(filePathSet, path, groupFrames, onlyFiles, getHiddenFiles);
Toshihiro Shimizu 890ddd
	return filePathSet;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePathSet TSystem::readDirectory(const TFilePathSet &pathSet, bool groupFrames,
Toshihiro Shimizu 890ddd
									bool onlyFiles, bool getHiddenFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet dst;
Toshihiro Shimizu 890ddd
	readDirectory(dst, pathSet, groupFrames, onlyFiles, getHiddenFiles);
Toshihiro Shimizu 890ddd
	return dst;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::readDirectoryTree(TFilePathSet &dst, const TFilePath &path, bool groupFrames, bool onlyFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!TFileStatus(path).isDirectory())
Toshihiro Shimizu 890ddd
		throw TSystemException(path, " is not a directory");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QFileInfoList fil = QDir(toQString(path)).entryInfoList();
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < fil.size(); i++) {
Toshihiro Shimizu 890ddd
		QFileInfo fi = fil.at(i);
Toshihiro Shimizu 890ddd
		if (fi.fileName() == QString(".") || fi.fileName() == QString(".."))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		TFilePath son = TFilePath(fi.filePath().toStdWString());
Toshihiro Shimizu 890ddd
		if (TFileStatus(son).isDirectory()) {
Toshihiro Shimizu 890ddd
			if (!onlyFiles)
Toshihiro Shimizu 890ddd
				dst.push_back(son);
Toshihiro Shimizu 890ddd
			readDirectoryTree(dst, son, groupFrames, onlyFiles);
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			dst.push_back(son);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::readDirectoryTree(TFilePathSet &dst, const TFilePathSet &pathSet, bool groupFrames, bool onlyFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (TFilePathSet::const_iterator it = pathSet.begin(); it != pathSet.end(); it++)
Toshihiro Shimizu 890ddd
		readDirectoryTree(dst, *it, groupFrames, onlyFiles);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePathSet TSystem::readDirectoryTree(const TFilePath &path, bool groupFrames, bool onlyFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet dst;
Toshihiro Shimizu 890ddd
	readDirectoryTree(dst, path, groupFrames, onlyFiles);
Toshihiro Shimizu 890ddd
	return dst;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePathSet TSystem::readDirectoryTree(const TFilePathSet &pathSet, bool groupFrames, bool onlyFiles)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet dst;
Toshihiro Shimizu 890ddd
	readDirectoryTree(dst, pathSet, groupFrames, onlyFiles);
Toshihiro Shimizu 890ddd
	return dst;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePathSet TSystem::packLevelNames(const TFilePathSet &fps)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::set<tfilepath> tmpSet;</tfilepath>
Toshihiro Shimizu 890ddd
	TFilePathSet::const_iterator cit;
Toshihiro Shimizu 890ddd
	for (cit = fps.begin(); cit != fps.end(); ++cit)
Toshihiro Shimizu 890ddd
		tmpSet.insert(cit->getParentDir() + cit->getLevelName());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePathSet fps2;
Toshihiro Shimizu 890ddd
	for (std::set<tfilepath>::const_iterator c_sit = tmpSet.begin(); c_sit != tmpSet.end(); ++c_sit) {</tfilepath>
Toshihiro Shimizu 890ddd
		fps2.push_back(*c_sit);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return fps2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePathSet TSystem::getDisks()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet filePathSet;
Toshihiro Shimizu 890ddd
	QFileInfoList fil = QDir::drives();
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < fil.size(); i++)
Toshihiro Shimizu 890ddd
		filePathSet.push_back(TFilePath(fil.at(i).filePath().toStdWString()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return filePathSet;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class LocalThread : public QThread
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	static LocalThread *currentThread() { return (LocalThread *)QThread::currentThread(); }
Toshihiro Shimizu 890ddd
	void sleep(TINT64 delay) { msleep(delay); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::sleep(TINT64 delay)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	LocalThread::currentThread()->sleep(delay);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TSystem::getProcessorCount()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QThread::idealThreadCount();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::doesExistFileOrLevel(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (TFileStatus(fp).doesExist())
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fp.isLevelName()) {
Toshihiro Shimizu 890ddd
		const TFilePath &parentDir = fp.getParentDir();
Toshihiro Shimizu 890ddd
		if (!TFileStatus(parentDir).doesExist())
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePathSet files;
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			files = TSystem::readDirectory(parentDir, false, true, true);
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePathSet::iterator it, end = files.end();
Toshihiro Shimizu 890ddd
		for (it = files.begin(); it != end; ++it) {
Toshihiro Shimizu 890ddd
			if (it->getLevelNameW() == fp.getLevelNameW())
Toshihiro Shimizu 890ddd
				return true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (fp.getType() == "psd") {
Toshihiro Shimizu 890ddd
		QString name(QString::fromStdWString(fp.getWideName()));
Toshihiro Shimizu 890ddd
		name.append(QString::fromStdString(fp.getDottedType()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int sepPos = name.indexOf("#");
Toshihiro Shimizu 890ddd
		int dotPos = name.indexOf(".", sepPos);
Toshihiro Shimizu 890ddd
		int removeChars = dotPos - sepPos;
Toshihiro Shimizu 890ddd
		int doubleUnderscorePos = name.indexOf("__", sepPos);
Toshihiro Shimizu 890ddd
		if (doubleUnderscorePos > 0)
Toshihiro Shimizu 890ddd
			removeChars = doubleUnderscorePos - sepPos;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		name.remove(sepPos, removeChars);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath psdpath(fp.getParentDir() + TFilePath(name.toStdWString()));
Toshihiro Shimizu 890ddd
		if (TFileStatus(psdpath).doesExist())
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::copyFileOrLevel_throw(const TFilePath &dst, const TFilePath &src)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (src.isLevelName()) {
Toshihiro Shimizu 890ddd
		TFilePathSet files;
Toshihiro Shimizu 890ddd
		files = TSystem::readDirectory(src.getParentDir(), false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePathSet::iterator it, end = files.end();
Toshihiro Shimizu 890ddd
		for (it = files.begin(); it != end; ++it) {
Toshihiro Shimizu 890ddd
			if (it->getLevelNameW() == src.getLevelNameW()) {
Toshihiro Shimizu 890ddd
				TFilePath src1 = *it;
Toshihiro Shimizu 890ddd
				TFilePath dst1 = dst.withFrame(it->getFrame());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TSystem::copyFile(dst1, src1);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		TSystem::copyFile(dst, src);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::renameFileOrLevel_throw(const TFilePath &dst, const TFilePath &src, bool renamePalette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (renamePalette && ((src.getType() == "tlv") || (src.getType() == "tzp") || (src.getType() == "tzu"))) {
Toshihiro Shimizu 890ddd
		// Special case: since renames cannot be 'grouped' in the UI, palettes are automatically
Toshihiro Shimizu 890ddd
		// renamed here if required
Toshihiro Shimizu 890ddd
		const char *type = (src.getType() == "tlv") ? "tpl" : "plt";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath srcpltname(src.withNoFrame().withType(type));
Toshihiro Shimizu 890ddd
		TFilePath dstpltname(dst.withNoFrame().withType(type));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (TSystem::doesExistFileOrLevel(src) && TSystem::doesExistFileOrLevel(srcpltname))
Toshihiro Shimizu 890ddd
			TSystem::renameFile(dstpltname, srcpltname, false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (src.isLevelName()) {
Toshihiro Shimizu 890ddd
		TFilePathSet files;
Toshihiro Shimizu 890ddd
		files = TSystem::readDirectory(src.getParentDir(), false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (TFilePathSet::iterator it = files.begin(); it != files.end(); it++) {
Toshihiro Shimizu 890ddd
			if (it->getLevelName() == src.getLevelName()) {
Toshihiro Shimizu 890ddd
				TFilePath src1 = *it;
Toshihiro Shimizu 890ddd
				TFilePath dst1 = dst.withFrame(it->getFrame());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TSystem::renameFile(dst1, src1);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		TSystem::renameFile(dst, src);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::removeFileOrLevel_throw(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (fp.isLevelName()) {
Toshihiro Shimizu 890ddd
		TFilePathSet files;
Toshihiro Shimizu 890ddd
		files = TSystem::readDirectory(fp.getParentDir(), false, true, true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePathSet::iterator it, end = files.end();
Toshihiro Shimizu 890ddd
		for (it = files.begin(); it != end; ++it) {
Toshihiro Shimizu 890ddd
			if (it->getLevelName() == fp.getLevelName())
Toshihiro Shimizu 890ddd
				TSystem::deleteFile(*it);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		TSystem::deleteFile(fp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::hideFileOrLevel_throw(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (fp.isLevelName()) {
Toshihiro Shimizu 890ddd
		TFilePathSet files;
Toshihiro Shimizu 890ddd
		files = TSystem::readDirectory(fp.getParentDir(), false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePathSet::iterator it, end = files.end();
Toshihiro Shimizu 890ddd
		for (it = files.begin(); it != end; ++it) {
Toshihiro Shimizu 890ddd
			if (it->getLevelNameW() == fp.getLevelNameW())
Toshihiro Shimizu 890ddd
				TSystem::hideFile(*it);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		TSystem::hideFile(fp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::moveFileOrLevelToRecycleBin_throw(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (fp.isLevelName()) {
Toshihiro Shimizu 890ddd
		TFilePathSet files;
Toshihiro Shimizu 890ddd
		files = TSystem::readDirectory(fp.getParentDir(), false, true, true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePathSet::iterator it, end = files.end();
Toshihiro Shimizu 890ddd
		for (it = files.begin(); it != end; ++it) {
Toshihiro Shimizu 890ddd
			if (it->getLevelNameW() == fp.getLevelNameW())
Toshihiro Shimizu 890ddd
				TSystem::moveFileToRecycleBin(*it);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		TSystem::moveFileToRecycleBin(fp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::copyFileOrLevel(const TFilePath &dst, const TFilePath &src)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		copyFileOrLevel_throw(dst, src);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::renameFileOrLevel(const TFilePath &dst, const TFilePath &src, bool renamePalette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		renameFileOrLevel_throw(dst, src, renamePalette);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::removeFileOrLevel(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		removeFileOrLevel_throw(fp);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::hideFileOrLevel(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		hideFileOrLevel_throw(fp);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::moveFileOrLevelToRecycleBin(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		moveFileOrLevelToRecycleBin_throw(fp);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::touchParentDir(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath parentDir = fp.getParentDir();
Toshihiro Shimizu 890ddd
	TFileStatus fs(parentDir);
Toshihiro Shimizu 890ddd
	if (fs.isDirectory())
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	else if (fs.doesExist())
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		mkDir(parentDir);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::showDocument(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	int ret = (int)
Toshihiro Shimizu 890ddd
		ShellExecuteW(0, L"open", path.getWideString().c_str(), 0, 0, SW_SHOWNORMAL);
Toshihiro Shimizu 890ddd
	if (ret <= 32) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
		throw TSystemException(path, "Can't open");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	string cmd = "open ";
Toshihiro Shimizu 890ddd
	string thePath(toString(path.getWideString()));
Toshihiro Shimizu 890ddd
	UINT pos = 0, count = 0;
Toshihiro Shimizu 890ddd
	//string newPath;
Toshihiro Shimizu 890ddd
	char newPath[2048];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (pos < thePath.size()) {
Toshihiro Shimizu 890ddd
		char c = thePath[pos];
Toshihiro Shimizu 890ddd
		if (c == ' ')
Toshihiro Shimizu 890ddd
			newPath[count++] = '\\';
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		newPath[count++] = c;
Toshihiro Shimizu 890ddd
		++pos;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	newPath[count] = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	cmd = cmd + string(newPath);
Toshihiro Shimizu 890ddd
	system(cmd.c_str());
Toshihiro Shimizu 890ddd
	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
Toshihiro Shimizu 890ddd
void TSystem::sleep(TINT64 delay)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Sleep((DWORD)delay);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//gestire gli errori con GetLastError?
Toshihiro Shimizu 890ddd
void TSystem::deleteFile(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::rmDirTree(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSystemException::TSystemException(const TFilePath &fname, int err)
Toshihiro Shimizu 890ddd
	: m_fname(fname), m_err(err), m_msg(L"")
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TSystemException::TSystemException(const TFilePath &fname, const std::string &msg)
Toshihiro Shimizu 890ddd
	: m_fname(fname), m_err(-1), m_msg(toWideString(msg))
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSystemException::TSystemException(const TFilePath &fname, const wstring &msg)
Toshihiro Shimizu 890ddd
	: m_fname(fname), m_err(-1), m_msg(msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TSystemException::TSystemException(const std::string &msg)
Toshihiro Shimizu 890ddd
	: m_fname(""), m_err(-1), m_msg(toWideString(msg))
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSystemException::TSystemException(const wstring &msg)
Toshihiro Shimizu 890ddd
	: m_fname(""), m_err(-1), m_msg(msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}