OpenTTD
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 
16 void FioSeekTo(size_t pos, int mode);
17 void FioSeekToFile(uint8 slot, size_t pos);
18 size_t FioGetPos();
19 const char *FioGetFilename(uint8 slot);
20 byte FioReadByte();
21 uint16 FioReadWord();
22 uint32 FioReadDword();
23 void FioCloseAll();
24 void FioOpenFile(int slot, const char *filename, Subdirectory subdir);
25 void FioReadBlock(void *ptr, size_t size);
26 void FioSkipBytes(int n);
27 
34 extern const char *_searchpaths[NUM_SEARCHPATHS];
35 
41 static inline bool IsValidSearchPath(Searchpath sp)
42 {
43  return sp < NUM_SEARCHPATHS && _searchpaths[sp] != nullptr;
44 }
45 
47 #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
48 
49 void FioFCloseFile(FILE *f);
50 FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
51 bool FioCheckFileExists(const char *filename, Subdirectory subdir);
52 char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename);
53 char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename);
54 char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir);
55 char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir);
56 void FioCreateDirectory(const char *name);
57 
58 const char *FiosGetScreenshotDir();
59 
60 void SanitizeFilename(char *filename);
61 bool AppendPathSeparator(char *buf, const char *last);
62 void DeterminePaths(const char *exe);
63 void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
64 bool FileExists(const char *filename);
65 bool ExtractTar(const char *tar_filename, Subdirectory subdir);
66 
67 extern const char *_personal_dir;
68 
70 class FileScanner {
71 protected:
73 public:
75  virtual ~FileScanner() {}
76 
77  uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
78  uint Scan(const char *extension, const char *directory, bool recursive = true);
79 
88  virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) = 0;
89 };
90 
93  uint DoScan(Subdirectory sd);
94 public:
96  enum Mode {
97  NONE = 0,
98  BASESET = 1 << 0,
99  NEWGRF = 1 << 1,
100  AI = 1 << 2,
101  SCENARIO = 1 << 3,
102  GAME = 1 << 4,
103  ALL = BASESET | NEWGRF | AI | SCENARIO | GAME,
104  };
105 
106  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename = nullptr) override;
107 
108  bool AddFile(Subdirectory sd, const char *filename);
109 
111  static uint DoScan(TarScanner::Mode mode);
112 };
113 
115 
116 /* Implementation of opendir/readdir/closedir for Windows */
117 #if defined(_WIN32)
118 struct DIR;
119 
120 struct dirent { // XXX - only d_name implemented
121  TCHAR *d_name; // name of found file
122  /* little hack which will point to parent DIR struct which will
123  * save us a call to GetFileAttributes if we want information
124  * about the file (for example in function fio_bla) */
125  DIR *dir;
126 };
127 
128 DIR *opendir(const TCHAR *path);
129 struct dirent *readdir(DIR *d);
130 int closedir(DIR *d);
131 #else
132 /* Use system-supplied opendir/readdir/closedir functions */
133 # include <sys/types.h>
134 # include <dirent.h>
135 #endif /* defined(_WIN32) */
136 
144 static inline DIR *ttd_opendir(const char *path)
145 {
146  return opendir(OTTD2FS(path));
147 }
148 
149 
151 class FileCloser {
152  FILE *f;
153 
154 public:
155  FileCloser(FILE *_f) : f(_f) {}
156  ~FileCloser()
157  {
158  fclose(f);
159  }
160 };
161 
162 #endif /* FILEIO_FUNC_H */
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
void FioCreateDirectory(const char *name)
Create a directory with the given name.
Definition: fileio.cpp:532
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
const char * _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition: fileio.cpp:1117
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:310
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:1373
virtual ~FileScanner()
Destruct the proper one...
Definition: fileio_func.h:75
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:92
bool AppendPathSeparator(char *buf, const char *last)
Appends, if necessary, the path separator character to the end of the string.
Definition: fileio.cpp:552
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: fileio.cpp:164
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:131
Type (helpers) for enums.
size_t FioGetPos()
Get position in the current file.
Definition: fileio.cpp:66
virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)=0
Add a file with the given filename.
Main AI class.
Definition: ai.hpp:24
Helper for scanning for files with a given name.
Definition: fileio_func.h:70
void FioOpenFile(int slot, const char *filename, Subdirectory subdir)
Open a slotted file.
Definition: fileio.cpp:248
Types for Standard In/Out file operations.
void FioReadBlock(void *ptr, size_t size)
Read a block.
Definition: fileio.cpp:185
Definition: win32.cpp:92
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize=nullptr)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:463
void FioSeekTo(size_t pos, int mode)
Seek in the current file.
Definition: fileio.cpp:86
void DeterminePaths(const char *exe)
Acquire the base paths (personal dir and game data dir), fill all other paths (save dir...
Definition: fileio.cpp:1125
Auto-close a file upon scope exit.
Definition: fileio_func.h:151
void FioCloseAll()
Close all slotted open files.
Definition: fileio.cpp:211
void FioSeekToFile(uint8 slot, size_t pos)
Switch to a different file and seek to a position.
Definition: fileio.cpp:113
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: fileio.cpp:174
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:576
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1242
char * FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:354
const char * _searchpaths[NUM_SEARCHPATHS]
The search paths OpenTTD could search through.
Definition: fileio.cpp:297
bool ExtractTar(const char *tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition: fileio.cpp:886
Subdirectory subdir
The current sub directory we are searching through.
Definition: fileio_func.h:72
Mode
The mode of tar scanning.
Definition: fileio_func.h:96
bool FileExists(const char *filename)
Test whether the given filename exists.
Definition: fileio.cpp:324
void * ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
Load a file into memory.
Definition: fileio.cpp:1264
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:144
void FioSkipBytes(int n)
Skip n bytes ahead in the file.
Definition: fileio.cpp:148
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:643
static bool IsValidSearchPath(Searchpath sp)
Checks whether the given search path is a valid search path.
Definition: fileio_func.h:41
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:332
const char * FioGetFilename(uint8 slot)
Get the filename associated with a slot.
Definition: fileio.cpp:76
byte FioReadByte()
Read a byte from the file.
Definition: fileio.cpp:131