diff options
author | rubidium <rubidium@openttd.org> | 2008-08-31 08:38:30 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2008-08-31 08:38:30 +0000 |
commit | 1afa473d0f26f56750d899ceaacfcbfbae7d21e0 (patch) | |
tree | defa07ef71e4c84470e000c2117cedd50e808214 /src/newgrf_config.cpp | |
parent | bb92a7ab08fdd51f2b6902b7cd2b8d4b1427ff39 (diff) | |
download | openttd-1afa473d0f26f56750d899ceaacfcbfbae7d21e0.tar.xz |
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
Diffstat (limited to 'src/newgrf_config.cpp')
-rw-r--r-- | src/newgrf_config.cpp | 89 |
1 files changed, 16 insertions, 73 deletions
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index a8aeb2f63..6faeb8d77 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -15,10 +15,8 @@ #include "gamelog.h" #include "network/network_type.h" -#include "tar_type.h" #include "fileio.h" #include "fios.h" -#include <sys/stat.h> GRFConfig *_all_grfs; @@ -267,10 +265,23 @@ compatible_grf: return res; } -static bool ScanPathAddGrf(const char *filename) +/** Helper for scanning for files with GRF as extension */ +class GRFFileScanner : FileScanner { +public: + /* virtual */ bool AddFile(const char *filename, size_t basepath_length); + + /** Do the scan for GRFs. */ + static uint DoScan() + { + GRFFileScanner fs; + return fs.Scan(".grf", DATA_DIR); + } +}; + +bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length) { GRFConfig *c = CallocT<GRFConfig>(1); - c->filename = strdup(filename); + c->filename = strdup(filename + basepath_length); bool added = true; if (FillGRFDetails(c, false)) { @@ -313,63 +324,6 @@ static bool ScanPathAddGrf(const char *filename) return added; } -/* Scan a path for NewGRFs */ -static uint ScanPath(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 (sb.st_mode & S_IFDIR) { - /* Directory */ - if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue; - AppendPathSeparator(filename, lengthof(filename)); - num += ScanPath(filename, basepath_length); - } else if (sb.st_mode & S_IFREG) { - /* File */ - char *ext = strrchr(filename, '.'); - - /* If no extension or extension isn't .grf, skip the file */ - if (ext == NULL) continue; - if (strcasecmp(ext, ".grf") != 0) continue; - - if (ScanPathAddGrf(filename + basepath_length)) num++; - } - } - - closedir(dir); - - return num; -} - -static uint ScanTar(TarFileList::iterator tar) -{ - uint num = 0; - const char *filename = (*tar).first.c_str(); - const char *ext = strrchr(filename, '.'); - - /* If no extension or extension isn't .grf, skip the file */ - if (ext == NULL) return false; - if (strcasecmp(ext, ".grf") != 0) return false; - - if (ScanPathAddGrf(filename)) num++; - - return num; -} - /** * Simple sorter for GRFS * @param p1 the first GRFConfig * @@ -388,21 +342,10 @@ static int CDECL GRFSorter(const void *p1, const void *p2) /* Scan for all NewGRFs */ void ScanNewGRFFiles() { - Searchpath sp; - char path[MAX_PATH]; - TarFileList::iterator tar; - uint num = 0; - ClearGRFConfigList(&_all_grfs); DEBUG(grf, 1, "Scanning for NewGRFs"); - FOR_ALL_SEARCHPATHS(sp) { - FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR); - num += ScanPath(path, strlen(path)); - } - FOR_ALL_TARS(tar) { - num += ScanTar(tar); - } + uint num = GRFFileScanner::DoScan(); DEBUG(grf, 1, "Scan complete, found %d files", num); if (num == 0 || _all_grfs == NULL) return; |