Skip to content

Commit

Permalink
Move everything in src/filesystem.hpp into its own namespace
Browse files Browse the repository at this point in the history
This is a chearry pick by me (gfgtdf) of AI0867@66176b1. It differes a lot from the original becasue since there were so much merging conflics i decided to do it mostly from scratch.

Conflicts:
	src/addon/manager.cpp
	src/addon/manager_ui.cpp
	src/campaign_server/campaign_server.cpp
	src/config_cache.cpp
	src/create_engine.cpp
	src/editor/map/context_manager.cpp
	src/editor/map/map_context.cpp
	src/font.cpp
	src/game_config_manager.cpp
	src/gamestatus.cpp
	src/gui/dialogs/editor/custom_tod.cpp
	src/gui/dialogs/lobby/lobby_data.cpp
	src/gui/dialogs/mp_create_game.cpp
	src/gui/widgets/settings.cpp
	src/hotkeys.cpp
	src/image.cpp
	src/multiplayer_lobby.cpp
	src/network.cpp
	src/playcampaign.cpp
	src/preferences.cpp
	src/savegame.cpp
	src/serialization/preprocessor.cpp
	src/tests/main.cpp
	src/wesnoth.cpp
	src/widgets/button.cpp
  • Loading branch information
AI0867 authored and gfgtdf committed Oct 13, 2014
1 parent 5cfef2e commit e5cb79b
Show file tree
Hide file tree
Showing 65 changed files with 360 additions and 352 deletions.
78 changes: 39 additions & 39 deletions src/addon/manager.cpp
Expand Up @@ -58,41 +58,41 @@ static lg::log_domain log_network("network");
namespace {
std::string get_pbl_file_path(const std::string& addon_name)
{
const std::string& parentd = get_addon_campaigns_dir();
const std::string& parentd = filesystem::get_addons_dir();
// Cope with old-style or new-style file organization
const std::string exterior = parentd + "/" + addon_name + ".pbl";
const std::string interior = parentd + "/" + addon_name + "/_server.pbl";
return file_exists(exterior) ? exterior : interior;
return filesystem::file_exists(exterior) ? exterior : interior;
}

inline std::string get_info_file_path(const std::string& addon_name)
{
return get_addon_campaigns_dir() + "/" + addon_name + "/_info.cfg";
return filesystem::get_addons_dir() + "/" + addon_name + "/_info.cfg";
}
}

bool have_addon_in_vcs_tree(const std::string& addon_name)
{
static const std::string parentd = get_addon_campaigns_dir();
static const std::string parentd = filesystem::get_addons_dir();
return
file_exists(parentd+"/"+addon_name+"/.svn") ||
file_exists(parentd+"/"+addon_name+"/.git") ||
file_exists(parentd+"/"+addon_name+"/.hg");
filesystem::file_exists(parentd+"/"+addon_name+"/.svn") ||
filesystem::file_exists(parentd+"/"+addon_name+"/.git") ||
filesystem::file_exists(parentd+"/"+addon_name+"/.hg");
}

bool have_addon_pbl_info(const std::string& addon_name)
{
static const std::string parentd = get_addon_campaigns_dir();
static const std::string parentd = filesystem::get_addons_dir();
return
file_exists(parentd+"/"+addon_name+".pbl") ||
file_exists(parentd+"/"+addon_name+"/_server.pbl");
filesystem::file_exists(parentd+"/"+addon_name+".pbl") ||
filesystem::file_exists(parentd+"/"+addon_name+"/_server.pbl");
}

