]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/TianoTools/String/PrintLibInternal.c
Add new OSS License
[mirror_edk2.git] / Tools / Source / TianoTools / String / PrintLibInternal.c
CommitLineData
d2ec0d9e 1/** @file\r
2 Print Library worker functions.\r
3\r
cf171110 4\r
5Copyright (c) 2004-2006 Intel Corporation. All rights reserved\r
6This software and associated documentation (if any) is furnished\r
7under a license and may only be used or copied in accordance\r
8with the terms of the license. Except as permitted by such\r
9license, no part of this software or documentation may be\r
10reproduced, stored in a retrieval system, or transmitted in any\r
11form or by any means without the express written consent of\r
12Intel Corporation.\r
d2ec0d9e 13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17 Module Name: PrintLibInternal.c\r
18\r
19**/\r
20\r
ce53a8c3 21#include <Common/UefiBaseTypes.h>\r
22#include <Library/PrintLib.h>\r
23\r
24#include "CommonLib.h"\r
d2ec0d9e 25#include "PrintLibInternal.h"\r
26\r
27static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
28\r
29CHAR8 *\r
30BasePrintLibFillBuffer (\r
31 CHAR8 *Buffer,\r
32 INTN Length,\r
33 UINTN Character,\r
34 INTN Increment\r
35 )\r
36{\r
37 INTN Index;\r
38\r
39 for (Index = 0; Index < Length; Index++) {\r
40 *Buffer = (CHAR8) Character;\r
41 *(Buffer + 1) = (CHAR8) (Character >> 8);\r
42 Buffer += Increment;\r
43 }\r
44 return Buffer;\r
45}\r
46\r
47/**\r
48 Print worker function that prints a Value as a decimal number in Buffer.\r
49\r
50 @param Buffer Location to place the Unicode or ASCII string of Value.\r
51 \r
52 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.\r
53 \r
54 @param Flags Flags to use in printing string, see file header for details.\r
55 \r
56 @param Precision Minimum number of digits to return in the ASCII string\r
57\r
58 @return Number of characters printed.\r
59\r
60**/\r
61UINTN\r
62EFIAPI\r
63BasePrintLibValueToString (\r
64 IN OUT CHAR8 *Buffer,\r
65 IN INT64 Value,\r
66 IN UINTN Radix\r
67 )\r
68{\r
69 UINTN Digits;\r
70 UINT32 Remainder;\r
71\r
72 //\r
73 // Loop to convert one digit at a time in reverse order\r
74 //\r
75 *(Buffer++) = 0;\r
76 Digits = 0;\r
77 do {\r
78 // Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
79 Remainder = (UINT64)Value % (UINT32)Radix;\r
80 Value = (UINT64)Value / (UINT32)Radix;\r
81 *(Buffer++) = mHexStr[Remainder];\r
82 Digits++;\r
83 } while (Value != 0);\r
84 return Digits;\r
85}\r
86\r
87UINTN\r
88BasePrintLibConvertValueToString (\r
89 IN OUT CHAR8 *Buffer,\r
90 IN UINTN Flags,\r
91 IN INT64 Value,\r
92 IN UINTN Width,\r
93 IN UINTN Increment\r
94 )\r
95{\r
96 CHAR8 *OriginalBuffer;\r
97 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
98 UINTN Count;\r
99 UINTN Digits;\r
100 UINTN Index;\r
101\r
102 OriginalBuffer = Buffer;\r
103\r
104 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
105 Flags &= (~PREFIX_ZERO);\r
106 }\r
107\r
108 if (Width == 0 || Width > (MAXIMUM_VALUE_CHARACTERS - 1)) {\r
109 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
110 }\r
111\r
112 if (Value < 0) {\r
113 Value = -Value;\r
114 Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment);\r
115 }\r
116\r
117 Count = BasePrintLibValueToString (ValueBuffer, Value, 10);\r
118\r
119 if ((Flags & PREFIX_ZERO) != 0) {\r
120 Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment);\r
121 }\r
122\r
123 Digits = 3 - (Count % 3);\r
124 for (Index = 0; Index < Count; Index++) {\r
125 Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment);\r
126 if ((Flags & COMMA_TYPE) != 0) {\r
127 Digits++;\r
128 if (Digits == 3) {\r
129 Digits = 0;\r
130 if ((Index + 1) < Count) {\r
131 Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment);\r
132 }\r
133 }\r
134 }\r
135 }\r
136\r
137 Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, Increment);\r
138\r
139 return ((Buffer - OriginalBuffer) / Increment);\r
140}\r