git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@263 6f19259b...
[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
21CHAR8 *\r
22BasePrintLibFillBuffer (\r
23 CHAR8 *Buffer,\r
24 INTN Length,\r
25 UINTN Character,\r
26 INTN Increment\r
27 )\r
28{\r
29 INTN Index;\r
30\r
31 for (Index = 0; Index < Length; Index++) {\r
32 *Buffer = (CHAR8) Character;\r
33 *(Buffer + 1) = (CHAR8) (Character >> 8);\r
34 Buffer += Increment;\r
35 }\r
36 return Buffer;\r
37}\r
38\r
39/**\r
40 Print worker function that prints a Value as a decimal number in Buffer.\r
41\r
42 @param Buffer Location to place the Unicode or ASCII string of Value.\r
43 \r
44 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.\r
45 \r
46 @param Flags Flags to use in printing string, see file header for details.\r
47 \r
48 @param Precision Minimum number of digits to return in the ASCII string\r
49\r
50 @return Number of characters printed.\r
51\r
52**/\r
53UINTN\r
54EFIAPI\r
55BasePrintLibValueToString (\r
56 IN OUT CHAR8 *Buffer,\r
57 IN INT64 Value,\r
58 IN UINTN Radix\r
59 )\r
60{\r
61 UINTN Digits;\r
62 UINT32 Remainder;\r
63\r
64 //\r
65 // Loop to convert one digit at a time in reverse order\r
66 //\r
67 *(Buffer++) = 0;\r
68 Digits = 0;\r
69 do {\r
70 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
71 *(Buffer++) = mHexStr[Remainder];\r
72 Digits++;\r
73 } while (Value != 0);\r
74 return Digits;\r
75}\r
76\r
77UINTN\r
78BasePrintLibConvertValueToString (\r
79 IN OUT CHAR8 *Buffer,\r
80 IN UINTN Flags,\r
81 IN INT64 Value,\r
82 IN UINTN Width,\r
83 IN UINTN Increment\r
84 )\r
85{\r
86 CHAR8 *OriginalBuffer;\r
87 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
88 UINTN Count;\r
89 UINTN Digits;\r
90 UINTN Index;\r
91\r
92 OriginalBuffer = Buffer;\r
93\r
94 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
95 Flags &= (~PREFIX_ZERO);\r
96 }\r
97\r
98 if (Width == 0 || Width > (MAXIMUM_VALUE_CHARACTERS - 1)) {\r
99 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
100 }\r
101\r
102 if (Value < 0) {\r
103 Value = -Value;\r
104 Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment);\r
105 }\r
106\r
107 Count = BasePrintLibValueToString (ValueBuffer, Value, 10);\r
108\r
109 if ((Flags & PREFIX_ZERO) != 0) {\r
110 Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment);\r
111 }\r
112\r
113 Digits = 3 - (Count % 3);\r
114 for (Index = 0; Index < Count; Index++) {\r
115 Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment);\r
116 if ((Flags & COMMA_TYPE) != 0) {\r
117 Digits++;\r
118 if (Digits == 3) {\r
119 Digits = 0;\r
120 if ((Index + 1) < Count) {\r
121 Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment);\r
122 }\r
123 }\r
124 }\r
125 }\r
126\r
127 Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, Increment);\r
128\r
129 return ((Buffer - OriginalBuffer) / Increment);\r
130}\r
131\r
132\r
133UINTN\r
134UnicodeValueToString (\r
135 IN OUT CHAR16 *Buffer,\r
136 IN UINTN Flags,\r
137 IN INT64 Value,\r
138 IN UINTN Width\r
139 )\r
140{\r
141 return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 2);\r
142}\r
143\r
144UINTN\r
145AsciiValueToString (\r
146 IN OUT CHAR8 *Buffer,\r
147 IN UINTN Flags,\r
148 IN INT64 Value,\r
149 IN UINTN Width\r
150 )\r
151{\r
152 return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 1);\r
153}\r