Return a unique_ptr from agi::io::Open
This commit is contained in:
parent
cf7d548d17
commit
f051e59a61
7 changed files with 31 additions and 19 deletions
|
@ -30,16 +30,15 @@
|
|||
namespace agi {
|
||||
namespace io {
|
||||
|
||||
std::ifstream* Open(fs::path const& file, bool binary) {
|
||||
std::unique_ptr<std::ifstream> Open(fs::path const& file, bool binary) {
|
||||
LOG_D("agi/io/open/file") << file;
|
||||
acs::CheckFileRead(file);
|
||||
|
||||
auto stream = new boost::filesystem::ifstream(file, (binary ? std::ios::binary : std::ios::in));
|
||||
std::unique_ptr<std::ifstream> stream(
|
||||
new boost::filesystem::ifstream(file, (binary ? std::ios::binary : std::ios::in)));
|
||||
|
||||
if (stream->fail()) {
|
||||
delete stream;
|
||||
if (stream->fail())
|
||||
throw IOFatal("Unknown fatal error as occurred");
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
|
|
@ -27,14 +27,13 @@
|
|||
#include "libaegisub/fs.h"
|
||||
#include "libaegisub/io.h"
|
||||
#include "libaegisub/log.h"
|
||||
#include "libaegisub/util.h"
|
||||
|
||||
namespace agi {
|
||||
namespace json_util {
|
||||
|
||||
json::UnknownElement parse(std::istream *stream) {
|
||||
json::UnknownElement parse(std::unique_ptr<std::istream> stream) {
|
||||
try {
|
||||
std::unique_ptr<std::istream> stream_deleter(stream);
|
||||
|
||||
json::UnknownElement root;
|
||||
json::Reader::Read(root, *stream);
|
||||
return root;
|
||||
|
@ -57,15 +56,15 @@ json::UnknownElement file(agi::fs::path const& file, const std::string &default_
|
|||
}
|
||||
catch (fs::FileNotFound const&) {
|
||||
// Not an error
|
||||
return parse(new std::istringstream(default_config));
|
||||
return parse(util::make_unique<std::istringstream>(default_config));
|
||||
}
|
||||
catch (json::Exception&) {
|
||||
// Already logged in parse
|
||||
return parse(new std::istringstream(default_config));
|
||||
return parse(util::make_unique<std::istringstream>(default_config));
|
||||
}
|
||||
catch (agi::Exception& e) {
|
||||
LOG_E("json/file") << "Unexpected error when reading config file " << file << ": " << e.GetMessage();
|
||||
return parse(new std::istringstream(default_config));
|
||||
return parse(util::make_unique<std::istringstream>(default_config));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -198,9 +198,9 @@ Framerate::Framerate(fs::path const& filename)
|
|||
: denominator(default_denominator)
|
||||
, numerator(0)
|
||||
{
|
||||
scoped_ptr<std::ifstream> file(agi::io::Open(filename));
|
||||
std::string encoding = agi::charset::Detect(filename);
|
||||
std::string line = *line_iterator<std::string>(*file, encoding);
|
||||
auto file = agi::io::Open(filename);
|
||||
auto encoding = agi::charset::Detect(filename);
|
||||
auto line = *line_iterator<std::string>(*file, encoding);
|
||||
if (line == "# timecode format v2") {
|
||||
copy(line_iterator<int>(*file, encoding), line_iterator<int>(), back_inserter(timecodes));
|
||||
SetFromTimecodes();
|
||||
|
@ -218,7 +218,7 @@ Framerate::Framerate(fs::path const& filename)
|
|||
|
||||
void Framerate::Save(fs::path const& filename, int length) const {
|
||||
agi::io::Save file(filename);
|
||||
std::ofstream &out = file.Get();
|
||||
auto &out = file.Get();
|
||||
|
||||
out << "# timecode format v2\n";
|
||||
boost::copy(timecodes, std::ostream_iterator<int>(out, "\n"));
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
|
||||
namespace agi {
|
||||
namespace io {
|
||||
|
@ -28,7 +29,7 @@ namespace agi {
|
|||
DEFINE_BASE_EXCEPTION_NOINNER(IOError, Exception)
|
||||
DEFINE_SIMPLE_EXCEPTION_NOINNER(IOFatal, IOError, "io/fatal")
|
||||
|
||||
std::ifstream* Open(fs::path const& file, bool binary = false);
|
||||
std::unique_ptr<std::ifstream> Open(fs::path const& file, bool binary = false);
|
||||
|
||||
class Save {
|
||||
std::ofstream *fp;
|
||||
|
|
|
@ -20,13 +20,15 @@
|
|||
#include <libaegisub/cajun/elements.h>
|
||||
#include <libaegisub/fs_fwd.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace agi {
|
||||
namespace json_util {
|
||||
|
||||
/// Parse a JSON stream.
|
||||
/// @param stream JSON stream, this is deleted internally.
|
||||
/// @param stream JSON stream to parse
|
||||
/// @return json::UnknownElement
|
||||
json::UnknownElement parse(std::istream *stream);
|
||||
json::UnknownElement parse(std::unique_ptr<std::istream> stream);
|
||||
|
||||
/// Parse a JSON file.
|
||||
/// @param file Path JSON to file
|
||||
|
|
|
@ -69,5 +69,15 @@ namespace agi {
|
|||
/// @param name New name for the thread
|
||||
void SetThreadName(const char *name);
|
||||
|
||||
template<typename T>
|
||||
std::unique_ptr<T> make_unique() {
|
||||
return std::unique_ptr<T>(new T);
|
||||
}
|
||||
|
||||
template<typename T, typename A1>
|
||||
std::unique_ptr<T> make_unique(A1&& a1) {
|
||||
return std::unique_ptr<T>(new T(std::forward<A1>(a1)));
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace agi
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include <libaegisub/json.h>
|
||||
#include <libaegisub/log.h>
|
||||
#include <libaegisub/signal.h>
|
||||
#include <libaegisub/util.h>
|
||||
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <sstream>
|
||||
|
@ -43,7 +44,7 @@ namespace {
|
|||
json::Object const& get_root() {
|
||||
static json::Object root;
|
||||
if (root.empty())
|
||||
root = agi::json_util::parse(new std::istringstream(GET_DEFAULT_CONFIG(default_toolbar)));
|
||||
root = agi::json_util::parse(agi::util::make_unique<std::istringstream>(GET_DEFAULT_CONFIG(default_toolbar)));
|
||||
return root;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue