Changeset - r11252:706b96dca92b
[Not reviewed]
master
0 2 0
rubidium - 15 years ago 2009-03-04 00:17:51
rubidium@openttd.org
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
2 files changed with 9 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/fileio.cpp
Show inline comments
 
@@ -1019,13 +1019,13 @@ void *ReadFileToMem(const char *filename
 
 * Scan a single directory (and recursively it's children) and add
 
 * any graphics sets that are found.
 
 * @param extension       the extension of files to search for.
 
 * @param path            full path we're currently at
 
 * @param basepath_length from where in the path are we 'based' on the search path
 
 */
 
static uint ScanPath(FileScanner *fs, const char *extension, const char *path, size_t basepath_length)
 
static uint ScanPath(FileScanner *fs, const char *extension, const char *path, size_t basepath_length, bool recursive)
 
{
 
	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
 

	
 
	uint num = 0;
 
	struct stat sb;
 
	struct dirent *dirent;
 
@@ -1040,15 +1040,16 @@ static uint ScanPath(FileScanner *fs, co
 
		if (!FiosIsValidFile(path, dirent, &sb)) continue;
 

	
 
		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
 

	
 
		if (S_ISDIR(sb.st_mode)) {
 
			/* Directory */
 
			if (!recursive) continue;
 
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
 
			AppendPathSeparator(filename, lengthof(filename));
 
			num += ScanPath(fs, extension, filename, basepath_length);
 
			num += ScanPath(fs, extension, filename, basepath_length, recursive);
 
		} else if (S_ISREG(sb.st_mode)) {
 
			/* File */
 
			char *ext = strrchr(filename, '.');
 

	
 
			/* If no extension or extension isn't .grf, skip the file */
 
			if (ext == NULL) continue;
 
@@ -1088,22 +1089,22 @@ static uint ScanTar(FileScanner *fs, con
 
 * @param extension the extension of files to search for.
 
 * @param sd        the sub directory to search in.
 
 * @param tars      whether to search in the tars too.
 
 * @return the number of found files, i.e. the number of times that
 
 *         AddFile returned true.
 
 */
 
uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars)
 
uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool recursive)
 
{
 
	Searchpath sp;
 
	char path[MAX_PATH];
 
	TarFileList::iterator tar;
 
	uint num = 0;
 

	
 
	FOR_ALL_SEARCHPATHS(sp) {
 
		FioAppendDirectory(path, MAX_PATH, sp, sd);
 
		num += ScanPath(this, extension, path, strlen(path));
 
		num += ScanPath(this, extension, path, strlen(path), recursive);
 
	}
 

	
 
	if (tars) {
 
		FOR_ALL_TARS(tar) {
 
			num += ScanTar(this, extension, tar);
 
		}
 
@@ -1116,13 +1117,13 @@ uint FileScanner::Scan(const char *exten
 
 * Scan for files with the given extention in the given search path.
 
 * @param extension the extension of files to search for.
 
 * @param directory the sub directory to search in.
 
 * @return the number of found files, i.e. the number of times that
 
 *         AddFile returned true.
 
 */
 
uint FileScanner::Scan(const char *extension, const char *directory)
 
uint FileScanner::Scan(const char *extension, const char *directory, bool recursive)
 
{
 
	char path[MAX_PATH];
 
	strecpy(path, directory, lastof(path));
 
	AppendPathSeparator(path, lengthof(path));
 
	return ScanPath(this, extension, path, strlen(path));
 
	return ScanPath(this, extension, path, strlen(path), recursive);
 
}
src/fileio_func.h
Show inline comments
 
@@ -70,14 +70,14 @@ extern char *_personal_dir; ///< custom 
 
class FileScanner
 
{
 
public:
 
	/** Destruct the proper one... */
 
	virtual ~FileScanner() {}
 

	
 
	uint Scan(const char *extension, Subdirectory sd, bool tars = true);
 
	uint Scan(const char *extension, const char *directory);
 
	uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
 
	uint Scan(const char *extension, const char *directory, bool recursive = true);
 

	
 
	/**
 
	 * Add a file with the given filename.
 
	 * @param filename        the full path to the file to read
 
	 * @param basepath_length amount of characters to chop of before to get a
 
	 *                        filename relative to the search path.
0 comments (0 inline, 0 general)