Return the ifstream from agi::io::Open by move

This commit is contained in:
Thomas Goyne 2013-02-10 06:38:54 -08:00
parent d99647ba28
commit 1f5484fedb
21 changed files with 61 additions and 73 deletions

View file

@ -48,24 +48,24 @@ public:
: nsUniversalDetector(NS_FILTER_ALL)
{
{
std::unique_ptr<std::ifstream> fp(agi::io::Open(file, true));
std::ifstream fp(agi::io::Open(file, true));
// If it's over 100 MB it's either binary or big enough that we won't
// be able to do anything useful with it anyway
fp->seekg(0, std::ios::end);
if (fp->tellg() > 100 * 1024 * 1024) {
fp.seekg(0, std::ios::end);
if (fp.tellg() > 100 * 1024 * 1024) {
list.emplace_back(1.f, "binary");
return;
}
fp->seekg(0, std::ios::beg);
fp.seekg(0, std::ios::beg);
std::streamsize binaryish = 0;
std::streamsize bytes = 0;
while (!mDone && *fp) {
while (!mDone && fp) {
char buf[4096];
fp->read(buf, sizeof(buf));
std::streamsize read = fp->gcount();
fp.read(buf, sizeof(buf));
std::streamsize read = fp.gcount();
HandleData(buf, (PRUint32)read);
// A dumb heuristic to detect binary files

View file

@ -30,18 +30,16 @@
namespace agi {
namespace io {
std::ifstream* Open(fs::path const& file, bool binary) {
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));
boost::filesystem::ifstream stream(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;
return std::move(stream);
}
Save::Save(fs::path const& file, bool binary)

View file

@ -21,22 +21,18 @@
#include "libaegisub/json.h"
#include <fstream>
#include <memory>
#include <sstream>
#include "libaegisub/fs.h"
#include "libaegisub/io.h"
#include "libaegisub/log.h"
namespace agi {
namespace json_util {
namespace agi { namespace json_util {
json::UnknownElement parse(std::istream *stream) {
json::UnknownElement parse(std::istream&& stream) {
try {
std::unique_ptr<std::istream> stream_deleter(stream);
json::UnknownElement root;
json::Reader::Read(root, *stream);
json::Reader::Read(root, stream);
return root;
} catch (json::Reader::ParseException& e) {
LOG_E("json/parse") << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1;
@ -57,17 +53,16 @@ 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(std::istringstream(default_config));
}
catch (json::Exception&) {
// Already logged in parse
return parse(new std::istringstream(default_config));
return parse(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(std::istringstream(default_config));
}
}
} // namespace json_util
} // namespace agi
} }

View file

@ -85,8 +85,7 @@ void Save(agi::fs::path const& filename, std::vector<int> const& keyframes) {
}
std::vector<int> Load(agi::fs::path const& filename) {
std::unique_ptr<std::ifstream> file(io::Open(filename));
std::istream &is(*file);
std::ifstream is(io::Open(filename));
std::string header;
getline(is, header);

View file

@ -89,8 +89,8 @@ void Options::ConfigNext(std::istream& stream) {
void Options::ConfigUser() {
try {
std::unique_ptr<std::istream> stream(io::Open(config_file));
LoadConfig(*stream, true);
std::ifstream stream(io::Open(config_file));
LoadConfig(stream, true);
}
catch (fs::FileNotFound const&) {
return;

View file

@ -35,15 +35,15 @@ namespace agi {
Thesaurus::Thesaurus(agi::fs::path const& dat_path, agi::fs::path const& idx_path)
: dat(io::Open(dat_path))
{
std::unique_ptr<std::ifstream> idx(io::Open(idx_path));
std::ifstream idx(io::Open(idx_path));
std::string encoding_name;
getline(*idx, encoding_name);
getline(idx, encoding_name);
std::string unused_entry_count;
getline(*idx, unused_entry_count);
getline(idx, unused_entry_count);
// Read the list of words and file offsets for those words
for (line_iterator<std::string> iter(*idx, encoding_name), end; iter != end; ++iter) {
for (line_iterator<std::string> iter(idx, encoding_name), end; iter != end; ++iter) {
std::vector<std::string> chunks;
boost::split(chunks, *iter, _1 == '|');
if (chunks.size() == 2)
@ -57,17 +57,17 @@ Thesaurus::~Thesaurus() { }
void Thesaurus::Lookup(std::string const& word, std::vector<Entry> *out) {
out->clear();
if (!dat.get()) return;
if (!dat) return;
std::map<std::string, int>::const_iterator it = offsets.find(word);
if (it == offsets.end()) return;
dat->seekg(it->second, std::ios::beg);
if (!dat->good()) return;
dat.seekg(it->second, std::ios::beg);
if (!dat.good()) return;
// First line is the word and meaning count
std::string temp;
getline(*dat, temp);
getline(dat, temp);
std::vector<std::string> header;
std::string converted(conv->Convert(temp));
boost::split(header, converted, _1 == '|');
@ -77,7 +77,7 @@ void Thesaurus::Lookup(std::string const& word, std::vector<Entry> *out) {
out->resize(meanings);
for (int i = 0; i < meanings; ++i) {
std::vector<std::string> line;
getline(*dat, temp);
getline(dat, temp);
std::string converted(conv->Convert(temp));
boost::split(line, converted, _1 == '|');

View file

@ -206,18 +206,18 @@ Framerate::Framerate(fs::path const& filename)
: denominator(default_denominator)
, numerator(0)
{
scoped_ptr<std::ifstream> file(agi::io::Open(filename));
std::ifstream file(agi::io::Open(filename));
std::string encoding = agi::charset::Detect(filename);
std::string line = *line_iterator<std::string>(*file, encoding);
std::string line = *line_iterator<std::string>(file, encoding);
if (line == "# timecode format v2") {
copy(line_iterator<int>(*file, encoding), line_iterator<int>(), back_inserter(timecodes));
copy(line_iterator<int>(file, encoding), line_iterator<int>(), back_inserter(timecodes));
SetFromTimecodes();
return;
}
if (line == "# timecode format v1" || line.substr(0, 7) == "Assume ") {
if (line[0] == '#')
line = *line_iterator<std::string>(*file, encoding);
numerator = v1_parse(line_iterator<std::string>(*file, encoding), line, timecodes, last);
line = *line_iterator<std::string>(file, encoding);
numerator = v1_parse(line_iterator<std::string>(file, encoding), line, timecodes, last);
return;
}

View file

@ -28,7 +28,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::ifstream Open(fs::path const& file, bool binary = false);
class Save {
std::ofstream *fp;

View file

@ -24,9 +24,8 @@ namespace agi {
namespace json_util {
/// Parse a JSON stream.
/// @param stream JSON stream, this is deleted internally.
/// @return json::UnknownElement
json::UnknownElement parse(std::istream *stream);
json::UnknownElement parse(std::istream&& stream);
/// Parse a JSON file.
/// @param file Path JSON to file

View file

@ -18,7 +18,7 @@
#include "fs_fwd.h"
#include <iosfwd>
#include <fstream>
#include <map>
#include <memory>
#include <string>
@ -32,13 +32,13 @@ class Thesaurus {
/// Map of word -> byte position in the data file
std::map<std::string, int> offsets;
/// Read handle to the data file
std::unique_ptr<std::istream> dat;
std::ifstream dat;
/// Converter from the data file's charset to UTF-8
std::unique_ptr<charset::IconvWrapper> conv;
public:
/// A pair of a word and synonyms for that word
typedef std::pair<std::string, std::vector<std::string> > Entry;
typedef std::pair<std::string, std::vector<std::string>> Entry;
/// Constructor
/// @param dat_path Path to data file

View file

@ -40,11 +40,11 @@ AssAttachment::AssAttachment(agi::fs::path const& name, AssEntryGroup group)
filename = filename.get().substr(0, filename.get().size() - 4) + "_0" + filename.get().substr(filename.get().size() - 4);
std::vector<char> data;
std::unique_ptr<std::istream> file(agi::io::Open(name, true));
file->seekg(0, std::ios::end);
data.resize(file->tellg());
file->seekg(0, std::ios::beg);
file->read(&data[0], data.size());
std::ifstream file(agi::io::Open(name, true));
file.seekg(0, std::ios::end);
data.resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&data[0], data.size());
entry_data = (group == ENTRY_FONT ? "fontname: " : "filename: ") + filename.get() + "\r\n";
entry_data = entry_data.get() + agi::ass::UUEncode(data);

View file

@ -66,8 +66,8 @@ void AssStyleStorage::Load(agi::fs::path const& filename) {
Clear();
try {
std::unique_ptr<std::ifstream> in(agi::io::Open(file));
for (auto const& line : agi::line_iterator<std::string>(*in)) {
std::ifstream in(agi::io::Open(file));
for (auto const& line : agi::line_iterator<std::string>(in)) {
try {
style.push_back(new AssStyle(line));
} catch(...) {

View file

@ -28,8 +28,6 @@
#include <libaegisub/io.h>
#include <libaegisub/charset_conv.h>
#include <fstream>
namespace Automation4 {
LuaScriptReader::LuaScriptReader(agi::fs::path const& filename)
: conv(new agi::charset::IconvWrapper(CharSetDetect::GetEncoding(filename).c_str(), "utf-8", false))
@ -41,11 +39,11 @@ namespace Automation4 {
const char *LuaScriptReader::Read(size_t *bytes_read) {
char in_buf[512];
file->read(in_buf, sizeof(in_buf));
file.read(in_buf, sizeof(in_buf));
const char *in = in_buf;
char *out = buf;
size_t in_bytes = file->gcount();
size_t in_bytes = file.gcount();
size_t out_bytes = sizeof(buf);
conv->Convert(&in, &in_bytes, &out, &out_bytes);

View file

@ -19,7 +19,7 @@
#include <libaegisub/fs_fwd.h>
#include <iosfwd>
#include <fstream>
#include <memory>
#include <string>
@ -29,7 +29,7 @@ namespace agi { namespace charset { class IconvWrapper; } }
namespace Automation4 {
class LuaScriptReader {
std::unique_ptr<agi::charset::IconvWrapper> conv;
std::unique_ptr<std::istream> file;
std::ifstream file;
char buf[512];
const char *Read(size_t *bytes_read);

View file

@ -302,9 +302,9 @@ void DialogShiftTimes::LoadHistory() {
history_box->Freeze();
try {
std::unique_ptr<std::istream> file(agi::io::Open(history_filename));
std::ifstream file(agi::io::Open(history_filename));
json::UnknownElement root;
json::Reader::Read(root, *file);
json::Reader::Read(root, file);
*history = root;
for (auto& history_entry : *history)

View file

@ -304,7 +304,7 @@ void DoCheck(bool interactive) {
throw VersionCheckError(from_wx(_("Could not connect to updates server.")));
stream << boost::format(
"GET %s?rev=%d&rel=%d&os=%s&lang=%s&aegilang=%s HTTP/1.0\r\n"
"GET %s.json?rev=%d&rel=%d&os=%s&lang=%s&aegilang=%s HTTP/1.0\r\n"
"User-Agent: Aegisub %s\r\n"
"Host: %s\r\n"
"Accept: */*\r\n"

View file

@ -140,7 +140,7 @@ bool AegisubApp::OnInit() {
// Try loading configuration from the install dir if one exists there
try {
auto conf_local(config::path->Decode("?data/config.json"));
std::unique_ptr<std::istream> localConfig(agi::io::Open(conf_local));
std::ifstream localConfig(agi::io::Open(conf_local));
config::opt = new agi::Options(conf_local, GET_DEFAULT_CONFIG(default_config));
// Local config, make ?user mean ?data so all user settings are placed in install dir

View file

@ -91,9 +91,9 @@ void HunspellSpellChecker::ReadUserDictionary() {
// Read the old contents of the user's dictionary
try {
std::unique_ptr<std::istream> stream(agi::io::Open(userDicPath));
std::ifstream stream(agi::io::Open(userDicPath));
copy_if(
++agi::line_iterator<std::string>(*stream), agi::line_iterator<std::string>(),
++agi::line_iterator<std::string>(stream), agi::line_iterator<std::string>(),
inserter(customWords, customWords.end()),
[](std::string const& str) { return !str.empty(); });
}

View file

@ -36,8 +36,8 @@ TextFileReader::TextFileReader(agi::fs::path const& filename, std::string encodi
{
if (encoding.empty())
encoding = CharSetDetect::GetEncoding(filename);
file.reset(agi::io::Open(filename, true));
iter = agi::line_iterator<std::string>(*file, encoding);
file = agi::io::Open(filename, true);
iter = agi::line_iterator<std::string>(file, encoding);
}
TextFileReader::~TextFileReader() {

View file

@ -22,7 +22,6 @@
#pragma once
#include <fstream>
#include <memory>
#include <string>
#include <libaegisub/fs_fwd.h>
@ -31,7 +30,7 @@
/// @class TextFileReader
/// @brief A line-based text file reader
class TextFileReader {
std::unique_ptr<std::ifstream> file;
std::ifstream file;
bool trim;
agi::line_iterator<std::string> iter;

View file

@ -43,7 +43,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(std::istringstream(GET_DEFAULT_CONFIG(default_toolbar)));
return root;
}