summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2011-08-25 10:24:49 +0000
committerrubidium <rubidium@openttd.org>2011-08-25 10:24:49 +0000
commit41169291e399771ef807777c8706b0d4b94d6448 (patch)
treefd70fcee489d374f57cb282a04059b7dfc56e2b7 /src
parent09c3cabc1e75257a64eef78b99238e05b038c969 (diff)
downloadopenttd-41169291e399771ef807777c8706b0d4b94d6448.tar.xz
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
Diffstat (limited to 'src')
-rw-r--r--src/fileio.cpp33
1 files changed, 15 insertions, 18 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp
index d2b3c7fc2..9f3d7ea94 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -1231,6 +1231,19 @@ void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
return mem;
}
+/**
+ * Helper to see whether a given filename matches the extension.
+ * @param extension The extension to look for.
+ * @param filename The filename to look in for the extension.
+ * @return True iff the extension is NULL, or the filename ends with it.
+ */
+static bool MatchesExtension(const char *extension, const char *filename)
+{
+ if (extension == NULL) return true;
+
+ const char *ext = strrchr(filename, extension[0]);
+ return ext != NULL && strcasecmp(ext, extension) == 0;
+}
/**
* Scan a single directory (and recursively its children) and add
@@ -1268,15 +1281,7 @@ static uint ScanPath(FileScanner *fs, const char *extension, const char *path, s
num += ScanPath(fs, extension, filename, basepath_length, recursive);
} else if (S_ISREG(sb.st_mode)) {
/* File */
- if (extension != NULL) {
- char *ext = strrchr(filename, '.');
-
- /* If no extension or extension isn't .grf, skip the file */
- if (ext == NULL) continue;
- if (strcasecmp(ext, extension) != 0) continue;
- }
-
- if (fs->AddFile(filename, basepath_length)) num++;
+ if (MatchesExtension(extension, filename) && fs->AddFile(filename, basepath_length)) num++;
}
}
@@ -1296,15 +1301,7 @@ static uint ScanTar(FileScanner *fs, const char *extension, TarFileList::iterato
uint num = 0;
const char *filename = (*tar).first.c_str();
- if (extension != NULL) {
- const char *ext = strrchr(filename, '.');
-
- /* If no extension or extension isn't .grf, skip the file */
- if (ext == NULL) return false;
- if (strcasecmp(ext, extension) != 0) return false;
- }
-
- if (fs->AddFile(filename, 0)) num++;
+ if (MatchesExtension(extension, filename) && fs->AddFile(filename, 0)) num++;
return num;
}