forked from wesnoth/wesnoth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.hpp
299 lines (241 loc) · 9.14 KB
/
filesystem.hpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright (C) 2003 - 2014 by David White <[email protected]>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Declarations for File-IO.
*/
#ifndef FILESYSTEM_HPP_INCLUDED
#define FILESYSTEM_HPP_INCLUDED
#include <time.h>
#include <iosfwd>
#include <string>
#include <vector>
#include "exceptions.hpp"
class config;
struct SDL_RWops;
namespace filesystem {
SDL_RWops* load_RWops(const std::string &path);
/** An exception object used when an IO error occurs */
struct io_exception : public game::error {
io_exception() : game::error("") {}
io_exception(const std::string& msg) : game::error(msg) {}
};
struct file_tree_checksum;
enum file_name_option { ENTIRE_FILE_PATH, FILE_NAME_ONLY };
enum file_filter_option { NO_FILTER, SKIP_MEDIA_DIR, SKIP_PBL_FILES };
enum file_reorder_option { DONT_REORDER, DO_REORDER };
/**
* Populates 'files' with all the files and
* 'dirs' with all the directories in dir.
* If files or dirs are NULL they will not be used.
*
* mode: determines whether the entire path or just the filename is retrieved.
* filter: determines if we skip images and sounds directories
* reorder: triggers the special handling of _main.cfg and _final.cfg
* checksum: can be used to store checksum info
*/
void get_files_in_dir(const std::string &dir,
std::vector<std::string>* files,
std::vector<std::string>* dirs=NULL,
file_name_option mode = FILE_NAME_ONLY,
file_filter_option filter = NO_FILTER,
file_reorder_option reorder = DONT_REORDER,
file_tree_checksum* checksum = NULL);
std::string get_dir(const std::string &dir);
// The location of various important files:
std::string get_prefs_file();
std::string get_default_prefs_file();
std::string get_save_index_file();
std::string get_saves_dir();
std::string get_intl_dir();
std::string get_screenshot_dir();
std::string get_addons_dir();
/**
* Get the next free filename using "name + number (3 digits) + extension"
* maximum 1000 files then start always giving 999
*/
std::string get_next_filename(const std::string& name, const std::string& extension);
void set_user_config_dir(std::string path);
void set_user_data_dir(std::string path);
std::string get_user_config_dir();
std::string get_user_data_dir();
std::string get_cache_dir();
std::string get_cwd();
std::string get_exe_dir();
bool make_directory(const std::string& dirname);
bool delete_directory(const std::string& dirname, const bool keep_pbl = false);
bool delete_file(const std::string &filename);
bool looks_like_pbl(const std::string& file);
// Basic disk I/O:
/** Basic disk I/O - read file. */
std::string read_file(const std::string &fname);
std::istream *istream_file(const std::string &fname, bool treat_failure_as_error = true);
std::ostream *ostream_file(std::string const &fname);
/** Throws io_exception if an error occurs. */
void write_file(const std::string& fname, const std::string& data);
std::string read_map(const std::string& name);
/**
* Creates a directory if it does not exist already.
*
* @param dirname Path to directory. All parents should exist.
* @returns True if the directory exists or could be
* successfully created; false otherwise.
*/
bool create_directory_if_missing(const std::string& dirname);
/**
* Creates a recursive directory tree if it does not exist already
* @param dirname Full path of target directory. Non existing parents
* will be created
* @return True if the directory exists or could be
* successfully created; false otherwise.
*/
bool create_directory_if_missing_recursive(const std::string& dirname);
/** Returns true if the given file is a directory. */
bool is_directory(const std::string& fname);
/** Returns true if a file or directory with such name already exists. */
bool file_exists(const std::string& name);
/** Get the modification time of a file. */
time_t file_modified_time(const std::string& fname);
/** Returns true if the file ends with '.gz'. */
bool is_gzip_file(const std::string& filename);
/** Returns true if the file ends with '.bz2'. */
bool is_bzip2_file(const std::string& filename);
inline bool is_compressed_file(const std::string& filename) {
return is_gzip_file(filename) || is_bzip2_file(filename);
}
struct file_tree_checksum
{
file_tree_checksum();
explicit file_tree_checksum(const config& cfg);
void write(config& cfg) const;
void reset() {nfiles = 0;modified = 0;sum_size=0;}
// @todo make variables private!
size_t nfiles, sum_size;
time_t modified;
bool operator==(const file_tree_checksum &rhs) const;
bool operator!=(const file_tree_checksum &rhs) const
{ return !operator==(rhs); }
};
/** Get the time at which the data/ tree was last modified at. */
const file_tree_checksum& data_tree_checksum(bool reset = false);
/** Returns the size of a file, or -1 if the file doesn't exist. */
int file_size(const std::string& fname);
/** Returns the sum of the sizes of the files contained in a directory. */
int dir_size(const std::string& path);
bool ends_with(const std::string& str, const std::string& suffix);
/**
* Returns the base filename of a file, with directory name stripped.
* Equivalent to a portable basename() function.
*/
std::string base_name(const std::string& file);
/**
* Returns the directory name of a file, with filename stripped.
* Equivalent to a portable dirname()
*/
std::string directory_name(const std::string& file);
/**
* Returns the absolute path of a file.
*/
std::string normalize_path(const std::string &path);
/**
* Returns whether @a c is a path separator.
*
* @note / is always a path separator. Additionally, on Windows \\ is a
* path separator as well.
*/
bool is_path_sep(char c);
/**
* The paths manager is responsible for recording the various paths
* that binary files may be located at.
* It should be passed a config object which holds binary path information.
* This is in the format
*@verbatim
* [binary_path]
* path=<path>
* [/binary_path]
* Binaries will be searched for in [wesnoth-path]/data/<path>/images/
*@endverbatim
*/
struct binary_paths_manager
{
binary_paths_manager();
binary_paths_manager(const config& cfg);
~binary_paths_manager();
void set_paths(const config& cfg);
private:
binary_paths_manager(const binary_paths_manager& o);
binary_paths_manager& operator=(const binary_paths_manager& o);
void cleanup();
std::vector<std::string> paths_;
};
void clear_binary_paths_cache();
/**
* Returns a vector with all possible paths to a given type of binary,
* e.g. 'images', 'sounds', etc,
*/
const std::vector<std::string>& get_binary_paths(const std::string& type);
/**
* Returns a complete path to the actual file of a given @a type
* or an empty string if the file isn't present.
*/
std::string get_binary_file_location(const std::string& type, const std::string& filename);
/**
* Returns a complete path to the actual directory of a given @a type
* or an empty string if the directory isn't present.
*/
std::string get_binary_dir_location(const std::string &type, const std::string &filename);
/**
* Returns a complete path to the actual WML file or directory
* or an empty string if the file isn't present.
*/
std::string get_wml_location(const std::string &filename,
const std::string ¤t_dir = std::string());
/**
* Returns a short path to @a filename, skipping the (user) data directory.
*/
std::string get_short_wml_path(const std::string &filename);
/**
* Returns an image path to @a filename for binary path-independent use in saved games.
*
* Example:
* units/konrad-fighter.png ->
* data/campaigns/Heir_To_The_Throne/images/units/konrad-fighter.png
*/
std::string get_independent_image_path(const std::string &filename);
/**
* Returns the appropriate invocation for a Wesnoth-related binary, assuming
* that it is located in the same directory as the running wesnoth binary.
* This is just a string-transformation based on argv[0], so the returned
* program is not guaranteed to actually exist. '-debug' variants are handled
* correctly.
*/
std::string get_program_invocation(const std::string &program_name);
class scoped_istream {
std::istream *stream;
public:
scoped_istream(std::istream *s): stream(s) {}
scoped_istream& operator=(std::istream *);
std::istream &operator*() { return *stream; }
std::istream *operator->() { return stream; }
~scoped_istream();
};
class scoped_ostream {
std::ostream *stream;
public:
scoped_ostream(std::ostream *s): stream(s) {}
scoped_ostream& operator=(std::ostream *);
std::ostream &operator*() { return *stream; }
std::ostream *operator->() { return stream; }
~scoped_ostream();
};
}
#endif