From 77b431e8b28ff6c246963acfe451358e722b1e37 Mon Sep 17 00:00:00 2001 From: "Alexander van Gessel (AI0867)" Date: Tue, 27 Nov 2012 03:41:22 +0100 Subject: [PATCH] Split the parts of filesystem.cpp that don't directly interface with the filesystem off into filesystem_common.cpp Conflicts: src/filesystem.cpp --- src/SConscript | 3 +- src/filesystem.cpp | 181 ---------------------------------- src/filesystem_common.cpp | 201 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+), 182 deletions(-) create mode 100644 src/filesystem_common.cpp diff --git a/src/SConscript b/src/SConscript index 7a111e921945..5e9386004f5d 100644 --- a/src/SConscript +++ b/src/SConscript @@ -63,7 +63,8 @@ if env['default_prefs_file']: libwesnoth_core_sources.extend([ game_config_env.Object("game_config.cpp"), - filesystem_env.Object("filesystem.cpp") + filesystem_env.Object("filesystem.cpp"), + filesystem_env.Object("filesystem_common.cpp"), ]) libwesnoth_core = [env.Library("wesnoth_core", libwesnoth_core_sources)] diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 9811e6fbcbf3..318d1b089f60 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -51,7 +51,6 @@ #include "game_config.hpp" #include "game_preferences.hpp" #include "log.hpp" -#include "loadscreen.hpp" #include "scoped_resource.hpp" #include "serialization/string_utils.hpp" #include "serialization/unicode.hpp" @@ -83,11 +82,6 @@ namespace { namespace filesystem { -bool ends_with(const std::string& str, const std::string& suffix) -{ - return str.size() >= suffix.size() && std::equal(suffix.begin(),suffix.end(),str.end()-suffix.size()); -} - void get_files_in_dir(const std::string &directory, std::vector* files, std::vector* dirs, @@ -249,84 +243,6 @@ void get_files_in_dir(const std::string &directory, } } -#ifdef __native_client__ -// For performance reasons, on NaCl we only keep preferences and saves in persistent storage. -std::string get_prefs_file() -{ - return "/wesnoth-userdata/preferences"; -} - -std::string get_save_index_file() -{ - return "/wesnoth-userdata/save_index"; -} - -std::string get_saves_dir() -{ - const std::string dir_path = "/wesnoth-userdata/saves"; - return get_dir(dir_path); -} - -#else - -std::string get_prefs_file() -{ - return get_user_config_dir() + "/preferences"; -} - -std::string get_default_prefs_file() -{ -#ifdef HAS_RELATIVE_DEFPREF - return game_config::path + "/" + game_config::default_preferences_path; -#else - return game_config::default_preferences_path; -#endif -} - -std::string get_save_index_file() -{ - return get_user_data_dir() + "/save_index"; -} - -std::string get_saves_dir() -{ - const std::string dir_path = get_user_data_dir() + "/saves"; - return get_dir(dir_path); -} -#endif - -std::string get_addons_dir() -{ - const std::string dir_path = get_user_data_dir() + "/data/add-ons"; - return get_dir(dir_path); -} - -std::string get_intl_dir() -{ -#ifdef _WIN32 - return get_cwd() + "/translations"; -#else - -#ifdef USE_INTERNAL_DATA - return get_cwd() + "/" LOCALEDIR; -#endif - -#if HAS_RELATIVE_LOCALEDIR - std::string res = game_config::path + "/" LOCALEDIR; -#else - std::string res = LOCALEDIR; -#endif - - return res; -#endif -} - -std::string get_screenshot_dir() -{ - const std::string dir_path = get_user_data_dir() + "/screenshots"; - return get_dir(dir_path); -} - std::string get_next_filename(const std::string& name, const std::string& extension) { std::string next_filename; @@ -372,11 +288,6 @@ bool make_directory(const std::string& path) return (mkdir(path.c_str(),AccessMode) == 0); } -bool looks_like_pbl(const std::string& file) -{ - return utils::wildcard_string_match(utf8::lowercase(file), "*.pbl"); -} - // This deletes a directory with no hidden files and subdirectories. // Also deletes a single file. bool delete_directory(const std::string& path, const bool keep_pbl) @@ -763,22 +674,6 @@ void write_file(const std::string& fname, const std::string& data) } } - -std::string read_map(const std::string& name) -{ - std::string res; - std::string map_location = get_wml_location("maps/" + name); - if(!map_location.empty()) { - res = read_file(map_location); - } - - if (res.empty()) { - res = read_file(get_user_data_dir() + "/editor/maps/" + name); - } - - return res; -} - static bool is_directory_internal(const std::string& fname) { #ifdef _WIN32 @@ -855,58 +750,6 @@ bool is_bzip2_file(const std::string& filename) && filename.substr(filename.length() - 4) == ".bz2"); } -file_tree_checksum::file_tree_checksum() - : nfiles(0), sum_size(0), modified(0) -{} - -file_tree_checksum::file_tree_checksum(const config& cfg) : - nfiles (cfg["nfiles"].to_size_t()), - sum_size(cfg["size"].to_size_t()), - modified(cfg["modified"].to_time_t()) -{ -} - -void file_tree_checksum::write(config& cfg) const -{ - cfg["nfiles"] = nfiles; - cfg["size"] = sum_size; - cfg["modified"] = modified; -} - -bool file_tree_checksum::operator==(const file_tree_checksum &rhs) const -{ - return nfiles == rhs.nfiles && sum_size == rhs.sum_size && - modified == rhs.modified; -} - -static void get_file_tree_checksum_internal(const std::string& path, file_tree_checksum& res) -{ - - std::vector dirs; - get_files_in_dir(path,NULL,&dirs, ENTIRE_FILE_PATH, SKIP_MEDIA_DIR, DONT_REORDER, &res); - loadscreen::increment_progress(); - - for(std::vector::const_iterator j = dirs.begin(); j != dirs.end(); ++j) { - get_file_tree_checksum_internal(*j,res); - } -} - -const file_tree_checksum& data_tree_checksum(bool reset) -{ - static file_tree_checksum checksum; - if (reset) - checksum.reset(); - if(checksum.nfiles == 0) { - get_file_tree_checksum_internal("data/",checksum); - get_file_tree_checksum_internal(get_user_data_dir() + "/data/",checksum); - LOG_FS << "calculated data tree checksum: " - << checksum.nfiles << " files; " - << checksum.sum_size << " bytes" << std::endl; - } - - return checksum; -} - int file_size(const std::string& fname) { struct stat buf; @@ -1328,28 +1171,4 @@ std::string normalize_path(const std::string &p1) return p4.str(); } -scoped_istream& scoped_istream::operator=(std::istream *s) -{ - delete stream; - stream = s; - return *this; -} - -scoped_istream::~scoped_istream() -{ - delete stream; -} - -scoped_ostream& scoped_ostream::operator=(std::ostream *s) -{ - delete stream; - stream = s; - return *this; -} - -scoped_ostream::~scoped_ostream() -{ - delete stream; -} - } diff --git a/src/filesystem_common.cpp b/src/filesystem_common.cpp new file mode 100644 index 000000000000..67cbb939eb6e --- /dev/null +++ b/src/filesystem_common.cpp @@ -0,0 +1,201 @@ + +#include "global.hpp" + +#include + +#include "filesystem.hpp" + +#include "config.hpp" +#include "game_config.hpp" +#include "loadscreen.hpp" +#include "log.hpp" +#include "serialization/string_utils.hpp" +#include "serialization/unicode.hpp" +#include "util.hpp" + +static lg::log_domain log_filesystem("filesystem"); +#define LOG_FS LOG_STREAM(info, log_filesystem) + +namespace filesystem { + + +scoped_istream& scoped_istream::operator=(std::istream *s) +{ + delete stream; + stream = s; + return *this; +} + +scoped_istream::~scoped_istream() +{ + delete stream; +} + +scoped_ostream& scoped_ostream::operator=(std::ostream *s) +{ + delete stream; + stream = s; + return *this; +} + +scoped_ostream::~scoped_ostream() +{ + delete stream; +} + +#ifdef __native_client__ +// For performance reasons, on NaCl we only keep preferences and saves in persistent storage. +std::string get_prefs_file() +{ + return "/wesnoth-userdata/preferences"; +} + +std::string get_save_index_file() +{ + return "/wesnoth-userdata/save_index"; +} + +std::string get_saves_dir() +{ + const std::string dir_path = "/wesnoth-userdata/saves"; + return get_dir(dir_path); +} + +#else + +std::string get_prefs_file() +{ + return get_user_config_dir() + "/preferences"; +} + +std::string get_default_prefs_file() +{ +#ifdef HAS_RELATIVE_DEFPREF + return game_config::path + "/" + game_config::default_preferences_path; +#else + return game_config::default_preferences_path; +#endif +} + +std::string get_save_index_file() +{ + return get_user_data_dir() + "/save_index"; +} + +std::string get_saves_dir() +{ + const std::string dir_path = get_user_data_dir() + "/saves"; + return get_dir(dir_path); +} +#endif + +std::string get_addons_dir() +{ + const std::string dir_path = get_user_data_dir() + "/data/add-ons"; + return get_dir(dir_path); +} + +std::string get_intl_dir() +{ +#ifdef _WIN32 + return get_cwd() + "/translations"; +#else + +#ifdef USE_INTERNAL_DATA + return get_cwd() + "/" LOCALEDIR; +#endif + +#if HAS_RELATIVE_LOCALEDIR + std::string res = game_config::path + "/" LOCALEDIR; +#else + std::string res = LOCALEDIR; +#endif + + return res; +#endif +} + +std::string get_screenshot_dir() +{ + const std::string dir_path = get_user_data_dir() + "/screenshots"; + return get_dir(dir_path); +} + +bool looks_like_pbl(const std::string& file) +{ + return utils::wildcard_string_match(utf8::lowercase(file), "*.pbl"); +} + +file_tree_checksum::file_tree_checksum() + : nfiles(0), sum_size(0), modified(0) +{} + +file_tree_checksum::file_tree_checksum(const config& cfg) : + nfiles (cfg["nfiles"].to_size_t()), + sum_size(cfg["size"].to_size_t()), + modified(cfg["modified"].to_time_t()) +{ +} + +void file_tree_checksum::write(config& cfg) const +{ + cfg["nfiles"] = nfiles; + cfg["size"] = sum_size; + cfg["modified"] = modified; +} + +bool file_tree_checksum::operator==(const file_tree_checksum &rhs) const +{ + return nfiles == rhs.nfiles && sum_size == rhs.sum_size && + modified == rhs.modified; +} + +bool ends_with(const std::string& str, const std::string& suffix) +{ + return str.size() >= suffix.size() && std::equal(suffix.begin(),suffix.end(),str.end()-suffix.size()); +} + +std::string read_map(const std::string& name) +{ + std::string res; + std::string map_location = get_wml_location("maps/" + name); + if(!map_location.empty()) { + res = read_file(map_location); + } + + if (res.empty()) { + res = read_file(get_user_data_dir() + "/editor/maps/" + name); + } + + return res; +} + +static void get_file_tree_checksum_internal(const std::string& path, file_tree_checksum& res) +{ + + std::vector dirs; + get_files_in_dir(path,NULL,&dirs, ENTIRE_FILE_PATH, SKIP_MEDIA_DIR, DONT_REORDER, &res); + loadscreen::increment_progress(); + + for(std::vector::const_iterator j = dirs.begin(); j != dirs.end(); ++j) { + get_file_tree_checksum_internal(*j,res); + } +} + +const file_tree_checksum& data_tree_checksum(bool reset) +{ + static file_tree_checksum checksum; + if (reset) + checksum.reset(); + if(checksum.nfiles == 0) { + get_file_tree_checksum_internal("data/",checksum); + get_file_tree_checksum_internal(get_user_data_dir() + "/data/",checksum); + LOG_FS << "calculated data tree checksum: " + << checksum.nfiles << " files; " + << checksum.sum_size << " bytes" << std::endl; + } + + return checksum; +} + +}