Back to index

lightning-sunbird  0.9+nobinonly
Functions | Variables
MacFileHelpers Namespace Reference

Functions

void PLstrcpy (Str255 dst, ConstStr255Param src)
void PLstrcpy (Str255 dst, const char *src, int inMaxLen=255)
void PLstrncpy (Str255 dst, const char *src, int inMaxLen)
void SwapSlashColon (char *s)
OSErr FSSpecFromUnixPath (const char *unixPath, FSSpec &ioSpec, Boolean hexDecode, Boolean resolveAlias, Boolean allowPartial=false, Boolean createDirs=false)
char * MacPathFromUnixPath (const char *unixPath, Boolean hexDecode)
char * EncodeMacPath (char *inPath, Boolean prependSlash, Boolean doEscape)
OSErr FSSpecFromPathname (const char *inPathNamePtr, FSSpec &ioSpec, Boolean inCreateDirs)
char * PathNameFromFSSpec (const FSSpec &inSpec)
OSErr CreateFolderInFolder (short refNum, long dirID, ConstStr255Param folderName, short &outRefNum, long &outDirID)
void EnsureAliasHaven ()
void SetNoResolve (Boolean inResolve)
PRBool IsAliasSafe (const FSSpec &inSpec)
OSErr MakeAliasSafe (FSSpec &inOutSpec)
OSErr ResolveAliasFile (FSSpec &inOutSpec, Boolean &wasAliased)

Variables

Boolean sNoResolve = false
long sAliasHavenDirID = 0
short sAliasHavenVRefNum = 0

Function Documentation

OSErr MacFileHelpers::CreateFolderInFolder ( short  refNum,
long  dirID,
ConstStr255Param  folderName,
short &  outRefNum,
long outDirID 
)

Definition at line 421 of file nsFileSpecMac.cpp.

{
       HFileParam hpb;
       hpb.ioVRefNum = refNum;
       hpb.ioDirID = dirID;
       hpb.ioNamePtr = (StringPtr)&folderName;

       OSErr err = PBDirCreateSync((HParmBlkPtr)&hpb);
       if (err == noErr)
       {
              outRefNum = hpb.ioVRefNum;
              outDirID = hpb.ioDirID;
       }
       else
       {
              outRefNum = 0;
              outDirID = 0;
       }
       return err;
} // MacFileHelpers::CreateFolderInFolder

Here is the caller graph for this function:

char * MacFileHelpers::EncodeMacPath ( char *  inPath,
Boolean  prependSlash,
Boolean  doEscape 
)

Definition at line 159 of file nsFileSpecMac.cpp.

{
       if (inPath == nsnull)
              return nsnull;
       int pathSize = strlen(inPath);
       
       // XP code sometimes chokes if there's a final slash in the unix path.
       // Since correct mac paths to folders and volumes will end in ':', strip this
       // first.
       char* c = inPath + pathSize - 1;
       if (*c == ':')
       {
              *c = 0;
              pathSize--;
       }

       char * newPath = nsnull;
       char * finalPath = nsnull;
       
       if (prependSlash)
       {
              newPath = new char[pathSize + 2];
              newPath[0] = ':';     // It will be converted to '/'
              memcpy(&newPath[1], inPath, pathSize + 1);
       }
       else
       {
              newPath = new char[pathSize + 1];
              strcpy(newPath, inPath);
       }
       if (newPath)
       {
              SwapSlashColon( newPath );
              if (doEscape)
              {
                     finalPath = nsEscape(newPath, url_Path);
                     delete [] newPath;
              }
              else
                     finalPath = newPath;
       }
       delete [] inPath;
       return finalPath;
} // MacFileHelpers::EncodeMacPath

Here is the call graph for this function:

Definition at line 451 of file nsFileSpecMac.cpp.

{
       // Alias Haven is a directory in which we never resolve aliases.
       if (sAliasHavenVRefNum != 0)
              return;

       
       FSSpec temp;
       if (FindFolder(0, kTemporaryFolderType, true, & temp.vRefNum, &temp.parID) == noErr)
       {
              CreateFolderInFolder(
                     temp.vRefNum,                        // Parent directory/volume
                     temp.parID,
                     kAliasHavenFolderName,               // Name of the new folder
                     sAliasHavenVRefNum,            // Volume of the created folder
                     sAliasHavenDirID);            
       }
} // MacFileHelpers::EnsureAliasHaven

