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