From b8af63d9eb841b400e42aa7ef00662faf1a0fc89 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 5 Dec 2015 07:12:40 +0000 Subject: [PATCH] Remove Boost.Iostreams usage It's still required by libespm, but using Boost.Filesystem's file streams has the same unicode path handling benefits without breaking platform line ending handling. Unlike the standard library or Boost.Filesystem file streams, Boost.Iostreams file streams throw if they can't be opened, so a couple of new checks are added for that. --- CMakeLists.txt | 1 - src/api/api.cpp | 6 ++-- src/backend/game/game.cpp | 1 - src/backend/game/game_cache.cpp | 1 - src/backend/helpers/helpers.cpp | 5 ++- src/backend/helpers/streams.h | 36 ------------------- src/backend/metadata_list.cpp | 9 +++-- src/backend/plugin_sorter.cpp | 1 - src/gui/loot_state.cpp | 6 ++-- src/tests/api/test_api.h | 2 +- .../backend/game/test_load_order_handler.h | 2 +- src/tests/backend/helpers/test_git_helper.h | 2 +- src/tests/backend/test_masterlist.h | 2 +- src/tests/fixtures.h | 12 +++---- 14 files changed, 24 insertions(+), 62 deletions(-) delete mode 100644 src/backend/helpers/streams.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f388d36..6a8caf58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,7 +121,6 @@ set (LOOT_HEADERS "${CMAKE_SOURCE_DIR}/src/backend/metadata/condition_grammar.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/git_helper.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/helpers.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/language.h" - "${CMAKE_SOURCE_DIR}/src/backend/helpers/streams.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/version.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/yaml_set_helpers.h" "${CMAKE_SOURCE_DIR}/src/backend/globals.h" diff --git a/src/api/api.cpp b/src/api/api.cpp index 47563d9c..7b6b5793 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -26,7 +26,6 @@ #include "../backend/game/game.h" #include "../backend/globals.h" #include "../backend/error.h" -#include "../backend/helpers/streams.h" #include "../backend/plugin_sorter.h" #include @@ -40,6 +39,7 @@ #include #include +#include #include const unsigned int loot_ok = loot::error::ok; @@ -822,9 +822,9 @@ LOOT_API unsigned int loot_write_minimal_list(loot_db db, const char * const out boost::filesystem::path p(outputFile); try { - loot::ofstream out(p); + boost::filesystem::ofstream out(p); if (out.fail()) - return c_error(loot_error_invalid_args, "Couldn't open output file."); + return c_error(loot_error_file_write_fail, "Couldn't open output file."); out << yout.c_str(); out.close(); } diff --git a/src/backend/game/game.cpp b/src/backend/game/game.cpp index 230369e9..79db51d4 100644 --- a/src/backend/game/game.cpp +++ b/src/backend/game/game.cpp @@ -26,7 +26,6 @@ #include "../globals.h" #include "../helpers/helpers.h" #include "../error.h" -#include "../helpers/streams.h" #include diff --git a/src/backend/game/game_cache.cpp b/src/backend/game/game_cache.cpp index 0a249851..533e6a5a 100644 --- a/src/backend/game/game_cache.cpp +++ b/src/backend/game/game_cache.cpp @@ -26,7 +26,6 @@ #include "../globals.h" #include "../helpers/helpers.h" #include "../error.h" -#include "../helpers/streams.h" #include diff --git a/src/backend/helpers/helpers.cpp b/src/backend/helpers/helpers.cpp index 12e9d397..41969b9b 100644 --- a/src/backend/helpers/helpers.cpp +++ b/src/backend/helpers/helpers.cpp @@ -24,17 +24,16 @@ #include "helpers.h" #include "../error.h" -#include "streams.h" #include #include #include #include +#include #include #include #include -#include #include #include #include @@ -68,7 +67,7 @@ namespace loot { uint32_t GetCrc32(const fs::path& filename) { uint32_t chksum = 0; try { - loot::ifstream ifile(filename, ios::binary); + fs::ifstream ifile(filename, ios::binary); BOOST_LOG_TRIVIAL(trace) << "Calculating CRC for: " << filename.string(); boost::crc_32_type result; if (ifile) { diff --git a/src/backend/helpers/streams.h b/src/backend/helpers/streams.h deleted file mode 100644 index 5534f613..00000000 --- a/src/backend/helpers/streams.h +++ /dev/null @@ -1,36 +0,0 @@ -/* LOOT - - A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and - Fallout: New Vegas. - - Copyright (C) 2013-2015 WrinklyNinja - - This file is part of LOOT. - - LOOT is free software: you can redistribute - it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation, either version 3 of - the License, or (at your option) any later version. - - LOOT is distributed in the hope that it will - be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with LOOT. If not, see - . -*/ -#ifndef __LOOT_STREAMS__ -#define __LOOT_STREAMS__ - -#include -#include - -namespace loot { - typedef boost::iostreams::stream< boost::iostreams::file_descriptor_sink > ofstream; - typedef boost::iostreams::stream< boost::iostreams::file_descriptor_source > ifstream; - typedef boost::iostreams::stream< boost::iostreams::file_descriptor > fstream; -} - -#endif diff --git a/src/backend/metadata_list.cpp b/src/backend/metadata_list.cpp index 13aa442f..29e20b8e 100644 --- a/src/backend/metadata_list.cpp +++ b/src/backend/metadata_list.cpp @@ -25,9 +25,9 @@ #include "metadata_list.h" #include "globals.h" #include "error.h" -#include "helpers/streams.h" #include +#include #include using namespace std; @@ -40,7 +40,10 @@ namespace loot { BOOST_LOG_TRIVIAL(debug) << "Loading file: " << filepath; - loot::ifstream in(filepath); + boost::filesystem::ifstream in(filepath); + if (!in.good()) + throw error(error::path_read_fail, "Cannot open " + filepath.string()); + YAML::Node metadataList = YAML::Load(in); in.close(); @@ -70,7 +73,7 @@ namespace loot { << YAML::Key << "globals" << YAML::Value << messages << YAML::EndMap; - loot::ofstream uout(filepath); + boost::filesystem::ofstream uout(filepath); uout << yout.c_str(); uout.close(); } diff --git a/src/backend/plugin_sorter.cpp b/src/backend/plugin_sorter.cpp index 91640802..f0776fa7 100644 --- a/src/backend/plugin_sorter.cpp +++ b/src/backend/plugin_sorter.cpp @@ -25,7 +25,6 @@ #include "game/game.h" #include "error.h" #include "plugin_sorter.h" -#include "helpers/streams.h" #include "helpers/helpers.h" #include diff --git a/src/gui/loot_state.cpp b/src/gui/loot_state.cpp index 990abcf9..f70bb2a0 100644 --- a/src/gui/loot_state.cpp +++ b/src/gui/loot_state.cpp @@ -28,8 +28,8 @@ #include "../backend/globals.h" #include "../backend/helpers/helpers.h" #include "../backend/helpers/language.h" -#include "../backend/helpers/streams.h" +#include #include #include #include @@ -71,7 +71,7 @@ namespace loot { } if (fs::exists(g_path_settings)) { try { - loot::ifstream in(g_path_settings); + fs::ifstream in(g_path_settings); _settings = YAML::Load(in); in.close(); } @@ -271,7 +271,7 @@ namespace loot { yout.SetIndent(2); yout << _settings; - loot::ofstream out(loot::g_path_settings); + fs::ofstream out(loot::g_path_settings); out << yout.c_str(); out.close(); } diff --git a/src/tests/api/test_api.h b/src/tests/api/test_api.h index 8d64dca4..d58eb2ba 100644 --- a/src/tests/api/test_api.h +++ b/src/tests/api/test_api.h @@ -651,7 +651,7 @@ TEST_F(OblivionAPIOperationsTest, WriteMinimalList) { EXPECT_EQ(loot_ok, loot_load_lists(db, masterlistPath.string().c_str(), NULL)); EXPECT_EQ(loot_ok, loot_write_minimal_list(db, outputFile.c_str(), false)); - loot::ifstream in(outputFile); + boost::filesystem::ifstream in(outputFile); std::string line; while (std::getline(in, line)) { EXPECT_FALSE(boost::contains(line, "- \"-\"")); diff --git a/src/tests/backend/game/test_load_order_handler.h b/src/tests/backend/game/test_load_order_handler.h index ee118994..eb3ee5f1 100644 --- a/src/tests/backend/game/test_load_order_handler.h +++ b/src/tests/backend/game/test_load_order_handler.h @@ -127,7 +127,7 @@ TEST_F(LoadOrderHandler, SetLoadOrder) { EXPECT_NO_THROW(loh.SetLoadOrder(loadOrder)); std::list actual; - loot::ifstream in(localPath / "loadorder.txt"); + boost::filesystem::ifstream in(localPath / "loadorder.txt"); while (in) { std::string line; std::getline(in, line); diff --git a/src/tests/backend/helpers/test_git_helper.h b/src/tests/backend/helpers/test_git_helper.h index e1ee2c91..15d4d0c4 100644 --- a/src/tests/backend/helpers/test_git_helper.h +++ b/src/tests/backend/helpers/test_git_helper.h @@ -101,7 +101,7 @@ class IsFileDifferent : public ::testing::Test { ASSERT_TRUE(boost::filesystem::exists(parentRepoRoot / "CONTRIBUTING.md.copy")); // Edit CONTRIBUTING.md - loot::ofstream out(parentRepoRoot / "CONTRIBUTING.md"); + boost::filesystem::ofstream out(parentRepoRoot / "CONTRIBUTING.md"); out.close(); } diff --git a/src/tests/backend/test_masterlist.h b/src/tests/backend/test_masterlist.h index 4d42b634..9a1946d4 100644 --- a/src/tests/backend/test_masterlist.h +++ b/src/tests/backend/test_masterlist.h @@ -139,7 +139,7 @@ TEST_F(Masterlist, GetInfo_Edited) { ASSERT_TRUE(masterlist.Update(masterlistPath, "https://github.com/loot/testing-metadata.git", "master")); - loot::ofstream out(masterlistPath); + boost::filesystem::ofstream out(masterlistPath); out.close(); loot::Masterlist::Info info = masterlist.GetInfo(masterlistPath, false); diff --git a/src/tests/fixtures.h b/src/tests/fixtures.h index 3dc8476b..b259b4d3 100644 --- a/src/tests/fixtures.h +++ b/src/tests/fixtures.h @@ -25,12 +25,12 @@ along with LOOT. If not, see #ifndef LOOT_TEST_FIXTURES #define LOOT_TEST_FIXTURES -#include "backend/helpers/streams.h" #include "printers.h" #include #include +#include class GameTest : public ::testing::Test { protected: @@ -72,7 +72,7 @@ protected: ASSERT_TRUE(boost::filesystem::exists(dataPath / "Blank - Master Dependent.esm.ghost")); // Write out an empty file. - loot::ofstream out(dataPath / "EmptyFile.esm"); + boost::filesystem::ofstream out(dataPath / "EmptyFile.esm"); out.close(); ASSERT_TRUE(boost::filesystem::exists(dataPath / "EmptyFile.esm")); @@ -163,7 +163,7 @@ protected: } // Set Oblivion's active plugins to a known list before running the test. - loot::ofstream activePlugins(localPath / "plugins.txt"); + boost::filesystem::ofstream activePlugins(localPath / "plugins.txt"); activePlugins << "Oblivion.esm" << std::endl << "Blank.esm" << std::endl; @@ -183,7 +183,7 @@ protected: }; inline void GenerateMasterlist() { - loot::ofstream masterlist(masterlistPath); + boost::filesystem::ofstream masterlist(masterlistPath); masterlist << "plugins:" << std::endl << " - name: Oblivion.esm" << std::endl @@ -268,7 +268,7 @@ protected: ASSERT_TRUE(boost::filesystem::exists(dataPath / "Skyrim.esm")); // Set Skyrim's load order to a known list before running the test. - loot::ofstream loadOrder(localPath / "loadorder.txt"); + boost::filesystem::ofstream loadOrder(localPath / "loadorder.txt"); loadOrder << "Skyrim.esm" << std::endl << "Blank.esm" << std::endl @@ -284,7 +284,7 @@ protected: loadOrder.close(); // Set Skyrim's active plugins to a known list before running the test. - loot::ofstream activePlugins(localPath / "plugins.txt"); + boost::filesystem::ofstream activePlugins(localPath / "plugins.txt"); activePlugins << "Blank.esm" << std::endl << "Blank - Different Master Dependent.esp" << std::endl;