⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtlfile.h

📁 一个使用wtl写的完整的多窗口浏览器
💻 H
字号:
#ifndef __MTLFILE_H__
#define __MTLFILE_H__

// MTL Version 0.03
// Copyright (C) 2000 MB<mb2@geocities.co.jp>
// All rights unreserved.
//
// This file is a part of Mb Template Library.
// The code and information is *NOT* provided "as-is" without
// warranty of any kind, either expressed or implied.
//
// Last updated: August 16, 2000

#pragma once

namespace MTL
{

inline bool MtlIsValidPath(const CString& strPath)
{
	DWORD dw = ::GetFileAttributes(strPath);
	if (dw == 0xFFFFFFFF)
		return false;
	else
		return true;
}

inline bool MtlIsDirectory(const CString& strPath)
{
	DWORD dw = ::GetFileAttributes(strPath);
	if (dw == 0xFFFFFFFF)
		return false;

	if (dw & FILE_ATTRIBUTE_DIRECTORY)
		return true;
	else
		return false;
}

inline bool MtlIsDirectoryPath(const CString& strPath, bool bUseSystem = false)
{
	if (!bUseSystem) {
		if (__MtlIsLastChar(strPath, _T('\\')))
			return true;
		else
			return false;
	}
	else {
		return MtlIsDirectory(strPath);
	}
}

inline CString MtlGetChangedExtFromModuleName(const CString& strExt)
{
	CString str;
	DWORD dwRet = ::GetModuleFileName(_Module.GetModuleInstance(), str.GetBufferSetLength(_MAX_PATH+10), _MAX_PATH);
	str.ReleaseBuffer();

	if (dwRet == 0 || str.IsEmpty())
		return CString();

	int nIndex = str.ReverseFind(_T('.'));
	if (nIndex == -1)
		return CString();

	return str.Left(nIndex) + strExt;
}

inline bool MtlIsExt(const CString& strPath, const CString& strExt)
{
	CString strExtSrc = strPath.Right(4);
	if (strExtSrc.CompareNoCase(strExt) == 0)
		return true;
	else
		return false;
}

inline bool MtlIsProtocol(const CString& strPath, const CString& strProSrc)
{
	int nIndex = strPath.Find(_T(':'));
	if (nIndex == -1)
		return false;

	CString strPro = strPath.Left(nIndex);
	if (strPro == strProSrc)
		return true;
	else
		return false;
}

inline void MtlValidateFileName(CString& strName)
{
	strName.Replace(_T("\\"),_T("-"));
	strName.Replace(_T("/"),_T("-"));
	strName.Replace(_T(":"),_T("-"));
	strName.Replace(_T(","),_T("-"));
	strName.Replace(_T(";"),_T("-"));
	strName.Replace(_T("*"),_T("-"));
	strName.Replace(_T("?"),_T("-"));
	strName.Replace(_T("\""),_T("-"));
	strName.Replace(_T("<"),_T("-"));
	strName.Replace(_T(">"),_T("-"));
	strName.Replace(_T("|"),_T("-"));
}

inline CItemIDList MtlGetHtmlFileIDList()
{
	CString strNewFile = MtlGetChangedExtFromModuleName(_T(".html"));
	if (strNewFile.IsEmpty())
		return CItemIDList();

	HANDLE h = ::CreateFile(strNewFile,
		GENERIC_WRITE,
		0,
		NULL,
		OPEN_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	if (h == INVALID_HANDLE_VALUE)
		return CItemIDList();

	::CloseHandle(h);

	return strNewFile;
}

inline void MtlDeleteHtmlFileIDList()
{
	CItemIDList idl = MtlGetChangedExtFromModuleName(_T(".html"));
	::DeleteFile(idl.GetPath());
}

int MtlGetNormalIconIndex(LPCITEMIDLIST pidl, LPCITEMIDLIST pidlHtm = NULL)
{
	if (pidlHtm != NULL && MtlIsTooSlowIDList(pidl))
		return MtlGetSystemIconIndex(pidlHtm);
	else
		return MtlGetSystemIconIndex(pidl);
}

int MtlGetSelectedIconIndex(LPCITEMIDLIST pidl, bool bFolder, LPCITEMIDLIST pidlHtm = NULL)
{
	if (pidlHtm != NULL && MtlIsTooSlowIDList(pidl))
		return MtlGetSystemIconIndex(pidlHtm);
	else if (bFolder)
		return MtlGetSystemIconIndex(pidl, SHGFI_SMALLICON | SHGFI_OPENICON);
	else
		return MtlGetSystemIconIndex(pidl);
}

inline CString MtlGetDisplayTextFromPath(const CString& strPath)
{
	CString str(strPath);
	if (MtlIsDirectoryPath(strPath)) {
		int nIndex = str.ReverseFind(_T('\\'));
		str = str.Left(nIndex);
		nIndex = str.ReverseFind(_T('\\'));
		str = str.Right(str.GetLength() - nIndex - 1);
	}
	else {
		int nIndex = str.ReverseFind(_T('\\'));
		str = str.Right(str.GetLength() - nIndex - 1);
		nIndex = str.ReverseFind(_T('.'));
		if (nIndex != -1)
			str = str.Left(nIndex);
	}

	return str;
}

inline CString MtlGetFileName(const CString& strPath)
{
	int nIndex = strPath.ReverseFind(_T('\\'));

	if (nIndex == strPath.GetLength() - 1) {// maybe directory
		return MtlGetDisplayTextFromPath(strPath);
	}

	return strPath.Right(strPath.GetLength() - nIndex - 1);
}

inline bool MtlIsInternetFile(const CString& strPath)
{
	CString strExt = strPath.Right(4);
	CString strExt2 = strPath.Right(5);
	CString strExt3 = strPath.Right(6);
	if (strExt2.CompareNoCase(_T(".html")) == 0 ||
		strExt3.CompareNoCase(_T(".shtml")) == 0 ||
		strExt.CompareNoCase(_T(".htm")) == 0 ||
		strExt.CompareNoCase(_T(".url")) == 0 ||
		strExt.CompareNoCase(_T(".cgi")) == 0 ||
		strExt.CompareNoCase(_T(".asp")) == 0 ||
		strPath.Right(1) == _T('/'))
		return true;
	else
		return false;
}

inline LPTSTR _MtlMakeFileOperationBuffer(const CSimpleArray<CString>& arrFiles)
{
	int nLen = 0;
	int i;
	for (i = 0; i < arrFiles.GetSize(); ++i)
		nLen += arrFiles[i].GetLength() + 1;

	nLen += 1;

	LPTSTR lpsz = new TCHAR[nLen];
	::memset(lpsz, 0, nLen);

	LPTSTR lpszRunner = lpsz;
	for (i = 0; i < arrFiles.GetSize(); ++i) {
		::lstrcpy(lpszRunner, arrFiles[i]);
		lpszRunner += arrFiles[i].GetLength() + 1;
	}

	return lpsz;
}

inline bool MtlCopyFile(const CString& strTo, const CSimpleArray<CString>& arrFiles)
{
	LPTSTR lpszFrom = _MtlMakeFileOperationBuffer(arrFiles);

	CString strTo_ = strTo + _T('\0');
	SHFILEOPSTRUCT fop = { NULL, FO_COPY, lpszFrom, strTo_, 
		FOF_ALLOWUNDO, FALSE, NULL, NULL };

	bool bOk = (::SHFileOperation(&fop) == 0);

	delete [] lpszFrom;
	return bOk;
}

inline bool MtlMoveFile(const CString& strTo, const CSimpleArray<CString>& arrFiles)
{
	LPTSTR lpszFrom = _MtlMakeFileOperationBuffer(arrFiles);

	CString strTo_ = strTo + _T('\0');
	SHFILEOPSTRUCT fop = { NULL, FO_MOVE, lpszFrom, strTo_, 
		FOF_ALLOWUNDO, FALSE, NULL, NULL };

	bool bOk = (::SHFileOperation(&fop) == 0);

	delete [] lpszFrom;
	return bOk;
}
/*
inline bool MtlCopyFile(const CString& strTo, LPCTSTR lpszFrom)
{
	CString strTo_ = strTo + _T('\0');
	SHFILEOPSTRUCT fop = { NULL, FO_COPY, lpszFrom, strTo_, 
		FOF_ALLOWUNDO, FALSE, NULL, NULL };

	return ::SHFileOperation(&fop) == 0;
}

inline bool MtlMoveFile(const CString& strTo, LPCTSTR lpszFrom)
{
	CString strTo_ = strTo + _T('\0');
	SHFILEOPSTRUCT fop = { NULL, FO_MOVE, lpszFrom, strTo_, 
		FOF_ALLOWUNDO, FALSE, NULL, NULL };

	return ::SHFileOperation(&fop) == 0;
}*/

inline CString MtlGetShortcutLink(const CString& strPath)
{
	if (!MtlIsExt(strPath, _T(".lnk")))
		return strPath;

	CComPtr<IShellLink> spShellLink;
	// Create IShellLink Objec
	HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLink, (void **)&spShellLink);
	if (SUCCEEDED(hr)) {
		CComQIPtr<IPersistFile> spPersistFile(spShellLink);
		if (spPersistFile) {
			// load shortcut
			USES_CONVERSION;
			hr = spPersistFile->Load(T2COLE(strPath), STGM_READ);
			if (SUCCEEDED(hr)) {
				// get link
				TCHAR szFilePath[MAX_PATH];
				::lstrcpy(szFilePath, strPath);
				WIN32_FIND_DATA wfd;
                hr = spShellLink->GetPath(szFilePath, MAX_PATH, &wfd, SLGP_UNCPRIORITY);
				if (SUCCEEDED(hr))
					return szFilePath;
            }
		}
	}

    return strPath;
}

inline bool MtlIsFileExtExe(const CString& strPath)
{
	if (MtlIsExt(strPath, _T(".exe")))
		return true;

	CString strLink = MtlGetShortcutLink(strPath);
	return MtlIsExt(strLink, _T(".exe"));
}

inline CString MtlGetExt(const CString& strUrl, bool bFromLeft)
{
	if (strUrl.IsEmpty())
		return CString();

	int nIndex = -1;
	if (bFromLeft) {
		int nIndexName = strUrl.ReverseFind(_T('/'));
		if (nIndexName == -1)
			nIndexName = strUrl.ReverseFind(_T('\\'));

		if (nIndexName == -1)
			return CString();

		nIndex = __MtlFindChar(strUrl, nIndexName, _T('.'));
	}
	else {
		nIndex = strUrl.ReverseFind(_T('.'));
	}

	if (nIndex == -1)
		return CString();

	CString str_ = strUrl.Mid(nIndex + 1);

	int nLen = str_.GetLength() + 1;
	LPTSTR lpsz = (LPTSTR)_alloca(nLen * sizeof(TCHAR));
	::lstrcpy(lpsz, str_);
		
	for (LPCTSTR lpszRunner = lpsz; *lpszRunner != _T('\0'); lpszRunner = ::CharNext(lpszRunner)) {
		if (!Mtl_isalpha(*lpszRunner))
			break;
	}

	return strUrl.Mid(nIndex + 1, lpszRunner - lpsz);
}

inline void _MtlRemoveHeaderNonAlpha(CString& str)
{
	if (str.IsEmpty())
		return;

	int nLen = str.GetLength() + 1;
	LPTSTR lpsz = (LPTSTR)_alloca(nLen * sizeof(TCHAR));
	::lstrcpy(lpsz, str);
		
	for (LPCTSTR lpszRunner = lpsz; *lpszRunner != _T('\0'); lpszRunner = ::CharNext(lpszRunner)) {
		if (Mtl_isalpha(*lpszRunner))
			break;
	}

	str = str.Mid(lpszRunner - lpsz);
}

inline bool _MtlFindExtFromArray(const CSimpleArray<CString>& arrExt, const CString& strExt)
{
	if (strExt.IsEmpty())
		return false;

	for (int i = 0; i < arrExt.GetSize(); ++i) {
		if (strExt.CompareNoCase(arrExt[i]) == 0)
			return true;
	}

	return false;
}

inline CString _MtlGetTrailingSeparator(const CString& strUrl)
{
	int nIndex =  strUrl.ReverseFind(_T('/'));
	if (nIndex == strUrl.GetLength() - 1)
		return _T('/');

	nIndex = strUrl.ReverseFind(_T('\\'));
	if (nIndex == strUrl.GetLength() - 1)
		return _T('\\');

	return CString();
}

inline void MtlBuildExtArray(CSimpleArray<CString>& arrExt, const CString& strExts)
{
	int nFirst = 0;
	int nLast = 0;

	while (nFirst < strExts.GetLength()) {
		nLast = __MtlFindChar(strExts, nFirst, _T(';'));
		if (nLast == -1)
			nLast = strExts.GetLength();

		CString strExt = strExts.Mid(nFirst, nLast - nFirst);
		if (!strExt.IsEmpty() && arrExt.Find(strExt) == -1) {
//			clbTRACE(_T("_BuildExtsArray.Add(%s)\n"), strExt);
			arrExt.Add(strExt);
		}

		nFirst = nLast + 1;
	}
}

inline void MtlBuildUrlArray(CSimpleArray<CString>& arrUrl, const CSimpleArray<CString>& arrExt, const CString& strText_)
{
	int nFirst = 0;
	int nLast = 0;

	CString strText = strText_;
	strText.Replace(_T("\r"), _T(""));

	while (nFirst < strText.GetLength()) {
		nLast = __MtlFindChar(strText, nFirst, _T('\n'));
		if (nLast == -1)
			nLast = strText.GetLength();

		CString strPart = strText.Mid(nFirst, nLast - nFirst);
		if (!strPart.IsEmpty()) {
			CString strExt = MtlGetExt(strPart, true);
			CString strUrl;
			if (_MtlFindExtFromArray(arrExt, strExt)) {
//				clbTRACE(_T("_BuildUrlArray.Add(%s)\n"), strPart);
				strUrl = strPart;
			}
			else {
				strExt = MtlGetExt(strPart, false);
				if (_MtlFindExtFromArray(arrExt, strExt)) {
//					clbTRACE(_T("_BuildUrlArray.Add(%s)\n"), strPart);
					strUrl = strPart;
				}
				else {
					MtlRemoveTrailingSpace(strPart);
					strExt = _MtlGetTrailingSeparator(strPart);
					if (_MtlFindExtFromArray(arrExt, strExt)) {
//						clbTRACE(_T("_BuildUrlArray.Add(%s)\n"), strPart);
						strUrl = strPart;
					}
				}
			}

			MtlRemoveTrailingSpace(strUrl);
			_MtlRemoveHeaderNonAlpha(strUrl);
			if (!strUrl.IsEmpty())
				arrUrl.Add(strUrl);

		}

		nFirst = nLast + 1;
	}
}


} // namespace MTL;

#endif // __MTLMISC_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -