Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#ifndef UNICODE
Toshihiro Shimizu 890ddd
#define UNICODE
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
//#include "tunicode.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <time.h></time.h>
Toshihiro Shimizu 890ddd
#include <sys timeb.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys stat.h=""></sys>
Toshihiro Shimizu 890ddd
#include <errno.h></errno.h>
Toshihiro Shimizu 890ddd
#include <set></set>
Toshihiro Shimizu 890ddd
#include <tenv.h></tenv.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef PLATFORM
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#define PLATFORM WIN32
Toshihiro Shimizu 890ddd
#include <process.h></process.h>
Toshihiro Shimizu 890ddd
#include <psapi.h></psapi.h>
Toshihiro Shimizu 890ddd
#include <io.h></io.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <direct.h></direct.h>
Toshihiro Shimizu 890ddd
// gmt: sulla mia macchina cosi' non compila!!!
Toshihiro Shimizu 890ddd
// #include "winsock2.h"
Toshihiro Shimizu 890ddd
// #include "lmcons.h"
Toshihiro Shimizu 890ddd
#include <sys utime.h=""></sys>
Toshihiro Shimizu 890ddd
#include <lm.h></lm.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef LINUX
Toshihiro Shimizu 890ddd
#define PLATFORM LINUX
Toshihiro Shimizu 890ddd
#include <grp.h></grp.h>
Toshihiro Shimizu 890ddd
#include <utime.h></utime.h>
Toshihiro Shimizu 890ddd
#include <sys param.h=""></sys>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <dirent.h></dirent.h>
Toshihiro Shimizu 890ddd
#include <sys dir.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys sysinfo.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys swap.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys statfs.h=""></sys>
Toshihiro Shimizu 890ddd
#include <pwd.h></pwd.h>
Toshihiro Shimizu 890ddd
#include <mntent.h></mntent.h>
Toshihiro Shimizu 890ddd
#include <dlfcn.h></dlfcn.h>
Toshihiro Shimizu 890ddd
#include <utime.h></utime.h>
Toshihiro Shimizu 890ddd
#include <sys time.h=""></sys>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(MACOSX)
Toshihiro Shimizu 890ddd
#define PLATFORM MACOSX
Toshihiro Shimizu 890ddd
#include <grp.h></grp.h>
Toshihiro Shimizu 890ddd
#include <utime.h></utime.h>
Toshihiro Shimizu 890ddd
#include <sys param.h=""></sys>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys timeb.h=""> // for ftime</sys>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <dirent.h></dirent.h>
Toshihiro Shimizu 890ddd
#include <sys dir.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys param.h=""> // for getfsstat</sys>
Toshihiro Shimizu 890ddd
#include <sys ucred.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys mount.h=""></sys>
Toshihiro Shimizu 890ddd
#include <pwd.h></pwd.h>
Toshihiro Shimizu 890ddd
#include <dlfcn.h></dlfcn.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "Carbon/Carbon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
#define PLATFORM SGI
Toshihiro Shimizu 890ddd
#include <sys param.h=""></sys>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <grp.h></grp.h>
Toshihiro Shimizu 890ddd
#include <sys dir.h=""> // dirent.h</sys>
Toshihiro Shimizu 890ddd
#include <sys utime.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys swap.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys statfs.h=""></sys>
Toshihiro Shimizu 890ddd
#include <pwd.h></pwd.h>
Toshihiro Shimizu 890ddd
#include <mntent.h></mntent.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <dlfcn.h></dlfcn.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef PLATFORM
Toshihiro Shimizu 890ddd
PLATFORM_NOT_SUPPORTED
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#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
	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
#endif
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::outputDebug(string s)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
	OutputDebugString((LPCWSTR)s.c_str());
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	cerr << s << endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	qDebug(s.c_str());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TSystem::getProcessId()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getpid();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSystem::memoryShortage()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	MEMORYSTATUSEX memStatus;
Toshihiro Shimizu 890ddd
	memStatus.dwLength = sizeof(MEMORYSTATUSEX);
