Apply GetBestLanguage() API in UEFI to display HII string.
[edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiLanguage.c
blob3aef7126e03812a3ab73e753dfedc53a3c432e44
1 /** @file
2 Language related HII Library implementation.
4 Copyright (c) 2006 - 2008, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
16 #include "InternalHiiLib.h"
18 /**
19 Get next language from language code list (with separator ';').
21 If LangCode is NULL, then ASSERT.
22 If Lang is NULL, then ASSERT.
24 @param LangCode On input: point to first language in the list. On
25 output: point to next language in the list, or
26 NULL if no more language in the list.
27 @param Lang The first language in the list.
29 **/
30 VOID
31 EFIAPI
32 HiiLibGetNextLanguage (
33 IN OUT CHAR8 **LangCode,
34 OUT CHAR8 *Lang
37 UINTN Index;
38 CHAR8 *StringPtr;
40 ASSERT (LangCode != NULL);
41 ASSERT (*LangCode != NULL);
42 ASSERT (Lang != NULL);
44 Index = 0;
45 StringPtr = *LangCode;
46 while (StringPtr[Index] != 0 && StringPtr[Index] != ';') {
47 Index++;
50 CopyMem (Lang, StringPtr, Index);
51 Lang[Index] = 0;
53 if (StringPtr[Index] == ';') {
54 Index++;
56 *LangCode = StringPtr + Index;
60 /**
61 This function returns the list of supported languages, in the format specified
62 in UEFI specification Appendix M.
64 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
66 @param HiiHandle The HII package list handle.
68 @retval !NULL The supported languages.
69 @retval NULL If Supported Languages can not be retrived.
71 **/
72 CHAR8 *
73 EFIAPI
74 HiiLibGetSupportedLanguages (
75 IN EFI_HII_HANDLE HiiHandle
78 EFI_STATUS Status;
79 UINTN BufferSize;
80 CHAR8 *LanguageString;
82 ASSERT (IsHiiHandleRegistered (HiiHandle));
84 // Collect current supported Languages for given HII handle
85 // First try allocate 4K buffer to store the current supported languages.
87 BufferSize = 0x1000;
88 LanguageString = AllocateZeroPool (BufferSize);
89 if (LanguageString == NULL) {
90 return NULL;
93 Status = mHiiStringProt->GetLanguages (mHiiStringProt, HiiHandle, LanguageString, &BufferSize);
95 if (Status == EFI_BUFFER_TOO_SMALL) {
96 FreePool (LanguageString);
97 LanguageString = AllocateZeroPool (BufferSize);
98 if (LanguageString == NULL) {
99 return NULL;
102 Status = mHiiStringProt->GetLanguages (mHiiStringProt, HiiHandle, LanguageString, &BufferSize);
105 if (EFI_ERROR (Status)) {
106 LanguageString = NULL;
109 return LanguageString;
114 This function returns the number of supported languages on HiiHandle.
116 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
117 If not enough resource to complete the operation, then ASSERT.
119 @param HiiHandle The HII package list handle.
121 @return The number of supported languages.
124 UINT16
125 EFIAPI
126 HiiLibGetSupportedLanguageNumber (
127 IN EFI_HII_HANDLE HiiHandle
130 CHAR8 *Languages;
131 CHAR8 *LanguageString;
132 UINT16 LangNumber;
133 CHAR8 *Lang;
135 Languages = HiiLibGetSupportedLanguages (HiiHandle);
136 if (Languages == NULL) {
137 return 0;
140 LangNumber = 0;
141 Lang = AllocatePool (AsciiStrSize (Languages));
142 if (Lang != NULL) {
143 LanguageString = Languages;
144 while (*LanguageString != 0) {
145 HiiLibGetNextLanguage (&LanguageString, Lang);
146 LangNumber++;
149 FreePool (Lang);
151 FreePool (Languages);
153 return LangNumber;
157 This function returns the list of supported 2nd languages, in the format specified
158 in UEFI specification Appendix M.
160 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
161 If not enough resource to complete the operation, then ASSERT.
163 @param HiiHandle The HII package list handle.
164 @param FirstLanguage Pointer to language name buffer.
166 @return The supported languages.
169 CHAR8 *
170 EFIAPI
171 HiiLibGetSupportedSecondaryLanguages (
172 IN EFI_HII_HANDLE HiiHandle,
173 IN CONST CHAR8 *FirstLanguage
176 EFI_STATUS Status;
177 UINTN BufferSize;
178 CHAR8 *LanguageString;
180 ASSERT (HiiHandle != NULL);
181 ASSERT (IsHiiHandleRegistered (HiiHandle));
183 // Collect current supported 2nd Languages for given HII handle
184 // First try allocate 4K buffer to store the current supported 2nd languages.
186 BufferSize = 0x1000;
187 LanguageString = AllocateZeroPool (BufferSize);
188 if (LanguageString == NULL) {
189 return NULL;
192 Status = mHiiStringProt->GetSecondaryLanguages (mHiiStringProt, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
194 if (Status == EFI_BUFFER_TOO_SMALL) {
195 FreePool (LanguageString);
196 LanguageString = AllocateZeroPool (BufferSize);
197 if (LanguageString == NULL) {
198 return NULL;
201 Status = mHiiStringProt->GetSecondaryLanguages (mHiiStringProt, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
204 if (EFI_ERROR (Status)) {
205 LanguageString = NULL;
208 return LanguageString;