-// analyse some lines of the file trying to guess it's type.
-// if it fails, it assumes the native type for our platform.
-wxTextFileType wxTextFile::GuessType() const
-{
- // file should be opened and we must be in it's beginning
- wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
-
- // scan the file lines
- size_t nUnix = 0, // number of '\n's alone
- nDos = 0, // number of '\r\n'
- nMac = 0; // number of '\r's
-
- // we take MAX_LINES_SCAN in the beginning, middle and the end of file
- #define MAX_LINES_SCAN (10)
- size_t nCount = m_aLines.Count() / 3,
- nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
-
- #define AnalyseLine(n) \
- switch ( m_aTypes[n] ) { \
- case wxTextFileType_Unix: nUnix++; break; \
- case wxTextFileType_Dos: nDos++; break; \
- case wxTextFileType_Mac: nMac++; break; \
- default: wxFAIL_MSG(_("unknown line terminator")); \
- }
+ for (n = 0; n < nRead; n++)
+ {
+ ch = buf[n];
+ switch ( ch )
+ {
+ case '\n':
+ // Dos/Unix line termination
+ *strPtr = '\0';
+ AddLine(wxString(strBuf, conv),
+ chLast == '\r' ? wxTextFileType_Dos
+ : wxTextFileType_Unix);
+ strPtr = strBuf;
+ chLast = '\n';
+ break;
+
+ case '\r':
+ if ( chLast == '\r' )
+ {
+ // Mac empty line
+ AddLine(wxEmptyString, wxTextFileType_Mac);
+ }
+ else
+ chLast = '\r';
+ break;
+
+ default:
+ if ( chLast == '\r' )
+ {
+ // Mac line termination
+ *strPtr = '\0';
+ AddLine(wxString(strBuf, conv), wxTextFileType_Mac);
+ chLast = ch;
+ strPtr = strBuf;
+ *(strPtr++) = ch;
+ }
+ else
+ {
+ // add to the current line
+ *(strPtr++) = ch;
+ if ( strPtr == strEnd )
+ {
+ // we must allocate more memory
+ size_t size = strEnd - strBuf;
+ char *newBuf = new char[size + 1024];
+ memcpy(newBuf, strBuf, size);
+ delete[] strBuf;
+ strBuf = newBuf;
+ strEnd = strBuf + size + 1024;
+ strPtr = strBuf + size;
+ }
+ }
+ }
+ }
+ } while ( nRead == WXSIZEOF(buf) );