Toshihiro Shimizu 890ddd
	GlobalMemoryStatusEx(&memStatus);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(memStatus.ullAvailPhys <= memStatus.ullTotalPhys);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (memStatus.ullAvailPhys < memStatus.ullTotalPhys * 0.20) //if available memory is less then 20% of total memory
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	PROCESS_MEMORY_COUNTERS c;
Toshihiro Shimizu 890ddd
	c.cb = sizeof(PROCESS_MEMORY_COUNTERS);
Toshihiro Shimizu 890ddd
	BOOL ret = GetProcessMemoryInfo(GetCurrentProcess(), &c, sizeof(PROCESS_MEMORY_COUNTERS));
Toshihiro Shimizu 890ddd
	assert(ret);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return c.WorkingSetSize > memStatus.ullTotalVirtual * 0.6; //if total memory used by this process(WorkingSetSize) is
Toshihiro Shimizu 890ddd
															   //half of max allocatable memory
Toshihiro Shimizu 890ddd
															   //(ullTotalVirtual: on 32bits machines, tipically it's 2GB)
Toshihiro Shimizu 890ddd
															   //It's better "to stay large"; for values >0.6 this function may
Toshihiro Shimizu 890ddd
															   //returns that there is memory, but for fragmentation the malloc fails the same!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// to be done...
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
Campbell Barton 107701
#elif defined(LINUX)
Campbell Barton 107701
Campbell Barton 107701
	// to be done...
Campbell Barton 107701
	return false;
Campbell Barton 107701
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	@ @ @ERROR : PLATFORM NOT SUPPORTED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TINT64 TSystem::getFreeMemorySize(bool onlyPhisicalMemory)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TINT64 totalFree = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	MEMORYSTATUSEX buff;
Toshihiro Shimizu 890ddd
	buff.dwLength = sizeof(MEMORYSTATUSEX);
Toshihiro Shimizu 890ddd
	GlobalMemoryStatusEx(&buff);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (onlyPhisicalMemory)
Toshihiro Shimizu 890ddd
		return buff.ullAvailPhys >> 10;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return buff.ullAvailPageFile >> 10;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(__sgi)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//check for virtual memory
Toshihiro Shimizu 890ddd
	int numberOfResources = swapctl(SC_GETNSWP, 0); /* get number of swapping resources configued */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (numberOfResources == 0)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//avrei voluto fare: struct swaptable *table = new struct swaptable[...]
Toshihiro Shimizu 890ddd
	struct swaptable *table = (struct swaptable *)calloc(1, sizeof(struct swapent) * numberOfResources + sizeof(int));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	table->swt_n = numberOfResources;
Toshihiro Shimizu 890ddd
	swapctl(SC_LIST, table); /* list all the swapping resources */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TINT64 virtualFree = 0;
