Changeset - r18015:26a04665a4eb
[Not reviewed]
master
0 1 0
rubidium - 13 years ago 2011-08-25 10:24:49
rubidium@openttd.org
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
1 file changed with 15 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/fileio.cpp
Show inline comments
 
@@ -1228,12 +1228,25 @@ void *ReadFileToMem(const char *filename
 
	fclose(in);
 

	
 
	*lenp = len;
 
	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
 
 * any graphics sets that are found.
 
 * @param fs              the file scanner to add the files to
 
 * @param extension       the extension of files to search for.
 
@@ -1265,21 +1278,13 @@ static uint ScanPath(FileScanner *fs, co
 
			if (!recursive) continue;
 
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
 
			if (!AppendPathSeparator(filename, lengthof(filename))) continue;
 
			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++;
 
		}
 
	}
 

	
 
	closedir(dir);
 

	
 
	return num;
 
@@ -1293,21 +1298,13 @@ static uint ScanPath(FileScanner *fs, co
 
 */
 
static uint ScanTar(FileScanner *fs, const char *extension, TarFileList::iterator tar)
 
{
 
	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;
 
}
 

	
 
/**
 
 * Scan for files with the given extention in the given search path.
0 comments (0 inline, 0 general)