]>
Commit | Line | Data |
---|---|---|
3eb9473e | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2004 - 2006, Intel Corporation \r | |
4 | All rights reserved. This program and the accompanying materials \r | |
5 | are licensed and made available under the terms and conditions of the BSD License \r | |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | \r | |
13 | Module Name:\r | |
14 | \r | |
15 | PrintLibInternal.c\r | |
16 | \r | |
17 | Abstract: \r | |
18 | \r | |
19 | Print Library worker functions.\r | |
20 | \r | |
21 | --*/\r | |
22 | \r | |
23 | #include "PrintLibInternal.h"\r | |
24 | \r | |
25 | GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r | |
26 | \r | |
27 | \r | |
28 | /**\r | |
29 | Internal function that places the character into the Buffer.\r | |
30 | \r | |
31 | Internal function that places ASCII or Unicode character into the Buffer.\r | |
32 | \r | |
33 | @param Buffer Buffer to place the Unicode or ASCII string.\r | |
34 | @param EndBuffer The end of the input Buffer. No characters will be\r | |
35 | placed after that. \r | |
36 | @param Length Count of character to be placed into Buffer.\r | |
37 | @param Character Character to be placed into Buffer.\r | |
38 | @param Increment Character increment in Buffer.\r | |
39 | \r | |
40 | @return Number of characters printed.\r | |
41 | \r | |
42 | **/\r | |
43 | CHAR8 *\r | |
44 | BasePrintLibFillBuffer (\r | |
45 | CHAR8 *Buffer,\r | |
46 | CHAR8 *EndBuffer,\r | |
47 | INTN Length,\r | |
48 | UINTN Character,\r | |
49 | INTN Increment\r | |
50 | )\r | |
51 | {\r | |
52 | INTN Index;\r | |
53 | \r | |
54 | for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r | |
55 | *Buffer = (CHAR8) Character;\r | |
56 | *(Buffer + 1) = (CHAR8) (Character >> 8);\r | |
57 | Buffer += Increment;\r | |
58 | }\r | |
59 | return Buffer;\r | |
60 | }\r | |
61 | \r | |
62 | /**\r | |
63 | Internal function that convert a decimal number to a string in Buffer.\r | |
64 | \r | |
65 | Print worker function that convert a decimal number to a string in Buffer.\r | |
66 | \r | |
67 | @param Buffer Location to place the Unicode or ASCII string of Value.\r | |
68 | @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.\r | |
69 | @param Radix Radix of the value\r | |
70 | \r | |
71 | @return Number of characters printed.\r | |
72 | \r | |
73 | **/\r | |
74 | UINTN\r | |
75 | EFIAPI\r | |
76 | BasePrintLibValueToString (\r | |
77 | IN OUT CHAR8 *Buffer,\r | |
78 | IN INT64 Value,\r | |
79 | IN UINTN Radix\r | |
80 | )\r | |
81 | {\r | |
82 | UINTN Digits;\r | |
83 | UINT32 Remainder;\r | |
84 | \r | |
85 | //\r | |
86 | // Loop to convert one digit at a time in reverse order\r | |
87 | //\r | |
88 | *(Buffer++) = 0;\r | |
89 | Digits = 0;\r | |
90 | do {\r | |
91 | Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r | |
92 | *(Buffer++) = mHexStr[Remainder];\r | |
93 | Digits++;\r | |
94 | } while (Value != 0);\r | |
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 | |
138 | UINTN\r | |
139 | BasePrintLibConvertValueToString (\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 | |
155 | ASSERT (Buffer != NULL);\r | |
156 | ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);\r | |
157 | //\r | |
158 | // Make sure Flags can only contain supported bits.\r | |
159 | //\r | |
160 | ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);\r | |
161 | \r | |
162 | //\r | |
163 | // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT ()\r | |
164 | //\r | |
165 | ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE);\r | |
166 | \r | |
167 | OriginalBuffer = Buffer;\r | |
168 | \r | |
169 | if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r | |
170 | Flags &= (~PREFIX_ZERO);\r | |
171 | }\r | |
172 | \r | |
173 | if (Width == 0) {\r | |
174 | Width = MAXIMUM_VALUE_CHARACTERS - 1;\r | |
175 | }\r | |
176 | //\r | |
177 | // Set the tag for the end of the input Buffer.\r | |
178 | //\r | |
179 | EndBuffer = Buffer + Width * Increment;\r | |
180 | \r | |
181 | if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r | |
182 | Value = -Value;\r | |
183 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r | |
184 | Width--;\r | |
185 | }\r | |
186 | \r | |
187 | Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r | |
188 | Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r | |
189 | \r | |
190 | if ((Flags & PREFIX_ZERO) != 0) {\r | |
191 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r | |
192 | }\r | |
193 | \r | |
194 | Digits = Count % 3;\r | |
195 | if (Digits != 0) {\r | |
196 | Digits = 3 - Digits;\r | |
197 | }\r | |
198 | for (Index = 0; Index < Count; Index++) {\r | |
199 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);\r | |
200 | if ((Flags & COMMA_TYPE) != 0) {\r | |
201 | Digits++;\r | |
202 | if (Digits == 3) {\r | |
203 | Digits = 0;\r | |
204 | if ((Index + 1) < Count) {\r | |
205 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r | |
206 | }\r | |
207 | }\r | |
208 | }\r | |
209 | }\r | |
210 | \r | |
211 | BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r | |
212 | \r | |
213 | return ((Buffer - OriginalBuffer) / Increment);\r | |
214 | }\r | |
215 | \r |