Toshihiro Shimizu 890ddd
	TINT64 physicalFree = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < table->swt_n; i++) {
Toshihiro Shimizu 890ddd
		virtualFree += table->swt_ent[i].ste_free;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	free(table);
Toshihiro Shimizu 890ddd
	totalFree = virtualFree << 4 + physicalFree;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(LINUX)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	struct sysinfo *sysInfo = (struct sysinfo *)calloc(1, sizeof(struct sysinfo));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!sysinfo(sysInfo)) {
Toshihiro Shimizu 890ddd
		if (onlyPhisicalMemory)
Toshihiro Shimizu 890ddd
			totalFree = sysInfo->freeram;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			totalFree = sysInfo->freeram + sysInfo->freeswap;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		assert(!"sysinfo function failed");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	free(sysInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// to be done...
Toshihiro Shimizu 890ddd
	totalFree = 512 * 1024;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	@ @ @ERROR : PLATFORM NOT SUPPORTED
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef WIN32
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return totalFree;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
ostream& operator<<(ostream&out, const TTime  &t)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  return out<
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TINT64 TSystem::getDiskSize(const TFilePath &diskName)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TINT64 size = 0;
Toshihiro Shimizu 890ddd
	if (!diskName.isAbsolute()) {
Toshihiro Shimizu 890ddd
		assert(0);
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#ifndef WIN32
Toshihiro Shimizu 890ddd
	struct statfs buf;
Toshihiro Shimizu 890ddd
	wstring str_diskname = diskName.getWideString();
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
	statfs(toString(str_diskname).c_str(), &buf, sizeof(struct statfs), 0);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	statfs(toString(str_diskname).c_str(), &buf);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	size = (TINT64)((buf.f_blocks * buf.f_bsize) >> 10);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	DWORD sectorsPerCluster;	// sectors per cluster
Toshihiro Shimizu 890ddd
	DWORD bytesPerSector;		// bytes per sector
Toshihiro Shimizu 890ddd
	DWORD numberOfFreeClusters; // free clusters
Toshihiro Shimizu 890ddd
	DWORD totalNumberOfClusters;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	BOOL rc = GetDiskFreeSpaceW(
Toshihiro Shimizu 890ddd
		diskName.getWideString().c_str(), // root path
Toshihiro Shimizu 890ddd
		§orsPerCluster,				  // sectors per cluster
Toshihiro Shimizu 890ddd
		&bytesPerSector,				  // bytes per sector
Toshihiro Shimizu 890ddd
		&numberOfFreeClusters,			  // free clusters
Toshihiro Shimizu 890ddd
		&totalNumberOfClusters			  // total clusters
Toshihiro Shimizu 890ddd
		);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!rc)
Toshihiro Shimizu 890ddd
		throw TSystemException(diskName, getFormattedMessage(GetLastError()));
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		size = (totalNumberOfClusters * sectorsPerCluster * bytesPerSector) >> 10;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return size;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TINT64 TSystem::getFreeDiskSize(const TFilePath &diskName)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TINT64 size = 0;
Toshihiro Shimizu 890ddd
	if (!diskName.isAbsolute()) {
Toshihiro Shimizu 890ddd
		assert(0);
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#ifndef WIN32
Toshihiro Shimizu 890ddd
	struct statfs buf;
Toshihiro Shimizu 890ddd
	wstring str_diskname = diskName.getWideString();
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
	statfs(str_diskname.c_str(), &buf, sizeof(struct statfs), 0);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	statfs(toString(str_diskname).c_str(), &buf);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	size = (TINT64)(buf.f_bfree * buf.f_bsize) >> 10;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	DWORD sectorsPerCluster;	// sectors per cluster
Toshihiro Shimizu 890ddd
	DWORD bytesPerSector;		// bytes per sector
Toshihiro Shimizu 890ddd
	DWORD numberOfFreeClusters; // free clusters
Toshihiro Shimizu 890ddd
	DWORD totalNumberOfClusters;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	BOOL rc = GetDiskFreeSpaceW(diskName.getWideString().c_str(), // root path
Toshihiro Shimizu 890ddd
								§orsPerCluster,				  // sectors per cluster
Toshihiro Shimizu 890ddd
								&bytesPerSector,				  // bytes per sector
Toshihiro Shimizu 890ddd
								&numberOfFreeClusters,			  // free clusters
Toshihiro Shimizu 890ddd
								&totalNumberOfClusters			  // total clusters
Toshihiro Shimizu 890ddd
								);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!rc) //eccezione... getLastError etc...
Toshihiro Shimizu 890ddd
		throw TSystemException(diskName, "cannot get disk info!");
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		size = (numberOfFreeClusters * sectorsPerCluster * bytesPerSector) >> 10;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return size;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TINT64 TSystem::getMemorySize(bool onlyPhisicalMemory)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	MEMORYSTATUS buff;
Toshihiro Shimizu 890ddd
	GlobalMemoryStatus(&buff);
Toshihiro Shimizu 890ddd
	if (onlyPhisicalMemory)
Toshihiro Shimizu 890ddd
		return buff.dwTotalPhys >> 10;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return buff.dwTotalPageFile >> 10;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(__sgi)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int physicalMemory;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (swapctl(SC_GETSWAPMAX, &physicalMemory))
Toshihiro Shimizu 890ddd
		return ((size_t)0);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return logSwapLibero >> 1;
Toshihiro Shimizu 890ddd
#elif defined(LINUX)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	struct sysinfo *sysInfo = (struct sysinfo *)calloc(1, sizeof(struct sysinfo));
Toshihiro Shimizu 890ddd
	TINT64 ret = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!sysinfo(sysInfo))
Toshihiro Shimizu 890ddd
		ret = sysInfo->totalram;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		assert(!"sysinfo function failed");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	free(sysInfo);
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// to be done...
Toshihiro Shimizu 890ddd
	return 512 * 1024;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	@ @ @ERROR : PLATFORM NOT SUPPORTED
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef WIN32
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSystem::moveFileToRecycleBin(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#if defined(WIN32)
Toshihiro Shimizu 890ddd
	//
Toshihiro Shimizu 890ddd
	// from http://msdn.microsoft.com/msdnmag/issues/01/04/c/default.aspx
Toshihiro Shimizu 890ddd
	//
Toshihiro Shimizu 890ddd
	// Copy pathname to double-NULL-terminated string.
Toshihiro Shimizu 890ddd
	//
Toshihiro Shimizu 890ddd
	wchar_t buf[_MAX_PATH + 1];				 // allow one more character
Toshihiro Shimizu 890ddd
	wcscpy(buf, fp.getWideString().c_str()); // copy caller's path name
Toshihiro Shimizu 890ddd
	buf[wcslen(buf) + 1] = 0;				 // need two NULLs at end
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	SHFILEOPSTRUCTW data;
Toshihiro Shimizu 890ddd
	memset(&data, 0, sizeof(SHFILEOPSTRUCTW));
Toshihiro Shimizu 890ddd
	data.fFlags |= FOF_SILENT;		   // don't report progress
Toshihiro Shimizu 890ddd
	data.fFlags |= FOF_NOERRORUI;	  // don't report errors
Toshihiro Shimizu 890ddd
	data.fFlags |= FOF_NOCONFIRMATION; // don't confirm delete
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	data.wFunc = FO_DELETE;			   // REQUIRED: delete operation
Toshihiro Shimizu 890ddd
	data.pFrom = buf;				   // REQUIRED: which file(s)
Toshihiro Shimizu 890ddd
	data.pTo = NULL;				   // MUST be NULL
Toshihiro Shimizu 890ddd
	data.fFlags |= FOF_ALLOWUNDO;	  // ..send to Recycle Bin
Toshihiro Shimizu 890ddd
	int ret = SHFileOperationW(&data); // do it!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Toshihiro Shimizu 890ddd
	FSRef foundRef;
Toshihiro Shimizu 890ddd
	OSErr err = FSFindFolder(kOnSystemDisk,
Toshihiro Shimizu 890ddd
							 kTrashFolderType, kDontCreateFolder,
Toshihiro Shimizu 890ddd
							 &foundRef);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (err) {
Toshihiro Shimizu 890ddd
		assert(false);
Toshihiro Shimizu 890ddd
		deleteFile(fp);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	UInt8 path[255];
Toshihiro Shimizu 890ddd
	err = FSRefMakePath(&foundRef, path, 254);
Toshihiro Shimizu 890ddd
	if (err) {
Toshihiro Shimizu 890ddd
		assert(false);
Toshihiro Shimizu 890ddd
		deleteFile(fp);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//TFilePath dest = TFilePath(path)+(fp.getName()+fp.getDottedType());
Toshihiro Shimizu 890ddd
	string fullNameWithExt = toString(fp.getWideString());
Toshihiro Shimizu 890ddd
	int i = fullNameWithExt.rfind("/");
Toshihiro Shimizu 890ddd
	string nameWithExt = fullNameWithExt.substr(i + 1);
Toshihiro Shimizu 890ddd
	TFilePath dest = TFilePath((char *)path) + nameWithExt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		renameFile(dest, fp);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			copyFile(dest, fp);
Toshihiro Shimizu 890ddd
			deleteFile(fp);
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	assert(!"Not implemented yet");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TString TSystemException::getMessage() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	wstring msg;
Toshihiro Shimizu 890ddd
	switch (m_err) {
Toshihiro Shimizu 890ddd
	case -1:
Toshihiro Shimizu 890ddd
		// nothing
Toshihiro Shimizu 890ddd
		msg = m_msg;
Toshihiro Shimizu 890ddd
		CASE EEXIST : msg = L": Directory was not created because filename is the name of an existing file, directory, or device";
Toshihiro Shimizu 890ddd
		CASE ENOENT : msg = L": Path was not found, or the named file does not exist or is a null pathname.";
Toshihiro Shimizu 890ddd
		CASE ENOTEMPTY : msg = L": Given path is not a directory; directory is not empty; or directory is either current working directory or root directory";
Toshihiro Shimizu 890ddd
		CASE EACCES : msg = L": Search permission is denied by a component of the path prefix, or write permission on the file named by path is denied, or times is NULL, and write access is denied";
Toshihiro Shimizu 890ddd
		CASE EFAULT : msg = L": Times is not NULL and, or points outside the process's allocated address space.";
Toshihiro Shimizu 890ddd
		CASE EINTR : msg = L": A signal was caught during the utime system call.";
Toshihiro Shimizu 890ddd
		CASE ENAMETOOLONG : msg = L": The length of the path argument exceeds {PATH_MAX}, or the length of a path component exceeds {NAME_MAX} while _POSIX_NO_TRUNC is in effect.";
Toshihiro Shimizu 890ddd
		CASE ENOTDIR : msg = L": A component of the path prefix is not a directory.";
Toshihiro Shimizu 890ddd
		CASE EPERM : msg = L": The calling process does not have the super-user privilege, the effective user ID is not the owner of the file, and times is not NULL, or the file system containing the file is mounted read-only";
Toshihiro Shimizu 890ddd
		CASE EROFS : msg = L": The current file system level range does not envelop the level of the file named by path, and the calling process does not have the super-user privilege.";
Toshihiro Shimizu 890ddd
		CASE ENOSYS : msg = L": When the named file cannot have its time reset.  The file is on a file system that doesn't have this operation.";
Toshihiro Shimizu 890ddd
		CASE EMFILE : msg = L": The maximum number of file descriptors are currently open.";
Toshihiro Shimizu 890ddd
		CASE ENFILE : msg = L": The system file table is full.";
Toshihiro Shimizu 890ddd
		CASE EBADF : msg = L": The file descriptor determined by the DIR stream is no longer valid.  This result occurs if the DIR stream has been closed.";
Toshihiro Shimizu 890ddd
		CASE EINVAL : msg = L": 64-bit and non-64-bit calls were mixed in a sequence of calls.";
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		msg = L": Unknown error";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef WIN32
Toshihiro Shimizu 890ddd
		CASE ELOOP : msg = L": Too many symbolic links were encountered in translating path.";
Toshihiro Shimizu 890ddd
#ifndef MACOSX
Toshihiro Shimizu 890ddd
		CASE EMULTIHOP : msg = L": Components of path require hopping to multiple remote machines and the file system does not allow it.";
Toshihiro Shimizu 890ddd
		CASE ENOLINK : msg = L": Path points to a remote machine and the link to that machine is no longer active.";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(__sgi)
Toshihiro Shimizu 890ddd
		CASE EDIRCORRUPTED : msg = L": The directory is corrupted on disk.";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		CASE EOVERFLOW : msg = L": One of the inode number values or offset values did not fit in 32 bits, and the 64-bit interfaces were not used.";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return m_fname.getWideString() + L"\n" + msg;
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
#ifndef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// string filename = path.getFullPath();
Toshihiro Shimizu 890ddd
	if (TFileStatus(path).doesExist()) {
Toshihiro Shimizu 890ddd
		int ret;
Toshihiro Shimizu 890ddd
#if defined(MACOSX) || defined(LINUX)
Toshihiro Shimizu 890ddd
		wstring str_file = path.getWideString();
Toshihiro Shimizu 890ddd
		ret = utimes(toString(str_file).c_str(), 0);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		ret = _wutime(path.getWideString().c_str(), 0);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		if (0 != ret)
Toshihiro Shimizu 890ddd
			throw TSystemException(path, errno);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		Tofstream file(path);
Toshihiro Shimizu 890ddd
		if (!file) {
Toshihiro Shimizu 890ddd
			throw TSystemException(path, errno);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		file.close(); // altrimenti il compilatore da' un warning:
Toshihiro Shimizu 890ddd
					  // variabile non utilizzata
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------