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