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