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