// Retrieves the fully-qualified path for the file that contains the onwer module.
// CuModuleFile.h
class CuModuleFile //: public IuModuleFile
{
public:
CuModuleFile();
~CuModuleFile();
wstring GetModuleFileName(wstring strFileName);
// 傳入空的字串 會傳回執行檔檔名
wstring GetUnModuleFileName(wstring strFileName = L"");
wstring CutRFind(wstring str, TCHAR ch);
};
// CuModuleFile.cpp
CuModuleFile::CuModuleFile()
{
}
CuModuleFile::~CuModuleFile()
{
}
//////////////////////////////////////////////////////////////////////
// 函式名稱:GetModuleFileName
// 函式說明:將strFileName 前面加上 主執行緒執行檔同目錄的路徑
// 使用範例:
// 啟動 C:\Documents and Settings\Eric Wang\My Documents\Work\test.exe
// strFileName = L"sample.ini"
// str = mf.GetModuleFileName(strFileName);
// Access(str = L"C:\Documents and Settings\Eric Wang\My Documents\Work\sample.ini");
//////////////////////////////////////////////////////////////////////
wstring CuModuleFile::GetModuleFileName(wstring strFileName)
{
wstring filename;
wstring strFile = strFileName;
wstring::size_type npos = -1;
wstring::size_type indexCh1a;
int nCount = 0;
while ( (indexCh1a = strFile.find(L"..\\")) != npos )
{
nCount++;
strFile = strFile.substr(indexCh1a + 3);
}
TCHAR szModule[255] = L"";
LPTSTR psz;
::GetModuleFileName(GetModuleHandle(NULL), szModule, MAX_PATH);
do
{
psz = wcsrchr(szModule, L'\\');
if (psz)
{
*psz = L'\0';
}
}while( nCount-- );
wcscat(szModule, L"\\");
wcscat(szModule, strFile.c_str());
return szModule;
}
//////////////////////////////////////////////////////////////////////
// 函式名稱:GetUnModuleFileName
// 函式說明:函路徑的檔名前面的路徑清掉, strFileName = NULL 時 會傳回主執行緒的執行檔檔名
// 使用範例:
// strFileName = L"C:\\Documents and Settings\\Eric Wang\My Documents\\Work\\temp.txt"
// str = mf.GetUnModuleFileName(strFileName);
// Access(str = L"temp.txt");
//////////////////////////////////////////////////////////////////////
wstring CuModuleFile::GetUnModuleFileName(wstring strFileName /* = L"" */)
{
wstring filename;
wstring strFull;
if( strFileName.size() == 0 )
{
TCHAR szModule[255] = L"";
::GetModuleFileName(GetModuleHandle(NULL), szModule, MAX_PATH);
strFull = szModule;
}
else
strFull = strFileName;
static const wstring::size_type npos = -1;
wstring::size_type indexCh1a;
indexCh1a = strFull.rfind('\\');
if( indexCh1a != npos )
filename = strFull.substr(indexCh1a + 1);
else
filename = strFull;
return filename;
}
//////////////////////////////////////////////////////////////////////
// 函式名稱: CutRFind
// 函式說明: 去掉 ch 右邊的字串
// 使用範例:
// str = text.txt
// str2 = mf.GetRFind(str, '.');
// Assert( str2 = L"text");
//////////////////////////////////////////////////////////////////////
wstring CuModuleFile::CutRFind(wstring str, TCHAR ch)
{
wstring strResult;
static const wstring::size_type npos = -1;
wstring::size_type indexCh1a;
indexCh1a = str.rfind(ch);
if( indexCh1a != npos )
strResult = str.substr(0, indexCh1a);
else
strResult = str;
return strResult;
}
2010年1月26日 星期二
CuModuleFile - Retrieves the fully-qualified path for the file that contains the onwer module.
訂閱:
張貼留言 (Atom)
寫程式時, 盡可能的將"資料"移到外部檔案, 在初期開發時, 最好使用文字檔當做資料來源, 要調整任何的參數, 都很方便, 不用rebuild.
回覆刪除