src/fileio.h
author rubidium
Wed, 05 Sep 2007 17:53:14 +0000
changeset 7528 fe994ed19d34
parent 6944 53e2a7e57071
child 7570 5d5d9b6af0ef
permissions -rw-r--r--
(svn r11047) -Fix (r11011) [FS#1205]: desync due to wrong cached vehicle length on articulated vehicle construction.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1198
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1198
diff changeset
     2
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6179
diff changeset
     3
/** @file fileio.h Declarations for Standard In/Out file operations */
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5475
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#ifndef FILEIO_H
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     6
#define FILEIO_H
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     7
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
     8
#include "helpers.hpp"
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
     9
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
void FioSeekTo(uint32 pos, int mode);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
void FioSeekToFile(uint32 pos);
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    12
uint32 FioGetPos();
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    13
const char *FioGetFilename();
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    14
byte FioReadByte();
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    15
uint16 FioReadWord();
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    16
uint32 FioReadDword();
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    17
void FioCloseAll();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
void FioOpenFile(int slot, const char *filename);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
void FioReadBlock(void *ptr, uint size);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
void FioSkipBytes(int n);
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    21
void FioCreateDirectory(const char *filename);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6247
diff changeset
    22
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    23
/**
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    24
 * The different kinds of subdirectories OpenTTD uses
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    25
 */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    26
enum Subdirectory {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    27
	BASE_DIR,      ///< Base directory for all subdirectories
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    28
	SAVE_DIR,      ///< Base directory for all savegames
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    29
	AUTOSAVE_DIR,  ///< Subdirectory of save for autosaves
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    30
	SCENARIO_DIR,  ///< Base directory for all scenarios
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    31
	HEIGHTMAP_DIR, ///< Subdirectory of scenario for heightmaps
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    32
	GM_DIR,        ///< Subdirectory for all music
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    33
	DATA_DIR,      ///< Subdirectory for all data (GRFs, sample.cat, intro game)
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    34
	LANG_DIR,      ///< Subdirectory for all translation files
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    35
	NUM_SUBDIRS,   ///< Number of subdirectories
6935
0a2a174d3f6a (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 6929
diff changeset
    36
	NO_DIRECTORY,  ///< A path without any base directory
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    37
};
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    38
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    39
/**
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    40
 * Types of searchpaths OpenTTD might use
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    41
 */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    42
enum Searchpath {
6944
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    43
	SP_FIRST_DIR,
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    44
	SP_WORKING_DIR = SP_FIRST_DIR, ///< Search in the working directory
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    45
	SP_PERSONAL_DIR,               ///< Search in the personal directory
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    46
	SP_SHARED_DIR,                 ///< Search in the shared directory, like 'Shared Files' under Windows
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    47
	SP_BINARY_DIR,                 ///< Search in the directory where the binary resides
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    48
	SP_INSTALLATION_DIR,           ///< Search in the installation directory
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    49
	SP_APPLICATION_BUNDLE_DIR,     ///< Search within the application bundle
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    50
	NUM_SEARCHPATHS
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    51
};
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    52
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    53
DECLARE_POSTFIX_INCREMENT(Searchpath);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    54
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    55
/**
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    56
 * The searchpaths OpenTTD could search through.
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    57
 * At least one of the slots has to be filled with a path.
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    58
 * NULL paths tell that there is no such path for the
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    59
 * current operating system.
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    60
 */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    61
extern const char *_searchpaths[NUM_SEARCHPATHS];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    62
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    63
/**
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    64
 * Checks whether the given search path is a valid search path
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    65
 * @param sp the search path to check
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    66
 * @return true if the search path is valid
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    67
 */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    68
static inline bool IsValidSearchPath(Searchpath sp)
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    69
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    70
	return sp < NUM_SEARCHPATHS && _searchpaths[sp] != NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    71
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    72
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    73
/** Iterator for all the search paths */
6944
53e2a7e57071 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 6941
diff changeset
    74
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    75
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    76
FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    77
bool FioCheckFileExists(const char *filename, Subdirectory subdir = DATA_DIR);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    78
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    79
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    80
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    81
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    82
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    83
static inline const char *FioGetSubdirectory(Subdirectory subdir)
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    84
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    85
	extern const char *_subdirs[NUM_SUBDIRS];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    86
	assert(subdir < NUM_SUBDIRS);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    87
	return _subdirs[subdir];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    88
}
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6247
diff changeset
    89
6875
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6317
diff changeset
    90
void SanitizeFilename(char *filename);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6247
diff changeset
    91
void AppendPathSeparator(char *buf, size_t buflen);
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6298
diff changeset
    92
void DeterminePaths(const char *exe);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    94
extern char *_personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    95
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
#endif /* FILEIO_H */