2013-08-05 6 views
3

라이브러리 함수 _wsplitpath_s()을 사용하여 경로를 파싱하면 Win32 API 함수와 함께 사용할 수 있습니다.긴 경로와 함께 사용할 _wsplitpath_s() 대안

this MSDN document에 따르면 긴 경로 (MAX_PATH 자보다 긴 문자)는 접두사 \\?\과 함께 사용해야합니다. 그러나 전체 경로 문자열에 붙여 넣으면 _wsplitpath_s() 올바르게 구문 분석 할 수 없습니다.

예제 코드 :

Full path to be splitted : \\?\K:\A Directory\Another Directory\File Name.After Period.extension 
Drive     : 
Directory    : \\?\K:\A Directory\Another Directory\ 
File name    : File Name.After Period 
Extension    : .extension 
Error value returned  : 0 
Did error occur?   : 0 
Value of 'EINVAL'  : 22 

이 짧은 경로를 작동하는 방법 :

Full path to be splitted : K:\A Directory\Another Directory\File Name.After Period.extension 
Drive     : K: 
Directory    : \A Directory\Another Directory\ 
File name    : File Name.After Period 
Extension    : .extension 

긴 경로에 대한 예상되는 동작 :

위의 코드의

std::wstring FullPath = L"\\\\?\\K:\\A Directory\\Another Directory\\File Name.After Period.extension"; 
std::wstring Drive, Directory, FileName, Extension; 
Drive.resize  (FullPath.size()); 
Directory.resize (FullPath.size()); 
FileName.resize  (FullPath.size()); 
Extension.resize (FullPath.size()); 
errno_t Error = _wsplitpath_s( FullPath.c_str(), 
           &Drive[0], 
           Drive.size(), 
           &Directory[0], 
           Directory.size(), 
           &FileName[0], 
           FileName.size(), 
           &Extension[0], 
           Extension.size()); 
std::wcout << L"Full path to be splitted : " << FullPath.c_str()  << std::endl; 
std::wcout << L"Drive     : " << Drive.c_str()  << std::endl; 
std::wcout << L"Directory    : " << Directory.c_str() << std::endl; 
std::wcout << L"File name    : " << FileName.c_str()  << std::endl; 
std::wcout << L"Extension    : " << Extension.c_str() << std::endl; 
std::wcout << L"Error value returned  : " << Error    << std::endl; 
std::wcout << L"Did error occur?   : " << (Error == EINVAL) << std::endl; 
std::wcout << L"Value of 'EINVAL'  : " << EINVAL    << std::endl; 

실제 출력

Full path to be splitted : \\?\K:\A Directory\Another Directory\File Name.After Period.extension 
Drive     : K: 
Directory    : \A Directory\Another Directory\ 
File name    : File Name.After Period 
Extension    : .extension 

긴 경로 이름 지정 규칙을 지원하는 _wsplitpath_s()의 대안이 있습니까?
STL 알고리즘, Win32 API 함수 또는 C 라이브러리 함수가 지정된 순서로 허용됩니다.

+0

정확하게 당신이 잘못된 것입니다 무슨 말? 당신이 제공하는 결과물은 나에게 잘 들립니다. –

+0

@Drew McGomen : 드라이브 이름이 비어 있습니다. 실제로 드라이브 이름은 긴 경로 접두어와 함께 디렉토리 이름에 붙여 넣어집니다. 물론이 코드에이 코드를 적용 할 수는 있지만 긴 경로에서이 함수가 항상 작동 할 수 있는지 확인할 수 있습니다. 이것은 문서화되지 않은 행동이며 아무도 그 기능이 영원히 작동하지 않을 것이라고 확신 할 수 있습니다. – hkBattousai

+0

잠깐, 왜 내가 언급 했습니까? 나는 의견을 말하고 그것을 삭제 했습니까? –

답변

0

당신은 간단하게 "긴 경로"도입부 건너 뜁니다 래퍼 함수를 ​​작성할 수 있습니다 예컨대 :

errno_t _wsplitpath_long_s(
    const wchar_t * path, 
    wchar_t * drive, 
    size_t driveNumberOfElements, 
    wchar_t *dir, 
    size_t dirNumberOfElements, 
    wchar_t * fname, 
    size_t nameNumberOfElements, 
    wchar_t * ext, 
    size_t extNumberOfElements 
) 
{ 
    if (_wcsnicmp(path, L"\\\\?\\", 4) == 0) 
     path += 4; 
    return _wsplitpath_s(path, drive, driveNumberOfElements, 
     dir, dirNumberOfElements, fname, nameNumberOfElements, 
     ext, extNumberOfElements); 
}