📄 filename.cpp
字号:
if ( ! ( nLength = GetFullPathNameW ( szFileName, 0, NULL, & szFilePart ) ) ) goto CAUGHT_ERROR ;
nLength += 8 ; // GetFullPathName() 偼僼傽僀儖柤偑侾帤偺偲偒梋桾偑昁梫
if ( ! ( szResult = (wchar_t*) malloc ( nLength * sizeof(wchar_t) ) ) ) goto CAUGHT_ERROR ;
if ( ! ( nResult = GetFullPathNameW ( szFileName, nLength, szResult, & szFilePart ) ) || nResult >= nLength ) goto CAUGHT_ERROR ;
return szResult ;
CAUGHT_ERROR :
free ( szResult ) ;
return NULL ;
}
////////////////////////////////////////////
// 僼儖僷僗柤偐専嵏 //
////////////////////////////////////////////
// 僼儖僷僗柤乮UNC傕娷傓乯偐専嵏乮ANSI斉乯
int IsFullPathNameA ( const char *szPathName ) {
if ( isascii ( *szPathName ) && isalpha ( *szPathName ) ) {
if ( *( szPathName + 1 ) == ':' && IsPathSeparator ( *( szPathName + 2 ) ) ) return 1 ;
}
if ( *szPathName == '\\' && *( szPathName + 1 ) == '\\' ) return 1 ;
return 0 ;
}
// 僼儖僷僗柤乮UNC傕娷傓乯偐専嵏乮UNICODE斉乯
int IsFullPathNameW ( const wchar_t *szPathName ) {
if ( isascii ( *szPathName ) && isalpha ( *szPathName ) ) {
if ( *( szPathName + 1 ) == ':' && IsPathSeparator ( *( szPathName + 2 ) ) ) return 1 ;
}
if ( *szPathName == '\\' && *( szPathName + 1 ) == '\\' ) return 1 ;
return 0 ;
}
////////////////////////////////////////////
// UNC偐専嵏 //
////////////////////////////////////////////
// UNC偐専嵏乮ANSI斉乯
int IsFileNameUncA ( const char *szPathName ) {
if ( *szPathName == '\\' && *( szPathName + 1 ) == '\\' ) return 1 ;
return 0 ;
}
// UNC偐専嵏乮UNICODE斉乯
int IsFileNameUncW ( const wchar_t *szPathName ) {
if ( *szPathName == '\\' && *( szPathName + 1 ) == '\\' ) return 1 ;
return 0 ;
}
////////////////////////////////////////////
// URL偐専嵏 //
////////////////////////////////////////////
// URL偐専嵏乮ANSI斉乯
int IsFileNameUrlA ( const char *szPathName ) {
const char *szString = szPathName ;
while ( isascii ( *szString ) && isalpha ( *szString ) ) szString ++ ;
if ( *szString == ':' && IsPathSeparator ( *( szString + 1 ) ) && szString - szPathName > 1 ) return 1 ;
return 0 ;
}
// URL偐専嵏乮UNICODE斉乯
int IsFileNameUrlW ( const wchar_t *szPathName ) {
const wchar_t *szString = szPathName ;
while ( isascii ( *szString ) && isalpha ( *szString ) ) szString ++ ;
if ( *szString == ':' && IsPathSeparator ( *( szString + 1 ) ) && szString - szPathName > 1 ) return 1 ;
return 0 ;
}
////////////////////////////////////////////
// 僷僗柤枛旜傪挷惍 //
////////////////////////////////////////////
static inline int strheadcmp ( const char *string, const char *head ) ;
static inline int wcsheadcmp ( const wchar_t *string, const wchar_t *head ) ;
// 僷僗柤偺枛旜偺 \ 傪挷惍乮ANSI斉乯
// 傕偟 IsAddSeparator 偑 REMOVE_SEPARATOR_ALWAYS 側傜偽 \ 傪彍嫀
// 傕偟 IsAddSeparator 偑 ADD_SEPARATOR_ALWAYS 側傜偽 \ 傪晅壛
// 傕偟 IsAddSeparator 偑 ADD_SEPARATOR_ONLY_ROOT 側傜偽儖乕僩僨傿儗僋僩儕偺傒 \ 傪晅壛
// 僷僗柤傪曉偡
char *SetPathSeparatorA ( char *szPathName, int IsAddSeparator ) {
for ( char *szString = szPathName ; *szString ; szString ++ ) {
if ( IsPathSeparator ( *szString ) && *( szString + 1 ) == 0 ) {
*szString = 0 ;
break ;
}
if ( ismbblead ( (unsigned char) *szString ) && *( szString + 1 ) ) szString ++ ;
}
if ( IsAddSeparator == ADD_SEPARATOR_ONLY_ROOT ) {
char *szStart = szPathName ;
static const char szLongPathHead [] = "\\\\?\\" ; // for "\\?\C:\<path>", "\\?\UNC\<server>\<share>"
if ( ! strheadcmp ( szStart, szLongPathHead ) ) szStart += strlen ( szLongPathHead ) ;
if ( ! *szStart || isascii ( *szStart ) && isalpha ( *szStart ) && *( szStart + 1 ) == ':' && *( szStart + 2 ) == 0 ) IsAddSeparator = ADD_SEPARATOR_ALWAYS ;
}
if ( IsAddSeparator == ADD_SEPARATOR_ALWAYS ) strcat ( szPathName, "\\" ) ;
return szPathName ;
}
// 僷僗柤偺枛旜偺 \ 傪挷惍乮UNICODE斉乯
// 傕偟 IsAddSeparator 偑 REMOVE_SEPARATOR_ALWAYS 側傜偽 \ 傪彍嫀
// 傕偟 IsAddSeparator 偑 ADD_SEPARATOR_ALWAYS 側傜偽 \ 傪晅壛
// 傕偟 IsAddSeparator 偑 ADD_SEPARATOR_ONLY_ROOT 側傜偽儖乕僩僨傿儗僋僩儕偺傒 \ 傪晅壛
// 僷僗柤傪曉偡
wchar_t *SetPathSeparatorW ( wchar_t *szPathName, int IsAddSeparator ) {
for ( wchar_t *szString = szPathName ; *szString ; szString ++ ) {
if ( IsPathSeparator ( *szString ) && *( szString + 1 ) == 0 ) {
*szString = 0 ;
break ;
}
}
if ( IsAddSeparator == ADD_SEPARATOR_ONLY_ROOT ) {
wchar_t *szStart = szPathName ;
static const wchar_t szLongPathHead [] = L"\\\\?\\" ; // for "\\?\C:\<path>", "\\?\UNC\<server>\<share>"
if ( ! wcsheadcmp ( szStart, szLongPathHead ) ) szStart += wcslen ( szLongPathHead ) ;
if ( ! *szStart || isascii ( *szStart ) && isalpha ( *szStart ) && *( szStart + 1 ) == ':' && *( szStart + 2 ) == 0 ) IsAddSeparator = ADD_SEPARATOR_ALWAYS ;
}
if ( IsAddSeparator == ADD_SEPARATOR_ALWAYS ) wcscat ( szPathName, L"\\" ) ;
return szPathName ;
}
// 暥帤楍偺愭摢傪斾妑乮ANSI斉乯
static inline int strheadcmp ( const char *string, const char *head ) {
return strncmp ( string, head, strlen ( head ) ) ;
}
// 暥帤楍偺愭摢傪斾妑乮UNICODE斉乯
static inline int wcsheadcmp ( const wchar_t *string, const wchar_t *head ) {
return wcsncmp ( string, head, wcslen ( head ) ) ;
}
////////////////////////////////////////////
// 擇廳堷梡晞傪晅壛 //
////////////////////////////////////////////
// 嬻敀偑偁傟偽擇廳堷梡晞偱埻傓乮ANSI斉乯
char *QuoteFileNameA ( char *szPathName ) {
if ( strchr ( szPathName, 0x20 ) || strchr ( szPathName, '\t' ) ) {
size_t nLength = strlen ( szPathName ) ;
memmove ( szPathName + 1, szPathName, ( nLength + 1 ) * sizeof(char) ) ;
*szPathName = '\"' ;
*( szPathName + nLength + 1 ) = '\"' ;
*( szPathName + nLength + 2 ) = 0 ;
}
return szPathName ;
}
// 嬻敀偑偁傟偽擇廳堷梡晞偱埻傓乮UNICODE斉乯
wchar_t *QuoteFileNameW ( wchar_t *szPathName ) {
if ( wcschr ( szPathName, 0x20 ) || wcschr ( szPathName, '\t' ) ) {
size_t nLength = wcslen ( szPathName ) ;
memmove ( szPathName + 1, szPathName, ( nLength + 1 ) * sizeof(wchar_t) ) ;
*szPathName = '\"' ;
*( szPathName + nLength + 1 ) = '\"' ;
*( szPathName + nLength + 2 ) = 0 ;
}
return szPathName ;
}
////////////////////////////////////////////
// 擇廳堷梡晞傪嶍彍 //
////////////////////////////////////////////
// 擇廳堷梡晞偵埻傑傟偨僼傽僀儖柤偐傜擇廳堷梡晞傪庢傝彍偔乮ANSI斉乯
char *UnquoteFileNameA ( char *szPathName ) {
if ( *szPathName == '\"' ) {
size_t nLength = strlen ( szPathName + 1 ) ;
memmove ( szPathName, szPathName + 1, ( nLength + 1 ) * sizeof(char) ) ;
if ( *( szPathName + nLength - 1 ) == '\"' ) *( szPathName + nLength - 1 ) = 0 ;
}
return szPathName ;
}
// 擇廳堷梡晞偵埻傑傟偨僼傽僀儖柤偐傜擇廳堷梡晞傪庢傝彍偔乮UNICODE斉乯
wchar_t *UnquoteFileNameW ( wchar_t *szPathName ) {
if ( *szPathName == '\"' ) {
size_t nLength = wcslen ( szPathName + 1 ) ;
memmove ( szPathName, szPathName + 1, ( nLength + 1 ) * sizeof(wchar_t) ) ;
if ( *( szPathName + nLength - 1 ) == '\"' ) *( szPathName + nLength - 1 ) = 0 ;
}
return szPathName ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -