2005-09-25 00:12:12 +00:00
|
|
|
/*
|
2023-03-14 22:27:29 +00:00
|
|
|
* PROJECT: ReactOS Notepad
|
|
|
|
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
|
|
|
|
* PURPOSE: Providing a Windows-compatible simple text editor for ReactOS
|
|
|
|
* COPYRIGHT: Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
|
|
|
|
* Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
|
|
|
|
* Copyright 2002 Andriy Palamarchuk
|
|
|
|
* Copyright 2019-2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
|
2005-09-25 00:12:12 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-24 23:00:42 +00:00
|
|
|
#include "notepad.h"
|
2023-02-02 01:10:30 +00:00
|
|
|
#include <assert.h>
|
2005-09-25 00:12:12 +00:00
|
|
|
|
2023-02-20 12:59:16 +00:00
|
|
|
static BOOL IsTextNonZeroASCII(LPCVOID pText, DWORD dwSize)
|
2019-10-31 19:41:48 +00:00
|
|
|
{
|
2023-02-20 12:59:16 +00:00
|
|
|
const signed char *pch = pText;
|
2019-10-31 19:41:48 +00:00
|
|
|
while (dwSize-- > 0)
|
|
|
|
{
|
2023-02-20 12:59:16 +00:00
|
|
|
if (*pch <= 0)
|
2019-10-31 19:41:48 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-02-20 12:59:16 +00:00
|
|
|
++pch;
|
2019-10-31 19:41:48 +00:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-02-20 12:59:16 +00:00
|
|
|
static ENCODING AnalyzeEncoding(const BYTE *pBytes, DWORD dwSize)
|
2019-08-18 13:46:56 +00:00
|
|
|
{
|
2023-02-14 04:34:20 +00:00
|
|
|
INT flags = IS_TEXT_UNICODE_STATISTICS | IS_TEXT_UNICODE_REVERSE_STATISTICS;
|
2019-08-18 13:46:56 +00:00
|
|
|
|
2023-02-20 12:59:16 +00:00
|
|
|
if (IsTextNonZeroASCII(pBytes, dwSize))
|
|
|
|
return ENCODING_DEFAULT;
|
2019-08-18 13:46:56 +00:00
|
|
|
|
|
|
|
if (IsTextUnicode(pBytes, dwSize, &flags))
|
|
|
|
return ENCODING_UTF16LE;
|
|
|
|
|
2023-02-14 04:12:47 +00:00
|
|
|
if (((flags & IS_TEXT_UNICODE_REVERSE_MASK) == IS_TEXT_UNICODE_REVERSE_STATISTICS))
|
2019-08-18 13:46:56 +00:00
|
|
|
return ENCODING_UTF16BE;
|
|
|
|
|
|
|
|
/* is it UTF-8? */
|
2023-02-20 12:59:16 +00:00
|
|
|
if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCSTR)pBytes, dwSize, NULL, 0))
|
2019-08-18 13:46:56 +00:00
|
|
|
return ENCODING_UTF8;
|
|
|
|
|
|
|
|
return ENCODING_ANSI;
|
|
|
|
}
|
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
static VOID
|
|
|
|
ReplaceNewLines(LPWSTR pszNew, SIZE_T cchNew, LPCWSTR pszOld, SIZE_T cchOld)
|
|
|
|
{
|
|
|
|
BOOL bPrevCR = FALSE;
|
|
|
|
SIZE_T ichNew, ichOld;
|
|
|
|
|
|
|
|
for (ichOld = ichNew = 0; ichOld < cchOld; ++ichOld)
|
|
|
|
{
|
|
|
|
WCHAR ch = pszOld[ichOld];
|
|
|
|
|
|
|
|
if (ch == L'\n')
|
|
|
|
{
|
|
|
|
if (!bPrevCR)
|
|
|
|
{
|
|
|
|
pszNew[ichNew++] = L'\r';
|
|
|
|
pszNew[ichNew++] = L'\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == '\r')
|
|
|
|
{
|
|
|
|
pszNew[ichNew++] = L'\r';
|
|
|
|
pszNew[ichNew++] = L'\n';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pszNew[ichNew++] = ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
bPrevCR = (ch == L'\r');
|
|
|
|
}
|
|
|
|
|
|
|
|
pszNew[ichNew] = UNICODE_NULL;
|
|
|
|
assert(ichNew == cchNew);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
ProcessNewLinesAndNulls(HLOCAL *phLocal, LPWSTR *ppszText, SIZE_T *pcchText, EOLN *piEoln)
|
|
|
|
{
|
|
|
|
SIZE_T ich, cchText = *pcchText, adwEolnCount[3] = { 0, 0, 0 }, cNonCRLFs;
|
|
|
|
LPWSTR pszText = *ppszText;
|
|
|
|
EOLN iEoln;
|
|
|
|
BOOL bPrevCR = FALSE;
|
|
|
|
|
|
|
|
/* Replace '\0' with SPACE. Count newlines. */
|
|
|
|
for (ich = 0; ich < cchText; ++ich)
|
|
|
|
{
|
|
|
|
WCHAR ch = pszText[ich];
|
|
|
|
if (ch == UNICODE_NULL)
|
|
|
|
pszText[ich] = L' ';
|
|
|
|
|
|
|
|
if (ch == L'\n')
|
|
|
|
{
|
|
|
|
if (bPrevCR)
|
|
|
|
{
|
|
|
|
adwEolnCount[EOLN_CR]--;
|
|
|
|
adwEolnCount[EOLN_CRLF]++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
adwEolnCount[EOLN_LF]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == '\r')
|
|
|
|
{
|
|
|
|
adwEolnCount[EOLN_CR]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bPrevCR = (ch == L'\r');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Choose the newline code */
|
|
|
|
if (adwEolnCount[EOLN_CR] > adwEolnCount[EOLN_CRLF])
|
|
|
|
iEoln = EOLN_CR;
|
|
|
|
else if (adwEolnCount[EOLN_LF] > adwEolnCount[EOLN_CRLF])
|
|
|
|
iEoln = EOLN_LF;
|
|
|
|
else
|
|
|
|
iEoln = EOLN_CRLF;
|
|
|
|
|
|
|
|
cNonCRLFs = adwEolnCount[EOLN_CR] + adwEolnCount[EOLN_LF];
|
|
|
|
if (cNonCRLFs != 0)
|
|
|
|
{
|
|
|
|
/* Allocate a buffer for EM_SETHANDLE */
|
|
|
|
SIZE_T cchNew = cchText + cNonCRLFs;
|
|
|
|
HLOCAL hLocal = LocalAlloc(LMEM_MOVEABLE, (cchNew + 1) * sizeof(WCHAR));
|
|
|
|
LPWSTR pszNew = LocalLock(hLocal);
|
|
|
|
if (!pszNew)
|
|
|
|
{
|
|
|
|
LocalFree(hLocal);
|
|
|
|
return FALSE; /* Failure */
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceNewLines(pszNew, cchNew, pszText, cchText);
|
|
|
|
|
|
|
|
/* Replace with new data */
|
|
|
|
LocalUnlock(*phLocal);
|
|
|
|
LocalFree(*phLocal);
|
|
|
|
*phLocal = hLocal;
|
|
|
|
*ppszText = pszNew;
|
|
|
|
*pcchText = cchNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
*piEoln = iEoln;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-01-08 13:20:02 +00:00
|
|
|
BOOL
|
2023-02-02 01:10:30 +00:00
|
|
|
ReadText(HANDLE hFile, HLOCAL *phLocal, ENCODING *pencFile, EOLN *piEoln)
|
2005-09-25 00:12:12 +00:00
|
|
|
{
|
2023-02-14 04:25:44 +00:00
|
|
|
LPBYTE pBytes = NULL;
|
2023-02-02 01:10:30 +00:00
|
|
|
LPWSTR pszText, pszNewText = NULL;
|
|
|
|
DWORD dwSize, dwPos;
|
|
|
|
SIZE_T i, cchText, cbContent;
|
2015-01-08 13:20:02 +00:00
|
|
|
BOOL bSuccess = FALSE;
|
2023-02-20 12:59:16 +00:00
|
|
|
ENCODING encFile;
|
2023-02-02 01:10:30 +00:00
|
|
|
UINT iCodePage;
|
|
|
|
HANDLE hMapping = INVALID_HANDLE_VALUE;
|
|
|
|
HLOCAL hNewLocal;
|
2015-01-08 13:20:02 +00:00
|
|
|
|
|
|
|
dwSize = GetFileSize(hFile, NULL);
|
|
|
|
if (dwSize == INVALID_FILE_SIZE)
|
|
|
|
goto done;
|
|
|
|
|
2023-02-09 12:54:20 +00:00
|
|
|
if (dwSize == 0) // If file is empty
|
|
|
|
{
|
|
|
|
hNewLocal = LocalReAlloc(*phLocal, sizeof(UNICODE_NULL), LMEM_MOVEABLE);
|
|
|
|
pszNewText = LocalLock(hNewLocal);
|
|
|
|
if (hNewLocal == NULL || pszNewText == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
*pszNewText = UNICODE_NULL;
|
|
|
|
LocalUnlock(hNewLocal);
|
|
|
|
|
|
|
|
*phLocal = hNewLocal;
|
|
|
|
*piEoln = EOLN_CRLF;
|
2023-02-20 12:59:16 +00:00
|
|
|
*pencFile = ENCODING_DEFAULT;
|
2023-02-09 12:54:20 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
hMapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
|
|
|
if (hMapping == NULL)
|
2015-01-08 13:20:02 +00:00
|
|
|
goto done;
|
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
pBytes = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, dwSize);
|
|
|
|
if (!pBytes)
|
2015-01-08 13:20:02 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Look for Byte Order Marks */
|
2023-02-02 01:10:30 +00:00
|
|
|
dwPos = 0;
|
2015-01-08 13:20:02 +00:00
|
|
|
if ((dwSize >= 2) && (pBytes[0] == 0xFF) && (pBytes[1] == 0xFE))
|
|
|
|
{
|
2019-01-05 09:50:11 +00:00
|
|
|
encFile = ENCODING_UTF16LE;
|
2015-01-08 13:20:02 +00:00
|
|
|
dwPos += 2;
|
|
|
|
}
|
|
|
|
else if ((dwSize >= 2) && (pBytes[0] == 0xFE) && (pBytes[1] == 0xFF))
|
|
|
|
{
|
2019-01-05 09:50:11 +00:00
|
|
|
encFile = ENCODING_UTF16BE;
|
2015-01-08 13:20:02 +00:00
|
|
|
dwPos += 2;
|
|
|
|
}
|
|
|
|
else if ((dwSize >= 3) && (pBytes[0] == 0xEF) && (pBytes[1] == 0xBB) && (pBytes[2] == 0xBF))
|
|
|
|
{
|
2022-09-05 16:21:27 +00:00
|
|
|
encFile = ENCODING_UTF8BOM;
|
2015-01-08 13:20:02 +00:00
|
|
|
dwPos += 3;
|
|
|
|
}
|
2019-08-18 13:46:56 +00:00
|
|
|
else
|
|
|
|
{
|
2023-02-20 12:59:16 +00:00
|
|
|
encFile = AnalyzeEncoding(pBytes, dwSize);
|
2019-08-18 13:46:56 +00:00
|
|
|
}
|
2015-01-08 13:20:02 +00:00
|
|
|
|
2015-03-14 20:00:39 +00:00
|
|
|
switch(encFile)
|
2015-01-08 13:20:02 +00:00
|
|
|
{
|
2019-01-05 09:50:11 +00:00
|
|
|
case ENCODING_UTF16BE:
|
|
|
|
case ENCODING_UTF16LE:
|
2023-02-02 01:10:30 +00:00
|
|
|
{
|
|
|
|
/* Re-allocate the buffer for EM_SETHANDLE */
|
2015-01-08 13:20:02 +00:00
|
|
|
pszText = (LPWSTR) &pBytes[dwPos];
|
2023-02-02 01:10:30 +00:00
|
|
|
cchText = (dwSize - dwPos) / sizeof(WCHAR);
|
|
|
|
hNewLocal = LocalReAlloc(*phLocal, (cchText + 1) * sizeof(WCHAR), LMEM_MOVEABLE);
|
|
|
|
pszNewText = LocalLock(hNewLocal);
|
|
|
|
if (pszNewText == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
*phLocal = hNewLocal;
|
|
|
|
CopyMemory(pszNewText, pszText, cchText * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (encFile == ENCODING_UTF16BE) /* big endian; Swap bytes */
|
|
|
|
{
|
|
|
|
BYTE tmp, *pb = (LPBYTE)pszNewText;
|
|
|
|
for (i = 0; i < cchText * 2; i += 2)
|
|
|
|
{
|
|
|
|
tmp = pb[i];
|
|
|
|
pb[i] = pb[i + 1];
|
|
|
|
pb[i + 1] = tmp;
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 13:20:02 +00:00
|
|
|
break;
|
2023-02-02 01:10:30 +00:00
|
|
|
}
|
2015-01-08 13:20:02 +00:00
|
|
|
|
|
|
|
case ENCODING_ANSI:
|
|
|
|
case ENCODING_UTF8:
|
2022-09-05 16:21:27 +00:00
|
|
|
case ENCODING_UTF8BOM:
|
2023-02-02 01:10:30 +00:00
|
|
|
{
|
|
|
|
iCodePage = ((encFile == ENCODING_UTF8 || encFile == ENCODING_UTF8BOM) ? CP_UTF8 : CP_ACP);
|
2015-01-08 13:20:02 +00:00
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
/* Get ready for ANSI-to-Wide conversion */
|
|
|
|
cbContent = dwSize - dwPos;
|
|
|
|
cchText = 0;
|
|
|
|
if (cbContent > 0)
|
2015-01-08 13:20:02 +00:00
|
|
|
{
|
2023-02-14 04:25:44 +00:00
|
|
|
cchText = MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], (INT)cbContent, NULL, 0);
|
2023-02-02 01:10:30 +00:00
|
|
|
if (cchText == 0)
|
2015-01-08 13:20:02 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
/* Re-allocate the buffer for EM_SETHANDLE */
|
|
|
|
hNewLocal = LocalReAlloc(*phLocal, (cchText + 1) * sizeof(WCHAR), LMEM_MOVEABLE);
|
|
|
|
pszNewText = LocalLock(hNewLocal);
|
|
|
|
if (!pszNewText)
|
2015-01-08 13:20:02 +00:00
|
|
|
goto done;
|
2023-02-02 01:10:30 +00:00
|
|
|
*phLocal = hNewLocal;
|
2015-01-08 13:20:02 +00:00
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
/* Do ANSI-to-Wide conversion */
|
|
|
|
if (cbContent > 0)
|
2015-01-08 13:20:02 +00:00
|
|
|
{
|
2023-02-14 04:25:44 +00:00
|
|
|
if (!MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], (INT)cbContent,
|
|
|
|
pszNewText, (INT)cchText))
|
2023-02-02 01:10:30 +00:00
|
|
|
{
|
2015-01-08 13:20:02 +00:00
|
|
|
goto done;
|
2023-02-02 01:10:30 +00:00
|
|
|
}
|
2015-01-08 13:20:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
DEFAULT_UNREACHABLE;
|
2015-01-08 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
pszNewText[cchText] = UNICODE_NULL;
|
2015-01-08 13:20:02 +00:00
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
if (!ProcessNewLinesAndNulls(phLocal, &pszNewText, &cchText, piEoln))
|
|
|
|
goto done;
|
2015-01-08 13:20:02 +00:00
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
*pencFile = encFile;
|
2015-01-08 13:20:02 +00:00
|
|
|
bSuccess = TRUE;
|
2005-09-25 00:12:12 +00:00
|
|
|
|
|
|
|
done:
|
2015-01-08 13:20:02 +00:00
|
|
|
if (pBytes)
|
2023-02-02 01:10:30 +00:00
|
|
|
UnmapViewOfFile(pBytes);
|
|
|
|
if (hMapping != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hMapping);
|
|
|
|
if (pszNewText)
|
|
|
|
LocalUnlock(*phLocal);
|
2015-01-08 13:20:02 +00:00
|
|
|
return bSuccess;
|
2005-09-25 00:12:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 12:59:16 +00:00
|
|
|
static BOOL WriteEncodedText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, ENCODING encFile)
|
2005-09-25 00:12:12 +00:00
|
|
|
{
|
2015-01-08 13:20:02 +00:00
|
|
|
LPBYTE pBytes = NULL;
|
|
|
|
LPBYTE pAllocBuffer = NULL;
|
|
|
|
DWORD dwPos = 0;
|
|
|
|
DWORD dwByteCount;
|
|
|
|
BYTE buffer[1024];
|
|
|
|
UINT iCodePage = 0;
|
|
|
|
DWORD dwDummy, i;
|
|
|
|
BOOL bSuccess = FALSE;
|
|
|
|
int iBufferSize, iRequiredBytes;
|
|
|
|
BYTE b;
|
|
|
|
|
|
|
|
while(dwPos < dwTextLen)
|
|
|
|
{
|
2015-03-14 20:00:39 +00:00
|
|
|
switch(encFile)
|
2015-01-08 13:20:02 +00:00
|
|
|
{
|
2019-01-05 09:50:11 +00:00
|
|
|
case ENCODING_UTF16LE:
|
2015-01-08 13:20:02 +00:00
|
|
|
pBytes = (LPBYTE) &pszText[dwPos];
|
|
|
|
dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR);
|
|
|
|
dwPos = dwTextLen;
|
|
|
|
break;
|
|
|
|
|
2019-01-05 09:50:11 +00:00
|
|
|
case ENCODING_UTF16BE:
|
2015-01-08 13:20:02 +00:00
|
|
|
dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR);
|
|
|
|
if (dwByteCount > sizeof(buffer))
|
|
|
|
dwByteCount = sizeof(buffer);
|
|
|
|
|
|
|
|
memcpy(buffer, &pszText[dwPos], dwByteCount);
|
|
|
|
for (i = 0; i < dwByteCount; i += 2)
|
|
|
|
{
|
|
|
|
b = buffer[i+0];
|
|
|
|
buffer[i+0] = buffer[i+1];
|
|
|
|
buffer[i+1] = b;
|
|
|
|
}
|
|
|
|
pBytes = (LPBYTE) &buffer[dwPos];
|
|
|
|
dwPos += dwByteCount / sizeof(WCHAR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENCODING_ANSI:
|
|
|
|
case ENCODING_UTF8:
|
2022-09-05 16:21:27 +00:00
|
|
|
case ENCODING_UTF8BOM:
|
|
|
|
if (encFile == ENCODING_UTF8 || encFile == ENCODING_UTF8BOM)
|
2015-01-08 13:20:02 +00:00
|
|
|
iCodePage = CP_UTF8;
|
2022-09-05 16:21:27 +00:00
|
|
|
else
|
|
|
|
iCodePage = CP_ACP;
|
2015-01-08 13:20:02 +00:00
|
|
|
|
|
|
|
iRequiredBytes = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, NULL, 0, NULL, NULL);
|
|
|
|
if (iRequiredBytes <= 0)
|
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
else if (iRequiredBytes < sizeof(buffer))
|
|
|
|
{
|
|
|
|
pBytes = buffer;
|
|
|
|
iBufferSize = sizeof(buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pAllocBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, iRequiredBytes);
|
|
|
|
if (!pAllocBuffer)
|
|
|
|
return FALSE;
|
|
|
|
pBytes = pAllocBuffer;
|
|
|
|
iBufferSize = iRequiredBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwByteCount = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, (LPSTR) pBytes, iBufferSize, NULL, NULL);
|
|
|
|
if (!dwByteCount)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
dwPos = dwTextLen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WriteFile(hFile, pBytes, dwByteCount, &dwDummy, NULL))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* free the buffer, if we have allocated one */
|
|
|
|
if (pAllocBuffer)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAllocBuffer);
|
|
|
|
pAllocBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bSuccess = TRUE;
|
2005-09-25 00:12:12 +00:00
|
|
|
|
|
|
|
done:
|
2015-01-08 13:20:02 +00:00
|
|
|
if (pAllocBuffer)
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAllocBuffer);
|
|
|
|
return bSuccess;
|
2005-09-25 00:12:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 01:10:30 +00:00
|
|
|
BOOL WriteText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, ENCODING encFile, EOLN iEoln)
|
2005-09-25 00:12:12 +00:00
|
|
|
{
|
2015-01-08 13:20:02 +00:00
|
|
|
WCHAR wcBom;
|
|
|
|
LPCWSTR pszLF = L"\n";
|
|
|
|
DWORD dwPos, dwNext;
|
2008-08-01 19:43:18 +00:00
|
|
|
|
2022-09-05 16:21:27 +00:00
|
|
|
/* Write the proper byte order marks if not ANSI or UTF-8 without BOM */
|
|
|
|
if (encFile != ENCODING_ANSI && encFile != ENCODING_UTF8)
|
2008-08-01 19:43:18 +00:00
|
|
|
{
|
2015-01-08 13:20:02 +00:00
|
|
|
wcBom = 0xFEFF;
|
2015-03-14 20:00:39 +00:00
|
|
|
if (!WriteEncodedText(hFile, &wcBom, 1, encFile))
|
2015-01-08 13:20:02 +00:00
|
|
|
return FALSE;
|
2009-01-19 11:13:31 +00:00
|
|
|
}
|
2015-01-08 13:20:02 +00:00
|
|
|
|
|
|
|
dwPos = 0;
|
|
|
|
|
|
|
|
/* pszText eoln are always \r\n */
|
|
|
|
|
|
|
|
do
|
2009-01-19 11:13:31 +00:00
|
|
|
{
|
2015-01-08 13:20:02 +00:00
|
|
|
/* Find the next eoln */
|
|
|
|
dwNext = dwPos;
|
|
|
|
while(dwNext < dwTextLen)
|
|
|
|
{
|
|
|
|
if (pszText[dwNext] == '\r' && pszText[dwNext + 1] == '\n')
|
|
|
|
break;
|
|
|
|
dwNext++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dwNext != dwTextLen)
|
|
|
|
{
|
|
|
|
switch (iEoln)
|
|
|
|
{
|
|
|
|
case EOLN_LF:
|
|
|
|
/* Write text (without eoln) */
|
2015-03-14 20:00:39 +00:00
|
|
|
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, encFile))
|
2015-01-08 13:20:02 +00:00
|
|
|
return FALSE;
|
|
|
|
/* Write eoln */
|
2015-03-14 20:00:39 +00:00
|
|
|
if (!WriteEncodedText(hFile, pszLF, 1, encFile))
|
2015-01-08 13:20:02 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
case EOLN_CR:
|
|
|
|
/* Write text (including \r as eoln) */
|
2015-03-14 20:00:39 +00:00
|
|
|
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos + 1, encFile))
|
2015-01-08 13:20:02 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
case EOLN_CRLF:
|
|
|
|
/* Write text (including \r\n as eoln) */
|
2015-03-14 20:00:39 +00:00
|
|
|
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos + 2, encFile))
|
2015-01-08 13:20:02 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write text (without eoln, since this is the end of the file) */
|
2015-03-14 20:00:39 +00:00
|
|
|
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, encFile))
|
2015-01-08 13:20:02 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip \r\n */
|
|
|
|
dwPos = dwNext + 2;
|
2008-08-01 19:43:18 +00:00
|
|
|
}
|
2015-01-08 13:20:02 +00:00
|
|
|
while (dwPos < dwTextLen);
|
2008-08-01 19:43:18 +00:00
|
|
|
|
2015-01-08 13:20:02 +00:00
|
|
|
return TRUE;
|
2005-09-25 00:12:12 +00:00
|
|
|
}
|