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>
|
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
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2014-04-30 15:50:42 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define NEWLINE "\r\n"
|
|
|
|
#else
|
|
|
|
#define NEWLINE "\n"
|
|
|
|
#endif
|
|
|
|
|
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))
|
2014-04-30 15:50:42 +02:00
|
|
|
, newline(NEWLINE)
|
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-04-30 15:50:42 +02:00
|
|
|
if (boost::iequals(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() {
|
2013-01-04 16:01:50 +01:00
|
|
|
// Explicit empty destructor required with a unique_ptr to an incomplete class
|
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
|
|
|
}
|