Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
imlinhanchao committed Jun 29, 2020
0 parents commit 4f544b2
Show file tree
Hide file tree
Showing 4 changed files with 517 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Hancel Lin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
256 changes: 256 additions & 0 deletions Path.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
#include "stdafx.h"
#include "Path.h"
#include <Shlwapi.h>
#include "imagehlp.h"
#pragma comment(lib, "imagehlp.lib")

namespace Easy {

CString Path::GetFileName( CString sPath )
{
CString sFilename(PathFindFileName(sPath.GetBuffer()));
sPath.ReleaseBuffer();
return sFilename;
}

CString Path::GetDirectory( CString sPath )
{
PathRemoveFileSpec(sPath.GetBuffer());
sPath.ReleaseBuffer();
return sPath + _T("\\");
}

CString Path::GetExtName( CString sPath )
{
CString sFilename(PathFindExtension(sPath.GetBuffer()));
sPath.ReleaseBuffer();
return sFilename;
}

CString Path::Resolve( CString sPath, CString sPathNext )
{
PathAddBackslash(sPath.GetBuffer(sPath.GetLength() + 1));
sPath.ReleaseBuffer();
CString sPathFull = sPath + sPathNext;
CString sPathFinal;

PathCanonicalize(sPathFinal.GetBuffer(sPathFull.GetLength() + 100), sPathFull.GetBuffer());
sPathFinal.ReleaseBuffer();
sPathFull.ReleaseBuffer();

if (IsDirectory(sPathFinal)) PathAddBackslash(sPathFinal.GetBuffer(sPath.GetLength() + 1));
sPathFinal.ReleaseBuffer();

return sPathFinal;
}

CString Path::Omit( CString sPath, int nSize )
{
CString sOmitPath;
PathCompactPathEx(sOmitPath.GetBuffer(sPath.GetLength() + 100), sPath.GetBuffer(), nSize, 0);
sOmitPath.ReleaseBuffer();
sPath.ReleaseBuffer();

return sOmitPath;
}

CString Path::GetCurDirectory( CString sPath/*=_T("")*/ )
{
return Resolve(GetDirectory(GetProgramPath()), sPath);
}

CString Path::GetTmpDirectory( CString sPath/*=_T("")*/ )
{
DWORD dwLen = 1024;
TCHAR szPath[1024] = {0};
if(0 != GetTempPath(dwLen, szPath))
{
return Resolve(CString(szPath), sPath);
}
return _T("");
}

CString Path::GetUserDirectory( CString sPath/*=_T("")*/ )
{
return Resolve(GetDesktopDirectory() + _T(".."), sPath);
}

CString Path::GetAppDataDirectory( CString sPath/*=_T("")*/ )
{
TCHAR szPath[MAX_PATH];
SHGetSpecialFolderPath(NULL, szPath, CSIDL_LOCAL_APPDATA, FALSE);
return Resolve(CString(szPath), sPath);
}

CString Path::GetDesktopDirectory( CString sPath/*=_T("")*/ )
{
TCHAR szPath[MAX_PATH];
SHGetSpecialFolderPath(NULL, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
return Resolve(CString(szPath), sPath);
}

CString Path::GetStartupDirectory( CString sPath/*=_T("")*/ )
{
TCHAR szPath[MAX_PATH];
SHGetSpecialFolderPath(NULL, szPath, CSIDL_STARTUP, FALSE);
return Resolve(CString(szPath), sPath);
}

CString Path::GetStartMenuDirectory( CString sPath/*=_T("")*/ )
{
TCHAR szPath[MAX_PATH];
SHGetSpecialFolderPath(NULL, szPath, CSIDL_STARTMENU, FALSE);
return Resolve(CString(szPath), sPath);
}

CString Path::GetProgramPath(void)
{
CString sPath = _T("");
GetModuleFileName(NULL, sPath.GetBuffer(MAX_PATH), MAX_PATH);
sPath.ReleaseBuffer();
return sPath;
}

CString Path::GetProgramName(void)
{
return GetFileName(GetProgramPath());
}

bool Path::IsDirectory( CString sPath )
{
bool bDirectory = PathIsDirectory(sPath.GetBuffer());
sPath.ReleaseBuffer();
return bDirectory;
}

bool Path::IsEmpty( CString sPath )
{
bool bEmpty = PathIsDirectoryEmpty(sPath.GetBuffer());
sPath.ReleaseBuffer();
return bEmpty;
}

bool Path::Exists( CString sPath )
{
bool bExists = PathFileExists(sPath.GetBuffer());
sPath.ReleaseBuffer();
return bExists;
}

CString Path::Browse( LPCTSTR lpszFilter, LPCTSTR lpszDefExt, BOOL bOpen, LPCTSTR lpszFileName )
{
CString sPathName = _T("");
CFileDialog dlg(bOpen,
lpszDefExt,
lpszFileName,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_CREATEPROMPT | (bOpen ? OFN_FILEMUSTEXIST : 0),
lpszFilter,
NULL);
if(dlg.DoModal() == IDOK)
{
sPathName = dlg.GetPathName();
}
return sPathName;
}

static int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch(uMsg)
{
case BFFM_INITIALIZED:
::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPTSTR)(LPCTSTR)lpData);
break;
case BFFM_SELCHANGED:
{
TCHAR szCurrent[MAX_PATH];
SHGetPathFromIDList((LPCITEMIDLIST)lParam, szCurrent);
::SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szCurrent);
}
break;
default:
break;
}
return 0;
}

