Bug 12911 - use relative file descriptors. This adds the needed methods to nsILocalFile. r=dougt/sr=alecf/a=asa/adt=jaime

git-svn-id: svn://10.0.0.236/trunk@118544 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
ccarlen%netscape.com
2002-04-09 14:12:24 +00:00
parent 445a26c47c
commit b01ade9e83
5 changed files with 227 additions and 16 deletions

View File

@@ -42,7 +42,7 @@
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsXPIDLString.h"
#include "nsReadableUtils.h"
void NS_StartupLocalFile()
@@ -62,6 +62,20 @@ void NS_ShutdownLocalFile()
#endif
}
#if !defined(XP_MAC)
NS_IMETHODIMP
nsLocalFile::InitWithFile(nsILocalFile *aFile)
{
NS_ENSURE_ARG(aFile);
nsXPIDLCString path;
aFile->GetPath(getter_Copies(path));
if (!path)
return NS_ERROR_FAILURE;
return InitWithPath(path.get());
}
#endif
// should work on Macintosh, Unix, and Win32.
#define kMaxFilenameLength 31
@@ -114,7 +128,140 @@ nsLocalFile::CreateUnique(PRUint32 type, PRUint32 attributes)
return NS_ERROR_FILE_TOO_BIG;
}
#if defined(XP_MAC)
const PRUnichar kPathSeparatorChar = ':';
#elif defined(XP_WIN) || defined(XP_OS2)
const PRUnichar kPathSeparatorChar = '\\';
#elif defined(XP_UNIX) || defined(XP_BEOS)
const PRUnichar kPathSeparatorChar = '/';
#else
#error Need to define file path separator for your platform
#endif
#if defined(XP_MAC)
const char* kSlashStr = "/";
const char* kESCSlashStr = "%2F";
#endif
static PRInt32 SplitPath(PRUnichar *path, PRUnichar **nodeArray, PRInt32 arrayLen)
{
if (*path == 0)
return 0;
PRUnichar **nodePtr = nodeArray;
if (*path == kPathSeparatorChar)
path++;
*nodePtr++ = path;
for (PRUnichar *cp = path; *cp != 0; cp++) {
if (*cp == kPathSeparatorChar) {
*cp++ = 0;
if (*cp != 0) {
if (nodePtr - nodeArray >= arrayLen)
return -1;
*nodePtr++ = cp;
}
}
}
return nodePtr - nodeArray;
}
NS_IMETHODIMP
nsLocalFile::GetRelativeDescriptor(nsILocalFile *fromFile, nsACString& _retval)
{
const PRInt32 kMaxNodesInPath = 32;
nsresult rv;
_retval.Truncate(0);
PRUnichar *thisPath = nsnull, *fromPath = nsnull;
PRUnichar *thisNodes[kMaxNodesInPath], *fromNodes[kMaxNodesInPath];
PRInt32 thisNodeCnt, fromNodeCnt, nodeIndex;
rv = GetUnicodePath(&thisPath);
if (NS_FAILED(rv))
return rv;
rv = fromFile->GetUnicodePath(&fromPath);
if (NS_FAILED(rv)) {
nsMemory::Free(thisPath);
return rv;
}
thisNodeCnt = SplitPath(thisPath, thisNodes, kMaxNodesInPath);
fromNodeCnt = SplitPath(fromPath, fromNodes, kMaxNodesInPath);
if (thisNodeCnt < 0 || fromNodeCnt < 0) {
nsMemory::Free(thisPath);
nsMemory::Free(fromPath);
return NS_ERROR_FAILURE;
}
for (nodeIndex = 0; nodeIndex < thisNodeCnt && nodeIndex < fromNodeCnt; nodeIndex++) {
if (!(nsDependentString(thisNodes[nodeIndex])).Equals(nsDependentString(fromNodes[nodeIndex])))
break;
}
PRInt32 branchIndex = nodeIndex;
for (nodeIndex = branchIndex; nodeIndex < fromNodeCnt; nodeIndex++)
_retval.Append("../");
for (nodeIndex = branchIndex; nodeIndex < thisNodeCnt; nodeIndex++) {
NS_ConvertUCS2toUTF8 nodeStr(thisNodes[nodeIndex]);
#ifdef XP_MAC
nodeStr.ReplaceSubstring(kSlashStr, kESCSlashStr);
#endif
_retval.Append(nodeStr.get());
if (nodeIndex + 1 < thisNodeCnt)
_retval.Append('/');
}
nsMemory::Free(thisPath);
nsMemory::Free(fromPath);
return NS_OK;
}
NS_IMETHODIMP
nsLocalFile::SetRelativeDescriptor(nsILocalFile *fromFile, const nsACString& relativeDesc)
{
NS_NAMED_LITERAL_CSTRING(kParentDirStr, "../");
nsCOMPtr<nsIFile> targetFile;
nsresult rv = fromFile->Clone(getter_AddRefs(targetFile));
if (NS_FAILED(rv))
return rv;
nsCString::const_iterator strBegin, strEnd;
relativeDesc.BeginReading(strBegin);
relativeDesc.EndReading(strEnd);
nsCString::const_iterator nodeBegin(strBegin), nodeEnd(strEnd);
nsCString::const_iterator pos(strBegin);
nsCOMPtr<nsIFile> parentDir;
while (FindInReadable(kParentDirStr, nodeBegin, nodeEnd)) {
rv = targetFile->GetParent(getter_AddRefs(parentDir));
if (NS_FAILED(rv))
return rv;
targetFile = parentDir;
nodeBegin = nodeEnd;
pos = nodeEnd;
nodeEnd = strEnd;
}
nodeBegin = nodeEnd = pos;
while (nodeEnd != strEnd) {
FindCharInReadable('/', nodeEnd, strEnd);
nsCAutoString nodeString(Substring(nodeBegin, nodeEnd));
#ifdef XP_MAC
nodeString.ReplaceSubstring(kESCSlashStr, kSlashStr);
#endif
targetFile->AppendUnicode((NS_ConvertUTF8toUCS2(nodeString)).get());
if (nodeEnd != strEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
++nodeEnd;
nodeBegin = nodeEnd;
}
nsCOMPtr<nsILocalFile> targetLocalFile(do_QueryInterface(targetFile));
return InitWithFile(targetLocalFile);
}