Made text file reader use iostream on all platforms... this will probably need some testing, though, so old code is only #ifdefed out.

Originally committed to SVN as r1069.
This commit is contained in:
Rodrigo Braz Monteiro 2007-04-13 02:05:38 +00:00
parent d342a55418
commit f8a711a0db
2 changed files with 15 additions and 18 deletions

View File

@ -85,7 +85,7 @@ wxString TextFileReader::GetEncoding(const wxString _filename) {
for (int i=0;i<4;i++) b[i] = 0; for (int i=0;i<4;i++) b[i] = 0;
// Read four bytes from file // Read four bytes from file
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
// TODO: maybe make this use posix-style fopen() api's instead as well? // TODO: maybe make this use posix-style fopen() api's instead as well?
HANDLE ifile = CreateFile( HANDLE ifile = CreateFile(
_filename.c_str(), // filename _filename.c_str(), // filename
@ -118,12 +118,6 @@ wxString TextFileReader::GetEncoding(const wxString _filename) {
ifile.close(); ifile.close();
#endif #endif
// 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");
}
// Try to get the byte order mark from them // Try to get the byte order mark from them
if (b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) return _T("UTF-8"); 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] == 0xFF && b[1] == 0xFE && b[2] == 0x00 && b[3] == 0x00) return _T("UTF-32LE");
@ -136,6 +130,12 @@ wxString TextFileReader::GetEncoding(const wxString _filename) {
else if (b[0] == 0x00 && b[2] == 0x00) return _T("UTF-16BE"); else if (b[0] == 0x00 && b[2] == 0x00) return _T("UTF-16BE");
else if (b[1] == 0x00 && b[3] == 0x00) return _T("UTF-16LE"); else if (b[1] == 0x00 && b[3] == 0x00) return _T("UTF-16LE");
// 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");
}
#ifdef AUTODETECT_CHARSET #ifdef AUTODETECT_CHARSET
// Use universalchardet library to detect charset // Use universalchardet library to detect charset
CharSetDetect det; CharSetDetect det;
@ -193,7 +193,7 @@ wxString TextFileReader::ReadLineFromFile() {
char aux; char aux;
wchar_t ch = 0; wchar_t ch = 0;
int n = 0; int n = 0;
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
while (ch != L'\n' && !feof(file)) { while (ch != L'\n' && !feof(file)) {
// Read two chars from file // Read two chars from file
fread(charbuffer, 2, 1, file); fread(charbuffer, 2, 1, file);
@ -219,7 +219,7 @@ wxString TextFileReader::ReadLineFromFile() {
// Read ASCII/UTF-8 line from file // Read ASCII/UTF-8 line from file
else { else {
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
char buffer[512]; char buffer[512];
while (1) { while (1) {
buffer[511] = '\1'; buffer[511] = '\1';
@ -272,7 +272,7 @@ wxString TextFileReader::ReadLineFromFile() {
// Open file // Open file
void TextFileReader::Open() { void TextFileReader::Open() {
if (open) return; if (open) return;
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
// binary mode, because ascii mode is never to be trusted // binary mode, because ascii mode is never to be trusted
file = _tfopen(filename.c_str(), _T("rb")); file = _tfopen(filename.c_str(), _T("rb"));
if (file == 0) { if (file == 0) {
@ -292,7 +292,7 @@ void TextFileReader::Open() {
// Close file // Close file
void TextFileReader::Close() { void TextFileReader::Close() {
if (!open) return; if (!open) return;
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
fclose(file); fclose(file);
#else #else
file.close(); file.close();
@ -304,7 +304,7 @@ void TextFileReader::Close() {
////////////////////////////////// //////////////////////////////////
// Checks if there's more to read // Checks if there's more to read
bool TextFileReader::HasMoreLines() { bool TextFileReader::HasMoreLines() {
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
if (encoding == _T("binary")) return false; if (encoding == _T("binary")) return false;
return !feof(file); return !feof(file);
#else #else

View File

@ -34,8 +34,7 @@
// //
#ifndef TEXT_FILE_READER_H #pragma once
#define TEXT_FILE_READER_H
/////////// ///////////
@ -43,7 +42,7 @@
#include <wx/wxprec.h> #include <wx/wxprec.h>
#include <wx/dynarray.h> #include <wx/dynarray.h>
#include <fstream> #include <fstream>
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
#include <stdio.h> #include <stdio.h>
#endif #endif
@ -54,7 +53,7 @@ class TextFileReader {
private: private:
wxString filename; wxString filename;
wxString encoding; wxString encoding;
#ifdef WIN32 #ifdef TEXT_READER_USE_STDIO
FILE *file; FILE *file;
#else #else
std::ifstream file; std::ifstream file;
@ -82,5 +81,3 @@ public:
static wxString GetEncoding(const wxString filename); static wxString GetEncoding(const wxString filename);
}; };
#endif