2013-01-04 16:01:50 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2013-01-04 16:01:50 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2013-01-04 16:01:50 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file text_file_writer.cpp
|
|
|
|
/// @brief Write plain text files line by line
|
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "text_file_writer.h"
|
|
|
|
|
|
|
|
#include "options.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2010-09-08 22:03:48 +02:00
|
|
|
#include <libaegisub/io.h>
|
2019-10-17 09:13:55 +02:00
|
|
|
#include <libaegisub/fs.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/charset_conv.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2010-09-08 22:03:48 +02:00
|
|
|
|
2014-07-07 16:11:01 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TextFileWriter::TextFileWriter(agi::fs::path const& filename, std::string encoding)
|
|
|
|
: file(new agi::io::Save(filename, true))
|
2012-03-25 06:05:38 +02:00
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
if (encoding.empty())
|
|
|
|
encoding = OPT_GET("App/Save Charset")->GetString();
|
2014-07-11 05:39:24 +02:00
|
|
|
if (encoding != "utf-8" && encoding != "UTF-8") {
|
2014-04-23 22:53:24 +02:00
|
|
|
conv = agi::make_unique<agi::charset::IconvWrapper>("utf-8", encoding.c_str(), true);
|
2014-04-30 15:50:42 +02:00
|
|
|
newline = conv->Convert(newline);
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
try {
|
2013-01-04 16:01:50 +01:00
|
|
|
// Write the BOM
|
|
|
|
WriteLineToFile("\xEF\xBB\xBF", false);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2010-06-03 22:32:25 +02:00
|
|
|
catch (agi::charset::ConversionFailure&) {
|
2009-07-14 23:28:49 +02:00
|
|
|
// If the BOM could not be converted to the target encoding it isn't needed
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-18 02:58:13 +02:00
|
|
|
TextFileWriter::~TextFileWriter() {
|
2019-10-17 09:13:55 +02:00
|
|
|
try {
|
|
|
|
file->Close();
|
|
|
|
}
|
|
|
|
catch (agi::fs::FileSystemError const&e) {
|
2019-10-17 22:14:21 +02:00
|
|
|
#if wxCHECK_VERSION (3, 1, 0)
|
2019-10-17 09:13:55 +02:00
|
|
|
wxString m = wxString::FromUTF8(e.GetMessage());
|
2019-10-17 22:14:21 +02:00
|
|
|
#else
|
|
|
|
wxString m = wxString::FromUTF8(e.GetMessage().c_str(), e.GetMessage().size());
|
|
|
|
#endif
|
2019-10-17 09:13:55 +02:00
|
|
|
if (!m.empty())
|
|
|
|
wxMessageBox(m, "Exception in agi::io::Save", wxOK | wxCENTRE | wxICON_ERROR);
|
|
|
|
else
|
|
|
|
wxMessageBox(e.GetMessage(), "Exception in agi::io::Save", wxOK | wxCENTRE | wxICON_ERROR);
|
|
|
|
}
|
2009-07-18 02:58:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 15:50:42 +02:00
|
|
|
void TextFileWriter::WriteLineToFile(std::string const& line, bool addLineBreak) {
|
|
|
|
if (conv) {
|
|
|
|
auto converted = conv->Convert(line);
|
2014-05-02 19:39:38 +02:00
|
|
|
file->Get().write(converted.data(), converted.size());
|
2014-04-30 15:50:42 +02:00
|
|
|
}
|
|
|
|
else
|
2014-05-02 19:39:38 +02:00
|
|
|
file->Get().write(line.data(), line.size());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2014-04-30 15:50:42 +02:00
|
|
|
if (addLineBreak)
|
2014-05-02 19:39:38 +02:00
|
|
|
file->Get().write(newline.data(), newline.size());
|
2010-06-24 03:24:32 +02:00
|
|
|
}
|