]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/TianoTools/String/PrintLibInternal.c
Add Mde String and PrintLibs. Port StrGather to the Mde Unicode implementation. Compi...
[mirror_edk2.git] / Tools / Source / TianoTools / String / PrintLibInternal.c
CommitLineData
d2ec0d9e 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 <Base.h>\r
18#include <UefiBaseTypes.h>\r
19#include <PrintLib.h>\r
20#include <BaseLib.h>\r
21#include <CommonLib.h>\r
22#include "PrintLibInternal.h"\r
23\r
24static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
25\r
26CHAR8 *\r
27BasePrintLibFillBuffer (\r
28 CHAR8 *Buffer,\r
29 INTN Length,\r
30 UINTN Character,\r
31 INTN Increment\r
32 )\r
33{\r
34 INTN Index;\r
35\r
36 for (Index = 0; Index < Length; Index++) {\r
37 *Buffer = (CHAR8) Character;\r
38 *(Buffer + 1) = (CHAR8) (Character >> 8);\r
39 Buffer += Increment;\r
40 }\r
41 return Buffer;\r
42}\r
43\r
44/**\r
45 Print worker function that prints a Value as a decimal number in Buffer.\r
46\r
47 @param Buffer Location to place the Unicode or ASCII string of Value.\r
48 \r
49 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.\r
50 \r
51 @param Flags Flags to use in printing string, see file header for details.\r
52 \r
53 @param Precision Minimum number of digits to return in the ASCII string\r
54\r
55 @return Number of characters printed.\r
56\r
57**/\r
58UINTN\r
59EFIAPI\r
60BasePrintLibValueToString (\r
61 IN OUT CHAR8 *Buffer,\r
62 IN INT64 Value,\r
63 IN UINTN Radix\r
64 )\r
65{\r
66 UINTN Digits;\r
67 UINT32 Remainder;\r
68\r
69 //\r
70 // Loop to convert one digit at a time in reverse order\r
71 //\r
72 *(Buffer++) = 0;\r
73 Digits = 0;\r
74 do {\r
75 // Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
76 Remainder = (UINT64)Value % (UINT32)Radix;\r
77 Value = (UINT64)Value / (UINT32)Radix;\r
78 *(Buffer++) = mHexStr[Remainder];\r
79 Digits++;\r
80 } while (Value != 0);\r
81 return Digits;\r
82}\r
83\r
84UINTN\r
85BasePrintLibConvertValueToString (\r
86 IN OUT CHAR8 *Buffer,\r
87 IN UINTN Flags,\r
88 IN INT64 Value,\r
89 IN UINTN Width,\r
90 IN UINTN Increment\r
91 )\r
92{\r
93 CHAR8 *OriginalBuffer;\r
94 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
95 UINTN Count;\r
96 UINTN Digits;\r
97 UINTN Index;\r
98\r
99 OriginalBuffer = Buffer;\r
100\r
101 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
102 Flags &= (~PREFIX_ZERO);\r
103 }\r
104\r
105 if (Width == 0 || Width > (MAXIMUM_VALUE_CHARACTERS - 1)) {\r
106 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
107 }\r
108\r
109 if (Value < 0) {\r
110 Value = -Value;\r
111 Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment);\r
112 }\r
113\r
114 Count = BasePrintLibValueToString (ValueBuffer, Value, 10);\r
115\r
116 if ((Flags & PREFIX_ZERO) != 0) {\r
117 Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment);\r
118 }\r
119\r
120 Digits = 3 - (Count % 3);\r
121 for (Index = 0; Index < Count; Index++) {\r
122 Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment);\r
123 if ((Flags & COMMA_TYPE) != 0) {\r
124 Digits++;\r
125 if (Digits == 3) {\r
126 Digits = 0;\r
127 if ((Index + 1) < Count) {\r
128 Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment);\r
129 }\r
130 }\r
131 }\r
132 }\r
133\r
134 Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, Increment);\r
135\r
136 return ((Buffer - OriginalBuffer) / Increment);\r
137}\r