/***************************************************************************************************
* 中 文 内 码 转 换 *
***************************************************************************************************/
//
#include "stdafx.h"
//内存泄露检测
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
////////////////////////////////////////////////////////
//
// 宏定义
//
////////////////////////////////////////////////////////
#define SAFERELEASE(p) if((p)){delete[] (p);(p) = NULL;}
//#define TRANSFORM_GB2GBK 1 //生成简体转繁体
#define TRANSFORM_GBK2GB 2 //生成繁体转简体
////////////////////////////////////////////////////////
//
// 函数申明
//
////////////////////////////////////////////////////////
void GetPath(char** pathDes,const char* pathSrc); //得到当前 exe 所在的文件夹路径
int FileSearch(string& path,int layer); //遍历文件
bool GB2GBK(char* data); //简体转繁体
bool GBK2GB(char* data); //繁体转简体
bool IsTxt(const char* fileName); //是否为 *.txt 文件
bool ReadFile(char** data,const char* fileName); //读取文件
bool WriteFile(const char* data,const char* fileName); //将转换完的内容写入文件
////////////////////////////////////////////////////////
//
// 入口函数
//
////////////////////////////////////////////////////////
int main(int argc,char** argv)
{
//{
char* path = NULL;
std::string strPath;
int fileAmount = 0;
GetPath(&path,argv[0]);
strPath = path;
fileAmount = FileSearch(strPath,0);
if(fileAmount > 0)
cout << "已成功转换文件 "<< fileAmount << " 个\n" <<endl;
//释放资源
SAFERELEASE(path);
system("PAUSE");
//}
//_CrtDumpMemoryLeaks();
return 0;
}
////////////////////////////////////////////////////////
//
// 函数定义
//
////////////////////////////////////////////////////////
void GetPath(char** pathDes,const char* pathSrc)
{
assert(pathSrc != NULL);
int Srclen = strlen(pathSrc);
int Deslen;
//得到 \ 的位置
for(Deslen = Srclen;Deslen > -1; --Deslen)
{
if(*(pathSrc + Deslen) == '\\')
{
break;
}
}
//分配内存
*pathDes = new char[Deslen + 1];
ZeroMemory(*pathDes,Deslen + 1);
//copy 值
for(int i = 0;i < Deslen;++i)
{
*((*pathDes) + i) = *(pathSrc + i);
}
//添加 '\0'
*((*pathDes) + Deslen) = '\0';
}
int FileSearch(string& path,int layer)
{
struct _finddata_t filefind;
string curr = path + "\\*.*";
int done = 0; //是否完成
int handle;
int txtAmount = 0; //*.txt 文件总数
//判断该文件夹是否为空
if((handle = _findfirst(curr.c_str(),&filefind)) == -1)
return 0;
while(!(done = _findnext(handle,&filefind)))
{
//跳过 ..
if(!strcmp(filefind.name,".."))
{
continue;
}
//为下级目录打 "\"
//for(int i = 0;i < layer;i++)
// cout<<"\t";
//判断是否有下级目录
if((_A_SUBDIR == filefind.attrib))
{
//cout<< filefind.name << "(dir) \\" << "\\" <<endl;
//得到文件名并继续遍历
curr = path + "\\" + filefind.name;
txtAmount += FileSearch(curr,(layer + 1));
}
else
{
if(IsTxt(filefind.name))
{
//cout<<filefind.name<<endl; //输出文件名
char* fileData = NULL;
std::string fullName = path + "\\" + filefind.name;
if(ReadFile(&fileData,fullName.c_str()))
{
bool (*pTransform)(char*);
#ifdef TRANSFORM_GB2GBK
pTransform = GB2GBK;
#endif
#ifdef TRANSFORM_GBK2GB
pTransform = GBK2GB;
#endif
if(pTransform(fileData))
{
//cout << fileData << endl;//输出转换完的内容
WriteFile(fileData,fullName.c_str());
txtAmount++;
}
}
SAFERELEASE(fileData);
}
}
}
_findclose(handle);
return txtAmount;
}
bool GB2GBK(char* data)
{
assert(data != NULL);
//无内容,不用转换
if(!strcmp(data, ""))
return false;
int dataLen = strlen(data);
DWORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
int nReturn = LCMapStringA(wLCID, LCMAP_TRADITIONAL_CHINESE, data, dataLen, NULL, 0);
if(!nReturn)
return false;
char *pcBuf = new char[nReturn + 1];
__try
{
wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
LCMapStringA(wLCID, LCMAP_TRADITIONAL_CHINESE, data, nReturn, pcBuf, nReturn + 1);
strncpy(data, pcBuf, nReturn);
}
__finally
{
SAFERELEASE(pcBuf);
}
SAFERELEASE(pcBuf);
return true;
}
bool GBK2GB(char* data)
{
assert(data != NULL);
if(!strcmp(data, ""))
return false;
int dataLen = strlen(data);
DWORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
int nReturn = LCMapStringA(wLCID, LCMAP_SIMPLIFIED_CHINESE, data, dataLen, NULL, 0);
if(!nReturn)
return false;
char *pcBuf = new char[nReturn + 1];
__try
{
wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
LCMapStringA(wLCID, LCMAP_SIMPLIFIED_CHINESE, data, nReturn, pcBuf, nReturn + 1);
strncpy(data, pcBuf, nReturn);
}
__finally
{
SAFERELEASE(pcBuf);
}
return true;
}
bool IsTxt(const char* fileName)
{
assert(fileName != NULL);
int nameLength = strlen(fileName);
if(*(fileName + nameLength - 1) == 't')
{
if(*(fileName + nameLength - 2) == 'x')
{
if(*(fileName + nameLength - 3) == 't')
{
if(*(fileName + nameLength - 4) == '.')
{
return true;
}
}
}
}
return false;
}
bool ReadFile(char** data,const char* fileName)
{
assert(fileName != NULL);
FILE* pFile = NULL;
fopen_s(&pFile,fileName,"r");
if(NULL == pFile)
return false;
//获得文件的大小
fseek(pFile,0,SEEK_END);
size_t fileSize = ftell(pFile);
fseek(pFile,0,SEEK_SET);
//分配内存
SAFERELEASE(*data);
*data = new char[fileSize + 1];
ZeroMemory(*data,fileSize + 1);
fread(*data,fileSize,1,pFile);
fclose(pFile);
pFile = NULL;
return true;
}
bool WriteFile(const char* data,const char* fileName)
{
assert(data != NULL && fileName != NULL);
FILE* pFile = NULL;
fopen_s(&pFile,fileName,"r");
if(pFile == NULL)
return false;
fseek(pFile,0,SEEK_END);
size_t fileSize = ftell(pFile);
fseek(pFile,0,SEEK_SET);
//保存文件数据
char* buffer = new char[fileSize + 1];
ZeroMemory(buffer,fileSize + 1);
fread(buffer,fileSize,1,pFile);
fclose(pFile);
fopen_s(&pFile,fileName,"w");
if(pFile == NULL)
{
SAFERELEASE(buffer);
return false;
}
//如果写入 data 失败,则写入 buffer 的数据
if(fwrite(data,fileSize,1,pFile) == -1)
{
fwrite(buffer,fileSize,1,pFile);
}
//释放资源
fclose(pFile);
SAFERELEASE(buffer);
pFile = NULL;
return true;
}
////////////////////////////////////////////////////////
//
// 相关知识
//
////////////////////////////////////////////////////////
/*
在 <io.h> 里
#define _finddata_t _finddata64i32_t
struct _finddata64i32_t {
unsigned attrib;
__time64_t time_create; -1 for FAT file systems
__time64_t time_access; -1 for FAT file systems
__time64_t time_write;
_fsize_t size;
char name[260];
};
*/
- 1
- 2
- 3
- 4
- 5
前往页