转码

来源:互联网 发布:淘宝上买车险可靠吗 编辑:程序博客网 时间:2024/06/13 02:29
#include <iconv.h>


/*code from one to another*/
int mailparser::MailParser::codeconv(char *from_code, char *to_code,  char *from, char *to)
{               
iconv_t cd;
char  *tptr, *fptr; 
size_t  ileft, oleft, ret,convert_len=0,i_len;
       
tptr = fptr =NULL;
           
if(from == NULL || to == NULL) return -1;

//如果有BOM BOM是编码
if((from[0] == 0xff && from[1] == 0xfe) || (from[0] == 0xfe && from[1] == 0xff)) 
{
from += 2;
}
//UTF-8的BOM,有时也没有BOM是编码
if((from[0] == 0xef && from[1] == 0xbb && from[2] == 0xbf)) 
{
from += 3;
}
       
cd = iconv_open(( const char * )to_code, ( const char * )from_code );
if ( cd == ( iconv_t )-1 )
{       
   return -1; 
}       
   
i_len=ileft = strlen(from);
fptr = from;
for( ; ; )
{
tptr = to;
oleft = strlen( from ) * 4 + 1;

memset( to, 0x00, strlen( from ) * 4 + 1 );
ret = iconv( cd, &fptr, &ileft, &tptr, &oleft );

convert_len+=ret;
if(ret==0)
{
  break;
}
else if(ret>0 && ileft==0 && convert_len<i_len)
{
( void )iconv_close( cd );
return -1;
}

if( errno == EINVAL )
{
   ( void )iconv_close( cd );
   return 1;
}
else if( errno == E2BIG )
{
   continue;
}
else if( errno == EILSEQ )
{
   ( void )iconv_close( cd );
   return 1;
}
else if( errno == EBADF )
{
   ( void )iconv_close( cd );
   return -1;
}
else
{
   ( void )iconv_close( cd );
   return -1;
}
}
( void )iconv_close( cd );
return 0;
}


String mailparser::MailParser::any2utf8(const String &src)
{
char code_knowned[13][20] =  {'\0'} ;
int i, ret, code_knowned_num;
char *to = NULL;

strcpy(code_knowned[0],"UTF8");
strcpy(code_knowned[1],"GB2312");
strcpy(code_knowned[2],"GBK");
strcpy(code_knowned[3],"GB18030");
strcpy(code_knowned[4],"BIG5");
strcpy(code_knowned[5],"ISO646-KR");
strcpy(code_knowned[6],"EUC-KR");
  strcpy(code_knowned[7],"EUC_JP");
strcpy(code_knowned[8],"ISO-2022-JP");
strcpy(code_knowned[9],"ISO-8859-1");
strcpy(code_knowned[10],"ISO-8859-2");
strcpy(code_knowned[11],"ISO-8859-9");
strcpy(code_knowned[12],"Shift_JIS");

if(src.size() < 1)
return src;

int len = src.size();

if(( to = ( char * )malloc( len * 4 + 1 )) == NULL )
  {
return src;
  }
  memset( to, 0x00, len * 4 + 1 );
  

code_knowned_num = sizeof( code_knowned ) / sizeof( code_knowned[ 0 ] );
for( i = 0; i < code_knowned_num; i++ )
{
   ret = codeconv( code_knowned[ i ], "UTF8", const_cast<char *>(src.c_str()), to );
   if( ret == 0 ) 
   {
    String rstString = to;
    if(to!=NULL)
{
free(to);
to = NULL;
}
    return rstString;
   }
   else if( ret == 1 ) 
   {
    memset( to, 0x00, len * 4 + 1 );
    continue;
   }
   else
   {
    memset( to, 0x00, len * 4 + 1 );
    continue;
   }
}
if(to!=NULL)
{
free(to);
to = NULL;
}
return src;
}


String mailparser::MailParser::any2utf8(const String &src, const String fromEncode)
{
int ret;
char *to = NULL;

if(src.size() < 1)
return src;

if(fromEncode.size()<1)
return src;

int len = src.size();
if(( to = ( char * )malloc( len * 4 + 1 )) == NULL )
  {
return src;
  }
  memset( to, 0x00, len * 4 + 1 );  

if((codeconv( const_cast<char *>(fromEncode.c_str()), "UTF8", const_cast<char *>(src.c_str()), to ))==0)
  {
  String rstString = to;
  if(to!=NULL)
  {
  free(to);
  to=NULL;
  }
  return rstString;
  }
if(to!=NULL)
{
free(to);
to=NULL;
}
return src;
}
原创粉丝点击