2010-07-07 04:41:46 +02:00
|
|
|
// Copyright (c) 2010, Thomas Goyne
|
2006-01-16 22:02:54 +01:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file text_file_reader.cpp
|
|
|
|
/// @brief Read 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"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2009-07-14 23:28:49 +02:00
|
|
|
#include <assert.h>
|
2009-07-18 02:58:13 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
2010-08-03 00:14:11 +02:00
|
|
|
|
|
|
|
#include <wx/string.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
2008-01-16 19:29:29 +01:00
|
|
|
|
2010-07-07 04:41:46 +02:00
|
|
|
#include <libaegisub/io.h>
|
2010-06-01 10:21:30 +02:00
|
|
|
#include <libaegisub/log.h>
|
|
|
|
|
2007-04-08 08:01:41 +02:00
|
|
|
#include "charset_detect.h"
|
2010-07-07 04:41:46 +02:00
|
|
|
#include "compat.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "text_file_reader.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2010-06-03 22:32:25 +02:00
|
|
|
TextFileReader::TextFileReader(wxString const& filename, wxString encoding, bool trim)
|
2010-07-07 04:41:46 +02:00
|
|
|
: trim(trim)
|
2010-06-03 22:32:25 +02:00
|
|
|
{
|
2010-07-07 04:41:46 +02:00
|
|
|
if (encoding.empty()) encoding = CharSetDetect::GetEncoding(filename);
|
2012-08-18 05:13:36 +02:00
|
|
|
file.reset(agi::io::Open(STD_STR(filename), true));
|
2010-07-07 04:41:46 +02:00
|
|
|
iter = agi::line_iterator<wxString>(*file, STD_STR(encoding));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TextFileReader::~TextFileReader() {
|
|
|
|
}
|
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
wxString TextFileReader::ReadLineFromFile() {
|
2010-07-07 04:41:46 +02:00
|
|
|
wxString str = *iter;
|
|
|
|
++iter;
|
|
|
|
if (trim) str.Trim(true).Trim(false);
|
|
|
|
if (str.StartsWith(L"\uFEFF")) str = str.Mid(1);
|
|
|
|
return str;
|
|
|
|
}
|
2009-07-14 23:28:49 +02:00
|
|
|
|
2010-07-07 04:41:46 +02:00
|
|
|
namespace agi {
|
|
|
|
#ifdef _WIN32
|
|
|
|
template<> void line_iterator<wxString>::init() {
|
|
|
|
conv.reset(new agi::charset::IconvWrapper(encoding.c_str(), "utf-16le"));
|
2009-07-14 23:28:49 +02:00
|
|
|
}
|
2010-07-07 04:41:46 +02:00
|
|
|
template<> bool line_iterator<wxString>::convert(std::string &str) {
|
|
|
|
value = wxString(str.c_str(), wxMBConvUTF16LE(), str.size());
|
|
|
|
return true;
|
2009-07-14 23:28:49 +02:00
|
|
|
}
|
2010-07-07 04:41:46 +02:00
|
|
|
#else
|
|
|
|
template<> bool line_iterator<wxString>::convert(std::string &str) {
|
|
|
|
value = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|