//=========================================================
|
// AsFile.h : head file Code from Notepadre
|
// Õë¶Ô²»Í¬±àÂë¸ñʽÎļþµÄ¶Áд´¦Àí
|
//
|
// Author:asoft
|
// Modify:2009/07/03
|
//-----------------------------------------------
|
// Ó÷¨:
|
// ReadÎļþ
|
// CFile file;
|
// HLOCAL memBuffer;
|
// CAsFile::e_FileType eFileType = CAsFile::eAutoFileType;
|
// CAsFile::e_FileFormat eFileFormat = CAsFile::eAutoFileFormat;
|
// DWORD dwNewSize;
|
// TCHAR *pBuffer = NULL;
|
// CString strData;
|
|
// TRY
|
// {
|
// if ( !file.Open( strXMLPath, CFile::shareDenyWrite | CFile::modeRead ) )
|
// return false;
|
// memBuffer = CAsFile::Load( &file, eFileType, eFileFormat, dwNewSize );
|
// file.Close( );
|
// pBuffer = static_cast<TCHAR*> (::LocalLock (memBuffer));
|
// _tcscpy_s( strData.GetBuffer( dwNewSize ), dwNewSize, pBuffer );
|
// strData.ReleaseBuffer( );
|
|
// ::LocalUnlock( memBuffer );
|
// ::LocalFree( memBuffer );
|
// }
|
// CATCH_ALL(e)
|
// {
|
// file.Close( );
|
// THROW_LAST ();
|
// }
|
// END_CATCH_ALL
|
|
// SaveÎļþ
|
// CFile file;
|
//
|
// TRY
|
// {
|
// if ( !file.Open( strFilePath, CFile::modeCreate | CFile::modeWrite ) )
|
// return false;
|
// CAsFile::Save( strData.GetBuffer( strData.GetLength() ),
|
// strData.GetLength(), CAsFile::eUTF8, CAsFile::eWindows, &file );
|
// file.Close( );
|
// }
|
// CATCH_ALL(e)
|
// {
|
// file.Close( );
|
// THROW_LAST ();
|
// }
|
// END_CATCH_ALL
|
//-----------------------------------------------
|
//
|
//=========================================================
|
|
#pragma once
|
#include "afx.h"
|
|
class CAsFile :
|
public CObject
|
{
|
public:
|
CAsFile(void);
|
~CAsFile(void);
|
|
public:
|
enum e_FileType {eANSI, eUnicode, eUnicodeBigEndian, eUTF8, eAutoFileType,
|
eINVALID};
|
enum e_FileFormat {eWindows, eUNIX, eMacintosh, eAutoFileFormat};
|
|
static HLOCAL Load (CFile *pFile, e_FileType &eFileType,
|
e_FileFormat &eFileFormat, DWORD &dwNewSize);
|
static void Save (const TCHAR *pszText, const DWORD dwChars,
|
const e_FileType eFileType, const e_FileFormat eFileFormat,
|
CFile *pFile);
|
|
private:
|
static const DWORD m_dwBufferSize;
|
|
static void Read (CFile *pFile, unsigned char *pucText,
|
const DWORD dwSize);
|
static void GetFileType (CFile *pFile, e_FileType &eFileType,
|
DWORD &dwSize);
|
static DWORD CountBytes (unsigned char *pucText, const DWORD dwSize,
|
const e_FileType &eFileType, e_FileFormat &eFileFormat);
|
static DWORD CountCharsANSI (const unsigned char *pucText,
|
const DWORD dwSize, unsigned __int64 &ui64Windows,
|
unsigned __int64 &ui64UNIX, unsigned __int64 &ui64Macintosh);
|
static DWORD CountCharsUnicode (const TCHAR *pszText, const DWORD dwSize,
|
unsigned __int64 &ui64Windows, unsigned __int64 &ui64UNIX,
|
unsigned __int64 &ui64Macintosh);
|
// Special case: byte swap whilst counting for efficiencies sake
|
static DWORD CountCharsUnicodeBE (TCHAR *pszText,
|
const DWORD dwSize, unsigned __int64 &ui64Windows,
|
unsigned __int64 &ui64UNIX, unsigned __int64 &ui64Macintosh);
|
static DWORD CountCharsUTF8 (const unsigned char *pucText,
|
const DWORD dwSize, unsigned __int64 &ui64Windows,
|
unsigned __int64 &ui64UNIX, unsigned __int64 &ui64Macintosh);
|
inline static wchar_t Swap (wchar_t wc);
|
static DWORD CheckUTF8Char (const unsigned char *pucBytes, const DWORD dwCharsLeft);
|
static DWORD DecodeUTF8Char (const unsigned char * &pucBytes, wchar_t &wcChar);
|
static void LoadUTF8File (CFile *pFile, const e_FileFormat eFileFormat,
|
wchar_t *pszText, const DWORD dwSize);
|
static void ExpandText (TCHAR *pszText, const DWORD dwSize,
|
const DWORD dwChars, const e_FileType eFileType,
|
const e_FileFormat eFileFormat);
|
static void ZerosToSpaces (TCHAR *pszText, const DWORD dwChars);
|
static void SaveHeader (CFile *pFile, const e_FileType eFileType);
|
static void FillBuffer (const TCHAR * &lpszText, DWORD &dwCharCount,
|
const e_FileType eFileType, const e_FileFormat eFileFormat,
|
unsigned char *ucBuffer, const DWORD dwBufferSize, DWORD &dwSize);
|
|
};
|