Here is the call graph for this function:

Here is the caller graph for this function:

OSErr MacFileHelpers::FSSpecFromPathname ( const char *  inPathNamePtr,
FSSpec ioSpec,
Boolean  inCreateDirs 
)

Definition at line 319 of file nsFileSpecMac.cpp.

{
       OSErr err;
       // Simplify this routine to use FSMakeFSSpec if length < 255. Otherwise use the MoreFiles
       // routine FSpLocationFromFullPath, which allocates memory, to handle longer pathnames. 
       
       short inVRefNum = ioSpec.vRefNum;
       long inParID = ioSpec.parID;
       
       size_t inLength = strlen(inPathNamePtr);
       bool isRelative = (strchr(inPathNamePtr, ':') == 0 || *inPathNamePtr == ':');
#ifdef NS_DEBUG
       // Attempt to catch people sending unix paths in to routines that expect native ones.
       NS_ASSERTION(strchr(inPathNamePtr, '/') == 0,
                     "Possible unix path where native path is required");
#endif
       if (inLength < 255)
       {
              Str255 pascalpath;
              MacFileHelpers::PLstrcpy(pascalpath, inPathNamePtr);
              if (isRelative)
                     err = ::FSMakeFSSpec(inVRefNum, inParID, pascalpath, &ioSpec);
              else
                     err = ::FSMakeFSSpec(0, 0, pascalpath, &ioSpec);
       }
       else if (!isRelative)
              err = FSpLocationFromFullPath(inLength, inPathNamePtr, &ioSpec);
       else
              err = bdNamErr;

       if ((err == dirNFErr || err == bdNamErr) && inCreateDirs)
       {
              const char* path = inPathNamePtr;
              if (isRelative)
              {
                     ioSpec.vRefNum = inVRefNum;
                     ioSpec.parID = inParID;
              }
              else
              {
                     ioSpec.vRefNum = 0;
                     ioSpec.parID = 0;
              }
              do {
                     // Locate the colon that terminates the node.
                     // But if we've a partial path (starting with a colon), find the second one.
                     const char* nextColon = strchr(path + (*path == ':'), ':');
                     // Well, if there are no more colons, point to the end of the string.
                     if (!nextColon)
                            nextColon = path + strlen(path);

                     // Make a pascal string out of this node.  Include initial
                     // and final colon, if any!
                     Str255 ppath;
                     MacFileHelpers::PLstrncpy(ppath, path, nextColon - path + 1);
                     
                     // Use this string as a relative path using the directory created
                     // on the previous round (or directory 0,0 on the first round).
                     err = ::FSMakeFSSpec(ioSpec.vRefNum, ioSpec.parID, ppath, &ioSpec);

                     // If this was the leaf node, then we are done.
                     if (!*nextColon)
                            break;

                     // Since there's more to go, we have to get the directory ID, which becomes
                     // the parID for the next round.
                     if (err == noErr)
                     {
                            // The directory (or perhaps a file) exists. Find its dirID.
                            long dirID;
                            Boolean isDirectory;
                            err = ::FSpGetDirectoryID(&ioSpec, &dirID, &isDirectory);
                            if (!isDirectory)
                                   return dupFNErr; // oops! a file exists with that name.
                            if (err)
                                   return err;
                            ioSpec.parID = dirID;
                     }
                     else if (err == fnfErr)
                     {
                            // If we got "file not found", then
                            // we need to create a directory.
                            err = ::FSpDirCreate(&ioSpec, smCurrentScript, &ioSpec.parID);
                            // For some reason, this usually returns fnfErr, even though it works.
                            if (err == fnfErr)
                                   err = noErr;
                     }
                     if (err != noErr)
                            return err;
                     path = nextColon; // next round
              } while (1);
       }
       return err;
} // MacFileHelpers::FSSpecFromPathname

Here is the call graph for this function:

Here is the caller graph for this function:

