コード例 #1
0
ファイル: NFileUtilities.cpp プロジェクト: refnum/nano
//============================================================================
//		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);
}
コード例 #2
0
ファイル: NFileUtilities.cpp プロジェクト: refnum/nano
//============================================================================
//		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);
}