MCPcopy Create free account
hub / github.com/buggins/coolreader / AutodetectEncoding

Method AutodetectEncoding

crengine/src/lvxml.cpp:833–868  ·  view source on GitHub ↗

tries to autodetect text encoding

Source from the content-addressed store, hash-verified

831
832/// tries to autodetect text encoding
833bool 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
871bool LVFileParserBase::Seek( lvpos_t pos, int bytesToPrefetch )

Callers 1

LVReadTextFileFunction · 0.80

Calls 9

hasXmlTagsFunction · 0.85
AutodetectCodePageUtfFunction · 0.85
AutodetectCodePageFunction · 0.85
lString16Class · 0.85
GetPosMethod · 0.45
SetPosMethod · 0.45
GetSizeMethod · 0.45
ReadMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected