]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/HiiLib/HiiLanguage.c
Clean up HiiLib.
[mirror_edk2.git] / MdePkg / Library / HiiLib / HiiLanguage.c
1 /** @file
2 Language related HII Library implementation.
3
4 Copyright (c) 2006, 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
9
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.
12
13 **/
14
15
16 #include "InternalHiiLib.h"
17
18 /**
19 Determine what is the current language setting. The space reserved for Lang
20 must be at least RFC_3066_ENTRY_SIZE bytes;
21
22 If Lang is NULL, then ASSERT.
23
24 @param Lang Pointer of system language. Lang will always be filled with
25 a valid RFC 3066 language string. If "PlatformLang" is not
26 set in the system, the default language specifed by PcdUefiVariableDefaultPlatformLang
27 is returned.
28
29 @return EFI_SUCCESS If the EFI Variable with "PlatformLang" is set and return in Lang.
30 @return EFI_NOT_FOUND If the EFI Variable with "PlatformLang" is not set, but a valid default language is return in Lang.
31
32 **/
33 EFI_STATUS
34 EFIAPI
35 HiiLibGetCurrentLanguage (
36 OUT CHAR8 *Lang
37 )
38 {
39 EFI_STATUS Status;
40 UINTN Size;
41
42 ASSERT (Lang != NULL);
43
44 //
45 // Get current language setting
46 //
47 Size = RFC_3066_ENTRY_SIZE;
48 Status = gRT->GetVariable (
49 L"PlatformLang",
50 &gEfiGlobalVariableGuid,
51 NULL,
52 &Size,
53 Lang
54 );
55
56 if (EFI_ERROR (Status)) {
57 AsciiStrCpy (Lang, (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang));
58 }
59
60 return Status;
61 }
62
63
64 /**
65 Get next language from language code list (with separator ';').
66
67 If LangCode is NULL, then ASSERT.
68 If Lang is NULL, then ASSERT.
69
70 @param LangCode On input: point to first language in the list. On
71 output: point to next language in the list, or
72 NULL if no more language in the list.
73 @param Lang The first language in the list.
74
75 **/
76 VOID
77 EFIAPI
78 HiiLibGetNextLanguage (
79 IN OUT CHAR8 **LangCode,
80 OUT CHAR8 *Lang
81 )
82 {
83 UINTN Index;
84 CHAR8 *StringPtr;
85
86 ASSERT (LangCode != NULL);
87 ASSERT (Lang != NULL);
88
89 Index = 0;
90 StringPtr = *LangCode;
91 while (StringPtr[Index] != 0 && StringPtr[Index] != ';') {
92 Index++;
93 }
94
95 CopyMem (Lang, StringPtr, Index);
96 Lang[Index] = 0;
97
98 if (StringPtr[Index] == ';') {
99 Index++;
100 }
101 *LangCode = StringPtr + Index;
102 }
103
104
105 /**
106 This function returns the list of supported languages, in the format specified
107 in UEFI specification Appendix M.
108
109 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
110
111 @param HiiHandle The HII package list handle.
112
113 @retval !NULL The supported languages.
114 @retval NULL If Supported Languages can not be retrived.
115
116 **/
117 CHAR8 *
118 EFIAPI
119 HiiLibGetSupportedLanguages (
120 IN EFI_HII_HANDLE HiiHandle
121 )
122 {
123 EFI_STATUS Status;
124 UINTN BufferSize;
125 CHAR8 *LanguageString;
126
127 ASSERT (IsHiiHandleRegistered (HiiHandle));
128 //
129 // Collect current supported Languages for given HII handle
130 //
131 BufferSize = 0x1000;
132 LanguageString = AllocateZeroPool (BufferSize);
133 if (LanguageString == NULL) {
134 return NULL;
135 }
136
137 LocateHiiProtocols ();
138
139 Status = mHiiStringProt->GetLanguages (mHiiStringProt, HiiHandle, LanguageString, &BufferSize);
140
141 if (Status == EFI_BUFFER_TOO_SMALL) {
142 FreePool (LanguageString);
143 LanguageString = AllocateZeroPool (BufferSize);
144 if (LanguageString == NULL) {
145 return NULL;
146 }
147
148 Status = mHiiStringProt->GetLanguages (mHiiStringProt, HiiHandle, LanguageString, &BufferSize);
149 }
150
151 if (EFI_ERROR (Status)) {
152 LanguageString = NULL;
153 }
154
155 return LanguageString;
156 }
157
158
159 /**
160 This function returns the number of supported languages on HiiHandle.
161
162 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
163 If not enough resource to complete the operation, then ASSERT.
164
165 @param HiiHandle The HII package list handle.
166
167 @return The number of supported languages.
168
169 **/
170 UINT16
171 EFIAPI
172 HiiLibGetSupportedLanguageNumber (
173 IN EFI_HII_HANDLE HiiHandle
174 )
175 {
176 CHAR8 *Languages;
177 CHAR8 *LanguageString;
178 UINT16 LangNumber;
179 CHAR8 Lang[RFC_3066_ENTRY_SIZE];
180
181 Languages = HiiLibGetSupportedLanguages (HiiHandle);
182 if (Languages == NULL) {
183 return 0;
184 }
185
186 LangNumber = 0;
187 LanguageString = Languages;
188 while (*LanguageString != 0) {
189 HiiLibGetNextLanguage (&LanguageString, Lang);
190 LangNumber++;
191 }
192 FreePool (Languages);
193
194 return LangNumber;
195 }
196
197 /**
198 This function returns the list of supported 2nd languages, in the format specified
199 in UEFI specification Appendix M.
200
201 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
202 If not enough resource to complete the operation, then ASSERT.
203
204 @param HiiHandle The HII package list handle.
205 @param FirstLanguage Pointer to language name buffer.
206
207 @return The supported languages.
208
209 **/
210 CHAR8 *
211 EFIAPI
212 HiiLibGetSupportedSecondaryLanguages (
213 IN EFI_HII_HANDLE HiiHandle,
214 IN CONST CHAR8 *FirstLanguage
215 )
216 {
217 EFI_STATUS Status;
218 UINTN BufferSize;
219 CHAR8 *LanguageString;
220
221 ASSERT (HiiHandle != NULL);
222 ASSERT (IsHiiHandleRegistered (HiiHandle));
223 //
224 // Collect current supported 2nd Languages for given HII handle
225 //
226 BufferSize = 0x1000;
227 LanguageString = AllocateZeroPool (BufferSize);
228 if (LanguageString == NULL) {
229 return NULL;
230 }
231
232 LocateHiiProtocols ();
233
234 Status = mHiiStringProt->GetSecondaryLanguages (mHiiStringProt, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
235
236 if (Status == EFI_BUFFER_TOO_SMALL) {
237 FreePool (LanguageString);
238 LanguageString = AllocateZeroPool (BufferSize);
239 if (LanguageString == NULL) {
240 return NULL;
241 }
242
243 Status = mHiiStringProt->GetSecondaryLanguages (mHiiStringProt, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
244 }
245
246 if (EFI_ERROR (Status)) {
247 LanguageString = NULL;
248 }
249
250 return LanguageString;
251 }
252
253