CString Path::Folder( HWND hWnd, CString sRootPath/*=_T("")*/ )
{
ASSERT(hWnd);

CString sPath = _T("");
LPITEMIDLIST pIdList = NULL;
if (sRootPath.IsEmpty()) sRootPath = GetDesktopDirectory();

TCHAR szBuffer[MAX_PATH];
ZeroMemory(szBuffer, MAX_PATH);
_tcscpy(szBuffer, sRootPath.GetBuffer());
sRootPath.ReleaseBuffer();

BROWSEINFO bi;
bi.hwndOwner = hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szBuffer;
bi.lpszTitle = _T("");
bi.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM)szBuffer;
bi.iImage = 0;

if(NULL != (pIdList = SHBrowseForFolder(&bi)))
{
SHGetPathFromIDList(pIdList, sPath.GetBuffer(MAX_PATH));
sPath.ReleaseBuffer();
}

return sPath;
}

vector<CString> Path::Traversing( CString sDirectory, FILITER filter/*=NULL*/ )
{
vector<CString> lstPath;

PathAddBackslash(sDirectory.GetBuffer(sDirectory.GetLength() + 1));
sDirectory.ReleaseBuffer();

if(!sDirectory.IsEmpty() && Exists(sDirectory) && !IsEmpty(sDirectory))
{
CFileFind ff;
BOOL bFound = ff.FindFile(sDirectory + _T("*"), 0);
while(bFound)
{
bFound = ff.FindNextFile();
if(ff.IsDots())
{
continue;
}

SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
CString sPath = ff.GetFilePath();
if(ff.IsDirectory())
{
if (filter != NULL && !filter(sPath)) continue;
vector<CString> lstPathEx = Traversing(sPath, filter);
lstPath.insert(lstPath.begin(), lstPathEx.begin(), lstPathEx.end());
}
else
{
if (filter != NULL && !filter(sPath)) continue;
lstPath.push_back(sPath);
}
}
ff.Close();
return lstPath;
}
return lstPath;
}

bool Path::Create( CString sPath )
{
PathAddBackslash(sPath.GetBuffer(sPath.GetLength() + 1));
sPath.ReleaseBuffer();

USES_CONVERSION;
return MakeSureDirectoryPathExists(T2A(sPath));
}

}
Loading

0 comments on commit 4f544b2

Please sign in to comment.