);\r
\r
/**\r
- Function to write a line of unicode text to a file.\r
+ Function to write a line of text to a file.\r
+ \r
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode \r
+ text.\r
+ If the file is an ASCII file then write the ASCII text.\r
+ If the size of file is zero (without file tag at the beginning) then write \r
+ ASCII text as default.\r
\r
@param[in] Handle FileHandle to write to.\r
@param[in] Buffer Buffer to write, if NULL the function will\r
@retval EFI_SUCCESS The data was written.\r
Buffer is NULL.\r
@retval EFI_INVALID_PARAMETER Handle is NULL.\r
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII \r
+ string due to out of resources.\r
\r
@sa FileHandleWrite\r
**/\r
}\r
\r
/**\r
- Function to write a line of unicode text to a file.\r
+ Function to write a line of text to a file.\r
+ \r
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode \r
+ text.\r
+ If the file is an ASCII file then write the ASCII text.\r
+ If the size of file is zero (without file tag at the beginning) then write \r
+ ASCII text as default.\r
\r
@param[in] Handle FileHandle to write to.\r
@param[in] Buffer Buffer to write, if NULL the function will\r
@retval EFI_SUCCESS The data was written.\r
Buffer is NULL.\r
@retval EFI_INVALID_PARAMETER Handle is NULL.\r
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII \r
+ string due to out of resources.\r
\r
@sa FileHandleWrite\r
**/\r
IN CHAR16 *Buffer\r
)\r
{\r
- EFI_STATUS Status;\r
- UINTN Size;\r
+ EFI_STATUS Status;\r
+ CHAR16 CharBuffer;\r
+ UINTN Size;\r
+ UINTN CharSize;\r
+ UINT64 FileSize;\r
+ UINT64 OriginalFilePosition;\r
+ BOOLEAN Ascii;\r
+ CHAR8 *AsciiBuffer;\r
\r
if (Buffer == NULL) {\r
return (EFI_SUCCESS);\r
if (Handle == NULL) {\r
return (EFI_INVALID_PARAMETER);\r
}\r
-\r
- Size = StrSize(Buffer) - sizeof(Buffer[0]);\r
- Status = FileHandleWrite(Handle, &Size, Buffer);\r
+ \r
+ Ascii = FALSE;\r
+ AsciiBuffer = NULL;\r
+ \r
+ Status = FileHandleGetPosition(Handle, &OriginalFilePosition);\r
if (EFI_ERROR(Status)) {\r
- return (Status);\r
+ return Status;\r
+ }\r
+ \r
+ Status = FileHandleSetPosition(Handle, 0);\r
+ if (EFI_ERROR(Status)) {\r
+ return Status;\r
}\r
- Size = StrSize(L"\r\n") - sizeof(CHAR16);\r
- return FileHandleWrite(Handle, &Size, L"\r\n");\r
+ \r
+ Status = FileHandleGetSize(Handle, &FileSize);\r
+ if (EFI_ERROR(Status)) {\r
+ return Status;\r
+ }\r
+ \r
+ if (FileSize == 0) {\r
+ Ascii = TRUE;\r
+ } else {\r
+ CharSize = sizeof (CHAR16);\r
+ Status = FileHandleRead (Handle, &CharSize, &CharBuffer);\r
+ ASSERT_EFI_ERROR (Status);\r
+ if (CharBuffer == gUnicodeFileTag) {\r
+ Ascii = FALSE;\r
+ } else {\r
+ Ascii = TRUE;\r
+ }\r
+ }\r
+ \r
+ Status = FileHandleSetPosition(Handle, OriginalFilePosition);\r
+ if (EFI_ERROR(Status)) {\r
+ return Status;\r
+ }\r
+ \r
+ if (Ascii) {\r
+ Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);\r
+ AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);\r
+ if (AsciiBuffer == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ UnicodeStrToAsciiStr (Buffer, AsciiBuffer);\r
+ \r
+ Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);\r
+ Status = FileHandleWrite(Handle, &Size, AsciiBuffer);\r
+ if (EFI_ERROR(Status)) {\r
+ FreePool (AsciiBuffer);\r
+ return (Status);\r
+ }\r
+ Size = AsciiStrSize("\r\n") - sizeof(CHAR8);\r
+ Status = FileHandleWrite(Handle, &Size, "\r\n");\r
+ } else {\r
+ if (OriginalFilePosition == 0) {\r
+ Status = FileHandleSetPosition (Handle, sizeof(CHAR16));\r
+ if (EFI_ERROR(Status)) {\r
+ return Status;\r
+ }\r
+ }\r
+ Size = StrSize(Buffer) - sizeof(CHAR16);\r
+ Status = FileHandleWrite(Handle, &Size, Buffer);\r
+ if (EFI_ERROR(Status)) {\r
+ return (Status);\r
+ }\r
+ Size = StrSize(L"\r\n") - sizeof(CHAR16);\r
+ Status = FileHandleWrite(Handle, &Size, L"\r\n");\r
+ }\r
+ \r
+ if (AsciiBuffer != NULL) {\r
+ FreePool (AsciiBuffer);\r
+ }\r
+ return Status;\r
}\r
\r
/**\r