Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#ifndef UNICODE
Toshihiro Shimizu 890ddd
#define UNICODE
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 42bfb6
#include <memory></memory>
Shinya Kitaoka 42bfb6
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
e280ae
#ifdef _MSC_VER
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
e280ae
#endif
e280ae
e280ae
#ifdef _WIN32
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>
Shinya Kitaoka d4642c
#include <shellapi.h></shellapi.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>
Martin van Zijl 3d473a
#include <qdir></qdir>
Martin van Zijl 3d473a
#include <qfileinfo></qfileinfo>
Martin van Zijl 3d473a
#include <qtextstream></qtextstream>
Martin van Zijl 3d473a
#include <qurl></qurl>
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>
Shinya Kitaoka 120a6e
#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>
Shinya Kitaoka 120a6e
#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>
Shinya Kitaoka 120a6e
#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
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
wstring getFormattedMessage(DWORD lastError) {
Shinya Kitaoka 120a6e
  LPVOID lpMsgBuf;
Shinya Kitaoka 120a6e
  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
Shinya Kitaoka 120a6e
                    FORMAT_MESSAGE_IGNORE_INSERTS,
Shinya Kitaoka 120a6e
                NULL, lastError,
Shinya Kitaoka 120a6e
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),  // Default language
Shinya Kitaoka 120a6e
                (LPTSTR)&lpMsgBuf, 0, NULL);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int wSize = MultiByteToWideChar(0, 0, (char *)lpMsgBuf, -1, 0, 0);
Shinya Kitaoka 120a6e
  if (!wSize) return wstring();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::unique_ptr<wchar_t[]> wBuffer(new wchar_t[wSize + 1]);</wchar_t[]>
Shinya Kitaoka 120a6e
  MultiByteToWideChar(0, 0, (char *)lpMsgBuf, -1, wBuffer.get(), wSize);
Shinya Kitaoka 120a6e
  wBuffer[wSize] = '\0';
Shinya Kitaoka 120a6e
  wstring wmsg(wBuffer.get());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  LocalFree(lpMsgBuf);
