Fixed property svn:eol-style

svn path=/trunk/; revision=18039
This commit is contained in:
Nathan Woods 2005-09-25 00:12:12 +00:00
parent e8bb7ed8ff
commit fd73ac02d7

View file

@ -1,389 +1,389 @@
/* /*
* Notepad (text.c) * Notepad (text.c)
* *
* Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch> * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
* Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr> * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
* Copyright 2002 Andriy Palamarchuk * Copyright 2002 Andriy Palamarchuk
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#define UNICODE #define UNICODE
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <windows.h> #include <windows.h>
#include <commdlg.h> #include <commdlg.h>
#include "main.h" #include "main.h"
static BOOL Append(LPWSTR *ppszText, DWORD *pdwTextLen, LPCWSTR pszAppendText, DWORD dwAppendLen) static BOOL Append(LPWSTR *ppszText, DWORD *pdwTextLen, LPCWSTR pszAppendText, DWORD dwAppendLen)
{ {
LPWSTR pszNewText; LPWSTR pszNewText;
if (dwAppendLen > 0) if (dwAppendLen > 0)
{ {
if (*ppszText) if (*ppszText)
{ {
pszNewText = (LPWSTR) HeapReAlloc(GetProcessHeap(), 0, *ppszText, (*pdwTextLen + dwAppendLen) * sizeof(WCHAR)); pszNewText = (LPWSTR) HeapReAlloc(GetProcessHeap(), 0, *ppszText, (*pdwTextLen + dwAppendLen) * sizeof(WCHAR));
} }
else else
{ {
pszNewText = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, dwAppendLen * sizeof(WCHAR)); pszNewText = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, dwAppendLen * sizeof(WCHAR));
} }
if (!pszNewText) if (!pszNewText)
return FALSE; return FALSE;
memcpy(pszNewText + *pdwTextLen, pszAppendText, dwAppendLen * sizeof(WCHAR)); memcpy(pszNewText + *pdwTextLen, pszAppendText, dwAppendLen * sizeof(WCHAR));
*ppszText = pszNewText; *ppszText = pszNewText;
*pdwTextLen += dwAppendLen; *pdwTextLen += dwAppendLen;
} }
return TRUE; return TRUE;
} }
BOOL ReadText(HANDLE hFile, LPWSTR *ppszText, DWORD *pdwTextLen, int *piEncoding, int *piEoln) BOOL ReadText(HANDLE hFile, LPWSTR *ppszText, DWORD *pdwTextLen, int *piEncoding, int *piEoln)
{ {
DWORD dwSize; DWORD dwSize;
LPBYTE pBytes = NULL; LPBYTE pBytes = NULL;
LPCWSTR pszText; LPCWSTR pszText;
LPWSTR pszAllocText = NULL; LPWSTR pszAllocText = NULL;
DWORD dwPos, i; DWORD dwPos, i;
DWORD dwCharCount; DWORD dwCharCount;
BOOL bSuccess = FALSE; BOOL bSuccess = FALSE;
BYTE b; BYTE b;
int iEncoding = ENCODING_ANSI; int iEncoding = ENCODING_ANSI;
int iCodePage; int iCodePage;
WCHAR szCrlf[2] = { '\r', '\n' }; WCHAR szCrlf[2] = { '\r', '\n' };
DWORD adwEolnCount[3] = { 0, 0, 0 }; DWORD adwEolnCount[3] = { 0, 0, 0 };
*ppszText = NULL; *ppszText = NULL;
*pdwTextLen = 0; *pdwTextLen = 0;
dwSize = GetFileSize(hFile, NULL); dwSize = GetFileSize(hFile, NULL);
if (dwSize == INVALID_FILE_SIZE) if (dwSize == INVALID_FILE_SIZE)
goto done; goto done;
pBytes = HeapAlloc(GetProcessHeap(), 0, dwSize + 2); pBytes = HeapAlloc(GetProcessHeap(), 0, dwSize + 2);
if (!pBytes) if (!pBytes)
goto done; goto done;
if (!ReadFile(hFile, pBytes, dwSize, &dwSize, NULL)) if (!ReadFile(hFile, pBytes, dwSize, &dwSize, NULL))
goto done; goto done;
dwPos = 0; dwPos = 0;
/* Make sure that there is a NUL character at the end, in any encoding */ /* Make sure that there is a NUL character at the end, in any encoding */
pBytes[dwSize + 0] = '\0'; pBytes[dwSize + 0] = '\0';
pBytes[dwSize + 1] = '\0'; pBytes[dwSize + 1] = '\0';
/* Look for Byte Order Marks */ /* Look for Byte Order Marks */
if ((dwSize >= 2) && (pBytes[0] == 0xFF) && (pBytes[1] == 0xFE)) if ((dwSize >= 2) && (pBytes[0] == 0xFF) && (pBytes[1] == 0xFE))
{ {
iEncoding = ENCODING_UNICODE; iEncoding = ENCODING_UNICODE;
dwPos += 2; dwPos += 2;
} }
else if ((dwSize >= 2) && (pBytes[0] == 0xFE) && (pBytes[1] == 0xFF)) else if ((dwSize >= 2) && (pBytes[0] == 0xFE) && (pBytes[1] == 0xFF))
{ {
iEncoding = ENCODING_UNICODE_BE; iEncoding = ENCODING_UNICODE_BE;
dwPos += 2; dwPos += 2;
} }
else if ((dwSize >= 3) && (pBytes[0] == 0xEF) && (pBytes[1] == 0xBB) && (pBytes[2] == 0xBF)) else if ((dwSize >= 3) && (pBytes[0] == 0xEF) && (pBytes[1] == 0xBB) && (pBytes[2] == 0xBF))
{ {
iEncoding = ENCODING_UTF8; iEncoding = ENCODING_UTF8;
dwPos += 3; dwPos += 3;
} }
switch(iEncoding) switch(iEncoding)
{ {
case ENCODING_UNICODE_BE: case ENCODING_UNICODE_BE:
for (i = dwPos; i < dwSize-1; i += 2) for (i = dwPos; i < dwSize-1; i += 2)
{ {
b = pBytes[i+0]; b = pBytes[i+0];
pBytes[i+0] = pBytes[i+1]; pBytes[i+0] = pBytes[i+1];
pBytes[i+1] = b; pBytes[i+1] = b;
} }
/* fall through */ /* fall through */
case ENCODING_UNICODE: case ENCODING_UNICODE:
pszText = (LPCWSTR) &pBytes[dwPos]; pszText = (LPCWSTR) &pBytes[dwPos];
dwCharCount = (dwSize - dwPos) / sizeof(WCHAR); dwCharCount = (dwSize - dwPos) / sizeof(WCHAR);
break; break;
case ENCODING_ANSI: case ENCODING_ANSI:
case ENCODING_UTF8: case ENCODING_UTF8:
if (iEncoding == ENCODING_ANSI) if (iEncoding == ENCODING_ANSI)
iCodePage = CP_ACP; iCodePage = CP_ACP;
else if (iEncoding == ENCODING_UTF8) else if (iEncoding == ENCODING_UTF8)
iCodePage = CP_UTF8; iCodePage = CP_UTF8;
else else
goto done; goto done;
dwCharCount = MultiByteToWideChar(iCodePage, 0, &pBytes[dwPos], dwSize - dwPos, NULL, 0); dwCharCount = MultiByteToWideChar(iCodePage, 0, &pBytes[dwPos], dwSize - dwPos, NULL, 0);
if (dwCharCount == 0) if (dwCharCount == 0)
goto done; goto done;
pszAllocText = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (dwCharCount + 1) * sizeof(WCHAR)); pszAllocText = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (dwCharCount + 1) * sizeof(WCHAR));
if (!pszAllocText) if (!pszAllocText)
goto done; goto done;
if (!MultiByteToWideChar(iCodePage, 0, &pBytes[dwPos], dwSize - dwPos, pszAllocText, dwCharCount)) if (!MultiByteToWideChar(iCodePage, 0, &pBytes[dwPos], dwSize - dwPos, pszAllocText, dwCharCount))
goto done; goto done;
pszAllocText[dwCharCount] = '\0'; pszAllocText[dwCharCount] = '\0';
pszText = pszAllocText; pszText = pszAllocText;
break; break;
} }
dwPos = 0; dwPos = 0;
for (i = 0; i < dwCharCount; i++) for (i = 0; i < dwCharCount; i++)
{ {
switch(pszText[i]) switch(pszText[i])
{ {
case '\r': case '\r':
if ((i < dwCharCount-1) && (pszText[i+1] == '\n')) if ((i < dwCharCount-1) && (pszText[i+1] == '\n'))
{ {
i++; i++;
adwEolnCount[EOLN_CRLF]++; adwEolnCount[EOLN_CRLF]++;
break; break;
} }
/* fall through */ /* fall through */
case '\n': case '\n':
if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos)) if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos))
return FALSE; return FALSE;
if (!Append(ppszText, pdwTextLen, szCrlf, sizeof(szCrlf) / sizeof(szCrlf[0]))) if (!Append(ppszText, pdwTextLen, szCrlf, sizeof(szCrlf) / sizeof(szCrlf[0])))
return FALSE; return FALSE;
dwPos = i + 1; dwPos = i + 1;
if (pszText[i] == '\r') if (pszText[i] == '\r')
adwEolnCount[EOLN_CR]++; adwEolnCount[EOLN_CR]++;
else else
adwEolnCount[EOLN_LF]++; adwEolnCount[EOLN_LF]++;
break; break;
} }
} }
if (!*ppszText && (pszText == pszAllocText)) if (!*ppszText && (pszText == pszAllocText))
{ {
/* special case; don't need to reallocate */ /* special case; don't need to reallocate */
*ppszText = pszAllocText; *ppszText = pszAllocText;
*pdwTextLen = dwCharCount; *pdwTextLen = dwCharCount;
pszAllocText = NULL; pszAllocText = NULL;
} }
else else
{ {
/* append last remaining text */ /* append last remaining text */
if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos + 1)) if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos + 1))
return FALSE; return FALSE;
} }
/* chose which eoln to use */ /* chose which eoln to use */
*piEoln = EOLN_CRLF; *piEoln = EOLN_CRLF;
if (adwEolnCount[EOLN_LF] > adwEolnCount[*piEoln]) if (adwEolnCount[EOLN_LF] > adwEolnCount[*piEoln])
*piEoln = EOLN_LF; *piEoln = EOLN_LF;
if (adwEolnCount[EOLN_CR] > adwEolnCount[*piEoln]) if (adwEolnCount[EOLN_CR] > adwEolnCount[*piEoln])
*piEoln = EOLN_CR; *piEoln = EOLN_CR;
*piEncoding = iEncoding; *piEncoding = iEncoding;
bSuccess = TRUE; bSuccess = TRUE;
done: done:
if (pBytes) if (pBytes)
HeapFree(GetProcessHeap(), 0, pBytes); HeapFree(GetProcessHeap(), 0, pBytes);
if (pszAllocText) if (pszAllocText)
HeapFree(GetProcessHeap(), 0, pszAllocText); HeapFree(GetProcessHeap(), 0, pszAllocText);
if (!bSuccess && *ppszText) if (!bSuccess && *ppszText)
{ {
HeapFree(GetProcessHeap(), 0, *ppszText); HeapFree(GetProcessHeap(), 0, *ppszText);
*ppszText = NULL; *ppszText = NULL;
*pdwTextLen = 0; *pdwTextLen = 0;
} }
return bSuccess; return bSuccess;
} }
static BOOL WriteEncodedText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, int iEncoding) static BOOL WriteEncodedText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, int iEncoding)
{ {
LPBYTE pBytes; LPBYTE pBytes;
LPBYTE pAllocBuffer = NULL; LPBYTE pAllocBuffer = NULL;
DWORD dwPos = 0; DWORD dwPos = 0;
DWORD dwByteCount; DWORD dwByteCount;
BYTE buffer[1024]; BYTE buffer[1024];
UINT iCodePage; UINT iCodePage;
DWORD dwDummy, i; DWORD dwDummy, i;
BOOL bSuccess; BOOL bSuccess;
int iBufferSize, iRequiredBytes; int iBufferSize, iRequiredBytes;
BYTE b; BYTE b;
while(dwPos < dwTextLen) while(dwPos < dwTextLen)
{ {
switch(iEncoding) switch(iEncoding)
{ {
case ENCODING_UNICODE: case ENCODING_UNICODE:
pBytes = (LPBYTE) &pszText[dwPos]; pBytes = (LPBYTE) &pszText[dwPos];
dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR); dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR);
dwPos = dwTextLen; dwPos = dwTextLen;
break; break;
case ENCODING_UNICODE_BE: case ENCODING_UNICODE_BE:
dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR); dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR);
if (dwByteCount > sizeof(buffer)) if (dwByteCount > sizeof(buffer))
dwByteCount = sizeof(buffer); dwByteCount = sizeof(buffer);
memcpy(buffer, &pszText[dwPos], dwByteCount); memcpy(buffer, &pszText[dwPos], dwByteCount);
for (i = 0; i < dwByteCount; i += 2) for (i = 0; i < dwByteCount; i += 2)
{ {
b = buffer[i+0]; b = buffer[i+0];
buffer[i+0] = buffer[i+1]; buffer[i+0] = buffer[i+1];
buffer[i+1] = b; buffer[i+1] = b;
} }
dwPos += dwByteCount / sizeof(WCHAR); dwPos += dwByteCount / sizeof(WCHAR);
break; break;
case ENCODING_ANSI: case ENCODING_ANSI:
case ENCODING_UTF8: case ENCODING_UTF8:
if (iEncoding == ENCODING_ANSI) if (iEncoding == ENCODING_ANSI)
iCodePage = CP_ACP; iCodePage = CP_ACP;
else if (iEncoding == ENCODING_UTF8) else if (iEncoding == ENCODING_UTF8)
iCodePage = CP_UTF8; iCodePage = CP_UTF8;
else else
goto done; goto done;
iRequiredBytes = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, NULL, 0, NULL, NULL); iRequiredBytes = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, NULL, 0, NULL, NULL);
if (iRequiredBytes <= 0) if (iRequiredBytes <= 0)
{ {
goto done; goto done;
} }
else if (iRequiredBytes < sizeof(buffer)) else if (iRequiredBytes < sizeof(buffer))
{ {
pBytes = buffer; pBytes = buffer;
iBufferSize = sizeof(buffer); iBufferSize = sizeof(buffer);
} }
else else
{ {
pAllocBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, iRequiredBytes); pAllocBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, iRequiredBytes);
if (!pAllocBuffer) if (!pAllocBuffer)
return FALSE; return FALSE;
pBytes = pAllocBuffer; pBytes = pAllocBuffer;
iBufferSize = iRequiredBytes; iBufferSize = iRequiredBytes;
} }
dwByteCount = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, (LPSTR) pBytes, iBufferSize, NULL, NULL); dwByteCount = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, (LPSTR) pBytes, iBufferSize, NULL, NULL);
if (!dwByteCount) if (!dwByteCount)
goto done; goto done;
dwPos = dwTextLen; dwPos = dwTextLen;
break; break;
default: default:
goto done; goto done;
} }
if (!WriteFile(hFile, pBytes, dwByteCount, &dwDummy, NULL)) if (!WriteFile(hFile, pBytes, dwByteCount, &dwDummy, NULL))
goto done; goto done;
/* free the buffer, if we have allocated one */ /* free the buffer, if we have allocated one */
if (pAllocBuffer) if (pAllocBuffer)
{ {
HeapFree(GetProcessHeap(), 0, pAllocBuffer); HeapFree(GetProcessHeap(), 0, pAllocBuffer);
pAllocBuffer = NULL; pAllocBuffer = NULL;
} }
} }
bSuccess = TRUE; bSuccess = TRUE;
done: done:
if (pAllocBuffer) if (pAllocBuffer)
HeapFree(GetProcessHeap(), 0, pAllocBuffer); HeapFree(GetProcessHeap(), 0, pAllocBuffer);
return bSuccess; return bSuccess;
} }
BOOL WriteText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, int iEncoding, int iEoln) BOOL WriteText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, int iEncoding, int iEoln)
{ {
WCHAR wcBom; WCHAR wcBom;
WCHAR wcEoln; WCHAR wcEoln;
BYTE bEoln; BYTE bEoln;
LPBYTE pbEoln = NULL; LPBYTE pbEoln = NULL;
DWORD dwDummy, dwPos, dwNext, dwEolnSize = 0; DWORD dwDummy, dwPos, dwNext, dwEolnSize = 0;
/* Write the proper byte order marks if not ANSI */ /* Write the proper byte order marks if not ANSI */
if (iEncoding != ENCODING_ANSI) if (iEncoding != ENCODING_ANSI)
{ {
wcBom = 0xFEFF; wcBom = 0xFEFF;
if (!WriteEncodedText(hFile, &wcBom, 1, iEncoding)) if (!WriteEncodedText(hFile, &wcBom, 1, iEncoding))
return FALSE; return FALSE;
} }
/* Identify the proper eoln to use */ /* Identify the proper eoln to use */
switch(iEoln) switch(iEoln)
{ {
case EOLN_LF: case EOLN_LF:
bEoln = '\n'; bEoln = '\n';
pbEoln = &bEoln; pbEoln = &bEoln;
dwEolnSize = sizeof(bEoln); dwEolnSize = sizeof(bEoln);
break; break;
case EOLN_CR: case EOLN_CR:
bEoln = '\r'; bEoln = '\r';
pbEoln = &bEoln; pbEoln = &bEoln;
dwEolnSize = sizeof(bEoln); dwEolnSize = sizeof(bEoln);
break; break;
} }
/* If we have an eoln, make sure it is of the proper encoding */ /* If we have an eoln, make sure it is of the proper encoding */
if (pbEoln && ((iEncoding == ENCODING_UNICODE) || (iEncoding == ENCODING_UNICODE_BE))) if (pbEoln && ((iEncoding == ENCODING_UNICODE) || (iEncoding == ENCODING_UNICODE_BE)))
{ {
wcEoln = bEoln; wcEoln = bEoln;
pbEoln = (LPBYTE) &wcEoln; pbEoln = (LPBYTE) &wcEoln;
dwEolnSize = sizeof(wcEoln); dwEolnSize = sizeof(wcEoln);
} }
dwPos = 0; dwPos = 0;
while(dwPos < dwTextLen) while(dwPos < dwTextLen)
{ {
if (pbEoln) if (pbEoln)
{ {
/* Find the next eoln */ /* Find the next eoln */
dwNext = dwPos; dwNext = dwPos;
while(dwNext < dwTextLen-1) while(dwNext < dwTextLen-1)
{ {
if ((pszText[dwNext] == '\r') && (pszText[dwNext+1] == '\n')) if ((pszText[dwNext] == '\r') && (pszText[dwNext+1] == '\n'))
break; break;
dwNext++; dwNext++;
} }
} }
else else
{ {
/* No eoln conversion is necessary */ /* No eoln conversion is necessary */
dwNext = dwTextLen; dwNext = dwTextLen;
} }
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding)) if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding))
return FALSE; return FALSE;
dwPos = dwNext; dwPos = dwNext;
/* are we at an eoln? */ /* are we at an eoln? */
while ((dwPos < dwTextLen-1) && while ((dwPos < dwTextLen-1) &&
((pszText[dwPos] == '\r') && (pszText[dwPos+1] == '\n'))) ((pszText[dwPos] == '\r') && (pszText[dwPos+1] == '\n')))
{ {
if (!WriteFile(hFile, pbEoln, dwEolnSize, &dwDummy, NULL)) if (!WriteFile(hFile, pbEoln, dwEolnSize, &dwDummy, NULL))
return FALSE; return FALSE;
dwPos += 2; dwPos += 2;
} }
} }
while(dwPos < dwTextLen); while(dwPos < dwTextLen);
return TRUE; return TRUE;
} }