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