The Battle for Wesnoth  1.15.0+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 std::string get_prefs_file()
43 {
44  return get_user_config_dir() + "/preferences";
45 }
46 
47 std::string get_credentials_file()
48 {
49  return get_user_config_dir() + "/credentials";
50 }
51 
53 {
54 #ifdef HAS_RELATIVE_DEFPREF
56 #else
58 #endif
59 }
60 
61 std::string get_save_index_file()
62 {
63  return get_user_data_dir() + "/save_index";
64 }
65 
66 std::string get_saves_dir()
67 {
68  const std::string dir_path = get_user_data_dir() + "/saves";
69  return get_dir(dir_path);
70 }
71 
72 std::string get_addons_dir()
73 {
74  const std::string dir_path = get_user_data_dir() + "/data/add-ons";
75  return get_dir(dir_path);
76 }
77 
78 std::string get_intl_dir()
79 {
80 #ifdef _WIN32
81  return get_cwd() + "/translations";
82 #else
83 
84 #ifdef USE_INTERNAL_DATA
85  return get_cwd() + "/" LOCALEDIR;
86 #endif
87 
88 #if HAS_RELATIVE_LOCALEDIR
89  std::string res = game_config::path + "/" LOCALEDIR;
90 #else
91  std::string res = LOCALEDIR;
92 #endif
93 
94  return res;
95 #endif
96 }
97 
98 std::string get_screenshot_dir()
99 {
100  const std::string dir_path = get_user_data_dir() + "/screenshots";
101  return get_dir(dir_path);
102 }
103 
104 bool looks_like_pbl(const std::string& file)
105 {
106  return utils::wildcard_string_match(utf8::lowercase(file), "*.pbl");
107 }
108 
110  : nfiles(0), sum_size(0), modified(0)
111 {}
112 
114  nfiles (cfg["nfiles"].to_size_t()),
115  sum_size(cfg["size"].to_size_t()),
116  modified(cfg["modified"].to_time_t())
117 {
118 }
119 
121 {
122  cfg["nfiles"] = nfiles;
123  cfg["size"] = sum_size;
124  cfg["modified"] = modified;
125 }
126 
128 {
129  return nfiles == rhs.nfiles && sum_size == rhs.sum_size &&
130  modified == rhs.modified;
131 }
132 
133 bool ends_with(const std::string& str, const std::string& suffix)
134 {
135  return str.size() >= suffix.size() && std::equal(suffix.begin(),suffix.end(),str.end()-suffix.size());
136 }
137 
138 std::string read_map(const std::string& name)
139 {
140  std::string res;
141  std::string map_location = get_wml_location(name);
142  if(!map_location.empty()) {
143  res = read_file(map_location);
144  }
145 
146  // TODO: might be nice to have automatic detection of the maps/ directory?
147 
148  if(res.empty()) {
149  res = read_file(get_user_data_dir() + "/editor/maps/" + name);
150  }
151 
152  return res;
153 }
154 
155 static void get_file_tree_checksum_internal(const std::string& path, file_tree_checksum& res)
156 {
157 
158  std::vector<std::string> dirs;
159  get_files_in_dir(path,nullptr,&dirs, ENTIRE_FILE_PATH, SKIP_MEDIA_DIR, DONT_REORDER, &res);
160 
161  for(std::vector<std::string>::const_iterator j = dirs.begin(); j != dirs.end(); ++j) {
163  }
164 }
165 
167 {
168  static file_tree_checksum checksum;
169  if (reset)
170  checksum.reset();
171  if(checksum.nfiles == 0) {
172  get_file_tree_checksum_internal("data/",checksum);
173  get_file_tree_checksum_internal(get_user_data_dir() + "/data/",checksum);
174  LOG_FS << "calculated data tree checksum: "
175  << checksum.nfiles << " files; "
176  << checksum.sum_size << " bytes" << std::endl;
177  }
178 
179  return checksum;
180 }
181 
182 }
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_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:293
std::string get_cwd()
Definition: filesystem.cpp:809
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
bool match_file(const std::string &name) const
Definition: filesystem.hpp:70
std::string get_user_data_dir()
Definition: filesystem.cpp:775
std::string get_intl_dir()
std::string path
Definition: game_config.cpp:39
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:912
std::string read_map(const std::string &name)
Encapsulates the map of the game.
Definition: location.hpp:42
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
#define LOG_FS
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, file_name_option mode, file_filter_option filter, file_reorder_option 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:368
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...
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:746
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
Definition: filesystem.hpp:76
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
std::string get_save_index_file()
#define LOCALEDIR
Definition: wesconfig.h:19
std::string default_preferences_path
Definition: game_config.cpp:45