개발/C,C++
절대 경로 비교
-=HaeJuK=-
2024. 2. 14. 15:40
반응형
아스트릭 처리된 경로 비교를 해봅시다
절대 경로를 비교 할 때 * 처리된 경로까지 넣어서 비교해 봅시다.\
구현
/******************************************************************************
* _ _ _ _ __ _____ _ _
*| | | | | | | |/ / | __ \ | | | |
*| |__| | __ _ ___ | |_ _| ' / | | | | _____ __ | | __ _| |__
*| __ |/ _` |/ _ \_ | | | | | < | | | |/ _ \ \ / / | | / _` | '_ \
*| | | | (_| | __/ |__| | |_| | . \ | |__| | __/\ V / | |___| (_| | |_) |
*|_| |_|\__,_|\___|\____/ \__,_|_|\_\ |_____/ \___| \_(_) |______\__,_|_.__/
*
* Copyright (c) HaeJuK Dev Lab All Rights Reserved.
*
*******************************************************************************/
/**
@file CompareString.cpp
@brief
@author Woolim, Choi
@date Create
@ntoe
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <cwctype>
namespace
{
// 대소문자를 구분하지 않는 비교를 위한 템플릿 오버로드
char _toUpperCase( char ch )
{
return std::toupper( static_cast<unsigned char>(ch) );
}
wchar_t _toUpperCase( wchar_t ch )
{
return std::towupper( ch );
}
// 문자 비교 템플릿 함수
template<typename CharType>
bool CharCompare( CharType c1, CharType c2, bool bCompCase )
{
if( bCompCase )
{
return c1 == c2;
}
else
{
return _toUpperCase( c1 ) == _toUpperCase( c2 );
}
}
}
// 문자열 패턴 매칭 템플릿 함수
template<typename StringType>
bool isCompareString( bool _bCompCase, const StringType& _refMask, const StringType& _refPath )
{
using CharType = typename StringType::value_type;
const CharType* pszMask = _refMask.c_str();
const CharType* pszPath = _refPath.c_str();
while( *pszPath && *pszMask != '*' )
{
if( *pszMask != '?' && !CharCompare( *pszMask, *pszPath, _bCompCase ) )
{
return false;
}
++pszMask;
++pszPath;
}
const CharType* mp = nullptr;
const CharType* pp = nullptr;
while( *pszPath )
{
if( *pszMask == '*' )
{
mp = ++pszMask;
pp = pszPath + 1;
}
else if( *pszMask == '?' || CharCompare( *pszMask, *pszPath, _bCompCase ) )
{
++pszMask;
++pszPath;
}
else
{
pszMask = mp;
pszPath = pp++;
}
}
while( *pszMask == '*' )
{
++pszMask;
}
return !*pszMask;
}
int main()
{
// 멀티바이트 문자열 예제
std::string mbMask = "*:\\temp\\data.exe";
std::string mbPath = "c:\\temp\\data.exe";
std::cout << "Multibyte: " << (isCompareString( false, mbMask, mbPath ) ? "Equal" : "Not Equal") << std::endl;
// 유니코드 문자열 예제
std::wstring wcMask = L"*:\\temp\\data.exe";
std::wstring wcPath = L"c:\\temp\\data.exe";
std::wcout << L"Unicode: " << (isCompareString( false, wcMask, wcPath ) ? L"Equal" : L"Not Equal") << std::endl;
return 0;
}
728x90