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

This reverts commit 1f5484fedb.

Move constructors for fstreams are not implemented yet in libstdc++.

Closes #1578.
This commit is contained in:
Thomas Goyne 2013-02-16 19:47:31 -08:00
parent 84410f770b
commit a9f83663e8
21 changed files with 73 additions and 61 deletions

View File

@ -48,24 +48,24 @@ public:
: nsUniversalDetector(NS_FILTER_ALL)
{
{
std::ifstream fp(agi::io::Open(file, true));
std::unique_ptr<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,16 +30,18 @@
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);
boost::filesystem::ifstream stream(file, (binary ? std::ios::binary : std::ios::in));
auto stream = new boost::filesystem::ifstream(file, (binary ? std::ios::binary : std::ios::in));
if (stream.fail())
if (stream->fail()) {
delete stream;
throw IOFatal("Unknown fatal error as occurred");
}
return std::move(stream);
return stream;
}
Save::Save(fs::path const& file, bool binary)

View File

@ -21,18 +21,22 @@
#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;
@ -53,16 +57,17 @@ json::UnknownElement file(agi::fs::path const& file, const std::string &default_
}
catch (fs::FileNotFound const&) {
// Not an error
return parse(std::istringstream(default_config));
return parse(new std::istringstream(default_config));
}
catch (json::Exception&) {
// Already logged in parse
return parse(std::istringstream(default_config));
return parse(new std::istringstream(default_config));
}
catch (agi::Exception& e) {
LOG_E("json/file") << "Unexpected error when reading config file " << file << ": " << e.GetMessage();
return parse(std::istringstream(default_config));
return parse(new std::istringstream(default_config));
}
}
} }
} // namespace json_util
} // namespace agi

View File

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

View File

@ -89,8 +89,8 @@ void Options::ConfigNext(std::istream& stream) {
void Options::ConfigUser() {
try {
std::ifstream stream(io::Open(config_file));
LoadConfig(stream, true);
std::unique_ptr<std::istream> 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::ifstream idx(io::Open(idx_path));
std::unique_ptr<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) return;
if (!dat.get()) 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)
{
std::ifstream file(agi::io::Open(filename));
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);
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,8 +24,9 @@ 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 <fstream>
#include <iosfwd>
#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::ifstream dat;
std::unique_ptr<std::istream> 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::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());
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());
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::ifstream in(agi::io::Open(file));
for (auto const& line : agi::line_iterator<std::string>(in)) {
std::unique_ptr<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,6 +28,8 @@
#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))
@ -39,11 +41,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 <fstream>
#include <iosfwd>
#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::ifstream file;
std::unique_ptr<std::istream> 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::ifstream file(agi::io::Open(history_filename));
std::unique_ptr<std::istream> 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.json?rev=%d&rel=%d&os=%s&lang=%s&aegilang=%s HTTP/1.0\r\n"
"GET %s?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::ifstream localConfig(agi::io::Open(conf_local));
std::unique_ptr<std::istream> 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::ifstream stream(agi::io::Open(userDicPath));
std::unique_ptr<std::istream> 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 = agi::io::Open(filename, true);
iter = agi::line_iterator<std::string>(file, encoding);
file.reset(agi::io::Open(filename, true));
iter = agi::line_iterator<std::string>(*file, encoding);
}
TextFileReader::~TextFileReader() {

View File

@ -22,6 +22,7 @@
#pragma once
#include <fstream>
#include <memory>
#include <string>
#include <libaegisub/fs_fwd.h>
@ -30,7 +31,7 @@
/// @class TextFileReader
/// @brief A line-based text file reader
class TextFileReader {
std::ifstream file;
std::unique_ptr<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(std::istringstream(GET_DEFAULT_CONFIG(default_toolbar)));
root = agi::json_util::parse(new std::istringstream(GET_DEFAULT_CONFIG(default_toolbar)));
return root;
}