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 (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.iEncoding, Globals.iEoln)) if (size)
ShowLastError(); {
else if (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.iEncoding, Globals.iEoln))
SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0); ShowLastError();
else
SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
}
CloseHandle(hFile); CloseHandle(hFile);
HeapFree(GetProcessHeap(), 0, pTemp); HeapFree(GetProcessHeap(), 0, pTemp);

View file

@ -316,79 +316,73 @@ 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;
/* 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[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';
bEoln[1] = '\n';
pbEoln = (LPBYTE) &bEoln;
dwEolnSize = 2;
break;
default:
return FALSE;
}
/* If we have an eoln, make sure it is of the proper encoding */ dwPos = 0;
if (pbEoln && ((iEncoding == ENCODING_UNICODE) || (iEncoding == ENCODING_UNICODE_BE)))
{
wcEoln = bEoln;
pbEoln = (LPBYTE) &wcEoln;
dwEolnSize = sizeof(wcEoln);
}
dwPos = 0; /* pszText eoln are always \r\n */
while(dwPos < dwTextLen) do
{ {
if (pbEoln) /* Find the next eoln */
{ dwNext = dwPos;
/* Find the next eoln */ while(dwNext < dwTextLen)
dwNext = dwPos; {
while(dwNext < dwTextLen-1) if (pszText[dwNext] == '\r' && pszText[dwNext + 1] == '\n')
{ break;
if ((pszText[dwNext] == '\r') && (pszText[dwNext+1] == '\n')) dwNext++;
break; }
dwNext++;
}
}
else
{
/* No eoln conversion is necessary */
dwNext = dwTextLen;
}
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding)) /* Write text (without eoln) */
return FALSE; if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding))
dwPos = dwNext; return FALSE;
/* 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;
}
}
while(dwPos < dwTextLen);
return TRUE; /* Skip \r\n */
dwPos = dwNext + 2;
}
while (dwPos < dwTextLen);
return TRUE;
} }