//============================================================================ // NFileUtilities::GetDirectory : Get a standard directory. //---------------------------------------------------------------------------- NFile NFileUtilities::GetDirectory(NDirectoryLocation theLocation, const NString &fileName, NDirectoryDomain theDomain, bool canCreate) { NFile theFile; // Get the directory theFile = NTargetFile::GetDirectory(theDomain, theLocation); theFile.ResolveTarget(); if (!fileName.IsEmpty() && theFile.IsValid()) { theFile = theFile.GetChild(fileName); theFile.ResolveTarget(); } // Create it if necessary if (!theFile.Exists() && canCreate) theFile.CreateDirectory(); return(theFile); }
//============================================================================ // NFileUtilities::GetUniqueFile : Get a uniquely-named file. //---------------------------------------------------------------------------- NFile NFileUtilities::GetUniqueFile(const NFile &theDirectory, const NString &fileName) { NString nameChild, nameFile, nameExt; NRange extBreak; NFile theFile; NIndex n; // Validate our parameters NN_ASSERT(theDirectory.IsDirectory()); // Get the state we need if (fileName.IsEmpty()) { nameFile = "Temp"; nameExt = ".tmp"; } else { extBreak = fileName.Find(".", kNStringBackwards); if (extBreak.GetSize() == 1) { nameFile = fileName.GetLeft( extBreak.GetLocation()); nameExt = fileName.GetString(extBreak.GetLocation()); } else { nameFile = fileName; nameExt = ""; } } // Generate a unique name n = 0; while (n < 10000) { // Build the name if (n == 0) nameChild = nameFile; else nameChild.Format("%@ %ld", nameFile, n); if (!nameExt.IsEmpty()) nameChild += nameExt; // Check for the file theFile = theDirectory.GetChild(nameChild); if (!theFile.Exists()) return(theFile); n++; } // Handle failure NN_LOG("Unable to create a unique name"); theFile.Clear(); return(theFile); }