VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strformatrt.cpp@ 25645

Last change on this file since 25645 was 25645, checked in by vboxsync, 15 years ago

IPRT,DoxyFile.Core: Mopped up the errors in the IPRT doxygen run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 44.2 KB
Line 
1/* $Id: strformatrt.cpp 25645 2010-01-05 09:29:31Z vboxsync $ */
2/** @file
3 * IPRT - IPRT String Formatter Extensions.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/** @page pg_rt_str_format_rt The IPRT String Format Extensions
32 *
33 * The string formatter supports most of the non-float format types and flags.
34 * See RTStrFormatV() for the full tail there. In addition we've added a number
35 * of iprt specific format types for the iprt typedefs and other useful stuff.
36 * Note that several of these are similar to \%p and doesn't care much if you try
37 * add formating flags/width/precision.
38 *
39 *
40 * Group 1, the basic runtime typedefs (excluding those which obviously are pointer).
41 * - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
42 * - \%RTfile - Takes a #RTFILE value.
43 * - \%RTfmode - Takes a #RTFMODE value.
44 * - \%RTfoff - Takes a #RTFOFF value.
45 * - \%RTfp16 - Takes a #RTFAR16 value.
46 * - \%RTfp32 - Takes a #RTFAR32 value.
47 * - \%RTfp64 - Takes a #RTFAR64 value.
48 * - \%RTgid - Takes a #RTGID value.
49 * - \%RTino - Takes a #RTINODE value.
50 * - \%RTint - Takes a #RTINT value.
51 * - \%RTiop - Takes a #RTIOPORT value.
52 * - \%RTldrm - Takes a #RTLDRMOD value.
53 * - \%RTmac - Takes a #PCRTMAC pointer.
54 * - \%RTnaipv4 - Takes a #RTNETADDRIPV4 value.
55 * - \%RTnaipv6 - Takes a #PCRTNETADDRIPV6 value.
56 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
57 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
58 * - \%RTproc - Takes a #RTPROCESS value.
59 * - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
60 * - \%RTreg - Takes a #RTCCUINTREG value.
61 * - \%RTsel - Takes a #RTSEL value.
62 * - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
63 * - \%RTsock - Takes a #RTSOCKET value.
64 * - \%RTthrd - Takes a #RTTHREAD value.
65 * - \%RTuid - Takes a #RTUID value.
66 * - \%RTuint - Takes a #RTUINT value.
67 * - \%RTunicp - Takes a #RTUNICP value.
68 * - \%RTutf16 - Takes a #RTUTF16 value.
69 * - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
70 * - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
71 * - \%RGi - Takes a #RTGCINT value.
72 * - \%RGp - Takes a #RTGCPHYS value.
73 * - \%RGr - Takes a #RTGCUINTREG value.
74 * - \%RGu - Takes a #RTGCUINT value.
75 * - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
76 * - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
77 * - \%RHi - Takes a #RTHCINT value.
78 * - \%RHp - Takes a #RTHCPHYS value.
79 * - \%RHr - Takes a #RTHCUINTREG value.
80 * - \%RHu - Takes a #RTHCUINT value.
81 * - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
82 * - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
83 * - \%RRv - Takes a #RTRCPTR, #RTRCINTPTR or #RTRCUINTPTR value.
84 * - \%RCi - Takes a #RTINT value.
85 * - \%RCp - Takes a #RTCCPHYS value.
86 * - \%RCr - Takes a #RTCCUINTREG value.
87 * - \%RCu - Takes a #RTUINT value.
88 * - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
89 * - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
90 *
91 *
92 * Group 2, the generic integer types which are prefered over relying on what
93 * bit-count a 'long', 'short', or 'long long' has on a platform. This are
94 * highly prefered for the [u]intXX_t kind of types.
95 * - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
96 * - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
97 * - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
98 *
99 *
100 * Group 3, hex dumpers and other complex stuff which requires more than simple formatting.
101 * - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
102 * hex format. Use the width to specify the length, and the precision to
103 * set the number of bytes per line. Default width and precision is 16.
104 * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
105 * i.e. a series of space separated bytes formatted as two digit hex value.
106 * Use the width to specify the length. Default length is 16 bytes.
107 * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
108 * status code define corresponding to the iprt status code.
109 * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
110 * short description of the specified status code.
111 * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
112 * full description of the specified status code.
113 * - \%Rra - Takes an integer iprt status code as argument. Will insert the
114 * status code define + full description.
115 * - \%Rt - Current thread (RTThreadSelf()), no arguments.
116 *
117 * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
118 * code define corresponding to the Windows error code.
119 * - \%Rwf - Takes a long Windows error code as argument. Will insert the
120 * full description of the specified status code.
121 * - \%Rwa - Takes a long Windows error code as argument. Will insert the
122 * error code define + full description.
123 *
124 * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
125 * code define corresponding to the Windows error code.
126 * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
127 * full description of the specified status code.
128 * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
129 * error code define + full description.
130 *
131 * - \%Rfn - Pretty printing of a function or method. It drops the
132 * return code and parameter list.
133 * - \%Rbn - Prints the base name. For dropping the path in
134 * order to save space when printing a path name.
135 *
136 * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
137 *
138 *
139 * Group 4, structure dumpers.
140 *
141 * - \%RDtimespec - Takes a PCRTTIMESPEC.
142 *
143 *
144 */
145
146/*******************************************************************************
147* Header Files *
148*******************************************************************************/
149#define LOG_GROUP RTLOGGROUP_STRING
150#include <iprt/string.h>
151#include "internal/iprt.h"
152
153#include <iprt/log.h>
154#include <iprt/assert.h>
155#include <iprt/string.h>
156#include <iprt/stdarg.h>
157#ifdef IN_RING3
158# include <iprt/thread.h>
159# include <iprt/err.h>
160#endif
161#include <iprt/ctype.h>
162#include <iprt/time.h>
163#include <iprt/net.h>
164#include <iprt/path.h>
165#include "internal/string.h"
166
167
168
169/**
170 * Callback to format iprt formatting extentions.
171 * See @ref pg_rt_str_format_rt for a reference on the format types.
172 *
173 * @returns The number of bytes formatted.
174 * @param pfnOutput Pointer to output function.
175 * @param pvArgOutput Argument for the output function.
176 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
177 * after the format specifier.
178 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
179 * @param cchWidth Format Width. -1 if not specified.
180 * @param cchPrecision Format Precision. -1 if not specified.
181 * @param fFlags Flags (RTSTR_NTFS_*).
182 * @param chArgSize The argument size specifier, 'l' or 'L'.
183 */
184size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
185{
186 const char *pszFormatOrg = *ppszFormat;
187 char ch = *(*ppszFormat)++;
188 if (ch == 'R')
189 {
190 ch = *(*ppszFormat)++;
191 switch (ch)
192 {
193 /*
194 * Groups 1 and 2.
195 */
196 case 'T':
197 case 'G':
198 case 'H':
199 case 'R':
200 case 'C':
201 case 'I':
202 case 'X':
203 case 'U':
204 {
205 /*
206 * Interpret the type.
207 */
208 typedef enum
209 {
210 RTSF_INT,
211 RTSF_INTW,
212 RTSF_BOOL,
213 RTSF_FP16,
214 RTSF_FP32,
215 RTSF_FP64,
216 RTSF_IPV4,
217 RTSF_IPV6,
218 RTSF_MAC,
219 RTSF_UUID
220 } RTSF;
221 static const struct
222 {
223 uint8_t cch; /**< the length of the string. */
224 char sz[10]; /**< the part following 'R'. */
225 uint8_t cb; /**< the size of the type. */
226 uint8_t u8Base; /**< the size of the type. */
227 RTSF enmFormat; /**< The way to format it. */
228 uint16_t fFlags; /**< additional RTSTR_F_* flags. */
229 }
230 /** Sorted array of types, looked up using binary search! */
231 s_aTypes[] =
232 {
233#define STRMEM(str) sizeof(str) - 1, str
234 { STRMEM("Ci"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
235 { STRMEM("Cp"), sizeof(RTCCPHYS), 16, RTSF_INTW, 0 },
236 { STRMEM("Cr"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
237 { STRMEM("Cu"), sizeof(RTUINT), 10, RTSF_INT, 0 },
238 { STRMEM("Cv"), sizeof(void *), 16, RTSF_INTW, 0 },
239 { STRMEM("Cx"), sizeof(RTUINT), 16, RTSF_INT, 0 },
240 { STRMEM("Gi"), sizeof(RTGCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
241 { STRMEM("Gp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
242 { STRMEM("Gr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
243 { STRMEM("Gu"), sizeof(RTGCUINT), 10, RTSF_INT, 0 },
244 { STRMEM("Gv"), sizeof(RTGCPTR), 16, RTSF_INTW, 0 },
245 { STRMEM("Gx"), sizeof(RTGCUINT), 16, RTSF_INT, 0 },
246 { STRMEM("Hi"), sizeof(RTHCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
247 { STRMEM("Hp"), sizeof(RTHCPHYS), 16, RTSF_INTW, 0 },
248 { STRMEM("Hr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
249 { STRMEM("Hu"), sizeof(RTHCUINT), 10, RTSF_INT, 0 },
250 { STRMEM("Hv"), sizeof(RTHCPTR), 16, RTSF_INTW, 0 },
251 { STRMEM("Hx"), sizeof(RTHCUINT), 16, RTSF_INT, 0 },
252 { STRMEM("I16"), sizeof(int16_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
253 { STRMEM("I32"), sizeof(int32_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
254 { STRMEM("I64"), sizeof(int64_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
255 { STRMEM("I8"), sizeof(int8_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
256 { STRMEM("Rv"), sizeof(RTRCPTR), 16, RTSF_INTW, 0 },
257 { STRMEM("Tbool"), sizeof(bool), 10, RTSF_BOOL, 0 },
258 { STRMEM("Tfile"), sizeof(RTFILE), 10, RTSF_INT, 0 },
259 { STRMEM("Tfmode"), sizeof(RTFMODE), 16, RTSF_INTW, 0 },
260 { STRMEM("Tfoff"), sizeof(RTFOFF), 10, RTSF_INT, RTSTR_F_VALSIGNED },
261 { STRMEM("Tfp16"), sizeof(RTFAR16), 16, RTSF_FP16, RTSTR_F_ZEROPAD },
262 { STRMEM("Tfp32"), sizeof(RTFAR32), 16, RTSF_FP32, RTSTR_F_ZEROPAD },
263 { STRMEM("Tfp64"), sizeof(RTFAR64), 16, RTSF_FP64, RTSTR_F_ZEROPAD },
264 { STRMEM("Tgid"), sizeof(RTGID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
265 { STRMEM("Tino"), sizeof(RTINODE), 16, RTSF_INTW, 0 },
266 { STRMEM("Tint"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
267 { STRMEM("Tiop"), sizeof(RTIOPORT), 16, RTSF_INTW, 0 },
268 { STRMEM("Tldrm"), sizeof(RTLDRMOD), 16, RTSF_INTW, 0 },
269 { STRMEM("Tmac"), sizeof(PCRTMAC), 16, RTSF_MAC, 0 },
270 { STRMEM("Tnaipv4"), sizeof(RTNETADDRIPV4), 10, RTSF_IPV4, 0 },
271 { STRMEM("Tnaipv6"), sizeof(PCRTNETADDRIPV6),16, RTSF_IPV6, 0 },
272 { STRMEM("Tnthrd"), sizeof(RTNATIVETHREAD), 16, RTSF_INTW, 0 },
273 { STRMEM("Tproc"), sizeof(RTPROCESS), 16, RTSF_INTW, 0 },
274 { STRMEM("Tptr"), sizeof(RTUINTPTR), 16, RTSF_INTW, 0 },
275 { STRMEM("Treg"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
276 { STRMEM("Tsel"), sizeof(RTSEL), 16, RTSF_INTW, 0 },
277 { STRMEM("Tsem"), sizeof(RTSEMEVENT), 16, RTSF_INTW, 0 },
278 { STRMEM("Tsock"), sizeof(RTSOCKET), 10, RTSF_INT, 0 },
279 { STRMEM("Tthrd"), sizeof(RTTHREAD), 16, RTSF_INTW, 0 },
280 { STRMEM("Tuid"), sizeof(RTUID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
281 { STRMEM("Tuint"), sizeof(RTUINT), 10, RTSF_INT, 0 },
282 { STRMEM("Tunicp"), sizeof(RTUNICP), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
283 { STRMEM("Tutf16"), sizeof(RTUTF16), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
284 { STRMEM("Tuuid"), sizeof(PCRTUUID), 16, RTSF_UUID, 0 },
285 { STRMEM("Txint"), sizeof(RTUINT), 16, RTSF_INT, 0 },
286 { STRMEM("U16"), sizeof(uint16_t), 10, RTSF_INT, 0 },
287 { STRMEM("U32"), sizeof(uint32_t), 10, RTSF_INT, 0 },
288 { STRMEM("U64"), sizeof(uint64_t), 10, RTSF_INT, 0 },
289 { STRMEM("U8"), sizeof(uint8_t), 10, RTSF_INT, 0 },
290 { STRMEM("X16"), sizeof(uint16_t), 16, RTSF_INT, 0 },
291 { STRMEM("X32"), sizeof(uint32_t), 16, RTSF_INT, 0 },
292 { STRMEM("X64"), sizeof(uint64_t), 16, RTSF_INT, 0 },
293 { STRMEM("X8"), sizeof(uint8_t), 16, RTSF_INT, 0 },
294#undef STRMEM
295 };
296 static const char s_szNull[] = "<NULL>";
297
298 const char *pszType = *ppszFormat - 1;
299 int iStart = 0;
300 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
301 int i = RT_ELEMENTS(s_aTypes) / 2;
302
303 union
304 {
305 uint8_t u8;
306 uint16_t u16;
307 uint32_t u32;
308 uint64_t u64;
309 int8_t i8;
310 int16_t i16;
311 int32_t i32;
312 int64_t i64;
313 RTFAR16 fp16;
314 RTFAR32 fp32;
315 RTFAR64 fp64;
316 bool fBool;
317 PCRTMAC pMac;
318 RTNETADDRIPV4 Ipv4Addr;
319 PCRTNETADDRIPV6 pIpv6Addr;
320 PCRTUUID pUuid;
321 } u;
322 char szBuf[80];
323 unsigned cch;
324
325 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
326
327 /*
328 * Lookup the type - binary search.
329 */
330 for (;;)
331 {
332 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
333 if (!iDiff)
334 break;
335 if (iEnd == iStart)
336 {
337 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
338 return 0;
339 }
340 if (iDiff < 0)
341 iEnd = i - 1;
342 else
343 iStart = i + 1;
344 if (iEnd < iStart)
345 {
346 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
347 return 0;
348 }
349 i = iStart + (iEnd - iStart) / 2;
350 }
351
352 /*
353 * Advance the format string and merge flags.
354 */
355 *ppszFormat += s_aTypes[i].cch - 1;
356 fFlags |= s_aTypes[i].fFlags;
357
358 /*
359 * Fetch the argument.
360 * It's important that a signed value gets sign-extended up to 64-bit.
361 */
362 u.u64 = 0;
363 if (fFlags & RTSTR_F_VALSIGNED)
364 {
365 switch (s_aTypes[i].cb)
366 {
367 case sizeof(int8_t):
368 u.i64 = va_arg(*pArgs, /*int8_t*/int);
369 fFlags |= RTSTR_F_8BIT;
370 break;
371 case sizeof(int16_t):
372 u.i64 = va_arg(*pArgs, /*int16_t*/int);
373 fFlags |= RTSTR_F_16BIT;
374 break;
375 case sizeof(int32_t):
376 u.i64 = va_arg(*pArgs, int32_t);
377 fFlags |= RTSTR_F_32BIT;
378 break;
379 case sizeof(int64_t):
380 u.i64 = va_arg(*pArgs, int64_t);
381 fFlags |= RTSTR_F_64BIT;
382 break;
383 default:
384 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
385 break;
386 }
387 }
388 else
389 {
390 switch (s_aTypes[i].cb)
391 {
392 case sizeof(uint8_t):
393 u.u8 = va_arg(*pArgs, /*uint8_t*/unsigned);
394 fFlags |= RTSTR_F_8BIT;
395 break;
396 case sizeof(uint16_t):
397 u.u16 = va_arg(*pArgs, /*uint16_t*/unsigned);
398 fFlags |= RTSTR_F_16BIT;
399 break;
400 case sizeof(uint32_t):
401 u.u32 = va_arg(*pArgs, uint32_t);
402 fFlags |= RTSTR_F_32BIT;
403 break;
404 case sizeof(uint64_t):
405 u.u64 = va_arg(*pArgs, uint64_t);
406 fFlags |= RTSTR_F_64BIT;
407 break;
408 case sizeof(RTFAR32):
409 u.fp32 = va_arg(*pArgs, RTFAR32);
410 break;
411 case sizeof(RTFAR64):
412 u.fp64 = va_arg(*pArgs, RTFAR64);
413 break;
414 default:
415 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
416 break;
417 }
418 }
419
420 /*
421 * Format the output.
422 */
423 switch (s_aTypes[i].enmFormat)
424 {
425 case RTSF_INT:
426 {
427 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
428 break;
429 }
430
431 /* hex which defaults to max width. */
432 case RTSF_INTW:
433 {
434 Assert(s_aTypes[i].u8Base == 16);
435 if (cchWidth < 0)
436 {
437 cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
438 fFlags |= RTSTR_F_ZEROPAD;
439 }
440 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
441 break;
442 }
443
444 case RTSF_BOOL:
445 {
446 static const char s_szTrue[] = "true ";
447 static const char s_szFalse[] = "false";
448 if (u.u64 == 1)
449 return pfnOutput(pvArgOutput, s_szTrue, sizeof(s_szTrue) - 1);
450 if (u.u64 == 0)
451 return pfnOutput(pvArgOutput, s_szFalse, sizeof(s_szFalse) - 1);
452 /* invalid boolean value */
453 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
454 }
455
456 case RTSF_FP16:
457 {
458 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
459 cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
460 Assert(cch == 4);
461 szBuf[4] = ':';
462 cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
463 Assert(cch == 4);
464 cch = 4 + 1 + 4;
465 break;
466 }
467 case RTSF_FP32:
468 {
469 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
470 cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
471 Assert(cch == 4);
472 szBuf[4] = ':';
473 cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
474 Assert(cch == 8);
475 cch = 4 + 1 + 8;
476 break;
477 }
478 case RTSF_FP64:
479 {
480 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
481 cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
482 Assert(cch == 4);
483 szBuf[4] = ':';
484 cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
485 Assert(cch == 16);
486 cch = 4 + 1 + 16;
487 break;
488 }
489
490 case RTSF_IPV4:
491 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
492 "%u.%u.%u.%u",
493 u.Ipv4Addr.au8[0],
494 u.Ipv4Addr.au8[1],
495 u.Ipv4Addr.au8[2],
496 u.Ipv4Addr.au8[3]);
497
498 case RTSF_IPV6:
499 {
500 if (VALID_PTR(u.pIpv6Addr))
501 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
502 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
503 u.pIpv6Addr->au8[0],
504 u.pIpv6Addr->au8[1],
505 u.pIpv6Addr->au8[2],
506 u.pIpv6Addr->au8[3],
507 u.pIpv6Addr->au8[4],
508 u.pIpv6Addr->au8[5],
509 u.pIpv6Addr->au8[6],
510 u.pIpv6Addr->au8[7],
511 u.pIpv6Addr->au8[8],
512 u.pIpv6Addr->au8[9],
513 u.pIpv6Addr->au8[10],
514 u.pIpv6Addr->au8[11],
515 u.pIpv6Addr->au8[12],
516 u.pIpv6Addr->au8[13],
517 u.pIpv6Addr->au8[14],
518 u.pIpv6Addr->au8[15]);
519 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
520 }
521
522 case RTSF_MAC:
523 {
524 if (VALID_PTR(u.pMac))
525 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
526 "%02x:%02x:%02x:%02x:%02x:%02x",
527 u.pMac->au8[0],
528 u.pMac->au8[1],
529 u.pMac->au8[2],
530 u.pMac->au8[3],
531 u.pMac->au8[4],
532 u.pMac->au8[5]);
533 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
534 }
535
536 case RTSF_UUID:
537 {
538 if (VALID_PTR(u.pUuid))
539 {
540 /* cannot call RTUuidToStr because of GC/R0. */
541 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
542 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
543 u.pUuid->Gen.u32TimeLow,
544 u.pUuid->Gen.u16TimeMid,
545 u.pUuid->Gen.u16TimeHiAndVersion,
546 u.pUuid->Gen.u8ClockSeqHiAndReserved,
547 u.pUuid->Gen.u8ClockSeqLow,
548 u.pUuid->Gen.au8Node[0],
549 u.pUuid->Gen.au8Node[1],
550 u.pUuid->Gen.au8Node[2],
551 u.pUuid->Gen.au8Node[3],
552 u.pUuid->Gen.au8Node[4],
553 u.pUuid->Gen.au8Node[5]);
554 }
555 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
556 }
557
558 default:
559 AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
560 return 0;
561 }
562
563 /*
564 * Finally, output the formatted string and return.
565 */
566 return pfnOutput(pvArgOutput, szBuf, cch);
567 }
568
569
570 /* Group 3 */
571
572 /*
573 * Base name printing.
574 */
575 case 'b':
576 {
577 switch (*(*ppszFormat)++)
578 {
579 case 'n':
580 {
581 const char *pszLastSep;
582 const char *psz = pszLastSep = va_arg(*pArgs, const char *);
583 if (!VALID_PTR(psz))
584 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
585
586 while ((ch = *psz) != '\0')
587 {
588 if (RTPATH_IS_SEP(ch))
589 {
590 do
591 psz++;
592 while ((ch = *psz) != '\0' && RTPATH_IS_SEP(ch));
593 if (!ch)
594 break;
595 pszLastSep = psz;
596 }
597 psz++;
598 }
599
600 return pfnOutput(pvArgOutput, pszLastSep, psz - pszLastSep);
601 }
602
603 default:
604 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
605 break;
606 }
607 break;
608 }
609
610
611 /*
612 * Pretty function / method name printing.
613 */
614 case 'f':
615 {
616 switch (*(*ppszFormat)++)
617 {
618 /*
619 * Pretty function / method name printing.
620 * This isn't 100% right (see classic signal prototype) and it assumes
621 * standardized names, but it'll do for today.
622 */
623 case 'n':
624 {
625 const char *pszStart;
626 const char *psz = pszStart = va_arg(*pArgs, const char *);
627 if (!VALID_PTR(psz))
628 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
629
630 while ((ch = *psz) != '\0' && ch != '(')
631 {
632 if (RT_C_IS_BLANK(ch))
633 {
634 psz++;
635 while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
636 psz++;
637 if (ch)
638 pszStart = psz;
639 }
640 else if (ch == '(')
641 break;
642 else
643 psz++;
644 }
645
646 return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
647 }
648
649 default:
650 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
651 break;
652 }
653 break;
654 }
655
656
657 /*
658 * hex dumping and COM/XPCOM.
659 */
660 case 'h':
661 {
662 switch (*(*ppszFormat)++)
663 {
664 /*
665 * Hex stuff.
666 */
667 case 'x':
668 {
669 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
670 if (cchWidth <= 0)
671 cchWidth = 16;
672 if (pu8)
673 {
674 switch (*(*ppszFormat)++)
675 {
676 /*
677 * Regular hex dump.
678 */
679 case 'd':
680 {
681 size_t cch = 0;
682 int off = 0;
683
684 if (cchPrecision <= 0)
685 cchPrecision = 16;
686
687 while (off < cchWidth)
688 {
689 int i;
690 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*x %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
691 for (i = 0; i < cchPrecision && off + i < cchWidth ; i++)
692 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
693 off + i < cchWidth ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
694 while (i++ < cchPrecision)
695 cch += pfnOutput(pvArgOutput, " ", 3);
696
697 cch += pfnOutput(pvArgOutput, " ", 1);
698
699 for (i = 0; i < cchPrecision && off + i < cchWidth; i++)
700 {
701 uint8_t u8 = pu8[i];
702 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
703 }
704
705 /* next */
706 pu8 += cchPrecision;
707 off += cchPrecision;
708 }
709 return cch;
710 }
711
712 /*
713 * Hex string.
714 */
715 case 's':
716 {
717 if (cchWidth-- > 0)
718 {
719 size_t cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
720 for (; cchWidth > 0; cchWidth--, pu8++)
721 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
722 return cch;
723 }
724 break;
725 }
726
727 default:
728 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
729 break;
730 }
731 }
732 else
733 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
734 break;
735 }
736
737
738#ifdef IN_RING3
739 /*
740 * XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
741 * ASSUMES: If Windows Then COM else XPCOM.
742 */
743 case 'r':
744 {
745 uint32_t hrc = va_arg(*pArgs, uint32_t);
746 PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
747 switch (*(*ppszFormat)++)
748 {
749 case 'c':
750 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
751 case 'f':
752 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
753 case 'a':
754 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
755 default:
756 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
757 return 0;
758 }
759 break;
760 }
761#endif /* IN_RING3 */
762
763 default:
764 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
765 return 0;
766
767 }
768 break;
769 }
770
771 /*
772 * iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
773 */
774 case 'r':
775 {
776 int rc = va_arg(*pArgs, int);
777#ifdef IN_RING3 /* we don't want this anywhere else yet. */
778 PCRTSTATUSMSG pMsg = RTErrGet(rc);
779 switch (*(*ppszFormat)++)
780 {
781 case 'c':
782 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
783 case 's':
784 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
785 case 'f':
786 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
787 case 'a':
788 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
789 default:
790 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
791 return 0;
792 }
793#else /* !IN_RING3 */
794 switch (*(*ppszFormat)++)
795 {
796 case 'c':
797 case 's':
798 case 'f':
799 case 'a':
800 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
801 default:
802 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
803 return 0;
804 }
805#endif /* !IN_RING3 */
806 break;
807 }
808
809#if defined(IN_RING3)
810 /*
811 * Windows status code: %Rwc, %Rwf, %Rwa
812 */
813 case 'w':
814 {
815 long rc = va_arg(*pArgs, long);
816# if defined(RT_OS_WINDOWS)
817 PCRTWINERRMSG pMsg = RTErrWinGet(rc);
818# endif
819 switch (*(*ppszFormat)++)
820 {
821# if defined(RT_OS_WINDOWS)
822 case 'c':
823 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
824 case 'f':
825 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
826 case 'a':
827 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
828# else
829 case 'c':
830 case 'f':
831 case 'a':
832 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
833# endif
834 default:
835 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
836 return 0;
837 }
838 break;
839 }
840#endif /* IN_RING3 */
841
842 /*
843 * Group 4, structure dumpers.
844 */
845 case 'D':
846 {
847 /*
848 * Interpret the type.
849 */
850 typedef enum
851 {
852 RTST_TIMESPEC
853 } RTST;
854/** Set if it's a pointer */
855#define RTST_FLAGS_POINTER RT_BIT(0)
856 static const struct
857 {
858 uint8_t cch; /**< the length of the string. */
859 char sz[16-2]; /**< the part following 'R'. */
860 uint8_t cb; /**< the size of the argument. */
861 uint8_t fFlags; /**< RTST_FLAGS_* */
862 RTST enmType; /**< The structure type. */
863 }
864 /** Sorted array of types, looked up using binary search! */
865 s_aTypes[] =
866 {
867#define STRMEM(str) sizeof(str) - 1, str
868 { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
869#undef STRMEM
870 };
871 const char *pszType = *ppszFormat - 1;
872 int iStart = 0;
873 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
874 int i = RT_ELEMENTS(s_aTypes) / 2;
875
876 union
877 {
878 const void *pv;
879 uint64_t u64;
880 PCRTTIMESPEC pTimeSpec;
881 } u;
882
883 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
884
885 /*
886 * Lookup the type - binary search.
887 */
888 for (;;)
889 {
890 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
891 if (!iDiff)
892 break;
893 if (iEnd == iStart)
894 {
895 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
896 return 0;
897 }
898 if (iDiff < 0)
899 iEnd = i - 1;
900 else
901 iStart = i + 1;
902 if (iEnd < iStart)
903 {
904 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
905 return 0;
906 }
907 i = iStart + (iEnd - iStart) / 2;
908 }
909 *ppszFormat += s_aTypes[i].cch - 1;
910
911 /*
912 * Fetch the argument.
913 */
914 u.u64 = 0;
915 switch (s_aTypes[i].cb)
916 {
917 case sizeof(const void *):
918 u.pv = va_arg(*pArgs, const void *);
919 break;
920 default:
921 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
922 break;
923 }
924
925 /*
926 * If it's a pointer, we'll check if it's valid before going on.
927 */
928 if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
929 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
930
931 /*
932 * Format the output.
933 */
934 switch (s_aTypes[i].enmType)
935 {
936 case RTST_TIMESPEC:
937 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%'lld ns", RTTimeSpecGetNano(u.pTimeSpec));
938
939 default:
940 AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
941 break;
942 }
943 break;
944 }
945
946 /*
947 * Invalid/Unknown. Bitch about it.
948 */
949 default:
950 AssertMsgFailed(("Invalid VBox format type '%.10s'!\n", pszFormatOrg));
951 break;
952 }
953 }
954 else
955 AssertMsgFailed(("Invalid VBox format type '%.10s'!\n", pszFormatOrg));
956
957 NOREF(pszFormatOrg);
958 return 0;
959}
960
961
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette