summaryrefslogtreecommitdiff
path: root/MdePkg
diff options
context:
space:
mode:
authorQiu Shumin <shumin.qiu@intel.com>2015-06-24 08:09:05 +0000
committershenshushi <shenshushi@Edk2>2015-06-24 08:09:05 +0000
commita86c73cddaaa88bfc65955be21b1d5102e20ee47 (patch)
treea4727891cc38423ad255671dfd8e458971b96c3d /MdePkg
parent8ad8a1fa52a8fdb0a00075f5f870235ef6e48b1b (diff)
downloadedk2-platforms-a86c73cddaaa88bfc65955be21b1d5102e20ee47.tar.xz
MdePkg\Library\UefiFileHandleLib: Make FileHandleWriteLine support both ASCII and UNICODE file.
When the file is a UNICODE file (with UNICODE file tag) write UNICODE text. When the file is an ASCII file write ASCII text. If the file size is zero (without the file tag at the beginning) write ASCII text as default. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Qiu Shumin <shumin.qiu@intel.com> Reviewed-by: Jaben Carsey <jaben.carsey@intel.com> Reviewed-by: Liming Gao <liming.gao@intel.com> git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17701 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'MdePkg')
-rw-r--r--MdePkg/Include/Library/FileHandleLib.h10
-rw-r--r--MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c97
2 files changed, 97 insertions, 10 deletions
diff --git a/MdePkg/Include/Library/FileHandleLib.h b/MdePkg/Include/Library/FileHandleLib.h
index bfcf8a4ee3..b5ac19ac85 100644
--- a/MdePkg/Include/Library/FileHandleLib.h
+++ b/MdePkg/Include/Library/FileHandleLib.h
@@ -433,7 +433,13 @@ FileHandleReturnLine(
);
/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode
+ text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then write
+ ASCII text as default.
@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -442,6 +448,8 @@ FileHandleReturnLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
+ string due to out of resources.
@sa FileHandleWrite
**/
diff --git a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
index 96f16cad53..f6cbfada7d 100644
--- a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
+++ b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
@@ -1027,7 +1027,13 @@ FileHandleReadLine(
}
/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode
+ text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then write
+ ASCII text as default.
@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -1036,6 +1042,8 @@ FileHandleReadLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
+ string due to out of resources.
@sa FileHandleWrite
**/
@@ -1046,8 +1054,14 @@ FileHandleWriteLine(
IN CHAR16 *Buffer
)
{
- EFI_STATUS Status;
- UINTN Size;
+ EFI_STATUS Status;
+ CHAR16 CharBuffer;
+ UINTN Size;
+ UINTN CharSize;
+ UINT64 FileSize;
+ UINT64 OriginalFilePosition;
+ BOOLEAN Ascii;
+ CHAR8 *AsciiBuffer;
if (Buffer == NULL) {
return (EFI_SUCCESS);
@@ -1056,14 +1070,79 @@ FileHandleWriteLine(
if (Handle == NULL) {
return (EFI_INVALID_PARAMETER);
}
-
- Size = StrSize(Buffer) - sizeof(Buffer[0]);
- Status = FileHandleWrite(Handle, &Size, Buffer);
+
+ Ascii = FALSE;
+ AsciiBuffer = NULL;
+
+ Status = FileHandleGetPosition(Handle, &OriginalFilePosition);
if (EFI_ERROR(Status)) {
- return (Status);
+ return Status;
+ }
+
+ Status = FileHandleSetPosition(Handle, 0);
+ if (EFI_ERROR(Status)) {
+ return Status;
}
- Size = StrSize(L"\r\n") - sizeof(CHAR16);
- return FileHandleWrite(Handle, &Size, L"\r\n");
+
+ Status = FileHandleGetSize(Handle, &FileSize);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (FileSize == 0) {
+ Ascii = TRUE;
+ } else {
+ CharSize = sizeof (CHAR16);
+ Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
+ ASSERT_EFI_ERROR (Status);
+ if (CharBuffer == gUnicodeFileTag) {
+ Ascii = FALSE;
+ } else {
+ Ascii = TRUE;
+ }
+ }
+
+ Status = FileHandleSetPosition(Handle, OriginalFilePosition);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (Ascii) {
+ Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);
+ AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);
+ if (AsciiBuffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+ UnicodeStrToAsciiStr (Buffer, AsciiBuffer);
+
+ Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, AsciiBuffer);
+ if (EFI_ERROR(Status)) {
+ FreePool (AsciiBuffer);
+ return (Status);
+ }
+ Size = AsciiStrSize("\r\n") - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, "\r\n");
+ } else {
+ if (OriginalFilePosition == 0) {
+ Status = FileHandleSetPosition (Handle, sizeof(CHAR16));
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+ }
+ Size = StrSize(Buffer) - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, Buffer);
+ if (EFI_ERROR(Status)) {
+ return (Status);
+ }
+ Size = StrSize(L"\r\n") - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, L"\r\n");
+ }
+
+ if (AsciiBuffer != NULL) {
+ FreePool (AsciiBuffer);
+ }
+ return Status;
}
/**