00001
00002
00005 #ifndef FILEIO_H
00006 #define FILEIO_H
00007
00008 #include <map>
00009 #include <string>
00010 #include "core/enum_type.hpp"
00011
00012 void FioSeekTo(uint32 pos, int mode);
00013 void FioSeekToFile(uint8 slot, uint32 pos);
00014 uint32 FioGetPos();
00015 const char *FioGetFilename(uint8 slot);
00016 byte FioReadByte();
00017 uint16 FioReadWord();
00018 uint32 FioReadDword();
00019 void FioCloseAll();
00020 void FioOpenFile(int slot, const char *filename);
00021 void FioReadBlock(void *ptr, uint size);
00022 void FioSkipBytes(int n);
00023 void FioCreateDirectory(const char *filename);
00024
00028 enum Subdirectory {
00029 BASE_DIR,
00030 SAVE_DIR,
00031 AUTOSAVE_DIR,
00032 SCENARIO_DIR,
00033 HEIGHTMAP_DIR,
00034 GM_DIR,
00035 DATA_DIR,
00036 LANG_DIR,
00037 NUM_SUBDIRS,
00038 NO_DIRECTORY,
00039 };
00040
00044 enum Searchpath {
00045 SP_FIRST_DIR,
00046 SP_WORKING_DIR = SP_FIRST_DIR,
00047 SP_PERSONAL_DIR,
00048 SP_SHARED_DIR,
00049 SP_BINARY_DIR,
00050 SP_INSTALLATION_DIR,
00051 SP_APPLICATION_BUNDLE_DIR,
00052 NUM_SEARCHPATHS
00053 };
00054
00055 DECLARE_POSTFIX_INCREMENT(Searchpath);
00056
00063 extern const char *_searchpaths[NUM_SEARCHPATHS];
00064
00068 struct TarListEntry {
00069 const char *filename;
00070 TarListEntry() : filename(NULL) {}
00071 ~TarListEntry() { free((void*)this->filename); }
00072 };
00073 struct TarFileListEntry {
00074 const char *tar_filename;
00075 int size;
00076 int position;
00077 };
00078 typedef std::map<std::string, TarListEntry> TarList;
00079 typedef std::map<std::string, TarFileListEntry> TarFileList;
00080 extern TarList _tar_list;
00081 extern TarFileList _tar_filelist;
00082
00088 static inline bool IsValidSearchPath(Searchpath sp)
00089 {
00090 return sp < NUM_SEARCHPATHS && _searchpaths[sp] != NULL;
00091 }
00092
00094 #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
00095 #define FOR_ALL_TARS(tar) for (tar = _tar_filelist.begin(); tar != _tar_filelist.end(); tar++)
00096
00097 typedef bool FioTarFileListCallback(const char *filename, int size, void *userdata);
00098 FILE *FioTarFileList(const char *tar, const char *mode, size_t *filesize, FioTarFileListCallback *callback, void *userdata);
00099
00100 void FioFCloseFile(FILE *f);
00101 FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR, size_t *filesize = NULL);
00102 bool FioCheckFileExists(const char *filename, Subdirectory subdir = DATA_DIR);
00103 char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
00104 char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
00105 char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir);
00106 char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir);
00107
00108 static inline const char *FioGetSubdirectory(Subdirectory subdir)
00109 {
00110 extern const char *_subdirs[NUM_SUBDIRS];
00111 assert(subdir < NUM_SUBDIRS);
00112 return _subdirs[subdir];
00113 }
00114
00115 void SanitizeFilename(char *filename);
00116 void AppendPathSeparator(char *buf, size_t buflen);
00117 void DeterminePaths(const char *exe);
00118 void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
00119 bool FileExists(const char *filename);
00120
00121 extern char *_personal_dir;
00122
00123 #endif