VirtualBox

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

Last change on this file since 38659 was 38659, checked in by vboxsync, 13 years ago

strformat.cpp: Burn fix (todo, fix properly later).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.3 KB
Line 
1/* $Id: strformat.cpp 38659 2011-09-06 14:36:10Z 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.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
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/**@todo #ifndef IN_RC*/
507#ifdef IN_RING3
508 RTUNICP Cp;
509 RTUtf16GetCpEx(&pwszStr, &Cp);
510 char szUtf8[8]; /* Cp=0x7fffffff -> 6 bytes. */
511 char *pszEnd = RTStrPutCp(szUtf8, Cp);
512 cch += pfnOutput(pvArgOutput, szUtf8, pszEnd - szUtf8);
513#else
514 char ch = (char)*pwszStr++;
515 cch += pfnOutput(pvArgOutput, &ch, 1);
516#endif
517 }
518 while (--cchWidth >= 0)
519 cch += pfnOutput(pvArgOutput, " ", 1);
520 }
521 else if (chArgSize == 'L')
522 {
523 /* unicp -> utf8 */
524 int cchStr;
525 PCRTUNICP puszStr = va_arg(args, PCRTUNICP);
526
527 if (!VALID_PTR(puszStr))
528 {
529 static RTUNICP s_uszNull[] = {'<', 'N', 'U', 'L', 'L', '>', '\0' };
530 puszStr = s_uszNull;
531 }
532 cchStr = _strnlenUni(puszStr, (unsigned)cchPrecision);
533 if (!(fFlags & RTSTR_F_LEFT))
534 while (--cchWidth >= cchStr)
535 cch += pfnOutput(pvArgOutput, " ", 1);
536
537 cchWidth -= cchStr;
538 while (cchStr-- > 0)
539 {
540/**@todo #ifndef IN_RC*/
541#ifdef IN_RING3
542 char szUtf8[8]; /* Cp=0x7fffffff -> 6 bytes. */
543 char *pszEnd = RTStrPutCp(szUtf8, *puszStr++);
544 cch += pfnOutput(pvArgOutput, szUtf8, pszEnd - szUtf8);
545#else
546 char ch = (char)*puszStr++;
547 cch += pfnOutput(pvArgOutput, &ch, 1);
548#endif
549 }
550 while (--cchWidth >= 0)
551 cch += pfnOutput(pvArgOutput, " ", 1);
552 }
553 else
554 {
555 int cchStr;
556 const char *pszStr = va_arg(args, char*);
557
558 if (!VALID_PTR(pszStr))
559 pszStr = "<NULL>";
560 cchStr = _strnlen(pszStr, (unsigned)cchPrecision);
561 if (!(fFlags & RTSTR_F_LEFT))
562 while (--cchWidth >= cchStr)
563 cch += pfnOutput(pvArgOutput, " ", 1);
564
565 cch += pfnOutput(pvArgOutput, pszStr, cchStr);
566
567 while (--cchWidth >= cchStr)
568 cch += pfnOutput(pvArgOutput, " ", 1);
569 }
570 break;
571 }
572
573 /*-----------------*/
574 /* integer/pointer */
575 /*-----------------*/
576 case 'd':
577 case 'i':
578 case 'o':
579 case 'p':
580 case 'u':
581 case 'x':
582 case 'X':
583 {
584 char achNum[64]; /* FIXME */
585 int cchNum;
586 uint64_t u64Value;
587
588 switch (pszFormat[-1])
589 {
590 case 'd': /* signed decimal integer */
591 case 'i':
592 fFlags |= RTSTR_F_VALSIGNED;
593 break;
594
595 case 'o':
596 uBase = 8;
597 break;
598
599 case 'p':
600 fFlags |= RTSTR_F_ZEROPAD; /* Note not standard behaviour (but I like it this way!) */
601 uBase = 16;
602 if (cchWidth < 0)
603 cchWidth = sizeof(char *) * 2;
604 break;
605
606 case 'u':
607 uBase = 10;
608 break;
609
610 case 'X':
611 fFlags |= RTSTR_F_CAPITAL;
612 case 'x':
613 uBase = 16;
614 break;
615 }
616
617 if (pszFormat[-1] == 'p')
618 u64Value = va_arg(args, uintptr_t);
619 else if (fFlags & RTSTR_F_VALSIGNED)
620 {
621 if (chArgSize == 'L')
622 {
623 u64Value = va_arg(args, int64_t);
624 fFlags |= RTSTR_F_64BIT;
625 }
626 else if (chArgSize == 'l')
627 {
628 u64Value = va_arg(args, signed long);
629 fFlags |= RTSTR_GET_BIT_FLAG(unsigned long);
630 }
631 else if (chArgSize == 'h')
632 {
633 u64Value = va_arg(args, /* signed short */ int);
634 fFlags |= RTSTR_GET_BIT_FLAG(signed short);
635 }
636 else if (chArgSize == 'H')
637 {
638 u64Value = va_arg(args, /* int8_t */ int);
639 fFlags |= RTSTR_GET_BIT_FLAG(int8_t);
640 }
641 else if (chArgSize == 'j')
642 {
643 u64Value = va_arg(args, /*intmax_t*/ int64_t);
644 fFlags |= RTSTR_F_64BIT;
645 }
646 else if (chArgSize == 'z')
647 {
648 u64Value = va_arg(args, size_t);
649 fFlags |= RTSTR_GET_BIT_FLAG(size_t);
650 }
651 else if (chArgSize == 't')
652 {
653 u64Value = va_arg(args, ptrdiff_t);
654 fFlags |= RTSTR_GET_BIT_FLAG(ptrdiff_t);
655 }
656 else
657 {
658 u64Value = va_arg(args, signed int);
659 fFlags |= RTSTR_GET_BIT_FLAG(signed int);
660 }
661 }
662 else
663 {
664 if (chArgSize == 'L')
665 {
666 u64Value = va_arg(args, uint64_t);
667 fFlags |= RTSTR_F_64BIT;
668 }
669 else if (chArgSize == 'l')
670 {
671 u64Value = va_arg(args, unsigned long);
672 fFlags |= RTSTR_GET_BIT_FLAG(unsigned long);
673 }
674 else if (chArgSize == 'h')
675 {
676 u64Value = va_arg(args, /* unsigned short */ int);
677 fFlags |= RTSTR_GET_BIT_FLAG(unsigned short);
678 }
679 else if (chArgSize == 'H')
680 {
681 u64Value = va_arg(args, /* uint8_t */ int);
682 fFlags |= RTSTR_GET_BIT_FLAG(uint8_t);
683 }
684 else if (chArgSize == 'j')
685 {
686 u64Value = va_arg(args, /*uintmax_t*/ int64_t);
687 fFlags |= RTSTR_F_64BIT;
688 }
689 else if (chArgSize == 'z')
690 {
691 u64Value = va_arg(args, size_t);
692 fFlags |= RTSTR_GET_BIT_FLAG(size_t);
693 }
694 else if (chArgSize == 't')
695 {
696 u64Value = va_arg(args, ptrdiff_t);
697 fFlags |= RTSTR_GET_BIT_FLAG(ptrdiff_t);
698 }
699 else
700 {
701 u64Value = va_arg(args, unsigned int);
702 fFlags |= RTSTR_GET_BIT_FLAG(unsigned int);
703 }
704 }
705 cchNum = RTStrFormatNumber((char *)SSToDS(&achNum), u64Value, uBase, cchWidth, cchPrecision, fFlags);
706 cch += pfnOutput(pvArgOutput, (char *)SSToDS(&achNum), cchNum);
707 break;
708 }
709
710 /*
711 * Nested extensions.
712 */
713 case 'M': /* replace the format string (not stacked yet). */
714 {
715 pszStartOutput = pszFormat = va_arg(args, const char *);
716 AssertPtr(pszStartOutput);
717 break;
718 }
719
720 case 'N': /* real nesting. */
721 {
722 const char *pszFormatNested = va_arg(args, const char *);
723 va_list *pArgsNested = va_arg(args, va_list *);
724 va_list ArgsNested;
725 va_copy(ArgsNested, *pArgsNested);
726 Assert(pszFormatNested);
727 cch += RTStrFormatV(pfnOutput, pvArgOutput, pfnFormat, pvArgFormat, pszFormatNested, ArgsNested);
728 va_end(ArgsNested);
729 break;
730 }
731
732 /*
733 * IPRT Extensions.
734 */
735 case 'R':
736 {
737 if (*pszFormat != '[')
738 {
739 pszFormat--;
740 cch += rtstrFormatRt(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
741 }
742 else
743 {
744 pszFormat--;
745 cch += rtstrFormatType(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
746 }
747 break;
748 }
749
750 /*
751 * Custom format.
752 */
753 default:
754 {
755 if (pfnFormat)
756 {
757 pszFormat--;
758 cch += pfnFormat(pvArgFormat, pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
759 }
760 break;
761 }
762 }
763 pszStartOutput = pszFormat;
764 }
765 }
766 else
767 pszFormat++;
768 }
769
770 /* output pending string. */
771 if (pszStartOutput != pszFormat)
772 cch += pfnOutput(pvArgOutput, pszStartOutput, pszFormat - pszStartOutput);
773
774 /* terminate the output */
775 pfnOutput(pvArgOutput, NULL, 0);
776
777 return cch;
778}
779RT_EXPORT_SYMBOL(RTStrFormatV);
780
781
782/**
783 * Partial implementation of a printf like formatter.
784 * It doesn't do everything correct, and there is no floating point support.
785 * However, it supports custom formats by the means of a format callback.
786 *
787 * @returns number of bytes formatted.
788 * @param pfnOutput Output worker.
789 * Called in two ways. Normally with a string an it's length.
790 * For termination, it's called with NULL for string, 0 for length.
791 * @param pvArgOutput Argument to the output worker.
792 * @param pfnFormat Custom format worker.
793 * @param pvArgFormat Argument to the format worker.
794 * @param pszFormat Format string.
795 * @param ... Argument list.
796 */
797RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...)
798{
799 size_t cch;
800 va_list args;
801 va_start(args, pszFormat);
802 cch = RTStrFormatV(pfnOutput, pvArgOutput, pfnFormat, pvArgFormat, pszFormat, args);
803 va_end(args);
804 return cch;
805}
806RT_EXPORT_SYMBOL(RTStrFormat);
807
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