Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsystem.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
Toshihiro Shimizu 890ddd
#undef PLATFORM
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#define PLATFORM WIN32
Toshihiro Shimizu 890ddd
#include <process.h></process.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
#include "winsock2.h"
Toshihiro Shimizu 890ddd
#include "lmcons.h"
Toshihiro Shimizu 890ddd
#include <sys utime.h=""></sys>
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
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
int gethostname(char *, size_t);
Shinya Kitaoka 120a6e
// int L_cuserid;
Toshihiro Shimizu 890ddd
char *cuserid(char *);
Toshihiro Shimizu 890ddd
char *tempnam(const char *, const char *);
Toshihiro Shimizu 890ddd
}
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
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
const int TSystem::MaxPathLen     = _MAX_PATH;
Shinya Kitaoka 120a6e
const int TSystem::MaxFNameLen    = _MAX_FNAME;
Toshihiro Shimizu 890ddd
const int TSystem::MaxHostNameLen = 64;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::UserReadable   = _S_IREAD;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::UserWritable   = _S_IWRITE;
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::UserExecutable = _S_IEXEC;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OwnerReadWriteExec =
Shinya Kitaoka 120a6e
    TFileStatus::UserReadable | TFileStatus::UserWritable |
Shinya Kitaoka 120a6e
    TFileStatus::UserExecutable;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OwnerReadable   = TFileStatus::UserReadable;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OwnerWritable   = TFileStatus::UserWritable;
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::OwnerExecutable = TFileStatus::UserExecutable;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::GroupReadWriteExec =
Shinya Kitaoka 120a6e
    TFileStatus::OwnerReadWriteExec;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::GroupReadable   = TFileStatus::UserReadable;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::GroupWritable   = TFileStatus::UserWritable;
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::GroupExecutable = TFileStatus::UserExecutable;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OtherReadWriteExec =
Shinya Kitaoka 120a6e
    TFileStatus::UserReadable | TFileStatus::UserWritable |
Shinya Kitaoka 120a6e
    TFileStatus::UserExecutable;
