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