OSErr MacFileHelpers::FSSpecFromUnixPath ( const char *  unixPath,
FSSpec ioSpec,
Boolean  hexDecode,
Boolean  resolveAlias,
Boolean  allowPartial = false,
Boolean  createDirs = false 
)

Definition at line 493 of file nsFileSpecMac.cpp.

{
       if (unixPath == nsnull)
              return badFidErr;
       char* macPath = MacPathFromUnixPath(unixPath, hexDecode);
       if (!macPath)
              return memFullErr;

       OSErr err = noErr;
       if (!allowPartial)
       {
              NS_ASSERTION(*unixPath == '/' /*full path*/, "Not a full Unix path!");
       }
       err = FSSpecFromPathname(macPath, ioSpec, createDirs);
       if (err == fnfErr)
              err = noErr;
       Boolean dummy;         
       if (err == noErr && resolveAlias)   // Added 
              err = MacFileHelpers::ResolveAliasFile(ioSpec, dummy);
       delete [] macPath;
       NS_ASSERTION(err==noErr||err==fnfErr||err==dirNFErr||err==nsvErr, "Not a path!");
       return err;
} // MacFileHelpers::FSSpecFromLocalUnixPath

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 472 of file nsFileSpecMac.cpp.

{
       return sNoResolve
              || (inSpec.parID == sAliasHavenDirID && inSpec.vRefNum == sAliasHavenVRefNum);
} // MacFileHelpers::IsAliasSafe

Here is the caller graph for this function:

char * MacFileHelpers::MacPathFromUnixPath ( const char *  unixPath,
Boolean  hexDecode 
)

Definition at line 247 of file nsFileSpecMac.cpp.

{
       // Relying on the fact that the unix path is always longer than the mac path:
       size_t len = strlen(unixPath);
       char* result = new char[len + 2]; // ... but allow for the initial colon in a partial name
       // REMEMBER: at the end we call SwapSlashColon, so bear that in mind when you see
       // this code using '/' as a separator in what is supposed to be a Macintosh path!
       if (result)
       {
              char* dst = result;
              const char* src = unixPath;
              if (*src == '/')                    // * full path
                     src++;
              else if (strchr(src, '/') && *src != '.')
              {
                     // * partial path, and not just a leaf name. The '.' test is there because
                     // the loop below will add sufficient colons in that case.
                     *dst++ = '/';
              }
              // Copy src to dst, but watch out for .. and .
              char c = '/';
              do
              {
                     char cprev = c; // remember the previous char (initially /)
                     c = *src++;
                     if (c == '.' && cprev == '/')
                     {
                            char* dstSaved = dst;
                            // Special cases: "." and "..". Convert to ':' and '::'
                            *dst++ = '/';  // . becomes :
                            c = *src++;
                            if (c == '.')
                            {
                                   *dst++ = '/'; // .. becomes ::
                                   c = *src++;
                            }
                            if (c == '/')
                            {
                                   // ../   becomes     :: so just skip the slash
                                   // ./    becomes     :  "  "          "   "      "
                                   src++;
                            }
                            else if (c)
                            {
                                   // Oh. A file called ".foo" or "..foo"
                                   // Back up and just do the normal thing.
                                   src -= (dst - dstSaved);
                                   dst = dstSaved;
                                   // Since c is not '/', we won't do this stuff on the
                                   // next iteration.
                            }
                            continue;
                     }
                     else if (c == '/' && cprev == '/')
                     {
                            // Hmm. A 'run on' path with two slashes right next to each other.
                            // This is an illegal path, but, hey, we'll be tough and try to
                            // deal with it (especially since '::' has loaded semantics in
                            // a Mac path)
                            continue;
                     }
                     *dst++ = c;
              } while (c);
              if (hexDecode)
                     nsUnescape(result);     // Hex Decode
              MacFileHelpers::SwapSlashColon(result);
       }
       return result;
} // MacFileHelpers::MacPathFromUnixPath

Here is the call graph for this function:

Here is the caller graph for this function:

OSErr MacFileHelpers::MakeAliasSafe ( FSSpec inOutSpec)

Definition at line 218 of file nsFileSpecMac.cpp.

