update cppcommon for gbkToUtf8

This commit is contained in:
gwdwyy 2013-07-12 15:10:18 +08:00
parent c10d3df523
commit dd48f14959
3 changed files with 68 additions and 13 deletions

View File

@ -28,7 +28,7 @@ file_functs.test: file_functs.cpp file_functs.h
io_functs.test: io_functs.cpp io_functs.h
g++ -o $@ $< -DTEST_IO_FUNCTS
str_functs.ut: str_functs.cpp str_functs.h
g++ -o $@ $< -DTEST_STR_FUNCTS
g++ -o $@ $< -DTEST_STR_FUNCTS -liconv
vec_functs.test: vec_functs.cpp vec_functs.h vec_functs.tcc
g++ -o $@ $< -DTEST_VEC_FUNCTS

View File

@ -307,6 +307,49 @@ namespace CPPCOMMON
return res;
}
//iconv
int code_convert(const char *from_charset,const char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd==NULL)
{
//cout<<__FILE__<<__LINE__<<endl;
return -1;
}
memset(outbuf,0,outlen);
size_t ret = iconv(cd,pin,&inlen,pout,&outlen);
if (ret == -1)
{
//cout<<__FILE__<<__LINE__<<endl;
return -1;
}
iconv_close(cd);
return 0;
}
//gbk -> utf8
string gbkToUtf8(const string& gbk)
{
//cout<<__FILE__<<__LINE__<<gbk<<endl;
string res;
size_t maxLen = gbk.size()*4;
char * pUtf = new char[maxLen];
int ret = code_convert("gbk", "utf-8", (char *)gbk.c_str(), gbk.size(), pUtf, maxLen);
if(ret == -1)
{
delete [] pUtf;
return "";
}
res = pUtf;
delete [] pUtf;
return res;
}
}
#ifdef TEST_STR_FUNCTS
@ -351,22 +394,30 @@ int main()
// //cout<<strlen(utf8str);
// cout<<utf8str<<endl;
//}
cout<<string_format("hehe%s11asd%dasf","[here]",2);
ifstream ifile("testdata/dict.txt");
//cout<<string_format("hehe%s11asd%dasf","[here]",2);
//ifstream ifile("testdata/dict.utf8");
//string line;
//while(getline(ifile, line))
//{
// cout<<line<<endl;
// string uniStr = utf8ToUnicode(line);
// //cout<<uniStr<<endl;
// string utfStr = unicodeToUtf8(uniStr);
// cout<<utfStr<<endl;
//}
//vector<string> tmp;
//tmp.push_back("1");
////tmp.push_back("2");
////tmp.clear();
//cout<<joinStr(tmp, ",")<<endl;
ifstream ifile("testdata/dict.gbk");
string line;
while(getline(ifile, line))
{
cout<<line<<endl;
string uniStr = utf8ToUnicode(line);
//cout<<uniStr<<endl;
string utfStr = unicodeToUtf8(uniStr);
cout<<utfStr<<endl;
//cout<<line<<endl;
string s = gbkToUtf8(line);
cout<<s<<endl;
}
vector<string> tmp;
tmp.push_back("1");
//tmp.push_back("2");
//tmp.clear();
cout<<joinStr(tmp, ",")<<endl;
return 0;
}
#endif

View File

@ -10,6 +10,8 @@
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <iconv.h>
#include <memory.h>
#include "typedefs.h"
namespace CPPCOMMON
{
@ -35,6 +37,8 @@ namespace CPPCOMMON
string unicodeToUtf8(const string& uniStr);
int utf8ToUnicode(const char* inutf8, int len, uint16_t* unicode);
string utf8ToUnicode(const string& utfStr);
int code_convert(const char *from_charset,const char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen);
string gbkToUtf8(const string& gbk);
inline uint16_t twocharToUint16(char high, char low)
{