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