Example #1
0
static PRBool pathBeginsWithVolName(const nsACString& path, nsACString& firstPathComponent)
{
  // Return whether the 1st path component in path (escaped) is equal to the name
  // of a mounted volume. Return the 1st path component (unescaped) in any case.
  // This needs to be done as quickly as possible, so we cache a list of volume names.
  // XXX Register an event handler to detect drives being mounted/unmounted?
  
  static nsCStringArray gVolumeList; // We will leak this - one for the life of the app :-/

  // Cache a list of volume names
  if (!gVolumeList.Count()) {
    OSErr err;
    ItemCount volumeIndex = 1;
    
    do {
      HFSUniStr255 volName;
      FSRef rootDirectory;
      err = ::FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoNone, NULL, &volName, &rootDirectory);
      if (err == noErr) {
        NS_ConvertUTF16toUTF8 volNameStr(Substring((PRUnichar *)volName.unicode,
                                                   (PRUnichar *)volName.unicode + volName.length));
        gVolumeList.AppendCString(volNameStr);
        volumeIndex++;
      }
    } while (err == noErr);
  }
  
  // Extract the first component of the path
  nsACString::const_iterator start;
  path.BeginReading(start);
  start.advance(1); // path begins with '/'
  nsACString::const_iterator directory_end;
  path.EndReading(directory_end);
  nsACString::const_iterator component_end(start);
  FindCharInReadable('/', component_end, directory_end);
  
  nsCAutoString flatComponent((Substring(start, component_end)));
  NS_UnescapeURL(flatComponent);
  PRInt32 foundIndex = gVolumeList.IndexOf(flatComponent);
  firstPathComponent = flatComponent;
  return (foundIndex != -1);
}
Example #2
0
static void AppendElements(nsCStringArray& target, nsCStringArray& source)
{
  for (PRInt32 i = source.Count() - 1; i >= 0; i--)
    target.AppendCString(*source.CStringAt(i));
}