Code:
/** Header:
#include <windows.h>
wchar_t* MBStr2WStr(LPCSTR strIn, UINT codePage);
char* WStr2MBStr(LPCWSTR strIn, UINT codePage);
char* MBStr2MBStr(LPCSTR strIn, UINT codepageIN, UINT codepageOUT);
BOOL WriteUTF8File(LPCTSTR szFileName, LPCTSTR szText);
TCHAR* ReadUTF8File(LPCTSTR szFileName);
**/
#include <windows.h>
#include <malloc.h>
/*
* Converts a unciode string to a multi-byte string with the specified code
* page(CP_ACP for the default). Returned string must be released with free().
*/
char* WStr2MBStr(LPCWSTR strIn, UINT codePage)
{
int cchSize;
char* strOut;
if (!strIn) return NULL;
cchSize = WideCharToMultiByte(codePage, 0, strIn, -1, 0, 0, NULL, NULL);
if (0 == cchSize) return NULL;
strOut = (char*) malloc(cchSize * sizeof(char));
if (NULL == strOut) return NULL;
if (0 == WideCharToMultiByte(codePage, 0, strIn, -1, strOut, cchSize, NULL, NULL))
{
free(strOut);
return NULL;
}
return strOut;
}
/*
* Converts a multi-byte string with the specified code page(CP_ACP for the default)
* to a unicode string. Returned string must be released with free().
*/
wchar_t* MBStr2WStr(LPCSTR strIn, UINT codePage)
{
int cchSize;
wchar_t* strOut;
if (!strIn) return NULL;
cchSize = MultiByteToWideChar(codePage, 0, strIn, -1, 0, 0);
if (0 == cchSize) return NULL;
strOut = (wchar_t*) malloc(cchSize * sizeof(wchar_t));
if (NULL == strOut) return NULL;
if (0 == MultiByteToWideChar(codePage, 0, strIn, -1, strOut, cchSize))
{
free(strOut);
return NULL;
}
return strOut;
}
/*
* Converts a multi-byte string with the specified input code page to a
* multi-byte string with the specified output code page.
* Returned string must be released with free().
*/
char* MBStr2MBStr(LPCSTR strIn, UINT codepageIN, UINT codepageOUT)
{
char* strOut = NULL;
wchar_t* strTextWide = MBStr2WStr(strIn, codepageIN);
if (strTextWide)
{
strOut = WStr2MBStr(strTextWide, codepageOUT);
free(strTextWide);
}
return strOut;
}
/*
* Write out text to a file in UTF8 format.
*/
BOOL WriteUTF8File(LPCTSTR szFileName, LPCTSTR szText)
{
HANDLE hFile;
char* szTextUTF8 = NULL;
BOOL bRet = FALSE;
DWORD dwWritten;
/* Open file. */
hFile = CreateFile(szFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) return FALSE;
/* Convert text into UTF8. */
#ifdef UNICODE
szTextUTF8 = WStr2MBStr(szText, CP_UTF8);
#else
szTextUTF8 = MBStr2MBStr(szText, CP_ACP, CP_UTF8);
#endif
/* Write UTF8 text to file. */
if (NULL != szTextUTF8)
{
bRet = WriteFile(hFile, szTextUTF8, lstrlenA(szTextUTF8), &dwWritten, NULL);
}
/* Cleanup. */
free(szTextUTF8);
CloseHandle(hFile);
return bRet;
}
/*
* Read text from a file in UTF8 format. Returned text must be released with free().
*/
TCHAR* ReadUTF8File(LPCTSTR szFileName)
{
HANDLE hFile;
char* szTextUTF8;
DWORD dwFileSize;
DWORD dwRead = 0;
TCHAR* szTextOut = NULL;
/* Open file. */
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) return NULL;
/* Get file size. */
dwFileSize = GetFileSize(hFile, NULL);
if (INVALID_FILE_SIZE == dwFileSize)
{
CloseHandle(hFile);
return NULL;
}
/* Allocate memory for buffer to read in UTF8 text. */
szTextUTF8 = malloc(dwFileSize + 1);
if (NULL == szTextUTF8)
{
CloseHandle(hFile);
return NULL;
}
/* Read text in UTF8 format. */
if (!ReadFile(hFile, szTextUTF8, dwFileSize, &dwRead, NULL))
{
free(szTextUTF8);
CloseHandle(hFile);
return NULL;
}
/* Terminate buffer. */
szTextUTF8[dwRead] = '\0';
/* Convert UTF8 text into TSTR. */
#ifdef UNICODE
szTextOut = MBStr2WStr(szTextUTF8, CP_UTF8);
#else
szTextOut = MBStr2MBStr(szTextUTF8, CP_UTF8, CP_ACP);
#endif
/* Cleanup. */
free(szTextUTF8);
CloseHandle(hFile);
return szTextOut;
}