OpenTTD Source  12.0-beta2
fileio_func.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef FILEIO_FUNC_H
11 #define FILEIO_FUNC_H
12 
13 #include "core/enum_type.hpp"
14 #include "fileio_type.h"
15 #include <string>
16 #include <vector>
17 
18 void FioFCloseFile(FILE *f);
19 FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
20 bool FioCheckFileExists(const std::string &filename, Subdirectory subdir);
21 std::string FioFindFullPath(Subdirectory subdir, const char *filename);
22 std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
23 std::string FioFindDirectory(Subdirectory subdir);
24 void FioCreateDirectory(const std::string &name);
25 
26 const char *FiosGetScreenshotDir();
27 
28 void SanitizeFilename(char *filename);
29 void AppendPathSeparator(std::string &buf);
30 void DeterminePaths(const char *exe, bool only_local_path);
31 std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize);
32 bool FileExists(const std::string &filename);
33 bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
34 
35 extern std::string _personal_dir;
36 extern std::vector<Searchpath> _valid_searchpaths;
37 
39 class FileScanner {
40 protected:
42 public:
44  virtual ~FileScanner() {}
45 
46  uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
47  uint Scan(const char *extension, const char *directory, bool recursive = true);
48 
57  virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) = 0;
58 };
59 
62  uint DoScan(Subdirectory sd);
63 public:
65  enum Mode {
66  NONE = 0,
67  BASESET = 1 << 0,
68  NEWGRF = 1 << 1,
69  AI = 1 << 2,
70  SCENARIO = 1 << 3,
71  GAME = 1 << 4,
73  };
74 
75  bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = {}) override;
76 
77  bool AddFile(Subdirectory sd, const std::string &filename);
78 
80  static uint DoScan(TarScanner::Mode mode);
81 };
82 
84 
85 /* Implementation of opendir/readdir/closedir for Windows */
86 #if defined(_WIN32)
87 struct DIR;
88 
89 struct dirent { // XXX - only d_name implemented
90  wchar_t *d_name; // name of found file
91  /* little hack which will point to parent DIR struct which will
92  * save us a call to GetFileAttributes if we want information
93  * about the file (for example in function fio_bla) */
94  DIR *dir;
95 };
96 
97 DIR *opendir(const wchar_t *path);
98 struct dirent *readdir(DIR *d);
99 int closedir(DIR *d);
100 #else
101 /* Use system-supplied opendir/readdir/closedir functions */
102 # include <sys/types.h>
103 # include <dirent.h>
104 #endif /* defined(_WIN32) */
105 
113 static inline DIR *ttd_opendir(const char *path)
114 {
115  return opendir(OTTD2FS(path).c_str());
116 }
117 
118 
120 class FileCloser {
121  FILE *f;
122 
123 public:
124  FileCloser(FILE *_f) : f(_f) {}
125  ~FileCloser()
126  {
127  fclose(f);
128  }
129 };
130 
132 struct FileDeleter {
133  void operator()(FILE *f)
134  {
135  if (f) fclose(f);
136  }
137 };
138 
139 #endif /* FILEIO_FUNC_H */
TarScanner::ALL
@ ALL
Scan for everything.
Definition: fileio_func.h:72
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:130
TarScanner::DoScan
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition: fileio.cpp:410
FioCreateDirectory
void FioCreateDirectory(const std::string &name)
Create a directory with the given name If the parent directory does not exist, it will try to create ...
Definition: fileio.cpp:331
SanitizeFilename
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1089
FileScanner::~FileScanner
virtual ~FileScanner()
Destruct the proper one...
Definition: fileio_func.h:44
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:141
Searchpath
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:131
FileScanner::Scan
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition: fileio.cpp:1216
FioCheckFileExists
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:108
AppendPathSeparator
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition: fileio.cpp:361
FileCloser
Auto-close a file upon scope exit.
Definition: fileio_func.h:120
FileScanner::AddFile
virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)=0
Add a file with the given filename.
FileScanner::subdir
Subdirectory subdir
The current sub directory we are searching through.
Definition: fileio_func.h:41
TarScanner::NEWGRF
@ NEWGRF
Scan for non-base sets.
Definition: fileio_func.h:68
ReadFileToMem
std::unique_ptr< char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
Load a file into memory.
Definition: fileio.cpp:1111
TarScanner::NONE
@ NONE
Scan nothing.
Definition: fileio_func.h:66
TarScanner::GAME
@ GAME
Scan for game scripts.
Definition: fileio_func.h:71
TarScanner::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename={}) override
Add a file with the given filename.
Definition: fileio.cpp:458
FileExists
bool FileExists(const std::string &filename)
Test whether the given filename exists.
Definition: fileio.cpp:122
DeterminePaths
void DeterminePaths(const char *exe, bool only_local_path)
Acquire the base paths (personal dir and game data dir), fill all other paths (save dir,...
Definition: fileio.cpp:966
TarScanner::SCENARIO
@ SCENARIO
Scan for scenarios and heightmaps.
Definition: fileio_func.h:70
DIR
Definition: win32.cpp:68
_personal_dir
std::string _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition: fileio.cpp:957
fileio_type.h
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
AI
Main AI class.
Definition: ai.hpp:24
enum_type.hpp
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:29
FileDeleter
Helper to manage a FILE with a std::unique_ptr.
Definition: fileio_func.h:132
TarScanner
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:61
ExtractTar
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition: fileio.cpp:683
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:39
TarScanner::BASESET
@ BASESET
Scan for base sets.
Definition: fileio_func.h:67
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize=nullptr)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:245
ttd_opendir
static DIR * ttd_opendir(const char *path)
A wrapper around opendir() which will convert the string from OPENTTD encoding to that of the filesys...
Definition: fileio_func.h:113
TarScanner::Mode
Mode
The mode of tar scanning.
Definition: fileio_func.h:65
OTTD2FS
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:560
FiosGetScreenshotDir
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:625