2013-01-04 07:01:50 -08:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-01-16 13:02:54 -08:00
|
|
|
//
|
2013-01-04 07:01:50 -08: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 13:02:54 -08:00
|
|
|
//
|
2013-01-04 07:01:50 -08: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 13:02:54 -08:00
|
|
|
//
|
2009-07-28 22:43:02 -07: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 13:02:54 -08:00
|
|
|
|
2013-01-04 07:01:50 -08:00
|
|
|
#include "text_file_writer.h"
|
|
|
|
|
|
|
|
#include "options.h"
|
2009-09-10 06:06:40 -07:00
|
|
|
|
2010-09-08 13:03:48 -07:00
|
|
|
#include <libaegisub/io.h>
|
2013-01-04 07:01:50 -08:00
|
|
|
#include <libaegisub/charset_conv.h>
|
2014-04-23 13:53:24 -07:00
|
|
|
#include <libaegisub/make_unique.h>
|
2010-09-08 13:03:48 -07:00
|
|
|
|
2014-07-07 07:11:01 -07:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2006-01-16 13:02:54 -08:00
|
|
|
|
2013-01-04 07:01:50 -08:00
|
|
|
TextFileWriter::TextFileWriter(agi::fs::path const& filename, std::string encoding)
|
|
|
|
: file(new agi::io::Save(filename, true))
|
2012-03-24 21:05:38 -07:00
|
|
|
{
|
2013-01-04 07:01:50 -08:00
|
|
|
if (encoding.empty())
|
|
|
|
encoding = OPT_GET("App/Save Charset")->GetString();
|
2014-07-10 20:39:24 -07:00
|
|
|
if (encoding != "utf-8" && encoding != "UTF-8") {
|
2014-04-23 13:53:24 -07:00
|
|
|
conv = agi::make_unique<agi::charset::IconvWrapper>("utf-8", encoding.c_str(), true);
|
2014-04-30 06:50:42 -07:00
|
|
|
newline = conv->Convert(newline);
|
|
|
|
}
|
2006-01-16 13:02:54 -08:00
|
|
|
|
2009-07-14 14:28:49 -07:00
|
|
|
try {
|
2013-01-04 07:01:50 -08:00
|
|
|
// Write the BOM
|
|
|
|
WriteLineToFile("\xEF\xBB\xBF", false);
|
2006-01-16 13:02:54 -08:00
|
|
|
}
|
2010-06-03 13:32:25 -07:00
|
|
|
catch (agi::charset::ConversionFailure&) {
|
2009-07-14 14:28:49 -07:00
|
|
|
// If the BOM could not be converted to the target encoding it isn't needed
|
2006-01-16 13:02:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 17:58:13 -07:00
|
|
|
TextFileWriter::~TextFileWriter() {
|
2013-01-04 07:01:50 -08:00
|
|
|
// Explicit empty destructor required with a unique_ptr to an incomplete class
|
2009-07-17 17:58:13 -07:00
|
|
|
}
|
|
|
|
|
2014-04-30 06:50:42 -07:00
|
|
|
void TextFileWriter::WriteLineToFile(std::string const& line, bool addLineBreak) {
|
|
|
|
if (conv) {
|
|
|
|
auto converted = conv->Convert(line);
|
2014-05-02 10:39:38 -07:00
|
|
|
file->Get().write(converted.data(), converted.size());
|
2014-04-30 06:50:42 -07:00
|
|
|
}
|
|
|
|
else
|
2014-05-02 10:39:38 -07:00
|
|
|
file->Get().write(line.data(), line.size());
|
2006-01-16 13:02:54 -08:00
|
|
|
|
2014-04-30 06:50:42 -07:00
|
|
|
if (addLineBreak)
|
2014-05-02 10:39:38 -07:00
|
|
|
file->Get().write(newline.data(), newline.size());
|
2010-06-23 18:24:32 -07:00
|
|
|
}
|