Shinya Kitaoka 120a6e
  return wmsg;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::outputDebug(string s) {
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_LIGHT
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  OutputDebugString((LPCWSTR)s.c_str());
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  cerr << s << endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#else
Christoph Korn c17229
  qDebug("%s", s.c_str());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TSystem::getProcessId() { return getpid(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TSystem::memoryShortage() {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  MEMORYSTATUSEX memStatus;
Shinya Kitaoka 120a6e
  memStatus.dwLength = sizeof(MEMORYSTATUSEX);
Shinya Kitaoka 120a6e
  GlobalMemoryStatusEx(&memStatus);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(memStatus.ullAvailPhys <= memStatus.ullTotalPhys);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (memStatus.ullAvailPhys <
Shinya Kitaoka 120a6e
      memStatus.ullTotalPhys *
Shinya Kitaoka 120a6e
          0.20)  // if available memory is less then 20% of total memory
Shinya Kitaoka 120a6e
    return true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  PROCESS_MEMORY_COUNTERS c;
Shinya Kitaoka 120a6e
  c.cb     = sizeof(PROCESS_MEMORY_COUNTERS);
Shinya Kitaoka 120a6e
  BOOL ret = GetProcessMemoryInfo(GetCurrentProcess(), &c,
Shinya Kitaoka 120a6e
                                  sizeof(PROCESS_MEMORY_COUNTERS));
Shinya Kitaoka 120a6e
  assert(ret);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return c.WorkingSetSize >
Shinya Kitaoka 120a6e
         memStatus.ullTotalVirtual *
Shinya Kitaoka 120a6e
             0.6;  // if total memory used by this process(WorkingSetSize) is
Shinya Kitaoka 120a6e
// half of max allocatable memory
Shinya Kitaoka 120a6e
//(ullTotalVirtual: on 32bits machines, tipically it's 2GB)
Shinya Kitaoka 120a6e
// It's better "to stay large"; for values >0.6 this function may
Shinya Kitaoka 120a6e
// returns that there is memory, but for fragmentation the malloc fails the
Shinya Kitaoka 120a6e
// same!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // to be done...
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
Campbell Barton 107701
#elif defined(LINUX)
Campbell Barton 107701
Shinya Kitaoka 120a6e
  // to be done...
Shinya Kitaoka 120a6e
  return false;
Campbell Barton 107701
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  @ @ @ERROR : PLATFORM NOT SUPPORTED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TINT64 TSystem::getFreeMemorySize(bool onlyPhisicalMemory) {
Shinya Kitaoka 120a6e
  TINT64 totalFree = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  MEMORYSTATUSEX buff;
Shinya Kitaoka 120a6e
  buff.dwLength = sizeof(MEMORYSTATUSEX);
Shinya Kitaoka 120a6e
  GlobalMemoryStatusEx(&buff);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (onlyPhisicalMemory)
Shinya Kitaoka 120a6e
    return buff.ullAvailPhys >> 10;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return buff.ullAvailPageFile >> 10;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(__sgi)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // check for virtual memory
Shinya Kitaoka 120a6e
  int numberOfResources =
Shinya Kitaoka 120a6e
      swapctl(SC_GETNSWP, 0); /* get number of swapping resources configued */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (numberOfResources == 0) return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // avrei voluto fare: struct swaptable *table = new struct swaptable[...]
Shinya Kitaoka 120a6e
  struct swaptable *table = (struct swaptable *)calloc(
Shinya Kitaoka 120a6e
      1, sizeof(struct swapent) * numberOfResources + sizeof(int));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  table->swt_n = numberOfResources;
Shinya Kitaoka 120a6e
  swapctl(SC_LIST, table); /* list all the swapping resources */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TINT64 virtualFree  = 0;
Shinya Kitaoka 120a6e
  TINT64 physicalFree = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < table->swt_n; i++) {
Shinya Kitaoka 120a6e
    virtualFree += table->swt_ent[i].ste_free;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  free(table);
Shinya Kitaoka 120a6e
  totalFree = virtualFree << 4 + physicalFree;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(LINUX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  struct sysinfo *sysInfo = (struct sysinfo *)calloc(1, sizeof(struct sysinfo));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!sysinfo(sysInfo)) {
Shinya Kitaoka 120a6e
    if (onlyPhisicalMemory)
Shinya Kitaoka 120a6e
      totalFree = sysInfo->freeram;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      totalFree = sysInfo->freeram + sysInfo->freeswap;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    assert(!"sysinfo function failed");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  free(sysInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // to be done...
Shinya Kitaoka 120a6e
  totalFree = 512 * 1024;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  @ @ @ERROR : PLATFORM NOT SUPPORTED
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  return out<
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TINT64 TSystem::getDiskSize(const TFilePath &diskName) {
Shinya Kitaoka 120a6e
  TINT64 size = 0;
Shinya Kitaoka 120a6e
  if (!diskName.isAbsolute()) {
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Shinya Kitaoka 120a6e
  struct statfs buf;
Toshihiro Shimizu 890ddd
#ifdef __sgi
Shinya Kitaoka 120a6e
  statfs(::to_string(diskName).c_str(), &buf, sizeof(struct statfs), 0);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  statfs(::to_string(diskName).c_str(), &buf);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  size = (TINT64)((buf.f_blocks * buf.f_bsize) >> 10);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  DWORD sectorsPerCluster;     // sectors per cluster
Shinya Kitaoka 120a6e
  DWORD bytesPerSector;        // bytes per sector
Shinya Kitaoka 120a6e
  DWORD numberOfFreeClusters;  // free clusters
Shinya Kitaoka 120a6e
  DWORD totalNumberOfClusters;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  BOOL rc = GetDiskFreeSpaceW(diskName.getWideString().c_str(),  // root path
Shinya Kitaoka 120a6e
                              §orsPerCluster,     // sectors per cluster
Shinya Kitaoka 120a6e
                              &bytesPerSector,        // bytes per sector
Shinya Kitaoka 120a6e
                              &numberOfFreeClusters,  // free clusters
Shinya Kitaoka 120a6e
                              &totalNumberOfClusters  // total clusters
Shinya Kitaoka 120a6e
                              );
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!rc)
Shinya Kitaoka 120a6e
    throw TSystemException(diskName, getFormattedMessage(GetLastError()));
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    size = (totalNumberOfClusters * sectorsPerCluster * bytesPerSector) >> 10;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  return size;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TINT64 TSystem::getFreeDiskSize(const TFilePath &diskName) {
Shinya Kitaoka 120a6e
  TINT64 size = 0;
Shinya Kitaoka 120a6e
  if (!diskName.isAbsolute()) {
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Shinya Kitaoka 120a6e
  struct statfs buf;
Toshihiro Shimizu 890ddd
#ifdef __sgi
Shinya Kitaoka 120a6e
  statfs(diskName.getWideString().c_str(), &buf, sizeof(struct statfs), 0);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  statfs(::to_string(diskName).c_str(), &buf);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  size = (TINT64)(buf.f_bfree * buf.f_bsize) >> 10;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  DWORD sectorsPerCluster;     // sectors per cluster
Shinya Kitaoka 120a6e
  DWORD bytesPerSector;        // bytes per sector
Shinya Kitaoka 120a6e
  DWORD numberOfFreeClusters;  // free clusters
Shinya Kitaoka 120a6e
  DWORD totalNumberOfClusters;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  BOOL rc = GetDiskFreeSpaceW(diskName.getWideString().c_str(),  // root path
Shinya Kitaoka 120a6e
                              §orsPerCluster,     // sectors per cluster
Shinya Kitaoka 120a6e
                              &bytesPerSector,        // bytes per sector
Shinya Kitaoka 120a6e
                              &numberOfFreeClusters,  // free clusters
Shinya Kitaoka 120a6e
                              &totalNumberOfClusters  // total clusters
Shinya Kitaoka 120a6e
                              );
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!rc)  // eccezione... getLastError etc...
Shinya Kitaoka 120a6e
    throw TSystemException(diskName, "cannot get disk info!");
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    size = (numberOfFreeClusters * sectorsPerCluster * bytesPerSector) >> 10;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  return size;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TINT64 TSystem::getMemorySize(bool onlyPhisicalMemory) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  MEMORYSTATUS buff;
Shinya Kitaoka 120a6e
  GlobalMemoryStatus(&buff);
Shinya Kitaoka 120a6e
  if (onlyPhisicalMemory)
Shinya Kitaoka 120a6e
    return buff.dwTotalPhys >> 10;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return buff.dwTotalPageFile >> 10;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(__sgi)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int physicalMemory;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (swapctl(SC_GETSWAPMAX, &physicalMemory))
Shinya Kitaoka 120a6e
    return ((size_t)0);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return logSwapLibero >> 1;
Toshihiro Shimizu 890ddd
#elif defined(LINUX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  struct sysinfo *sysInfo = (struct sysinfo *)calloc(1, sizeof(struct sysinfo));
Shinya Kitaoka 120a6e
  TINT64 ret              = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!sysinfo(sysInfo))
Shinya Kitaoka 120a6e
    ret = sysInfo->totalram;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    assert(!"sysinfo function failed");
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  free(sysInfo);
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // to be done...
Shinya Kitaoka 120a6e
  return 512 * 1024;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  @ @ @ERROR : PLATFORM NOT SUPPORTED
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::moveFileToRecycleBin(const TFilePath &fp) {
Shinya Kitaoka 9f5a1b
#if defined(_WIN32)
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  // from http://msdn.microsoft.com/msdnmag/issues/01/04/c/default.aspx
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  // Copy pathname to double-NULL-terminated string.
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  wchar_t buf[_MAX_PATH + 1];               // allow one more character
Shinya Kitaoka 120a6e
  wcscpy(buf, fp.getWideString().c_str());  // copy caller's path name
Shinya Kitaoka 120a6e
  buf[wcslen(buf) + 1] = 0;                 // need two NULLs at end
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  SHFILEOPSTRUCTW data;
Shinya Kitaoka 120a6e
  memset(&data, 0, sizeof(SHFILEOPSTRUCTW));
Shinya Kitaoka 120a6e
  data.fFlags |= FOF_SILENT;          // don't report progress
Shinya Kitaoka 120a6e
  data.fFlags |= FOF_NOERRORUI;       // don't report errors
Shinya Kitaoka 120a6e
  data.fFlags |= FOF_NOCONFIRMATION;  // don't confirm delete
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  data.wFunc = FO_DELETE;             // REQUIRED: delete operation
Shinya Kitaoka 120a6e
  data.pFrom = buf;                   // REQUIRED: which file(s)
Shinya Kitaoka 120a6e
  data.pTo   = NULL;                  // MUST be NULL
Shinya Kitaoka 120a6e
  data.fFlags |= FOF_ALLOWUNDO;       // ..send to Recycle Bin
Shinya Kitaoka 120a6e
  int ret = SHFileOperationW(&data);  // do it!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(MACOSX)
Shinya Kitaoka 120a6e
  FSRef foundRef;
Shinya Kitaoka 120a6e
  OSErr err = FSFindFolder(kOnSystemDisk, kTrashFolderType, kDontCreateFolder,
Shinya Kitaoka 120a6e
                           &foundRef);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (err) {
Shinya Kitaoka 120a6e
    assert(false);
Shinya Kitaoka 120a6e
    deleteFile(fp);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  UInt8 path[255];
Shinya Kitaoka 120a6e
  err = FSRefMakePath(&foundRef, path, 254);
Shinya Kitaoka 120a6e
  if (err) {
Shinya Kitaoka 120a6e
    assert(false);
Shinya Kitaoka 120a6e
    deleteFile(fp);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // TFilePath dest = TFilePath(path)+(fp.getName()+fp.getDottedType());
Shinya Kitaoka 120a6e
  string fullNameWithExt = ::to_string(fp);
Shinya Kitaoka 120a6e
  int i                  = fullNameWithExt.rfind("/");
Shinya Kitaoka 120a6e
  string nameWithExt     = fullNameWithExt.substr(i + 1);
Shinya Kitaoka 120a6e
  TFilePath dest         = TFilePath((char *)path) + nameWithExt;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    renameFile(dest, fp);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      copyFile(dest, fp);
Shinya Kitaoka 120a6e
      deleteFile(fp);
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Martin van Zijl 3d473a
#elif defined(LINUX)
Martin van Zijl 3d473a
  //
Martin van Zijl 3d473a
  // From https://stackoverflow.com/questions/17964439/move-files-to-trash-recycle-bin-in-qt
Martin van Zijl 3d473a
  //
Martin van Zijl 3d473a
  QString fileToRecycle = fp.getQString();
Martin van Zijl 3d473a
  QFileInfo FileName(fileToRecycle);
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  QDateTime currentTime(QDateTime::currentDateTime());    // get system time
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  QString trashFilePath = QDir::homePath() + "/.local/share/Trash/files/";    // this folder contains deleted files
Martin van Zijl 3d473a
  QString trashInfoPath = QDir::homePath() + "/.local/share/Trash/info/";     // this folder contains information about the deleted files
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  // check paths exist
Martin van Zijl 3d473a
  if( !QDir(trashFilePath).exists() || !QDir(trashFilePath).exists(trashInfoPath) ) {
Martin van Zijl 3d473a
    outputDebug("Could not find the right paths to send the file to the recycle bin.");
Martin van Zijl 3d473a
    return;
Martin van Zijl 3d473a
  }
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  // create file for the "Trash/info" folder
Martin van Zijl 3d473a
  QFile infoFile(trashInfoPath + FileName.completeBaseName() + "." + FileName.completeSuffix() + ".trashinfo");     // filename+extension+.trashinfo
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  infoFile.open(QIODevice::ReadWrite);
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  QTextStream stream(&infoFile);
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  stream << "[Trash Info]" << endl;
Martin van Zijl 3d473a
  stream << "Path=" + QString(QUrl::toPercentEncoding(FileName.absoluteFilePath(), "~_-./")) << endl;     // convert path to percentage encoded string
Martin van Zijl 3d473a
  stream << "DeletionDate=" + currentTime.toString("yyyy-MM-dd") + "T" + currentTime.toString("hh:mm:ss") << endl;      // get date and time in format YYYY-MM-DDThh:mm:ss
Martin van Zijl 3d473a
Martin van Zijl 3d473a
  infoFile.close();
Toshihiro Shimizu 890ddd
Martin van Zijl 3d473a
  // move the original file to the "Trash/files" folder
Martin van Zijl 3d473a
  QDir file;
Martin van Zijl 3d473a
  file.rename(FileName.absoluteFilePath(), trashFilePath+FileName.completeBaseName() + "." + FileName.completeSuffix());  // rename(original path, trash path)
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  assert(!"Not implemented yet");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TString TSystemException::getMessage() const {
Shinya Kitaoka 120a6e
  wstring msg;
Shinya Kitaoka 120a6e
  switch (m_err) {
Shinya Kitaoka 120a6e
  case -1:
Shinya Kitaoka 120a6e
    msg = m_msg;
Shinya Kitaoka 120a6e
    break;  // // nothing
Shinya Kitaoka 120a6e
  case EEXIST:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": Directory was not created because filename is the name of an "
Shinya Kitaoka 120a6e
        L"existing file, directory, or device";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ENOENT:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": Path was not found, or the named file does not exist or is a null "
Shinya Kitaoka 120a6e
        L"pathname.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ENOTEMPTY:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": Given path is not a directory; directory is not empty; or "
Shinya Kitaoka 120a6e
        L"directory is either current working directory or root directory";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EACCES:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": Search permission is denied by a component of the path prefix, or "
Shinya Kitaoka 120a6e
        L"write permission on the file named by path is denied, or times is "
Shinya Kitaoka 120a6e
        L"NULL, and write access is denied";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EFAULT:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": Times is not NULL and, or points outside the process's allocated "
Shinya Kitaoka 120a6e
        L"address space.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EINTR:
Shinya Kitaoka 120a6e
    msg = L": A signal was caught during the utime system call.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ENAMETOOLONG:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": The length of the path argument exceeds {PATH_MAX}, or the length "
Shinya Kitaoka 120a6e
        L"of a path component exceeds {NAME_MAX} while _POSIX_NO_TRUNC is in "
Shinya Kitaoka 120a6e
        L"effect.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ENOTDIR:
Shinya Kitaoka 120a6e
    msg = L": A component of the path prefix is not a directory.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EPERM:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": The calling process does not have the super-user privilege, the "
Shinya Kitaoka 120a6e
        L"effective user ID is not the owner of the file, and times is not "
Shinya Kitaoka 120a6e
        L"NULL, or the file system containing the file is mounted read-only";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EROFS:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": The current file system level range does not envelop the level of "
Shinya Kitaoka 120a6e
        L"the file named by path, and the calling process does not have the "
Shinya Kitaoka 120a6e
        L"super-user privilege.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ENOSYS:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": When the named file cannot have its time reset.  The file is on a "
Shinya Kitaoka 120a6e
        L"file system that doesn't have this operation.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EMFILE:
Shinya Kitaoka 120a6e
    msg = L": The maximum number of file descriptors are currently open.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ENFILE:
Shinya Kitaoka 120a6e
    msg = L": The system file table is full.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EBADF:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": The file descriptor determined by the DIR stream is no longer "
Shinya Kitaoka 120a6e
        L"valid.  This result occurs if the DIR stream has been closed.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case EINVAL:
Shinya Kitaoka 120a6e
    msg = L": 64-bit and non-64-bit calls were mixed in a sequence of calls.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    msg = L": Unknown error";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Shinya Kitaoka 120a6e
  case ELOOP:
Shinya Kitaoka 120a6e
    msg = L": Too many symbolic links were encountered in translating path.";
Shinya Kitaoka 120a6e
    break;
Toshihiro Shimizu 890ddd
#ifndef MACOSX
Shinya Kitaoka 120a6e
  case EMULTIHOP:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": Components of path require hopping to multiple remote machines and "
Shinya Kitaoka 120a6e
        L"the file system does not allow it.";
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ENOLINK:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": Path points to a remote machine and the link to that machine is no "
Shinya Kitaoka 120a6e
        L"longer active.";
Shinya Kitaoka 120a6e
    break;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(__sgi)
Shinya Kitaoka 120a6e
  case EDIRCORRUPTED:
Shinya Kitaoka 120a6e
    msg = L": The directory is corrupted on disk.";
Shinya Kitaoka 120a6e
    break;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  case EOVERFLOW:
Shinya Kitaoka 120a6e
    msg =
Shinya Kitaoka 120a6e
        L": One of the inode number values or offset values did not fit in 32 "
Shinya Kitaoka 120a6e
        L"bits, and the 64-bit interfaces were not used.";
Shinya Kitaoka 120a6e
    break;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return m_fname.getWideString() + L"\n" + msg;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::touchFile(const TFilePath &path) {
Toshihiro Shimizu 890ddd
#ifndef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // string filename = path.getFullPath();
Shinya Kitaoka 120a6e
  if (TFileStatus(path).doesExist()) {
Shinya Kitaoka 120a6e
    int ret;
Shinya Kitaoka 9eb50d
#ifdef _WIN32
Shinya Kitaoka 120a6e
    ret = _wutime(path.getWideString().c_str(), 0);
Shinya Kitaoka 9eb50d
#else
Shinya Kitaoka 120a6e
    ret = utimes(::to_string(path).c_str(), 0);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
    if (0 != ret) throw TSystemException(path, errno);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    Tofstream file(path);
Shinya Kitaoka 120a6e
    if (!file) {
Shinya Kitaoka 120a6e
      throw TSystemException(path, errno);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    file.close();  // altrimenti il compilatore da' un warning:
Shinya Kitaoka 120a6e
                   // variabile non utilizzata
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------