{
       EnsureAliasHaven();
       nsFileSpec dstDirSpec(sAliasHavenVRefNum, sAliasHavenDirID, "\p");

       // Make sure its name is unique
       nsFileSpec havenSpec(sAliasHavenVRefNum, sAliasHavenDirID, "\pG'day");
       if (havenSpec.Valid())
              havenSpec.MakeUnique(inOutSpec.name);
       // Copy the file into the haven directory
       if (havenSpec.Valid())
       {
              OSErr err = ::FSpFileCopy(
                                          &inOutSpec,
                                          dstDirSpec,
                                          havenSpec.GetLeafPName(),
                                          nil, 0, true);
              // Return the spec of the copy to the caller.
              if (err != noErr)
                     return err;
              inOutSpec = havenSpec;
       }
       return noErr;
} // MacFileHelpers::MakeAliasSafe

Here is the call graph for this function:

Definition at line 529 of file nsFileSpecMac.cpp.

{
       char* result = nil;
       nsresult rv;

    FSSpec nonConstSpec = inSpec;
    nsCAutoString path;     
    nsCOMPtr<nsILocalFileMac> macFile;
    
    rv = NS_NewLocalFileWithFSSpec(&nonConstSpec, PR_TRUE, getter_AddRefs(macFile));
    if (NS_FAILED(rv)) return nsnull;
    nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(macFile, &rv));
    if (NS_FAILED(rv)) return nsnull;
    rv = localFile->GetNativePath(path);
    if (NS_FAILED(rv)) return nsnull;
    PRInt32 strLen = path.Length();
    result = new char [strLen + 1];
    if (!result) return nsnull;
       memcpy(result, path.get(), strLen);
       result[ strLen ] = 0;

       return result;
} // MacFileHelpers::PathNameFromFSSpec

Here is the call graph for this function:

void MacFileHelpers::PLstrcpy ( Str255  dst,
ConstStr255Param  src 
) [inline]

Definition at line 69 of file nsFileSpecMac.cpp.

                                                               {
                                                                      memcpy(dst, src, 1 + src[0]);
                                                               }

Here is the call graph for this function:

Here is the caller graph for this function:

void MacFileHelpers::PLstrcpy ( Str255  dst,
const char *  src,
int  inMaxLen = 255 
)

Definition at line 120 of file nsFileSpecMac.cpp.

{
       int srcLength = strlen(src);
       NS_ASSERTION(srcLength <= inMax, "Oops, string is too long!");
       if (srcLength > inMax)
              srcLength = inMax;
       dst[0] = srcLength;
       memcpy(&dst[1], src, srcLength);
}

Here is the call graph for this function:

void MacFileHelpers::PLstrncpy ( Str255  dst,
const char *  src,
int  inMaxLen 
)

Definition at line 132 of file nsFileSpecMac.cpp.

{
       int srcLength = strlen(src);
       if (srcLength > inMax)
              srcLength = inMax;
       dst[0] = srcLength;
       memcpy(&dst[1], src, srcLength);
}

Here is the call graph for this function:

Here is the caller graph for this function:

OSErr MacFileHelpers::ResolveAliasFile ( FSSpec inOutSpec,
Boolean wasAliased 
)

Definition at line 482 of file nsFileSpecMac.cpp.

{
       wasAliased = false;
       if (IsAliasSafe(inOutSpec))
              return noErr;
       Boolean dummy;         
       return ::ResolveAliasFile(&inOutSpec, TRUE, &dummy, &wasAliased);
} // MacFileHelpers::ResolveAliasFile

Here is the call graph for this function:

Here is the caller graph for this function:

void MacFileHelpers::SetNoResolve ( Boolean  inResolve) [inline]

Definition at line 211 of file nsFileSpecMac.cpp.

{
       sNoResolve = inResolve;
} // MacFileHelpers::SetNoResolve

Definition at line 143 of file nsFileSpecMac.cpp.

{
       while (*s)
       {
              if (*s == '/')
                     *s++ = ':';
              else if (*s == ':')
                     *s++ = '/';
              else
                     *s++;
       }
} // MacFileHelpers::SwapSlashColon

Here is the caller graph for this function:


Variable Documentation

Definition at line 115 of file nsFileSpecMac.cpp.

Definition at line 116 of file nsFileSpecMac.cpp.

Definition at line 114 of file nsFileSpecMac.cpp.