rewrote wxTextFile::OnRead to handle variable-length encodings correctly

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13617 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2002-01-17 23:41:02 +00:00
parent 8e92ccef9c
commit d9ade1df2f

View File

@@ -94,38 +94,42 @@ bool wxTextFile::OnRead(wxMBConv& conv)
// file should be opened and we must be in it's beginning // file should be opened and we must be in it's beginning
wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
#if wxUSE_UNICODE char *strBuf, *strPtr, *strEnd;
char conv_mbBuf[2];
wchar_t conv_wcBuf[2];
conv_mbBuf[1] = 0;
#else
(void)conv;
#endif
wxString str;
char ch, chLast = '\0'; char ch, chLast = '\0';
char buf[1024]; char buf[1024];
int n, nRead; int n, nRead;
do {
strPtr = strBuf = new char[1024];
strEnd = strBuf + 1024;
do
{
nRead = m_file.Read(buf, WXSIZEOF(buf)); nRead = m_file.Read(buf, WXSIZEOF(buf));
if ( nRead == wxInvalidOffset ) { if ( nRead == wxInvalidOffset )
{
// read error (error message already given in wxFile::Read) // read error (error message already given in wxFile::Read)
delete[] strBuf;
return FALSE; return FALSE;
} }
for ( n = 0; n < nRead; n++ ) { for (n = 0; n < nRead; n++)
{
ch = buf[n]; ch = buf[n];
switch ( ch ) { switch ( ch )
{
case '\n': case '\n':
// Dos/Unix line termination // Dos/Unix line termination
AddLine(str, chLast == '\r' ? wxTextFileType_Dos *strPtr = '\0';
AddLine(wxString(strBuf, conv),
chLast == '\r' ? wxTextFileType_Dos
: wxTextFileType_Unix); : wxTextFileType_Unix);
str.Empty(); strPtr = strBuf;
chLast = '\n'; chLast = '\n';
break; break;
case '\r': case '\r':
if ( chLast == '\r' ) { if ( chLast == '\r' )
{
// Mac empty line // Mac empty line
AddLine(wxEmptyString, wxTextFileType_Mac); AddLine(wxEmptyString, wxTextFileType_Mac);
} }
@@ -134,39 +138,44 @@ bool wxTextFile::OnRead(wxMBConv& conv)
break; break;
default: default:
if ( chLast == '\r' ) { if ( chLast == '\r' )
{
// Mac line termination // Mac line termination
AddLine(str, wxTextFileType_Mac); *strPtr = '\0';
AddLine(wxString(strBuf, conv), wxTextFileType_Mac);
chLast = ch; chLast = ch;
#if wxUSE_UNICODE strPtr = strBuf;
conv_mbBuf[0] = ch; *(strPtr++) = ch;
if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
conv_wcBuf[0] = ch;
str = conv_wcBuf[0];
#else
str = ch;
#endif
} }
else { else
{
// add to the current line // add to the current line
#if wxUSE_UNICODE *(strPtr++) = ch;
conv_mbBuf[0] = ch; if ( strPtr == strEnd )
if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) {
conv_wcBuf[0] = ch; // we must allocate more memory
str += conv_wcBuf[0]; size_t size = strEnd - strBuf;
#else char *newBuf = new char[size + 1024];
str += ch; memcpy(newBuf, strBuf, size);
#endif delete[] strBuf;
strBuf = newBuf;
strEnd = strBuf + size + 1024;
strPtr = strBuf + size;
}
} }
} }
} }
} while ( nRead == WXSIZEOF(buf) ); } while ( nRead == WXSIZEOF(buf) );
// anything in the last line? // anything in the last line?
if ( !str.IsEmpty() ) { if ( strPtr != strBuf )
AddLine(str, wxTextFileType_None); // no line terminator {
*strPtr = '\0';
AddLine(wxString(strBuf, conv),
wxTextFileType_None); // no line terminator
} }
delete[] strBuf;
return TRUE; return TRUE;
} }