TFilePath TFilePath::withType(const string &type) const { const string dotDot = ".."; assert(type.length() < 2 || type.substr(0, 2) != dotDot); int i = getLastSlash(m_path); // cerco l'ultimo slash string str = m_path.substr(i + 1); // str e' il path senza parentdir int j = str.rfind('.'); if (j == string::npos || str == dotDot) // il path originale non ha tipo { if (type == "") return *this; else if (type[0] == '.') return TFilePath(m_path + type); else return TFilePath(m_path + "." + type); } else // il path originale ha gia' il tipo { if (type == "") return TFilePath(m_path.substr(0, i + j + 1)); else if (type[0] == '.') return TFilePath(m_path.substr(0, i + j + 1) + type); else return TFilePath(m_path.substr(0, i + j + 2) + type); } }
TFilePath TFilePath::withFrame(const TFrameId &frame, TFrameId::FrameFormat format) const { const string dot = ".", dotDot = ".."; int i = getLastSlash(m_path); // cerco l'ultimo slash string str = m_path.substr(i + 1); // str e' il path senza parentdir assert(str != dot && str != dotDot); int j = str.rfind('.'); if (j == string::npos) { if (frame.isEmptyFrame() || frame.isNoFrame()) return *this; else return TFilePath(m_path + "." + frame.expand(format)); } string frameString; if (frame.isNoFrame()) frameString = ""; else frameString = "." + frame.expand(format); int k = str.substr(0, j).rfind('.'); if (k == string::npos) return TFilePath(m_path.substr(0, j + i + 1) + frameString + str.substr(j)); else return TFilePath(m_path.substr(0, k + i + 1) + frameString + str.substr(j)); }
// ritorna ""(niente tipo, niente punto), "." (file con tipo) o ".." (file con // tipo e frame) string TFilePath::getDots() const { int i = getLastSlash(m_path); string str = m_path.substr(i + 1); // potrei anche avere a.b.c.d dove d e' l'estensione i = str.rfind("."); if (i == string::npos || str == "..") return ""; return str.substr(0, i).rfind(".") == string::npos ? "." : ".."; }
TFilePath TFilePath::withName(const string &name) const { int i = getLastSlash(m_path); // cerco l'ultimo slash string str = m_path.substr(i + 1); // str e' il path senza parentdir int j = str.rfind('.'); if (j == string::npos) return TFilePath(m_path.substr(0, i + 1) + name); int k = str.substr(0, j).rfind("."); if (k == string::npos) k = j; return TFilePath(m_path.substr(0, i + 1) + name + str.substr(k)); }
string TFilePath::getName() const // noDot! noSlash! { int i = getLastSlash(m_path); // cerco l'ultimo slash string str = m_path.substr(i + 1); i = str.rfind("."); if (i == string::npos) return str; int j = str.substr(0, i).rfind("."); if (j != string::npos) i = j; return str.substr(0, i); }
//----------------------------------------------------------------------------- // es. TFilePath("/pippo/pluto.0001.gif").getLevelName() == "pluto..gif" string TFilePath::getLevelName() const { int i = getLastSlash(m_path); // cerco l'ultimo slash string str = m_path.substr(i + 1); // str e' m_path senza directory i = str.find("."); // posizione del primo punto di str if (i == string::npos) return str; // no frame; no type int j = str.rfind("."); // str[j..] = ".type" if (j == i || j - i == 1) // prova.tif o prova..tif return str; else // prova.0001.tif return str.erase(i + 1, j - i - 1); }
string TFilePath::getUndottedType() const // ritorna l'estensione senza PUNTO { size_t i = getLastSlash(m_path); string str = m_path.substr(i + 1); i = str.rfind("."); if (i == string::npos || i == str.length() - 1) return ""; #ifdef WIN32 return toLower(str.substr(i + 1)); #else return str.substr(i + 1); #endif }
string TFilePath::getDottedType() const // ritorna l'estensione con PUNTO (se c'e') { int i = getLastSlash(m_path); string str = m_path.substr(i + 1); i = str.rfind("."); if (i == string::npos) return ""; #ifdef WIN32 return toLower(str.substr(i)); #else return str.substr(i); #endif }
TFilePath TFilePath::getParentDir() const // noSlash! { int i = getLastSlash(m_path); //cerco l'ultimo slash if (i < 0) { if (m_path.length() >= 2 && ('a' <= m_path[0] && m_path[0] <= 'z' || 'A' <= m_path[0] && m_path[0] <= 'Z') && m_path[1] == ':') return TFilePath(m_path.substr(0, 2)); else return TFilePath(""); } else if (i == 0) return TFilePath("/"); else return TFilePath(m_path.substr(0, i)); }
TFrameId TFilePath::getFrame() const { int i = getLastSlash(m_path); // cerco l'ultimo slash string str = m_path.substr(i + 1); // str e' il path senza parentdir i = str.rfind('.'); if (i == string::npos || str == "." || str == "..") return TFrameId(TFrameId::NO_FRAME); int j = str.substr(0, i).rfind('.'); if (j == string::npos) return TFrameId(TFrameId::NO_FRAME); if (i == j + 1) return TFrameId(TFrameId::EMPTY_FRAME); int k, number = 0; for (k = j + 1; k < i && isdigit(str[k]); k++) number = number * 10 + str[k] - '0'; char letter = '\0'; if (isalpha(str[k])) letter = str[k++]; if (number == 0 || k < i) throw TMalformedFrameException(); return TFrameId(number, letter); }
TFilePath TFilePath::withParentDir(const TFilePath &dir) const { int i = getLastSlash(m_path); // cerco l'ultimo slash return dir + TFilePath(m_path.substr(i + 1)); }