00001
00002
00003
00004
00005
00006
00007
00008 #ifndef FILES_H
00009 #define FILES_H
00010
00011 #include "externs.h"
00012 #include "player.h"
00013 #include <stdio.h>
00014 #include <string>
00015 #include <vector>
00016 #include <set>
00017
00018 enum load_mode_type
00019 {
00020 LOAD_START_GAME,
00021 LOAD_RESTART_GAME,
00022 LOAD_ENTER_LEVEL,
00023 LOAD_VISITOR,
00024 };
00025
00026
00027 #define MAX_LEVELS 50
00028
00029
00030 typedef std::set<level_id> level_id_set;
00031 extern level_id_set Generated_Levels;
00032
00033 bool file_exists(const std::string &name);
00034 bool dir_exists(const std::string &dir);
00035 bool is_absolute_path(const std::string &path);
00036 bool is_read_safe_path(const std::string &path);
00037 void assert_read_safe_path(const std::string &path) throw (std::string);
00038
00039 std::vector<std::string> get_dir_files(const std::string &dir);
00040 std::vector<std::string> get_dir_files_ext(const std::string &dir,
00041 const std::string &ext);
00042 std::vector<std::string> get_dir_files_recursive(
00043 const std::string &dirname,
00044 const std::string &ext = "",
00045 int recursion_depth = -1,
00046 bool include_directories = false);
00047
00048 std::string datafile_path(
00049 std::string basename,
00050 bool croak_on_fail = true,
00051 bool test_base_path = false,
00052 bool (*thing_exists)(const std::string&) = file_exists);
00053
00054
00055 bool get_dos_compatible_file_name(std::string *fname);
00056 std::string get_parent_directory(const std::string &filename);
00057 std::string get_base_filename(const std::string &filename);
00058 std::string get_path_relative_to(const std::string &referencefile,
00059 const std::string &relativepath);
00060 std::string catpath(const std::string &first, const std::string &second);
00061 std::string canonicalise_file_separator(const std::string &path);
00062
00063 bool check_mkdir(const std::string &what, std::string *dir,
00064 bool silent = false);
00065
00066
00067 std::vector<player_save_info> find_all_saved_characters();
00068
00069
00070 std::vector<player_save_info> find_saved_characters();
00071
00072 std::string get_savefile_directory(bool ignore_game_type = false);
00073 std::string get_bonefile_directory(bool ignore_game_type = false);
00074 std::string get_save_filename(const std::string &pre,
00075 const std::string &suf,
00076 const std::string &ext,
00077 bool suppress_uid = false);
00078 std::string get_savedir_filename(const std::string &pre,
00079 const std::string &suf,
00080 const std::string &ext,
00081 bool suppress_uid = false);
00082 std::string get_base_savedir_path(const std::string &subpath = "");
00083 std::string get_savedir_path(const std::string &shortpath);
00084 std::string savedir_versioned_path(const std::string &subdirs = "");
00085 std::string get_prefs_filename();
00086 std::string change_file_extension(const std::string &file,
00087 const std::string &ext);
00088
00089 void file_touch(const std::string &file);
00090 time_t file_modtime(const std::string &file);
00091 bool is_newer(const std::string &a, const std::string &b);
00092 void check_newer(const std::string &target,
00093 const std::string &dependency,
00094 void (*action)());
00095
00096
00097 class level_id;
00098
00099 bool load(dungeon_feature_type stair_taken, load_mode_type load_mode,
00100 const level_id& old_level);
00101
00102 void save_game(bool leave_game, const char *bye = NULL);
00103
00104
00105 void save_game_state();
00106
00107 bool get_save_version(reader &file, int &major, int &minor);
00108
00109 bool save_exists(const std::string& name);
00110 void restore_game(const std::string& name);
00111
00112 bool is_existing_level(const level_id &level);
00113
00114 class level_excursion
00115 {
00116 protected:
00117 level_id original;
00118 bool ever_changed_levels;
00119
00120 public:
00121 level_excursion();
00122 ~level_excursion();
00123
00124 void go_to(const level_id &level);
00125 };
00126
00127 void save_ghost(bool force = false);
00128 bool load_ghost(bool creating_level);
00129
00130 FILE *lk_open(const char *mode, const std::string &file);
00131 void lk_close(FILE *handle, const char *mode, const std::string &file);
00132
00133
00134 #ifdef USE_FILE_LOCKING
00135 bool lock_file_handle(FILE *handle, bool write);
00136 bool unlock_file_handle(FILE *handle);
00137 #endif // USE_FILE_LOCKING
00138
00139 class file_lock
00140 {
00141 public:
00142 file_lock(const std::string &filename, const char *mode,
00143 bool die_on_fail = true);
00144 ~file_lock();
00145 private:
00146 FILE *handle;
00147 const char *mode;
00148 std::string filename;
00149 };
00150
00151 class SavefileCallback
00152 {
00153 public:
00154 typedef void (*callback)(bool saving);
00155
00156 SavefileCallback(callback func);
00157
00158 static void pre_save();
00159 static void post_restore();
00160 };
00161
00162 FILE *fopen_replace(const char *name);
00163 #endif