summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglx22 <glx@openttd.org>2021-04-30 15:41:58 +0200
committerLoïc Guilloux <glx22@users.noreply.github.com>2021-05-03 19:46:57 +0200
commit983c7ade60f82a49d8c23c1f295add1fd9a913b0 (patch)
tree879eace14cb5fa56c407fbd3c0b5a04fa6a808e2
parent34215f7faa2de08d767b4f6945443a8f3175acbf (diff)
downloadopenttd-983c7ade60f82a49d8c23c1f295add1fd9a913b0.tar.xz
Codechange: Replace FOR_ALL_SEARCHPATHS with range-based for loops
-rw-r--r--src/fileio.cpp29
-rw-r--r--src/fileio_func.h12
-rw-r--r--src/fios.cpp3
-rw-r--r--src/script/script_instance.cpp3
-rw-r--r--src/strings.cpp4
5 files changed, 22 insertions, 29 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp
index 651fddff9..810f3f34e 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -241,6 +241,7 @@ static_assert(lengthof(_subdirs) == NUM_SUBDIRS);
* current operating system.
*/
std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
+std::vector<Searchpath> _valid_searchpaths;
std::array<TarList, NUM_SUBDIRS> _tar_list;
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
* @return true if the search path is valid
*/
-bool IsValidSearchPath(Searchpath sp)
+static bool IsValidSearchPath(Searchpath sp)
{
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
* @param filename the file to try for existence.
@@ -298,10 +307,9 @@ void FioFCloseFile(FILE *f)
*/
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
{
- Searchpath sp;
assert(subdir < NUM_SUBDIRS);
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
std::string buf = FioGetDirectory(sp, subdir);
buf += filename;
if (FileExists(buf)) return buf;
@@ -326,10 +334,8 @@ std::string FioGetDirectory(Searchpath sp, Subdirectory subdir)
std::string FioFindDirectory(Subdirectory subdir)
{
- Searchpath sp;
-
/* Find and return the first valid directory */
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
std::string ret = FioGetDirectory(sp, subdir);
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 *f = nullptr;
- Searchpath sp;
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
if (f != nullptr || subdir == NO_DIRECTORY) break;
}
@@ -1130,6 +1135,7 @@ std::string _personal_dir;
void DeterminePaths(const char *exe)
{
DetermineBasePaths(exe);
+ FillValidSearchPaths();
#ifdef USE_XDG
std::string config_home;
@@ -1148,8 +1154,7 @@ void DeterminePaths(const char *exe)
AppendPathSeparator(config_home);
#endif
- Searchpath sp;
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;
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 */
_searchpaths[SP_AUTODOWNLOAD_DIR] = _personal_dir + "content_download" PATHSEP;
FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]);
+ FillValidSearchPaths();
/* 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 };
@@ -1368,10 +1374,9 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r
{
this->subdir = sd;
- Searchpath sp;
uint num = 0;
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
/* Don't search in the working directory */
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;
diff --git a/src/fileio_func.h b/src/fileio_func.h
index 142835650..4b9b2dd9e 100644
--- a/src/fileio_func.h
+++ b/src/fileio_func.h
@@ -13,6 +13,7 @@
#include "core/enum_type.hpp"
#include "fileio_type.h"
#include <string>
+#include <vector>
void FioSeekTo(size_t pos, int mode);
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 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);
FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
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);
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 */
class FileScanner {
diff --git a/src/fios.cpp b/src/fios.cpp
index 8528e8c62..3b11bcbe0 100644
--- a/src/fios.cpp
+++ b/src/fios.cpp
@@ -582,8 +582,7 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::s
* collections of NewGRFs or 32 bpp graphics replacement PNGs.
*/
bool match = false;
- Searchpath sp;
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp
index f7f9de4fc..fadbba70d 100644
--- a/src/script/script_instance.cpp
+++ b/src/script/script_instance.cpp
@@ -116,8 +116,7 @@ bool ScriptInstance::LoadCompatibilityScripts(const char *api_version, Subdirect
{
char script_name[32];
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
- Searchpath sp;
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
std::string buf = FioGetDirectory(sp, dir);
buf += script_name;
if (!FileExists(buf)) continue;
diff --git a/src/strings.cpp b/src/strings.cpp
index e340a2b1c..048971459 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -1949,9 +1949,7 @@ static void GetLanguageList(const char *path)
*/
void InitializeLanguagePacks()
{
- Searchpath sp;
-
- FOR_ALL_SEARCHPATHS(sp) {
+ for (Searchpath sp : _valid_searchpaths) {
std::string path = FioGetDirectory(sp, LANG_DIR);
GetLanguageList(path.c_str());
}