Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef T_FILEPATH_INCLUDED
Toshihiro Shimizu 890ddd
#define T_FILEPATH_INCLUDED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TSYSTEM_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class QString;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/* 
Toshihiro Shimizu 890ddd
  This is an example of how to use TFilePath and TFrameId classes.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//!\include frameid_ex.cpp
Toshihiro Shimizu 890ddd
//! The class TFrameId describes a frame identified by an integer number of four figures and, in case, by a character (necessary for added frames)
Toshihiro Shimizu 890ddd
class DVAPI TFrameId
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int m_frame;
Toshihiro Shimizu 890ddd
	char m_letter; // serve per i frame "aggiunti" del tipo pippo.0001a.tzp => f=1 c='a'
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	enum { EMPTY_FRAME = -1, // es. pippo..tif
Toshihiro Shimizu 890ddd
		   NO_FRAME = -2	 // es. pippo.tif
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum FrameFormat {
Toshihiro Shimizu 890ddd
		FOUR_ZEROS,
Toshihiro Shimizu 890ddd
		NO_PAD,
Toshihiro Shimizu 890ddd
		UNDERSCORE_FOUR_ZEROS, //pippo_0001.tif
Toshihiro Shimizu 890ddd
		UNDERSCORE_NO_PAD
Toshihiro Shimizu 890ddd
	}; //pippo_1.tif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFrameId(int f = EMPTY_FRAME) : m_frame(f), m_letter(0){};
Toshihiro Shimizu 890ddd
	TFrameId(int f, char c) : m_frame(f), m_letter(c){};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inline bool operator==(const TFrameId &f) const { return f.m_frame == m_frame && f.m_letter == m_letter; };
Toshihiro Shimizu 890ddd
	inline bool operator!=(const TFrameId &f) const { return (m_frame != f.m_frame || m_letter != f.m_letter); };
Toshihiro Shimizu 890ddd
	inline bool operator<(const TFrameId &f) const { return (m_frame < f.m_frame || (m_frame == f.m_frame && m_letter < f.m_letter)); };
Toshihiro Shimizu 890ddd
	inline bool operator>(const TFrameId &f) const { return f < *this; }
Toshihiro Shimizu 890ddd
	inline bool operator>=(const TFrameId &f) const { return !operator<(f); }
Toshihiro Shimizu 890ddd
	inline bool operator<=(const TFrameId &f) const { return !operator>(f); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const TFrameId &operator++();
Toshihiro Shimizu 890ddd
	const TFrameId &operator--();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFrameId &operator=(const TFrameId &f)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_frame = f.m_frame;
Toshihiro Shimizu 890ddd
		m_letter = f.m_letter;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isEmptyFrame() const { return m_frame == EMPTY_FRAME; };
Toshihiro Shimizu 890ddd
	bool isNoFrame() const { return m_frame == NO_FRAME; };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//operator string() const;
Shinya Kitaoka 3bfa54
	std::string expand(FrameFormat format = FOUR_ZEROS) const;
Toshihiro Shimizu 890ddd
	int getNumber() const { return m_frame; };
Toshihiro Shimizu 890ddd
	char getLetter() const { return m_letter; };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! \relates TFrameId*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
inline std::ostream &operator<<(std::ostream &out, const TFrameId &f)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (f.isNoFrame())
Toshihiro Shimizu 890ddd
		out << "<noframe>";</noframe>
Toshihiro Shimizu 890ddd
	else if (f.isEmptyFrame())
Toshihiro Shimizu 890ddd
		out << "''";
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		out << f.expand().c_str();
Toshihiro Shimizu 890ddd
	return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!The class TFilePath defines a file path.Its constructor creates a string according 
Toshihiro Shimizu 890ddd
   to the platform and to certain rules.For more information see the constructor.*/
Toshihiro Shimizu 890ddd
class DVAPI TFilePath
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static bool m_underscoreFormatAllowed;
Shinya Kitaoka 3bfa54
	std::wstring m_path;
Shinya Kitaoka 3bfa54
	void setPath(std::wstring path);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	/*! this static method allows correct reading of levels in the form pippo_0001.tif (represented  always as pippo..tif) */
Toshihiro Shimizu 890ddd
	static void setUnderscoreFormatAllowed(bool state) { m_underscoreFormatAllowed = state; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!This constructor creates a string removing redundances ('//', './',etc.) and final slashes,
Toshihiro Shimizu 890ddd
	    correcting (redressing) the "twisted" slashes.
Toshihiro Shimizu 890ddd
 	    Note that if the current directory is ".", it becomes "".
Toshihiro Shimizu 890ddd
      If the path is "<alpha>:" a slash will be added*/</alpha>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	explicit TFilePath(const char *path = "");
Shinya Kitaoka 3bfa54
	explicit TFilePath(const std::string &path);
Toshihiro Shimizu 890ddd
	explicit TFilePath(const std::wstring &path);
Toshihiro Shimizu 890ddd
	explicit TFilePath(const QString &path);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~TFilePath(){};
Toshihiro Shimizu 890ddd
	TFilePath(const TFilePath &fp) : m_path(fp.m_path) {}
Toshihiro Shimizu 890ddd
	TFilePath &operator=(const TFilePath &fp)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_path = fp.m_path;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool operator==(const TFilePath &fp) const;
Toshihiro Shimizu 890ddd
	inline bool operator!=(const TFilePath &fp) const { return !(m_path == fp.m_path); }
Toshihiro Shimizu 890ddd
	bool operator<(const TFilePath &fp) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const std::wstring getWideString() const;
Toshihiro Shimizu 890ddd
	QString getQString() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!Returns:
Toshihiro Shimizu 890ddd
	 a.  "" if there is no filename extension
Toshihiro Shimizu 890ddd
	 b. "." if there is a filename extension but no frame
Toshihiro Shimizu 890ddd
	 c.".." if there are both filename extension and frame */
Shinya Kitaoka 3bfa54
	std::string getDots() const; // ritorna ""(no estensione), "."(estensione, no frame) o ".."(estensione e frame)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!Returns the filename extension, including leading period (.).
Toshihiro Shimizu 890ddd
	    Returns "" if there is no filename extension.*/
Shinya Kitaoka 3bfa54
	std::string getDottedType() const; // ritorna l'estensione con il PUNTO (se non c'e' estensione ritorna "")
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!Returns the filename extension, escluding leading period (.).
Toshihiro Shimizu 890ddd
	    Returns "" if there is no filename extension.*/
Shinya Kitaoka 3bfa54
	std::string getUndottedType() const; // ritorna l'estensione senza PUNTO
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!It is equal to getUndottedType():
Toshihiro Shimizu 890ddd
	    Returns the filename extension, excluding leading period (.).
Toshihiro Shimizu 890ddd
	    Returns "" if there is no filename extension.*/
Shinya Kitaoka 3bfa54
	std::string getType() const { return getUndottedType(); }; // ritorna l'estensione SENZA PUNTO
Toshihiro Shimizu 890ddd
	/*!Returns the base filename (no extension, no dots, no slash)*/
Shinya Kitaoka 3bfa54
	std::string getName() const;		 // noDot! noSlash!
Shinya Kitaoka 3bfa54
	std::wstring getWideName() const; // noDot! noSlash!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!Returns the filename (with extension, escluding in case the frame number).
Toshihiro Shimizu 890ddd
	    ex.: TFilePath("/pippo/pluto.0001.gif").getLevelName() == "pluto..gif"
Toshihiro Shimizu 890ddd
	 */
Shinya Kitaoka 3bfa54
	std::string getLevelName() const;		// es. TFilePath("/pippo/pluto.0001.gif").getLevelName() == "pluto..gif"
Toshihiro Shimizu 890ddd
	std::wstring getLevelNameW() const; // es. TFilePath("/pippo/pluto.0001.gif").getLevelName() == "pluto..gif"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!Returns the parent directory escluding the eventual final slash.*/
Toshihiro Shimizu 890ddd
	TFilePath getParentDir() const; // noSlash!;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFrameId getFrame() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isLevelName() const; //{return getFrame() == TFrameId(TFrameId::EMPTY_FRAME);};
Toshihiro Shimizu 890ddd
	bool isAbsolute() const;
Toshihiro Shimizu 890ddd
	bool isRoot() const;
Toshihiro Shimizu 890ddd
	bool isEmpty() const { return m_path == L""; };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath with extension type.
Toshihiro Shimizu 890ddd
   type is a string that indicate the filename extension(ex:. bmp or .bmp)*/
Shinya Kitaoka 3bfa54
	TFilePath withType(const std::string &type) const;
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath with filename "name".*/
Shinya Kitaoka 3bfa54
	TFilePath withName(const std::string &name) const;
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath with filename "name". Unicode*/
Toshihiro Shimizu 890ddd
	TFilePath withName(const std::wstring &name) const;
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath with parent directory "dir".*/
Toshihiro Shimizu 890ddd
	TFilePath withParentDir(const TFilePath &dir) const;
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath without parent directory */
Toshihiro Shimizu 890ddd
	TFilePath withoutParentDir() const { return withParentDir(TFilePath()); }
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath with frame "frame".*/
Toshihiro Shimizu 890ddd
	TFilePath withFrame(const TFrameId &frame, TFrameId::FrameFormat format = TFrameId::FOUR_ZEROS) const;
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath with a frame identified by an integer number "f".*/
Toshihiro Shimizu 890ddd
	TFilePath withFrame(int f) const { return withFrame(TFrameId(f)); };
Toshihiro Shimizu 890ddd
	/*!Return a TFilePath with a frame identified by an integer and by a character*/
Toshihiro Shimizu 890ddd
	TFilePath withFrame(int f, char letter) const { return withFrame(TFrameId(f, letter)); };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath withFrame() const { return withFrame(TFrameId(TFrameId::EMPTY_FRAME)); };
Toshihiro Shimizu 890ddd
	TFilePath withNoFrame() const { return withFrame(TFrameId(TFrameId::NO_FRAME)); }; // pippo.tif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath operator+(const TFilePath &fp) const;
Toshihiro Shimizu 890ddd
	TFilePath &operator+=(const TFilePath &fp) /*{*this=*this+fp;return *this;}*/;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	inline TFilePath operator+(const std::string &s) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return operator+(TFilePath(s));
Toshihiro Shimizu 890ddd
	};
Shinya Kitaoka 3bfa54
	inline TFilePath &operator+=(const std::string &s)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return operator+=(TFilePath(s));
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath &operator+=(const std::wstring &s);
Toshihiro Shimizu 890ddd
	TFilePath operator+(const std::wstring &s) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TFilePath res(*this);
Toshihiro Shimizu 890ddd
		return res += s;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isAncestorOf(const TFilePath &) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath operator-(const TFilePath &fp) const; // TFilePath("/a/b/c.txt")-TFilePath("/a/") == TFilePath("b/c.txt");
Toshihiro Shimizu 890ddd
													// se fp.isAncestorOf(this) == false ritorna this
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool match(const TFilePath &fp) const; // sono uguali a meno del numero di digits del frame
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// '/a/b/c.txt' => head='a' tail='b/c.txt'
Shinya Kitaoka 3bfa54
	void split(std::wstring &head, TFilePath &tail) const;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TMalformedFrameException : public TException
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 3bfa54
	TMalformedFrameException(const TFilePath &fp, const std::wstring &msg = std::wstring())
Toshihiro Shimizu 890ddd
		: TException(fp.getWideName() + L":" + msg)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	TMalformedFrameException();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
DVAPI std::ostream &operator<<(std::ostream &out, const TFilePath &path);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
typedef std::list<tfilepath> TFilePathSet;</tfilepath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif //T_FILEPATH_INCLUDED