1
0
Fork 0

Codechange: Replace FOR_ALL_SEARCHPATHS with range-based for loops

pull/9187/head
glx22 2021-04-30 15:41:58 +02:00 committed by Loïc Guilloux
parent 34215f7faa
commit 983c7ade60
5 changed files with 22 additions and 29 deletions

View File

@ -241,6 +241,7 @@ static_assert(lengthof(_subdirs) == NUM_SUBDIRS);
* current operating system. * current operating system.
*/ */
std::array<std::string, NUM_SEARCHPATHS> _searchpaths; std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
std::vector<Searchpath> _valid_searchpaths;
std::array<TarList, NUM_SUBDIRS> _tar_list; std::array<TarList, NUM_SUBDIRS> _tar_list;
TarFileList _tar_filelist[NUM_SUBDIRS]; TarFileList _tar_filelist[NUM_SUBDIRS];
@ -252,11 +253,19 @@ static TarLinkList _tar_linklist[NUM_SUBDIRS]; ///< List of directory links
* @param sp the search path to check * @param sp the search path to check
* @return true if the search path is valid * @return true if the search path is valid
*/ */
bool IsValidSearchPath(Searchpath sp) static bool IsValidSearchPath(Searchpath sp)
{ {
return sp < _searchpaths.size() && !_searchpaths[sp].empty(); return sp < _searchpaths.size() && !_searchpaths[sp].empty();
} }
static void FillValidSearchPaths()
{
_valid_searchpaths.clear();
for (Searchpath sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) {
if (IsValidSearchPath(sp)) _valid_searchpaths.emplace_back(sp);
}
}
/** /**
* Check whether the given file exists * Check whether the given file exists
* @param filename the file to try for existence. * @param filename the file to try for existence.
@ -298,10 +307,9 @@ void FioFCloseFile(FILE *f)
*/ */
std::string FioFindFullPath(Subdirectory subdir, const char *filename) std::string FioFindFullPath(Subdirectory subdir, const char *filename)
{ {
Searchpath sp;
assert(subdir < NUM_SUBDIRS); assert(subdir < NUM_SUBDIRS);
FOR_ALL_SEARCHPATHS(sp) { for (Searchpath sp : _valid_searchpaths) {
std::string buf = FioGetDirectory(sp, subdir); std::string buf = FioGetDirectory(sp, subdir);
buf += filename; buf += filename;
if (FileExists(buf)) return buf; if (FileExists(buf)) return buf;
@ -326,10 +334,8 @@ std::string FioGetDirectory(Searchpath sp, Subdirectory subdir)
std::string FioFindDirectory(Subdirectory subdir) std::string FioFindDirectory(Subdirectory subdir)
{ {
Searchpath sp;
/* Find and return the first valid directory */ /* Find and return the first valid directory */
FOR_ALL_SEARCHPATHS(sp) { for (Searchpath sp : _valid_searchpaths) {
std::string ret = FioGetDirectory(sp, subdir); std::string ret = FioGetDirectory(sp, subdir);
if (FileExists(ret)) return ret; if (FileExists(ret)) return ret;
} }
@ -406,11 +412,10 @@ FILE *FioFOpenFileTar(const TarFileListEntry &entry, size_t *filesize)
FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize) FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
{ {
FILE *f = nullptr; FILE *f = nullptr;
Searchpath sp;
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY); assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
FOR_ALL_SEARCHPATHS(sp) { for (Searchpath sp : _valid_searchpaths) {
f = FioFOpenFileSp(filename, mode, sp, subdir, filesize); f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
if (f != nullptr || subdir == NO_DIRECTORY) break; if (f != nullptr || subdir == NO_DIRECTORY) break;
} }
@ -1130,6 +1135,7 @@ std::string _personal_dir;
void DeterminePaths(const char *exe) void DeterminePaths(const char *exe)
{ {
DetermineBasePaths(exe); DetermineBasePaths(exe);
FillValidSearchPaths();
#ifdef USE_XDG #ifdef USE_XDG
std::string config_home; std::string config_home;
@ -1148,8 +1154,7 @@ void DeterminePaths(const char *exe)
AppendPathSeparator(config_home); AppendPathSeparator(config_home);
#endif #endif
Searchpath sp; for (Searchpath sp : _valid_searchpaths) {
FOR_ALL_SEARCHPATHS(sp) {
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue; if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;
DEBUG(misc, 4, "%s added as search path", _searchpaths[sp].c_str()); DEBUG(misc, 4, "%s added as search path", _searchpaths[sp].c_str());
} }
@ -1222,6 +1227,7 @@ void DeterminePaths(const char *exe)
/* If we have network we make a directory for the autodownloading of content */ /* If we have network we make a directory for the autodownloading of content */
_searchpaths[SP_AUTODOWNLOAD_DIR] = _personal_dir + "content_download" PATHSEP; _searchpaths[SP_AUTODOWNLOAD_DIR] = _personal_dir + "content_download" PATHSEP;
FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]); FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]);
FillValidSearchPaths();
/* Create the directory for each of the types of content */ /* Create the directory for each of the types of content */
const Subdirectory dirs[] = { SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR }; const Subdirectory dirs[] = { SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR };
@ -1368,10 +1374,9 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r
{ {
this->subdir = sd; this->subdir = sd;
Searchpath sp;
uint num = 0; uint num = 0;
FOR_ALL_SEARCHPATHS(sp) { for (Searchpath sp : _valid_searchpaths) {
/* Don't search in the working directory */ /* Don't search in the working directory */
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue; if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;

View File

@ -13,6 +13,7 @@
#include "core/enum_type.hpp" #include "core/enum_type.hpp"
#include "fileio_type.h" #include "fileio_type.h"
#include <string> #include <string>
#include <vector>
void FioSeekTo(size_t pos, int mode); void FioSeekTo(size_t pos, int mode);
void FioSeekToFile(uint8 slot, size_t pos); void FioSeekToFile(uint8 slot, size_t pos);
@ -26,16 +27,6 @@ void FioOpenFile(int slot, const std::string &filename, Subdirectory subdir);
void FioReadBlock(void *ptr, size_t size); void FioReadBlock(void *ptr, size_t size);
void FioSkipBytes(int n); void FioSkipBytes(int n);
/**
* Checks whether the given search path is a valid search path
* @param sp the search path to check
* @return true if the search path is valid
*/
bool IsValidSearchPath(Searchpath sp);
/** Iterator for all the search paths */
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
void FioFCloseFile(FILE *f); void FioFCloseFile(FILE *f);
FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir); bool FioCheckFileExists(const std::string &filename, Subdirectory subdir);
@ -54,6 +45,7 @@ bool FileExists(const std::string &filename);
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir); bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc. extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
extern std::vector<Searchpath> _valid_searchpaths;
/** Helper for scanning for files with a given name */ /** Helper for scanning for files with a given name */
class FileScanner { class FileScanner {

View File

@ -582,8 +582,7 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::s
* collections of NewGRFs or 32 bpp graphics replacement PNGs. * collections of NewGRFs or 32 bpp graphics replacement PNGs.
*/ */
bool match = false; bool match = false;
Searchpath sp; for (Searchpath sp : _valid_searchpaths) {
FOR_ALL_SEARCHPATHS(sp) {
std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR); std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) { if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {

View File

@ -116,8 +116,7 @@ bool ScriptInstance::LoadCompatibilityScripts(const char *api_version, Subdirect
{ {
char script_name[32]; char script_name[32];
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version); seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
Searchpath sp; for (Searchpath sp : _valid_searchpaths) {
FOR_ALL_SEARCHPATHS(sp) {
std::string buf = FioGetDirectory(sp, dir); std::string buf = FioGetDirectory(sp, dir);
buf += script_name; buf += script_name;
if (!FileExists(buf)) continue; if (!FileExists(buf)) continue;

View File

@ -1949,9 +1949,7 @@ static void GetLanguageList(const char *path)
*/ */
void InitializeLanguagePacks() void InitializeLanguagePacks()
{ {
Searchpath sp; for (Searchpath sp : _valid_searchpaths) {
FOR_ALL_SEARCHPATHS(sp) {
std::string path = FioGetDirectory(sp, LANG_DIR); std::string path = FioGetDirectory(sp, LANG_DIR);
GetLanguageList(path.c_str()); GetLanguageList(path.c_str());
} }