1
0
Fork 0

(svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit

release/0.6
truelight 2007-09-13 18:28:56 +00:00
parent bf17a96b63
commit 70772e9cc9
1 changed files with 44 additions and 40 deletions

View File

@ -270,42 +270,10 @@ compatible_grf:
return res;
}
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
/* Scan a path for NewGRFs */
static uint ScanPath(const char *path, int basepath_length)
static bool ScanPathAddGrf(const char *filename)
{
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;
GRFConfig *c = CallocT<GRFConfig>(1);
c->filename = strdup(filename + basepath_length);
c->filename = strdup(filename);
bool added = true;
if (FillGRFDetails(c, false)) {
@ -340,9 +308,45 @@ static uint ScanPath(const char *path, int basepath_length)
free(c->name);
free(c->info);
free(c);
} else {
num++;
}
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;
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++;
}
}