2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AEGISUB
|
|
|
|
//
|
|
|
|
// Website: http://aegisub.cellosoft.com
|
|
|
|
// Contact: mailto:zeratul@cellosoft.com
|
|
|
|
//
|
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
2009-07-14 23:28:49 +02:00
|
|
|
#include <assert.h>
|
2009-07-18 02:58:13 +02:00
|
|
|
#include <errno.h>
|
2006-01-16 22:02:54 +01:00
|
|
|
#include "text_file_reader.h"
|
2009-07-18 02:58:13 +02:00
|
|
|
#include "charset_conv.h"
|
|
|
|
|
2008-01-16 19:29:29 +01:00
|
|
|
|
2008-01-01 23:42:29 +01:00
|
|
|
#ifdef WITH_UNIVCHARDET
|
2007-04-08 08:01:41 +02:00
|
|
|
#include "charset_detect.h"
|
2007-04-08 08:10:52 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
TextFileReader::TextFileReader(wxString filename, wxString enc, bool trim)
|
|
|
|
: encoding(enc), conv((iconv_t)-1), trim(trim), readComplete(false), currout(0), outptr(0), currentLine(0) {
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
file.open(filename.wc_str(),std::ios::in | std::ios::binary);
|
|
|
|
#else
|
|
|
|
file.open(wxFNCONV(filename),std::ios::in | std::ios::binary);
|
|
|
|
#endif
|
|
|
|
if (!file.is_open()) {
|
|
|
|
throw _T("Failed opening file for reading.");
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-02-20 22:32:58 +01:00
|
|
|
if (encoding.IsEmpty()) encoding = GetEncoding(filename);
|
2007-06-21 06:11:24 +02:00
|
|
|
if (encoding == _T("binary")) return;
|
2009-07-14 23:28:49 +02:00
|
|
|
encoding = AegisubCSConv::GetRealEncodingName(encoding);
|
|
|
|
conv = iconv_open(WCHAR_T_ENCODING, encoding.ToAscii());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TextFileReader::~TextFileReader() {
|
2009-07-14 23:28:49 +02:00
|
|
|
if (conv != (iconv_t)-1) iconv_close(conv);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2009-07-18 02:58:13 +02:00
|
|
|
wxString TextFileReader::GetEncoding(const wxString filename) {
|
2006-01-16 22:02:54 +01:00
|
|
|
// Prepare
|
|
|
|
unsigned char b[4];
|
2009-07-14 23:28:49 +02:00
|
|
|
memset(b, 0, sizeof(b));
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Read four bytes from file
|
2009-07-14 23:28:49 +02:00
|
|
|
std::ifstream ifile;
|
2008-03-21 03:41:46 +01:00
|
|
|
#ifdef __WINDOWS__
|
2009-07-18 02:58:13 +02:00
|
|
|
ifile.open(filename.wc_str());
|
2007-08-07 22:45:41 +02:00
|
|
|
#else
|
2009-07-18 02:58:13 +02:00
|
|
|
ifile.open(wxFNCONV(filename));
|
2007-08-07 22:45:41 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
if (!ifile.is_open()) {
|
|
|
|
return _T("unknown");
|
|
|
|
}
|
2009-07-14 23:28:49 +02:00
|
|
|
ifile.read(reinterpret_cast<char *>(b),4);
|
2006-01-16 22:02:54 +01:00
|
|
|
ifile.close();
|
|
|
|
|
|
|
|
// Try to get the byte order mark from them
|
|
|
|
if (b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) return _T("UTF-8");
|
|
|
|
else if (b[0] == 0xFF && b[1] == 0xFE && b[2] == 0x00 && b[3] == 0x00) return _T("UTF-32LE");
|
|
|
|
else if (b[0] == 0x00 && b[1] == 0x00 && b[2] == 0xFE && b[3] == 0xFF) return _T("UTF-32BE");
|
|
|
|
else if (b[0] == 0xFF && b[1] == 0xFE) return _T("UTF-16LE");
|
|
|
|
else if (b[0] == 0xFE && b[1] == 0xFF) return _T("UTF-16BE");
|
|
|
|
else if (b[0] == 0x2B && b[1] == 0x2F && b[2] == 0x76) return _T("UTF-7");
|
|
|
|
|
|
|
|
// Try to guess UTF-16
|
2007-06-21 06:11:24 +02:00
|
|
|
else if (b[0] == 0 && b[1] >= 32 && b[2] == 0 && b[3] >= 32) return _T("UTF-16BE");
|
|
|
|
else if (b[0] >= 32 && b[1] == 0 && b[2] >= 32 && b[3] == 0) return _T("UTF-16LE");
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2007-04-13 04:05:38 +02:00
|
|
|
// If any of the first four bytes are under 0x20 (the first printable character),
|
|
|
|
// except for 9-13 range, assume binary
|
|
|
|
for (int i=0;i<4;i++) {
|
|
|
|
if (b[i] < 9 || (b[i] > 13 && b[i] < 32)) return _T("binary");
|
|
|
|
}
|
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
#ifdef WITH_UNIVCHARDET
|
2007-04-08 08:01:41 +02:00
|
|
|
// Use universalchardet library to detect charset
|
|
|
|
CharSetDetect det;
|
2009-07-18 02:58:13 +02:00
|
|
|
return det.GetEncoding(filename);
|
2009-07-14 23:28:49 +02:00
|
|
|
#else
|
2007-04-08 08:10:52 +02:00
|
|
|
// Fall back to local
|
|
|
|
return _T("Local");
|
2009-07-14 23:28:49 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
wchar_t TextFileReader::GetWChar() {
|
|
|
|
// If there's already some converted characters waiting, return the next one
|
|
|
|
if (++currout < outptr) {
|
|
|
|
return *currout;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
if (file.eof()) return 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
// Otherwise convert another block
|
|
|
|
char inbuf[64];
|
|
|
|
char *inptr = inbuf;
|
|
|
|
size_t inbytesleft = sizeof(inbuf) - 4;
|
|
|
|
int bytesAdded = 0;
|
|
|
|
memset(inbuf, 0, inbytesleft);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
outptr = outbuf;
|
|
|
|
outbytesleft = sizeof(outbuf);
|
|
|
|
currout = outbuf;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
file.read(inbuf, inbytesleft);
|
|
|
|
inbytesleft = file.gcount();
|
2009-07-16 17:10:40 +02:00
|
|
|
if (inbytesleft == 0)
|
|
|
|
return 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
do {
|
|
|
|
size_t ret = iconv(conv, &inptr, &inbytesleft, reinterpret_cast<char **>(&outptr), &outbytesleft);
|
|
|
|
if (ret != (size_t)-1) break;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
int err = errno;
|
|
|
|
// If 64 chars do not fit into 256 wchar_ts the environment is so bizzare that doing
|
|
|
|
// anything is probably futile
|
|
|
|
assert(err != E2BIG);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
// (Hopefully) the edge of the buffer happened to split a multibyte character, so keep
|
|
|
|
// adding one byte to the input buffer until either it succeeds or we add enough bytes to
|
|
|
|
// complete any character
|
|
|
|
if (++bytesAdded > 3)
|
|
|
|
throw wxString::Format(_T("Invalid input character found near line %u"), currentLine);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
file.read(inptr + inbytesleft, 1);
|
|
|
|
inbytesleft++;
|
2009-07-18 02:58:13 +02:00
|
|
|
} while (!file.eof() && file.gcount());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
if (outptr > outbuf)
|
|
|
|
return *currout;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
throw wxString::Format(_T("Invalid input character found near line %u"), currentLine);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
wxString TextFileReader::ReadLineFromFile() {
|
|
|
|
wxString buffer;
|
|
|
|
size_t bufAlloc = 1024;
|
|
|
|
buffer.Alloc(bufAlloc);
|
|
|
|
|
|
|
|
currentLine++;
|
|
|
|
// Read a line
|
|
|
|
wchar_t ch;
|
|
|
|
size_t len = 0;
|
|
|
|
for (ch = GetWChar(); ch != L'\n' && ch != 0; ch = GetWChar()) {
|
|
|
|
if (ch == L'\r') continue;
|
2009-07-18 02:58:13 +02:00
|
|
|
// Skip the BOM -- we don't need it as the encoding is already known
|
|
|
|
// and it sometimes causes conversion problems
|
2009-07-14 23:28:49 +02:00
|
|
|
if (ch == 0xFEFF && len == 0) continue;
|
|
|
|
|
|
|
|
if (len >= bufAlloc - 1) {
|
|
|
|
bufAlloc *= 2;
|
|
|
|
buffer.Alloc(bufAlloc);
|
|
|
|
}
|
|
|
|
buffer += ch;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
if (ch == 0)
|
|
|
|
readComplete = true;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
// Trim
|
|
|
|
if (trim) {
|
|
|
|
buffer.Trim(true);
|
|
|
|
buffer.Trim(false);
|
|
|
|
}
|
|
|
|
return buffer;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFileReader::HasMoreLines() {
|
2009-07-14 23:28:49 +02:00
|
|
|
return !readComplete;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextFileReader::EnsureValid(wxString enc) {
|
2009-07-14 23:28:49 +02:00
|
|
|
if (enc == _T("binary")) return;
|
|
|
|
|
|
|
|
enc = AegisubCSConv::GetRealEncodingName(enc);
|
|
|
|
iconv_t cd = iconv_open(WCHAR_T_ENCODING, enc.ToAscii());
|
|
|
|
bool canOpen = cd != (iconv_t)-1;
|
|
|
|
iconv_close(cd);
|
|
|
|
if (!canOpen) {
|
|
|
|
throw wxString::Format(_T("Character set %s is not supported."), enc.c_str());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
2007-04-08 08:01:41 +02:00
|
|
|
|
|
|
|
wxString TextFileReader::GetCurrentEncoding() {
|
|
|
|
return encoding;
|
|
|
|
}
|