audk/EdkCompatibilityPkg/Foundation/Library/EfiCommonLib/ValueToString.c

215 lines
4.8 KiB
C

/*++
Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
ValueToString.c
Abstract:
Routines changing value to Hex or Dec string
--*/
#include "Tiano.h"
#include "EfiDriverLib.h"
static CHAR16 mHexStr[] = { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7',
L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
UINTN
EfiValueToHexStr (
IN OUT CHAR16 *Buffer,
IN UINT64 Value,
IN UINTN Flags,
IN UINTN Width
)
/*++
Routine Description:
VSPrint worker function that prints a Value as a hex number in Buffer
Arguments:
Buffer - Location to place ascii hex string of Value.
Value - Hex value to convert to a string in Buffer.
Flags - Flags to use in printing Hex string, see file header for details.
Width - Width of hex value.
Returns:
Number of characters printed.
--*/
{
CHAR16 TempBuffer[CHARACTER_NUMBER_FOR_VALUE];
CHAR16 *TempStr;
CHAR16 Prefix;
CHAR16 *BufferPtr;
UINTN Count;
UINTN Index;
TempStr = TempBuffer;
BufferPtr = Buffer;
//
// Count starts at one since we will null terminate. Each iteration of the
// loop picks off one nibble. Oh yea TempStr ends up backwards
//
Count = 0;
if (Width > CHARACTER_NUMBER_FOR_VALUE - 1) {
Width = CHARACTER_NUMBER_FOR_VALUE - 1;
}
do {
Index = ((UINTN)Value & 0xf);
*(TempStr++) = mHexStr[Index];
Value = RShiftU64 (Value, 4);
Count++;
} while (Value != 0);
if (Flags & PREFIX_ZERO) {
Prefix = '0';
} else {
Prefix = ' ';
}
Index = Count;
if (!(Flags & LEFT_JUSTIFY)) {
for (; Index < Width; Index++) {
*(TempStr++) = Prefix;
}
}
//
// Reverse temp string into Buffer.
//
if (Width > 0 && (UINTN) (TempStr - TempBuffer) > Width) {
TempStr = TempBuffer + Width;
}
Index = 0;
while (TempStr != TempBuffer) {
*(BufferPtr++) = *(--TempStr);
Index++;
}
*BufferPtr = 0;
return Index;
}
UINTN
EfiValueToString (
IN OUT CHAR16 *Buffer,
IN INT64 Value,
IN UINTN Flags,
IN UINTN Width
)
/*++
Routine Description:
VSPrint worker function that prints a Value as a decimal number in Buffer
Arguments:
Buffer - Location to place ascii decimal number string of Value.
Value - Decimal value to convert to a string in Buffer.
Flags - Flags to use in printing decimal string, see file header for details.
Width - Width of hex value.
Returns:
Number of characters printed.
--*/
{
CHAR16 TempBuffer[CHARACTER_NUMBER_FOR_VALUE];
CHAR16 *TempStr;
CHAR16 *BufferPtr;
UINTN Count;
UINTN ValueCharNum;
UINTN Remainder;
CHAR16 Prefix;
UINTN Index;
BOOLEAN ValueIsNegative;
TempStr = TempBuffer;
BufferPtr = Buffer;
Count = 0;
ValueCharNum = 0;
ValueIsNegative = FALSE;
Remainder = 0;
if (Width > CHARACTER_NUMBER_FOR_VALUE - 1) {
Width = CHARACTER_NUMBER_FOR_VALUE - 1;
}
if (Value < 0) {
Value = -Value;
ValueIsNegative = TRUE;
}
do {
Value = (INT64)DivU64x32 ((UINT64)Value, 10, &Remainder);
*(TempStr++) = (CHAR16)(Remainder + '0');
ValueCharNum++;
Count++;
if ((Flags & COMMA_TYPE) == COMMA_TYPE) {
if (ValueCharNum % 3 == 0 && Value != 0) {
*(TempStr++) = ',';
Count++;
}
}
} while (Value != 0);
if (ValueIsNegative) {
*(TempStr++) = '-';
Count++;
}
if ((Flags & PREFIX_ZERO) && !ValueIsNegative) {
Prefix = '0';
} else {
Prefix = ' ';
}
Index = Count;
if (!(Flags & LEFT_JUSTIFY)) {
for (; Index < Width; Index++) {
*(TempStr++) = Prefix;
}
}
//
// Reverse temp string into Buffer.
//
if (Width > 0 && (UINTN) (TempStr - TempBuffer) > Width) {
TempStr = TempBuffer + Width;
}
Index = 0;
while (TempStr != TempBuffer) {
*(BufferPtr++) = *(--TempStr);
Index++;
}
*BufferPtr = 0;
return Index;
}