1370b324cSopenharmony_ci// Archive/Common/ItemNameUtils.cpp 2370b324cSopenharmony_ci 3370b324cSopenharmony_ci#include "StdAfx.h" 4370b324cSopenharmony_ci 5370b324cSopenharmony_ci#include "ItemNameUtils.h" 6370b324cSopenharmony_ci 7370b324cSopenharmony_cinamespace NArchive { 8370b324cSopenharmony_cinamespace NItemName { 9370b324cSopenharmony_ci 10370b324cSopenharmony_cistatic const wchar_t kOsPathSepar = WCHAR_PATH_SEPARATOR; 11370b324cSopenharmony_ci 12370b324cSopenharmony_ci#if WCHAR_PATH_SEPARATOR != L'/' 13370b324cSopenharmony_cistatic const wchar_t kUnixPathSepar = L'/'; 14370b324cSopenharmony_ci#endif 15370b324cSopenharmony_ci 16370b324cSopenharmony_civoid ReplaceSlashes_OsToUnix 17370b324cSopenharmony_ci#if WCHAR_PATH_SEPARATOR != L'/' 18370b324cSopenharmony_ci (UString &name) 19370b324cSopenharmony_ci { 20370b324cSopenharmony_ci name.Replace(kOsPathSepar, kUnixPathSepar); 21370b324cSopenharmony_ci } 22370b324cSopenharmony_ci#else 23370b324cSopenharmony_ci (UString &) {} 24370b324cSopenharmony_ci#endif 25370b324cSopenharmony_ci 26370b324cSopenharmony_ci 27370b324cSopenharmony_ciUString GetOsPath(const UString &name) 28370b324cSopenharmony_ci{ 29370b324cSopenharmony_ci #if WCHAR_PATH_SEPARATOR != L'/' 30370b324cSopenharmony_ci UString newName = name; 31370b324cSopenharmony_ci newName.Replace(kUnixPathSepar, kOsPathSepar); 32370b324cSopenharmony_ci return newName; 33370b324cSopenharmony_ci #else 34370b324cSopenharmony_ci return name; 35370b324cSopenharmony_ci #endif 36370b324cSopenharmony_ci} 37370b324cSopenharmony_ci 38370b324cSopenharmony_ci 39370b324cSopenharmony_ciUString GetOsPath_Remove_TailSlash(const UString &name) 40370b324cSopenharmony_ci{ 41370b324cSopenharmony_ci if (name.IsEmpty()) 42370b324cSopenharmony_ci return UString(); 43370b324cSopenharmony_ci UString newName = GetOsPath(name); 44370b324cSopenharmony_ci if (newName.Back() == kOsPathSepar) 45370b324cSopenharmony_ci newName.DeleteBack(); 46370b324cSopenharmony_ci return newName; 47370b324cSopenharmony_ci} 48370b324cSopenharmony_ci 49370b324cSopenharmony_ci 50370b324cSopenharmony_civoid ReplaceToOsSlashes_Remove_TailSlash(UString &name, bool 51370b324cSopenharmony_ci #if WCHAR_PATH_SEPARATOR != L'/' 52370b324cSopenharmony_ci useBackslashReplacement 53370b324cSopenharmony_ci #endif 54370b324cSopenharmony_ci ) 55370b324cSopenharmony_ci{ 56370b324cSopenharmony_ci if (name.IsEmpty()) 57370b324cSopenharmony_ci return; 58370b324cSopenharmony_ci 59370b324cSopenharmony_ci #if WCHAR_PATH_SEPARATOR != L'/' 60370b324cSopenharmony_ci { 61370b324cSopenharmony_ci // name.Replace(kUnixPathSepar, kOsPathSepar); 62370b324cSopenharmony_ci const unsigned len = name.Len(); 63370b324cSopenharmony_ci for (unsigned i = 0; i < len; i++) 64370b324cSopenharmony_ci { 65370b324cSopenharmony_ci wchar_t c = name[i]; 66370b324cSopenharmony_ci if (c == L'/') 67370b324cSopenharmony_ci c = WCHAR_PATH_SEPARATOR; 68370b324cSopenharmony_ci else if (useBackslashReplacement && c == L'\\') 69370b324cSopenharmony_ci c = WCHAR_IN_FILE_NAME_BACKSLASH_REPLACEMENT; // WSL scheme 70370b324cSopenharmony_ci else 71370b324cSopenharmony_ci continue; 72370b324cSopenharmony_ci name.ReplaceOneCharAtPos(i, c); 73370b324cSopenharmony_ci } 74370b324cSopenharmony_ci } 75370b324cSopenharmony_ci #endif 76370b324cSopenharmony_ci 77370b324cSopenharmony_ci if (name.Back() == kOsPathSepar) 78370b324cSopenharmony_ci name.DeleteBack(); 79370b324cSopenharmony_ci} 80370b324cSopenharmony_ci 81370b324cSopenharmony_ci 82370b324cSopenharmony_civoid NormalizeSlashes_in_FileName_for_OsPath(wchar_t *name, unsigned len) 83370b324cSopenharmony_ci{ 84370b324cSopenharmony_ci for (unsigned i = 0; i < len; i++) 85370b324cSopenharmony_ci { 86370b324cSopenharmony_ci wchar_t c = name[i]; 87370b324cSopenharmony_ci if (c == L'/') 88370b324cSopenharmony_ci c = L'_'; 89370b324cSopenharmony_ci #if WCHAR_PATH_SEPARATOR != L'/' 90370b324cSopenharmony_ci else if (c == L'\\') 91370b324cSopenharmony_ci c = WCHAR_IN_FILE_NAME_BACKSLASH_REPLACEMENT; // WSL scheme 92370b324cSopenharmony_ci #endif 93370b324cSopenharmony_ci else 94370b324cSopenharmony_ci continue; 95370b324cSopenharmony_ci name[i] = c; 96370b324cSopenharmony_ci } 97370b324cSopenharmony_ci} 98370b324cSopenharmony_ci 99370b324cSopenharmony_civoid NormalizeSlashes_in_FileName_for_OsPath(UString &name) 100370b324cSopenharmony_ci{ 101370b324cSopenharmony_ci NormalizeSlashes_in_FileName_for_OsPath(name.GetBuf(), name.Len()); 102370b324cSopenharmony_ci} 103370b324cSopenharmony_ci 104370b324cSopenharmony_ci 105370b324cSopenharmony_cibool HasTailSlash(const AString &name, UINT 106370b324cSopenharmony_ci #if defined(_WIN32) && !defined(UNDER_CE) 107370b324cSopenharmony_ci codePage 108370b324cSopenharmony_ci #endif 109370b324cSopenharmony_ci ) 110370b324cSopenharmony_ci{ 111370b324cSopenharmony_ci if (name.IsEmpty()) 112370b324cSopenharmony_ci return false; 113370b324cSopenharmony_ci char c; 114370b324cSopenharmony_ci #if defined(_WIN32) && !defined(UNDER_CE) 115370b324cSopenharmony_ci if (codePage != CP_UTF8) 116370b324cSopenharmony_ci c = *CharPrevExA((WORD)codePage, name, name.Ptr(name.Len()), 0); 117370b324cSopenharmony_ci else 118370b324cSopenharmony_ci #endif 119370b324cSopenharmony_ci { 120370b324cSopenharmony_ci c = name.Back(); 121370b324cSopenharmony_ci } 122370b324cSopenharmony_ci return (c == '/'); 123370b324cSopenharmony_ci} 124370b324cSopenharmony_ci 125370b324cSopenharmony_ci 126370b324cSopenharmony_ci#ifndef _WIN32 127370b324cSopenharmony_ciUString WinPathToOsPath(const UString &name) 128370b324cSopenharmony_ci{ 129370b324cSopenharmony_ci UString newName = name; 130370b324cSopenharmony_ci newName.Replace(L'\\', WCHAR_PATH_SEPARATOR); 131370b324cSopenharmony_ci return newName; 132370b324cSopenharmony_ci} 133370b324cSopenharmony_ci#endif 134370b324cSopenharmony_ci 135370b324cSopenharmony_ci}} 136