(svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
authortruelight
Thu, 13 Sep 2007 18:28:56 +0000
changeset 8068 5dd156184d13
parent 8067 1a31a15e638d
child 8069 da9d65ad1615
(svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
src/newgrf_config.cpp
--- a/src/newgrf_config.cpp	Thu Sep 13 18:26:18 2007 +0000
+++ b/src/newgrf_config.cpp	Thu Sep 13 18:28:56 2007 +0000
@@ -270,12 +270,54 @@
 	return res;
 }
 
+static bool ScanPathAddGrf(const char *filename)
+{
+	GRFConfig *c = CallocT<GRFConfig>(1);
+	c->filename = strdup(filename);
 
-extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
+	bool added = true;
+	if (FillGRFDetails(c, false)) {
+		if (_all_grfs == NULL) {
+			_all_grfs = c;
+		} else {
+			/* Insert file into list at a position determined by its
+				* name, so the list is sorted as we go along */
+			GRFConfig **pd, *d;
+			bool stop = false;
+			for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
+				if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false;
+				/* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
+				*  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
+				*  just after the first with the same name. Avoids doubles in the list. */
+				if (strcasecmp(c->name, d->name) <= 0) stop = true;
+				else if (stop) break;
+			}
+			if (added) {
+				c->next = d;
+				*pd = c;
+			}
+		}
+	} else {
+		added = false;
+	}
+
+	if (!added) {
+		/* File couldn't be opened, or is either not a NewGRF or is a
+			* 'system' NewGRF or it's already known, so forget about it. */
+		free(c->filename);
+		free(c->name);
+		free(c->info);
+		free(c);
+	}
+
+	return added;
+}
 
 /* Scan a path for NewGRFs */
 static uint ScanPath(const char *path, int basepath_length)
 {
+	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
+
 	uint num = 0;
 	struct stat sb;
 	struct dirent *dirent;
@@ -304,45 +346,7 @@
 			if (ext == NULL) continue;
 			if (strcasecmp(ext, ".grf") != 0) continue;
 
-			GRFConfig *c = CallocT<GRFConfig>(1);
-			c->filename = strdup(filename + basepath_length);
-
-			bool added = true;
-			if (FillGRFDetails(c, false)) {
-				if (_all_grfs == NULL) {
-					_all_grfs = c;
-				} else {
-					/* Insert file into list at a position determined by its
-					 * name, so the list is sorted as we go along */
-					GRFConfig **pd, *d;
-					bool stop = false;
-					for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
-						if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false;
-						/* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
-						*  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
-						*  just after the first with the same name. Avoids doubles in the list. */
-						if (strcasecmp(c->name, d->name) <= 0) stop = true;
-						else if (stop) break;
-					}
-					if (added) {
-						c->next = d;
-						*pd = c;
-					}
-				}
-			} else {
-				added = false;
-			}
-
-			if (!added) {
-				/* File couldn't be opened, or is either not a NewGRF or is a
-				 * 'system' NewGRF or it's already known, so forget about it. */
-				free(c->filename);
-				free(c->name);
-				free(c->info);
-				free(c);
-			} else {
-				num++;
-			}
+			if (ScanPathAddGrf(filename + basepath_length)) num++;
 		}
 	}