void get_addon_pbl_info(const std::string& addon_name, config& cfg)
{
const std::string& pbl_path = get_pbl_file_path(addon_name);
try {
scoped_istream stream = istream_file(pbl_path);
filesystem::scoped_istream stream = filesystem::istream_file(pbl_path);
read(cfg, *stream);
} catch(const config::error& e) {
throw invalid_pbl_exception(pbl_path, e.message);
Expand All @@ -101,19 +101,19 @@ void get_addon_pbl_info(const std::string& addon_name, config& cfg)

void set_addon_pbl_info(const std::string& addon_name, const config& cfg)
{
scoped_ostream stream = ostream_file(get_pbl_file_path(addon_name));
filesystem::scoped_ostream stream = filesystem::ostream_file(get_pbl_file_path(addon_name));
write(*stream, cfg);
}

bool have_addon_install_info(const std::string& addon_name)
{
return file_exists(get_info_file_path(addon_name));
return filesystem::file_exists(get_info_file_path(addon_name));
}

void get_addon_install_info(const std::string& addon_name, config& cfg)
{
const std::string& info_path = get_info_file_path(addon_name);
scoped_istream stream = istream_file(info_path);
filesystem::scoped_istream stream = filesystem::istream_file(info_path);
try {
read(cfg, *stream);
} catch(const config::error& e) {
Expand All @@ -126,16 +126,16 @@ void get_addon_install_info(const std::string& addon_name, config& cfg)
bool remove_local_addon(const std::string& addon)
{
bool ret = true;
const std::string addon_dir = get_addon_campaigns_dir() + "/" + addon;
const std::string addon_dir = filesystem::get_addons_dir() + "/" + addon;

LOG_CFG << "removing local add-on: " << addon << '\n';

if(file_exists(addon_dir) && !delete_directory(addon_dir, true)) {
if(filesystem::file_exists(addon_dir) && !filesystem::delete_directory(addon_dir, true)) {
ERR_CFG << "Failed to delete directory/file: " << addon_dir << '\n';
ret = false;
}

if(file_exists(addon_dir + ".cfg") && !delete_directory(addon_dir + ".cfg", true)) {
if(filesystem::file_exists(addon_dir + ".cfg") && !filesystem::delete_directory(addon_dir + ".cfg", true)) {
ERR_CFG << "Failed to delete directory/file: " << addon_dir << ".cfg" << std::endl;
ret = false;
}
Expand All @@ -151,16 +151,16 @@ std::vector<std::string> available_addons()
{
std::vector<std::string> res;
std::vector<std::string> files, dirs;
const std::string parentd = get_addon_campaigns_dir();
get_files_in_dir(parentd,&files,&dirs);
const std::string parentd = filesystem::get_addons_dir();
filesystem::get_files_in_dir(parentd,&files,&dirs);

for(std::vector<std::string>::const_iterator i = dirs.begin(); i != dirs.end(); ++i) {
const std::string external_cfg_file = *i + ".cfg";
const std::string internal_cfg_file = *i + "/_main.cfg";
const std::string external_pbl_file = *i + ".pbl";
const std::string internal_pbl_file = *i + "/_server.pbl";
if((std::find(files.begin(),files.end(),external_cfg_file) != files.end() || file_exists(parentd + "/" + internal_cfg_file)) &&
(std::find(files.begin(),files.end(),external_pbl_file) != files.end() || (file_exists(parentd + "/" + internal_pbl_file)))) {
if((std::find(files.begin(),files.end(),external_cfg_file) != files.end() || filesystem::file_exists(parentd + "/" + internal_cfg_file)) &&
(std::find(files.begin(),files.end(),external_pbl_file) != files.end() || (filesystem::file_exists(parentd + "/" + internal_pbl_file)))) {
res.push_back(*i);
}
}
Expand All @@ -181,14 +181,14 @@ std::vector<std::string> available_addons()
std::vector<std::string> installed_addons()
{
std::vector<std::string> res;
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();
std::vector<std::string> files, dirs;
get_files_in_dir(parentd,&files,&dirs);
filesystem::get_files_in_dir(parentd,&files,&dirs);

for(std::vector<std::string>::const_iterator i = dirs.begin(); i != dirs.end(); ++i) {
const std::string external_cfg_file = *i + ".cfg";
const std::string internal_cfg_file = *i + "/_main.cfg";
if(std::find(files.begin(),files.end(),external_cfg_file) != files.end() || file_exists(parentd + "/" + internal_cfg_file)) {
if(std::find(files.begin(),files.end(),external_cfg_file) != files.end() || filesystem::file_exists(parentd + "/" + internal_cfg_file)) {
res.push_back(*i);
}
}
Expand All @@ -198,9 +198,9 @@ std::vector<std::string> installed_addons()

bool is_addon_installed(const std::string& addon_name)
{
const std::string namestem = get_addon_campaigns_dir() + "/" + addon_name;
const std::string namestem = filesystem::get_addons_dir() + "/" + addon_name;

return file_exists(namestem + ".cfg") || file_exists(namestem + "/_main.cfg");
return filesystem::file_exists(namestem + ".cfg") || filesystem::file_exists(namestem + "/_main.cfg");
}

static inline bool IsCR(const char& c)
Expand Down Expand Up @@ -256,16 +256,16 @@ namespace {

static std::pair<std::vector<std::string>, std::vector<std::string> > read_ignore_patterns(const std::string& addon_name)
{
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();
const std::string exterior = parentd + "/" + addon_name + ".ign";
const std::string interior = parentd + "/" + addon_name + "/_server.ign";

std::pair<std::vector<std::string>, std::vector<std::string> > patterns;
std::string ign_file;
LOG_CFG << "searching for .ign file for '" << addon_name << "'...\n";
if (file_exists(interior)) {
if (filesystem::file_exists(interior)) {
ign_file = interior;
} else if (file_exists(exterior)) {
} else if (filesystem::file_exists(exterior)) {
ign_file = exterior;
} else {
LOG_CFG << "no .ign file found for '" << addon_name << "'\n"
Expand All @@ -274,7 +274,7 @@ static std::pair<std::vector<std::string>, std::vector<std::string> > read_ignor
return patterns; // just default patterns
}
LOG_CFG << "found .ign file: " << ign_file << '\n';
std::istream *stream = istream_file(ign_file);
std::istream *stream = filesystem::istream_file(ign_file);
std::string line;
while (std::getline(*stream, line)) {
utils::strip(line);
Expand All @@ -292,7 +292,7 @@ static void archive_file(const std::string& path, const std::string& fname, conf
{
cfg["name"] = fname;
const bool is_cfg = (fname.size() > 4 ? (fname.substr(fname.size() - 4) == ".cfg") : false);
cfg["contents"] = encode_binary(strip_cr(read_file(path + '/' + fname),is_cfg));
cfg["contents"] = encode_binary(strip_cr(filesystem::read_file(path + '/' + fname),is_cfg));
}

static void archive_dir(const std::string& path, const std::string& dirname, config& cfg, std::pair<std::vector<std::string>, std::vector<std::string> >& ignore_patterns)
Expand All @@ -301,9 +301,9 @@ static void archive_dir(const std::string& path, const std::string& dirname, con
const std::string dir = path + '/' + dirname;

std::vector<std::string> files, dirs;
get_files_in_dir(dir,&files,&dirs);
filesystem::get_files_in_dir(dir,&files,&dirs);
for(std::vector<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) {
bool valid = !looks_like_pbl(*i);
bool valid = !filesystem::looks_like_pbl(*i);
for(std::vector<std::string>::const_iterator p = ignore_patterns.first.begin(); p != ignore_patterns.first.end(); ++p) {
if (utils::wildcard_string_match(*i, *p)) {
valid = false;
Expand Down Expand Up @@ -331,12 +331,12 @@ static void archive_dir(const std::string& path, const std::string& dirname, con

void archive_addon(const std::string& addon_name, config& cfg)
{
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();

std::pair<std::vector<std::string>, std::vector<std::string> > ignore_patterns;
// External .cfg may not exist; newer campaigns have a _main.cfg
const std::string external_cfg = addon_name + ".cfg";
if (file_exists(parentd + "/" + external_cfg)) {
if (filesystem::file_exists(parentd + "/" + external_cfg)) {
archive_file(parentd, external_cfg, cfg.add_child("file"));
}
ignore_patterns = read_ignore_patterns(addon_name);
Expand All @@ -345,7 +345,7 @@ void archive_addon(const std::string& addon_name, config& cfg)

static void unarchive_file(const std::string& path, const config& cfg)
{
write_file(path + '/' + cfg["name"].str(), unencode_binary(cfg["contents"]));
filesystem::write_file(path + '/' + cfg["name"].str(), unencode_binary(cfg["contents"]));
}

static void unarchive_dir(const std::string& path, const config& cfg)
Expand All @@ -356,7 +356,7 @@ static void unarchive_dir(const std::string& path, const config& cfg)
else
dir = path + '/' + cfg["name"].str();

make_directory(dir);
filesystem::make_directory(dir);

BOOST_FOREACH(const config &d, cfg.child_range("dir")) {
unarchive_dir(dir, d);
Expand All @@ -369,7 +369,7 @@ static void unarchive_dir(const std::string& path, const config& cfg)

void unarchive_addon(const config& cfg)
{
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();
unarchive_dir(parentd, cfg);
}

Expand Down Expand Up @@ -399,7 +399,7 @@ void refresh_addon_version_info_cache()
const std::string& addon = addons[i];
const std::string& info_file = addon_info_files[i];

if(file_exists(info_file)) {
if(filesystem::file_exists(info_file)) {
config cfg;
get_addon_install_info(addon, cfg);

Expand Down
4 changes: 2 additions & 2 deletions src/addon/manager_ui.cpp
Expand Up @@ -992,8 +992,8 @@ bool addons_manager_ui(display& disp, const std::string& remote_address)
} catch(const network_asio::error& e) {
ERR_NET << "network_asio::error thrown during transaction with add-on server; \""<< e.what() << "\"" << std::endl;
gui2::show_error_message(disp.video(), _("Remote host disconnected."));
} catch(const io_exception& e) {
ERR_FS << "io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
} catch(const filesystem::io_exception& e) {
ERR_FS << "filesystem::io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
gui2::show_error_message(disp.video(), _("A problem occurred when trying to create the files necessary to install this add-on."));
} catch(const invalid_pbl_exception& e) {
ERR_CFG << "could not read .pbl file " << e.path << ": " << e.message << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/ai/configuration.cpp
Expand Up @@ -204,7 +204,7 @@ config configuration::default_config_ = config();

bool configuration::get_side_config_from_file(const std::string& file, config& cfg ){
try {
scoped_istream stream = preprocess_file(get_wml_location(file));
filesystem::scoped_istream stream = preprocess_file(filesystem::get_wml_location(file));
read(cfg, *stream);
LOG_AI_CONFIGURATION << "Reading AI configuration from file '" << file << "'" << std::endl;
} catch(config::error &) {
Expand Down
4 changes: 2 additions & 2 deletions src/ai/formula/function_table.cpp
Expand Up @@ -500,13 +500,13 @@ class run_file_function : public function_expression {
const std::string filename = var0.string_cast();

//NOTE: get_wml_location also filters file path to ensure it doesn't contain things like "../../top/secret"
std::string path = get_wml_location(filename);
std::string path = filesystem::get_wml_location(filename);
if(path.empty()) {
ERR_AI << "run_file : not found [" << filename <<"]"<< std::endl;
return variant(); //no suitable file
}

std::string formula_string = read_file(path);
std::string formula_string = filesystem::read_file(path);
//need to get function_table from somewhere or delegate to someone who has access to it
formula_ptr parsed_formula = ai_.create_optional_formula(formula_string);
if(parsed_formula == game_logic::formula_ptr()) {
Expand Down
2 changes: 1 addition & 1 deletion src/campaign_server/addon_utils.cpp
Expand Up @@ -124,7 +124,7 @@ void add_license(config& cfg)
}

// Copy over COPYING.txt
const std::string& contents = read_file("COPYING.txt");
const std::string& contents = filesystem::read_file("COPYING.txt");
if (contents.empty()) {
LOG_CS << "Could not find COPYING.txt, path is \"" << game_config::path << "\"\n";
return;
Expand Down
20 changes: 10 additions & 10 deletions src/campaign_server/campaign_server.cpp
Expand Up @@ -118,7 +118,7 @@ server::~server()

int server::load_config()
{
scoped_istream in = istream_file(cfg_file_);
filesystem::scoped_istream in = filesystem::istream_file(cfg_file_);
read(cfg_, *in);

read_only_ = cfg_["read_only"].to_bool(false);
Expand Down Expand Up @@ -171,7 +171,7 @@ void server::load_blacklist()
}

try {
scoped_istream in = istream_file(blacklist_file_);
filesystem::scoped_istream in = filesystem::istream_file(blacklist_file_);
config blcfg;

read(blcfg, *in);
Expand All @@ -185,7 +185,7 @@ void server::load_blacklist()

void server::write_config()
{
scoped_ostream out = ostream_file(cfg_file_);
filesystem::scoped_ostream out = filesystem::ostream_file(cfg_file_);
write(*out, cfg_);
}

Expand Down Expand Up @@ -444,7 +444,7 @@ void server::handle_request_campaign(const server::request& req)
if(!campaign) {
send_error("Add-on '" + req.cfg["name"].str() + "' not found.", req.sock);
} else {
const int size = file_size(campaign["filename"]);
const int size = filesystem::file_size(campaign["filename"]);

if(size < 0) {
std::cerr << " size: <unknown> KiB\n";
Expand Down Expand Up @@ -613,12 +613,12 @@ void server::handle_upload(const server::request& req)
add_license(data);

{
scoped_ostream campaign_file = ostream_file(filename);
filesystem::scoped_ostream campaign_file = filesystem::ostream_file(filename);
config_writer writer(*campaign_file, true, compress_level_);
writer.write(data);
}

(*campaign)["size"] = file_size(filename);
(*campaign)["size"] = filesystem::file_size(filename);

write_config();

Expand Down Expand Up @@ -656,7 +656,7 @@ void server::handle_delete(const server::request& req)
}

// Erase the campaign.
write_file(campaign["filename"], std::string());
filesystem::write_file(campaign["filename"], std::string());
if(remove(campaign["filename"].str().c_str()) != 0) {
ERR_CS << "failed to delete archive for campaign '" << erase["name"]
<< "' (" << campaign["filename"] << "): " << strerror(errno)
Expand Down Expand Up @@ -711,15 +711,15 @@ void server::handle_change_passphrase(const server::request& req)

int main(int argc, char**argv)
{
game_config::path = get_cwd();
game_config::path = filesystem::get_cwd();

lg::set_log_domain_severity("campaignd", lg::info);
lg::timestamps(true);

try {
printf("argc %d argv[0] %s 1 %s\n",argc,argv[0],argv[1]);

const std::string& cfg_path = normalize_path("server.cfg");
const std::string& cfg_path = filesystem::normalize_path("server.cfg");

if(argc >= 2 && atoi(argv[1])){
campaignd::server(cfg_path, atoi(argv[1])).run();
Expand All @@ -729,7 +729,7 @@ int main(int argc, char**argv)
} catch(config::error& /*e*/) {
std::cerr << "Could not parse config file\n";
return 1;
} catch(io_exception& /*e*/) {
} catch(filesystem::io_exception& /*e*/) {
std::cerr << "File I/O error\n";
return 2;
} catch(network::error& e) {
Expand Down

0 comments on commit e5cb79b

Please sign in to comment.