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