]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/SetupBrowserDxe/Print.c
Add comments to MdeModulePkg.dec, Correct minor comments for other files and Add...
[mirror_edk2.git] / MdeModulePkg / Universal / SetupBrowserDxe / Print.c
1 /** @file
2 Basic Ascii AvSPrintf() function named VSPrint(). VSPrint() enables very
3 simple implemenation of SPrint() and Print() to support debug.
4
5 You can not Print more than EFI_DRIVER_LIB_MAX_PRINT_BUFFER characters at a
6 time. This makes the implementation very simple.
7
8 VSPrint, Print, SPrint format specification has the follwoing form
9
10 %type
11
12 type:
13 'S','s' - argument is an Unicode string
14 'c' - argument is an ascii character
15 '%' - Print a %
16
17
18 Copyright (c) 2004 - 2007, Intel Corporation
19 All rights reserved. This program and the accompanying materials
20 are licensed and made available under the terms and conditions of the BSD License
21 which accompanies this distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php
23
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26
27 **/
28
29 #include "Setup.h"
30
31 /**
32 VSPrint worker function that prints a Value as a decimal number in Buffer.
33
34 @param Buffer Location to place ascii decimal number string of Value.
35 @param Flags Flags to use in printing decimal string, see file header for
36 details.
37 @param Value Decimal value to convert to a string in Buffer.
38
39 @return Number of characters printed.
40
41 **/
42 UINTN
43 ValueToString (
44 IN OUT CHAR16 *Buffer,
45 IN BOOLEAN Flags,
46 IN INT64 Value
47 );
48
49 /**
50 The internal function prints to the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
51 protocol instance.
52
53 @param Column The position of the output string.
54 @param Row The position of the output string.
55 @param Out The EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
56 @param Fmt The format string.
57 @param Args The additional argument for the variables in the format string.
58
59 @return Number of Unicode character printed.
60
61 **/
62 UINTN
63 PrintInternal (
64 IN UINTN Column,
65 IN UINTN Row,
66 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Out,
67 IN CHAR16 *Fmt,
68 IN VA_LIST Args
69 )
70 {
71 CHAR16 *Buffer;
72 CHAR16 *BackupBuffer;
73 UINTN Index;
74 UINTN PreviousIndex;
75 UINTN Count;
76
77 //
78 // For now, allocate an arbitrarily long buffer
79 //
80 Buffer = AllocateZeroPool (0x10000);
81 BackupBuffer = AllocateZeroPool (0x10000);
82 ASSERT (Buffer);
83 ASSERT (BackupBuffer);
84
85 if (Column != (UINTN) -1) {
86 Out->SetCursorPosition (Out, Column, Row);
87 }
88
89 UnicodeVSPrint (Buffer, 0x10000, Fmt, Args);
90
91 Out->Mode->Attribute = Out->Mode->Attribute & 0x7f;
92
93 Out->SetAttribute (Out, Out->Mode->Attribute);
94
95 Index = 0;
96 PreviousIndex = 0;
97 Count = 0;
98
99 do {
100 for (; (Buffer[Index] != NARROW_CHAR) && (Buffer[Index] != WIDE_CHAR) && (Buffer[Index] != 0); Index++) {
101 BackupBuffer[Index] = Buffer[Index];
102 }
103
104 if (Buffer[Index] == 0) {
105 break;
106 }
107 //
108 // Null-terminate the temporary string
109 //
110 BackupBuffer[Index] = 0;
111
112 //
113 // Print this out, we are about to switch widths
114 //
115 Out->OutputString (Out, &BackupBuffer[PreviousIndex]);
116 Count += StrLen (&BackupBuffer[PreviousIndex]);
117
118 //
119 // Preserve the current index + 1, since this is where we will start printing from next
120 //
121 PreviousIndex = Index + 1;
122
123 //
124 // We are at a narrow or wide character directive. Set attributes and strip it and print it
125 //
126 if (Buffer[Index] == NARROW_CHAR) {
127 //
128 // Preserve bits 0 - 6 and zero out the rest
129 //
130 Out->Mode->Attribute = Out->Mode->Attribute & 0x7f;
131 Out->SetAttribute (Out, Out->Mode->Attribute);
132 } else {
133 //
134 // Must be wide, set bit 7 ON
135 //
136 Out->Mode->Attribute = Out->Mode->Attribute | EFI_WIDE_ATTRIBUTE;
137 Out->SetAttribute (Out, Out->Mode->Attribute);
138 }
139
140 Index++;
141
142 } while (Buffer[Index] != 0);
143
144 //
145 // We hit the end of the string - print it
146 //
147 Out->OutputString (Out, &BackupBuffer[PreviousIndex]);
148 Count += StrLen (&BackupBuffer[PreviousIndex]);
149
150 gBS->FreePool (Buffer);
151 gBS->FreePool (BackupBuffer);
152 return Count;
153 }
154
155
156 /**
157 Prints a formatted unicode string to the default console.
158
159 @param Fmt Format string
160 @param ... Variable argument list for format string.
161
162 @return Length of string printed to the console.
163
164 **/
165 UINTN
166 ConsolePrint (
167 IN CHAR16 *Fmt,
168 IN ...
169 )
170 {
171 VA_LIST Args;
172
173 VA_START (Args, Fmt);
174 return PrintInternal ((UINTN) -1, (UINTN) -1, gST->ConOut, Fmt, Args);
175 }
176
177
178 /**
179 Prints a unicode string to the default console,
180 using L"%s" format.
181
182 @param String String pointer.
183
184 @return Length of string printed to the console
185
186 **/
187 UINTN
188 PrintString (
189 IN CHAR16 *String
190 )
191 {
192 return ConsolePrint (L"%s", String);
193 }
194
195
196 /**
197 Prints a chracter to the default console,
198 using L"%c" format.
199
200 @param Character Character to print.
201
202 @return Length of string printed to the console.
203
204 **/
205 UINTN
206 PrintChar (
207 CHAR16 Character
208 )
209 {
210 return ConsolePrint (L"%c", Character);
211 }
212
213
214 /**
215 Prints a formatted unicode string to the default console, at
216 the supplied cursor position.
217
218 @param Column The cursor position to print the string at.
219 @param Row The cursor position to print the string at.
220 @param Fmt Format string.
221 @param ... Variable argument list for format string.
222
223 @return Length of string printed to the console
224
225 **/
226 UINTN
227 PrintAt (
228 IN UINTN Column,
229 IN UINTN Row,
230 IN CHAR16 *Fmt,
231 ...
232 )
233 {
234 VA_LIST Args;
235
236 VA_START (Args, Fmt);
237 return PrintInternal (Column, Row, gST->ConOut, Fmt, Args);
238 }
239
240
241 /**
242 Prints a unicode string to the default console, at
243 the supplied cursor position, using L"%s" format.
244
245 @param Column The cursor position to print the string at.
246 @param Row The cursor position to print the string at
247 @param String String pointer.
248
249 @return Length of string printed to the console
250
251 **/
252 UINTN
253 PrintStringAt (
254 IN UINTN Column,
255 IN UINTN Row,
256 CHAR16 *String
257 )
258 {
259 return PrintAt (Column, Row, L"%s", String);
260 }
261
262
263 /**
264 Prints a chracter to the default console, at
265 the supplied cursor position, using L"%c" format.
266
267 @param Column The cursor position to print the string at.
268 @param Row The cursor position to print the string at.
269 @param Character Character to print.
270
271 @return Length of string printed to the console.
272
273 **/
274 UINTN
275 PrintCharAt (
276 IN UINTN Column,
277 IN UINTN Row,
278 CHAR16 Character
279 )
280 {
281 return PrintAt (Column, Row, L"%c", Character);
282 }
283
284
285 /**
286 VSPrint worker function that prints a Value as a decimal number in Buffer.
287
288 @param Buffer Location to place ascii decimal number string of Value.
289 @param Flags Flags to use in printing decimal string, see file header for
290 details.
291 @param Value Decimal value to convert to a string in Buffer.
292
293 @return Number of characters printed.
294
295 **/
296 UINTN
297 ValueToString (
298 IN OUT CHAR16 *Buffer,
299 IN BOOLEAN Flags,
300 IN INT64 Value
301 )
302 {
303 CHAR16 TempBuffer[30];
304 CHAR16 *TempStr;
305 CHAR16 *BufferPtr;
306 UINTN Count;
307 UINTN NumberCount;
308 UINT32 Remainder;
309 BOOLEAN Negative;
310
311 Negative = FALSE;
312 TempStr = TempBuffer;
313 BufferPtr = Buffer;
314 Count = 0;
315 NumberCount = 0;
316
317 if (Value < 0) {
318 Negative = TRUE;
319 Value = -Value;
320 }
321
322 do {
323 Value = (INT64) DivU64x32Remainder ((UINT64) Value, 10, &Remainder);
324 *(TempStr++) = (CHAR16) (Remainder + '0');
325 Count++;
326 NumberCount++;
327 if ((Flags & COMMA_TYPE) == COMMA_TYPE) {
328 if (NumberCount % 3 == 0 && Value != 0) {
329 *(TempStr++) = ',';
330 Count++;
331 }
332 }
333 } while (Value != 0);
334
335 if (Negative) {
336 *(BufferPtr++) = '-';
337 Count++;
338 }
339
340 //
341 // Reverse temp string into Buffer.
342 //
343 while (TempStr != TempBuffer) {
344 *(BufferPtr++) = *(--TempStr);
345 }
346
347 *BufferPtr = 0;
348 return Count;
349 }