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
  • Loading branch information
AI0867 committed Nov 27, 2013
1 parent f23ef16 commit 66176b1
Show file tree
Hide file tree
Showing 59 changed files with 318 additions and 310 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,34 +101,34 @@ 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)
{
scoped_istream stream = istream_file(get_info_file_path(addon_name));
filesystem::scoped_istream stream = filesystem::istream_file(get_info_file_path(addon_name));
read(cfg, *stream);
}

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\n";
ret = false;
}
Expand All @@ -144,16 +144,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 @@ -174,14 +174,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 @@ -191,9 +191,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 @@ -249,16 +249,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 @@ -267,7 +267,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 @@ -285,7 +285,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 @@ -294,9 +294,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 @@ -324,12 +324,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 @@ -338,7 +338,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 @@ -349,7 +349,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 @@ -362,7 +362,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 @@ -392,7 +392,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 @@ -980,8 +980,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() << "\"\n";
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() << "\"\n";
} catch(const filesystem::io_exception& e) {
ERR_FS << "filesystem::io_exception thrown while installing an addon; \"" << e.what() << "\"\n";
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 << "\n";
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 @@ -498,13 +498,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

0 comments on commit 66176b1

Please sign in to comment.