Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
//#define UNICODE  // per le funzioni di conversione da/a UNC
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#include <lm.h></lm.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const char slash = '\\';
Toshihiro Shimizu 890ddd
const char auxslash = '/';
Toshihiro Shimizu 890ddd
const char wslash = L'\\';
Toshihiro Shimizu 890ddd
const char wauxslash = L'/';
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
const char slash = '/';
Toshihiro Shimizu 890ddd
const char auxslash = '\\';
Toshihiro Shimizu 890ddd
const char wslash = '/';
Toshihiro Shimizu 890ddd
const char wauxslash = '\\';
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
//#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
//#include <tcommon.h>   contenuto in tconvert.h</tcommon.h>
Toshihiro Shimizu 890ddd
#include <ctype.h></ctype.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::m_underscoreFormatAllowed = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-- fromSeg位置 と toSeg位置は含まず、それらの間に挟まれている文字列が「数字4ケタ」ならtrueを返す --*/
Toshihiro Shimizu 890ddd
bool isNumbers(std::wstring str, int fromSeg, int toSeg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (toSeg - fromSeg != 5)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	for (int pos = fromSeg + 1; pos < toSeg; pos++) {
Toshihiro Shimizu 890ddd
		if (str[pos] < '0' || str[pos] > '9')
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//TFrameId::operator string() const
Toshihiro Shimizu 890ddd
string TFrameId::expand(FrameFormat format) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_frame == EMPTY_FRAME)
Toshihiro Shimizu 890ddd
		return "";
Toshihiro Shimizu 890ddd
	else if (m_frame == NO_FRAME)
Toshihiro Shimizu 890ddd
		return "-";
Toshihiro Shimizu 890ddd
	char buffer[80];
Toshihiro Shimizu 890ddd
	ostrstream o_buff(buffer, sizeof(buffer));
Toshihiro Shimizu 890ddd
	if (format == FOUR_ZEROS || format == UNDERSCORE_FOUR_ZEROS) {
Toshihiro Shimizu 890ddd
		o_buff.fill('0');
Toshihiro Shimizu 890ddd
		o_buff.width(4);
Toshihiro Shimizu 890ddd
		o_buff << m_frame;
Toshihiro Shimizu 890ddd
		o_buff.width(0);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		o_buff << m_frame;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (m_letter != '\0')
Toshihiro Shimizu 890ddd
		o_buff << m_letter;
Toshihiro Shimizu 890ddd
	int len = o_buff.pcount();
Toshihiro Shimizu 890ddd
	return string(buffer, len);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TFrameId &TFrameId::operator++()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	++m_frame;
Toshihiro Shimizu 890ddd
	m_letter = 0;
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TFrameId &TFrameId::operator--()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_letter > 0)
Toshihiro Shimizu 890ddd
		m_letter = 0;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		--m_frame;
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline bool isSlash(char c)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return c == slash || c == auxslash;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline bool isSlash(wchar_t c)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return c == wslash || c == wauxslash;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// cerca l'ultimo '/' o '\'. Se non c'e' ritorna -1
Toshihiro Shimizu 890ddd
// di modo che la sottostringa che parte da getLastSlash() + 1 e' nome.frame.tipo
Toshihiro Shimizu 890ddd
inline int getLastSlash(const wstring &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = path.length() - 1; i >= 0 && !isSlash(path[i]); i--) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return i;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void TFilePath::setPath(string path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
bool isUncName = false;
Toshihiro Shimizu 890ddd
  // elimino i '//', './' e '/' finali; raddrizzo gli slash 'storti'. 
Toshihiro Shimizu 890ddd
  // se il path comincia con  "<alpha>:" aggiungo uno slash</alpha>
Toshihiro Shimizu 890ddd
  int length =path.length();   
Toshihiro Shimizu 890ddd
  int pos = 0;
Toshihiro Shimizu 890ddd
  if(path.length()>=2 && isalpha(path[0]) && path[1] == ':')
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
     m_path.append(path,0,2);
Toshihiro Shimizu 890ddd
     pos=2;
Toshihiro Shimizu 890ddd
     if(path.length()==2 || !isSlash(path[pos])) m_path.append(1,slash);
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
  else  //se si tratta di un path in formato UNC e' del tipo "\\\\MachineName"
Toshihiro Shimizu 890ddd
     	//RICONTROLLARE! SE SI HA IP ADDRESS FALLIVA!
Toshihiro Shimizu 890ddd
    if (path.length() >= 3 && path[0] == '\\' &&  path[1] == '\\' && (isalpha(path[2]) || isdigit(path[2])) )
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
      isUncName = true;
Toshihiro Shimizu 890ddd
      m_path.append(path,0,3);
Toshihiro Shimizu 890ddd
      pos = 3;
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
  for(;pos
Toshihiro Shimizu 890ddd
    if(path[pos] == '.')
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      pos++;
Toshihiro Shimizu 890ddd
      if(pos>=length) 
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
        if(pos>1) m_path.append(1,'.');
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
      else if(!isSlash(path[pos])) m_path.append(path,pos-1,2);
Toshihiro Shimizu 890ddd
      else {
Toshihiro Shimizu 890ddd
         while(pos+1
Toshihiro Shimizu 890ddd
           pos++;
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
    else if(isSlash(path[pos]))
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      do pos++;
Toshihiro Shimizu 890ddd
      while(pos
Toshihiro Shimizu 890ddd
      pos--;
Toshihiro Shimizu 890ddd
      m_path.append(1,slash);
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
    else
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      m_path.append(1,path[pos]);
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    // rimuovo l'eventuale '/' finale (a meno che m_path == "/" o m_path == "<letter>:\"</letter>
Toshihiro Shimizu 890ddd
    // oppure sia UNC (Windows only) )
Toshihiro Shimizu 890ddd
    if(!(m_path.length()==1 && m_path[0] == slash ||
Toshihiro Shimizu 890ddd
         m_path.length()==3 && isalpha(m_path[0]) && m_path[1] == ':' && m_path[2] == slash)
Toshihiro Shimizu 890ddd
      && m_path.length()>1 && m_path[m_path.length()-1] == slash)
Toshihiro Shimizu 890ddd
      m_path.erase(m_path.length()-1, 1);
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  if (isUncName && m_path.find_last_of('\\') == 1) // e' indicato solo il nome della macchina...
Toshihiro Shimizu 890ddd
    m_path.append(1, slash);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void append(string &out, wchar_t c)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  if(32 <= c && c<=127 && c!='&') out.append(1, (char)c);
Toshihiro Shimizu 890ddd
  else if(c=='&') out.append("&");
Toshihiro Shimizu 890ddd
  else 
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
     ostrstream ss;
Toshihiro Shimizu 890ddd
     ss << "&#" <<  c << ";" << '\0';
Toshihiro Shimizu 890ddd
     out += ss.str();
Toshihiro Shimizu 890ddd
     ss.freeze(0);
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TFilePath::setPath(std::wstring path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool isUncName = false;
Toshihiro Shimizu 890ddd
	// elimino i '//', './' e '/' finali; raddrizzo gli slash 'storti'.
Toshihiro Shimizu 890ddd
	// se il path comincia con  "<alpha>:" aggiungo uno slash</alpha>
Toshihiro Shimizu 890ddd
	int length = path.length();
Toshihiro Shimizu 890ddd
	int pos = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (path.length() >= 2 && iswalpha(path[0]) && path[1] == L':') {
Toshihiro Shimizu 890ddd
		m_path.append(1, (wchar_t)path[0]);
Toshihiro Shimizu 890ddd
		m_path.append(1, L':');
Toshihiro Shimizu 890ddd
		// m_path.append(path,0,2);
Toshihiro Shimizu 890ddd
		pos = 2;
Toshihiro Shimizu 890ddd
		if (path.length() == 2 || !isSlash(path[pos]))
Toshihiro Shimizu 890ddd
			m_path.append(1, wslash);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//se si tratta di un path in formato UNC e' del tipo "\\\\MachineName"
Toshihiro Shimizu 890ddd
	else if (path.length() >= 3 && path[0] == L'\\' && path[1] == L'\\' && iswalnum(path[2]) ||
Toshihiro Shimizu 890ddd
			 path.length() >= 3 && path[0] == L'/' && path[1] == L'/' && iswalnum(path[2])) {
Toshihiro Shimizu 890ddd
		isUncName = true;
Toshihiro Shimizu 890ddd
		m_path.append(2, L'\\');
Toshihiro Shimizu 890ddd
		m_path.append(1, path[2]);
Toshihiro Shimizu 890ddd
		pos = 3;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (; pos < length; pos++)
Toshihiro Shimizu 890ddd
		if (path[pos] == L'.') {
Toshihiro Shimizu 890ddd
			pos++;
Toshihiro Shimizu 890ddd
			if (pos >= length) {
Toshihiro Shimizu 890ddd
				if (pos > 1)
Toshihiro Shimizu 890ddd
					m_path.append(1, L'.');
Toshihiro Shimizu 890ddd
			} else if (!isSlash(path[pos])) {
Toshihiro Shimizu 890ddd
				m_path.append(1, L'.');
Toshihiro Shimizu 890ddd
				m_path.append(1, path[pos]);
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				while (pos + 1 < length && isSlash(path[pos + 1]))
Toshihiro Shimizu 890ddd
					pos++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (isSlash(path[pos])) {
Toshihiro Shimizu 890ddd
			int firstSlashPos = pos;
Toshihiro Shimizu 890ddd
			do
Toshihiro Shimizu 890ddd
				pos++;
Toshihiro Shimizu 890ddd
			while (pos < length && isSlash(path[pos]));
Toshihiro Shimizu 890ddd
			if (firstSlashPos == 0 && pos == 4) //Caso "\\\\MachineName"
Toshihiro Shimizu 890ddd
				m_path.append(2, wslash);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				m_path.append(1, wslash);
Toshihiro Shimizu 890ddd
			pos--;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			m_path.append(1, path[pos]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// rimuovo l'eventuale '/' finale (a meno che m_path == "/" o m_path == "<letter>:\"</letter>
Toshihiro Shimizu 890ddd
	// oppure sia UNC (Windows only) )
Toshihiro Shimizu 890ddd
	if (!(m_path.length() == 1 && m_path[0] == wslash ||
Toshihiro Shimizu 890ddd
		  m_path.length() == 3 && iswalpha(m_path[0]) && m_path[1] == L':' && m_path[2] == wslash) &&
Toshihiro Shimizu 890ddd
		m_path.length() > 1 && m_path[m_path.length() - 1] == wslash)
Toshihiro Shimizu 890ddd
		m_path.erase(m_path.length() - 1, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (isUncName && !(m_path.find_last_of(L'\\') > 1 || m_path.find_last_of(L'/') > 1)) // e' indicato solo il nome della macchina...
Toshihiro Shimizu 890ddd
		m_path.append(1, wslash);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath::TFilePath(const char *path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setPath(toWideString(path));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath::TFilePath(const string &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setPath(toWideString(path));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath::TFilePath(const std::wstring &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setPath(path);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath::TFilePath(const QString &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setPath(path.toStdWString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::operator==(const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
	return _wcsicmp(m_path.c_str(), fp.m_path.c_str()) == 0;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	return m_path == fp.m_path;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::operator<(const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	wstring iName = m_path;
Toshihiro Shimizu 890ddd
	wstring jName = fp.m_path;
Toshihiro Shimizu 890ddd
	int i1 = 0, j1 = 0;
Toshihiro Shimizu 890ddd
	int i2 = m_path.find(L"\\");
Toshihiro Shimizu 890ddd
	int j2 = fp.m_path.find(L"\\");
Toshihiro Shimizu 890ddd
	if (i2 == j2 && j2 == -1)
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
		return _wcsicmp(m_path.c_str(), fp.m_path.c_str()) < 0;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		return m_path < fp.m_path;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	if (!i2) {
Toshihiro Shimizu 890ddd
		++i1;
Toshihiro Shimizu 890ddd
		i2 = m_path.find(L"\\", i1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!j2) {
Toshihiro Shimizu 890ddd
		++j1;
Toshihiro Shimizu 890ddd
		j2 = fp.m_path.find(L"\\", j1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	while (i2 != -1 || j2 != -1) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		iName = (i2 != -1) ? m_path.substr(i1, i2 - i1) : m_path;
Toshihiro Shimizu 890ddd
		jName = (j2 != -1) ? fp.m_path.substr(j1, j2 - j1) : fp.m_path;
Toshihiro Shimizu 890ddd
//se le due parti di path, conpresi tra slash sono uguali
Toshihiro Shimizu 890ddd
//itero il processo di confronto altrimenti ritorno
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
		char differ;
Toshihiro Shimizu 890ddd
		differ = _wcsicmp(iName.c_str(), jName.c_str());
Toshihiro Shimizu 890ddd
		if (differ != 0)
Toshihiro Shimizu 890ddd
			return differ < 0 ? true : false;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		if (TFilePath(iName) != TFilePath(jName))
Toshihiro Shimizu 890ddd
			return TFilePath(iName) < TFilePath(jName);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		i1 = (i2 != -1) ? i2 + 1 : m_path.size();
Toshihiro Shimizu 890ddd
		j1 = (j2 != -1) ? j2 + 1 : fp.m_path.size();
Toshihiro Shimizu 890ddd
		i2 = m_path.find(L"\\", i1);
Toshihiro Shimizu 890ddd
		j2 = fp.m_path.find(L"\\", j1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	iName = m_path.substr(i1, m_path.size() - i1);
Toshihiro Shimizu 890ddd
	jName = fp.m_path.substr(j1, fp.m_path.size() - j1);
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
	return _wcsicmp(iName.c_str(), jName.c_str()) < 0;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	return TFilePath(iName) < TFilePath(jName);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef LEVO
Toshihiro Shimizu 890ddd
bool TFilePath::operator<(const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  wstring a = m_path, b = fp.m_path;
Toshihiro Shimizu 890ddd
  for(;;)
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
     wstring ka,kb;
Toshihiro Shimizu 890ddd
     int i;
Toshihiro Shimizu 890ddd
     i = a.find_first_of("/\\");
Toshihiro Shimizu 890ddd
     if(i==wstring::npos) {ka = a; a = L"";}
Toshihiro Shimizu 890ddd
     i = b.find_first_of("/\\");
Toshihiro Shimizu 890ddd
     if(i==wstring::npos) {ka = a; a = L"";}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	wstring a = toLower(m_path), b = toLower(fp.m_path);
Toshihiro Shimizu 890ddd
	return a < b;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath &TFilePath::operator+=(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(!fp.isAbsolute());
Toshihiro Shimizu 890ddd
	if (fp.isEmpty())
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	else if (isEmpty()) {
Toshihiro Shimizu 890ddd
		*this = fp;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	} else if (m_path.length() != 1 || m_path[0] != slash) {
Toshihiro Shimizu 890ddd
		assert(!m_path.empty());
Toshihiro Shimizu 890ddd
		if (!isSlash(m_path[m_path.length() - 1]))
Toshihiro Shimizu 890ddd
			m_path.append(1, wslash);
Toshihiro Shimizu 890ddd
		m_path += fp.m_path;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		*this = TFilePath(m_path + fp.m_path);
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TFilePath TFilePath::operator+(const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath ret(*this);
Toshihiro Shimizu 890ddd
	ret += fp;
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath &TFilePath::operator+=(const std::wstring &s)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (s.empty())
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	// if(m_path.length()!=1 || m_path[0] != slash)
Toshihiro Shimizu 890ddd
	//  m_path += slash;
Toshihiro Shimizu 890ddd
	if (m_path.length() > 0 && !isSlash(m_path[m_path.length() - 1]))
Toshihiro Shimizu 890ddd
		m_path.append(1, wslash);
Toshihiro Shimizu 890ddd
	m_path += s;
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const wstring TFilePath::getWideString() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_path;
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  std::wstring s;
Toshihiro Shimizu 890ddd
  int n = m_path.size();
Toshihiro Shimizu 890ddd
  for(int i=0;i
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
     char c = m_path[i];
Toshihiro Shimizu 890ddd
     if(c!='&') s.append(1, (unsigned short)c);
Toshihiro Shimizu 890ddd
     else 
Toshihiro Shimizu 890ddd
       {
Toshihiro Shimizu 890ddd
        i++;
Toshihiro Shimizu 890ddd
        if(m_path[i] == '#')
Toshihiro Shimizu 890ddd
          {
Toshihiro Shimizu 890ddd
           unsigned short w = 0;
Toshihiro Shimizu 890ddd
           i++;
Toshihiro Shimizu 890ddd
           while(i
Toshihiro Shimizu 890ddd
             {
Toshihiro Shimizu 890ddd
              c = m_path[i];
Toshihiro Shimizu 890ddd
              if('0'<=c && c<='9')
Toshihiro Shimizu 890ddd
                w = w*10 + c - '0';
Toshihiro Shimizu 890ddd
              else if('a' <=c && c<='f')
Toshihiro Shimizu 890ddd
                w = w*10 + c - 'a' + 10;
Toshihiro Shimizu 890ddd
              else if('A' <=c && c<='F')
Toshihiro Shimizu 890ddd
                w = w*10 + c - 'A' + 10;
Toshihiro Shimizu 890ddd
              else
Toshihiro Shimizu 890ddd
                break;
Toshihiro Shimizu 890ddd
              i++;
Toshihiro Shimizu 890ddd
             }
Toshihiro Shimizu 890ddd
           s.append(1, w);           
Toshihiro Shimizu 890ddd
          }
Toshihiro Shimizu 890ddd
       }
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
  return s;
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
const wstring TFilePath::getWideString() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
   wstring a(L"dummy string");
Toshihiro Shimizu 890ddd
   return a;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TFilePath::getQString() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QString::fromStdWString(getWideString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ostream &operator<<(ostream &out, const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	wstring w = path.getWideString();
Toshihiro Shimizu 890ddd
	return out << toString(w) << " ";
Toshihiro Shimizu 890ddd
	//  string w = path.getString();
Toshihiro Shimizu 890ddd
	//  return out << w << " ";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
TFilePath TFilePath::operator+ (const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
assert(!fp.isAbsolute());
Toshihiro Shimizu 890ddd
if(fp.isEmpty()) return *this;
Toshihiro Shimizu 890ddd
else if(isEmpty()) return fp;
Toshihiro Shimizu 890ddd
else if(m_path.length()!=1 || m_path[0] != slash) 
Toshihiro Shimizu 890ddd
  return TFilePath(m_path + slash + fp.m_path);
Toshihiro Shimizu 890ddd
else 
Toshihiro Shimizu 890ddd
  return TFilePath(m_path + fp.m_path);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::isAbsolute() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_path.length() >= 1 && m_path[0] == slash ||
Toshihiro Shimizu 890ddd
		   m_path.length() >= 2 && iswalpha(m_path[0]) && m_path[1] == ':';
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::isRoot() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_path.length() == 1 && m_path[0] == slash ||
Toshihiro Shimizu 890ddd
		   m_path.length() == 3 && iswalpha(m_path[0]) && m_path[1] == ':' && m_path[2] == slash ||
Toshihiro Shimizu 890ddd
		   m_path.length() > 2 && m_path[0] == slash && m_path[1] == slash && (string::npos == m_path.find(slash, 2) || m_path.find(slash, 2) == m_path.size() - 1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ritorna ""(niente tipo, niente punto), "." (file con tipo) o ".." (file con tipo e frame)
Toshihiro Shimizu 890ddd
string TFilePath::getDots() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path);
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1);
Toshihiro Shimizu 890ddd
	// potrei anche avere a.b.c.d dove d e' l'estensione
Toshihiro Shimizu 890ddd
	i = str.rfind(L".");
Toshihiro Shimizu 890ddd
	if (i == (int)wstring::npos || str == L"..")
Toshihiro Shimizu 890ddd
		return "";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (str.substr(0, i).rfind(L".") != wstring::npos)
Toshihiro Shimizu 890ddd
		return "..";
Toshihiro Shimizu 890ddd
	else if (m_underscoreFormatAllowed) {
Toshihiro Shimizu 890ddd
		int j = str.substr(0, i).rfind(L"_");
Toshihiro Shimizu 890ddd
		/*-- j == i-1は、フレーム番号を抜いて"A_.tga"のような場合の条件 --*/
Toshihiro Shimizu 890ddd
		return (j != (int)wstring::npos &&
Toshihiro Shimizu 890ddd
				(j == i - 1 || isNumbers(str, j, i)))
Toshihiro Shimizu 890ddd
				   ? ".."
Toshihiro Shimizu 890ddd
				   : ".";
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return ".";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
string TFilePath::getDottedType() const // ritorna l'estensione con PUNTO (se c'e')
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path);
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1);
Toshihiro Shimizu 890ddd
	i = str.rfind(L".");
Toshihiro Shimizu 890ddd
	if (i == (int)wstring::npos)
Toshihiro Shimizu 890ddd
		return "";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return toLower(toString(str.substr(i)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
string TFilePath::getUndottedType() const // ritorna l'estensione senza PUNTO
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	size_t i = getLastSlash(m_path);
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1);
Toshihiro Shimizu 890ddd
	i = str.rfind(L".");
Toshihiro Shimizu 890ddd
	if (i == wstring::npos || i == str.length() - 1)
Toshihiro Shimizu 890ddd
		return "";
Toshihiro Shimizu 890ddd
	return toLower(toString(str.substr(i + 1)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
wstring TFilePath::getWideName() const // noDot! noSlash!
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path); //cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1);
Toshihiro Shimizu 890ddd
	i = str.rfind(L".");
Toshihiro Shimizu 890ddd
	if (i == (int)wstring::npos)
Toshihiro Shimizu 890ddd
		return str;
Toshihiro Shimizu 890ddd
	int j = str.substr(0, i).rfind(L".");
Toshihiro Shimizu 890ddd
	if (j != (int)wstring::npos)
Toshihiro Shimizu 890ddd
		i = j;
Toshihiro Shimizu 890ddd
	else if (m_underscoreFormatAllowed) {
Toshihiro Shimizu 890ddd
		j = str.substr(0, i).rfind(L"_");
Toshihiro Shimizu 890ddd
		if (j != (int)wstring::npos && isNumbers(str, j, i))
Toshihiro Shimizu 890ddd
			i = j;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return str.substr(0, i);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
string TFilePath::getName() const // noDot! noSlash!
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toString(getWideName());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// es. TFilePath("/pippo/pluto.0001.gif").getLevelName() == "pluto..gif"
Toshihiro Shimizu 890ddd
string TFilePath::getLevelName() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toString(getLevelNameW());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// es. TFilePath("/pippo/pluto.0001.gif").getLevelName() == "pluto..gif"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
std::wstring TFilePath::getLevelNameW() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path);		//cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1); // str e' m_path senza directory
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int j = str.rfind(L"."); // str[j..] = ".type"
Toshihiro Shimizu 890ddd
	if (j == (int)wstring::npos)
Toshihiro Shimizu 890ddd
		return str; // no frame; no type
Toshihiro Shimizu 890ddd
	i = str.substr(0, j).rfind(L'.');
Toshihiro Shimizu 890ddd
	if (i == (int)wstring::npos && m_underscoreFormatAllowed)
Toshihiro Shimizu 890ddd
		i = str.substr(0, j).rfind(L'_');
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (j == i || j - i == 1) //prova.tif o prova..tif
Toshihiro Shimizu 890ddd
		return str;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!isNumbers(str, i, j))
Toshihiro Shimizu 890ddd
		return str;
Toshihiro Shimizu 890ddd
	//prova.0001.tif
Toshihiro Shimizu 890ddd
	return str.erase(i + 1, j - i - 1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TFilePath::getParentDir() const // noSlash!
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path); //cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	if (i < 0) {
Toshihiro Shimizu 890ddd
		if (m_path.length() >= 2 && ('a' <= m_path[0] && m_path[0] <= 'z' || 'A' <= m_path[0] && m_path[0] <= 'Z') && m_path[1] == ':')
Toshihiro Shimizu 890ddd
			return TFilePath(m_path.substr(0, 2));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			return TFilePath("");
Toshihiro Shimizu 890ddd
	} else if (i == 0)
Toshihiro Shimizu 890ddd
		return TFilePath("/");
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return TFilePath(m_path.substr(0, i));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::isLevelName() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		return getFrame() == TFrameId(TFrameId::EMPTY_FRAME);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFrameId TFilePath::getFrame() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path);		//cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1); // str e' il path senza parentdir
Toshihiro Shimizu 890ddd
	i = str.rfind(L'.');
Toshihiro Shimizu 890ddd
	if (i == (int)wstring::npos || str == L"." || str == L"..")
Toshihiro Shimizu 890ddd
		return TFrameId(TFrameId::NO_FRAME);
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	j = str.substr(0, i).rfind(L'.');
Toshihiro Shimizu 890ddd
	if (j == (int)wstring::npos && m_underscoreFormatAllowed)
Toshihiro Shimizu 890ddd
		j = str.substr(0, i).rfind(L'_');
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (j == (int)wstring::npos)
Toshihiro Shimizu 890ddd
		return TFrameId(TFrameId::NO_FRAME);
Toshihiro Shimizu 890ddd
	if (i == j + 1)
Toshihiro Shimizu 890ddd
		return TFrameId(TFrameId::EMPTY_FRAME);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*-- 間が数字でない場合(ファイル名にまぎれた"_" や "."がある場合)を除外する --*/
Toshihiro Shimizu 890ddd
	if (!isNumbers(str, j, i))
Toshihiro Shimizu 890ddd
		return TFrameId(TFrameId::NO_FRAME);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int k, number = 0;
Toshihiro Shimizu 890ddd
	for (k = j + 1; k < i && iswdigit(str[k]); k++)
Toshihiro Shimizu 890ddd
		number = number * 10 + str[k] - L'0';
Toshihiro Shimizu 890ddd
	char letter = '\0';
Toshihiro Shimizu 890ddd
	if (iswalpha(str[k]))
Toshihiro Shimizu 890ddd
		letter = str[k++] + ('a' - L'a');
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (number == 0 || k < i) // || letter!='\0')
Toshihiro Shimizu 890ddd
		throw(toString(m_path) + ": malformed frame name.");
Toshihiro Shimizu 890ddd
	return TFrameId(number, letter);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TFilePath::withType(const string &type) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(type.length() < 2 || type.substr(0, 2) != "..");
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path);		//cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1); // str e' il path senza parentdir
Toshihiro Shimizu 890ddd
	int j = str.rfind(L'.');
Toshihiro Shimizu 890ddd
	if (j == (int)wstring::npos || str == L"..")
Toshihiro Shimizu 890ddd
	// il path originale non ha tipo
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (type == "")
Toshihiro Shimizu 890ddd
			return *this;
Toshihiro Shimizu 890ddd
		else if (type[0] == '.')
Toshihiro Shimizu 890ddd
			return TFilePath(m_path + toWideString(type));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			return TFilePath(m_path + toWideString("." + type));
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
	// il path originale ha gia' il tipo
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (type == "")
Toshihiro Shimizu 890ddd
			return TFilePath(m_path.substr(0, i + j + 1));
Toshihiro Shimizu 890ddd
		else if (type[0] == '.')
Toshihiro Shimizu 890ddd
			return TFilePath(m_path.substr(0, i + j + 1) + toWideString(type));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			return TFilePath(m_path.substr(0, i + j + 2) + toWideString(type));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TFilePath::withName(const string &name) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return withName(toWideString(name));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TFilePath::withName(const std::wstring &name) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path);		//cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1); // str e' il path senza parentdir
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	j = str.rfind(L'.');
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (j == (int)wstring::npos) {
Toshihiro Shimizu 890ddd
		if (m_underscoreFormatAllowed) {
Toshihiro Shimizu 890ddd
			j = str.rfind(L'_');
Toshihiro Shimizu 890ddd
			if (j != (int)wstring::npos)
Toshihiro Shimizu 890ddd
				return TFilePath(m_path.substr(0, i + 1) + name + str.substr(j));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return TFilePath(m_path.substr(0, i + 1) + name);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int k;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	k = str.substr(0, j).rfind(L".");
Toshihiro Shimizu 890ddd
	if (k == (int)wstring::npos && m_underscoreFormatAllowed)
Toshihiro Shimizu 890ddd
		k = str.substr(0, j).rfind(L"_");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (k == (int)(wstring::npos))
Toshihiro Shimizu 890ddd
		k = j;
Toshihiro Shimizu 890ddd
	else if (k != j - 1 && !isNumbers(str, k, j))
Toshihiro Shimizu 890ddd
		k = j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TFilePath(m_path.substr(0, i + 1) + name + str.substr(k));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TFilePath::withParentDir(const TFilePath &dir) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path); //cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	return dir + TFilePath(m_path.substr(i + 1));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TFilePath::withFrame(const TFrameId &frame, TFrameId::FrameFormat format) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const wstring dot = L".", dotDot = L"..";
Toshihiro Shimizu 890ddd
	int i = getLastSlash(m_path);		//cerco l'ultimo slash
Toshihiro Shimizu 890ddd
	wstring str = m_path.substr(i + 1); // str e' il path senza parentdir
Toshihiro Shimizu 890ddd
	assert(str != dot && str != dotDot);
Toshihiro Shimizu 890ddd
	int j = str.rfind(L'.');
Toshihiro Shimizu 890ddd
	char *ch = ".";
Toshihiro Shimizu 890ddd
	if (m_underscoreFormatAllowed &&
Toshihiro Shimizu 890ddd
		(format == TFrameId::UNDERSCORE_FOUR_ZEROS || format == TFrameId::UNDERSCORE_NO_PAD))
Toshihiro Shimizu 890ddd
		ch = "_";
Toshihiro Shimizu 890ddd
	if (j == (int)wstring::npos) {
Toshihiro Shimizu 890ddd
		if (frame.isEmptyFrame() || frame.isNoFrame())
Toshihiro Shimizu 890ddd
			return *this;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			return TFilePath(m_path + toWideString(ch + frame.expand(format)));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string frameString;
Toshihiro Shimizu 890ddd
	if (frame.isNoFrame())
Toshihiro Shimizu 890ddd
		frameString = "";
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		frameString = ch + frame.expand(format);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int k = str.substr(0, j).rfind(L'.');
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (k != (int)wstring::npos)
Toshihiro Shimizu 890ddd
		return TFilePath(m_path.substr(0, k + i + 1) + toWideString(frameString) + str.substr(j));
Toshihiro Shimizu 890ddd
	else if (m_underscoreFormatAllowed) {
Toshihiro Shimizu 890ddd
		k = str.substr(0, j).rfind(L'_');
Toshihiro Shimizu 890ddd
		if (k != (int)wstring::npos &&
Toshihiro Shimizu 890ddd
			(k == j - 1 || isNumbers(str, k, j))) /*-- "_." の並びか、"_[数字]."の並びのとき --*/
Toshihiro Shimizu 890ddd
			return TFilePath(m_path.substr(0, k + i + 1) + ((frame.isNoFrame()) ? L"" : toWideString("_" + frame.expand(format))) + str.substr(j));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TFilePath(m_path.substr(0, j + i + 1) + toWideString(frameString) + str.substr(j));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::isAncestorOf(const TFilePath &possibleDescendent) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	size_t len = m_path.length();
Toshihiro Shimizu 890ddd
	if (len == 0) {
Toshihiro Shimizu 890ddd
		// il punto e' antenato di tutti i path non assoluti
Toshihiro Shimizu 890ddd
		return !possibleDescendent.isAbsolute();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// un path e' antenato di se stesso
Toshihiro Shimizu 890ddd
	if (m_path == possibleDescendent.m_path)
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int possibleDescendentLen = possibleDescendent.m_path.length();
Toshihiro Shimizu 890ddd
	// altrimenti l'antenato deve essere piu' corto
Toshihiro Shimizu 890ddd
	if ((int)len >= possibleDescendentLen)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	// e deve coincidere con la prima parte del discendente
Toshihiro Shimizu 890ddd
	if (toLower(m_path) != toLower(possibleDescendent.m_path.substr(0, len)))
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	// se l'antenato non finisce con slash ci deve essere uno slash nel discendente, subito dopo
Toshihiro Shimizu 890ddd
	if (m_path[len - 1] != wslash && possibleDescendent.m_path[len] != wslash)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TFilePath::operator-(const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (toLower(m_path) == toLower(fp.m_path))
Toshihiro Shimizu 890ddd
		return TFilePath("");
Toshihiro Shimizu 890ddd
	if (!fp.isAncestorOf(*this))
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	int len = fp.m_path.length();
Toshihiro Shimizu 890ddd
	if (len == 0 || fp.m_path[len - 1] != wslash)
Toshihiro Shimizu 890ddd
		len++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TFilePath(m_path.substr(len));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TFilePath::match(const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getParentDir() == fp.getParentDir() &&
Toshihiro Shimizu 890ddd
		   getName() == fp.getName() &&
Toshihiro Shimizu 890ddd
		   getFrame() == fp.getFrame() &&
Toshihiro Shimizu 890ddd
		   getType() == fp.getType();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TFilePath::split(wstring &head, TFilePath &tail) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath ancestor = getParentDir();
Toshihiro Shimizu 890ddd
	if (ancestor == TFilePath()) {
Toshihiro Shimizu 890ddd
		head = getWideString();
Toshihiro Shimizu 890ddd
		tail = TFilePath();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (;;) {
Toshihiro Shimizu 890ddd
		if (ancestor.isRoot())
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		TFilePath p = ancestor.getParentDir();
Toshihiro Shimizu 890ddd
		if (p == TFilePath())
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		ancestor = p;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	head = ancestor.getWideString();
Toshihiro Shimizu 890ddd
	tail = *this - ancestor;
Toshihiro Shimizu 890ddd
}