changeset 15148:236482e2e554 draft

(svn r19777) -Change: use the file scanner to find the .tars
author rubidium <rubidium@openttd.org>
date Mon, 10 May 2010 09:37:17 +0000
parents 8d93ab1f11fd
children 705c7db7523c
files src/fileio.cpp src/fileio_func.h src/network/network_content.cpp
diffstat 3 files changed, 23 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -534,7 +534,18 @@
 #endif
 }
 
-bool TarListAddFile(const char *filename)
+/* static */ uint TarScanner::DoScan() {
+	DEBUG(misc, 1, "Scanning for tars");
+	TarScanner fs;
+	uint num = fs.Scan(".tar", DATA_DIR, false);
+	num += fs.Scan(".tar", AI_DIR, false);
+	num += fs.Scan(".tar", AI_LIBRARY_DIR, false);
+	num += fs.Scan(".tar", SCENARIO_DIR, false);
+	DEBUG(misc, 1, "Scan complete, found %d files", num);
+	return num;
+}
+
+bool TarScanner::AddFile(const char *filename, size_t basepath_length)
 {
 	/* The TAR-header, repeated for every file */
 	typedef struct TarHeader {
@@ -820,66 +831,6 @@
 	return true;
 }
 
-static int ScanPathForTarFiles(const char *path, size_t basepath_length)
-{
-	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
-
-	uint num = 0;
-	struct stat sb;
-	struct dirent *dirent;
-	DIR *dir;
-
-	if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
-
-	while ((dirent = readdir(dir)) != NULL) {
-		const char *d_name = FS2OTTD(dirent->d_name);
-		char filename[MAX_PATH];
-
-		if (!FiosIsValidFile(path, dirent, &sb)) continue;
-
-		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
-
-		if (S_ISDIR(sb.st_mode)) {
-			/* Directory */
-			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
-			AppendPathSeparator(filename, lengthof(filename));
-			num += ScanPathForTarFiles(filename, basepath_length);
-		} else if (S_ISREG(sb.st_mode)) {
-			/* File */
-			char *ext = strrchr(filename, '.');
-
-			/* If no extension or extension isn't .tar, skip the file */
-			if (ext == NULL) continue;
-			if (strcasecmp(ext, ".tar") != 0) continue;
-
-			if (TarListAddFile(filename)) num++;
-		}
-	}
-
-	closedir(dir);
-	return num;
-}
-
-void ScanForTarFiles()
-{
-	Searchpath sp;
-	char path[MAX_PATH];
-	uint num = 0;
-
-	DEBUG(misc, 1, "Scanning for tars");
-	FOR_ALL_SEARCHPATHS(sp) {
-		FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR);
-		num += ScanPathForTarFiles(path, strlen(path));
-		FioAppendDirectory(path, MAX_PATH, sp, AI_DIR);
-		num += ScanPathForTarFiles(path, strlen(path));
-		FioAppendDirectory(path, MAX_PATH, sp, AI_LIBRARY_DIR);
-		num += ScanPathForTarFiles(path, strlen(path));
-		FioAppendDirectory(path, MAX_PATH, sp, SCENARIO_DIR);
-		num += ScanPathForTarFiles(path, strlen(path));
-	}
-	DEBUG(misc, 1, "Scan complete, found %d files", num);
-}
-
 #if defined(WIN32) || defined(WINCE)
 /**
  * Determine the base (personal dir and game data dir) paths
@@ -1079,7 +1030,7 @@
 	}
 #endif /* ENABLE_NETWORK */
 
-	ScanForTarFiles();
+	TarScanner::DoScan();
 }
 
 /**
--- a/src/fileio_func.h
+++ b/src/fileio_func.h
@@ -87,6 +87,14 @@
 	virtual bool AddFile(const char *filename, size_t basepath_length) = 0;
 };
 
+/** Helper for scanning for files with tar as extension */
+class TarScanner : FileScanner {
+public:
+	/* virtual */ bool AddFile(const char *filename, size_t basepath_length);
+
+	/** Do the scan for Tars. */
+	static uint DoScan();
+};
 
 /* Implementation of opendir/readdir/closedir for Windows */
 #if defined(WIN32)
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -27,7 +27,6 @@
 #include <zlib.h>
 #endif
 
-extern bool TarListAddFile(const char *filename);
 extern bool HasScenario(const ContentInfo *ci, bool md5sum);
 ClientNetworkContentSocketHandler _network_content_client;
 
@@ -498,7 +497,8 @@
 	if (GunzipFile(this->curInfo)) {
 		unlink(GetFullFilename(this->curInfo, true));
 
-		TarListAddFile(GetFullFilename(this->curInfo, false));
+		TarScanner ts;
+		ts.AddFile(GetFullFilename(this->curInfo, false), 0);
 
 		if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
 			/* Music can't be in a tar. So extract the tar! */