mirror of
https://github.com/reactos/reactos.git
synced 2025-08-06 15:03:00 +00:00
JIRA issue: CORE-19278 - Simplify formatString handling. - Don't use magic number. - Commonize some code.
This commit is contained in:
parent
cfcc8d85b2
commit
88d9285bd0
1 changed files with 42 additions and 72 deletions
|
@ -671,7 +671,7 @@ static BOOL PathMakeUniqueNameW(
|
||||||
_Out_ PWSTR pszUniqueName,
|
_Out_ PWSTR pszUniqueName,
|
||||||
_In_ UINT cchMax,
|
_In_ UINT cchMax,
|
||||||
_In_ PCWSTR pszTemplate,
|
_In_ PCWSTR pszTemplate,
|
||||||
_In_opt_ PCWSTR pszLongPlate,
|
_In_opt_ PCWSTR pszLongPlate, /* Long template */
|
||||||
_In_opt_ PCWSTR pszDir)
|
_In_opt_ PCWSTR pszDir)
|
||||||
{
|
{
|
||||||
TRACE("%p %u %s %s %s\n",
|
TRACE("%p %u %s %s %s\n",
|
||||||
|
@ -683,7 +683,7 @@ static BOOL PathMakeUniqueNameW(
|
||||||
|
|
||||||
pszUniqueName[0] = UNICODE_NULL;
|
pszUniqueName[0] = UNICODE_NULL;
|
||||||
|
|
||||||
LPWSTR pszDest = pszUniqueName;
|
PWSTR pszDest = pszUniqueName;
|
||||||
UINT dirLength = 0;
|
UINT dirLength = 0;
|
||||||
if (pszDir)
|
if (pszDir)
|
||||||
{
|
{
|
||||||
|
@ -698,6 +698,8 @@ static BOOL PathMakeUniqueNameW(
|
||||||
}
|
}
|
||||||
|
|
||||||
PCWSTR pszTitle = pszLongPlate ? pszLongPlate : pszTemplate;
|
PCWSTR pszTitle = pszLongPlate ? pszLongPlate : pszTemplate;
|
||||||
|
PCWSTR pchDotExt, formatString = L"%d";
|
||||||
|
INT maxCount, cchTitle;
|
||||||
|
|
||||||
if ( !pszTitle
|
if ( !pszTitle
|
||||||
|| !IsLFNDriveW(pszDir)
|
|| !IsLFNDriveW(pszDir)
|
||||||
|
@ -709,15 +711,9 @@ static BOOL PathMakeUniqueNameW(
|
||||||
if (!pszTemplate)
|
if (!pszTemplate)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
PCWSTR pszSrc = pszTemplate;
|
pchDotExt = PathFindExtensionW(pszTemplate);
|
||||||
PCWSTR pchDotExt = PathFindExtensionW(pszTemplate);
|
|
||||||
INT extLength = lstrlenW(pchDotExt);
|
|
||||||
|
|
||||||
WCHAR formatString[MAX_PATH];
|
cchTitle = pchDotExt - pszTemplate;
|
||||||
if (StringCchCopyW(formatString, _countof(formatString), L"%d") != S_OK)
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
INT cchTitle = pchDotExt - pszTemplate;
|
|
||||||
if (cchTitle > 1)
|
if (cchTitle > 1)
|
||||||
{
|
{
|
||||||
PCWSTR pch = pchDotExt - 1;
|
PCWSTR pch = pchDotExt - 1;
|
||||||
|
@ -728,46 +724,26 @@ static BOOL PathMakeUniqueNameW(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cchTitle > 7)
|
#define MSDOS_8DOT3_FILENAME_TITLE_LEN 8
|
||||||
cchTitle = 7;
|
if (cchTitle > MSDOS_8DOT3_FILENAME_TITLE_LEN - 1)
|
||||||
|
cchTitle = MSDOS_8DOT3_FILENAME_TITLE_LEN - 1;
|
||||||
|
|
||||||
while (extLength + cchTitle + dirLength + 1 > (cchMax - 1) && cchTitle > 1)
|
INT extLength = lstrlenW(pchDotExt);
|
||||||
|
while (cchTitle > 1 && (dirLength + cchTitle + extLength + 1 > (cchMax - 1)))
|
||||||
--cchTitle;
|
--cchTitle;
|
||||||
|
|
||||||
INT maxCount;
|
if (cchTitle <= 0)
|
||||||
if (cchTitle >= 1)
|
|
||||||
maxCount = (cchTitle != 1) ? 100 : 10;
|
|
||||||
else
|
|
||||||
maxCount = 1;
|
maxCount = 1;
|
||||||
|
else if (cchTitle == 1)
|
||||||
|
maxCount = 10;
|
||||||
|
else
|
||||||
|
maxCount = 100;
|
||||||
|
|
||||||
if (StringCchCopyNW(pszDest, cchMax - dirLength, pszSrc, cchTitle) != S_OK)
|
pszTitle = pszTemplate;
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
LPWSTR pchTitle = pszDest + cchTitle;
|
|
||||||
INT ich;
|
|
||||||
for (ich = 1; ich < maxCount; ++ich)
|
|
||||||
{
|
|
||||||
WCHAR tempName[MAX_PATH];
|
|
||||||
if (StringCchPrintfW(tempName, _countof(tempName), formatString, ich) != S_OK ||
|
|
||||||
StringCchCatW(tempName, _countof(tempName), pchDotExt) != S_OK)
|
|
||||||
{
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringCchCopyW(pchTitle, cchMax - (pchTitle - pszUniqueName), tempName) != S_OK)
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
if (!PathFileExistsW(pszUniqueName))
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
PCWSTR openParen = StrChrW(pszTitle, L'(');
|
PCWSTR openParen = StrChrW(pszTitle, L'(');
|
||||||
PCWSTR pchDotExt = NULL;
|
|
||||||
INT cchTitle = 0;
|
|
||||||
WCHAR formatString[MAX_PATH];
|
|
||||||
|
|
||||||
if (openParen)
|
if (openParen)
|
||||||
{
|
{
|
||||||
while (openParen)
|
while (openParen)
|
||||||
|
@ -786,57 +762,51 @@ static BOOL PathMakeUniqueNameW(
|
||||||
{
|
{
|
||||||
pchDotExt = openParen + 1;
|
pchDotExt = openParen + 1;
|
||||||
cchTitle = pchDotExt - pszTitle;
|
cchTitle = pchDotExt - pszTitle;
|
||||||
if (StringCchCopyW(formatString, _countof(formatString), L"%d") != S_OK)
|
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pchDotExt = PathFindExtensionW(pszTitle);
|
pchDotExt = PathFindExtensionW(pszTitle);
|
||||||
cchTitle = pchDotExt - pszTitle;
|
cchTitle = pchDotExt - pszTitle;
|
||||||
if (StringCchCopyW(formatString, _countof(formatString), L" (%d)") != S_OK)
|
formatString = L" (%d)";
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pchDotExt = PathFindExtensionW(pszTitle);
|
pchDotExt = PathFindExtensionW(pszTitle);
|
||||||
cchTitle = pchDotExt - pszTitle;
|
cchTitle = pchDotExt - pszTitle;
|
||||||
if (StringCchCopyW(formatString, _countof(formatString), L" (%d)") != S_OK)
|
formatString = L" (%d)";
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
INT maxCount = 1;
|
INT remainingChars = cchMax - (dirLength + cchTitle + (lstrlenW(formatString) - 2));
|
||||||
INT remainingChars = cchMax - cchTitle - dirLength - lstrlenW(formatString) + 2;
|
if (remainingChars <= 0)
|
||||||
|
maxCount = 1;
|
||||||
if (remainingChars == 1)
|
else if (remainingChars == 1)
|
||||||
maxCount = 10;
|
maxCount = 10;
|
||||||
else if (remainingChars == 2)
|
else if (remainingChars == 2)
|
||||||
maxCount = 100;
|
maxCount = 100;
|
||||||
else if (remainingChars > 0)
|
|
||||||
maxCount = 1000;
|
|
||||||
else
|
else
|
||||||
maxCount = 1;
|
maxCount = 1000;
|
||||||
|
}
|
||||||
|
|
||||||
if (StringCchCopyNW(pszDest, cchMax - dirLength, pszTitle, cchTitle) != S_OK)
|
if (StringCchCopyNW(pszDest, cchMax - dirLength, pszTitle, cchTitle) != S_OK)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
PWSTR pchTitle = pszDest + cchTitle;
|
||||||
|
INT count;
|
||||||
|
for (count = 1; count < maxCount; ++count)
|
||||||
|
{
|
||||||
|
WCHAR tempName[MAX_PATH];
|
||||||
|
if (StringCchPrintfW(tempName, _countof(tempName), formatString, count) != S_OK ||
|
||||||
|
StringCchCatW(tempName, _countof(tempName), pchDotExt) != S_OK)
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringCchCopyW(pchTitle, cchMax - (pchTitle - pszUniqueName), tempName) != S_OK)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
LPWSTR pchTitle = pszDest + cchTitle;
|
if (!PathFileExistsW(pszUniqueName))
|
||||||
INT ich;
|
return TRUE;
|
||||||
for (ich = 1; ich < maxCount; ++ich)
|
|
||||||
{
|
|
||||||
WCHAR tempName[MAX_PATH];
|
|
||||||
if (StringCchPrintfW(tempName, _countof(tempName), formatString, ich) != S_OK ||
|
|
||||||
StringCchCatW(tempName, _countof(tempName), pchDotExt) != S_OK)
|
|
||||||
{
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringCchCopyW(pchTitle, cchMax - (pchTitle - pszUniqueName), tempName) != S_OK)
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
if (!PathFileExistsW(pszUniqueName))
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pszUniqueName[0] = UNICODE_NULL;
|
pszUniqueName[0] = UNICODE_NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue