src/fileio.h
author Tero Marttila <terom@fixme.fi>
Tue, 22 Jul 2008 18:29:06 +0300
changeset 11179 fa96e29d7187
parent 11061 f5806d84e7a9
permissions -rw-r--r--
error messages, typos/omissions, and it can now actually download the .tar into cache/<GRFID>-<MD5SUM>.tar
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1198
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1198
diff changeset
     2
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6505
diff changeset
     3
/** @file fileio.h Declarations for Standard In/Out file operations */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5726
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
8759
eae56497cf83 (svn r11827) -Codechange: do not include enum_type.hpp unnecessary.
rubidium
parents: 8627
diff changeset
     8
#include "core/enum_type.hpp"
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
     9
10751
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10465
diff changeset
    10
void FioSeekTo(size_t pos, int mode);
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10465
diff changeset
    11
void FioSeekToFile(uint8 slot, size_t pos);
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10465
diff changeset
    12
size_t FioGetPos();
8870
461ed7760525 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8759
diff changeset
    13
const char *FioGetFilename(uint8 slot);
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
    14
byte FioReadByte();
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
    15
uint16 FioReadWord();
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
    16
uint32 FioReadDword();
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
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);
10465
0c68afe3d725 (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 8870
diff changeset
    19
void FioReadBlock(void *ptr, size_t size);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
void FioSkipBytes(int n);
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    21
void FioCreateDirectory(const char *filename);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6573
diff changeset
    22
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    23
/**
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    24
 * The different kinds of subdirectories OpenTTD uses
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    25
 */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    26
enum Subdirectory {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    27
	BASE_DIR,      ///< Base directory for all subdirectories
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    28
	SAVE_DIR,      ///< Base directory for all savegames
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    29
	AUTOSAVE_DIR,  ///< Subdirectory of save for autosaves
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    30
	SCENARIO_DIR,  ///< Base directory for all scenarios
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    31
	HEIGHTMAP_DIR, ///< Subdirectory of scenario for heightmaps
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    32
	GM_DIR,        ///< Subdirectory for all music
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    33
	DATA_DIR,      ///< Subdirectory for all data (GRFs, sample.cat, intro game)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    34
	LANG_DIR,      ///< Subdirectory for all translation files
11179
fa96e29d7187 error messages, typos/omissions, and it can now actually download the .tar into cache/<GRFID>-<MD5SUM>.tar
Tero Marttila <terom@fixme.fi>
parents: 11061
diff changeset
    35
    CACHE_DIR,     ///< Subdirectory for all cache data (not user-visible)
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    36
	NUM_SUBDIRS,   ///< Number of subdirectories
7431
f340111a55ec (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 7425
diff changeset
    37
	NO_DIRECTORY,  ///< A path without any base directory
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    38
};
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    39
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    40
/**
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    41
 * Types of searchpaths OpenTTD might use
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    42
 */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    43
enum Searchpath {
7440
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    44
	SP_FIRST_DIR,
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    45
	SP_WORKING_DIR = SP_FIRST_DIR, ///< Search in the working directory
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    46
	SP_PERSONAL_DIR,               ///< Search in the personal directory
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    47
	SP_SHARED_DIR,                 ///< Search in the shared directory, like 'Shared Files' under Windows
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    48
	SP_BINARY_DIR,                 ///< Search in the directory where the binary resides
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    49
	SP_INSTALLATION_DIR,           ///< Search in the installation directory
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    50
	SP_APPLICATION_BUNDLE_DIR,     ///< Search within the application bundle
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    51
	NUM_SEARCHPATHS
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    52
};
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    53
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    54
DECLARE_POSTFIX_INCREMENT(Searchpath);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    55
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    56
/**
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    57
 * The searchpaths OpenTTD could search through.
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    58
 * At least one of the slots has to be filled with a path.
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    59
 * NULL paths tell that there is no such path for the
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    60
 * current operating system.
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    61
 */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    62
extern const char *_searchpaths[NUM_SEARCHPATHS];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    63
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    64
/**
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    65
 * Checks whether the given search path is a valid search path
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    66
 * @param sp the search path to check
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    67
 * @return true if the search path is valid
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    68
 */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    69
static inline bool IsValidSearchPath(Searchpath sp)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    70
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    71
	return sp < NUM_SEARCHPATHS && _searchpaths[sp] != NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    72
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    73
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    74
/** Iterator for all the search paths */
7440
abd68dd9c015 (svn r10198) -Fix (r10194): did not search all search paths.
rubidium
parents: 7437
diff changeset
    75
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    76
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8080
diff changeset
    77
void FioFCloseFile(FILE *f);
8070
959633559d9f (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 8066
diff changeset
    78
FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR, size_t *filesize = NULL);
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    79
bool FioCheckFileExists(const char *filename, Subdirectory subdir = DATA_DIR);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    80
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    81
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    82
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    83
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    84
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    85
static inline const char *FioGetSubdirectory(Subdirectory subdir)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    86
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    87
	extern const char *_subdirs[NUM_SUBDIRS];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    88
	assert(subdir < NUM_SUBDIRS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    89
	return _subdirs[subdir];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    90
}
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6573
diff changeset
    91
7371
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6643
diff changeset
    92
void SanitizeFilename(char *filename);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6573
diff changeset
    93
void AppendPathSeparator(char *buf, size_t buflen);
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6624
diff changeset
    94
void DeterminePaths(const char *exe);
8627
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8626
diff changeset
    95
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8626
diff changeset
    96
bool FileExists(const char *filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    98
extern char *_personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    99
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
#endif /* FILEIO_H */