JBoolean
JXDocumentManager::FindFile
	(
	const JCharacter*	fileName,
	const JCharacter*	currPath,
	JString*			newFileName,
	const JBoolean		askUser
	)
	const
{
	// if the file exists, we are done

	if (JFileExists(fileName))
		{
		*newFileName = fileName;
		return kJTrue;
		}

	// search the directory tree below currPath

	JString path, name, newPath;
	JSplitPathAndName(fileName, &path, &name);

	if (JSearchSubdirs(currPath, name, kJTrue, kJTrue, &newPath))
		{
		*newFileName = newPath + name;
		return kJTrue;
		}

	// check for known case of move/rename

	if (SearchFileMap(fileName, newFileName))
		{
		return kJTrue;
		}

	// ask the user to find it

	if (askUser)
		{
		JString instrMsg = "Unable to locate ";
		instrMsg += fileName;
		instrMsg += "\n\nPlease find it.";

		while ((JGetChooseSaveFile())->ChooseFile("New name of file:", instrMsg, newFileName))
			{
			JString newPath, newName;
			JSplitPathAndName(*newFileName, &newPath, &newName);
			if (newName != name)
				{
				JString warnMsg = name;
				warnMsg += " was requested.\n\nYou selected ";
				warnMsg += newName;
				warnMsg += ".\n\nAre you sure that this is correct?";
				if (!(JGetUserNotification())->AskUserNo(warnMsg))
					{
					continue;
					}
				}

			JString trueName;
			const JBoolean ok = JGetTrueName(*newFileName, &trueName);
			assert( ok );

			FileMap map;
			map.oldName = new JString(fileName);
			assert( map.oldName != NULL );
			map.newName = new JString(trueName);
			assert( map.newName != NULL );
			itsFileMap->AppendElement(map);

			*newFileName = trueName;
			return kJTrue;
			}
		}

	newFileName->Clear();
	return kJFalse;
}
Example #2
0
JBoolean
CBApp::FindFile
	(
	const JCharacter*	fileName,
	const JBoolean		caseSensitive,
	JString*			fullName
	)
	const
{
	JBoolean cancelled = kJFalse;

	const JBoolean relative = JIsRelativePath(fileName);
	if (!relative && JFileExists(fileName))
		{
		*fullName = fileName;
		return kJTrue;
		}
	else if (relative)
		{
		CBDirInfoList searchPaths;
		CollectSearchPaths(&searchPaths);
		const JSize dirCount = searchPaths.GetElementCount(),
					sysCount = itsSystemIncludeDirs->GetElementCount();

		JBoolean found = kJFalse;

		JLatentPG pg;
		JString msg = "Searching for \"";
		msg += fileName;
		msg += "\"...";
		pg.FixedLengthProcessBeginning(dirCount+sysCount, msg, kJTrue, kJFalse);

		JString path, newName;
		for (JIndex i=1; i<=dirCount; i++)
			{
			const CBDirInfo info = searchPaths.GetElement(i);
			if (!info.recurse)
				{
				*fullName = JCombinePathAndName(*(info.path), fileName);
				if (JFileExists(*fullName))
					{
					found = kJTrue;
					break;
					}
				}
			else if (JSearchSubdirs(*(info.path), fileName, kJTrue, caseSensitive,
									&path, &newName, NULL, &cancelled))
				{
				*fullName = JCombinePathAndName(path, newName);
				found     = kJTrue;
				break;
				}
			else if (cancelled)
				{
				break;
				}

			if (!pg.IncrementProgress())
				{
				cancelled = kJTrue;
				break;
				}
			}

		if (!found && !cancelled)
			{
			// We have to search system paths last because these are always
			// last on the compiler search path.

			for (JIndex i=1; i<=sysCount; i++)
				{
				if (JSearchSubdirs(*itsSystemIncludeDirs->GetElement(i), fileName, kJTrue, caseSensitive,
								   &path, &newName, NULL, &cancelled))
					{
					*fullName = JCombinePathAndName(path, newName);
					found     = kJTrue;
					break;
					}
				else if (cancelled)
					{
					break;
					}

				if (!pg.IncrementProgress())
					{
					cancelled = kJTrue;
					break;
					}
				}
			}

		pg.ProcessFinished();
		searchPaths.DeleteAll();
		if (found)
			{
			return kJTrue;
			}
		}

	if (!cancelled)
		{
		JString msg = "Unable to find the file \"";
		msg += fileName;
		msg += "\".";
		(JGetUserNotification())->ReportError(msg);
		}

	fullName->Clear();
	return kJFalse;
}