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