//	Roast+ License


#ifndef __SFJP_OPENMGL_roast_mxp_File_win32_H__
#define __SFJP_OPENMGL_roast_mxp_File_win32_H__

//#include "roast_bufsafe.h"
#include "roast_str.h"
#include "roast/std/safe.hpp"

#include <vector>
#include <string>


namespace roast
{
namespace win32
{

using namespace std;

//	ファイルリスト取得
BOOL GetFileList( std::vector<std::string> &filelist, const char* szTargetDir, BOOL bIncludeDir=FALSE, BOOL bRecursive=TRUE );
BOOL GetFindDatas( std::vector<WIN32_FIND_DATA> &fdlist, const char* szTargetDir, BOOL bIncludeDir=FALSE );

//	パス取得系
const char* GetThisModuleDir( LPCSTR lpFilename, DWORD nSize );	//	最後の\は無しで返します
inline const char* GetThisModuleDir()	//	最後の\は無しで返します
{
	static char work[MAX_PATH+1];
	return GetThisModuleDir( work, (DWORD)sizeof(work) );
}
inline const char* GetFullPath( const char* szFile, char* szFullPathOut, int nBufSize )
{
	//	既にフルパスの場合
	if ( strchr( szFile, ':' ) != NULL )
	{
		roast::safe_strcpy( szFullPathOut, szFile, nBufSize );
		return szFullPathOut;
	}

	GetThisModuleDir( szFullPathOut, nBufSize );
	if ( get_last_char(szFullPathOut) != '\\' )
		roast::safe_strcat( szFullPathOut, "\\", nBufSize );
	roast::safe_strcat( szFullPathOut, szFile, nBufSize );
	return szFullPathOut;

/*	safe_strcpy( szFullPathOut, szFile, nBufSize );
	return GetThisModuleDir( szFullPathOut + strlen(szFullPathOut), nBufSize - strlen(szFullPathOut) ); */
}
inline const char* GetFullPath( const char* szFile ){
	static char work[MAX_PATH+1];
	return GetFullPath( szFile, work, sizeof(work) );
}
#define _FULLPATH(FILE)	GetFullPath(FILE)

inline void SetCurrentDirectoryThis(){
	char work[MAX_PATH+1];
	GetThisModuleDir( work, sizeof(work) );
	//SetCurrentDirectory(work);
	SetCurrentDirectoryA(work);
}

/*	MSLへ移動
//	拡張子
inline const char* GetExt( const char* szFromFile )
{
	const char* pWork = strrchr( szFromFile, '\\' );
	if ( pWork == NULL )
		pWork = szFromFile;
	//const char* pExt = strrchr( szFromFile, '.' );
	const char* pExt = strrchr( pWork, '.' );
	if ( pExt == NULL )
		return NULL;
	return pExt+1;
}
inline const char* GetExt2( const char* szFromFile )
{
	const char* pWork = strrchr( szFromFile, '\\' );
	if ( pWork == NULL )
		pWork = szFromFile;
	//const char* pExt = strrchr( szFromFile, '.' );
	const char* pExt = strrchr( pWork, '.' );
	if ( pExt == NULL )
		return "";
	return pExt+1;
}
char* GetNoExt( const char* szFile, char* szOutNotExt, int nBufSize );
inline const char* GetNoExt( const char* szFile ){
	static char work[MAX_PATH+1];
	return GetNoExt( szFile, work, sizeof(work) );}

//	拡張子操作
const char* RpcExt( const char* szFromFile, char* szToFile, int nBufSize, const char* szNewExt );
inline char* EraseExt( char* szFile ){
	char* pExt = strrchr( szFile, '.' );
	if ( pExt != NULL )
		*pExt = '\0';
	return szFile;
}
*/


inline bool IsDirectory( const char* szFile );
inline bool IsFile( const char* szFile );
inline BOOL IsExistFile( const char* szPath ){
	if ( exist_file(szPath) )
		return TRUE;
	else
		return FALSE;
}
inline BOOL IsExistDir( const char* szPath ){ return IsExistFile(szPath); }
inline BOOL IsExistDirectory( const char* szPath ){ return IsExistFile(szPath); }

/*
char* CutDirPath( const char* szFileFullPath, char* szOutDirPath, int nBufSize );
inline const char* CutDirPath( const char* szFileFullPath ){
	static char work[MAX_PATH+1];
	return CutDirPath( szFileFullPath, work, sizeof(work) );}
*/

/*	MSLへ移動
char* CutDirName( const char* szFilePath, char* szOutDir, int nBufSize );
inline char* CutDirName( const char* szFilePath ){
	static char work[MAX_PATH+1];
	return CutDirName( szFilePath, work, sizeof(work) );
}

char* CutFileName( const char* szFilePath, char* szOutFileName, int nBufSize );
inline char* CutFileName( const char* szFilePath ){
	static char work[MAX_PATH];
	return CutFileName( szFilePath, work, sizeof(work) );
}
*/

//BOOL DeleteDirectory(LPCTSTR lpPathName);

inline void CallXcopy( const char* szFrom, const char* szTo ){
	system_ex( "xcopy \"%s\" \"%s\" /e /y /i", szFrom, szTo );}


//	空ディレクトリ削除
BOOL CleanBlankDirectory( const char* szTargetDir, BOOL bIsDeleteThis=FALSE );




int GetDragQueryFiles( HDROP hDrop, vector<string> &filesOut );
inline int HdropToVctArray( HDROP hDrop, vector<string> &filesOut ){
	return GetDragQueryFiles( hDrop, filesOut ); }

BOOL GetFileList( vector<string> &filelist, const char* szTargetDir, DWORD dwOption=0 );
inline BOOL GetDirFiles( vector<string> &filelist, const char* szTargetDir, DWORD dwOption=0 ){
	return GetFileList( filelist, szTargetDir, dwOption ); }
inline BOOL GetDirAllFiles( vector<string> &filelist, const char* szTargetDir ){
	return GetFileList( filelist, szTargetDir, MWL_DIRFILES_INC_SUBDIR ); }

BOOL CopyDirectory( const char* szFrom, const char* szTo );

std::string& PathLastYenedString( std::string &str );
char* DeleteRedundancyYen( char *szStr, int nStrBufSize );



//	カレントディレクトリ記憶クラス
//	Restor()を呼び出すか、デストラクタにてカレントディレクトリが復元される。
//	デストラクタにてカレントディレクトリが復元されるのをキャンセルするにはCancel()を呼び出す。
class CCurDirRestorer
{
private:
	char m_szBkupPath[MAX_PATH+1];
public:
	CCurDirRestorer(){
		ZeroMemory(m_szBkupPath,sizeof(m_szBkupPath));
		GetCurrentDirectory( sizeof(m_szBkupPath), m_szBkupPath );
	}
	virtual ~CCurDirRestorer(){ Restore(); }
	//void Restore(){ if ( *m_szBkupPath != 0 ){ SetCurrentDirectory( m_szBkupPath ); } } <- 別にチェックいらなくね？
	//void Restore(){ SetCurrentDirectory( m_szBkupPath ); }
	void Restore(){ SetCurrentDirectoryA( m_szBkupPath ); }
	void Cancel(){ ZeroMemory(m_szBkupPath,sizeof(m_szBkupPath)); }
};


}
}

#endif//__SFJP_OPENMGL_roast_mxp_File_win32_H__