]> git.proxmox.com Git - mirror_edk2.git/blobdiff - FatPkg/EnhancedFatDxe/UnicodeCollation.c
Apply GetBestLanguage() UefiLib to initialize Unicode Collation Protocol.
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / UnicodeCollation.c
index 9e5db7b96714abc2d80a6db95bdd03ebc4918f71..574db9783e3518cb6864b91ef14347c78b3d7216 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
-  Unicode Collation Library that hides the trival difference of Unicode Collation\r
+  Unicode Collation Support component that hides the trivial difference of Unicode Collation\r
   and Unicode collation 2 Protocol.\r
 \r
-  Copyright (c) 2007, Intel Corporation<BR>\r
+  Copyright (c) 2007 - 2009, Intel Corporation<BR>\r
   All rights reserved. This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 \r
 #include "Fat.h"\r
 \r
-STATIC EFI_UNICODE_COLLATION_PROTOCOL  *mUnicodeCollationInterface = NULL;\r
+EFI_UNICODE_COLLATION_PROTOCOL  *mUnicodeCollationInterface = NULL;\r
 \r
-typedef\r
-BOOLEAN\r
-(* SEARCH_LANG_CODE) (\r
-  IN CONST CHAR8    *Languages,\r
-  IN CONST CHAR8    *MatchLangCode\r
-  );\r
-\r
-struct _UNICODE_INTERFACE {\r
-  CHAR16            *VariableName;\r
-  CHAR8             *DefaultLangCode;\r
-  SEARCH_LANG_CODE  SearchLangCode;\r
-  EFI_GUID          *UnicodeProtocolGuid;\r
-};\r
-\r
-typedef struct _UNICODE_INTERFACE UNICODE_INTERFACE;\r
-\r
-STATIC\r
-BOOLEAN\r
-SearchIso639LangCode (\r
-  IN CONST CHAR8    *Languages,\r
-  IN CONST CHAR8    *MatchLangCode\r
-  )\r
-{\r
-  CONST CHAR8       *LangCode;\r
-\r
-  for (LangCode = Languages; *LangCode != '\0'; LangCode += 3) {\r
-    if (CompareMem (LangCode, MatchLangCode, 3) == 0) {\r
-      return TRUE;\r
-    }\r
-  }\r
-\r
-  return FALSE;\r
-}\r
-\r
-STATIC\r
-BOOLEAN\r
-SearchRfc3066LangCode (\r
-  IN CONST CHAR8    *Languages,\r
-  IN CONST CHAR8    *MatchLangCode\r
-  )\r
-{\r
-  CHAR8       *SubStr;\r
-  CHAR8       Terminal;\r
-\r
-  SubStr = AsciiStrStr (Languages, MatchLangCode);\r
-  if (SubStr == NULL) {\r
-    return FALSE;\r
-  }\r
+/**\r
+  Worker function to initialize Unicode Collation support.\r
 \r
-  if (SubStr != Languages && *(SubStr - 1) != ';') {\r
-    return FALSE;\r
-  }\r
+  This function searches Initialized Unicode Collation support based on PCDs:\r
+  PcdUnicodeCollation2Support and PcdUnicodeCollationSupport.\r
+  It first tries to locate Unicode Collation 2 protocol and matches it with current\r
+  platform language code. If for any reason the first attempt fails, it then tries to\r
+  use Unicode Collation Protocol.\r
 \r
-  Terminal = *(SubStr + AsciiStrLen (MatchLangCode));\r
-  if (Terminal != '\0' && Terminal != ';') {\r
-    return FALSE;\r
-  }\r
+  @param  AgentHandle          The handle used to open Unicode Collation (2) protocol.\r
+  @param  ProtocolGuid         The pointer to Unicode Collation (2) protocol GUID.\r
+  @param  VariableName         The name of the RFC 4646 or ISO 639-2 language variable.\r
+  @param  DefaultLanguage      The default language in case the RFC 4646 or ISO 639-2 language is absent.\r
 \r
-  return TRUE;\r
-}\r
+  @retval EFI_SUCCESS          The Unicode Collation (2) protocol has been successfully located.\r
+  @retval Others               The Unicode Collation (2) protocol has not been located.\r
 \r
-GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mIso639Lang = {\r
-  L"Lang",\r
-  (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang),\r
-  SearchIso639LangCode,\r
-  &gEfiUnicodeCollationProtocolGuid,\r
-};\r
-\r
-GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mRfc3066Lang = {\r
-  L"PlatformLang",\r
-  (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang),\r
-  SearchRfc3066LangCode,\r
-  &gEfiUnicodeCollation2ProtocolGuid,\r
-};\r
-\r
-STATIC\r
+**/\r
 EFI_STATUS\r
-InitializeUnicodeCollationSupportWithConfig (\r
+InitializeUnicodeCollationSupportWorker (\r
   IN EFI_HANDLE         AgentHandle,\r
-  IN UNICODE_INTERFACE  *UnicodeInterface\r
+  IN EFI_GUID           *ProtocolGuid,\r
+  IN CONST CHAR16       *VariableName,\r
+  IN CONST CHAR8        *DefaultLanguage\r
   )\r
 {\r
   EFI_STATUS                      Status;\r
-  CHAR8                           Buffer[100];\r
-  UINTN                           BufferSize;\r
+  UINTN                           NumHandles;\r
   UINTN                           Index;\r
-  CHAR8                           *LangCode;\r
-  UINTN                           NoHandles;\r
   EFI_HANDLE                      *Handles;\r
   EFI_UNICODE_COLLATION_PROTOCOL  *Uci;\r
-\r
-  LangCode   = Buffer;\r
-  BufferSize = sizeof (Buffer);\r
-  Status = gRT->GetVariable (\r
-                  UnicodeInterface->VariableName,\r
-                  &gEfiGlobalVariableGuid,\r
-                  NULL,\r
-                  &BufferSize,\r
-                  Buffer\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    LangCode = UnicodeInterface->DefaultLangCode;\r
-  }\r
+  BOOLEAN                         Iso639Language;\r
+  CHAR8                           *Language;\r
+  CHAR8                           *BestLanguage;\r
 \r
   Status = gBS->LocateHandleBuffer (\r
                   ByProtocol,\r
-                  UnicodeInterface->UnicodeProtocolGuid,\r
+                  ProtocolGuid,\r
                   NULL,\r
-                  &NoHandles,\r
+                  &NumHandles,\r
                   &Handles\r
                   );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  for (Index = 0; Index < NoHandles; Index++) {\r
+  Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);\r
+  Language = GetEfiGlobalVariable(VariableName);\r
+\r
+  Status = EFI_UNSUPPORTED;\r
+  for (Index = 0; Index < NumHandles; Index++) {\r
     //\r
     // Open Unicode Collation Protocol\r
     //\r
     Status = gBS->OpenProtocol (\r
                     Handles[Index],\r
-                    UnicodeInterface->UnicodeProtocolGuid,\r
+                    ProtocolGuid,\r
                     (VOID **) &Uci,\r
                     AgentHandle,\r
                     NULL,\r
@@ -148,15 +83,42 @@ InitializeUnicodeCollationSupportWithConfig (
       continue;\r
     }\r
 \r
-    if (UnicodeInterface->SearchLangCode (Uci->SupportedLanguages, LangCode)) {\r
+    //\r
+    // Find the best matching matching language from the supported languages\r
+    // of Unicode Collation (2) protocol. \r
+    //\r
+    if (Language == NULL) {\r
+      BestLanguage = GetBestLanguage (\r
+                       Uci->SupportedLanguages,\r
+                       Iso639Language,\r
+                       DefaultLanguage,\r
+                       NULL\r
+                       );\r
+    } else {\r
+      BestLanguage = GetBestLanguage (\r
+                       Uci->SupportedLanguages,\r
+                       Iso639Language,\r
+                       Language,\r
+                       Iso639Language,\r
+                       DefaultLanguage,\r
+                       NULL\r
+                       );\r
+    }\r
+    if (BestLanguage != NULL) {\r
+      FreePool (BestLanguage);\r
       mUnicodeCollationInterface = Uci;\r
+      Status = EFI_SUCCESS;\r
       break;\r
     }\r
   }\r
 \r
+  if (Language != NULL) {\r
+    FreePool (Language);\r
+  }\r
+\r
   FreePool (Handles);\r
 \r
-  return (mUnicodeCollationInterface != NULL)? EFI_SUCCESS : EFI_NOT_FOUND;\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -185,10 +147,15 @@ InitializeUnicodeCollationSupport (
   Status = EFI_UNSUPPORTED;\r
 \r
   //\r
-  // First try to use RFC 3066 Unicode Collation 2 Protocol.\r
+  // First try to use RFC 4646 Unicode Collation 2 Protocol.\r
   //\r
   if (FeaturePcdGet (PcdUnicodeCollation2Support)) {\r
-    Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mRfc3066Lang);\r
+    Status = InitializeUnicodeCollationSupportWorker (\r
+               AgentHandle,\r
+               &gEfiUnicodeCollation2ProtocolGuid,\r
+               L"PlatformLang",\r
+               (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang)\r
+               );\r
   }\r
 \r
   //\r
@@ -196,12 +163,18 @@ InitializeUnicodeCollationSupport (
   // on the ISO 639-2 Unicode Collation Protocol.\r
   //\r
   if (FeaturePcdGet (PcdUnicodeCollationSupport) && EFI_ERROR (Status)) {\r
-    Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mIso639Lang);\r
+    Status = InitializeUnicodeCollationSupportWorker (\r
+               AgentHandle,\r
+               &gEfiUnicodeCollationProtocolGuid,\r
+               L"Lang",\r
+               (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang)\r
+               );\r
   }\r
 \r
   return Status;\r
 }\r
 \r
+\r
 /**\r
   Performs a case-insensitive comparison of two Null-terminated Unicode strings.\r
 \r