]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePrintLib/PrintLibInternal.c
Removed MdePkg usage of ModuleName: in file headers
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
1 /** @file
2 Print Library worker functions.
3
4 Copyright (c) 2006 - 2007, 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 common header file for this module.
17 //
18
19
20 #include "PrintLibInternal.h"
21
22 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
23
24
25 /**
26 Internal function that places the character into the Buffer.
27
28 Internal function that places ASCII or Unicode character into the Buffer.
29
30 @param Buffer Buffer to place the Unicode or ASCII string.
31 @param EndBuffer The end of the input Buffer. No characters will be
32 placed after that.
33 @param Length Count of character to be placed into Buffer.
34 @param Character Character to be placed into Buffer.
35 @param Increment Character increment in Buffer.
36
37 @return Number of characters printed.
38
39 **/
40 CHAR8 *
41 BasePrintLibFillBuffer (
42 CHAR8 *Buffer,
43 CHAR8 *EndBuffer,
44 INTN Length,
45 UINTN Character,
46 INTN Increment
47 )
48 {
49 INTN Index;
50
51 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
52 *Buffer = (CHAR8) Character;
53 *(Buffer + 1) = (CHAR8) (Character >> 8);
54 Buffer += Increment;
55 }
56 return Buffer;
57 }
58
59 /**
60 Internal function that convert a decimal number to a string in Buffer.
61
62 Print worker function that convert a decimal number to a string in Buffer.
63
64 @param Buffer Location to place the Unicode or ASCII string of Value.
65 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.
66 @param Radix Radix of the value
67
68 @return Number of characters printed.
69
70 **/
71 UINTN
72 EFIAPI
73 BasePrintLibValueToString (
74 IN OUT CHAR8 *Buffer,
75 IN INT64 Value,
76 IN UINTN Radix
77 )
78 {
79 UINTN Digits;
80 UINT32 Remainder;
81
82 //
83 // Loop to convert one digit at a time in reverse order
84 //
85 *(Buffer++) = 0;
86 Digits = 0;
87 do {
88 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
89 *(Buffer++) = mHexStr[Remainder];
90 Digits++;
91 } while (Value != 0);
92 return Digits;
93 }
94
95 /**
96 Internal function that converts a decimal value to a Null-terminated string.
97
98 Converts the decimal number specified by Value to a Null-terminated
99 string specified by Buffer containing at most Width characters.
100 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
101 The number of characters in Buffer is returned not including the Null-terminator.
102 If the conversion contains more than Width characters, then only the first
103 Width characters are returned, and the total number of characters
104 required to perform the conversion is returned.
105 Additional conversion parameters are specified in Flags.
106 The Flags bit LEFT_JUSTIFY is always ignored.
107 All conversions are left justified in Buffer.
108 If Width is 0, PREFIX_ZERO is ignored in Flags.
109 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
110 are inserted every 3rd digit starting from the right.
111 If HEX_RADIX is set in Flags, then the output buffer will be formatted in hexadecimal format.
112 If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.
113 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
114 then Buffer is padded with '0' characters so the combination of the optional '-'
115 sign character, '0' characters, digit characters for Value, and the Null-terminator
116 add up to Width characters.
117 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
118
119 If Buffer is NULL, then ASSERT().
120 If unsupported bits are set in Flags, then ASSERT().
121 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
122
123 @param Buffer Pointer to the output buffer for the produced Null-terminated
124 string.
125 @param Flags The bitmask of flags that specify left justification, zero pad,
126 and commas.
127 @param Value The 64-bit signed value to convert to a string.
128 @param Width The maximum number of characters to place in Buffer, not including
129 the Null-terminator.
130 @param Increment Character increment in Buffer.
131
132 @return The number of characters in Buffer not including the Null-terminator.
133
134 **/
135 UINTN
136 BasePrintLibConvertValueToString (
137 IN OUT CHAR8 *Buffer,
138 IN UINTN Flags,
139 IN INT64 Value,
140 IN UINTN Width,
141 IN UINTN Increment
142 )
143 {
144 CHAR8 *OriginalBuffer;
145 CHAR8 *EndBuffer;
146 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
147 UINTN Count;
148 UINTN Digits;
149 UINTN Index;
150 UINTN Radix;
151
152 ASSERT (Buffer != NULL);
153 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);
154 //
155 // Make sure Flags can only contain supported bits.
156 //
157 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);
158
159 //
160 // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT ()
161 //
162 ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE);
163
164 OriginalBuffer = Buffer;
165
166 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {
167 Flags &= (~PREFIX_ZERO);
168 }
169
170 if (Width == 0) {
171 Width = MAXIMUM_VALUE_CHARACTERS - 1;
172 }
173 //
174 // Set the tag for the end of the input Buffer.
175 //
176 EndBuffer = Buffer + Width * Increment;
177
178 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {
179 Value = -Value;
180 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);
181 Width--;
182 }
183
184 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;
185 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);
186
187 if ((Flags & PREFIX_ZERO) != 0) {
188 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);
189 }
190
191 Digits = Count % 3;
192 if (Digits != 0) {
193 Digits = 3 - Digits;
194 }
195 for (Index = 0; Index < Count; Index++) {
196 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);
197 if ((Flags & COMMA_TYPE) != 0) {
198 Digits++;
199 if (Digits == 3) {
200 Digits = 0;
201 if ((Index + 1) < Count) {
202 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);
203 }
204 }
205 }
206 }
207
208 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);
209
210 return ((Buffer - OriginalBuffer) / Increment);
211 }
212