src/file.cpp

changeset 497
c51941e590b6
parent 493
16766ac1bbd9
child 498
791c831c8020
--- a/src/file.cpp	Thu Oct 03 21:48:11 2013 +0300
+++ b/src/file.cpp	Sat Oct 05 12:51:13 2013 +0300
@@ -53,8 +53,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 namespace LDPaths
-{
-	static str pathError;
+{	static str pathError;
 
 	struct
 	{	str LDConfigPath;
@@ -99,12 +98,15 @@
 	str getError()
 	{	return pathError;
 	}
+
 	str ldconfig()
 	{	return pathInfo.LDConfigPath;
 	}
+
 	str prims()
 	{	return pathInfo.primsPath;
 	}
+
 	str parts()
 	{	return pathInfo.partsPath;
 	}
@@ -123,11 +125,11 @@
 // -----------------------------------------------------------------------------
 LDFile::~LDFile()
 {	// Clear everything from the model
-for (LDObject * obj : objects())
+	for (LDObject* obj : objects())
 		delete obj;
 
 	// Clear the cache as well
-for (LDObject * obj : cache())
+	for (LDObject* obj : cache())
 		delete obj;
 
 	// Remove this file from the list of files
@@ -141,18 +143,20 @@
 	// If we just closed the current file, we need to set the current
 	// file as something else.
 	if (this == LDFile::current())
-	{	// If we closed the last file, create a blank one.
-		if (countExplicitFiles() == 0)
+	{	bool found = false;
+
+		// Try find an explicitly loaded file - if we can't find one,
+		// we need to create a new file to switch to.
+		for (LDFile* file : g_loadedFiles)
+		{	if (!file->implicit())
+			{	LDFile::setCurrent (file);
+				found = true;
+				break;
+			}
+		}
+
+		if (!found)
 			newFile();
-		else
-		{	// Find the first explicit file loaded
-			int idx = 0;
-
-			while (g_loadedFiles[idx]->implicit())
-				idx++;
-
-			LDFile::setCurrent (g_loadedFiles[idx]);
-		}
 	}
 
 	g_win->updateFileList();
@@ -229,15 +233,16 @@
 		return f;
 
 	if (subdirs)
-	{	// Look in sub-directories: parts and p. Also look in net_downloadpath.
-	for (const str & topdir : initlist<str> ( { io_ldpath, net_downloadpath }))
-
-		for (const str & subdir : initlist<str> ( { "parts", "p" }))
+	{	// Look in sub-directories: parts and p. Also look in net_downloadpath, since that's
+		// where we download parts from the PT to.
+		for (const str& topdir : initlist<str> ({ io_ldpath, net_downloadpath }))
+		{	for (const str& subdir : initlist<str> ({ "parts", "p" }))
 			{	fullPath = fmt ("%1" DIRSLASH "%2" DIRSLASH "%3", topdir, subdir, relpath);
 
 				if (f->open (fullPath, File::Read))
 					return f;
 			}
+		}
 	}
 
 	// Did not find the file.

mercurial