Toshihiro Shimizu 890ddd
;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OtherReadable   = TFileStatus::UserReadable;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OtherWritable   = TFileStatus::UserWritable;
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::OtherExecutable = TFileStatus::UserReadable;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::IfDir = _S_IFDIR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MKDIR _mkdir
Toshihiro Shimizu 890ddd
#define RMDIR _rmdir
Toshihiro Shimizu 890ddd
#define TEMPNAM _tempnam
Toshihiro Shimizu 890ddd
#define ACCESS _access
Toshihiro Shimizu 890ddd
#define STAT _stat
Toshihiro Shimizu 890ddd
#define UTIME _utime
Toshihiro Shimizu 890ddd
#define FTIME _ftime
Toshihiro Shimizu 890ddd
using std::ifstream;
Toshihiro Shimizu 890ddd
using std::ofstream;
Toshihiro Shimizu 890ddd
using std::filebuf;
Shinya Kitaoka 120a6e
#else  // these are common for IRIX & LINUX
Shinya Kitaoka 120a6e
const int TSystem::MaxPathLen     = MAXPATHLEN;
Toshihiro Shimizu 890ddd
const int TSystem::MaxHostNameLen = MAXHOSTNAMELEN;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::OwnerReadWriteExec = S_IRWXU;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OwnerReadable      = S_IRUSR;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OwnerWritable      = S_IWUSR;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OwnerExecutable    = S_IXUSR;
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::GroupReadWriteExec = S_IRWXG;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::GroupReadable      = S_IRGRP;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::GroupWritable      = S_IWGRP;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::GroupExecutable    = S_IXGRP;
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::OtherReadWriteExec = S_IRWXO;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OtherReadable      = S_IROTH;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OtherWritable      = S_IWOTH;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::OtherExecutable    = S_IXOTH;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MKDIR mkdir
Toshihiro Shimizu 890ddd
#define RMDIR rmdir
Toshihiro Shimizu 890ddd
#define TEMPNAM tempnam
Toshihiro Shimizu 890ddd
#define ACCESS access
Toshihiro Shimizu 890ddd
#define STAT stat
Toshihiro Shimizu 890ddd
#define UTIME utime
Toshihiro Shimizu 890ddd
#define FTIME ftime
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __sgi
Shinya Kitaoka 120a6e
const int TSystem::MaxFNameLen                       = MAXNAMELEN;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::UserReadable       = S_IREAD;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::UserWritable       = S_IWRITE;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::UserExecutable     = S_IEXEC;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::IfDir              = S_IFDIR;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
const int TSystem::MaxFNameLen                   = 1024;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::UserReadable   = __S_IREAD;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::UserWritable   = __S_IWRITE;
Toshihiro Shimizu 890ddd
const unsigned short TFileStatus::UserExecutable = __S_IEXEC;
Shinya Kitaoka 120a6e
const unsigned short TFileStatus::IfDir          = __S_IFDIR;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TSystemException &e) {
Shinya Kitaoka 120a6e
  return out << "TSystem Exception : " << e.getMessage() << endl;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
// conversion functions
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void makeTm(const TTime &t, tm *p) {
Shinya Kitaoka 120a6e
  ::memset(p, 0, sizeof(tm));
Shinya Kitaoka 120a6e
  p->tm_year  = t.getYear() - 1900;
Shinya Kitaoka 120a6e
  p->tm_mon   = t.getMonth() - 1;  //[0,11]
Shinya Kitaoka 120a6e
  p->tm_mday  = t.getDay();
Shinya Kitaoka 120a6e
  p->tm_hour  = t.getHour();
Shinya Kitaoka 120a6e
  p->tm_min   = t.getMinutes();
Shinya Kitaoka 120a6e
  p->tm_sec   = t.getSeconds();
Shinya Kitaoka 120a6e
  p->tm_isdst = -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime makeTTime(const tm &p, int msec = 0) {
Shinya Kitaoka 120a6e
  return TTime(p.tm_year + 1900, p.tm_mon + 1, p.tm_mday, p.tm_hour, p.tm_min,
Shinya Kitaoka 120a6e
               p.tm_sec, msec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
time_t makeTimeT(const TTime &t) {
Shinya Kitaoka 120a6e
  tm x;
Shinya Kitaoka 120a6e
  makeTm(t, &x);
Shinya Kitaoka 120a6e
  time_t tt = mktime(&x);
Shinya Kitaoka 120a6e
  return tt;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TDeltaTime::TDeltaTime(long sec, long msec) {
Shinya Kitaoka 120a6e
  // assert((sec && msec>=0) ||(sec==0 ));
Shinya Kitaoka 120a6e
  assert(!((sec < 0 && msec > 0) || (sec > 0 && msec < 0)));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
0   0   ok
Shinya Kitaoka 120a6e
0   1   ok
Shinya Kitaoka 120a6e
1   0   ok
Shinya Kitaoka 120a6e
1   1   ok
Shinya Kitaoka 120a6e
0  -1   ok
Shinya Kitaoka 120a6e
-1   0   ok
Shinya Kitaoka 120a6e
-1  -1   ok
Shinya Kitaoka 120a6e
-1   1   NO
Shinya Kitaoka 120a6e
1  -1   NO
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_dsec  = sec + msec / 1000;
Shinya Kitaoka 120a6e
  m_dmsec = msec % 1000;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TDeltaTime TDeltaTime::operator+(const TDeltaTime &tdt) {
Shinya Kitaoka 120a6e
  long sumSec  = m_dsec + tdt.m_dsec;
Shinya Kitaoka 120a6e
  long sumMsec = m_dmsec + tdt.m_dmsec;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (sumSec > 0 && sumMsec < 0) return TDeltaTime(sumSec - 1, sumMsec + 1000);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (sumSec < 0 && sumMsec > 0) return TDeltaTime(sumSec + 1, sumMsec - 1000);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return TDeltaTime(sumSec, sumMsec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TDeltaTime TDeltaTime::operator-(const TDeltaTime &tdt) {
Shinya Kitaoka 120a6e
  long diffSec  = m_dsec - tdt.m_dsec;
Shinya Kitaoka 120a6e
  long diffMsec = m_dmsec - tdt.m_dmsec;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (diffSec < 0 && diffMsec > 0)
Shinya Kitaoka 120a6e
    return TDeltaTime(diffSec + 1, diffMsec - 1000);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (diffSec > 0 && diffMsec < 0)
Shinya Kitaoka 120a6e
    return TDeltaTime(diffSec - 1, diffMsec + 1000);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return TDeltaTime(diffSec, diffMsec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TDeltaTime &dt) {
Shinya Kitaoka 120a6e
  return out << dt.getSeconds() << "." << dt.getMilliSeconds();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// correggere gli assert => invalid (esiste il metodo bool isValid()const;
Shinya Kitaoka 120a6e
// operazioni con
Toshihiro Shimizu 890ddd
// un TTIme invalid danno un risultato invalid
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//
Shinya Kitaoka 120a6e
TTime::TTime(int yyyy, int mm, int dd, int hh, int min, int sec, int msec) {
Shinya Kitaoka 120a6e
  if (yyyy < 10)
Shinya Kitaoka 120a6e
    yyyy += 2000;
Shinya Kitaoka 120a6e
  else if (yyyy < 99)
Shinya Kitaoka 120a6e
    yyyy += 1900;
Shinya Kitaoka 120a6e
  assert(1970 <= yyyy && yyyy <= 2038);
Shinya Kitaoka 120a6e
  m_y = yyyy;
Shinya Kitaoka 120a6e
  assert(1 <= mm && mm <= 12);
Shinya Kitaoka 120a6e
  m_m = mm;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int table[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Shinya Kitaoka 120a6e
  int ddlimit = mm == 2 ? (28 + (isLeap() ? 1 : 0)) : table[mm - 1];
Shinya Kitaoka 120a6e
  assert(1 <= dd && dd <= ddlimit);
Shinya Kitaoka 120a6e
  m_d = dd;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(0 <= hh && hh <= 23);
Shinya Kitaoka 120a6e
  m_h = hh;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(0 <= min && min <= 59);
Shinya Kitaoka 120a6e
  m_min = min;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(0 <= sec && sec <= 59);
Shinya Kitaoka 120a6e
  m_sec = sec;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(0 <= msec && msec <= 999);
Shinya Kitaoka 120a6e
  m_msec = msec;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime::TTime(const TTime &t)
Shinya Kitaoka 120a6e
    : m_y(t.m_y)
Shinya Kitaoka 120a6e
    , m_m(t.m_m)
Shinya Kitaoka 120a6e
    , m_d(t.m_d)
Shinya Kitaoka 120a6e
    , m_h(t.m_h)
Shinya Kitaoka 120a6e
    , m_min(t.m_min)
Shinya Kitaoka 120a6e
    , m_sec(t.m_sec)
Shinya Kitaoka 120a6e
    , m_msec(t.m_msec) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TTime::operator==(const TTime &time) const {
Shinya Kitaoka 120a6e
  return (m_y == time.m_y) && (m_m == time.m_m) && (m_d == time.m_d) &&
Shinya Kitaoka 120a6e
         (m_h == time.m_h) && (m_min == time.m_min) && (m_sec == time.m_sec) &&
Shinya Kitaoka 120a6e
         (m_msec == time.m_msec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TTime::operator>(const TTime &time) const {
Shinya Kitaoka 120a6e
  return (m_y > time.m_y) || (m_y == time.m_y && m_m > time.m_m) ||
Shinya Kitaoka 120a6e
         (m_y == time.m_y && m_m == time.m_m && m_d > time.m_d) ||
Shinya Kitaoka 120a6e
         (m_y == time.m_y && m_m == time.m_m && m_d == time.m_d &&
Shinya Kitaoka 120a6e
          m_h > time.m_h) ||
Shinya Kitaoka 120a6e
         (m_y == time.m_y && m_m == time.m_m && m_d == time.m_d &&
Shinya Kitaoka 120a6e
          m_h == time.m_h && m_min > time.m_min) ||
Shinya Kitaoka 120a6e
         (m_y == time.m_y && m_m == time.m_m && m_d == time.m_d &&
Shinya Kitaoka 120a6e
          m_h == time.m_h && m_min == time.m_min && m_sec > time.m_sec) ||
Shinya Kitaoka 120a6e
         (m_y == time.m_y && m_m == time.m_m && m_d == time.m_d &&
Shinya Kitaoka 120a6e
          m_h == time.m_h && m_min == time.m_min && m_sec == time.m_sec &&
Shinya Kitaoka 120a6e
          m_msec > time.m_msec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TDeltaTime TTime::operator-(const TTime &t) const {
Shinya Kitaoka 120a6e
  time_t t2 = makeTimeT(*this);
Shinya Kitaoka 120a6e
  assert(t2 != -1);
Shinya Kitaoka 120a6e
  time_t t1 = makeTimeT(t);
Shinya Kitaoka 120a6e
  assert(t1 != -1);
Shinya Kitaoka 120a6e
  double d = difftime(t2, t1);
Shinya Kitaoka 120a6e
  if (m_msec >= t.m_msec) {
Shinya Kitaoka 120a6e
    if (d <= -1)
Shinya Kitaoka 120a6e
      return TDeltaTime((long)(d + 1), -(long)(1000 - (m_msec - t.m_msec)));
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return TDeltaTime((long)d, (long)(m_msec - t.m_msec));
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (d <= 0)
Shinya Kitaoka 120a6e
      return TDeltaTime((long)(d), (long)(m_msec - t.m_msec));
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return TDeltaTime((long)(d - 1), (long)(1000 + m_msec - t.m_msec));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TTime::operator+(const TDeltaTime &tdt) const {
Shinya Kitaoka 120a6e
  tm x;
Shinya Kitaoka 120a6e
  makeTm(*this, &x);
Shinya Kitaoka 120a6e
  int ms = m_msec + tdt.getMilliSeconds();
Shinya Kitaoka 120a6e
  if (ms < 0) {
Shinya Kitaoka 120a6e
    ms += 1000;
Shinya Kitaoka 120a6e
    x.tm_sec += tdt.getSeconds() - 1;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    x.tm_sec += tdt.getSeconds() + ms / 1000;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  mktime(&x);
Shinya Kitaoka 120a6e
  return makeTTime(x, ms % 1000);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TTime::operator-(const TDeltaTime &tdt) const {
Shinya Kitaoka 120a6e
  tm x;
Shinya Kitaoka 120a6e
  makeTm(*this, &x);
Shinya Kitaoka 120a6e
  int ms = m_msec - tdt.getMilliSeconds();
Shinya Kitaoka 120a6e
  if (ms < 0) {
Shinya Kitaoka 120a6e
    ms += 1000;
Shinya Kitaoka 120a6e
    x.tm_sec -= tdt.getSeconds() + 1;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    x.tm_sec -= tdt.getSeconds() + ms / 1000;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  mktime(&x);
Shinya Kitaoka 120a6e
  return makeTTime(x, ms % 1000);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TTime::getFormattedString(char *fmt) const {
Shinya Kitaoka 120a6e
  tm x;
Shinya Kitaoka 120a6e
  makeTm(*this, &x);
Shinya Kitaoka 120a6e
  char strDest[256];
Shinya Kitaoka 120a6e
  if (!fmt)
Shinya Kitaoka 120a6e
    // fmt="%b %d %Y %X";
Shinya Kitaoka 120a6e
    return getDate() + " " + getTime();
Shinya Kitaoka 120a6e
  strftime(strDest, 256, fmt, &x);
Shinya Kitaoka 120a6e
  return string(strDest);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TTime::getDate() const { return getFormattedString("%b %d %Y"); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TTime::getTime() const {  // hh:mm:ss
2fc36c
  stringstream buff_s;
Shinya Kitaoka 120a6e
  buff_s << "." << m_msec << '\0';
2fc36c
  return getFormattedString("%X") + buff_s.str();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TTime::floor() const {
Shinya Kitaoka 120a6e
  return TTime(m_y, m_m, m_d, m_h, m_min, m_sec, 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TTime::ceil() const {
Shinya Kitaoka 120a6e
  if (m_msec == 0)
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return *this + TDeltaTime(0, 1000 - m_msec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFileStatus::TFileStatus(const TFilePath &path) {
Shinya Kitaoka 120a6e
  int acc = ACCESS(path.getFullPath().c_str(), 00);  // 00 == solo esistenza
Shinya Kitaoka 120a6e
  m_exist = acc != -1;
Shinya Kitaoka 120a6e
  // gestire eccezioni controllando il valore di ritorno di access
Shinya Kitaoka 120a6e
  // controllare il valore di errno
Shinya Kitaoka 120a6e
  // int ret=
Shinya Kitaoka 120a6e
  STAT(path.getFullPath().c_str(),
Shinya Kitaoka 120a6e
       &m_fStatus);  // returns 0 if the file-status information is obtained
Shinya Kitaoka 120a6e
  /* if(ret!=0) ::memset(&m_fStatus,0,sizeof(m_fStatus)):*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TFileStatus::getGroup() const {
Toshihiro Shimizu 890ddd
#ifndef WIN32
Shinya Kitaoka 120a6e
  struct group *grp = getgrgid(m_fStatus.st_gid);
Shinya Kitaoka 120a6e
  if (grp) return string(grp->gr_name);
Toshihiro Shimizu 890ddd
#endif
2fc36c
  stringstream buff;
Shinya Kitaoka 120a6e
  buff << m_fStatus.st_gid;
2fc36c
  return buff.str();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TFileStatus::getUser() const {
Toshihiro Shimizu 890ddd
#ifndef WIN32
Shinya Kitaoka 120a6e
  struct passwd *pw = getpwuid(m_fStatus.st_uid);
Shinya Kitaoka 120a6e
  if (pw) return string(pw->pw_name);
Toshihiro Shimizu 890ddd
#endif
2fc36c
  stringstream buff;
Shinya Kitaoka 120a6e
  buff << m_fStatus.st_uid;
2fc36c
  return buff.str();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
long TFileStatus::getSize() const { return (long)m_fStatus.st_size; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TFileStatus::getLastAccessTime() const {
Shinya Kitaoka 120a6e
  struct tm *t = localtime(&(m_fStatus.st_atime));
Shinya Kitaoka 120a6e
  return TTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
Shinya Kitaoka 120a6e
               t->tm_min, t->tm_sec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TFileStatus::getLastModificationTime() const {
Shinya Kitaoka 120a6e
  struct tm *t = localtime(&(m_fStatus.st_mtime));
Shinya Kitaoka 120a6e
  return TTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
Shinya Kitaoka 120a6e
               t->tm_min, t->tm_sec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TFileStatus::getCreationTime() const {
Shinya Kitaoka 120a6e
  struct tm *t = localtime(&(m_fStatus.st_ctime));
Shinya Kitaoka 120a6e
  return TTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
Shinya Kitaoka 120a6e
               t->tm_min, t->tm_sec);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
unsigned long TFileStatus::getPermissions() const { return m_fStatus.st_mode; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TFileStatus::isDirectory() const {
Shinya Kitaoka 120a6e
  return doesExist() && (getPermissions() & TFileStatus::IfDir) != 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TSystem::getHostName() {
Shinya Kitaoka 120a6e
  char hostName[MaxHostNameLen];
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  WSADATA wdata;
Shinya Kitaoka 120a6e
  int err;
Shinya Kitaoka 120a6e
  err = WSAStartup(MAKEWORD(1, 1), &wdata);
Shinya Kitaoka 120a6e
  if (!err) strcpy(hostName, "localhost");  // la rete non e' partita  ?!?
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  gethostname((char *)&hostName, MaxHostNameLen);
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  if (err) WSACleanup();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return hostName;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TSystem::getUserName() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#ifndef UNLEN
Toshihiro Shimizu 890ddd
#define UNLEN (256)
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  const long bufSize = UNLEN;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  const long bufSize                                 = L_cuserid;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  char userName[bufSize + 1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  GetUserName((char *)&userName, (unsigned long *)(&bufSize));
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  cuserid(userName);
Shinya Kitaoka 120a6e
  if (userName[0] == 0x00) strcpy(userName, "User Unknown");
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  return string(userName);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TSystem::getGroupName() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return string();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  struct group *currentGroup;
Shinya Kitaoka 120a6e
  currentGroup = getgrgid(getgid());
Shinya Kitaoka 120a6e
  if (currentGroup)
Shinya Kitaoka 120a6e
    return string(currentGroup->gr_name);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return string();
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
TFilePath TSystem::getHomeDirectory() {
Toshihiro Shimizu 890ddd
#ifndef WIN32
Shinya Kitaoka 120a6e
  struct passwd *pwd = getpwnam(getUserName().c_str());
Shinya Kitaoka 120a6e
  if (!pwd) {
Shinya Kitaoka 120a6e
    return TFilePath();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TFilePath(pwd->pw_dir);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  char *s = getenv("USERPROFILE");
Shinya Kitaoka 120a6e
  if (!s)
Shinya Kitaoka 120a6e
    return TFilePath();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return TFilePath(s);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TSystem::getTempDir() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  // gestire eccezioni se dw==0
Shinya Kitaoka 38fd86
  DWORD dw   = GetTempPath(0, 0);  // non include il terminatore
Shinya Kitaoka 120a6e
  char *path = new char[dw + 1];
Shinya Kitaoka 120a6e
  GetTempPath(dw, path);
Shinya Kitaoka 120a6e
  TFilePath tempDir(path);
Shinya Kitaoka 120a6e
  delete[] path;
Shinya Kitaoka 120a6e
  return tempDir;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  return TFilePath(tmpnam(0)).getParentDir();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TSystem::getTestDir() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  return TFilePath("\\\\sirio\\toonz5.0\\TNZCORE_TEST");
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  return TFilePath("/ULTRA/toonz5.0/TNZCORE_TEST");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TSystem::getSystemValue(const TFilePath &name) {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  string keyName = name.getParentDir().getFullPath();
Shinya Kitaoka 120a6e
  string varName = name.getName();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  HKEY hkey;
Shinya Kitaoka 120a6e
  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName.c_str(), 0, KEY_READ, &hkey) !=
Shinya Kitaoka 120a6e
      ERROR_SUCCESS) {
Shinya Kitaoka 120a6e
    /*key not defined*/
Shinya Kitaoka 120a6e
    return "";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  unsigned char buffer[1024];
Shinya Kitaoka 120a6e
  unsigned long bufferSize = sizeof(buffer);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  string value;
Shinya Kitaoka 120a6e
  if (RegQueryValueEx(hkey, varName.c_str(), NULL, NULL, buffer, &bufferSize) ==
Shinya Kitaoka 120a6e
      ERROR_SUCCESS)
Shinya Kitaoka 120a6e
    value = string((char *)buffer);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  RegCloseKey(hkey);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  char *s = getenv(name.getFullPath().c_str());
Shinya Kitaoka 120a6e
  return string(s ? s : "");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TSystem::getBinDir() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  char buf[MaxPathLen];
Shinya Kitaoka 120a6e
  GetModuleFileName(0, buf, MaxPathLen);
Shinya Kitaoka 120a6e
  return TFilePath(buf).getParentDir();
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  string binroot = TSystem::getSystemValue("BINROOT");
Shinya Kitaoka 120a6e
  if (binroot == "") {
Shinya Kitaoka 120a6e
    assert(!"BINROOT variable undefined");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TFilePath(binroot) + "bin";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TSystem::getUniqueFile(string field) {
Shinya Kitaoka 120a6e
  char *tempName = TEMPNAM(getTempDir().getFullPath().c_str(), field.c_str());
Shinya Kitaoka 120a6e
  if (tempName == 0)
Shinya Kitaoka 120a6e
    throw TSystemException(TFilePath(getTempDir().getFullPath() + field), 1);
Shinya Kitaoka 120a6e
  TFilePath outPath(tempName);
Shinya Kitaoka 120a6e
  free(tempName);
Shinya Kitaoka 120a6e
  return outPath;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TDeltaTime TDeltaTime::second = TDeltaTime(1, 0);
Toshihiro Shimizu 890ddd
const TDeltaTime TDeltaTime::minute = 60 * TDeltaTime::second;
Shinya Kitaoka 120a6e
const TDeltaTime TDeltaTime::hour   = 60 * TDeltaTime::minute;
Shinya Kitaoka 120a6e
const TDeltaTime TDeltaTime::day    = 24 * TDeltaTime::hour;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTime TSystem::getCurrentTime() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  struct _timeb curTime;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  struct timeb curTime;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  FTIME(&curTime);
Shinya Kitaoka 120a6e
  struct tm *localTime = localtime(&(curTime.time));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return TTime(localTime->tm_year + 1900, localTime->tm_mon + 1,
Shinya Kitaoka 120a6e
               localTime->tm_mday, localTime->tm_hour, localTime->tm_min,
Shinya Kitaoka 120a6e
               localTime->tm_sec, curTime.millitm);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
// gestire exception
Shinya Kitaoka 120a6e
void TSystem::mkDir(const TFilePath &path) {
Shinya Kitaoka 120a6e
  if (path == "") {
Shinya Kitaoka 120a6e
    throw TSystemException("mkdir: Empty argument");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TFileStatus(path).doesExist() || path.isRoot()) return;
Shinya Kitaoka 120a6e
  while (!TFileStatus(path.getParentDir()).doesExist() &&
Shinya Kitaoka 120a6e
         !path.getParentDir().isRoot()) {
Shinya Kitaoka 120a6e
    mkDir(path.getParentDir());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  int ret = MKDIR(path.getFullPath().c_str());  // ret e' EEXIST o ENOENT
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  umask(0);
Shinya Kitaoka 120a6e
  mode_t attr = TFileStatus::UserReadable | TFileStatus::UserWritable |
Shinya Kitaoka 120a6e
                TFileStatus::UserExecutable | TFileStatus::OwnerReadWriteExec |
Shinya Kitaoka 120a6e
                TFileStatus::GroupReadWriteExec |
Shinya Kitaoka 120a6e
                TFileStatus::OtherReadWriteExec;
Shinya Kitaoka 120a6e
  int ret = MKDIR(path.getFullPath().c_str(), attr);  // ret e` EEXIST o ENOENT
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  if (ret == -1) throw TSystemException(path, errno);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
// gestire exception
Shinya Kitaoka 120a6e
void TSystem::rmDir(const TFilePath &path) {
Shinya Kitaoka 120a6e
  //  if (!TFileStatus(path).doesExist())
Shinya Kitaoka 120a6e
  //	return;
Shinya Kitaoka 120a6e
  int ret = RMDIR(path.getFullPath().c_str());
Shinya Kitaoka 120a6e
  if (ret == -1)  // ret e' ENOTEMPTY o ENOENT
Shinya Kitaoka 120a6e
    throw TSystemException(path, errno);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::rmDirTree(const TFilePath &path) {
Shinya Kitaoka 120a6e
  TFilePathSet pathSet = readDirectory(path);
Shinya Kitaoka 120a6e
  for (TFilePathSet::iterator it = pathSet.begin(); it != pathSet.end(); it++) {
Shinya Kitaoka 120a6e
    TFilePath path = *it;
Shinya Kitaoka 120a6e
    if (TFileStatus(path).isDirectory())
Shinya Kitaoka 120a6e
      rmDirTree(path);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      deleteFile(path);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  rmDir(path);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::copyDir(const TFilePath &dst, const TFilePath &src) {
Shinya Kitaoka 120a6e
  if (!TFileStatus(dst).doesExist()) mkDir(dst);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePathSet pathSet = readDirectory(src);
Shinya Kitaoka 120a6e
  for (TFilePathSet::iterator it = pathSet.begin(); it != pathSet.end(); it++) {
Shinya Kitaoka 120a6e
    TFilePath path = *it;
Shinya Kitaoka 120a6e
    if (TFileStatus(path).isDirectory())
Shinya Kitaoka 120a6e
      copyDir(path.withParentDir(dst), path);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      copyFile(path.withParentDir(dst), path);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::touchFile(const TFilePath &path) {
Shinya Kitaoka 120a6e
  string filename = path.getFullPath();
Shinya Kitaoka 120a6e
  if (TFileStatus(path).doesExist()) {
Shinya Kitaoka 120a6e
    if (0 != UTIME(filename.c_str(), 0)) throw TSystemException(path, errno);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    ofstream file(filename.c_str());
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
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string 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
  return (char *)lpMsgBuf;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::copyFile(const TFilePath &dst, const TFilePath &src) {
Shinya Kitaoka 120a6e
  assert(dst != src);
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  // o e' meglio usare CopyFileEx??
Shinya Kitaoka 120a6e
  BOOL res = CopyFile(
Shinya Kitaoka 120a6e
      src.getFullPath().c_str(),  // pointer to name of an existing file
Shinya Kitaoka 120a6e
      dst.getFullPath().c_str(),  // pointer to filename to copy to
Shinya Kitaoka 120a6e
      TRUE);                      // flag for operation if file exists
Shinya Kitaoka 120a6e
  if (res == 0) {
Shinya Kitaoka 120a6e
    throw TSystemException(src, getFormattedMessage(GetLastError()));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  ifstream fpin(src.getFullPath().c_str(), ios::in);
Shinya Kitaoka 120a6e
  ofstream fpout(dst.getFullPath().c_str(), ios::out);
Shinya Kitaoka 120a6e
  if (!fpin || !fpout) throw TSystemException(src, "unable to copy file");
Shinya Kitaoka 120a6e
  int c = fpin.get();
Shinya Kitaoka 120a6e
  while (!fpin.eof()) {
Shinya Kitaoka 120a6e
    fpout.put(c);
Shinya Kitaoka 120a6e
    c = fpin.get();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::renameFile(const TFilePath &dst, const TFilePath &src) {
Shinya Kitaoka 120a6e
  int ret = rename(src.getFullPath().c_str(), dst.getFullPath().c_str());
Shinya Kitaoka 120a6e
  if (ret != 0) throw TSystemException(dst, errno);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// gestire gli errori con GetLastError?
Shinya Kitaoka 120a6e
void TSystem::deleteFile(const TFilePath &dst) {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  BOOL res = DeleteFile(
Shinya Kitaoka 120a6e
      dst.getFullPath().c_str());  // pointer to name of file to delete
Shinya Kitaoka 120a6e
  // se fallisce perche' un file e' aperto chiudiamo prima il file?
Shinya Kitaoka 120a6e
  // To close an open file, use the CloseHandle function.
Shinya Kitaoka 120a6e
  if (res == 0)
Shinya Kitaoka 120a6e
    throw TSystemException(dst, getFormattedMessage(GetLastError()));
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  int ret = remove(dst.getFullPath().c_str());
Shinya Kitaoka 120a6e
  if (ret != 0) throw TSystemException(dst, errno);
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) {
Toshihiro Shimizu 890ddd
#ifdef 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
Shinya Kitaoka 120a6e
  // Copy pathname to double-NULL-terminated string.
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  char buf[_MAX_PATH + 1];                // allow one more character
Shinya Kitaoka 120a6e
  strcpy(buf, fp.getFullPath().c_str());  // copy caller's path name
Shinya Kitaoka 120a6e
  buf[strlen(buf) + 1] = 0;               // need two NULLs at end
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  SHFILEOPSTRUCT data;
Shinya Kitaoka 120a6e
  memset(&data, 0, sizeof(SHFILEOPSTRUCT));
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
  SHFileOperation(&data);        // do it!
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
void TSystem::readDirectory(TFilePathSet &dst, const TFilePath &path) {
Shinya Kitaoka 120a6e
  if (!TFileStatus(path).isDirectory()) {
Shinya Kitaoka 120a6e
    throw TSystemException(path, " is not a directory");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  WIN32_FIND_DATA findFileData;
Shinya Kitaoka 120a6e
  string search = (path + "*").getFullPath();
Shinya Kitaoka 120a6e
  HANDLE h      = FindFirstFile(search.c_str(), &findFileData);
Shinya Kitaoka 120a6e
  if (h == INVALID_HANDLE_VALUE)
Shinya Kitaoka 120a6e
    throw TSystemException(path, getFormattedMessage(GetLastError()));
Shinya Kitaoka 120a6e
  do {
Shinya Kitaoka 120a6e
    string filename(findFileData.cFileName);
Shinya Kitaoka 120a6e
    if (filename == "." || filename == "..") continue;
Shinya Kitaoka 120a6e
    TFilePath son = path + filename;
Shinya Kitaoka 120a6e
    dst.push_back(son);
Shinya Kitaoka 120a6e
  } while (FindNextFile(h, &findFileData));
Shinya Kitaoka 120a6e
  FindClose(h);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  DIR *dirp;
Shinya Kitaoka 120a6e
  struct direct *directp;
Shinya Kitaoka 120a6e
  dirp = opendir(path.getFullPath().c_str());
Shinya Kitaoka 120a6e
  if (dirp == 0) throw TSystemException(path, errno);
Shinya Kitaoka 120a6e
  while (directp = readdir(dirp)) {
Shinya Kitaoka 120a6e
    string filename(directp->d_name);
Shinya Kitaoka 120a6e
    if (filename == "." || filename == "..") continue;
Shinya Kitaoka 120a6e
    TFilePath son = path + filename;
Shinya Kitaoka 120a6e
    dst.push_back(son);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  closedir(dirp);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::readDirectory(TFilePathSet &dst, const TFilePathSet &pathSet) {
Shinya Kitaoka 120a6e
  for (TFilePathSet::const_iterator it = pathSet.begin(); it != pathSet.end();
Shinya Kitaoka 120a6e
       it++)
Shinya Kitaoka 120a6e
    readDirectory(dst, *it);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePathSet TSystem::readDirectory(const TFilePath &path) {
Shinya Kitaoka 120a6e
  TFilePathSet filePathSet;
Shinya Kitaoka 120a6e
  readDirectory(filePathSet, path);
Shinya Kitaoka 120a6e
  return filePathSet;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePathSet TSystem::readDirectory(const TFilePathSet &pathSet) {
Shinya Kitaoka 120a6e
  TFilePathSet dst;
Shinya Kitaoka 120a6e
  readDirectory(dst, pathSet);
Shinya Kitaoka 120a6e
  return dst;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::readDirectoryTree(TFilePathSet &dst, const TFilePath &path) {
Shinya Kitaoka 120a6e
  if (!TFileStatus(path).isDirectory()) {
Shinya Kitaoka 120a6e
    throw TSystemException(path, " is not a directory");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  WIN32_FIND_DATA findFileData;
Shinya Kitaoka 120a6e
  string search = (path + "*").getFullPath();
Shinya Kitaoka 120a6e
  HANDLE h      = FindFirstFile(search.c_str(), &findFileData);
Shinya Kitaoka 120a6e
  if (h == INVALID_HANDLE_VALUE) return;
Shinya Kitaoka 120a6e
  do {
Shinya Kitaoka 120a6e
    string filename(findFileData.cFileName);
Shinya Kitaoka 120a6e
    if (filename == "." || filename == "..") continue;
Shinya Kitaoka 120a6e
    TFilePath son = path + findFileData.cFileName;
Shinya Kitaoka 120a6e
    if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
Shinya Kitaoka 120a6e
      readDirectoryTree(dst, son);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      dst.push_back(son);
Shinya Kitaoka 120a6e
  } while (FindNextFile(h, &findFileData));
Shinya Kitaoka 120a6e
  FindClose(h);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  DIR *dirp;
Shinya Kitaoka 120a6e
  struct direct *directp;
Shinya Kitaoka 120a6e
  dirp = opendir(path.getFullPath().c_str());
Shinya Kitaoka 120a6e
  if (dirp == 0) throw TSystemException(path, errno);
Shinya Kitaoka 120a6e
  while (directp = readdir(dirp)) {
Shinya Kitaoka 120a6e
    string filename(directp->d_name);
Shinya Kitaoka 120a6e
    if (filename == "." || filename == "..") continue;
Shinya Kitaoka 120a6e
    TFilePath son = path + filename;
Shinya Kitaoka 120a6e
    if (TFileStatus(son).isDirectory())
Shinya Kitaoka 120a6e
      readDirectoryTree(dst, son);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      dst.push_back(son);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  closedir(dirp);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::readDirectoryTree(TFilePathSet &dst,
Shinya Kitaoka 120a6e
                                const TFilePathSet &pathSet) {
Shinya Kitaoka 120a6e
  for (TFilePathSet::const_iterator it = pathSet.begin(); it != pathSet.end();
Shinya Kitaoka 120a6e
       it++)
Shinya Kitaoka 120a6e
    readDirectoryTree(dst, *it);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePathSet TSystem::readDirectoryTree(const TFilePath &path) {
Shinya Kitaoka 120a6e
  TFilePathSet dst;
Shinya Kitaoka 120a6e
  readDirectoryTree(dst, path);
Shinya Kitaoka 120a6e
  return dst;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePathSet TSystem::readDirectoryTree(const TFilePathSet &pathSet) {
Shinya Kitaoka 120a6e
  TFilePathSet dst;
Shinya Kitaoka 120a6e
  readDirectoryTree(dst, pathSet);
Shinya Kitaoka 120a6e
  return dst;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePathSet TSystem::packLevelNames(const TFilePathSet &fps) {
Shinya Kitaoka 120a6e
  std::set<tfilepath> tmpSet;</tfilepath>
Shinya Kitaoka 120a6e
  TFilePathSet::const_iterator cit;
Shinya Kitaoka 120a6e
  for (cit = fps.begin(); cit != fps.end(); ++cit)
Shinya Kitaoka 120a6e
    tmpSet.insert(cit->getParentDir() + cit->getLevelName());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePathSet fps2;
Shinya Kitaoka 120a6e
  for (std::set<tfilepath>::const_iterator c_sit = tmpSet.begin();</tfilepath>
Shinya Kitaoka 120a6e
       c_sit != tmpSet.end(); ++c_sit) {
Shinya Kitaoka 120a6e
    fps2.push_back(*c_sit);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return fps2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePathSet TSystem::getDisks() {
Shinya Kitaoka 120a6e
  TFilePathSet filePathSet;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  DWORD size   = GetLogicalDriveStrings(0, NULL);
Shinya Kitaoka 120a6e
  char *buffer = new char[size + 1];
Shinya Kitaoka 120a6e
  char *ptr    = buffer;
Shinya Kitaoka 120a6e
  DWORD rc     = GetLogicalDriveStrings(size, buffer);
Shinya Kitaoka 120a6e
  if (rc == 0) throw TSystemException("", getFormattedMessage(GetLastError()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (*ptr) {
Shinya Kitaoka 120a6e
    filePathSet.push_back(ptr);
Shinya Kitaoka 120a6e
    while (*ptr) ptr++;
Shinya Kitaoka 120a6e
    ptr++;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  delete[] buffer;
Shinya Kitaoka 120a6e
  return filePathSet;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  FILE *f = setmntent("/etc/fstab", "r");
Shinya Kitaoka 120a6e
  if (f) {
Shinya Kitaoka 120a6e
    while (struct mntent *m = getmntent(f)) {
Shinya Kitaoka 120a6e
      // cout << "machine "<< m->mnt_fsname << " dir " <<m->mnt_dir << " type "</m->
Shinya Kitaoka 120a6e
      // <<  m->mnt_type << endl;
Shinya Kitaoka 120a6e
      filePathSet.push_back(m->mnt_dir);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    endmntent(f);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return filePathSet;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ULONG TSystem::getDiskSize(const TFilePath &diskName) {
Shinya Kitaoka 120a6e
  ULONG size = 0;
Shinya Kitaoka 120a6e
  if (!diskName.isAbsolute()) {
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#ifndef WIN32
Shinya Kitaoka 120a6e
  struct statfs buf;
Toshihiro Shimizu 890ddd
#ifdef __sgi
Shinya Kitaoka 120a6e
  statfs(diskName.getFullPath().c_str(), &buf, sizeof(struct statfs), 0);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  statfs(diskName.getFullPath().c_str(), &buf);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  size = (ULONG)((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 = GetDiskFreeSpace(diskName.getFullPath().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
ULONG TSystem::getFreeDiskSize(const TFilePath &diskName) {
Shinya Kitaoka 120a6e
  ULONG size = 0;
Shinya Kitaoka 120a6e
  if (!diskName.isAbsolute()) {
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#ifndef WIN32
Shinya Kitaoka 120a6e
  struct statfs buf;
Toshihiro Shimizu 890ddd
#ifdef __sgi
Shinya Kitaoka 120a6e
  statfs(diskName.getFullPath().c_str(), &buf, sizeof(struct statfs), 0);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  statfs(diskName.getFullPath().c_str(), &buf);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  size = (ULONG)(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 = GetDiskFreeSpace(diskName.getFullPath().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, getFormattedMessage(GetLastError()));
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
ULONG TSystem::getFreeMemorySize() {
Shinya Kitaoka 120a6e
  ULONG totalFree = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  ULONG virtualFree  = 0;
Shinya Kitaoka 120a6e
  ULONG physicalFree = 0;
Shinya Kitaoka 120a6e
  MEMORYSTATUS buff;
Shinya Kitaoka 120a6e
  GlobalMemoryStatus(&buff);
Shinya Kitaoka 120a6e
  physicalFree = buff.dwAvailPhys;
Shinya Kitaoka 120a6e
  virtualFree  = buff.dwAvailVirtual;
Shinya Kitaoka 120a6e
  totalFree    = (physicalFree + virtualFree) >> 10;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef __sgi
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
  ULONG virtualFree  = 0;
Shinya Kitaoka 120a6e
  ULONG physicalFree = 0;
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
#else
Toshihiro Shimizu 890ddd
#ifdef 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
    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
#else
Shinya Kitaoka 120a6e
  @ @ @ERROR : PLATFORM NOT SUPPORTED
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
#endif  //__sgi
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#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
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TTime &t) {
Shinya Kitaoka 120a6e
  return out << t.getDate() << " " << t.getTime();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
extern "C" long sginap(long ticks);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef LINUX
Toshihiro Shimizu 890ddd
extern "C" int usleep(unsigned int);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::sleep(const TDeltaTime &delay) {
Shinya Kitaoka 120a6e
  int ms = delay.getSeconds() * 1000;
Shinya Kitaoka 120a6e
  assert(ms >= delay.getSeconds());
Shinya Kitaoka 120a6e
  ms += delay.getMilliSeconds();
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  Sleep(ms);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef __sgi
Shinya Kitaoka 120a6e
  sginap(ms * CLK_TCK / 1000);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef LINUX
Shinya Kitaoka 120a6e
  ms *= 1000;
Shinya Kitaoka 120a6e
  usleep(ms);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSystemException::TSystemException(const TFilePath &fname, int err)
Shinya Kitaoka 120a6e
    : m_fname(fname.getFullPath())
Shinya Kitaoka 120a6e
    , m_err(err)
Shinya Kitaoka 120a6e
    , m_msg("")
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
{}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSystemException::TSystemException(const TFilePath &fname, const string &msg)
Shinya Kitaoka 120a6e
    : m_fname(fname.getFullPath()), m_err(-1), m_msg(msg) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSystemException::TSystemException(const string &msg)
Shinya Kitaoka 120a6e
    : m_fname(""), m_err(-1), m_msg(msg) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TSystemException::getMessage() const {
Shinya Kitaoka 120a6e
  string msg = m_fname;
Shinya Kitaoka 120a6e
  switch (m_err) {
Shinya Kitaoka 120a6e
  case -1:
Shinya Kitaoka 120a6e
    // nothing
Shinya Kitaoka 120a6e
    msg += ": ";
Shinya Kitaoka 120a6e
    msg += m_msg;
Shinya Kitaoka 120a6e
    CASE EEXIST : msg +=
Shinya Kitaoka 120a6e
                  ": Directory was not created because filename is the name of "
Shinya Kitaoka 120a6e
                  "an existing file, directory, or device";
Shinya Kitaoka 120a6e
    CASE ENOENT : msg +=
Shinya Kitaoka 120a6e
                  ": Path was not found, or the named file does not exist or "
Shinya Kitaoka 120a6e
                  "is a null pathname.";
Shinya Kitaoka 120a6e
    CASE ENOTEMPTY : msg +=
Shinya Kitaoka 120a6e
                     ": Given path is not a directory; directory is not empty; "
Shinya Kitaoka 120a6e
                     "or directory is either current working directory or root "
Shinya Kitaoka 120a6e
                     "directory";
Shinya Kitaoka 120a6e
    CASE EACCES : msg +=
Shinya Kitaoka 120a6e
                  ": Search permission is denied by a component of the path "
Shinya Kitaoka 120a6e
                  "prefix, or write permission on the file named by path is "
Shinya Kitaoka 120a6e
                  "denied, or times is NULL, and write access is denied";
Shinya Kitaoka 120a6e
    CASE EFAULT : msg +=
Shinya Kitaoka 120a6e
                  ": Times is not NULL and, or points outside the process's "
Shinya Kitaoka 120a6e
                  "allocated address space.";
Shinya Kitaoka 120a6e
    CASE EINTR : msg += ": A signal was caught during the utime system call.";
Shinya Kitaoka 120a6e
    CASE ENAMETOOLONG : msg +=
Shinya Kitaoka 120a6e
                        ": The length of the path argument exceeds {PATH_MAX}, "
Shinya Kitaoka 120a6e
                        "or the length of a path component exceeds {NAME_MAX} "
Shinya Kitaoka 120a6e
                        "while _POSIX_NO_TRUNC is in effect.";
Shinya Kitaoka 120a6e
    CASE ENOTDIR : msg +=
Shinya Kitaoka 120a6e
                   ": A component of the path prefix is not a directory.";
Shinya Kitaoka 120a6e
    CASE EPERM : msg +=
Shinya Kitaoka 120a6e
                 ": The calling process does not have the super-user "
Shinya Kitaoka 120a6e
                 "privilege, the effective user ID is not the owner of the "
Shinya Kitaoka 120a6e
                 "file, and times is not NULL, or the file system containing "
Shinya Kitaoka 120a6e
                 "the file is mounted read-only";
Shinya Kitaoka 120a6e
    CASE EROFS : msg +=
Shinya Kitaoka 120a6e
                 ": The current file system level range does not envelop the "
Shinya Kitaoka 120a6e
                 "level of the file named by path, and the calling process "
Shinya Kitaoka 120a6e
                 "does not have the super-user privilege.";
Shinya Kitaoka 120a6e
    CASE ENOSYS : msg +=
Shinya Kitaoka 120a6e
                  ": When the named file cannot have its time reset.  The file "
Shinya Kitaoka 120a6e
                  "is on a file system that doesn't have this operation.";
Shinya Kitaoka 120a6e
    CASE EMFILE
Shinya Kitaoka 120a6e
        : msg += ": The maximum number of file descriptors are currently open.";
Shinya Kitaoka 120a6e
    CASE ENFILE : msg += ": The system file table is full.";
Shinya Kitaoka 120a6e
    CASE EBADF : msg +=
Shinya Kitaoka 120a6e
                 ": The file descriptor determined by the DIR stream is no "
Shinya Kitaoka 120a6e
                 "longer valid.  This result occurs if the DIR stream has been "
Shinya Kitaoka 120a6e
                 "closed.";
Shinya Kitaoka 120a6e
    CASE EINVAL
Shinya Kitaoka 120a6e
        : msg +=
Shinya Kitaoka 120a6e
          ": 64-bit and non-64-bit calls were mixed in a sequence of calls.";
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    msg += ": Unknown error";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef WIN32
Shinya Kitaoka 120a6e
    CASE ELOOP
Shinya Kitaoka 120a6e
        : msg +=
Shinya Kitaoka 120a6e
          ": Too many symbolic links were encountered in translating path.";
Shinya Kitaoka 120a6e
    CASE EMULTIHOP : msg +=
Shinya Kitaoka 120a6e
                     ": Components of path require hopping to multiple remote "
Shinya Kitaoka 120a6e
                     "machines and the file system does not allow it.";
Shinya Kitaoka 120a6e
    CASE ENOLINK : msg +=
Shinya Kitaoka 120a6e
                   ": Path points to a remote machine and the link to that "
Shinya Kitaoka 120a6e
                   "machine is no longer active.";
Toshihiro Shimizu 890ddd
#ifndef LINUX
Shinya Kitaoka 120a6e
    CASE EDIRCORRUPTED : msg += ": The directory is corrupted on disk.";
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
    CASE EOVERFLOW : msg +=
Shinya Kitaoka 120a6e
                     ": One of the inode number values or offset values did "
Shinya Kitaoka 120a6e
                     "not fit in 32 bits, and the 64-bit interfaces were not "
Shinya Kitaoka 120a6e
                     "used.";
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return msg;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static const char *TnzLibMainProcName = "TLibMain";
Toshihiro Shimizu 890ddd
static std::map<string, *="" const="" tplugininfo=""> PluginTable;</string,>
Toshihiro Shimizu 890ddd
static std::set<tnzlibmainproctype *=""> PluginMainTable;</tnzlibmainproctype>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
static std::vector<hinstance> PluginInstanceTable;</hinstance>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
static std::vector<void *=""> PluginInstanceTable;</void>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
extern "C" void unloadPlugins() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  for (std::vector<hinstance>::iterator it = PluginInstanceTable.begin();</hinstance>
Shinya Kitaoka 120a6e
       it != PluginInstanceTable.end(); ++it)
Shinya Kitaoka 120a6e
    FreeLibrary(*it);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  for (std::vector<void *="">::iterator it = PluginInstanceTable.begin();</void>
Shinya Kitaoka 120a6e
       it != PluginInstanceTable.end(); ++it)
Shinya Kitaoka 120a6e
    dlclose(*it);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  PluginInstanceTable.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::unLoadPlugins() { unloadPlugins(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::loadPlugins(const TFilePath &dir) {
Shinya Kitaoka 120a6e
  static bool cbReg = false;
Shinya Kitaoka 120a6e
  if (!cbReg) {
Shinya Kitaoka 120a6e
    cbReg = true;
Shinya Kitaoka 120a6e
    atexit(unloadPlugins);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  string extension = "dll";
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  string extension = "so";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePathSet dirContent = TSystem::readDirectory(dir);
Shinya Kitaoka 120a6e
  if (dirContent.empty()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (TFilePathSet::iterator it = dirContent.begin(); it != dirContent.end();
Shinya Kitaoka 120a6e
       it++) {
Shinya Kitaoka 120a6e
    TFilePath fp = *it;
Shinya Kitaoka 120a6e
    if (fp.getType() != extension) continue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// cout << "Loading..." << fp << endl;
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
    HINSTANCE handle = LoadLibrary(fp.getFullPath().c_str());
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    void *handle   = dlopen(fp.getFullPath().c_str(), RTLD_LAZY);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (!handle) {
Toshihiro Shimizu 890ddd
// non riesce a caricare la libreria;
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
      DWORD err = GetLastError();
Shinya Kitaoka 120a6e
      string s;
Shinya Kitaoka 120a6e
      s = "*Error* unable to load " + fp.getFullPath() + ": " +
Shinya Kitaoka 120a6e
          getFormattedMessage(err) + '\0';
Shinya Kitaoka 120a6e
      TSystem::outputDebug(s);
Shinya Kitaoka 120a6e
#else
Shinya Kitaoka 120a6e
      cout << "*ERROR* couldn't load " << fp << ":";
Shinya Kitaoka 120a6e
      cout << dlerror() << endl;
Shinya Kitaoka 120a6e
#endif
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      PluginInstanceTable.push_back(handle);
Shinya Kitaoka 120a6e
// cout << "loaded" << endl;
Shinya Kitaoka 120a6e
#ifdef WIN32
Shinya Kitaoka 120a6e
      TnzLibMainProcType *tnzLibMain =
Shinya Kitaoka 120a6e
          (TnzLibMainProcType *)GetProcAddress(handle, TnzLibMainProcName);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
      TnzLibMainProcType *tnzLibMain =
Shinya Kitaoka 120a6e
          (TnzLibMainProcType *)dlsym(handle, TnzLibMainProcName);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
      if (!tnzLibMain) {
Shinya Kitaoka 120a6e
        // La libreria non esporta TLibMain;
Shinya Kitaoka 120a6e
        // per ora niente messaggi di errore
Shinya Kitaoka 120a6e
        // cout<< "Unable to load TLibMain" << endl;
Shinya Kitaoka 120a6e
        /*
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
FreeLibrary(handle);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
dlclose(handle);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        std::set<tnzlibmainproctype *="">::iterator it;</tnzlibmainproctype>
Shinya Kitaoka 120a6e
        it = PluginMainTable.find(tnzLibMain);
Shinya Kitaoka 120a6e
        if (it == PluginMainTable.end()) {
Shinya Kitaoka 120a6e
          PluginMainTable.insert(tnzLibMain);
Shinya Kitaoka 120a6e
          const TPluginInfo *info = tnzLibMain();
Shinya Kitaoka 120a6e
          if (info) {
Shinya Kitaoka 120a6e
            PluginTable[info->getName()] = info;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }  // if(tnzLibMain)
Shinya Kitaoka 120a6e
    }    // if(handle)
Shinya Kitaoka 120a6e
  }      // for
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::loadStandardPlugins() {
Shinya Kitaoka 120a6e
  static bool alreadyDone = false;
Shinya Kitaoka 120a6e
  if (alreadyDone) return;
Shinya Kitaoka 120a6e
  alreadyDone          = true;
Shinya Kitaoka 120a6e
  TFilePath pluginsDir = TSystem::getBinDir() + "plugins";
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // cout << "loading standard plugins ... " << pluginsDir << endl;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::loadPlugins(pluginsDir + "io");
Shinya Kitaoka 120a6e
  } catch (const TException &e) {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
    MessageBox(0, e.getMessage().c_str(), "Error loading plugin", MB_OK);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    cout << e.getMessage() << endl;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::loadPlugins(pluginsDir + "fx");
Shinya Kitaoka 120a6e
  } catch (const TException &e) {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
    MessageBox(0, e.getMessage().c_str(), "Error loading plugin", MB_OK);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    cout << e.getMessage() << endl;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // cout << "done ... " << endl;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TPluginInfo *TSystem::getLoadedPluginInfo(string name) {
Shinya Kitaoka 120a6e
  std::map<string, *="" const="" tplugininfo="">::iterator it;</string,>
Shinya Kitaoka 120a6e
  it = PluginTable.find(name);
Shinya Kitaoka 120a6e
  if (it == PluginTable.end())
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return it->second;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::showDocument(const TFilePath &path) {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  int ret = (int)ShellExecute(0, "open", path.getFullPath().c_str(), 0, 0,
Shinya Kitaoka 120a6e
                              SW_SHOWNORMAL);
Shinya Kitaoka 120a6e
  if (ret <= 32) {
Shinya Kitaoka 120a6e
    throw TException(path.getFullPath() + " : can't open");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  string cmd = "mediaplayer ";
Shinya Kitaoka 120a6e
  cmd        = cmd + path.getFullPath();
Shinya Kitaoka 120a6e
  system(cmd.c_str());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TSystem::getProcessorCount() {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  SYSTEM_INFO sysInfo;
Shinya Kitaoka 120a6e
  GetSystemInfo(&sysInfo);
Shinya Kitaoka 120a6e
  return sysInfo.dwNumberOfProcessors;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef __sgi
Shinya Kitaoka 120a6e
  return sysconf(_SC_NPROC_CONF);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  return sysconf(_SC_NPROCESSORS_CONF);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSystem::outputDebug(string s) {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  OutputDebugString(s.c_str());
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  cerr << s << endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}