3 Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
4 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
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.
19 Print Library worker functions.
23 #include "PrintLibInternal.h"
25 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
29 Internal function that places the character into the Buffer.
31 Internal function that places ASCII or Unicode character into the Buffer.
33 @param Buffer Buffer to place the Unicode or ASCII string.
34 @param EndBuffer The end of the input Buffer. No characters will be
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.
40 @return Number of characters printed.
44 BasePrintLibFillBuffer (
54 for (Index
= 0; Index
< Length
&& Buffer
< EndBuffer
; Index
++) {
55 *Buffer
= (CHAR8
) Character
;
56 *(Buffer
+ 1) = (CHAR8
) (Character
>> 8);
63 Internal function that convert a decimal number to a string in Buffer.
65 Print worker function that convert a decimal number to a string in Buffer.
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
71 @return Number of characters printed.
76 BasePrintLibValueToString (
86 // Loop to convert one digit at a time in reverse order
91 Value
= (INT64
)DivU64x32Remainder ((UINT64
)Value
, (UINT32
)Radix
, &Remainder
);
92 *(Buffer
++) = mHexStr
[Remainder
];
99 Internal function that converts a decimal value to a Null-terminated string.
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 RADIX_HEX is set in Flags, then the output buffer will be formatted in hexadecimal format.
115 If Value is < 0 and RADIX_HEX 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 RADIX_HEX are set in Flags, then ASSERT().
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()
126 @param Buffer Pointer to the output buffer for the produced Null-terminated
128 @param Flags The bitmask of flags that specify left justification, zero pad,
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
133 @param Increment Character increment in Buffer.
135 @return The number of characters in Buffer not including the Null-terminator.
139 BasePrintLibConvertValueToString (
140 IN OUT CHAR8
*Buffer
,
147 CHAR8
*OriginalBuffer
;
149 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
155 ASSERT (Buffer
!= NULL
);
156 ASSERT (Width
< MAXIMUM_VALUE_CHARACTERS
);
158 // Make sure Flags can only contain supported bits.
160 ASSERT ((Flags
& ~(LEFT_JUSTIFY
| COMMA_TYPE
| PREFIX_ZERO
| RADIX_HEX
)) == 0);
163 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()
165 ASSERT (((Flags
& COMMA_TYPE
) != 0 && (Flags
& RADIX_HEX
) != 0) == FALSE
);
167 OriginalBuffer
= Buffer
;
169 if (Width
== 0 || (Flags
& COMMA_TYPE
) != 0) {
170 Flags
&= (~PREFIX_ZERO
);
174 Width
= MAXIMUM_VALUE_CHARACTERS
- 1;
177 // Set the tag for the end of the input Buffer.
179 EndBuffer
= Buffer
+ Width
* Increment
;
181 if ((Value
< 0) && ((Flags
& RADIX_HEX
) == 0)) {
183 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, '-', Increment
);
187 Radix
= ((Flags
& RADIX_HEX
) == 0)? 10 : 16;
188 Count
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
);
190 if ((Flags
& PREFIX_ZERO
) != 0) {
191 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Count
, '0', Increment
);
198 for (Index
= 0; Index
< Count
; Index
++) {
199 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ValueBuffer
[Count
- Index
], Increment
);
200 if ((Flags
& COMMA_TYPE
) != 0) {
204 if ((Index
+ 1) < Count
) {
205 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', Increment
);
211 BasePrintLibFillBuffer (Buffer
, EndBuffer
+ Increment
, 1, 0, Increment
);
213 return ((Buffer
- OriginalBuffer
) / Increment
);