* fixed PhFormat bugs

* switched some functions to PhFormat

git-svn-id: svn://svn.code.sf.net/p/processhacker/code@3706 21ef857c-d57f-4fe0-8362-d861dc6d29cd
This commit is contained in:
wj32
2010-10-01 06:01:55 +00:00
parent 7eec476c5e
commit 6bf3480337
7 changed files with 196 additions and 159 deletions
+82 -49
View File
@@ -172,7 +172,7 @@ VOID PhUpdateIconCpuHistory()
HICON icon;
HANDLE maxCpuProcessId;
PPH_PROCESS_ITEM maxCpuProcessItem;
PPH_STRING maxCpuText = NULL;
PH_FORMAT format[8];
PPH_STRING text;
// Icon
@@ -199,21 +199,34 @@ VOID PhUpdateIconCpuHistory()
maxCpuProcessId = (HANDLE)PhGetItemCircularBuffer_ULONG(&PhMaxCpuHistory, 0);
if (maxCpuProcessId != NULL)
if (maxCpuProcessId)
maxCpuProcessItem = PhReferenceProcessItem(maxCpuProcessId);
format[0].Type = StringFormatType;
PhInitializeStringRef(&format[0].u.String, L"CPU usage: ");
format[1].Type = DoubleFormatType | FormatUsePrecision;
format[1].Precision = 2;
format[1].u.Double = (PhCpuKernelUsage + PhCpuUserUsage) * 100;
format[2].Type = CharFormatType;
format[2].u.Char = '%';
if (maxCpuProcessItem)
{
if (maxCpuProcessItem = PhReferenceProcessItem(maxCpuProcessId))
{
maxCpuText = PhFormatString(
L"\n%s: %.2f%%",
maxCpuProcessItem->ProcessName->Buffer,
maxCpuProcessItem->CpuUsage * 100
);
PhDereferenceObject(maxCpuProcessItem);
}
format[3].Type = CharFormatType;
format[3].u.Char = '\n';
format[4].Type = StringFormatType;
format[4].u.String = maxCpuProcessItem->ProcessName->sr;
format[5].Type = StringFormatType;
PhInitializeStringRef(&format[5].u.String, L": ");
format[6].Type = DoubleFormatType | FormatUsePrecision;
format[6].Precision = 2;
format[6].u.Double = maxCpuProcessItem->CpuUsage * 100;
format[7].Type = CharFormatType;
format[7].u.Char = '%';
}
text = PhFormatString(L"CPU usage: %.2f%%%s", (PhCpuKernelUsage + PhCpuUserUsage) * 100, PhGetStringOrEmpty(maxCpuText));
if (maxCpuText) PhDereferenceObject(maxCpuText);
text = PhFormat(format, maxCpuProcessItem ? 8 : 3, 128);
if (maxCpuProcessItem) PhDereferenceObject(maxCpuProcessItem);
PhModifyNotifyIcon(PH_ICON_CPU_HISTORY, NIF_TIP | NIF_ICON, text->Buffer, icon);
@@ -250,10 +263,7 @@ VOID PhUpdateIconIoHistory()
HICON icon;
HANDLE maxIoProcessId;
PPH_PROCESS_ITEM maxIoProcessItem;
PPH_STRING readString;
PPH_STRING writeString;
PPH_STRING otherString;
PPH_STRING maxIoText = NULL;
PH_FORMAT format[8];
PPH_STRING text;
// Icon
@@ -294,31 +304,32 @@ VOID PhUpdateIconIoHistory()
maxIoProcessId = (HANDLE)PhGetItemCircularBuffer_ULONG(&PhMaxIoHistory, 0);
if (maxIoProcessId != NULL)
{
if (maxIoProcessItem = PhReferenceProcessItem(maxIoProcessId))
{
static PH_STRINGREF newLine = PH_STRINGREF_INIT(L"\n");
if (maxIoProcessId)
maxIoProcessItem = PhReferenceProcessItem(maxIoProcessId);
maxIoText = PhConcatStringRef2(&newLine, &maxIoProcessItem->ProcessName->sr);
PhDereferenceObject(maxIoProcessItem);
}
format[0].Type = StringFormatType;
PhInitializeStringRef(&format[0].u.String, L"R: ");
format[1].Type = SizeFormatType;
format[1].u.Size = PhIoReadDelta.Delta;
format[2].Type = StringFormatType;
PhInitializeStringRef(&format[2].u.String, L"\nW: ");
format[3].Type = SizeFormatType;
format[3].u.Size = PhIoWriteDelta.Delta;
format[4].Type = StringFormatType;
PhInitializeStringRef(&format[4].u.String, L"\nO: ");
format[5].Type = SizeFormatType;
format[5].u.Size = PhIoOtherDelta.Delta;
if (maxIoProcessItem)
{
format[6].Type = CharFormatType;
format[6].u.Char = '\n';
format[7].Type = StringFormatType;
format[7].u.String = maxIoProcessItem->ProcessName->sr;
}
readString = PhFormatSize(PhIoReadDelta.Delta, -1);
writeString = PhFormatSize(PhIoWriteDelta.Delta, -1);
otherString = PhFormatSize(PhIoOtherDelta.Delta, -1);
text = PhFormatString(
L"R: %s\nW: %s\nO: %s%s",
readString->Buffer,
writeString->Buffer,
otherString->Buffer,
PhGetStringOrEmpty(maxIoText)
);
PhDereferenceObject(readString);
PhDereferenceObject(writeString);
PhDereferenceObject(otherString);
if (maxIoText) PhDereferenceObject(maxIoText);
text = PhFormat(format, maxIoProcessItem ? 8 : 6, 128);
if (maxIoProcessItem) PhDereferenceObject(maxIoProcessItem);
PhModifyNotifyIcon(PH_ICON_IO_HISTORY, NIF_TIP | NIF_ICON, text->Buffer, icon);
@@ -351,8 +362,8 @@ VOID PhUpdateIconCommitHistory()
HDC hdc;
HBITMAP oldBitmap;
HICON icon;
PPH_STRING commitString;
FLOAT commitFraction;
DOUBLE commitFraction;
PH_FORMAT format[5];
PPH_STRING text;
// Icon
@@ -377,10 +388,21 @@ VOID PhUpdateIconCommitHistory()
// Text
commitString = PhFormatSize(UInt32x32To64(PhPerfInformation.CommittedPages, PAGE_SIZE), -1);
commitFraction = (FLOAT)PhPerfInformation.CommittedPages / PhPerfInformation.CommitLimit;
text = PhFormatString(L"Commit: %s (%.2f%%)", commitString->Buffer, commitFraction * 100);
PhDereferenceObject(commitString);
commitFraction = (DOUBLE)PhPerfInformation.CommittedPages / PhPerfInformation.CommitLimit;
format[0].Type = StringFormatType;
PhInitializeStringRef(&format[0].u.String, L"Commit: ");
format[1].Type = SizeFormatType;
format[1].u.Size = UInt32x32To64(PhPerfInformation.CommittedPages, PAGE_SIZE);
format[2].Type = StringFormatType;
PhInitializeStringRef(&format[2].u.String, L" (");
format[3].Type = DoubleFormatType | FormatUsePrecision;
format[3].Precision = 2;
format[3].u.Double = commitFraction * 100;
format[4].Type = StringFormatType;
PhInitializeStringRef(&format[4].u.String, L"%)");
text = PhFormat(format, sizeof(format) / sizeof(PH_FORMAT), 64);
PhModifyNotifyIcon(PH_ICON_COMMIT_HISTORY, NIF_TIP | NIF_ICON, text->Buffer, icon);
@@ -414,8 +436,8 @@ VOID PhUpdateIconPhysicalHistory()
HBITMAP oldBitmap;
HICON icon;
ULONG physicalUsage;
PPH_STRING physicalString;
FLOAT physicalFraction;
PH_FORMAT format[5];
PPH_STRING text;
// Icon
@@ -441,10 +463,21 @@ VOID PhUpdateIconPhysicalHistory()
// Text
physicalUsage = PhSystemBasicInformation.NumberOfPhysicalPages - PhPerfInformation.AvailablePages;
physicalString = PhFormatSize(UInt32x32To64(physicalUsage, PAGE_SIZE), -1);
physicalFraction = (FLOAT)physicalUsage / PhSystemBasicInformation.NumberOfPhysicalPages;
text = PhFormatString(L"Physical Memory: %s (%.2f%%)", physicalString->Buffer, physicalFraction * 100);
PhDereferenceObject(physicalString);
format[0].Type = StringFormatType;
PhInitializeStringRef(&format[0].u.String, L"Physical Memory: ");
format[1].Type = SizeFormatType;
format[1].u.Size = UInt32x32To64(physicalUsage, PAGE_SIZE);
format[2].Type = StringFormatType;
PhInitializeStringRef(&format[2].u.String, L" (");
format[3].Type = DoubleFormatType | FormatUsePrecision;
format[3].Precision = 2;
format[3].u.Double = physicalFraction * 100;
format[4].Type = StringFormatType;
PhInitializeStringRef(&format[4].u.String, L"%)");
text = PhFormat(format, sizeof(format) / sizeof(PH_FORMAT), 64);
PhModifyNotifyIcon(PH_ICON_PHYSICAL_HISTORY, NIF_TIP | NIF_ICON, text->Buffer, icon);
+29 -6
View File
@@ -304,26 +304,49 @@ PPH_STRING PhGetClientIdNameEx(
)
{
PPH_STRING name;
PH_FORMAT format[5];
if (ClientId->UniqueThread)
{
if (ProcessName)
{
name = PhFormatString(L"%s (%u): %u", ProcessName->Buffer,
(ULONG)ClientId->UniqueProcess, (ULONG)ClientId->UniqueThread);
format[0].Type = StringFormatType; format[0].u.String = ProcessName->sr;
format[1].Type = StringFormatType; PhInitializeStringRef(&format[1].u.String, L" (");
format[2].Type = UIntPtrFormatType; format[2].u.UIntPtr = (ULONG_PTR)ClientId->UniqueProcess;
format[3].Type = StringFormatType; PhInitializeStringRef(&format[3].u.String, L"): ");
format[4].Type = UIntPtrFormatType; format[4].u.UIntPtr = (ULONG_PTR)ClientId->UniqueThread;
name = PhFormat(format, 5, ProcessName->Length + 16 * sizeof(WCHAR));
}
else
{
name = PhFormatString(L"Non-existent process (%u): %u",
(ULONG)ClientId->UniqueProcess, (ULONG)ClientId->UniqueThread);
format[0].Type = StringFormatType; PhInitializeStringRef(&format[0].u.String, L"Non-existent process (");
format[1].Type = UIntPtrFormatType; format[1].u.UIntPtr = (ULONG_PTR)ClientId->UniqueProcess;
format[2].Type = StringFormatType; PhInitializeStringRef(&format[2].u.String, L"): ");
format[3].Type = UIntPtrFormatType; format[3].u.UIntPtr = (ULONG_PTR)ClientId->UniqueThread;
name = PhFormat(format, 4, 0);
}
}
else
{
if (ProcessName)
name = PhFormatString(L"%s (%u)", ProcessName->Buffer, (ULONG)ClientId->UniqueProcess);
{
format[0].Type = StringFormatType; format[0].u.String = ProcessName->sr;
format[1].Type = StringFormatType; PhInitializeStringRef(&format[1].u.String, L" (");
format[2].Type = UIntPtrFormatType; format[2].u.UIntPtr = (ULONG_PTR)ClientId->UniqueProcess;
format[3].Type = CharFormatType; format[3].u.Char = ')';
name = PhFormat(format, 4, 0);
}
else
name = PhFormatString(L"Non-existent process (%u)", (ULONG)ClientId->UniqueProcess);
{
format[0].Type = StringFormatType; PhInitializeStringRef(&format[0].u.String, L"Non-existent process (");
format[1].Type = UIntPtrFormatType; format[1].u.UIntPtr = (ULONG_PTR)ClientId->UniqueProcess;
format[2].Type = CharFormatType; format[2].u.Char = ')';
name = PhFormat(format, 3, 0);
}
}
return name;
+21 -3
View File
@@ -1107,7 +1107,13 @@ BOOLEAN NTAPI PhpProcessTreeListCallback(
if (number != 0)
{
PhSwapReference2(&node->IoTotalText, PhConcatStringRef2(&PhaFormatSize(number, -1)->sr, &perSecondString));
PH_FORMAT format[2];
format[0].Type = SizeFormatType;
format[0].u.Size = number;
format[1].Type = StringFormatType;
format[1].u.String = perSecondString;
PhSwapReference2(&node->IoTotalText, PhFormat(format, 2, 0));
getNodeText->Text = node->IoTotalText->sr;
}
else
@@ -1222,7 +1228,13 @@ BOOLEAN NTAPI PhpProcessTreeListCallback(
if (number != 0)
{
PhSwapReference2(&node->IoRoText, PhConcatStringRef2(&PhaFormatSize(number, -1)->sr, &perSecondString));
PH_FORMAT format[2];
format[0].Type = SizeFormatType;
format[0].u.Size = number;
format[1].Type = StringFormatType;
format[1].u.String = perSecondString;
PhSwapReference2(&node->IoRoText, PhFormat(format, 2, 0));
getNodeText->Text = node->IoRoText->sr;
}
else
@@ -1248,7 +1260,13 @@ BOOLEAN NTAPI PhpProcessTreeListCallback(
if (number != 0)
{
PhSwapReference2(&node->IoWText, PhConcatStringRef2(&PhaFormatSize(number, -1)->sr, &perSecondString));
PH_FORMAT format[2];
format[0].Type = SizeFormatType;
format[0].u.Size = number;
format[1].Type = StringFormatType;
format[1].u.String = perSecondString;
PhSwapReference2(&node->IoWText, PhFormat(format, 2, 0));
getNodeText->Text = node->IoWText->sr;
}
else
+6 -8
View File
@@ -974,14 +974,15 @@ VOID PhThreadProviderUpdate(
if (threadItem->ContextSwitchesDelta.Delta != oldDelta)
{
WCHAR deltaString[PH_INT32_STR_LEN_1];
if (threadItem->ContextSwitchesDelta.Delta != 0)
{
PhPrintUInt32(deltaString, threadItem->ContextSwitchesDelta.Delta);
PH_FORMAT format;
format.Type = UInt32FormatType | FormatGroupDigits;
format.u.UInt32 = threadItem->ContextSwitchesDelta.Delta;
PhSwapReference2(
&threadItem->ContextSwitchesDeltaString,
PhFormatDecimal(deltaString, 0, TRUE)
PhFormat(&format, 1, 0)
);
}
else
@@ -1015,14 +1016,11 @@ VOID PhThreadProviderUpdate(
if (threadItem->CyclesDelta.Delta != oldDelta)
{
WCHAR deltaString[PH_INT64_STR_LEN_1];
if (threadItem->CyclesDelta.Delta != 0)
{
PhPrintUInt64(deltaString, threadItem->CyclesDelta.Delta);
PhSwapReference2(
&threadItem->CyclesDeltaString,
PhFormatDecimal(deltaString, 0, TRUE)
PhFormatUInt64(threadItem->CyclesDelta.Delta, TRUE)
);
}
else
+47 -34
View File
@@ -21,6 +21,9 @@
*/
#include <phbase.h>
#include <locale.h>
extern ULONG PhMaxSizeUnit;
#define SMALL_BUFFER_LENGTH (PHOBJ_SMALL_OBJECT_SIZE - FIELD_OFFSET(PH_STRING, Buffer) - sizeof(WCHAR))
#define BUFFER_SIZE 512
@@ -28,6 +31,7 @@
#define PHP_FORMAT_NEGATIVE 0x1000
// Internal CRT routines needed for floating-point conversion
errno_t __cdecl _cfltcvt_l(double *arg, char *buffer, size_t sizeInBytes,
int format, int precision, int caps, _locale_t plocinfo);
@@ -55,6 +59,7 @@ PPH_STRING PhFormat(
static PH_INITONCE initOnce = PH_INITONCE_INIT;
static WCHAR decimalSeparator = '.';
static WCHAR thousandSeparator = ',';
static _locale_t userLocale = NULL;
PPH_STRING string;
PWSTR buffer;
@@ -81,6 +86,8 @@ PPH_STRING PhFormat(
thousandSeparator = localeBuffer[0];
}
userLocale = _create_locale(LC_ALL, "");
PhEndInitOnce(&initOnce);
}
@@ -96,14 +103,14 @@ PPH_STRING PhFormat(
#define ENSURE_BUFFER_LENGTH(NeededLength) \
do { \
if (allocatedLength < usedLength + NeededLength) \
if (allocatedLength < usedLength + (NeededLength)) \
{ \
PPH_STRING newString; \
\
allocatedLength *= 2; \
\
if (allocatedLength < usedLength + NeededLength) \
allocatedLength = usedLength + NeededLength; \
if (allocatedLength < usedLength + (NeededLength)) \
allocatedLength = usedLength + (NeededLength); \
\
newString = PhCreateStringEx(NULL, allocatedLength); \
memcpy(newString->Buffer, string->Buffer, usedLength); \
@@ -114,7 +121,7 @@ PPH_STRING PhFormat(
} while (0)
#define ADVANCE_BUFFER(Length) \
do { buffer += Length / sizeof(WCHAR); usedLength += Length; } while (0)
do { buffer += (Length) / sizeof(WCHAR); usedLength += (Length); } while (0)
while (Count--)
{
@@ -214,23 +221,24 @@ PPH_STRING PhFormat(
PCHAR integerToChar; \
PWSTR temp; \
ULONG tempCount; \
ULONG r; \
\
radix = (Format)->Radix; \
if (radix < 2) radix = 10; \
radix = 10; \
if (((Format)->Type & FormatUseRadix) && (Format)->Radix >= 2 && (Format)->Radix <= 69) \
radix = (Format)->Radix; \
integerToChar = PhIntegerToChar; \
if ((Format)->Type & FormatUpperCase) integerToChar = PhIntegerToCharUpper; \
if ((Format)->Type & FormatUpperCase) \
integerToChar = PhIntegerToCharUpper; \
temp = tempBuffer + BUFFER_SIZE - 1; \
tempCount = 0; \
\
if (radix <= 69) \
if (Input != 0) \
{ \
ULONG r; \
\
if ((Format)->Type & FormatGroupDigits) \
{ \
ULONG needsSep = 0; \
\
while (Input != 0) \
do \
{ \
PROCESS_DIGIT(Input); \
\
@@ -240,32 +248,37 @@ PPH_STRING PhFormat(
tempCount++; \
needsSep = 0; \
} \
} \
} while (Input != 0); \
} \
else \
{ \
while (Input != 0) \
do \
{ \
PROCESS_DIGIT(Input); \
} \
} while (Input != 0); \
} \
\
if (flags & PHP_FORMAT_NEGATIVE) \
{ \
*temp-- = '-'; \
tempCount++; \
} \
else if ((Format)->Type & FormatPrefixSign) \
{ \
*temp-- = '+'; \
tempCount++; \
} \
\
temp++; \
ENSURE_BUFFER_LENGTH(tempCount * sizeof(WCHAR)); \
memcpy(buffer, temp, tempCount * sizeof(WCHAR)); \
ADVANCE_BUFFER(tempCount * sizeof(WCHAR)); \
} \
else \
{ \
*temp-- = '0'; \
tempCount++; \
} \
\
if (flags & PHP_FORMAT_NEGATIVE) \
{ \
*temp-- = '-'; \
tempCount++; \
} \
else if ((Format)->Type & FormatPrefixSign) \
{ \
*temp-- = '+'; \
tempCount++; \
} \
\
temp++; \
ENSURE_BUFFER_LENGTH(tempCount * sizeof(WCHAR)); \
memcpy(buffer, temp, tempCount * sizeof(WCHAR)); \
ADVANCE_BUFFER(tempCount * sizeof(WCHAR)); \
} while (0)
#ifdef _M_IX86
@@ -356,13 +369,13 @@ CommonInt64Format:
c, \
precision, \
!!((Format)->Type & FormatUpperCase), \
NULL \
userLocale \
); \
\
/* if (((Format)->Type & FormatForceDecimalPoint) && precision == 0) */ \
/* _forcdecpt_l(tempBufferAnsi, NULL); */ \
/* _forcdecpt_l(tempBufferAnsi, userLocale); */ \
if ((Format)->Type & FormatCropZeros) \
_cropzeros_l((PSTR)tempBuffer, NULL); \
_cropzeros_l((PSTR)tempBuffer, userLocale); \
\
length = (ULONG)strlen((PSTR)tempBuffer); \
\
@@ -473,7 +486,7 @@ CommonInt64Format:
if (format->Type & FormatUsePrecision)
maxSizeUnit = format->Precision;
else
maxSizeUnit = -1;
maxSizeUnit = PhMaxSizeUnit;
while (
s > 1024 &&
+1
View File
@@ -2957,6 +2957,7 @@ typedef enum _PH_FORMAT_TYPE
FormatUsePrecision = 0x100,
FormatUseWidth = 0x200,
FormatUseRadix = 0x400,
// Floating-point flags
FormatStandardForm = 0x1000, // Use standard form instead of normal form
+10 -59
View File
@@ -1088,11 +1088,12 @@ PPH_STRING PhFormatUInt64(
__in BOOLEAN GroupDigits
)
{
WCHAR string[PH_INT64_STR_LEN_1];
PH_FORMAT format;
PhPrintUInt64(string, Value);
format.Type = UInt64FormatType | (GroupDigits ? FormatGroupDigits : 0);
format.u.UInt64 = Value;
return PhFormatDecimal(string, 0, GroupDigits);
return PhFormat(&format, 1, 0);
}
PPH_STRING PhpFormatDecimalFast(
@@ -1307,65 +1308,15 @@ PPH_STRING PhFormatSize(
__in ULONG MaxSizeUnit
)
{
ULONG i = 0;
ULONG maxSizeUnit;
DOUBLE s = (DOUBLE)Size;
PH_FORMAT format;
if (Size == 0)
return PhCreateString(L"0");
// PhFormat handles this better than the old method.
if (MaxSizeUnit != -1)
maxSizeUnit = MaxSizeUnit;
else
maxSizeUnit = PhMaxSizeUnit;
format.Type = SizeFormatType | FormatUsePrecision;
format.Precision = (USHORT)(MaxSizeUnit != -1 ? MaxSizeUnit : PhMaxSizeUnit);
format.u.Size = Size;
while (
s > 1024 &&
i < sizeof(PhSizeUnitNames) / sizeof(PWSTR) &&
i < maxSizeUnit
)
{
s /= 1024;
i++;
}
{
WCHAR numberString[512]; // perf hack
PPH_STRING formattedString;
PPH_STRING outputString;
ULONG length;
swprintf_s(numberString, sizeof(numberString) / 2, L"%.2f", s);
formattedString = PhFormatDecimal(numberString, 2, TRUE);
if (!formattedString)
{
return PhFormatString(L"%.2g %s", s, PhSizeUnitNames[i]);
}
length = formattedString->Length / 2;
if (
length >= 3 &&
formattedString->Buffer[length - 1] == '0' &&
formattedString->Buffer[length - 2] == '0'
)
{
// Remove the last three characters by making sure
// PhConcatStrings doesn't include them.
formattedString->Buffer[length - 3] = 0;
}
else if (length >= 1 && formattedString->Buffer[length - 1] == '0')
{
// Remove the last character.
formattedString->Buffer[length - 1] = 0;
}
outputString = PhConcatStrings(3, formattedString->Buffer, L" ", PhSizeUnitNames[i]);
PhDereferenceObject(formattedString);
return outputString;
}
return PhFormat(&format, 1, 0);
}
/**