📄 filenamehelpers.cpp
字号:
//*****************************************************************************
// FILE: FileNameHelpers.cpp
// DESC: Implementation of functions to extract file title and file path
// given a filename.
//
// By Giovanni Dicanio <giovanni.dicanio@gmail.com>
// 2008, September 26th
//*****************************************************************************
#include "stdafx.h"
#include "FileNameHelpers.h" // Module header
namespace FileNameHelpers {
CString GetFilePath( LPCTSTR fileName )
{
// Check input string (must be not null and not empty)
ASSERT( fileName != NULL );
ASSERT( *fileName != TEXT('\0') );
// Scan the string from last character to first one.
// Stop scanning when we find \ or : .
// The left part will be the file path, the right part is the file title.
// Start from the end of the string
LPCTSTR pchCurrent = fileName + _tcslen( fileName ) - 1;
// Just to make code more clear (I could have used just 'fileName' as
// pointer to beginning of string...)
LPCTSTR pchStart = fileName;
while ( pchCurrent >= pchStart )
{
if ( *pchCurrent == TEXT('\\') )
{
// backslash not included in the path
--pchCurrent;
break;
}
else if ( *pchCurrent == TEXT(':') )
{
// colon included in the path
break;
}
else
{
// Neither '\' nor ':' found, so we are still pointing
// to file title characters. So we must move left one more.
--pchCurrent;
}
}
if ( pchCurrent < pchStart )
{
// No file path present - just return empty string
return TEXT("");
}
// On success, at the exit of while loop, pchCurrent points to last character
// of file path, i.e. the file path is defined by range
// [pchStart, pchCurrent]
int pathLength = static_cast< int >(pchCurrent - pchStart) + 1;
return CString( pchStart, pathLength );
}
CString GetFileTitle( LPCTSTR fileName )
{
// Check input string (must be not null and not empty)
ASSERT( fileName != NULL );
ASSERT( *fileName != TEXT('\0') );
// Scan the string from last character to first one.
// Stop scanning when we find \ or : .
// The left part will be the file path, the right part is the file title.
// Start from the end of the string
LPCTSTR pchEnd = fileName + _tcslen( fileName ) - 1;
// Start from the end of the string
LPCTSTR pchCurrent = pchEnd;
// If the last character is a \ or a :, then there is no title in input string
if ( *pchCurrent == TEXT('\\') || *pchCurrent == TEXT(':') )
{
// No title
return TEXT("");
}
// Move left - first character already processed
--pchCurrent;
// Just to make code more clear (I could have used just 'pszFileName' as
// pointer to beginning of string...)
LPCTSTR pchStart = fileName;
while ( pchCurrent >= pchStart )
{
if ( *pchCurrent == TEXT('\\') || *pchCurrent == TEXT(':') )
{
// Neither backslash nor colon are included in file title
++pchCurrent;
break;
}
// We did not find \ or :, so move left in search of them
// (we are still pointing to file title characters)
--pchCurrent;
}
if ( pchCurrent < pchStart )
{
// The input string was made by only title (no path), so return
// the whole input string
return fileName;
}
// On success, at the exit of while loop, pchCurrent points to first character
// of file title, i.e. the file path is defined by range
// [pchCurrent, pchEnd]
int titleLength = static_cast< int >(pchEnd - pchCurrent) + 1;
return CString( pchCurrent, titleLength );
}
} // namespace FileNameHelpers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -