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
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
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>
|
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
|
|
|
|
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
|
|
|
, conv()
|
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
if (encoding.empty())
|
|
|
|
encoding = OPT_GET("App/Save Charset")->GetString();
|
|
|
|
if (boost::iequals(encoding, "utf-8"))
|
|
|
|
conv.reset(new agi::charset::IconvWrapper("utf-8", encoding.c_str(), true));
|
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
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void TextFileWriter::WriteLineToFile(std::string&& line, bool addLineBreak) {
|
|
|
|
if (addLineBreak)
|
2010-09-15 07:00:12 +02:00
|
|
|
#ifdef _WIN32
|
2013-01-04 16:01:50 +01:00
|
|
|
line += "\r\n";
|
2010-09-15 07:00:12 +02:00
|
|
|
#else
|
2013-01-04 16:01:50 +01:00
|
|
|
line += "\n";
|
2010-09-15 07:00:12 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (conv)
|
|
|
|
line = conv->Convert(line);
|
|
|
|
file->Get().write(line.data(), line.size());
|
2010-06-24 03:24:32 +02:00
|
|
|
}
|