summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/files.h
blob: 02aeced5c7b65c7a16e85870559a25e5cdfcdf82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 *  File:       files.h
 *  Summary:    Functions used to save and load levels/games.
 *  Written by: Linley Henzell and Alexey Guzeev
 */


#ifndef FILES_H
#define FILES_H

#include "externs.h"
#include "player.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <set>

enum load_mode_type
{
    LOAD_START_GAME,            // game has just begun
    LOAD_RESTART_GAME,          // loaded savefile
    LOAD_ENTER_LEVEL,           // entered a level for the first time
    LOAD_VISITOR,               // Visitor pattern to see all levels
};

// referenced in files - newgame - ouch - dgn-overview:
#define MAX_LEVELS 50

// referenced in files - newgame - ouch:
typedef std::set<level_id> level_id_set;
extern level_id_set Generated_Levels;

bool file_exists(const std::string &name);
bool dir_exists(const std::string &dir);
bool is_absolute_path(const std::string &path);
bool is_read_safe_path(const std::string &path);
void assert_read_safe_path(const std::string &path) throw (std::string);

std::vector<std::string> get_dir_files(const std::string &dir);
std::vector<std::string> get_dir_files_ext(const std::string &dir,
                                           const std::string &ext);
std::vector<std::string> get_dir_files_recursive(
    const std::string &dirname,
    const std::string &ext = "",
    int recursion_depth = -1,
    bool include_directories = false);

std::string datafile_path(
    std::string basename,
    bool croak_on_fail = true,
    bool test_base_path = false,
    bool (*thing_exists)(const std::string&) = file_exists);


bool get_dos_compatible_file_name(std::string *fname);
std::string get_parent_directory(const std::string &filename);
std::string get_base_filename(const std::string &filename);
std::string get_path_relative_to(const std::string &referencefile,
                                 const std::string &relativepath);
std::string catpath(const std::string &first, const std::string &second);
std::string canonicalise_file_separator(const std::string &path);

bool check_mkdir(const std::string &what, std::string *dir,
                 bool silent = false);

// Find saved games for all game types.
std::vector<player_save_info> find_all_saved_characters();

// Find saved games for the current game type.
std::vector<player_save_info> find_saved_characters();

std::string get_savefile_directory(bool ignore_game_type = false);
std::string get_bonefile_directory(bool ignore_game_type = false);
std::string get_save_filename(const std::string &pre,
                              const std::string &suf,
                              const std::string &ext,
                              bool suppress_uid = false);
std::string get_savedir_filename(const std::string &pre,
                                 const std::string &suf,
                                 const std::string &ext,
                                 bool suppress_uid = false);
std::string get_base_savedir_path(const std::string &subpath = "");
std::string get_savedir_path(const std::string &shortpath);
std::string savedir_versioned_path(const std::string &subdirs = "");
std::string get_prefs_filename();
std::string change_file_extension(const std::string &file,
                                  const std::string &ext);

void file_touch(const std::string &file);
time_t file_modtime(const std::string &file);
bool is_newer(const std::string &a, const std::string &b);
void check_newer(const std::string &target,
                 const std::string &dependency,
                 void (*action)());


class level_id;

bool load(dungeon_feature_type stair_taken, load_mode_type load_mode,
          const level_id& old_level);

void save_game(bool leave_game, const char *bye = NULL);

// Save game without exiting (used when changing levels).
void save_game_state();

bool get_save_version(FILE *file, int &major, int &minor);
bool get_save_version(chunk_reader &file, int &major, int &minor);

bool save_exists(const std::string& name);
void restore_game(const std::string& name);

bool is_existing_level(const level_id &level);

class level_excursion
{
protected:
    level_id original;
    bool ever_changed_levels;

public:
    level_excursion();
    ~level_excursion();

    void go_to(const level_id &level);
};

void save_ghost( bool force = false );
bool load_ghost( bool creating_level );

std::string make_filename(std::string prefix, int level, branch_type branch,
                          level_area_type lt, bool isGhost );

FILE *lk_open(const char *mode, const std::string &file);
void lk_close(FILE *handle, const char *mode, const std::string &file);

// file locking stuff
#ifdef USE_FILE_LOCKING
bool lock_file_handle( FILE *handle, int type );
bool unlock_file_handle( FILE *handle );
#endif // USE_FILE_LOCKING

#ifdef SHARED_FILES_CHMOD_PRIVATE
#define DO_CHMOD_PRIVATE(x) chmod( (x), SHARED_FILES_CHMOD_PRIVATE )
#else
#define DO_CHMOD_PRIVATE(x) // empty command
#endif

class file_lock
{
public:
    file_lock(const std::string &filename, const char *mode,
              bool die_on_fail = true);
    ~file_lock();
private:
    FILE *handle;
    const char *mode;
    std::string filename;
};

class SavefileCallback
{
public:
    typedef void (*callback)(bool saving);

    SavefileCallback(callback func);

    static void pre_save();
    static void post_restore();
};

FILE *fopen_replace(const char *name);
#endif