The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filesystem_common.cpp
Go to the documentation of this file.
1 
2 #include "global.hpp"
3 
4 #include <fstream>
5 
6 #include "filesystem.hpp"
7 
8 #include "config.hpp"
9 #include "game_config.hpp"
10 #include "log.hpp"
13 #include "util.hpp"
14 
15 static lg::log_domain log_filesystem("filesystem");
16 #define LOG_FS LOG_STREAM(info, log_filesystem)
17 #define ERR_FS LOG_STREAM(err, log_filesystem)
18 
19 namespace filesystem {
20 
21 
23 {
24  delete stream;
25  stream = s;
26  return *this;
27 }
28 
30 {
31  delete stream;
32 }
33 
35 {
36  delete stream;
37  stream = s;
38  return *this;
39 }
40 
42 {
43  delete stream;
44 }
45 
46 #ifdef __native_client__
47 // For performance reasons, on NaCl we only keep preferences and saves in persistent storage.
49 {
50  return "/wesnoth-userdata/preferences";
51 }
52 
54 {
55  return "/wesnoth-userdata/save_index";
56 }
57 
59 {
60  const std::string dir_path = "/wesnoth-userdata/saves";
61  return get_dir(dir_path);
62 }
63 
64 #else
65 
67 {
68  return get_user_config_dir() + "/preferences";
69 }
70 
72 {
73 #ifdef HAS_RELATIVE_DEFPREF
75 #else
77 #endif
78 }
79 
81 {
82  return get_user_data_dir() + "/save_index";
83 }
84 
86 {
87  const std::string dir_path = get_user_data_dir() + "/saves";
88  return get_dir(dir_path);
89 }
90 #endif
91 
93 {
94  const std::string dir_path = get_user_data_dir() + "/data/add-ons";
95  return get_dir(dir_path);
96 }
97 
99 {
100 #ifdef _WIN32
101  return get_cwd() + "/translations";
102 #else
103 
104 #ifdef USE_INTERNAL_DATA
105  return get_cwd() + "/" LOCALEDIR;
106 #endif
107 
108 #if HAS_RELATIVE_LOCALEDIR
110 #else
111  std::string res = LOCALEDIR;
112 #endif
113 
114  return res;
115 #endif
116 }
117 
119 {
120  const std::string dir_path = get_user_data_dir() + "/screenshots";
121  return get_dir(dir_path);
122 }
123 
124 bool looks_like_pbl(const std::string& file)
125 {
126  return utils::wildcard_string_match(utf8::lowercase(file), "*.pbl");
127 }
128 
130  : nfiles(0), sum_size(0), modified(0)
131 {}
132 
134  nfiles (cfg["nfiles"].to_size_t()),
135  sum_size(cfg["size"].to_size_t()),
136  modified(cfg["modified"].to_time_t())
137 {
138 }
139 
141 {
142  cfg["nfiles"] = nfiles;
143  cfg["size"] = sum_size;
144  cfg["modified"] = modified;
145 }
146 
148 {
149  return nfiles == rhs.nfiles && sum_size == rhs.sum_size &&
150  modified == rhs.modified;
151 }
152 
153 bool ends_with(const std::string& str, const std::string& suffix)
154 {
155  return str.size() >= suffix.size() && std::equal(suffix.begin(),suffix.end(),str.end()-suffix.size());
156 }
157 
159 {
161  std::string map_location = get_wml_location("maps/" + name);
162  if(!map_location.empty()) {
163  res = read_file(map_location);
164  }
165 
166  if (res.empty()) {
167  res = read_file(get_user_data_dir() + "/editor/maps/" + name);
168  }
169 
170  return res;
171 }
172 
174 {
175 
176  std::vector<std::string> dirs;
177  get_files_in_dir(path,nullptr,&dirs, ENTIRE_FILE_PATH, SKIP_MEDIA_DIR, DONT_REORDER, &res);
178 
179  for(std::vector<std::string>::const_iterator j = dirs.begin(); j != dirs.end(); ++j) {
181  }
182 }
183 
185 {
186  static file_tree_checksum checksum;
187  if (reset)
188  checksum.reset();
189  if(checksum.nfiles == 0) {
190  get_file_tree_checksum_internal("data/",checksum);
191  get_file_tree_checksum_internal(get_user_data_dir() + "/data/",checksum);
192  LOG_FS << "calculated data tree checksum: "
193  << checksum.nfiles << " files; "
194  << checksum.sum_size << " bytes" << std::endl;
195  }
196 
197  return checksum;
198 }
199 
200 }
static void get_file_tree_checksum_internal(const std::string &path, file_tree_checksum &res)
bool looks_like_pbl(const std::string &file)
scoped_istream & operator=(std::istream *)
bool ends_with(const std::string &str, const std::string &suffix)
bool wildcard_string_match(const std::string &str, const std::string &match)
Match using '*' as any number of characters (including none), and '?' as any one character.
utf8::string lowercase(const utf8::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:53
std::string get_screenshot_dir()
std::string get_saves_dir()
Definitions for the interface to Wesnoth Markup Language (WML).
std::string get_cwd()
GLuint GLuint stream
Definition: glew.h:5239
GLsizei const char ** path
Definition: glew.h:4654
std::string get_user_data_dir()
std::string get_intl_dir()
scoped_ostream & operator=(std::ostream *)
std::string path
Templates and utility-routines for strings and numbers.
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)
GLboolean reset
Definition: glew.h:3799
Encapsulates the map of the game.
Definition: location.hpp:38
GLuint res
Definition: glew.h:9258
#define LOG_FS
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.
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...
void write(config &cfg) const
Declarations for File-IO.
const file_tree_checksum & data_tree_checksum(bool reset=false)
Get the time at which the data/ tree was last modified at.
GLuint const GLchar * name
Definition: glew.h:1782
std::string get_user_config_dir()
std::string get_addons_dir()
Standard logging facilities (interface).
static lg::log_domain log_filesystem("filesystem")
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()
GLsizei const GLcharARB ** string
Definition: glew.h:4503
#define LOCALEDIR
Definition: wesconfig.h:19
std::string default_preferences_path