Bug #33768 --> add MakeUnqiue to nsIFile. this is actually dougt's code.

r=mscott


git-svn-id: svn://10.0.0.236/trunk@72738 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
mscott%netscape.com
2000-06-21 06:32:45 +00:00
parent 61621593ad
commit d92552bbfe
3 changed files with 74 additions and 24 deletions

View File

@@ -367,3 +367,49 @@ nsresult nsFileSpec::Execute(const nsString& args) const
SET_UCS( Execute , args.GetUnicode());
}
// should work on Macintosh, Unix, and Win32.
#define kMaxFilenameLength 31
NS_IMETHODIMP
nsLocalFile::MakeUnique(const char* suggestedName)
{
PRBool exists;
nsresult rv = Exists(&exists);
if (NS_FAILED(rv)) return rv;
if (!exists) return NS_OK;
char* leafName;
rv = GetLeafName(&leafName);
if (NS_FAILED(rv)) return rv;
char* lastDot = strrchr(leafName, '.');
char* suffix = "";
if (lastDot)
{
suffix = nsCRT::strdup(lastDot); // include '.'
*lastDot = '\0'; // strip suffix and dot.
}
// 27 should work on Macintosh, Unix, and Win32.
const int maxRootLength = 27 - nsCRT::strlen(suffix) - 1;
if ((int)nsCRT::strlen(leafName) > (int)maxRootLength)
leafName[maxRootLength] = '\0';
for (short indx = 1; indx < 10000 && exists; indx++)
{
// start with "Picture-1.jpg" after "Picture.jpg" exists
char newName[kMaxFilenameLength + 1];
sprintf(newName, "%s-%d%s", leafName, indx, suffix);
SetLeafName(newName);
rv = Exists(&exists);
if (NS_FAILED(rv)) return rv;
}
return NS_OK;
}