The Battle for Wesnoth  1.15.9+dev
filesystem_common.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #include <fstream>
15 
16 #include "filesystem.hpp"
17 #include "wesconfig.h"
18 
19 #include "config.hpp"
20 #include "game_config.hpp"
21 #include "log.hpp"
24 
25 static lg::log_domain log_filesystem("filesystem");
26 #define LOG_FS LOG_STREAM(info, log_filesystem)
27 #define ERR_FS LOG_STREAM(err, log_filesystem)
28 
29 namespace filesystem
30 {
31 
32 void blacklist_pattern_list::remove_blacklisted_files_and_dirs(std::vector<std::string>& files, std::vector<std::string>& directories) const
33 {
34  files.erase(
35  std::remove_if(files.begin(), files.end(), [this](const std::string& name) { return match_file(name); }),
36  files.end());
37  directories.erase(
38  std::remove_if(directories.begin(), directories.end(), [this](const std::string& name) { return match_dir(name); }),
39  directories.end());
40 }
41 
42 bool blacklist_pattern_list::match_file(const std::string& name) const
43 {
44  return std::any_of(file_patterns_.begin(), file_patterns_.end(),
45  std::bind(&utils::wildcard_string_match, std::ref(name), std::placeholders::_1));
46 }
47 
48 bool blacklist_pattern_list::match_dir(const std::string& name) const
49 {
50  return std::any_of(directory_patterns_.begin(), directory_patterns_.end(),
51  std::bind(&utils::wildcard_string_match, std::ref(name), std::placeholders::_1));
52 }
53 
54 std::string get_prefs_file()
55 {
56  return get_user_config_dir() + "/preferences";
57 }
58 
59 std::string get_credentials_file()
60 {
61  return get_user_config_dir() + "/credentials";
62 }
63 
65 {
66 #ifdef HAS_RELATIVE_DEFPREF
68 #else
70 #endif
71 }
72 
73 std::string get_save_index_file()
74 {
75  return get_user_data_dir() + "/save_index";
76 }
77 
78 std::string get_saves_dir()
79 {
80  const std::string dir_path = get_user_data_dir() + "/saves";
81  return get_dir(dir_path);
82 }
83 
84 std::string get_addons_dir()
85 {
86  const std::string dir_path = get_user_data_dir() + "/data/add-ons";
87  return get_dir(dir_path);
88 }
89 
90 std::string get_intl_dir()
91 {
92 #ifdef _WIN32
93  return game_config::path + "/" LOCALEDIR;
94 #else
95 
96 #ifdef USE_INTERNAL_DATA
97  return get_cwd() + "/" LOCALEDIR;
98 #endif
99 
100 #if HAS_RELATIVE_LOCALEDIR
101  std::string res = game_config::path + "/" LOCALEDIR;
102 #else
103  std::string res = LOCALEDIR;
104 #endif
105 
106  return res;
107 #endif
108 }
109 
110 std::string get_screenshot_dir()
111 {
112  const std::string dir_path = get_user_data_dir() + "/screenshots";
113  return get_dir(dir_path);
114 }
115 
116 bool looks_like_pbl(const std::string& file)
117 {
118  return utils::wildcard_string_match(utf8::lowercase(file), "*.pbl");
119 }
120 
122  : nfiles(0), sum_size(0), modified(0)
123 {}
124 
126  nfiles (cfg["nfiles"].to_size_t()),
127  sum_size(cfg["size"].to_size_t()),
128  modified(cfg["modified"].to_time_t())
129 {
130 }
131 
133 {
134  cfg["nfiles"] = nfiles;
135  cfg["size"] = sum_size;
136  cfg["modified"] = modified;
137 }
138 
140 {
141  return nfiles == rhs.nfiles && sum_size == rhs.sum_size &&
142  modified == rhs.modified;
143 }
144 
145 bool ends_with(const std::string& str, const std::string& suffix)
146 {
147  return str.size() >= suffix.size() && std::equal(suffix.begin(),suffix.end(),str.end()-suffix.size());
148 }
149 
150 std::string read_map(const std::string& name)
151 {
152  std::string res;
153  std::string map_location = get_wml_location(name);
154  if(map_location.empty()) {
155  // If this is an add-on or campaign that's set the [binary_path] for its image directory,
156  // automatically check for a sibling maps directory.
157  map_location = get_binary_file_location("maps", name);
158  }
159  if(!map_location.empty()) {
160  res = read_file(map_location);
161  }
162 
163  if(res.empty()) {
164  res = read_file(get_user_data_dir() + "/editor/maps/" + name);
165  }
166 
167  return res;
168 }
169 
170 static void get_file_tree_checksum_internal(const std::string& path, file_tree_checksum& res)
171 {
172 
173  std::vector<std::string> dirs;
175 
176  for(std::vector<std::string>::const_iterator j = dirs.begin(); j != dirs.end(); ++j) {
178  }
179 }
180 
182 {
183  static file_tree_checksum checksum;
184  if (reset)
185  checksum.reset();
186  if(checksum.nfiles == 0) {
187  get_file_tree_checksum_internal("data/",checksum);
188  get_file_tree_checksum_internal(get_user_data_dir() + "/data/",checksum);
189  LOG_FS << "calculated data tree checksum: "
190  << checksum.nfiles << " files; "
191  << checksum.sum_size << " bytes" << std::endl;
192  }
193 
194  return checksum;
195 }
196 
197 }
static void get_file_tree_checksum_internal(const std::string &path, file_tree_checksum &res)
bool looks_like_pbl(const std::string &file)
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 &#39;*&#39; as any number of characters (including none), &#39;+&#39; as one or more characters, and &#39;?&#39; as any one character.
std::string get_screenshot_dir()
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&#39;t prese...
std::string get_saves_dir()
Definitions for the interface to Wesnoth Markup Language (WML).
void remove_blacklisted_files_and_dirs(std::vector< std::string > &files, std::vector< std::string > &directories) const
static bfs::path get_dir(const bfs::path &dirpath)
Definition: filesystem.cpp:274
std::string get_cwd()
Definition: filesystem.cpp:878
bool match_file(const std::string &name) const
std::string get_user_data_dir()
Definition: filesystem.cpp:791
std::vector< std::string > directory_patterns_
Definition: filesystem.hpp:85
std::string get_intl_dir()
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Populates &#39;files&#39; with all the files and &#39;dirs&#39; with all the directories in dir.
Definition: filesystem.cpp:349
std::string path
Definition: game_config.cpp:39
std::vector< std::string > file_patterns_
Definition: filesystem.hpp:84
bool operator==(const file_tree_checksum &rhs) const
std::string get_default_prefs_file()
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
Definition: filesystem.cpp:997
std::string read_map(const std::string &name)
Encapsulates the map of the game.
Definition: location.hpp:37
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
#define LOG_FS
std::string get_wml_location(const std::string &filename, const std::string &current_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn&#39;t pres...
std::string name
Definition: sdl_ttf.cpp:70
Declarations for File-IO.
std::string lowercase(const std::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:51
const file_tree_checksum & data_tree_checksum(bool reset=false)
Get the time at which the data/ tree was last modified at.
std::string get_user_config_dir()
Definition: filesystem.cpp:762
std::string get_addons_dir()
std::string get_credentials_file()
Standard logging facilities (interface).
static lg::log_domain log_filesystem("filesystem")
std::string get_prefs_file()
bool match_dir(const std::string &name) const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
std::string get_save_index_file()
#define LOCALEDIR
Definition: wesconfig.h:19
std::string default_preferences_path
Definition: game_config.cpp:45