MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / guessEncoding

Method guessEncoding

src/csvapplication.cpp:997–1074  ·  view source on GitHub ↗

* Returns guessed encoding and the length of a BOM sequence – or zero if no BOM is present. * TODO remove 'bom' as it's not needed */

Source from the content-addressed store, hash-verified

995 * TODO remove 'bom' as it's not needed
996 */
997std::pair<CsvDefinition::Encodings, int> CsvApplication::guessEncoding(std::istream *input, long streamLength) {
998 #ifdef DEBUG
999 CsvDefinition::BOMs bom = CsvDefinition::BOM_NONE;
1000 #endif
1001 CsvDefinition::Encodings enc = CsvDefinition::ENC_NONE;
1002 int bomBytes = 0;
1003 unsigned char octet[4];
1004
1005
1006 // reset stream
1007 input->clear();
1008 input->seekg(0);
1009
1010
1011 std::istreambuf_iterator<char> it(input->rdbuf());
1012 std::istreambuf_iterator<char> eos;
1013
1014
1015 // read first 4 bytes for BOM
1016 input->read((char *) &octet, 1);
1017 input->read((char *) &octet + 1, 1);
1018 input->read((char *) &octet + 2, 1);
1019 input->read((char *) &octet + 3, 1);
1020
1021 if( octet[0] == 0xEF && octet[1] == 0xBB && octet[2] == 0xBF ) {
1022 // UTF8 mit BOM
1023 #ifdef DEBUG
1024 bom = CsvDefinition::BOM_UTF8;
1025 #endif
1026 bomBytes = 3;
1027 enc = CsvDefinition::ENC_UTF8;
1028 } else if( octet[0] == 0x00 && octet[1] == 0x00 && octet[2] == 0xFE && octet[3] == 0xFF ) {
1029 // UTF32BE
1030 #ifdef DEBUG
1031 bom = CsvDefinition::BOM_UTF32BE;
1032 #endif
1033 bomBytes = 4;
1034 enc = CsvDefinition::ENC_UTF32BE;
1035 } else if( octet[0] == 0xFF && octet[1] == 0xFE && octet[2] == 0x00 && octet[2] == 0x00 ) {
1036 // UTF32LE
1037 #ifdef DEBUG
1038 bom = CsvDefinition::BOM_UTF32LE;
1039 #endif
1040 bomBytes = 4;
1041 enc = CsvDefinition::ENC_UTF32LE;
1042 } else if( octet[0] == 0xFE && octet[1] == 0xFF ) {
1043 // UTF16BE
1044 #ifdef DEBUG
1045 bom = CsvDefinition::BOM_UTF16BE;
1046 #endif
1047 bomBytes = 2;
1048 enc = CsvDefinition::ENC_UTF16BE;
1049 } else if( octet[0] == 0xFF && octet[1] == 0xFE ) {
1050 // UTF16LE
1051 #ifdef DEBUG
1052 bom = CsvDefinition::BOM_UTF16LE;
1053 #endif
1054 bomBytes = 2;

Callers

nothing calls this directly

Calls 3

readMethod · 0.80
is_validFunction · 0.50
clearMethod · 0.45

Tested by

no test coverage detected