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