OpenTTD Source: src/fileio_func.h Source File

OpenTTD Source 1.11.0-RC1

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
17 void FioSeekTo(size_t pos, int mode);
18 void FioSeekToFile(uint8 slot, size_t pos);
19 size_t FioGetPos();
20 const char *FioGetFilename(uint8 slot);
21 byte FioReadByte();
22 uint16 FioReadWord();
23 uint32 FioReadDword();
24 void FioCloseAll();
25 void FioOpenFile(int slot, const std::string &filename, Subdirectory subdir);
26 void FioReadBlock(void *ptr, size_t size);
27 void FioSkipBytes(int n);
28
34 bool IsValidSearchPath(Searchpath sp);
35
37 #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
38
39 void FioFCloseFile(FILE *f);
40 FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
41 bool FioCheckFileExists(const std::string &filename, Subdirectory subdir);
42 std::string FioFindFullPath(Subdirectory subdir, const char *filename);
43 std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
44 std::string FioFindDirectory(Subdirectory subdir);
45 void FioCreateDirectory(const std::string &name);
46
47 const char *FiosGetScreenshotDir();
48
49 void SanitizeFilename(char *filename);
50 void AppendPathSeparator(std::string &buf);
51 void DeterminePaths(const char *exe);
52 std::unique_ptr<char> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize);
53 bool FileExists(const std::string &filename);
54 bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
55
56 extern std::string _personal_dir;
57
59 class FileScanner {
60 protected:
61 Subdirectory subdir;
62 public:
64 virtual ~FileScanner() {}
65
66 uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
67 uint Scan(const char *extension, const char *directory, bool recursive = true);
68
77 virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) = 0;
78 };
79
81 class TarScanner : FileScanner {
82 uint DoScan(Subdirectory sd);
83 public:
85 enum Mode {
86 NONE = 0,
87 BASESET = 1 << 0,
88 NEWGRF = 1 << 1,
89 AI = 1 << 2,
90 SCENARIO = 1 << 3,
91 GAME = 1 << 4,
92 ALL = BASESET | NEWGRF | AI | SCENARIO | GAME,
93 };
94
95 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = {}) override;
96
97 bool AddFile(Subdirectory sd, const std::string &filename);
98
100 static uint DoScan(TarScanner::Mode mode);
101 };
102
103 DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
104
105 /* Implementation of opendir/readdir/closedir for Windows */
106 #if defined(_WIN32)
107 struct DIR;
108
109 struct dirent { // XXX - only d_name implemented
110 wchar_t *d_name; // name of found file
111 /* little hack which will point to parent DIR struct which will
112 * save us a call to GetFileAttributes if we want information
113 * about the file (for example in function fio_bla) */
114 DIR *dir;
115 };
116
117 DIR *opendir(const wchar_t *path);
118 struct dirent *readdir(DIR *d);
119 int closedir(DIR *d);
120 #else
121 /* Use system-supplied opendir/readdir/closedir functions */
122 # include <sys/types.h>
123 # include <dirent.h>
124 #endif /* defined(_WIN32) */
125
133 static inline DIR *ttd_opendir(const char *path)
134 {
135 return opendir(OTTD2FS(path));
136 }
137
138
140 class FileCloser {
141 FILE *f;
142
143 public:
144 FileCloser(FILE *_f) : f(_f) {}
145 ~FileCloser()
146 {
147 fclose(f);
148 }
149 };
150
152 struct FileDeleter {
153 void operator()(FILE *f)
154 {
155 if (f) fclose(f);
156 }
157 };
158
159 #endif /* FILEIO_FUNC_H */
TarScanner::ALL
@ ALL
Scan for everything.
Definition: fileio_func.h:92
FioCloseAll
void FioCloseAll()
Close all slotted open files.
Definition: fileio.cpp:183
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:288
FioReadDword
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: fileio.cpp:150
FioReadWord
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: fileio.cpp:140
TarScanner::DoScan
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition: fileio.cpp:569
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:490
SanitizeFilename
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1237
FileScanner::~FileScanner
virtual ~FileScanner()
Destruct the proper one...
Definition: fileio_func.h:64
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:299
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:1367
ReadFileToMem
std::unique_ptr< char > ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
Load a file into memory.
Definition: fileio.cpp:1259
FioCheckFileExists
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:266
FioOpenFile
void FioOpenFile(int slot, const std::string &filename, Subdirectory subdir)
Open a slotted file.
Definition: fileio.cpp:196
FioReadByte
byte FioReadByte()
Read a byte from the file.
Definition: fileio.cpp:107
AppendPathSeparator
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition: fileio.cpp:520
FileCloser
Auto-close a file upon scope exit.
Definition: fileio_func.h:140
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.
FioReadBlock
void FioReadBlock(void *ptr, size_t size)
Read a block.
Definition: fileio.cpp:161
FileScanner::subdir
Subdirectory subdir
The current sub directory we are searching through.
Definition: fileio_func.h:61
TarScanner::NEWGRF
@ NEWGRF
Scan for non-base sets.
Definition: fileio_func.h:88
FioGetPos
size_t FioGetPos()
Get position in the current file.
Definition: fileio.cpp:59
TarScanner::NONE
@ NONE
Scan nothing.
Definition: fileio_func.h:86
TarScanner::GAME
@ GAME
Scan for game scripts.
Definition: fileio_func.h:91
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:617
FioSkipBytes
void FioSkipBytes(int n)
Skip n bytes ahead in the file.
Definition: fileio.cpp:124
FileExists
bool FileExists(const std::string &filename)
Test whether the given filename exists.
Definition: fileio.cpp:280
TarScanner::SCENARIO
@ SCENARIO
Scan for scenarios and heightmaps.
Definition: fileio_func.h:90
DeterminePaths
void DeterminePaths(const char *exe)
Acquire the base paths (personal dir and game data dir), fill all other paths (save dir,...
Definition: fileio.cpp:1127
FioSeekTo
void FioSeekTo(size_t pos, int mode)
Seek in the current file.
Definition: fileio.cpp:79
DIR
Definition: win32.cpp:92
IsValidSearchPath
bool IsValidSearchPath(Searchpath sp)
Checks whether the given search path is a valid search path.
Definition: fileio.cpp:255
_personal_dir
std::string _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition: fileio.cpp:1119
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
FioGetFilename
const char * FioGetFilename(uint8 slot)
Get the filename associated with a slot.
Definition: fileio.cpp:69
FileDeleter
Helper to manage a FILE with a std::unique_ptr.
Definition: fileio_func.h:152
TarScanner
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:81
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:845
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:59
TarScanner::BASESET
@ BASESET
Scan for base sets.
Definition: fileio_func.h:87
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:406
FioSeekToFile
void FioSeekToFile(uint8 slot, size_t pos)
Switch to a different file and seek to a position.
Definition: fileio.cpp:94
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:133
TarScanner::Mode
Mode
The mode of tar scanning.
Definition: fileio_func.h:85
OTTD2FS
const wchar_t * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD's encoding to wide characters.
Definition: win32.cpp:580
FiosGetScreenshotDir
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:628