]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg\Library\UefiFileHandleLib: Make FileHandleWriteLine support both ASCII and...
authorQiu Shumin <shumin.qiu@intel.com>
Wed, 24 Jun 2015 08:09:05 +0000 (08:09 +0000)
committershenshushi <shenshushi@Edk2>
Wed, 24 Jun 2015 08:09:05 +0000 (08:09 +0000)
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

MdePkg/Include/Library/FileHandleLib.h
MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c

index bfcf8a4ee3f997169780d471f9d05eace5988e27..b5ac19ac85de4506a010d659b42889074aee524a 100644 (file)
@@ -433,7 +433,13 @@ FileHandleReturnLine(
   );\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
@@ -442,6 +448,8 @@ FileHandleReturnLine(
   @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
index 96f16cad53aac1ec4e53a1a6060b8b773e1b75bd..f6cbfada7df513dfbdd57f34eaf307d6582cf626 100644 (file)
@@ -1027,7 +1027,13 @@ FileHandleReadLine(
 }\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
@@ -1036,6 +1042,8 @@ FileHandleReadLine(
   @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
@@ -1046,8 +1054,14 @@ FileHandleWriteLine(
   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
@@ -1056,14 +1070,79 @@ FileHandleWriteLine(
   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