The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filesystem.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Declarations for File-IO.
18  */
19 
20 #ifndef FILESYSTEM_HPP_INCLUDED
21 #define FILESYSTEM_HPP_INCLUDED
22 
23 #include <time.h>
24 
25 #include <iosfwd>
26 #include <string>
27 #include <vector>
28 
29 #include "exceptions.hpp"
30 
31 class config;
32 
33 struct SDL_RWops;
34 
35 namespace filesystem {
36 
37 SDL_RWops* load_RWops(const std::string &path);
38 
39 /** An exception object used when an IO error occurs */
40 struct io_exception : public game::error {
41  io_exception() : game::error("") {}
42  io_exception(const std::string& msg) : game::error(msg) {}
43 };
44 
45 struct file_tree_checksum;
46 
50 
51 /**
52  * Populates 'files' with all the files and
53  * 'dirs' with all the directories in dir.
54  * If files or dirs are nullptr they will not be used.
55  *
56  * mode: determines whether the entire path or just the filename is retrieved.
57  * filter: determines if we skip images and sounds directories
58  * reorder: triggers the special handling of _main.cfg and _final.cfg
59  * checksum: can be used to store checksum info
60  */
61 void get_files_in_dir(const std::string &dir,
62  std::vector<std::string>* files,
63  std::vector<std::string>* dirs=nullptr,
67  file_tree_checksum* checksum = nullptr);
68 
69 std::string get_dir(const std::string &dir);
70 
71 // The location of various important files:
79 
80 /**
81  * Get the next free filename using "name + number (3 digits) + extension"
82  * maximum 1000 files then start always giving 999
83  */
84 std::string get_next_filename(const std::string& name, const std::string& extension);
87 
91 
94 
95 bool make_directory(const std::string& dirname);
96 bool delete_directory(const std::string& dirname, const bool keep_pbl = false);
97 bool delete_file(const std::string &filename);
98 
99 bool looks_like_pbl(const std::string& file);
100 
101 // Basic disk I/O:
102 
103 /** Basic disk I/O - read file. */
104 std::string read_file(const std::string &fname);
105 std::istream *istream_file(const std::string &fname, bool treat_failure_as_error = true);
106 std::ostream *ostream_file(std::string const &fname, bool create_directory = true);
107 /** Throws io_exception if an error occurs. */
108 void write_file(const std::string& fname, const std::string& data);
109 
111 
112 /**
113  * Creates a directory if it does not exist already.
114  *
115  * @param dirname Path to directory. All parents should exist.
116  * @returns True if the directory exists or could be
117  * successfully created; false otherwise.
118  */
119 bool create_directory_if_missing(const std::string& dirname);
120 /**
121  * Creates a recursive directory tree if it does not exist already
122  * @param dirname Full path of target directory. Non existing parents
123  * will be created
124  * @return True if the directory exists or could be
125  * successfully created; false otherwise.
126  */
128 
129 /** Returns true if the given file is a directory. */
130 bool is_directory(const std::string& fname);
131 
132 /** Returns true if a file or directory with such name already exists. */
133 bool file_exists(const std::string& name);
134 
135 /** Get the modification time of a file. */
136 time_t file_modified_time(const std::string& fname);
137 
138 /** Returns true if the file ends with '.gz'. */
139 bool is_gzip_file(const std::string& filename);
140 
141 /** Returns true if the file ends with '.bz2'. */
142 bool is_bzip2_file(const std::string& filename);
143 
144 inline bool is_compressed_file(const std::string& filename) {
145  return is_gzip_file(filename) || is_bzip2_file(filename);
146 }
147 
149 {
151  explicit file_tree_checksum(const config& cfg);
152  void write(config& cfg) const;
153  void reset() {nfiles = 0;modified = 0;sum_size=0;}
154  // @todo make variables private!
155  size_t nfiles, sum_size;
156  time_t modified;
157  bool operator==(const file_tree_checksum &rhs) const;
158  bool operator!=(const file_tree_checksum &rhs) const
159  { return !operator==(rhs); }
160 };
161 
162 /** Get the time at which the data/ tree was last modified at. */
163 const file_tree_checksum& data_tree_checksum(bool reset = false);
164 
165 /** Returns the size of a file, or -1 if the file doesn't exist. */
166 int file_size(const std::string& fname);
167 
168 /** Returns the sum of the sizes of the files contained in a directory. */
169 int dir_size(const std::string& path);
170 
171 bool ends_with(const std::string& str, const std::string& suffix);
172 
173 /**
174  * Returns the base filename of a file, with directory name stripped.
175  * Equivalent to a portable basename() function.
176  */
177 std::string base_name(const std::string& file);
178 
179 /**
180  * Returns the directory name of a file, with filename stripped.
181  * Equivalent to a portable dirname()
182  */
184 
185 /**
186  * Returns the absolute path of a file.
187  */
189 
190 /**
191  * Returns whether @a c is a path separator.
192  *
193  * @note / is always a path separator. Additionally, on Windows \\ is a
194  * path separator as well.
195  */
196 bool is_path_sep(char c);
197 
198 /**
199  * The paths manager is responsible for recording the various paths
200  * that binary files may be located at.
201  * It should be passed a config object which holds binary path information.
202  * This is in the format
203  *@verbatim
204  * [binary_path]
205  * path=<path>
206  * [/binary_path]
207  * Binaries will be searched for in [wesnoth-path]/data/<path>/images/
208  *@endverbatim
209  */
211 {
213  binary_paths_manager(const config& cfg);
215 
216  void set_paths(const config& cfg);
217 
218 private:
221 
222  void cleanup();
223 
224  std::vector<std::string> paths_;
225 };
226 
228 
229 /**
230  * Returns a vector with all possible paths to a given type of binary,
231  * e.g. 'images', 'sounds', etc,
232  */
233 const std::vector<std::string>& get_binary_paths(const std::string& type);
234 
235 /**
236  * Returns a complete path to the actual file of a given @a type
237  * or an empty string if the file isn't present.
238  */
240 
241 /**
242  * Returns a complete path to the actual directory of a given @a type
243  * or an empty string if the directory isn't present.
244  */
246 
247 /**
248  * Returns a complete path to the actual WML file or directory
249  * or an empty string if the file isn't present.
250  */
252  const std::string &current_dir = std::string());
253 
254 /**
255  * Returns a short path to @a filename, skipping the (user) data directory.
256  */
258 
259 /**
260  * Returns an image path to @a filename for binary path-independent use in saved games.
261  *
262  * Example:
263  * units/konrad-fighter.png ->
264  * data/campaigns/Heir_To_The_Throne/images/units/konrad-fighter.png
265  */
267 
268 /**
269  * Returns the appropriate invocation for a Wesnoth-related binary, assuming
270  * that it is located in the same directory as the running wesnoth binary.
271  * This is just a string-transformation based on argv[0], so the returned
272  * program is not guaranteed to actually exist. '-debug' variants are handled
273  * correctly.
274  */
275 std::string get_program_invocation(const std::string &program_name);
276 
278  std::istream *stream;
279 public:
280  scoped_istream(std::istream *s): stream(s) {}
281  scoped_istream& operator=(std::istream *);
282  std::istream &operator*() { return *stream; }
283  std::istream *operator->() { return stream; }
284  ~scoped_istream();
285 };
286 
288  std::ostream *stream;
289 public:
290  scoped_ostream(std::ostream *s): stream(s) {}
291  scoped_ostream& operator=(std::ostream *);
292  std::ostream &operator*() { return *stream; }
293  std::ostream *operator->() { return stream; }
294  ~scoped_ostream();
295 };
296 
297 }
298 
299 #endif
std::string get_binary_dir_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual directory of a given type or an empty string if the directory i...
std::string get_program_invocation(const std::string &program_name)
Returns the appropriate invocation for a Wesnoth-related binary, assuming that it is located in the s...
void set_user_data_dir(std::string path)
void set_paths(const config &cfg)
std::string get_next_filename(const std::string &name, const std::string &extension)
Get the next free filename using "name + number (3 digits) + extension" maximum 1000 files then start...
bool delete_file(const std::string &filename)
bool looks_like_pbl(const std::string &file)
scoped_istream & operator=(std::istream *)
const GLfloat * c
Definition: glew.h:12741
std::istream & operator*()
Definition: filesystem.hpp:282
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
bool ends_with(const std::string &str, const std::string &suffix)
The paths manager is responsible for recording the various paths that binary files may be located at...
Definition: filesystem.hpp:210
std::string get_screenshot_dir()
scoped_ostream(std::ostream *s)
Definition: filesystem.hpp:290
std::string get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type or an empty string if the file isn't prese...
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
GLenum mode
Definition: glew.h:2390
std::string get_saves_dir()
io_exception(const std::string &msg)
Definition: filesystem.hpp:42
void clear_binary_paths_cache()
std::string get_cwd()
GLsizei const char ** path
Definition: glew.h:4654
bool create_directory_if_missing_recursive(const std::string &dirname)
Creates a recursive directory tree if it does not exist already.
bool create_directory_if_missing(const std::string &dirname)
Creates a directory if it does not exist already.
std::string get_user_data_dir()
std::string get_intl_dir()
std::string normalize_path(const std::string &path)
Returns the absolute path of a file.
void write_file(const std::string &fname, const std::string &data)
Throws io_exception if an error occurs.
std::istream * operator->()
Definition: filesystem.hpp:283
std::string base_name(const std::string &file)
Returns the base filename of a file, with directory name stripped.
std::istream * istream_file(const std::string &fname, bool treat_failure_as_error=true)
scoped_ostream & operator=(std::ostream *)
std::ostream * ostream_file(std::string const &fname, bool create_directory=true)
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
std::string get_short_wml_path(const std::string &filename)
Returns a short path to filename, skipping the (user) data directory.
std::string get_independent_image_path(const std::string &filename)
Returns an image path to filename for binary path-independent use in saved games. ...
std::string get_dir(const std::string &dir)
std::string get_default_prefs_file()
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
std::string read_map(const std::string &name)
bool is_path_sep(char c)
Returns whether c is a path separator.
GLboolean reset
Definition: glew.h:3799
bool is_gzip_file(const std::string &filename)
Returns true if the file ends with '.gz'.
std::string get_cache_dir()
const std::vector< std::string > & get_binary_paths(const std::string &type)
Returns a vector with all possible paths to a given type of binary, e.g.
time_t file_modified_time(const std::string &fname)
Get the modification time of a file.
std::string get_exe_dir()
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs=nullptr, file_name_option mode=FILE_NAME_ONLY, file_filter_option filter=NO_FILTER, file_reorder_option reorder=DONT_REORDER, file_tree_checksum *checksum=nullptr)
Populates 'files' with all the files and 'dirs' with all the directories in dir.
int dir_size(const std::string &path)
Returns the sum of the sizes of the files contained in a directory.
void set_user_config_dir(std::string path)
std::string get_wml_location(const std::string &filename, const std::string &current_dir=std::string())
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
std::vector< std::string > paths_
Definition: filesystem.hpp:224
An exception object used when an IO error occurs.
Definition: filesystem.hpp:40
bool make_directory(const std::string &dirname)
bool is_compressed_file(const std::string &filename)
Definition: filesystem.hpp:144
void write(config &cfg) const
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
bool operator!=(const file_tree_checksum &rhs) const
Definition: filesystem.hpp:158
bool delete_directory(const std::string &dirname, const bool keep_pbl=false)
const file_tree_checksum & data_tree_checksum(bool reset=false)
Get the time at which the data/ tree was last modified at.
int file_size(const std::string &fname)
Returns the size of a file, or -1 if the file doesn't exist.
GLuint const GLchar * name
Definition: glew.h:1782
std::ostream * operator->()
Definition: filesystem.hpp:293
std::string get_user_config_dir()
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glew.h:3448
bool is_bzip2_file(const std::string &filename)
Returns true if the file ends with '.bz2'.
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
std::string get_addons_dir()
std::ostream & operator*()
Definition: filesystem.hpp:292
binary_paths_manager & operator=(const binary_paths_manager &o)
SDL_RWops * load_RWops(const std::string &path)
std::string get_prefs_file()
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLdouble s
Definition: glew.h:1358
bool operator==(const file_tree_checksum &rhs) const
std::string get_save_index_file()
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
scoped_istream(std::istream *s)
Definition: filesystem.hpp:280
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.