Clucene C++編碼轉換

來源:互聯網
上載者:User

在做Clucene與lucene產生的Index檔案相互相容時,遇到了編碼轉換問題。它們的相容性對於非英文的編碼可能都會存在這樣的問題,經過跟蹤clucene程式,發現它用的是unicode編碼方式儲蓄,因此,要先把字串或檔案轉換成unicode編碼,然後再進行其它處理。

轉換的具體代碼如下(Linux與vc6.0測試通過):


#ifndef _UNIX
static inline int codepage(const char* code_page)
{
    return 936;//"GBK"
}
#endifstatic inline int mb2wc(const char* code_page,/*in*/const char* in,int in_len,
  /*out*/wchar_t* out,int out_max)
{
#ifdef _UNIX
 size_t result;
 iconv_t env;
 env = iconv_open("WCHAR_T",code_page);
 result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
 iconv_close(env);
 return (int) result;
#else
 return ::MultiByteToWideChar(codepage(code_page),0,in,in_len,out,out_max);
#endif
}static inline int wc2mb(const char* code_page,/*in*/const wchar_t* in,int in_len,
  /*out*/char* out,int out_max)
{
#ifdef _UNIX
 size_t result;
 iconv_t env;
 env = iconv_open(code_page,"WCHAR_T");
 result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
 iconv_close(env);
 return (int) result;
#else
 return ::WideCharToMultiByte(codepage(code_page),0,in,-1,out,out_max, NULL, NULL);
#endif  
}void str_to_UnicodeChar(const char* strIn,TCHAR* &strOut){
 if(!strIn)
  return; int  i=  mb2wc("936",(char*)strIn, -1, NULL, 0);
 strOut = (TCHAR*)malloc(sizeof(TCHAR)*i);
 mb2wc("936",(char*)strIn, -1, strOut, i); 
}
void UnicodeChar_to_str(const TCHAR* strIn,char* &strOut){
 if(!strIn)
  return;
  
 int i = wc2mb("936",strIn,-1,NULL,0); 
 strOut = new char[i+1]; 
 wc2mb("936", strIn, -1, strOut, i);
 strOut[i] = 0;
}void tchar_to_str(const const TCHAR* strIn ,char* &strOut){
 int i=0;
 if(!strIn)
  return ;
 strOut = new char[1024]; 
 
 while(*strIn) {
  strOut[i]=*strIn++;
  i++;
 }
 strOut[i]='\0';

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.