linux下c++编程 转码 GBKTOUTF8问题
老杂鱼 2009-10-10 10:13:36 setup.cpp:
#include "setup.h"
#define __cdn_win32_platform__
#define CP_GB2312 20936
class CodingTransformer
{
public:
// UTF-8 TO GB2312
int UTF_8ToGB2312(char* pOut, int iBufSize, char *pText, int iLenth);
//GB2312 TO UTF-8
int GB2312ToUTF_8(char* pOut, int iBufSize,char *pText, int iLenth);
};
int CodingTransformer::UTF_8ToGB2312(char* pOut, int iBufSize, char *pText, int pLen)
{
#ifdef __cdn_win32_platform__
WCHAR* pWtemp = new WCHAR[pLen];
int iWcharLenth = ::MultiByteToWideChar(CP_UTF8,0,pText,-1,pWtemp,pLen);
if (0 == iWcharLenth)
{
DWORD dwLastErr = GetLastError();
printf("alexaroma:CodingTransformer err,err:%d\n", dwLastErr);
delete pWtemp;
return 0;
}
int iMultByteLenth = ::WideCharToMultiByte(CP_GB2312, NULL, pWtemp,
iWcharLenth, pOut,
iBufSize, NULL, NULL);
if (0 == iMultByteLenth)
{
DWORD dwLastErr = GetLastError();
printf("alexaroma:CodingTransformer err,err:%d\n", dwLastErr);
delete pWtemp;
return 0;
}
delete pWtemp;
return iMultByteLenth;
#else//__cdn_win32_platform__
iconv_t cd;
int rc;
char **pin = &pText;
char **pout = &pOut;
int outlen = iBufSize;
cd = iconv_open("GB2312","UTF-8");
if(cd==0)return -1;
memset(pOut,0,strlen(pOut));
if(iconv(cd,pin,(size_t *)&pLen,pout,(size_t *)&outlen) == -1)
{
iconv_close(cd);
return outlen;
}
iconv_close(cd);
return -1;
#endif//__cdn_win32_platform__
}
int CodingTransformer::GB2312ToUTF_8(char* pOut, int iBufSize, char *pText, int pLen)
{
#ifdef __cdn_win32_platform__
WCHAR* pWtemp = new WCHAR[pLen];
int iWcharLenth = ::MultiByteToWideChar(CP_GB2312, MB_PRECOMPOSED,
pText, -1, pWtemp, pLen);
if (0 == iWcharLenth)
{
DWORD dwLastErr = GetLastError();
printf("alexaroma:CodingTransformer err,err:%d\n", dwLastErr);
delete pWtemp; return 0;
}
int iMultByteLenth = ::WideCharToMultiByte(CP_UTF8, 0, pWtemp,
iWcharLenth, pOut,
iBufSize, NULL, NULL);
if (0 == iMultByteLenth)
{
DWORD dwLastErr = GetLastError();
printf("alexaroma:CodingTransformer err,err:%d\n", dwLastErr);
delete pWtemp;
return 0;
}
delete pWtemp;
return iMultByteLenth;
#else//__cdn_win32_platform__
iconv_t cd;
int rc;
char **pin = &pText;
char **pout = &pOut;
int outlen = iBufSize;
cd=iconv_open("UTF-8","GB2312");
if(cd==0)return -1;
memset(pOut,0,strlen(pOut));
if(iconv(cd,pin,(size_t *)&pLen,pout,(size_t *)&outlen) == -1)
{
iconv_close(cd);
return outlen;
}
iconv_close(cd);
return -1;
#endif//__cdn_win32_platform__
}
setup.h:
#ifndef SC_SETUP_H
#define SC_SETUP_H
#include "../ScriptMgr.h"
#include "Cell.h"
#include "CellImpl.h"
#include "GridNotifiers.h"
#include "GridNotifiersImpl.h"
#include "Unit.h"
#include "GameObject.h"
#include "sc_creature.h"
#include "sc_gossip.h"
#include "sc_instance.h"
#include "CombatAI.h"
#include "PassiveAI.h"
#include "Chat.h"
#include "DBCStructure.h"
#include "DBCStores.h"
#ifdef WIN32
#include <windows.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return true;
}
#endif
#endif
/*
//TODO:UTF8
char* Utf8ToGBK(const char* strUtf8);
char* GBKToUtf8(const char* strGBK);
*/
#define __cdn_win32_platform__
#define CP_GB2312 20936
//UTF-8 TO GB2312
int UTF_8ToGB2312(char* pOut, int iBufSize, char *pText, int iLenth);
//GB2312 TO UTF-8
int GB2312ToUTF_8(char* pOut, int iBufSize, char *pText, int iLenth);
Onescript.cpp:
#define GOSSIP_TEXT_TAVERN GB2312ToUTF_8(" 【主菜单】")
以上代码可以WIN下用也可以在LINUX下用
可是编译出错:
Onescript.cpp.cpp(358) : error C2660: “GB2312ToUTF_8”: 函数不接受 1 个参数
主要是我不懂C++,请教高人给修改修改!