VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strformat.cpp@ 38658

Last change on this file since 38658 was 38658, checked in by vboxsync, 14 years ago

IPRT: , and are now deprecated, the conversion to local codeset being moved to the streams (RTPrintf / RTStrmPrintf). Adding special detection of the windows console and talk UTF-16 to it in order to avoid lost-in-translation issues when its codepage differs from the active codepage of the process.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.2 KB
Line 
1/* $Id: strformat.cpp 38658 2011-09-06 14:22:53Z vboxsync $ */
2/** @file
3 * IPRT - String Formatter.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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
27
28/*******************************************************************************
29* Defined Constants *
30*******************************************************************************/
31#define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
32/*#define MAX(a, b) ((a) >= (b) ? (a) : (b))
33#define MIN(a, b) ((a) < (b) ? (a) : (b)) */
34
35
36/*******************************************************************************
37* Header Files *
38*******************************************************************************/
39#define LOG_GROUP RTLOGGROUP_STRING
40#include <iprt/string.h>
41#include "internal/iprt.h"
42
43#include <iprt/assert.h>
44#ifdef IN_RING3
45# include <iprt/alloc.h>
46# include <iprt/err.h>
47# include <iprt/uni.h>
48#endif
49#include <iprt/string.h>
50#include <iprt/stdarg.h>
51#include "internal/string.h"
52
53/* Wrappers for converting to iprt facilities. */
54#define SSToDS(ptr) ptr
55#define kASSERT Assert
56#define KENDIAN_LITTLE 1
57#define KENDIAN KENDIAN_LITTLE
58#define KSIZE size_t
59typedef struct
60{
61 uint32_t ulLo;
62 uint32_t ulHi;
63} KSIZE64;
64
65
66/*******************************************************************************
67* Internal Functions *
68*******************************************************************************/
69static unsigned _strnlen(const char *psz, unsigned cchMax);
70static unsigned _strnlenUtf16(PCRTUTF16 pwsz, unsigned cchMax);
71static int rtStrFormatNumber(char *psz, KSIZE64 ullValue, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags);
72
73
74/**
75 * Finds the length of a string up to cchMax.
76 * @returns Length.
77 * @param psz Pointer to string.
78 * @param cchMax Max length.
79 */
80static unsigned _strnlen(const char *psz, unsigned cchMax)
81{
82 const char *pszC = psz;
83
84 while (cchMax-- > 0 && *psz != '\0')
85 psz++;
86
87 return (unsigned)(psz - pszC);
88}
89
90
91/**
92 * Finds the length of a string up to cchMax.
93 * @returns Length.
94 * @param pwsz Pointer to string.
95 * @param cchMax Max length.
96 */
97static unsigned _strnlenUtf16(PCRTUTF16 pwsz, unsigned cchMax)
98{
99#ifdef IN_RING3
100 unsigned cwc = 0;
101 while (cchMax-- > 0)
102 {
103 RTUNICP cp;
104 int rc = RTUtf16GetCpEx(&pwsz, &cp);
105 AssertRC(rc);
106 if (RT_FAILURE(rc) || !cp)
107 break;
108 cwc++;
109 }
110 return cwc;
111#else /* !IN_RING3 */
112 PCRTUTF16 pwszC = pwsz;
113
114 while (cchMax-- > 0 && *pwsz != '\0')
115 pwsz++;
116
117 return (unsigned)(pwsz - pwszC);
118#endif /* !IN_RING3 */
119}
120
121
122/**
123 * Finds the length of a string up to cchMax.
124 * @returns Length.
125 * @param pusz Pointer to string.
126 * @param cchMax Max length.
127 */
128static unsigned _strnlenUni(PCRTUNICP pusz, unsigned cchMax)
129{
130 PCRTUNICP puszC = pusz;
131
132 while (cchMax-- > 0 && *pusz != '\0')
133 pusz++;
134
135 return (unsigned)(pusz - puszC);
136}
137
138
139/**
140 * Formats an integer number according to the parameters.
141 *
142 * @returns Length of the formatted number.
143 * @param psz Pointer to output string buffer of sufficient size.
144 * @param u64Value Value to format.
145 * @param uiBase Number representation base.
146 * @param cchWidth Width.
147 * @param cchPrecision Precision.
148 * @param fFlags Flags (NTFS_*).
149 */
150RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags)
151{
152 return rtStrFormatNumber(psz, *(KSIZE64 *)(void *)&u64Value, uiBase, cchWidth, cchPrecision, fFlags);
153}
154RT_EXPORT_SYMBOL(RTStrFormatNumber);
155
156
157
158/**
159 * Formats an integer number according to the parameters.
160 *
161 * @returns Length of the number.
162 * @param psz Pointer to output string.
163 * @param ullValue Value. Using the high part is optional.
164 * @param uiBase Number representation base.
165 * @param cchWidth Width
166 * @param cchPrecision Precision.
167 * @param fFlags Flags (NTFS_*).
168 */
169static int rtStrFormatNumber(char *psz, KSIZE64 ullValue, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags)
170{
171 const char *pachDigits = "0123456789abcdef";
172 char *pszStart = psz;
173 int cchValue;
174 unsigned long ul;
175 int i;
176 int j;
177
178 /*
179 * Validate and adjust input...
180 */
181 Assert(uiBase >= 2 || uiBase <= 16);
182 if (fFlags & RTSTR_F_CAPITAL)
183 pachDigits = "0123456789ABCDEF";
184 if (fFlags & RTSTR_F_LEFT)
185 fFlags &= ~RTSTR_F_ZEROPAD;
186 if ( (fFlags & RTSTR_F_THOUSAND_SEP)
187 && ( uiBase != 10
188 || (fFlags & RTSTR_F_ZEROPAD))) /** @todo implement RTSTR_F_ZEROPAD + RTSTR_F_THOUSAND_SEP. */
189 fFlags &= ~RTSTR_F_THOUSAND_SEP;
190
191 /*
192 * Determine value length
193 */
194 cchValue = 0;
195 if (ullValue.ulHi || (fFlags & RTSTR_F_64BIT))
196 {
197 uint64_t u64 = *(uint64_t *)(void *)&ullValue;
198 if ((fFlags & RTSTR_F_VALSIGNED) && (ullValue.ulHi & 0x80000000))
199 u64 = -(int64_t)u64;
200 do
201 {
202 cchValue++;
203 u64 /= uiBase;
204 } while (u64);
205 }
206 else
207 {
208 ul = (fFlags & RTSTR_F_VALSIGNED) && (ullValue.ulLo & 0x80000000) ? -(int32_t)ullValue.ulLo : ullValue.ulLo;
209 do
210 {
211 cchValue++;
212 ul /= uiBase;
213 } while (ul);
214 }
215 if (fFlags & RTSTR_F_THOUSAND_SEP)
216 {
217 if (cchValue <= 3)
218 fFlags &= ~RTSTR_F_THOUSAND_SEP;
219 else
220 cchValue += cchValue / 3 - (cchValue % 3 == 0);
221 }
222
223 /*
224 * Sign (+/-).
225 */
226 i = 0;
227 if (fFlags & RTSTR_F_VALSIGNED)
228 {
229 if ((ullValue.ulHi || (fFlags & RTSTR_F_64BIT) ? ullValue.ulHi : ullValue.ulLo) & 0x80000000)
230 {
231 ullValue.ulLo = -(int32_t)ullValue.ulLo;
232 if (ullValue.ulHi)
233 ullValue.ulHi = ~ullValue.ulHi;
234 psz[i++] = '-';
235 }
236 else if (fFlags & (RTSTR_F_PLUS | RTSTR_F_BLANK))
237 psz[i++] = (char)(fFlags & RTSTR_F_PLUS ? '+' : ' ');
238 }
239
240 /*
241 * Special (0/0x).
242 */
243 if ((fFlags & RTSTR_F_SPECIAL) && (uiBase % 8) == 0)
244 {
245 psz[i++] = '0';
246 if (uiBase == 16)
247 psz[i++] = (char)(fFlags & RTSTR_F_CAPITAL ? 'X' : 'x');
248 }
249
250 /*
251 * width - only if ZEROPAD
252 */
253 cchWidth -= i + cchValue;
254 if (fFlags & RTSTR_F_ZEROPAD)
255 while (--cchWidth >= 0)
256 {
257 psz[i++] = '0';
258 cchPrecision--;
259 }
260 else if (!(fFlags & RTSTR_F_LEFT) && cchWidth > 0)
261 {
262 for (j = i-1; j >= 0; j--)
263 psz[cchWidth + j] = psz[j];
264 for (j = 0; j < cchWidth; j++)
265 psz[j] = ' ';
266 i += cchWidth;
267 }
268 psz += i;
269
270
271 /*
272 * precision
273 */
274 while (--cchPrecision >= cchValue)
275 *psz++ = '0';
276
277 /*
278 * write number - not good enough but it works
279 */
280 psz += cchValue;
281 i = -1;
282 if (ullValue.ulHi || (fFlags & RTSTR_F_64BIT))
283 {
284 uint64_t u64 = *(uint64_t *)(void *)&ullValue;
285 if (fFlags & RTSTR_F_THOUSAND_SEP)
286 {
287 do
288 {
289 if ((-i - 1) % 4 == 3)
290 psz[i--] = ' ';
291 psz[i--] = pachDigits[u64 % uiBase];
292 u64 /= uiBase;
293 } while (u64);
294 }
295 else
296 {
297 do
298 {
299 psz[i--] = pachDigits[u64 % uiBase];
300 u64 /= uiBase;
301 } while (u64);
302 }
303 }
304 else
305 {
306 ul = (fFlags & RTSTR_F_VALSIGNED) && (ullValue.ulLo & 0x80000000) ? -(int32_t)ullValue.ulLo : ullValue.ulLo;
307 if (fFlags & RTSTR_F_THOUSAND_SEP)
308 {
309 do
310 {
311 if ((-i - 1) % 4 == 3)
312 psz[i--] = ' ';
313 psz[i--] = pachDigits[ul % uiBase];
314 ul /= uiBase;
315 } while (ul);
316 }
317 else
318 {
319 do
320 {
321 psz[i--] = pachDigits[ul % uiBase];
322 ul /= uiBase;
323 } while (ul);
324 }
325 }
326
327 /*
328 * width if RTSTR_F_LEFT
329 */
330 if (fFlags & RTSTR_F_LEFT)
331 while (--cchWidth >= 0)
332 *psz++ = ' ';
333
334 *psz = '\0';
335 return (unsigned)(psz - pszStart);
336}
337
338
339/**
340 * Partial implementation of a printf like formatter.
341 * It doesn't do everything correct, and there is no floating point support.
342 * However, it supports custom formats by the means of a format callback.
343 *
344 * @returns number of bytes formatted.
345 * @param pfnOutput Output worker.
346 * Called in two ways. Normally with a string an it's length.
347 * For termination, it's called with NULL for string, 0 for length.
348 * @param pvArgOutput Argument to the output worker.
349 * @param pfnFormat Custom format worker.
350 * @param pvArgFormat Argument to the format worker.
351 * @param pszFormat Format string.
352 * @param InArgs Argument list.
353 */
354RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
355 const char *pszFormat, va_list InArgs)
356{
357 va_list args;
358 KSIZE cch = 0;
359 const char *pszStartOutput = pszFormat;
360
361 va_copy(args, InArgs); /* make a copy so we can reference it (AMD64 / gcc). */
362
363 while (*pszFormat != '\0')
364 {
365 if (*pszFormat == '%')
366 {
367 /* output pending string. */
368 if (pszStartOutput != pszFormat)
369 cch += pfnOutput(pvArgOutput, pszStartOutput, pszFormat - pszStartOutput);
370
371 /* skip '%' */
372 pszFormat++;
373 if (*pszFormat == '%') /* '%%'-> '%' */
374 pszStartOutput = pszFormat++;
375 else
376 {
377 unsigned int fFlags = 0;
378 int cchWidth = -1;
379 int cchPrecision = -1;
380 unsigned int uBase = 10;
381 char chArgSize;
382
383 /* flags */
384 for (;;)
385 {
386 switch (*pszFormat++)
387 {
388 case '#': fFlags |= RTSTR_F_SPECIAL; continue;
389 case '-': fFlags |= RTSTR_F_LEFT; continue;
390 case '+': fFlags |= RTSTR_F_PLUS; continue;
391 case ' ': fFlags |= RTSTR_F_BLANK; continue;
392 case '0': fFlags |= RTSTR_F_ZEROPAD; continue;
393 case '\'': fFlags |= RTSTR_F_THOUSAND_SEP; continue;
394 }
395 pszFormat--;
396 break;
397 }
398
399 /* width */
400 if (ISDIGIT(*pszFormat))
401 {
402 for (cchWidth = 0; ISDIGIT(*pszFormat); pszFormat++)
403 {
404 cchWidth *= 10;
405 cchWidth += *pszFormat - '0';
406 }
407 fFlags |= RTSTR_F_WIDTH;
408 }
409 else if (*pszFormat == '*')
410 {
411 pszFormat++;
412 cchWidth = va_arg(args, int);
413 if (cchWidth < 0)
414 {
415 cchWidth = -cchWidth;
416 fFlags |= RTSTR_F_LEFT;
417 }
418 fFlags |= RTSTR_F_WIDTH;
419 }
420
421 /* precision */
422 if (*pszFormat == '.')
423 {
424 pszFormat++;
425 if (ISDIGIT(*pszFormat))
426 {
427 for (cchPrecision = 0; ISDIGIT(*pszFormat); pszFormat++)
428 {
429 cchPrecision *= 10;
430 cchPrecision += *pszFormat - '0';
431 }
432
433 }
434 else if (*pszFormat == '*')
435 {
436 pszFormat++;
437 cchPrecision = va_arg(args, int);
438 }
439 if (cchPrecision < 0)
440 cchPrecision = 0;
441 fFlags |= RTSTR_F_PRECISION;
442 }
443
444 /* argsize */
445 chArgSize = *pszFormat;
446 if (chArgSize != 'l' && chArgSize != 'L' && chArgSize != 'h' && chArgSize != 'j' && chArgSize != 'z' && chArgSize != 't')
447 chArgSize = 0;
448 else
449 {
450 pszFormat++;
451 if (*pszFormat == 'l' && chArgSize == 'l')
452 {
453 chArgSize = 'L';
454 pszFormat++;
455 }
456 else if (*pszFormat == 'h' && chArgSize == 'h')
457 {
458 chArgSize = 'H';
459 pszFormat++;
460 }
461 }
462
463 /*
464 * The type.
465 */
466 switch (*pszFormat++)
467 {
468 /* char */
469 case 'c':
470 {
471 char ch;
472
473 if (!(fFlags & RTSTR_F_LEFT))
474 while (--cchWidth > 0)
475 cch += pfnOutput(pvArgOutput, " ", 1);
476
477 ch = (char)va_arg(args, int);
478 cch += pfnOutput(pvArgOutput, SSToDS(&ch), 1);
479
480 while (--cchWidth > 0)
481 cch += pfnOutput(pvArgOutput, " ", 1);
482 break;
483 }
484
485 case 'S': /* Legacy, conversion done by streams now. */
486 case 's':
487 {
488 if (chArgSize == 'l')
489 {
490 /* utf-16 -> utf-8 */
491 int cchStr;
492 PCRTUTF16 pwszStr = va_arg(args, PRTUTF16);
493
494 if (!VALID_PTR(pwszStr))
495 {
496 static RTUTF16 s_wszNull[] = {'<', 'N', 'U', 'L', 'L', '>', '\0' };
497 pwszStr = s_wszNull;
498 }
499 cchStr = _strnlenUtf16(pwszStr, (unsigned)cchPrecision);
500 if (!(fFlags & RTSTR_F_LEFT))
501 while (--cchWidth >= cchStr)
502 cch += pfnOutput(pvArgOutput, " ", 1);
503 cchWidth -= cchStr;
504 while (cchStr-- > 0)
505 {
506#ifndef IN_RC
507 RTUNICP Cp;
508 RTUtf16GetCpEx(&pwszStr, &Cp);
509 char szUtf8[8]; /* Cp=0x7fffffff -> 6 bytes. */
510 char *pszEnd = RTStrPutCp(szUtf8, Cp);
511 cch += pfnOutput(pvArgOutput, szUtf8, pszEnd - szUtf8);
512#else
513 char ch = (char)*pwszStr++;
514 cch += pfnOutput(pvArgOutput, &ch, 1);
515#endif
516 }
517 while (--cchWidth >= 0)
518 cch += pfnOutput(pvArgOutput, " ", 1);
519 }
520 else if (chArgSize == 'L')
521 {
522 /* unicp -> utf8 */
523 int cchStr;
524 PCRTUNICP puszStr = va_arg(args, PCRTUNICP);
525
526 if (!VALID_PTR(puszStr))
527 {
528 static RTUNICP s_uszNull[] = {'<', 'N', 'U', 'L', 'L', '>', '\0' };
529 puszStr = s_uszNull;
530 }
531 cchStr = _strnlenUni(puszStr, (unsigned)cchPrecision);
532 if (!(fFlags & RTSTR_F_LEFT))
533 while (--cchWidth >= cchStr)
534 cch += pfnOutput(pvArgOutput, " ", 1);
535
536 cchWidth -= cchStr;
537 while (cchStr-- > 0)
538 {
539#ifndef IN_RC
540 char szUtf8[8]; /* Cp=0x7fffffff -> 6 bytes. */
541 char *pszEnd = RTStrPutCp(szUtf8, *puszStr++);
542 cch += pfnOutput(pvArgOutput, szUtf8, pszEnd - szUtf8);
543#else
544 char ch = (char)*puszStr++;
545 cch += pfnOutput(pvArgOutput, &ch, 1);
546#endif
547 }
548 while (--cchWidth >= 0)
549 cch += pfnOutput(pvArgOutput, " ", 1);
550 }
551 else
552 {
553 int cchStr;
554 const char *pszStr = va_arg(args, char*);
555
556 if (!VALID_PTR(pszStr))
557 pszStr = "<NULL>";
558 cchStr = _strnlen(pszStr, (unsigned)cchPrecision);
559 if (!(fFlags & RTSTR_F_LEFT))
560 while (--cchWidth >= cchStr)
561 cch += pfnOutput(pvArgOutput, " ", 1);
562
563 cch += pfnOutput(pvArgOutput, pszStr, cchStr);
564
565 while (--cchWidth >= cchStr)
566 cch += pfnOutput(pvArgOutput, " ", 1);
567 }
568 break;
569 }
570
571 /*-----------------*/
572 /* integer/pointer */
573 /*-----------------*/
574 case 'd':
575 case 'i':
576 case 'o':
577 case 'p':
578 case 'u':
579 case 'x':
580 case 'X':
581 {
582 char achNum[64]; /* FIXME */
583 int cchNum;
584 uint64_t u64Value;
585
586 switch (pszFormat[-1])
587 {
588 case 'd': /* signed decimal integer */
589 case 'i':
590 fFlags |= RTSTR_F_VALSIGNED;
591 break;
592
593 case 'o':
594 uBase = 8;
595 break;
596
597 case 'p':
598 fFlags |= RTSTR_F_ZEROPAD; /* Note not standard behaviour (but I like it this way!) */
599 uBase = 16;
600 if (cchWidth < 0)
601 cchWidth = sizeof(char *) * 2;
602 break;
603
604 case 'u':
605 uBase = 10;
606 break;
607
608 case 'X':
609 fFlags |= RTSTR_F_CAPITAL;
610 case 'x':
611 uBase = 16;
612 break;
613 }
614
615 if (pszFormat[-1] == 'p')
616 u64Value = va_arg(args, uintptr_t);
617 else if (fFlags & RTSTR_F_VALSIGNED)
618 {
619 if (chArgSize == 'L')
620 {
621 u64Value = va_arg(args, int64_t);
622 fFlags |= RTSTR_F_64BIT;
623 }
624 else if (chArgSize == 'l')
625 {
626 u64Value = va_arg(args, signed long);
627 fFlags |= RTSTR_GET_BIT_FLAG(unsigned long);
628 }
629 else if (chArgSize == 'h')
630 {
631 u64Value = va_arg(args, /* signed short */ int);
632 fFlags |= RTSTR_GET_BIT_FLAG(signed short);
633 }
634 else if (chArgSize == 'H')
635 {
636 u64Value = va_arg(args, /* int8_t */ int);
637 fFlags |= RTSTR_GET_BIT_FLAG(int8_t);
638 }
639 else if (chArgSize == 'j')
640 {
641 u64Value = va_arg(args, /*intmax_t*/ int64_t);
642 fFlags |= RTSTR_F_64BIT;
643 }
644 else if (chArgSize == 'z')
645 {
646 u64Value = va_arg(args, size_t);
647 fFlags |= RTSTR_GET_BIT_FLAG(size_t);
648 }
649 else if (chArgSize == 't')
650 {
651 u64Value = va_arg(args, ptrdiff_t);
652 fFlags |= RTSTR_GET_BIT_FLAG(ptrdiff_t);
653 }
654 else
655 {
656 u64Value = va_arg(args, signed int);
657 fFlags |= RTSTR_GET_BIT_FLAG(signed int);
658 }
659 }
660 else
661 {
662 if (chArgSize == 'L')
663 {
664 u64Value = va_arg(args, uint64_t);
665 fFlags |= RTSTR_F_64BIT;
666 }
667 else if (chArgSize == 'l')
668 {
669 u64Value = va_arg(args, unsigned long);
670 fFlags |= RTSTR_GET_BIT_FLAG(unsigned long);
671 }
672 else if (chArgSize == 'h')
673 {
674 u64Value = va_arg(args, /* unsigned short */ int);
675 fFlags |= RTSTR_GET_BIT_FLAG(unsigned short);
676 }
677 else if (chArgSize == 'H')
678 {
679 u64Value = va_arg(args, /* uint8_t */ int);
680 fFlags |= RTSTR_GET_BIT_FLAG(uint8_t);
681 }
682 else if (chArgSize == 'j')
683 {
684 u64Value = va_arg(args, /*uintmax_t*/ int64_t);
685 fFlags |= RTSTR_F_64BIT;
686 }
687 else if (chArgSize == 'z')
688 {
689 u64Value = va_arg(args, size_t);
690 fFlags |= RTSTR_GET_BIT_FLAG(size_t);
691 }
692 else if (chArgSize == 't')
693 {
694 u64Value = va_arg(args, ptrdiff_t);
695 fFlags |= RTSTR_GET_BIT_FLAG(ptrdiff_t);
696 }
697 else
698 {
699 u64Value = va_arg(args, unsigned int);
700 fFlags |= RTSTR_GET_BIT_FLAG(unsigned int);
701 }
702 }
703 cchNum = RTStrFormatNumber((char *)SSToDS(&achNum), u64Value, uBase, cchWidth, cchPrecision, fFlags);
704 cch += pfnOutput(pvArgOutput, (char *)SSToDS(&achNum), cchNum);
705 break;
706 }
707
708 /*
709 * Nested extensions.
710 */
711 case 'M': /* replace the format string (not stacked yet). */
712 {
713 pszStartOutput = pszFormat = va_arg(args, const char *);
714 AssertPtr(pszStartOutput);
715 break;
716 }
717
718 case 'N': /* real nesting. */
719 {
720 const char *pszFormatNested = va_arg(args, const char *);
721 va_list *pArgsNested = va_arg(args, va_list *);
722 va_list ArgsNested;
723 va_copy(ArgsNested, *pArgsNested);
724 Assert(pszFormatNested);
725 cch += RTStrFormatV(pfnOutput, pvArgOutput, pfnFormat, pvArgFormat, pszFormatNested, ArgsNested);
726 va_end(ArgsNested);
727 break;
728 }
729
730 /*
731 * IPRT Extensions.
732 */
733 case 'R':
734 {
735 if (*pszFormat != '[')
736 {
737 pszFormat--;
738 cch += rtstrFormatRt(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
739 }
740 else
741 {
742 pszFormat--;
743 cch += rtstrFormatType(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
744 }
745 break;
746 }
747
748 /*
749 * Custom format.
750 */
751 default:
752 {
753 if (pfnFormat)
754 {
755 pszFormat--;
756 cch += pfnFormat(pvArgFormat, pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
757 }
758 break;
759 }
760 }
761 pszStartOutput = pszFormat;
762 }
763 }
764 else
765 pszFormat++;
766 }
767
768 /* output pending string. */
769 if (pszStartOutput != pszFormat)
770 cch += pfnOutput(pvArgOutput, pszStartOutput, pszFormat - pszStartOutput);
771
772 /* terminate the output */
773 pfnOutput(pvArgOutput, NULL, 0);
774
775 return cch;
776}
777RT_EXPORT_SYMBOL(RTStrFormatV);
778
779
780/**
781 * Partial implementation of a printf like formatter.
782 * It doesn't do everything correct, and there is no floating point support.
783 * However, it supports custom formats by the means of a format callback.
784 *
785 * @returns number of bytes formatted.
786 * @param pfnOutput Output worker.
787 * Called in two ways. Normally with a string an it's length.
788 * For termination, it's called with NULL for string, 0 for length.
789 * @param pvArgOutput Argument to the output worker.
790 * @param pfnFormat Custom format worker.
791 * @param pvArgFormat Argument to the format worker.
792 * @param pszFormat Format string.
793 * @param ... Argument list.
794 */
795RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...)
796{
797 size_t cch;
798 va_list args;
799 va_start(args, pszFormat);
800 cch = RTStrFormatV(pfnOutput, pvArgOutput, pfnFormat, pvArgFormat, pszFormat, args);
801 va_end(args);
802 return cch;
803}
804RT_EXPORT_SYMBOL(RTStrFormat);
805
Note: See TracBrowser for help on using the repository browser.

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