e1f414b6 |
1 | /** @file\r |
2 | Print Library worker functions.\r |
3 | \r |
4 | Copyright (c) 2006 - 2007, Intel Corporation<BR>\r |
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 |
22 | GLOBAL_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 |
37 | @return Number of characters printed.\r |
38 | \r |
39 | **/\r |
40 | CHAR8 *\r |
41 | BasePrintLibFillBuffer (\r |
42 | CHAR8 *Buffer,\r |
43 | CHAR8 *EndBuffer,\r |
44 | INTN Length,\r |
45 | UINTN Character,\r |
46 | INTN Increment\r |
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 |
71 | UINTN\r |
72 | EFIAPI\r |
73 | BasePrintLibValueToString (\r |
74 | IN OUT CHAR8 *Buffer,\r |
75 | IN INT64 Value,\r |
76 | IN UINTN Radix\r |
77 | )\r |
78 | {\r |
79 | UINTN Digits;\r |
80 | UINT32 Remainder;\r |
81 | \r |
82 | //\r |
83 | // Loop to convert one digit at a time in reverse order\r |
84 | //\r |
85 | *(Buffer++) = 0;\r |
86 | Digits = 0;\r |
87 | do {\r |
88 | Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r |
89 | *(Buffer++) = mHexStr[Remainder];\r |
90 | Digits++;\r |
91 | } while (Value != 0);\r |
92 | return Digits;\r |
93 | }\r |
94 | \r |
95 | /**\r |
96 | Internal function that converts a decimal value to a Null-terminated string.\r |
97 | \r |
98 | Converts the decimal number specified by Value to a Null-terminated \r |
99 | string specified by Buffer containing at most Width characters.\r |
100 | If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r |
101 | The number of characters in Buffer is returned not including the Null-terminator.\r |
102 | If the conversion contains more than Width characters, then only the first\r |
103 | Width characters are returned, and the total number of characters \r |
104 | required to perform the conversion is returned.\r |
105 | Additional conversion parameters are specified in Flags. \r |
106 | The Flags bit LEFT_JUSTIFY is always ignored.\r |
107 | All conversions are left justified in Buffer.\r |
108 | If Width is 0, PREFIX_ZERO is ignored in Flags.\r |
109 | If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r |
110 | are inserted every 3rd digit starting from the right.\r |
111 | If HEX_RADIX is set in Flags, then the output buffer will be formatted in hexadecimal format.\r |
112 | If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.\r |
113 | If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r |
114 | then Buffer is padded with '0' characters so the combination of the optional '-' \r |
115 | sign character, '0' characters, digit characters for Value, and the Null-terminator\r |
116 | add up to Width characters.\r |
117 | If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().\r |
118 | \r |
119 | If Buffer is NULL, then ASSERT().\r |
120 | If unsupported bits are set in Flags, then ASSERT().\r |
121 | If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r |
122 | \r |
123 | @param Buffer Pointer to the output buffer for the produced Null-terminated\r |
124 | string.\r |
125 | @param Flags The bitmask of flags that specify left justification, zero pad,\r |
126 | and commas.\r |
127 | @param Value The 64-bit signed value to convert to a string.\r |
128 | @param Width The maximum number of characters to place in Buffer, not including\r |
129 | the Null-terminator.\r |
130 | @param Increment Character increment in Buffer.\r |
131 | \r |
132 | @return The number of characters in Buffer not including the Null-terminator.\r |
133 | \r |
134 | **/\r |
135 | UINTN\r |
136 | BasePrintLibConvertValueToString (\r |
137 | IN OUT CHAR8 *Buffer,\r |
138 | IN UINTN Flags,\r |
139 | IN INT64 Value,\r |
140 | IN UINTN Width,\r |
141 | IN UINTN Increment\r |
142 | )\r |
143 | {\r |
144 | CHAR8 *OriginalBuffer;\r |
145 | CHAR8 *EndBuffer;\r |
146 | CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r |
147 | UINTN Count;\r |
148 | UINTN Digits;\r |
149 | UINTN Index;\r |
150 | UINTN Radix;\r |
151 | \r |
152 | ASSERT (Buffer != NULL);\r |
153 | ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);\r |
154 | //\r |
155 | // Make sure Flags can only contain supported bits.\r |
156 | //\r |
157 | ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);\r |
158 | \r |
159 | //\r |
160 | // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT ()\r |
161 | //\r |
162 | ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE);\r |
163 | \r |
164 | OriginalBuffer = Buffer;\r |
165 | \r |
166 | if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r |
167 | Flags &= (~PREFIX_ZERO);\r |
168 | }\r |
169 | \r |
170 | if (Width == 0) {\r |
171 | Width = MAXIMUM_VALUE_CHARACTERS - 1;\r |
172 | }\r |
173 | //\r |
174 | // Set the tag for the end of the input Buffer.\r |
175 | //\r |
176 | EndBuffer = Buffer + Width * Increment;\r |
177 | \r |
178 | if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r |
179 | Value = -Value;\r |
180 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r |
181 | Width--;\r |
182 | }\r |
183 | \r |
184 | Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r |
185 | Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r |
186 | \r |
187 | if ((Flags & PREFIX_ZERO) != 0) {\r |
188 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r |
189 | }\r |
190 | \r |
191 | Digits = Count % 3;\r |
192 | if (Digits != 0) {\r |
193 | Digits = 3 - Digits;\r |
194 | }\r |
195 | for (Index = 0; Index < Count; Index++) {\r |
196 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);\r |
197 | if ((Flags & COMMA_TYPE) != 0) {\r |
198 | Digits++;\r |
199 | if (Digits == 3) {\r |
200 | Digits = 0;\r |
201 | if ((Index + 1) < Count) {\r |
202 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r |
203 | }\r |
204 | }\r |
205 | }\r |
206 | }\r |
207 | \r |
208 | BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r |
209 | \r |
210 | return ((Buffer - OriginalBuffer) / Increment);\r |
211 | }\r |
212 | \r |