tries to autodetect text encoding
| 831 | |
| 832 | /// tries to autodetect text encoding |
| 833 | bool LVTextFileBase::AutodetectEncoding( bool utfOnly ) |
| 834 | { |
| 835 | char enc_name[32]; |
| 836 | char lang_name[32]; |
| 837 | lvpos_t oldpos = m_stream->GetPos(); |
| 838 | unsigned sz = CP_AUTODETECT_BUF_SIZE; |
| 839 | m_stream->SetPos( 0 ); |
| 840 | if ( sz>m_stream->GetSize() ) |
| 841 | sz = m_stream->GetSize(); |
| 842 | if ( sz < 16 ) |
| 843 | return false; |
| 844 | unsigned char * buf = new unsigned char[ sz ]; |
| 845 | lvsize_t bytesRead = 0; |
| 846 | if ( m_stream->Read( buf, sz, &bytesRead )!=LVERR_OK ) { |
| 847 | delete[] buf; |
| 848 | m_stream->SetPos( oldpos ); |
| 849 | return false; |
| 850 | } |
| 851 | |
| 852 | int res = 0; |
| 853 | bool hasTags = hasXmlTags(buf, sz); |
| 854 | if ( utfOnly ) |
| 855 | res = AutodetectCodePageUtf(buf, sz, enc_name, lang_name); |
| 856 | else |
| 857 | res = AutodetectCodePage(buf, sz, enc_name, lang_name, hasTags); |
| 858 | delete[] buf; |
| 859 | m_stream->SetPos( oldpos ); |
| 860 | if ( res) { |
| 861 | //CRLog::debug("Code page decoding results: encoding=%s, lang=%s", enc_name, lang_name); |
| 862 | m_lang_name = lString16( lang_name ); |
| 863 | SetCharset( lString16( enc_name ).c_str() ); |
| 864 | } |
| 865 | |
| 866 | // restore state |
| 867 | return res!=0 || utfOnly; |
| 868 | } |
| 869 | |
| 870 | /// seek to specified stream position |
| 871 | bool LVFileParserBase::Seek( lvpos_t pos, int bytesToPrefetch ) |
no test coverage detected