2013-10-24 16:44:38 +00:00
|
|
|
#include "drw_textcodec.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "../drw_base.h"
|
|
|
|
#include "drw_cptables.h"
|
|
|
|
#include "drw_cptable932.h"
|
|
|
|
#include "drw_cptable936.h"
|
|
|
|
#include "drw_cptable949.h"
|
|
|
|
#include "drw_cptable950.h"
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
DRW_TextCodec::DRW_TextCodec()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
version = DRW::AC1021;
|
2015-02-24 09:07:40 +00:00
|
|
|
conv = new DRW_Converter( NULL, 0 );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
DRW_TextCodec::~DRW_TextCodec()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
delete conv;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
void DRW_TextCodec::setVersion( std::string* v )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
std::string versionStr = *v;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( versionStr == "AC1009" || versionStr == "AC1006" )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
version = DRW::AC1009;
|
|
|
|
cp = "ANSI_1252";
|
2013-10-28 08:06:38 +00:00
|
|
|
setCodePage( &cp );
|
|
|
|
}
|
|
|
|
else if( versionStr == "AC1012" || versionStr == "AC1014"
|
|
|
|
|| versionStr == "AC1015" || versionStr == "AC1018" )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
version = DRW::AC1015;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( cp.empty() ) // codepage not set, initialize
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
cp = "ANSI_1252";
|
2013-10-28 08:06:38 +00:00
|
|
|
setCodePage( &cp );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
version = DRW::AC1021;
|
|
|
|
cp = "ANSI_1252";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
void DRW_TextCodec::setCodePage( std::string* c )
|
|
|
|
{
|
|
|
|
cp = correctCodePage( *c );
|
2013-10-24 16:44:38 +00:00
|
|
|
delete conv;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( version == DRW::AC1009 || version == DRW::AC1015 )
|
|
|
|
{
|
|
|
|
if( cp == "ANSI_874" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table874, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_932" )
|
|
|
|
conv = new DRW_Conv932Table( DRW_Table932, DRW_LeadTable932,
|
2015-02-24 09:07:40 +00:00
|
|
|
DRW_DoubleTable932, CPLENGHT932 );
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( cp == "ANSI_936" )
|
|
|
|
conv = new DRW_ConvDBCSTable( DRW_Table936, DRW_LeadTable936,
|
2015-02-24 09:07:40 +00:00
|
|
|
DRW_DoubleTable936, CPLENGHT936 );
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( cp == "ANSI_949" )
|
|
|
|
conv = new DRW_ConvDBCSTable( DRW_Table949, DRW_LeadTable949,
|
2015-02-24 09:07:40 +00:00
|
|
|
DRW_DoubleTable949, CPLENGHT949 );
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( cp == "ANSI_950" )
|
|
|
|
conv = new DRW_ConvDBCSTable( DRW_Table950, DRW_LeadTable950,
|
2015-02-24 09:07:40 +00:00
|
|
|
DRW_DoubleTable950, CPLENGHT950 );
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( cp == "ANSI_1250" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1250, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_1251" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1251, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_1253" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1253, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_1254" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1254, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_1255" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1255, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_1256" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1256, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_1257" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1257, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "ANSI_1258" )
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1258, CPLENGHTCOMMON );
|
|
|
|
else if( cp == "UTF-8" ) // DXF older than 2007 are write in win codepages
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
cp = "ANSI_1252";
|
2013-10-28 08:06:38 +00:00
|
|
|
conv = new DRW_Converter( NULL, 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
conv = new DRW_ConvTable( DRW_Table1252, CPLENGHTCOMMON );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conv = new DRW_Converter( NULL, 0 );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_TextCodec::toUtf8( std::string s )
|
|
|
|
{
|
|
|
|
return conv->toUtf8( &s );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_TextCodec::fromUtf8( std::string s )
|
|
|
|
{
|
|
|
|
return conv->fromUtf8( &s );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_Converter::toUtf8( std::string* s )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
std::string result;
|
|
|
|
int j = 0;
|
|
|
|
unsigned int i = 0;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
for( i = 0; i < s->length(); i++ )
|
|
|
|
{
|
|
|
|
unsigned char c = s->at( i );
|
|
|
|
|
|
|
|
if( c < 0x80 ) // ascii check for /U+????
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
if( c == '\\' && i + 6 < s->length() && s->at( i + 1 ) == 'U' && s->at( i + 2 ) == '+' )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
|
|
|
result += s->substr( j, i - j );
|
|
|
|
result += encodeText( s->substr( i, 7 ) );
|
|
|
|
i += 6;
|
|
|
|
j = i + 1;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else if( c < 0xE0 ) // 2 bits
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
i++;
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else if( c < 0xF0 ) // 3 bits
|
|
|
|
{
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
else if( c < 0xF8 ) // 4 bits
|
|
|
|
{
|
|
|
|
i += 3;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
result += s->substr( j );
|
2013-10-24 16:44:38 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
std::string DRW_ConvTable::fromUtf8( std::string* s )
|
|
|
|
{
|
|
|
|
std::string result;
|
2015-02-24 09:07:40 +00:00
|
|
|
bool notFound;
|
|
|
|
int code;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2015-02-24 09:07:40 +00:00
|
|
|
int j = 0;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < s->length(); i++ )
|
|
|
|
{
|
|
|
|
unsigned char c = s->at( i );
|
|
|
|
|
|
|
|
if( c > 0x7F ) // need to decode
|
|
|
|
{
|
|
|
|
result += s->substr( j, i - j );
|
|
|
|
std::string part1 = s->substr( i, 4 );
|
2015-02-24 09:07:40 +00:00
|
|
|
int l;
|
2013-10-28 08:06:38 +00:00
|
|
|
code = decodeNum( part1, &l );
|
2015-02-24 09:07:40 +00:00
|
|
|
j = i + l;
|
|
|
|
i = j - 1;
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = true;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
for( int k = 0; k<cpLenght; k++ )
|
|
|
|
{
|
|
|
|
if( table[k] == code )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
result += CPOFFSET + k; // translate from table
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( notFound )
|
|
|
|
result += decodeText( code );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
result += s->substr( j );
|
2013-10-24 16:44:38 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_ConvTable::toUtf8( std::string* s )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
std::string res;
|
|
|
|
std::string::iterator it;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2015-02-24 09:07:40 +00:00
|
|
|
for( it = s->begin(); it < s->end(); ++it )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
unsigned char c = *it;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( c < 0x80 )
|
|
|
|
{
|
|
|
|
// check for \U+ encoded text
|
|
|
|
if( c == '\\' )
|
|
|
|
{
|
|
|
|
if( it + 6 < s->end() && *(it + 1) == 'U' && *(it + 2) == '+' )
|
|
|
|
{
|
|
|
|
res += encodeText( std::string( it, it + 7 ) );
|
|
|
|
it += 6;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
res += c; // no \U+ encoded text write
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res += c; // c!='\' ascii char write
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
else // end c < 0x80
|
|
|
|
{
|
|
|
|
res += encodeNum( table[c - 0x80] ); // translate from table
|
|
|
|
}
|
|
|
|
} // end for
|
2013-10-24 16:44:38 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2017-09-23 09:20:10 +00:00
|
|
|
std::string DRW_Converter::encodeText( const std::string& stmp )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
int code;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
#if defined(__APPLE__)
|
2013-10-28 08:06:38 +00:00
|
|
|
int Succeeded = sscanf( &( stmp.substr( 3, 4 )[0]), "%x", &code );
|
|
|
|
|
|
|
|
if( !Succeeded || Succeeded == EOF )
|
2013-10-24 16:44:38 +00:00
|
|
|
code = 0;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
#else
|
2013-10-28 08:06:38 +00:00
|
|
|
std::istringstream sd( stmp.substr( 3, 4 ) );
|
2013-10-24 16:44:38 +00:00
|
|
|
sd >> std::hex >> code;
|
|
|
|
#endif
|
2013-10-28 08:06:38 +00:00
|
|
|
return encodeNum( code );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_Converter::decodeText( int c )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
std::string res = "\\U+";
|
|
|
|
std::string num;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
#if defined(__APPLE__)
|
2013-10-28 08:06:38 +00:00
|
|
|
std::string str( 16, '\0' );
|
|
|
|
snprintf( &(str[0]), 16, "%04X", c );
|
2013-10-24 16:44:38 +00:00
|
|
|
num = str;
|
|
|
|
#else
|
|
|
|
std::stringstream ss;
|
2013-10-28 08:06:38 +00:00
|
|
|
ss << std::uppercase << std::setfill( '0' ) << std::setw( 4 ) << std::hex << c;
|
2013-10-24 16:44:38 +00:00
|
|
|
ss >> num;
|
|
|
|
#endif
|
|
|
|
res += num;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_Converter::encodeNum( int c )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
unsigned char ret[5];
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( c < 128 ) // 0-7F US-ASCII 7 bits
|
|
|
|
{
|
|
|
|
ret[0] = c;
|
|
|
|
ret[1] = 0;
|
|
|
|
}
|
|
|
|
else if( c < 0x800 ) // 80-07FF 2 bytes
|
|
|
|
{
|
|
|
|
ret[0] = 0xC0 | (c >> 6);
|
|
|
|
ret[1] = 0x80 | (c & 0x3f);
|
|
|
|
ret[2] = 0;
|
|
|
|
}
|
|
|
|
else if( c< 0x10000 ) // 800-FFFF 3 bytes
|
|
|
|
{
|
|
|
|
ret[0] = 0xe0 | (c >> 12);
|
|
|
|
ret[1] = 0x80 | ( (c >> 6) & 0x3f );
|
|
|
|
ret[2] = 0x80 | (c & 0x3f);
|
|
|
|
ret[3] = 0;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
else // 10000-10FFFF 4 bytes
|
|
|
|
{
|
|
|
|
ret[0] = 0xf0 | (c >> 18);
|
|
|
|
ret[1] = 0x80 | ( (c >> 12) & 0x3f );
|
|
|
|
ret[2] = 0x80 | ( (c >> 6) & 0x3f );
|
|
|
|
ret[3] = 0x80 | (c & 0x3f);
|
|
|
|
ret[4] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string( (char*) ret );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
/** 's' is a string with at least 4 bytes lenght
|
|
|
|
** returned 'b' is byte lenght of encoded char: 2,3 or 4
|
|
|
|
**/
|
2013-10-28 08:06:38 +00:00
|
|
|
int DRW_Converter::decodeNum( std::string s, int* b )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
int code = 0;
|
|
|
|
unsigned char c = s.at( 0 );
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( (c & 0xE0) == 0xC0 ) // 2 bytes
|
|
|
|
{
|
|
|
|
code = ( c & 0x1F) << 6;
|
|
|
|
code = (s.at( 1 ) & 0x3F) | code;
|
2015-02-24 09:07:40 +00:00
|
|
|
*b = 2;
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else if( (c & 0xF0) == 0xE0 ) // 3 bytes
|
|
|
|
{
|
|
|
|
code = ( c & 0x0F) << 12;
|
|
|
|
code = ( (s.at( 1 ) & 0x3F) << 6 ) | code;
|
|
|
|
code = (s.at( 2 ) & 0x3F) | code;
|
2015-02-24 09:07:40 +00:00
|
|
|
*b = 3;
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else if( (c & 0xF8) == 0xF0 ) // 4 bytes
|
|
|
|
{
|
|
|
|
code = ( c & 0x07) << 18;
|
|
|
|
code = ( (s.at( 1 ) & 0x3F) << 12 ) | code;
|
|
|
|
code = ( (s.at( 2 ) & 0x3F) << 6 ) | code;
|
|
|
|
code = (s.at( 3 ) & 0x3F) | code;
|
2015-02-24 09:07:40 +00:00
|
|
|
*b = 4;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
std::string DRW_ConvDBCSTable::fromUtf8( std::string* s )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
std::string result;
|
2015-02-24 09:07:40 +00:00
|
|
|
bool notFound;
|
|
|
|
int code;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2015-02-24 09:07:40 +00:00
|
|
|
int j = 0;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < s->length(); i++ )
|
|
|
|
{
|
|
|
|
unsigned char c = s->at( i );
|
|
|
|
|
|
|
|
if( c > 0x7F ) // need to decode
|
|
|
|
{
|
|
|
|
result += s->substr( j, i - j );
|
|
|
|
std::string part1 = s->substr( i, 4 );
|
2015-02-24 09:07:40 +00:00
|
|
|
int l;
|
2013-10-28 08:06:38 +00:00
|
|
|
code = decodeNum( part1, &l );
|
2015-02-24 09:07:40 +00:00
|
|
|
j = i + l;
|
|
|
|
i = j - 1;
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = true;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
for( int k = 0; k<cpLenght; k++ )
|
|
|
|
{
|
|
|
|
if( doubleTable[k][1] == code )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
int data = doubleTable[k][0];
|
|
|
|
char d[3];
|
|
|
|
d[0] = data >> 8;
|
|
|
|
d[1] = data & 0xFF;
|
|
|
|
d[2] = '\0';
|
2013-10-28 08:06:38 +00:00
|
|
|
result += d; // translate from table
|
|
|
|
notFound = false;
|
|
|
|
break;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( notFound )
|
|
|
|
result += decodeText( code );
|
|
|
|
} // direct conversion
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
result += s->substr( j );
|
2013-10-24 16:44:38 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_ConvDBCSTable::toUtf8( std::string* s )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
std::string res;
|
|
|
|
std::string::iterator it;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2015-02-24 09:07:40 +00:00
|
|
|
for( it = s->begin(); it < s->end(); ++it )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
bool notFound = true;
|
|
|
|
unsigned char c = *it;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( c < 0x80 )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
// check for \U+ encoded text
|
|
|
|
if( c == '\\' )
|
|
|
|
{
|
|
|
|
if( it + 6 < s->end() && *(it + 1) == 'U' && *(it + 2) == '+' )
|
|
|
|
{
|
|
|
|
res += encodeText( std::string( it, it + 7 ) );
|
|
|
|
it += 6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res += c; // no \U+ encoded text write
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
res += c; // c!='\' ascii char write
|
|
|
|
}
|
|
|
|
else if( c == 0x80 ) // 1 byte table
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
2013-10-28 08:06:38 +00:00
|
|
|
res += encodeNum( 0x20AC ); // euro sign
|
|
|
|
}
|
|
|
|
else // 2 bytes
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
++it;
|
2013-10-28 08:06:38 +00:00
|
|
|
int code = (c << 8) | (unsigned char) (*it);
|
|
|
|
int sta = leadTable[c - 0x81];
|
|
|
|
int end = leadTable[c - 0x80];
|
|
|
|
|
|
|
|
for( int k = sta; k<end; k++ )
|
|
|
|
{
|
|
|
|
if( doubleTable[k][0] == code )
|
|
|
|
{
|
|
|
|
res += encodeNum( doubleTable[k][1] ); // translate from table
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
// not found
|
|
|
|
if( notFound )
|
|
|
|
res += encodeNum( NOTFOUND936 );
|
|
|
|
} // end for
|
2013-10-24 16:44:38 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
std::string DRW_Conv932Table::fromUtf8( std::string* s )
|
|
|
|
{
|
|
|
|
std::string result;
|
2015-02-24 09:07:40 +00:00
|
|
|
bool notFound;
|
|
|
|
int code;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2015-02-24 09:07:40 +00:00
|
|
|
int j = 0;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < s->length(); i++ )
|
|
|
|
{
|
|
|
|
unsigned char c = s->at( i );
|
|
|
|
|
|
|
|
if( c > 0x7F ) // need to decode
|
|
|
|
{
|
|
|
|
result += s->substr( j, i - j );
|
|
|
|
std::string part1 = s->substr( i, 4 );
|
2015-02-24 09:07:40 +00:00
|
|
|
int l;
|
2013-10-28 08:06:38 +00:00
|
|
|
code = decodeNum( part1, &l );
|
2015-02-24 09:07:40 +00:00
|
|
|
j = i + l;
|
|
|
|
i = j - 1;
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = true;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
// 1 byte table
|
2013-10-28 08:06:38 +00:00
|
|
|
if( code > 0xff60 && code < 0xFFA0 )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
result += code - CPOFFSET932; // translate from table
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( notFound && ( code<0xF8 || (code>0x390 && code<0x542)
|
|
|
|
|| (code>0x200F && code<0x9FA1) || code>0xF928 ) )
|
|
|
|
{
|
|
|
|
for( int k = 0; k<cpLenght; k++ )
|
|
|
|
{
|
|
|
|
if( doubleTable[k][1] == code )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
int data = doubleTable[k][0];
|
|
|
|
char d[3];
|
|
|
|
d[0] = data >> 8;
|
|
|
|
d[1] = data & 0xFF;
|
|
|
|
d[2] = '\0';
|
|
|
|
result += d; // translate from table
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( notFound )
|
|
|
|
result += decodeText( code );
|
|
|
|
} // direct conversion
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
result += s->substr( j );
|
2013-10-24 16:44:38 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_Conv932Table::toUtf8( std::string* s )
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
std::string res;
|
|
|
|
std::string::iterator it;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2015-02-24 09:07:40 +00:00
|
|
|
for( it = s->begin(); it < s->end(); ++it )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
bool notFound = true;
|
|
|
|
unsigned char c = *it;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( c < 0x80 )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
// check for \U+ encoded text
|
|
|
|
if( c == '\\' )
|
|
|
|
{
|
|
|
|
if( it + 6 < s->end() && *(it + 1) == 'U' && *(it + 2) == '+' )
|
|
|
|
{
|
|
|
|
res += encodeText( std::string( it, it + 7 ) );
|
|
|
|
it += 6;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
res += c; // no \U+ encoded text write
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res += c; // c!='\' ascii char write
|
|
|
|
}
|
|
|
|
else if( c > 0xA0 && c < 0xE0 ) // 1 byte table
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
2013-10-28 08:06:38 +00:00
|
|
|
res += encodeNum( c + CPOFFSET932 ); // translate from table
|
|
|
|
}
|
|
|
|
else // 2 bytes
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
++it;
|
2013-10-28 08:06:38 +00:00
|
|
|
int code = (c << 8) | (unsigned char) (*it);
|
2013-10-24 16:44:38 +00:00
|
|
|
int sta;
|
2013-10-28 08:06:38 +00:00
|
|
|
int end = 0;
|
|
|
|
|
|
|
|
if( c > 0x80 && c < 0xA0 )
|
|
|
|
{
|
|
|
|
sta = DRW_LeadTable932[c - 0x81];
|
|
|
|
end = DRW_LeadTable932[c - 0x80];
|
|
|
|
}
|
|
|
|
else if( c > 0xDF && c < 0xFD )
|
|
|
|
{
|
|
|
|
sta = DRW_LeadTable932[c - 0xC1];
|
|
|
|
end = DRW_LeadTable932[c - 0xC0];
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( end > 0 )
|
|
|
|
{
|
|
|
|
for( int k = sta; k<end; k++ )
|
|
|
|
{
|
|
|
|
if( DRW_DoubleTable932[k][0] == code )
|
|
|
|
{
|
|
|
|
res += encodeNum( DRW_DoubleTable932[k][1] ); // translate from table
|
2013-10-24 16:44:38 +00:00
|
|
|
notFound = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
// not found
|
|
|
|
if( notFound )
|
|
|
|
res += encodeNum( NOTFOUND932 );
|
|
|
|
} // end for
|
2013-10-24 16:44:38 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
std::string DRW_TextCodec::correctCodePage( const std::string& s )
|
|
|
|
{
|
|
|
|
// stringstream cause crash in OS/X, bug#3597944
|
2018-01-10 08:04:15 +00:00
|
|
|
std::string c = s;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2018-01-10 08:04:15 +00:00
|
|
|
transform( c.begin(), c.end(), c.begin(), toupper );
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
// Latin/Thai
|
2018-01-10 08:04:15 +00:00
|
|
|
if( c=="ANSI_874" || c=="c874" || c=="ISO8859-11" || c=="TIS-620" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_874";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Central Europe and Eastern Europe
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1250" || c=="c1250" || c=="ISO8859-2" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1250";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Cyrillic script
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1251" || c=="c1251" || c=="ISO8859-5" || c=="KOI8-R"
|
|
|
|
|| c=="KOI8-U" || c=="IBM 866" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1251";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Western Europe
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1252" || c=="c1252" || c=="LATIN1" || c=="ISO-8859-1"
|
|
|
|
|| c=="c819" || c=="CSISO" || c=="IBM819" || c=="ISO_8859-1" || c=="APPLE ROMAN"
|
|
|
|
|| c=="ISO8859-1" || c=="ISO8859-15" || c=="ISO-IR-100" || c=="L1" || c==
|
2013-10-28 08:06:38 +00:00
|
|
|
"IBM 850" )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1252";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Greek
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1253" || c=="c1253" || c=="iso8859-7" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1253";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Turkish
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1254" || c=="c1254" || c=="iso8859-9" || c=="iso8859-3" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1254";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Hebrew
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1255" || c=="c1255" || c=="iso8859-8" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1255";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Arabic
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1256" || c=="c1256" || c=="ISO8859-6" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1256";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Baltic
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1257" || c=="c1257" || c=="ISO8859-4" || c=="ISO8859-10" || c==
|
2013-10-28 08:06:38 +00:00
|
|
|
"ISO8859-13" )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1257";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Vietnamese
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_1258" || c=="c1258" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_1258";
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
// Japanese
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_932" || c=="SHIFT-JIS" || c=="SHIFT_JIS" || c=="CSSHIFTJIS"
|
|
|
|
|| c=="CSWINDOWS31J" || c=="MS_KANJI" || c=="X-MS-c932" || c=="X-SJIS"
|
|
|
|
|| c=="EUCJP" || c=="EUC-JP" || c=="CSEUcKDFMTJAPANESE" || c=="X-EUC"
|
|
|
|
|| c=="X-EUC-JP" || c=="JIS7" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_932";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Chinese PRC GBK (XGB) simplified
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_936" || c=="GBK" || c=="GB2312" || c=="CHINESE" || c=="CN-GB"
|
|
|
|
|| c=="CSGB2312" || c=="CSGB231280" || c=="CSISO58BG231280"
|
|
|
|
|| c=="GB_2312-80" || c=="GB231280" || c=="GB2312-80"
|
|
|
|
|| c=="ISO-IR-58" || c=="GB18030" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_936";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Korean
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_949" || c=="EUCKR" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_949";
|
2013-10-28 08:06:38 +00:00
|
|
|
// Chinese Big5 (Taiwan, Hong Kong SAR)
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="ANSI_950" || c=="BIG5" || c=="CN-BIG5" || c=="CSBIG5"
|
|
|
|
|| c=="X-X-BIG5" || c=="BIG5-HKSCS" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "ANSI_950";
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
// celtic
|
2018-01-10 08:04:15 +00:00
|
|
|
/* } else if (c=="ISO8859-14") {
|
2013-10-28 08:06:38 +00:00
|
|
|
* return "ISO8859-14";
|
2018-01-10 08:04:15 +00:00
|
|
|
* } else if (c=="TSCII") {
|
2013-10-28 08:06:38 +00:00
|
|
|
* return "TSCII"; //tamil
|
2018-01-10 08:04:15 +00:00
|
|
|
* } else if (c=="UTF16") {
|
2013-10-28 08:06:38 +00:00
|
|
|
* return "UTF16"; */
|
|
|
|
}
|
2018-01-10 08:04:15 +00:00
|
|
|
else if( c=="UTF-8" || c=="UTF8" || c=="UTF88-BIT" )
|
2013-10-28 08:06:38 +00:00
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return "UTF-8";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "ANSI_1252";
|
|
|
|
}
|