typedef enum FileType
{
ANSI_CODE = 0,
UNICODE_CODE,
UTF8_CODE,
UNKNOW_CODE,
}FILETYPE;
FILETYPE eBook::GetTextType(LPTSTR fPath) //获取指定路径文件的编码格式
{
FILETYPE nReturn = UNICODE_CODE;
unsigned char uniTxt[] = {0xFF, 0xFE}; // Unicode file header
unsigned char endianTxt[] = {0xFE, 0xFF}; // Unicode big endian file header
unsigned char utf8Txt[] = {0xEF, 0xBB}; // UTF_8 file header
DWORD dwBytesRead = 0;
HANDLE hFile = CreateFile(fPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
hFile = NULL;
CloseHandle(hFile);
return UNICODE_CODE;
}
BYTE *lpHeader = new BYTE[2];
::ReadFile(hFile, lpHeader, 2, &dwBytesRead, NULL);
CloseHandle(hFile);
if (lpHeader[0] == uniTxt[0] && lpHeader[1] == uniTxt[1])// Unicode file
{
nReturn = UNICODE_CODE;
}
else if (lpHeader[0] == endianTxt[0] && lpHeader[1] == endianTxt[1])// Unicode big endian file
{
nReturn = UNICODE_CODE;
}
else if (lpHeader[0] == utf8Txt[0] && lpHeader[1] == utf8Txt[1])// UTF-8 file
{
nReturn = UTF8_CODE;
}
else
{
nReturn = ANSI_CODE; //Ascii
}
delete []lpHeader;
return nReturn;
}