gbk uf8互换

来源:互联网 发布:飞机大战子弹算法 编辑:程序博客网 时间:2024/05/31 20:51
 #ifdef WIN32

bool DeleteDir(const char szPath[])

WIN32_FIND_DATA   finddata; 
HANDLE   hfind; 
char   *   pdir = NULL; 

pdir = new char[strlen(szPath)+5];
strcpy(pdir,szPath); 
if(szPath[strlen(szPath)-1]!= '\\') 
strcat(pdir, "\\*"); 
else 
strcat(pdir, "*.*"); 

hfind=FindFirstFile(pdir,&finddata); 
if(hfind==INVALID_HANDLE_VALUE) 
return   FALSE; 

delete []pdir; 
do 

pdir=new   char[strlen(szPath)+strlen(finddata.cFileName)+10]; 
sprintf(pdir, "%s\\%s",szPath,finddata.cFileName); 
if(strcmp(finddata.cFileName, ".")==0 
||strcmp(finddata.cFileName, "..")==0) 

RemoveDirectory(pdir); 
continue; 


if((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0) 
DeleteFile(pdir); 
else 
DeleteDir(pdir); 
delete   []pdir; 
}while(FindNextFile(hfind,&finddata)); 
FindClose(hfind);

if(RemoveDirectory(szPath)) 
return   true; 
else 
return   false; 
}

void _UTF8ToGBK(char *szOut)
{
unsigned short *wszGBK;
char *szGBK;
//长度
int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)szOut, -1, NULL, 0);
wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)szOut, -1, (LPWSTR)wszGBK, len);

//长度
len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
szGBK = new char[len+1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, szGBK, len, NULL, NULL);

//szOut = szGBK; //这样得到的szOut不正确,因为此句意义是将szGBK的首地址赋给szOut,当delete []szGBK执行后szGBK的内

//存空间将被释放,此时将得不到szOut的内容

memset(szOut,'\0',strlen(szGBK)+1); //改将szGBK的内容赋给szOut ,这样即使szGBK被释放也能得到正确的值
memcpy(szOut,szGBK,strlen(szGBK));


delete []szGBK;
delete []wszGBK;
}

void _GBKToUTF8(char *szOut)
{
char* strGBK = szOut;

int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK, -1, (LPWSTR)wszUtf8, len);

len = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, (LPWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);

//szOut = szUtf8;
memset(szOut,'\0',strlen(szUtf8)+1);
memcpy(szOut,szUtf8,strlen(szUtf8));

delete[] szUtf8;
delete[] wszUtf8;
}

#else

int is_dir(const char szDirPath[])
{

int nResult  = 0;
int nRetCode = 0;

struct   stat   buf; 
LOGPROCESS_ERROR(szDirPath);

nRetCode = lstat(szDirPath, &buf);

PROCESS_ERROR(nRetCode == 0) ;

if(S_ISDIR(buf.st_mode)) 
{
nResult = 1;
}
else if(S_ISREG(buf.st_mode)) 
{
nResult = 2;
}

Exit0:
return nResult;
}


bool DeleteDir(const char szDirPath[])
{
int  nResult = 0;
DIR* pDir    = NULL;

struct dirent* pEntry = NULL;
char   szPath[MAX_LEN];

LOGPROCESS_ERROR(szDirPath);

if (1 == is_dir(szDirPath))
{
pDir = opendir(szDirPath);
while ( (pEntry = readdir(pDir)) != NULL )
{
if(strcmp(".", pEntry->d_name) == 0 || strcmp("..", pEntry->d_name) == 0) 
continue; 
snprintf(szPath, sizeof(szPath),"%s/%s", szDirPath, pEntry->d_name);
DeleteDir(szPath);
}
rmdir(szDirPath);
closedir(pDir);
}
else 
{
unlink(szDirPath);
}

nResult = 1;
Exit0:
return nResult;

}


bool PathFileExists(const char szPath[])
{
int   nResult = 0;
DIR*  pDir    = NULL;
FILE* pFile   = NULL;

LOGPROCESS_ERROR(szPath);

if (1 == is_dir(szPath))
{
pDir = opendir(szPath);
PROCESS_SUCCESS(pDir);
PROCESS_ERROR(pDir);

}
else
{
pFile = fopen(szPath, "r");
PROCESS_SUCCESS(pFile);
PROCESS_ERROR(pFile);
}

Exit1:
nResult = 1;
Exit0:
if (pDir)
{
closedir(pDir);
pDir = NULL;
}

FILE_CLOSE(pFile);

return nResult;
}

int _UTF8ToGBK(char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
{
iconv_t cd;
size_t ut = 0;

if( (cd = iconv_open("gbk","utf-8")) ==0 )
return -1;
memset(destbuf,0,destlen);
char **source = &sourcebuf;
char **dest = &destbuf;

ut = (size_t)(-1);

if(ut == iconv(cd,source,&sourcelen,dest,&destlen))
return -1;
iconv_close(cd);
return 0;

}

int _GBKToUTF8(char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
{
iconv_t cd;
size_t ut = 0;

if( (cd = iconv_open("utf-8","gbk")) ==0 )
return -1;
memset(destbuf,0,destlen);
char **source = &sourcebuf;
char **dest = &destbuf;

ut = (size_t)(-1);

if(ut == iconv(cd,source,&sourcelen,dest,&destlen))
return -1;
iconv_close(cd);
return 0;

原创粉丝点击