mirror of https://github.com/odrling/Aegisub
Add _tmp before the extension when saving
This makes it easier to open the temporary file if an error occurs at some point in the saving process.
This commit is contained in:
parent
063e1bc0dc
commit
b968a7333b
|
@ -30,6 +30,16 @@
|
||||||
#include "libaegisub/log.h"
|
#include "libaegisub/log.h"
|
||||||
#include "libaegisub/util.h"
|
#include "libaegisub/util.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
std::string make_temp_name(std::string const& filename) {
|
||||||
|
std::string::size_type pos = filename.rfind('.');
|
||||||
|
if (pos == std::string::npos)
|
||||||
|
return filename + "_tmp";
|
||||||
|
|
||||||
|
return filename.substr(0, pos) + "_tmp" + filename.substr(pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace agi {
|
namespace agi {
|
||||||
namespace io {
|
namespace io {
|
||||||
|
|
||||||
|
@ -53,7 +63,10 @@ std::ifstream* Open(const std::string &file, bool binary) {
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
Save::Save(const std::string& file, bool binary): file_name(file) {
|
Save::Save(const std::string& file, bool binary)
|
||||||
|
: file_name(file)
|
||||||
|
, tmp_name(make_temp_name(file))
|
||||||
|
{
|
||||||
LOG_D("agi/io/save/file") << file;
|
LOG_D("agi/io/save/file") << file;
|
||||||
const std::string pwd = util::DirName(file);
|
const std::string pwd = util::DirName(file);
|
||||||
|
|
||||||
|
@ -68,12 +81,12 @@ Save::Save(const std::string& file, bool binary): file_name(file) {
|
||||||
std::ofstream fp_touch(ConvertW(file).c_str());
|
std::ofstream fp_touch(ConvertW(file).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
fp = new std::ofstream(ConvertW(file + "_tmp").c_str(), binary ? std::ios::binary : std::ios::out);
|
fp = new std::ofstream(ConvertW(tmp_name).c_str(), binary ? std::ios::binary : std::ios::out);
|
||||||
}
|
}
|
||||||
|
|
||||||
Save::~Save() {
|
Save::~Save() {
|
||||||
delete fp;
|
delete fp;
|
||||||
util::Rename(file_name + "_tmp", file_name);
|
util::Rename(tmp_name, file_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ofstream& Save::Get() {
|
std::ofstream& Save::Get() {
|
||||||
|
|
|
@ -36,6 +36,7 @@ std::ifstream* Open(const std::string &file, bool binary = false);
|
||||||
class Save {
|
class Save {
|
||||||
std::ofstream *fp;
|
std::ofstream *fp;
|
||||||
const std::string file_name;
|
const std::string file_name;
|
||||||
|
const std::string tmp_name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Save(const std::string& file, bool binary = false);
|
Save(const std::string& file, bool binary = false);
|
||||||
|
|
Loading…
Reference in New Issue