strformatrt.cpp revision 76396bcfd42530a5024b2e5d91cbdb2843f26ede
/* $Id$ */
/** @file
* IPRT - IPRT String Formatter Extensions.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/** @page pg_rt_str_format_rt The IPRT String Format Extensions
*
* The string formatter supports most of the non-float format types and flags.
* See RTStrFormatV() for the full tail there. In addition we've added a number
* of iprt specific format types for the iprt typedefs and other useful stuff.
* Note that several of these are similar to \%p and doesn't care much if you try
*
*
* Group 1, the basic runtime typedefs (excluding those which obviously are pointer).
* - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
* - \%RTfile - Takes a #RTFILE value.
* - \%RTfmode - Takes a #RTFMODE value.
* - \%RTfoff - Takes a #RTFOFF value.
* - \%RTfp16 - Takes a #RTFAR16 value.
* - \%RTfp32 - Takes a #RTFAR32 value.
* - \%RTfp64 - Takes a #RTFAR64 value.
* - \%RTgid - Takes a #RTGID value.
* - \%RTino - Takes a #RTINODE value.
* - \%RTint - Takes a #RTINT value.
* - \%RTiop - Takes a #RTIOPORT value.
* - \%RTldrm - Takes a #RTLDRMOD value.
* - \%RTmac - Takes a #PCRTMAC pointer.
* - \%RTnaddr - Takes a #PCRTNETADDR value.
* - \%RTnaipv4 - Takes a #RTNETADDRIPV4 value.
* - \%RTnaipv6 - Takes a #PCRTNETADDRIPV6 value.
* - \%RTnthrd - Takes a #RTNATIVETHREAD value.
* - \%RTnthrd - Takes a #RTNATIVETHREAD value.
* - \%RTproc - Takes a #RTPROCESS value.
* - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
* - \%RTreg - Takes a #RTCCUINTREG value.
* - \%RTsel - Takes a #RTSEL value.
* - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
* - \%RTsock - Takes a #RTSOCKET value.
* - \%RTthrd - Takes a #RTTHREAD value.
* - \%RTuid - Takes a #RTUID value.
* - \%RTuint - Takes a #RTUINT value.
* - \%RTunicp - Takes a #RTUNICP value.
* - \%RTutf16 - Takes a #RTUTF16 value.
* - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
* - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
* - \%RGi - Takes a #RTGCINT value.
* - \%RGp - Takes a #RTGCPHYS value.
* - \%RGr - Takes a #RTGCUINTREG value.
* - \%RGu - Takes a #RTGCUINT value.
* - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
* - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
* - \%RHi - Takes a #RTHCINT value.
* - \%RHp - Takes a #RTHCPHYS value.
* - \%RHr - Takes a #RTHCUINTREG value.
* - \%RHu - Takes a #RTHCUINT value.
* - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
* - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
* - \%RRv - Takes a #RTRCPTR, #RTRCINTPTR or #RTRCUINTPTR value.
* - \%RCi - Takes a #RTINT value.
* - \%RCp - Takes a #RTCCPHYS value.
* - \%RCr - Takes a #RTCCUINTREG value.
* - \%RCu - Takes a #RTUINT value.
* - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
* - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
*
*
* Group 2, the generic integer types which are prefered over relying on what
* bit-count a 'long', 'short', or 'long long' has on a platform. This are
* highly prefered for the [u]intXX_t kind of types.
* - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
* - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
* - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
*
*
* Group 3, hex dumpers and other complex stuff which requires more than simple formatting.
* - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
* hex format. Use the width to specify the length, and the precision to
* set the number of bytes per line. Default width and precision is 16.
* - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
* i.e. a series of space separated bytes formatted as two digit hex value.
* Use the width to specify the length. Default length is 16 bytes.
* - \%Rrc - Takes an integer iprt status code as argument. Will insert the
* status code define corresponding to the iprt status code.
* - \%Rrs - Takes an integer iprt status code as argument. Will insert the
* short description of the specified status code.
* - \%Rrf - Takes an integer iprt status code as argument. Will insert the
* full description of the specified status code.
* - \%Rra - Takes an integer iprt status code as argument. Will insert the
* status code define + full description.
* - \%Rwc - Takes a long Windows error code as argument. Will insert the status
* code define corresponding to the Windows error code.
* - \%Rwf - Takes a long Windows error code as argument. Will insert the
* full description of the specified status code.
* - \%Rwa - Takes a long Windows error code as argument. Will insert the
* error code define + full description.
*
* code define corresponding to the Windows error code.
* full description of the specified status code.
* error code define + full description.
*
* - \%Rfn - Pretty printing of a function or method. It drops the
* return code and parameter list.
* - \%Rbn - Prints the base name. For dropping the path in
* order to save space when printing a path name.
*
* On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
*
*
* Group 4, structure dumpers.
*
* - \%RDtimespec - Takes a PCRTTIMESPEC.
*
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_STRING
#ifdef IN_RING3
#endif
/**
* Callback to format iprt formatting extentions.
* See @ref pg_rt_str_format_rt for a reference on the format types.
*
* @returns The number of bytes formatted.
* @param pfnOutput Pointer to output function.
* @param pvArgOutput Argument for the output function.
* @param ppszFormat Pointer to the format string pointer. Advance this till the char
* after the format specifier.
* @param pArgs Pointer to the argument list. Use this to fetch the arguments.
* @param cchWidth Format Width. -1 if not specified.
* @param cchPrecision Format Precision. -1 if not specified.
* @param fFlags Flags (RTSTR_NTFS_*).
* @param chArgSize The argument size specifier, 'l' or 'L'.
*/
size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
{
const char *pszFormatOrg = *ppszFormat;
char ch = *(*ppszFormat)++;
if (ch == 'R')
{
ch = *(*ppszFormat)++;
switch (ch)
{
/*
* Groups 1 and 2.
*/
case 'T':
case 'G':
case 'H':
case 'R':
case 'C':
case 'I':
case 'X':
case 'U':
{
/*
* Interpret the type.
*/
typedef enum
{
} RTSF;
static const struct
{
}
/** Sorted array of types, looked up using binary search! */
s_aTypes[] =
{
};
static const char s_szNull[] = "<NULL>";
int iStart = 0;
union
{
bool fBool;
} u;
char szBuf[80];
unsigned cch;
/*
* Lookup the type - binary search.
*/
for (;;)
{
if (!iDiff)
break;
{
return 0;
}
if (iDiff < 0)
iEnd = i - 1;
else
iStart = i + 1;
{
return 0;
}
}
/*
* Advance the format string and merge flags.
*/
/*
* Fetch the argument.
* It's important that a signed value gets sign-extended up to 64-bit.
*/
RT_ZERO(u);
if (fFlags & RTSTR_F_VALSIGNED)
{
{
case sizeof(int8_t):
fFlags |= RTSTR_F_8BIT;
break;
case sizeof(int16_t):
fFlags |= RTSTR_F_16BIT;
break;
case sizeof(int32_t):
fFlags |= RTSTR_F_32BIT;
break;
case sizeof(int64_t):
fFlags |= RTSTR_F_64BIT;
break;
default:
break;
}
}
else
{
{
case sizeof(uint8_t):
fFlags |= RTSTR_F_8BIT;
break;
case sizeof(uint16_t):
fFlags |= RTSTR_F_16BIT;
break;
case sizeof(uint32_t):
fFlags |= RTSTR_F_32BIT;
break;
case sizeof(uint64_t):
fFlags |= RTSTR_F_64BIT;
break;
case sizeof(RTFAR32):
break;
case sizeof(RTFAR64):
break;
default:
break;
}
}
/*
* Format the output.
*/
{
case RTSF_INT:
{
break;
}
/* hex which defaults to max width. */
case RTSF_INTW:
{
if (cchWidth < 0)
{
}
break;
}
case RTSF_BOOL:
{
static const char s_szTrue[] = "true ";
static const char s_szFalse[] = "false";
if (u.u64 == 1)
if (u.u64 == 0)
/* invalid boolean value */
}
case RTSF_FP16:
{
fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
break;
}
case RTSF_FP32:
{
fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
break;
}
case RTSF_FP64:
{
fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
break;
}
case RTSF_IPV4:
"%u.%u.%u.%u",
case RTSF_IPV6:
{
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
}
case RTSF_MAC:
{
"%02x:%02x:%02x:%02x:%02x:%02x",
}
case RTSF_NETADDR:
{
{
{
case RTNETADDRTYPE_IPV4:
"%u.%u.%u.%u",
"%u.%u.%u.%u:%u",
case RTNETADDRTYPE_IPV6:
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x %u",
case RTNETADDRTYPE_MAC:
"%02x:%02x:%02x:%02x:%02x:%02x",
default:
}
}
}
case RTSF_UUID:
{
{
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
}
}
default:
return 0;
}
/*
* Finally, output the formatted string and return.
*/
}
/* Group 3 */
/*
* Base name printing.
*/
case 'b':
{
switch (*(*ppszFormat)++)
{
case 'n':
{
const char *pszLastSep;
{
if (RTPATH_IS_SEP(ch))
{
do
psz++;
if (!ch)
break;
pszLastSep = psz;
}
psz++;
}
}
default:
break;
}
break;
}
/*
* Pretty function / method name printing.
*/
case 'f':
{
switch (*(*ppszFormat)++)
{
/*
* Pretty function / method name printing.
* This isn't 100% right (see classic signal prototype) and it assumes
* standardized names, but it'll do for today.
*/
case 'n':
{
const char *pszStart;
{
if (RT_C_IS_BLANK(ch))
{
psz++;
psz++;
if (ch)
}
else if (ch == '(')
break;
else
psz++;
}
}
default:
break;
}
break;
}
/*
*/
case 'h':
{
switch (*(*ppszFormat)++)
{
/*
* Hex stuff.
*/
case 'x':
{
if (cchWidth <= 0)
cchWidth = 16;
if (pu8)
{
switch (*(*ppszFormat)++)
{
/*
* Regular hex dump.
*/
case 'd':
{
int off = 0;
if (cchPrecision <= 0)
cchPrecision = 16;
{
int i;
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*x %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
while (i++ < cchPrecision)
{
}
/* next */
pu8 += cchPrecision;
off += cchPrecision;
}
return cch;
}
/*
* Hex string.
*/
case 's':
{
if (cchWidth-- > 0)
{
return cch;
}
break;
}
default:
break;
}
}
else
break;
}
#ifdef IN_RING3
/*
* XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
* ASSUMES: If Windows Then COM else XPCOM.
*/
case 'r':
{
switch (*(*ppszFormat)++)
{
case 'c':
case 'f':
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
default:
return 0;
}
break;
}
#endif /* IN_RING3 */
default:
return 0;
}
break;
}
/*
* iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
*/
case 'r':
{
#ifdef IN_RING3 /* we don't want this anywhere else yet. */
switch (*(*ppszFormat)++)
{
case 'c':
case 's':
case 'f':
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
default:
return 0;
}
#else /* !IN_RING3 */
switch (*(*ppszFormat)++)
{
case 'c':
case 's':
case 'f':
case 'a':
default:
return 0;
}
#endif /* !IN_RING3 */
break;
}
#if defined(IN_RING3)
/*
* Windows status code: %Rwc, %Rwf, %Rwa
*/
case 'w':
{
# if defined(RT_OS_WINDOWS)
# endif
switch (*(*ppszFormat)++)
{
# if defined(RT_OS_WINDOWS)
case 'c':
case 'f':
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
# else
case 'c':
case 'f':
case 'a':
# endif
default:
return 0;
}
break;
}
#endif /* IN_RING3 */
/*
* Group 4, structure dumpers.
*/
case 'D':
{
/*
* Interpret the type.
*/
typedef enum
{
} RTST;
/** Set if it's a pointer */
#define RTST_FLAGS_POINTER RT_BIT(0)
static const struct
{
}
/** Sorted array of types, looked up using binary search! */
s_aTypes[] =
{
};
int iStart = 0;
union
{
const void *pv;
} u;
/*
* Lookup the type - binary search.
*/
for (;;)
{
if (!iDiff)
break;
{
return 0;
}
if (iDiff < 0)
iEnd = i - 1;
else
iStart = i + 1;
{
return 0;
}
}
/*
* Fetch the argument.
*/
u.u64 = 0;
{
case sizeof(const void *):
break;
default:
break;
}
/*
* If it's a pointer, we'll check if it's valid before going on.
*/
/*
* Format the output.
*/
{
case RTST_TIMESPEC:
default:
break;
}
break;
}
/*
*/
default:
break;
}
}
else
return 0;
}