Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <ctype.h></ctype.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
const char slash    = '\\';
Toshihiro Shimizu 890ddd
const char auxslash = '/';
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
const char slash    = '/';
Toshihiro Shimizu 890ddd
const char auxslash = '\\';
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// TFrameId::operator string() const
2fc36c
std::string TFrameId::expand(FrameFormat format) const {
Shinya Kitaoka 120a6e
  if (m_frame == EMPTY_FRAME)
Shinya Kitaoka 120a6e
    return "";
Shinya Kitaoka 120a6e
  else if (m_frame == NO_FRAME)
Shinya Kitaoka 120a6e
    return "-";
2fc36c
  std::stringstream o_buff;
Shinya Kitaoka 120a6e
  if (format == FOUR_ZEROS) {
Shinya Kitaoka 120a6e
    o_buff.fill('0');
Shinya Kitaoka 120a6e
    o_buff.width(4);
Shinya Kitaoka 120a6e
    o_buff << m_frame;
Shinya Kitaoka 120a6e
    o_buff.width(0);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    o_buff << m_frame;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_letter != '\0') o_buff << m_letter;
2fc36c
  return o_buff.str();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TFrameId &TFrameId::operator++() {
Shinya Kitaoka 120a6e
  ++m_frame;
Shinya Kitaoka 120a6e
  m_letter = 0;
Shinya Kitaoka 120a6e
  return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TFrameId &TFrameId::operator--() {
Shinya Kitaoka 120a6e
  if (m_letter > 0)
Shinya Kitaoka 120a6e
    m_letter = 0;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    --m_frame;
Shinya Kitaoka 120a6e
  return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline bool isSlash(char c) { return c == slash || c == auxslash; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// cerca l'ultimo '/' o '\'. Se non c'e' ritorna -1
Shinya Kitaoka 120a6e
// di modo che la sottostringa che parte da getLastSlash() + 1 e'
Shinya Kitaoka 120a6e
// nome.frame.tipo
Shinya Kitaoka 120a6e
inline int getLastSlash(const string &path) {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = path.length() - 1; i >= 0 && !isSlash(path[i]); i--) {
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return i;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TFilePath::setPath(string path) {
Shinya Kitaoka 120a6e
  bool isUncName = false;
Shinya Kitaoka 120a6e
  // elimino i '//', './' e '/' finali; raddrizzo gli slash 'storti'.
Shinya Kitaoka 120a6e
  // se il path comincia con  "<alpha>:" aggiungo uno slash</alpha>
Shinya Kitaoka 120a6e
  int length = path.length();
Shinya Kitaoka 120a6e
  int pos    = 0;
Shinya Kitaoka 120a6e
  if (path.length() >= 2 && isalpha(path[0]) && path[1] == ':') {
Shinya Kitaoka 120a6e
    m_path.append(path, 0, 2);
Shinya Kitaoka 120a6e
    pos = 2;
Shinya Kitaoka 120a6e
    if (path.length() == 2 || !isSlash(path[pos])) m_path.append(1, slash);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  else  // se si tratta di un path in formato UNC e' del tipo "\\\\MachineName"
Shinya Kitaoka 120a6e
      if (path.length() >= 3 && path[0] == '\\' && path[1] == '\\' &&
Shinya Kitaoka 120a6e
          isalpha(path[2])) {
Shinya Kitaoka 120a6e
    isUncName = true;
Shinya Kitaoka 120a6e
    m_path.append(path, 0, 3);
Shinya Kitaoka 120a6e
    pos = 3;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  for (; pos < length; pos++)
Shinya Kitaoka 120a6e
    if (path[pos] == '.') {
Shinya Kitaoka 120a6e
      pos++;
Shinya Kitaoka 120a6e
      if (pos >= length) {
Shinya Kitaoka 120a6e
        if (pos > 1) m_path.append(1, '.');
Shinya Kitaoka 120a6e
      } else if (!isSlash(path[pos]))
Shinya Kitaoka 120a6e
        m_path.append(path, pos - 1, 2);
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        while (pos + 1 < length && isSlash(path[pos + 1])) pos++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else if (isSlash(path[pos])) {
Shinya Kitaoka 120a6e
      do
Shinya Kitaoka 120a6e
        pos++;
Shinya Kitaoka 120a6e
      while (pos < length && isSlash(path[pos]));
Shinya Kitaoka 120a6e
      pos--;
Shinya Kitaoka 120a6e
      m_path.append(1, slash);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      m_path.append(1, path[pos]);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // rimuovo l'eventuale '/' finale (a meno che m_path == "/" o m_path ==
Shinya Kitaoka 120a6e
  // "<letter>:\"</letter>
Shinya Kitaoka 120a6e
  // oppure sia UNC (Windows only) )
Shinya Kitaoka 120a6e
  if (!(m_path.length() == 1 && m_path[0] == slash ||
Shinya Kitaoka 120a6e
        m_path.length() == 3 && isalpha(m_path[0]) && m_path[1] == ':' &&
Shinya Kitaoka 120a6e
            m_path[2] == slash) &&
Shinya Kitaoka 120a6e
      m_path.length() > 1 && m_path[m_path.length() - 1] == slash)
Shinya Kitaoka 120a6e
    m_path.erase(m_path.length() - 1, 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (isUncName &&
Shinya Kitaoka 120a6e
      m_path.find_last_of('\\') ==
Shinya Kitaoka 120a6e
          1)  // e' indicato solo il nome della macchina...
Shinya Kitaoka 120a6e
    m_path.append(1, slash);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath::TFilePath(string path) : m_path("") { setPath(path); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath::TFilePath(const char *path) : m_path("") { setPath(string(path)); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TFilePath::operator==(const TFilePath &fp) const {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  return stricmp(m_path.c_str(), fp.m_path.c_str()) == 0;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  return m_path == fp.m_path;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TFilePath::operator<(const TFilePath &fp) const {
Shinya Kitaoka 120a6e
  string iName = m_path;
Shinya Kitaoka 120a6e
  string jName = fp.m_path;
Shinya Kitaoka 120a6e
  int i1 = 0, j1 = 0;
Shinya Kitaoka 120a6e
  int i2 = m_path.find("\\");
Shinya Kitaoka 120a6e
  int j2 = fp.m_path.find("\\");
Shinya Kitaoka 120a6e
  if (i2 == j2 && j2 == -1)
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
    return stricmp(m_path.c_str(), fp.m_path.c_str()) < 0;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    return m_path < fp.m_path;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  if (!i2) {
Shinya Kitaoka 120a6e
    ++i1;
Shinya Kitaoka 120a6e
    i2 = m_path.find("\\", i1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!j2) {
Shinya Kitaoka 120a6e
    ++j1;
Shinya Kitaoka 120a6e
    j2 = fp.m_path.find("\\", j1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  while (i2 != -1 || j2 != -1) {
Shinya Kitaoka 120a6e
    iName = m_path.substr(i1, i2 - i1);
Shinya Kitaoka 120a6e
    jName = fp.m_path.substr(j1, j2 - j1);
Shinya Kitaoka 120a6e
// se le due parti di path, conpresi tra slash sono uguali
Shinya Kitaoka 120a6e
// itero il processo di confronto altrimenti ritorno
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
    char differ;
Shinya Kitaoka 120a6e
    differ = stricmp(iName.c_str(), jName.c_str());
Shinya Kitaoka 120a6e
    if (differ != 0) return differ < 0 ? true : false;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    if (TFilePath(iName) != TFilePath(jName))
Shinya Kitaoka 120a6e
      return TFilePath(iName) < TFilePath(jName);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
    i1 = i2 + 1;
Shinya Kitaoka 120a6e
    j1 = j2 + 1;
Shinya Kitaoka 120a6e
    i2 = m_path.find("\\", i1);
Shinya Kitaoka 120a6e
    j2 = fp.m_path.find("\\", j1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  iName = m_path.substr(i1, m_path.size() - i1);
Shinya Kitaoka 120a6e
  jName = fp.m_path.substr(j1, fp.m_path.size() - j1);
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  return stricmp(iName.c_str(), jName.c_str()) < 0;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  return TFilePath(iName) < TFilePath(jName);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath &TFilePath::operator+=(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  assert(!fp.isAbsolute());
Shinya Kitaoka 120a6e
  if (fp.isEmpty())
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  else if (isEmpty()) {
Shinya Kitaoka 120a6e
    *this = fp;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  } else if (m_path.length() != 1 || m_path[0] != slash) {
Shinya Kitaoka 120a6e
    *this = TFilePath(m_path + slash + fp.m_path);
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    *this = TFilePath(m_path + fp.m_path);
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
TFilePath TFilePath::operator+(const TFilePath &fp) const {
Shinya Kitaoka 120a6e
  TFilePath ret(*this);
Shinya Kitaoka 120a6e
  ret += fp;
Shinya Kitaoka 120a6e
  return ret;
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;
Shinya Kitaoka 120a6e
else if(m_path.length()!=1 || m_path[0] != slash)
Toshihiro Shimizu 890ddd
  return TFilePath(m_path + slash + fp.m_path);
Shinya Kitaoka 120a6e
else
Toshihiro Shimizu 890ddd
  return TFilePath(m_path + fp.m_path);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TFilePath::isAbsolute() const {
Shinya Kitaoka 120a6e
  return m_path.length() >= 1 && m_path[0] == slash ||
Shinya Kitaoka 120a6e
         m_path.length() >= 2 && isalpha(m_path[0]) && m_path[1] == ':';
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TFilePath::isRoot() const {
Shinya Kitaoka 120a6e
  return m_path.length() == 1 && m_path[0] == slash ||
Shinya Kitaoka 120a6e
         m_path.length() == 3 && isalpha(m_path[0]) && m_path[1] == ':' &&
Shinya Kitaoka 120a6e
             m_path[2] == slash;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// ritorna ""(niente tipo, niente punto), "." (file con tipo) o ".." (file con
Shinya Kitaoka 120a6e
// tipo e frame)
Shinya Kitaoka 120a6e
string TFilePath::getDots() const {
Shinya Kitaoka 120a6e
  int i      = getLastSlash(m_path);
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);
Shinya Kitaoka 120a6e
  // potrei anche avere a.b.c.d dove d e' l'estensione
Shinya Kitaoka 120a6e
  i = str.rfind(".");
Shinya Kitaoka 120a6e
  if (i == string::npos || str == "..") return "";
Shinya Kitaoka 120a6e
  return str.substr(0, i).rfind(".") == string::npos ? "." : "..";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TFilePath::getDottedType()
Shinya Kitaoka 120a6e
    const  // ritorna l'estensione con PUNTO (se c'e')
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  int i      = getLastSlash(m_path);
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);
Shinya Kitaoka 120a6e
  i          = str.rfind(".");
Shinya Kitaoka 120a6e
  if (i == string::npos) return "";
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  return toLower(str.substr(i));
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  return str.substr(i);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TFilePath::getUndottedType() const  // ritorna l'estensione senza PUNTO
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  size_t i   = getLastSlash(m_path);
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);
Shinya Kitaoka 120a6e
  i          = str.rfind(".");
Shinya Kitaoka 120a6e
  if (i == string::npos || i == str.length() - 1) return "";
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  return toLower(str.substr(i + 1));
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  return str.substr(i + 1);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
string TFilePath::getName() const  // noDot! noSlash!
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 38fd86
  int i      = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);
Shinya Kitaoka 120a6e
  i          = str.rfind(".");
Shinya Kitaoka 120a6e
  if (i == string::npos) return str;
Shinya Kitaoka 120a6e
  int j                    = str.substr(0, i).rfind(".");
Shinya Kitaoka 120a6e
  if (j != string::npos) i = j;
Shinya Kitaoka 120a6e
  return str.substr(0, i);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// es. TFilePath("/pippo/pluto.0001.gif").getLevelName() == "pluto..gif"
Shinya Kitaoka 120a6e
string TFilePath::getLevelName() const {
Shinya Kitaoka 38fd86
  int i      = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);  // str e' m_path senza directory
Shinya Kitaoka 120a6e
  i          = str.find(".");         // posizione del primo punto di str
Shinya Kitaoka 120a6e
  if (i == string::npos) return str;  // no frame; no type
Shinya Kitaoka 120a6e
  int j = str.rfind(".");             // str[j..] = ".type"
Shinya Kitaoka 120a6e
  if (j == i || j - i == 1)           // prova.tif o prova..tif
Shinya Kitaoka 120a6e
    return str;
Shinya Kitaoka 120a6e
  else  // prova.0001.tif
Shinya Kitaoka 120a6e
    return str.erase(i + 1, j - i - 1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TFilePath::getParentDir() const  // noSlash!
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  int i = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  if (i < 0) {
Shinya Kitaoka 120a6e
    if (m_path.length() >= 2 && ('a' <= m_path[0] && m_path[0] <= 'z' ||
Shinya Kitaoka 120a6e
                                 'A' <= m_path[0] && m_path[0] <= 'Z') &&
Shinya Kitaoka 120a6e
        m_path[1] == ':')
Shinya Kitaoka 120a6e
      return TFilePath(m_path.substr(0, 2));
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return TFilePath("");
Shinya Kitaoka 120a6e
  } else if (i == 0)
Shinya Kitaoka 120a6e
    return TFilePath("/");
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return TFilePath(m_path.substr(0, i));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFrameId TFilePath::getFrame() const {
Shinya Kitaoka 38fd86
  int i      = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);  // str e' il path senza parentdir
Shinya Kitaoka 120a6e
  i          = str.rfind('.');
Shinya Kitaoka 120a6e
  if (i == string::npos || str == "." || str == "..")
Shinya Kitaoka 120a6e
    return TFrameId(TFrameId::NO_FRAME);
Shinya Kitaoka 120a6e
  int j = str.substr(0, i).rfind('.');
Shinya Kitaoka 120a6e
  if (j == string::npos) return TFrameId(TFrameId::NO_FRAME);
Shinya Kitaoka 120a6e
  if (i == j + 1) return TFrameId(TFrameId::EMPTY_FRAME);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int k, number = 0;
Shinya Kitaoka 120a6e
  for (k = j + 1; k < i && isdigit(str[k]); k++)
Shinya Kitaoka 120a6e
    number                    = number * 10 + str[k] - '0';
Shinya Kitaoka 120a6e
  char letter                 = '\0';
Shinya Kitaoka 120a6e
  if (isalpha(str[k])) letter = str[k++];
Shinya Kitaoka 120a6e
  if (number == 0 || k < i) throw TMalformedFrameException();
Shinya Kitaoka 120a6e
  return TFrameId(number, letter);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TFilePath::withType(const string &type) const {
Shinya Kitaoka 120a6e
  const string dotDot = "..";
Shinya Kitaoka 120a6e
  assert(type.length() < 2 || type.substr(0, 2) != dotDot);
Shinya Kitaoka 38fd86
  int i      = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);  // str e' il path senza parentdir
Shinya Kitaoka 120a6e
  int j      = str.rfind('.');
Shinya Kitaoka 120a6e
  if (j == string::npos || str == dotDot)
Shinya Kitaoka 120a6e
  // il path originale non ha tipo
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    if (type == "")
Shinya Kitaoka 120a6e
      return *this;
Shinya Kitaoka 120a6e
    else if (type[0] == '.')
Shinya Kitaoka 120a6e
      return TFilePath(m_path + type);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return TFilePath(m_path + "." + type);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
  // il path originale ha gia' il tipo
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    if (type == "")
Shinya Kitaoka 120a6e
      return TFilePath(m_path.substr(0, i + j + 1));
Shinya Kitaoka 120a6e
    else if (type[0] == '.')
Shinya Kitaoka 120a6e
      return TFilePath(m_path.substr(0, i + j + 1) + type);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return TFilePath(m_path.substr(0, i + j + 2) + type);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TFilePath::withName(const string &name) const {
Shinya Kitaoka 38fd86
  int i      = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);  // str e' il path senza parentdir
Shinya Kitaoka 120a6e
  int j      = str.rfind('.');
Shinya Kitaoka 120a6e
  if (j == string::npos) return TFilePath(m_path.substr(0, i + 1) + name);
Shinya Kitaoka 120a6e
  int k                    = str.substr(0, j).rfind(".");
Shinya Kitaoka 120a6e
  if (k == string::npos) k = j;
Shinya Kitaoka 120a6e
  return TFilePath(m_path.substr(0, i + 1) + name + str.substr(k));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TFilePath::withParentDir(const TFilePath &dir) const {
Shinya Kitaoka 120a6e
  int i = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  return dir + TFilePath(m_path.substr(i + 1));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TFilePath::withFrame(const TFrameId &frame,
Shinya Kitaoka 120a6e
                               TFrameId::FrameFormat format) const {
Shinya Kitaoka 120a6e
  const string dot = ".", dotDot = "..";
Shinya Kitaoka 38fd86
  int i      = getLastSlash(m_path);  // cerco l'ultimo slash
Shinya Kitaoka 120a6e
  string str = m_path.substr(i + 1);  // str e' il path senza parentdir
Shinya Kitaoka 120a6e
  assert(str != dot && str != dotDot);
Shinya Kitaoka 120a6e
  int j = str.rfind('.');
Shinya Kitaoka 120a6e
  if (j == string::npos) {
Shinya Kitaoka 120a6e
    if (frame.isEmptyFrame() || frame.isNoFrame())
Shinya Kitaoka 120a6e
      return *this;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return TFilePath(m_path + "." + frame.expand(format));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  string frameString;
Shinya Kitaoka 120a6e
  if (frame.isNoFrame())
Shinya Kitaoka 120a6e
    frameString = "";
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    frameString = "." + frame.expand(format);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int k = str.substr(0, j).rfind('.');
Shinya Kitaoka 120a6e
  if (k == string::npos)
Shinya Kitaoka 120a6e
    return TFilePath(m_path.substr(0, j + i + 1) + frameString + str.substr(j));
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return TFilePath(m_path.substr(0, k + i + 1) + frameString + str.substr(j));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TFilePath::isAncestorOf(const TFilePath &fp) const {
Shinya Kitaoka 120a6e
  size_t len = m_path.length();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (len == 0) {
Shinya Kitaoka 120a6e
    // il punto e' antenato di tutti i path non assoluti
Shinya Kitaoka 120a6e
    return !fp.isAbsolute();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return len < fp.m_path.length()  // l'antenato deve essere piu' corto
Shinya Kitaoka 120a6e
         &&
Shinya Kitaoka 120a6e
         (m_path[len - 1] == slash  // deve finire con slash se e' "/" o "C:\"
Shinya Kitaoka 120a6e
          ||
Shinya Kitaoka 120a6e
          fp.m_path[len] ==
Shinya Kitaoka 120a6e
              slash)  // negli altri casi ci deve essere uno slash subito dopo
Shinya Kitaoka 120a6e
         &&
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
         toLower(m_path) == toLower(fp.m_path.substr(0, len));
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
         m_path == fp.m_path.substr(0, len);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TFilePath::operator-(const TFilePath &fp) const {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  if (toLower(m_path) == toLower(fp.m_path)) return TFilePath("");
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  if (m_path == fp.m_path) return TFilePath("");
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  if (!fp.isAncestorOf(*this)) return *this;
Shinya Kitaoka 120a6e
  int len = fp.m_path.length();
Shinya Kitaoka 120a6e
  if (len == 0 || fp.m_path[len - 1] != slash) len++;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return TFilePath(m_path.substr(len));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TFilePath::match(const TFilePath &fp) const {
Shinya Kitaoka 120a6e
  return getParentDir() == fp.getParentDir() && getName() == fp.getName() &&
Shinya Kitaoka 120a6e
         getFrame() == fp.getFrame() && getType() == fp.getType();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath TFilePath::decode(const std::map<string, string=""> &dictionary) const {</string,>
Shinya Kitaoka 120a6e
  TFilePath parent   = getParentDir();
Shinya Kitaoka 120a6e
  TFilePath filename = withParentDir("");
Shinya Kitaoka 120a6e
  std::map<string, string="">::const_iterator it =</string,>
Shinya Kitaoka 120a6e
      dictionary.find(filename.getFullPath());
Shinya Kitaoka 120a6e
  if (it != dictionary.end()) filename = TFilePath(it->second);
Shinya Kitaoka 120a6e
  if (parent.isEmpty())
Shinya Kitaoka 120a6e
    return filename;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return parent.decode(dictionary) + filename;
Toshihiro Shimizu 890ddd
}