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
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Headers
|
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include "text_file_reader.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
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Constructor
|
|
|
|
TextFileReader::TextFileReader(wxString _filename,wxString enc,bool _trim) {
|
|
|
|
// Setup
|
|
|
|
open = false;
|
|
|
|
customConv = false;
|
|
|
|
trim = _trim;
|
|
|
|
filename = _filename;
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
Open();
|
|
|
|
|
|
|
|
// Set encoding
|
|
|
|
encoding = enc;
|
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;
|
2006-01-16 22:02:54 +01:00
|
|
|
SetEncodingConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// Destructor
|
|
|
|
TextFileReader::~TextFileReader() {
|
|
|
|
Close();
|
|
|
|
|
|
|
|
// Clean up conversion
|
|
|
|
if (customConv) delete conv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
// Determine file encoding
|
|
|
|
wxString TextFileReader::GetEncoding(const wxString _filename) {
|
|
|
|
// Prepare
|
|
|
|
using namespace std;
|
|
|
|
unsigned char b[4];
|
|
|
|
for (int i=0;i<4;i++) b[i] = 0;
|
|
|
|
|
|
|
|
// Read four bytes from file
|
2007-04-13 04:05:38 +02:00
|
|
|
#ifdef TEXT_READER_USE_STDIO
|
2006-08-28 22:00:51 +02:00
|
|
|
// TODO: maybe make this use posix-style fopen() api's instead as well?
|
|
|
|
HANDLE ifile = CreateFile(
|
|
|
|
_filename.c_str(), // filename
|
|
|
|
FILE_READ_DATA, // access mode
|
|
|
|
FILE_SHARE_READ, // share mode
|
|
|
|
0, // security descriptor
|
|
|
|
OPEN_EXISTING, // creation disposition
|
|
|
|
FILE_FLAG_SEQUENTIAL_SCAN, // flags
|
|
|
|
0); // template file
|
|
|
|
if (ifile == INVALID_HANDLE_VALUE) {
|
|
|
|
return _T("unknown");
|
|
|
|
}
|
|
|
|
DWORD numread;
|
|
|
|
if (!ReadFile(ifile, (char*)b, 4, &numread, 0)) {
|
|
|
|
// Unable to open
|
|
|
|
return _T("unknown");
|
|
|
|
}
|
|
|
|
if (numread < 4) {
|
|
|
|
// File too short to decide, assume local
|
|
|
|
return _T("Local");
|
|
|
|
}
|
|
|
|
CloseHandle(ifile);
|
|
|
|
#else
|
2006-01-16 22:02:54 +01:00
|
|
|
ifstream ifile;
|
2007-08-07 22:45:41 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
ifile.open(_filename.wc_str());
|
|
|
|
#else
|
2007-04-18 22:24:32 +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");
|
|
|
|
}
|
|
|
|
ifile.read((char*)b,4);
|
|
|
|
ifile.close();
|
2006-08-28 22:00:51 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
|
2008-01-01 23:42:29 +01:00
|
|
|
#ifdef WITH_UNIVCHARDET
|
2007-04-08 08:01:41 +02:00
|
|
|
// Use universalchardet library to detect charset
|
|
|
|
CharSetDetect det;
|
|
|
|
return det.GetEncoding(_filename);
|
2007-04-08 08:10:52 +02:00
|
|
|
#else
|
|
|
|
// Fall back to local
|
|
|
|
return _T("Local");
|
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////
|
|
|
|
// Set encoding configuration
|
|
|
|
void TextFileReader::SetEncodingConfiguration() {
|
|
|
|
// Set encoding configuration
|
|
|
|
swap = false;
|
|
|
|
Is16 = false;
|
|
|
|
customConv = false;
|
|
|
|
conv = NULL;
|
|
|
|
if (encoding == _T("UTF-8")) {
|
|
|
|
conv = new wxMBConvUTF8;
|
|
|
|
customConv = true;
|
|
|
|
}
|
|
|
|
else if (encoding == _T("UTF-16LE")) {
|
|
|
|
Is16 = true;
|
|
|
|
}
|
|
|
|
else if (encoding == _T("UTF-16BE")) {
|
|
|
|
Is16 = true;
|
|
|
|
swap = true;
|
|
|
|
}
|
|
|
|
else if (encoding == _T("UTF-7")) {
|
|
|
|
conv = new wxCSConv(encoding);
|
|
|
|
customConv = true;
|
|
|
|
}
|
|
|
|
else if (encoding == _T("Local")) {
|
|
|
|
conv = wxConvCurrent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
conv = new wxCSConv(encoding);
|
|
|
|
customConv = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
// Reads a line from file
|
|
|
|
wxString TextFileReader::ReadLineFromFile() {
|
|
|
|
Open();
|
2007-04-19 17:22:47 +02:00
|
|
|
wxString wxbuffer;
|
2007-04-22 04:03:40 +02:00
|
|
|
size_t bufAlloc = 1024;
|
2007-04-19 17:22:47 +02:00
|
|
|
wxbuffer.Alloc(bufAlloc);
|
|
|
|
#ifdef TEXT_READER_USE_STDIO
|
|
|
|
char buffer[512];
|
|
|
|
buffer[0] = 0;
|
|
|
|
#else
|
|
|
|
std::string buffer = "";
|
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Read UTF-16 line from file
|
|
|
|
if (Is16) {
|
|
|
|
char charbuffer[3];
|
|
|
|
charbuffer[2] = 0;
|
|
|
|
wchar_t ch = 0;
|
2007-04-21 00:17:42 +02:00
|
|
|
size_t len = 0;
|
2007-04-13 04:05:38 +02:00
|
|
|
#ifdef TEXT_READER_USE_STDIO
|
2006-08-28 22:00:51 +02:00
|
|
|
while (ch != L'\n' && !feof(file)) {
|
|
|
|
// Read two chars from file
|
|
|
|
fread(charbuffer, 2, 1, file);
|
|
|
|
#else
|
2006-01-16 22:02:54 +01:00
|
|
|
while (ch != L'\n' && !file.eof()) {
|
|
|
|
// Read two chars from file
|
2007-10-18 05:12:18 +02:00
|
|
|
charbuffer[0] = 0;
|
|
|
|
charbuffer[1] = 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
file.read(charbuffer,2);
|
2006-08-28 22:00:51 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Swap bytes for big endian
|
|
|
|
if (swap) {
|
2007-04-21 00:17:42 +02:00
|
|
|
register char aux = charbuffer[0];
|
2006-01-16 22:02:54 +01:00
|
|
|
charbuffer[0] = charbuffer[1];
|
|
|
|
charbuffer[1] = aux;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert two chars into a widechar and append to string
|
|
|
|
ch = *((wchar_t*)charbuffer);
|
2007-04-21 00:17:42 +02:00
|
|
|
if (len >= bufAlloc - 1) {
|
2007-04-19 17:22:47 +02:00
|
|
|
bufAlloc *= 2;
|
|
|
|
wxbuffer.Alloc(bufAlloc);
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
wxbuffer += ch;
|
2007-04-21 00:17:42 +02:00
|
|
|
len++;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read ASCII/UTF-8 line from file
|
|
|
|
else {
|
2007-04-13 04:05:38 +02:00
|
|
|
#ifdef TEXT_READER_USE_STDIO
|
2006-08-28 22:00:51 +02:00
|
|
|
while (1) {
|
|
|
|
buffer[511] = '\1';
|
|
|
|
if (fgets(buffer, 512, file)) {
|
|
|
|
// read succeeded
|
2007-01-03 04:56:40 +01:00
|
|
|
// FIXME, this might break on incomplete multibyte characters
|
2006-08-28 22:00:51 +02:00
|
|
|
wxString linepart(buffer, *conv);
|
|
|
|
wxbuffer += linepart;
|
2007-02-05 00:46:10 +01:00
|
|
|
if (buffer[511] == '\1' || buffer[510] == '\n') {
|
2006-08-28 22:00:51 +02:00
|
|
|
// our sentinel \1 wasn't overwritten, meaning an EOL was found
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// otherwise the sentinel \1 was overwritten (presumably with \0), so just loop on
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// hit EOF
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2006-01-16 22:02:54 +01:00
|
|
|
getline(file,buffer);
|
2008-01-19 02:59:50 +01:00
|
|
|
wxbuffer.Clear();
|
|
|
|
if (buffer.length()) wxbuffer = wxString(buffer.c_str(),*conv);
|
2006-08-28 22:00:51 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove line breaks
|
2007-04-19 17:22:47 +02:00
|
|
|
//wxbuffer.Replace(_T("\r"),_T("\0"));
|
|
|
|
//wxbuffer.Replace(_T("\n"),_T("\0"));
|
|
|
|
size_t len=wxbuffer.Length();
|
|
|
|
for (size_t i=0;i<len;i++) {
|
|
|
|
if (wxbuffer[i] == _T('\r') || wxbuffer[i] == _T('\n')) wxbuffer[i] = _T(' ');
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Remove BOM
|
2007-04-19 17:22:47 +02:00
|
|
|
if (wxbuffer.Length() > 0 && wxbuffer[0] == 0xFEFF) {
|
|
|
|
wxbuffer = wxbuffer.Mid(1);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trim
|
|
|
|
if (trim) {
|
2007-04-19 17:22:47 +02:00
|
|
|
wxbuffer.Trim(true);
|
|
|
|
wxbuffer.Trim(false);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2007-04-19 17:22:47 +02:00
|
|
|
return wxbuffer;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Open file
|
|
|
|
void TextFileReader::Open() {
|
|
|
|
if (open) return;
|
2007-04-13 04:05:38 +02:00
|
|
|
#ifdef TEXT_READER_USE_STDIO
|
2006-08-28 22:00:51 +02:00
|
|
|
// binary mode, because ascii mode is never to be trusted
|
|
|
|
file = _tfopen(filename.c_str(), _T("rb"));
|
|
|
|
if (file == 0) {
|
|
|
|
throw _T("Failed opening file for reading.");
|
|
|
|
}
|
2007-08-07 22:45:41 +02:00
|
|
|
#else
|
|
|
|
#ifdef WIN32
|
|
|
|
file.open(filename.wc_str(),std::ios::in | std::ios::binary);
|
2006-08-28 22:00:51 +02:00
|
|
|
#else
|
2007-04-18 22:24:32 +02:00
|
|
|
file.open(wxFNCONV(filename),std::ios::in | std::ios::binary);
|
2007-08-07 22:45:41 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
if (!file.is_open()) {
|
2006-08-28 22:00:51 +02:00
|
|
|
throw _T("Failed opening file for reading.");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2006-08-28 22:00:51 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
open = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// Close file
|
|
|
|
void TextFileReader::Close() {
|
|
|
|
if (!open) return;
|
2007-04-13 04:05:38 +02:00
|
|
|
#ifdef TEXT_READER_USE_STDIO
|
2006-08-28 22:00:51 +02:00
|
|
|
fclose(file);
|
|
|
|
#else
|
2006-01-16 22:02:54 +01:00
|
|
|
file.close();
|
2006-08-28 22:00:51 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
open = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
// Checks if there's more to read
|
|
|
|
bool TextFileReader::HasMoreLines() {
|
2007-04-13 04:05:38 +02:00
|
|
|
#ifdef TEXT_READER_USE_STDIO
|
2007-04-08 23:20:32 +02:00
|
|
|
if (encoding == _T("binary")) return false;
|
2006-08-28 22:00:51 +02:00
|
|
|
return !feof(file);
|
|
|
|
#else
|
2006-01-16 22:02:54 +01:00
|
|
|
return (!file.eof());
|
2006-08-28 22:00:51 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
// Ensure that charset is valid
|
|
|
|
void TextFileReader::EnsureValid(wxString enc) {
|
|
|
|
if (enc == _T("unknown") || enc == _T("UTF-32BE") || enc == _T("UTF-32LE")) {
|
|
|
|
wxString error = _T("Character set ");
|
|
|
|
error += enc;
|
|
|
|
error += _T(" is not supported.");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2007-04-08 08:01:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
// Get encoding being used
|
|
|
|
wxString TextFileReader::GetCurrentEncoding() {
|
|
|
|
return encoding;
|
|
|
|
}
|