Changes by Hervé Poussineau:

- Simplified code for WriteText which avoids infinite loops and fixes EOLN conversion. This fixes bug 3551
Changes by Pierre Schweitzer
- Removed some non-understandable unicode code in WriteText
- Added a check to prevent crash when trying to create an empty file
- Fixed formatting for WriteText
See issue #3551 for more details.

svn path=/trunk/; revision=35018
This commit is contained in:
Pierre Schweitzer 2008-08-01 19:43:18 +00:00
parent f81b3e63d2
commit 160a7614dd
2 changed files with 67 additions and 70 deletions

View file

@ -235,10 +235,13 @@ static VOID DoSaveFile(VOID)
} }
#endif #endif
if (size)
{
if (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.iEncoding, Globals.iEoln)) if (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.iEncoding, Globals.iEoln))
ShowLastError(); ShowLastError();
else else
SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0); SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
}
CloseHandle(hFile); CloseHandle(hFile);
HeapFree(GetProcessHeap(), 0, pTemp); HeapFree(GetProcessHeap(), 0, pTemp);

View file

@ -317,8 +317,7 @@ done:
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; BYTE bEoln[1];
BYTE bEoln;
LPBYTE pbEoln = NULL; LPBYTE pbEoln = NULL;
DWORD dwDummy, dwPos, dwNext, dwEolnSize = 0; DWORD dwDummy, dwPos, dwNext, dwEolnSize = 0;
@ -334,60 +333,55 @@ BOOL WriteText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, int iEncoding, in
switch(iEoln) switch(iEoln)
{ {
case EOLN_LF: case EOLN_LF:
bEoln = '\n'; bEoln[0] = '\n';
pbEoln = &bEoln; pbEoln = (LPBYTE) &bEoln;
dwEolnSize = sizeof(bEoln); dwEolnSize = 1;
break; break;
case EOLN_CR: case EOLN_CR:
bEoln = '\r'; bEoln[0] = '\r';
pbEoln = &bEoln; pbEoln = (LPBYTE) &bEoln;
dwEolnSize = sizeof(bEoln); dwEolnSize = 1;
break; break;
} case EOLN_CRLF:
bEoln[0] = '\r';
/* If we have an eoln, make sure it is of the proper encoding */ bEoln[1] = '\n';
if (pbEoln && ((iEncoding == ENCODING_UNICODE) || (iEncoding == ENCODING_UNICODE_BE))) pbEoln = (LPBYTE) &bEoln;
{ dwEolnSize = 2;
wcEoln = bEoln; break;
pbEoln = (LPBYTE) &wcEoln; default:
dwEolnSize = sizeof(wcEoln); return FALSE;
} }
dwPos = 0; dwPos = 0;
while(dwPos < dwTextLen) /* pszText eoln are always \r\n */
{
if (pbEoln) do
{ {
/* Find the next eoln */ /* Find the next eoln */
dwNext = dwPos; dwNext = dwPos;
while(dwNext < dwTextLen-1) while(dwNext < dwTextLen)
{ {
if ((pszText[dwNext] == '\r') && (pszText[dwNext+1] == '\n')) if (pszText[dwNext] == '\r' && pszText[dwNext + 1] == '\n')
break; break;
dwNext++; dwNext++;
} }
}
else
{
/* No eoln conversion is necessary */
dwNext = dwTextLen;
}
/* Write text (without eoln) */
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding)) if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding))
return FALSE; return FALSE;
dwPos = dwNext;
/* are we at an eoln? */ /* Write eoln */
while ((dwPos < dwTextLen-1) && if (dwNext != dwTextLen)
((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;
} }
/* Skip \r\n */
dwPos = dwNext + 2;
} }
while(dwPos < dwTextLen); while (dwPos < dwTextLen);
return TRUE; return TRUE;
} }