VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrFormatV.c@ 60112

Last change on this file since 60112 was 59941, checked in by vboxsync, 9 years ago

bs3kit: Updates and fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.9 KB
Line 
1/* $Id: bs3-cmn-StrFormatV.c 59941 2016-03-07 15:13:51Z vboxsync $ */
2/** @file
3 * BS3Kit - Bs3StrFormatV
4 */
5
6/*
7 * Copyright (C) 2007-2015 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* Header Files *
30*********************************************************************************************************************************/
31#include "bs3kit-template-header.h"
32#include <iprt/ctype.h>
33
34
35/*********************************************************************************************************************************
36* Defined Constants And Macros *
37*********************************************************************************************************************************/
38#define STR_F_CAPITAL 0x0001
39#define STR_F_LEFT 0x0002
40#define STR_F_ZEROPAD 0x0004
41#define STR_F_SPECIAL 0x0008
42#define STR_F_VALSIGNED 0x0010
43#define STR_F_PLUS 0x0020
44#define STR_F_BLANK 0x0040
45#define STR_F_WIDTH 0x0080
46#define STR_F_PRECISION 0x0100
47#define STR_F_THOUSAND_SEP 0x0200
48#define STR_F_NEGATIVE 0x0400 /**< Used to indicated '-' must be printed. */
49
50
51/*********************************************************************************************************************************
52* Structures and Typedefs *
53*********************************************************************************************************************************/
54/** Size of the temporary buffer. */
55#define BS3FMT_TMP_SIZE 64
56
57/**
58 * BS3kit string format state.
59 */
60typedef struct BS3FMTSTATE
61{
62 /** The output function. */
63 PFNBS3STRFORMATOUTPUT pfnOutput;
64 /** User argument for pfnOutput. */
65 void BS3_FAR *pvUser;
66
67 /** STR_F_XXX flags. */
68 unsigned fFlags;
69 /** The width when STR_F_WIDTH is specific. */
70 int cchWidth;
71 /** The width when STR_F_PRECISION is specific. */
72 int cchPrecision;
73 /** The number format base. */
74 unsigned uBase;
75 /** Temporary buffer. */
76 char szTmp[BS3FMT_TMP_SIZE];
77} BS3FMTSTATE;
78/** Pointer to a BS3Kit string formatter state. */
79typedef BS3FMTSTATE *PBS3FMTSTATE;
80
81
82
83/*********************************************************************************************************************************
84* Internal Functions *
85*********************************************************************************************************************************/
86#if ARCH_BITS != 64
87static size_t bs3StrFormatU32(PBS3FMTSTATE pState, uint32_t uValue);
88#endif
89
90
91
92/**
93 * Formats a number string.
94 *
95 * @returns Number of chars printed.
96 * @param pState The string formatter state.
97 * @param pszNumber The formatted number string.
98 * @param cchNumber The length of the number.
99 */
100static size_t bs3StrFormatNumberString(PBS3FMTSTATE pState, char const *pszNumber, size_t cchNumber)
101{
102 /*
103 * Calc the length of the core number with prefixes.
104 */
105 size_t cchActual = 0;
106 size_t cchRet = cchNumber;
107
108 /* Accunt for sign char. */
109 cchRet += !!(pState->fFlags & (STR_F_NEGATIVE | STR_F_PLUS | STR_F_BLANK));
110
111 /* Account for the hex prefix: '0x' or '0X' */
112 if (pState->fFlags & STR_F_SPECIAL)
113 {
114 cchRet += 2;
115 BS3_ASSERT(pState->uBase == 16);
116 }
117
118 /* Account for thousand separators (applied while printing). */
119 if (pState->fFlags & STR_F_THOUSAND_SEP)
120 cchRet += (cchNumber - 1) / (pState->uBase == 10 ? 3 : 8);
121
122 /*
123 * Do left blank padding.
124 */
125 if ((pState->fFlags & (STR_F_ZEROPAD | STR_F_LEFT | STR_F_WIDTH)) == STR_F_WIDTH)
126 while (cchRet < pState->cchWidth)
127 {
128 cchActual += pState->pfnOutput(' ', pState->pvUser);
129 cchRet++;
130 }
131
132 /*
133 * Sign indicator / space.
134 */
135 if (pState->fFlags & (STR_F_NEGATIVE | STR_F_PLUS | STR_F_BLANK))
136 {
137 char ch;
138 if (pState->fFlags & STR_F_NEGATIVE)
139 ch = '-';
140 else if (pState->fFlags & STR_F_PLUS)
141 ch = '+';
142 else
143 ch = ' ';
144 cchActual += pState->pfnOutput(ch, pState->pvUser);
145 }
146
147 /*
148 * Hex prefix.
149 */
150 if (pState->fFlags & STR_F_SPECIAL)
151 {
152 cchActual += pState->pfnOutput('0', pState->pvUser);
153 cchActual += pState->pfnOutput(!(pState->fFlags & STR_F_CAPITAL) ? 'x' : 'X', pState->pvUser);
154 }
155
156 /*
157 * Zero padding.
158 */
159 if (pState->fFlags & STR_F_ZEROPAD)
160 while (cchRet < pState->cchWidth)
161 {
162 cchActual += pState->pfnOutput('0', pState->pvUser);
163 cchRet++;
164 }
165
166 /*
167 * Output the number.
168 */
169 if ( !(pState->fFlags & STR_F_THOUSAND_SEP)
170 || cchNumber < 4)
171 while (cchNumber-- > 0)
172 cchActual += pState->pfnOutput(*pszNumber++, pState->pvUser);
173 else
174 {
175 char const chSep = pState->uBase == 10 ? ' ' : '\'';
176 unsigned const cchEvery = pState->uBase == 10 ? 3 : 8;
177 unsigned cchLeft = --cchNumber % cchEvery;
178
179 cchActual += pState->pfnOutput(*pszNumber++, pState->pvUser);
180 while (cchNumber-- > 0)
181 {
182 if (cchLeft == 0)
183 {
184 cchActual += pState->pfnOutput(chSep, pState->pvUser);
185 cchLeft = cchEvery;
186 }
187 cchLeft--;
188 cchActual += pState->pfnOutput(*pszNumber++, pState->pvUser);
189 }
190 }
191
192 /*
193 * Do right blank padding.
194 */
195 if ((pState->fFlags & (STR_F_ZEROPAD | STR_F_LEFT | STR_F_WIDTH)) == (STR_F_WIDTH | STR_F_LEFT))
196 while (cchRet < pState->cchWidth)
197 {
198 cchActual += pState->pfnOutput(' ', pState->pvUser);
199 cchRet++;
200 }
201
202 return cchActual;
203}
204
205
206/**
207 * Format a 64-bit number.
208 *
209 * @returns Number of characters.
210 * @param pState The string formatter state.
211 * @param uValue The value.
212 */
213static size_t bs3StrFormatU64(PBS3FMTSTATE pState, uint64_t uValue)
214{
215#if ARCH_BITS != 64
216 /* Avoid 64-bit division by formatting 64-bit numbers as hex if they're higher than _4G. */
217 if ( pState->uBase == 10
218 && !(uValue >> 32)) /* uValue <= UINT32_MAX does not work, trouble with 64-bit compile time math! */
219 return bs3StrFormatU32(pState, uValue);
220 pState->fFlags |= STR_F_SPECIAL;
221 pState->uBase = 16;
222#endif
223
224 {
225 const char *pachDigits = !(pState->fFlags & STR_F_CAPITAL)
226 ? BS3_DATA_NM(g_achBs3HexDigits) : BS3_DATA_NM(g_achBs3HexDigitsUpper);
227 char *psz = &pState->szTmp[BS3FMT_TMP_SIZE];
228
229 *--psz = '\0';
230#if ARCH_BITS == 64
231 if (pState->uBase == 10)
232 {
233 do
234 {
235 *--psz = pachDigits[uValue % 10];
236 uValue /= 10;
237 } while (uValue > 0);
238 }
239 else
240#endif
241 {
242 BS3_ASSERT(pState->uBase == 16);
243 do
244 {
245 *--psz = pachDigits[uValue & 0xf];
246 uValue >>= 4;
247 } while (uValue > 0);
248 }
249 return bs3StrFormatNumberString(pState, psz, &pState->szTmp[BS3FMT_TMP_SIZE - 1] - psz);
250 }
251}
252
253
254/**
255 * Format a 32-bit number.
256 *
257 * @returns Number of characters.
258 * @param pState The string formatter state.
259 * @param uValue The value.
260 */
261static size_t bs3StrFormatU32(PBS3FMTSTATE pState, uint32_t uValue)
262{
263#if ARCH_BITS < 64
264 const char *pachDigits = !(pState->fFlags & STR_F_CAPITAL)
265 ? BS3_DATA_NM(g_achBs3HexDigits) : BS3_DATA_NM(g_achBs3HexDigitsUpper);
266 char *psz = &pState->szTmp[BS3FMT_TMP_SIZE];
267
268 *--psz = '\0';
269 if (pState->uBase == 10)
270 {
271 do
272 {
273 *--psz = pachDigits[uValue % 10];
274 uValue /= 10;
275 } while (uValue > 0);
276 }
277 else
278 {
279 BS3_ASSERT(pState->uBase == 16);
280 do
281 {
282 *--psz = pachDigits[uValue & 0xf];
283 uValue >>= 4;
284 } while (uValue > 0);
285 }
286 return bs3StrFormatNumberString(pState, psz, &pState->szTmp[BS3FMT_TMP_SIZE - 1] - psz);
287
288#else
289 /* We've got native 64-bit division, save space. */
290 return bs3StrFormatU64(pState, uValue);
291#endif
292}
293
294
295#if ARCH_BITS == 16
296/**
297 * Format a 16-bit number.
298 *
299 * @returns Number of characters.
300 * @param pState The string formatter state.
301 * @param uValue The value.
302 */
303static size_t bs3StrFormatU16(PBS3FMTSTATE pState, uint16_t uValue)
304{
305 if (pState->uBase == 10)
306 {
307 const char *pachDigits = !(pState->fFlags & STR_F_CAPITAL)
308 ? BS3_DATA_NM(g_achBs3HexDigits) : BS3_DATA_NM(g_achBs3HexDigitsUpper);
309 char *psz = &pState->szTmp[BS3FMT_TMP_SIZE];
310
311 *--psz = '\0';
312 do
313 {
314 *--psz = pachDigits[uValue % 10];
315 uValue /= 10;
316 } while (uValue > 0);
317 return bs3StrFormatNumberString(pState, psz, &pState->szTmp[BS3FMT_TMP_SIZE - 1] - psz);
318 }
319
320 /*
321 * 32-bit shifting is reasonably cheap and inlined, so combine with 32-bit.
322 */
323 return bs3StrFormatU32(pState, uValue);
324}
325#endif
326
327
328static size_t bs3StrFormatS64(PBS3FMTSTATE pState, int32_t iValue)
329{
330 if (iValue < 0)
331 {
332 iValue = -iValue;
333 pState->fFlags |= STR_F_NEGATIVE;
334 }
335 return bs3StrFormatU64(pState, iValue);
336}
337
338
339static size_t bs3StrFormatS32(PBS3FMTSTATE pState, int32_t iValue)
340{
341 if (iValue < 0)
342 {
343 iValue = -iValue;
344 pState->fFlags |= STR_F_NEGATIVE;
345 }
346 return bs3StrFormatU32(pState, iValue);
347}
348
349
350#if ARCH_BITS == 16
351static size_t bs3StrFormatS16(PBS3FMTSTATE pState, int16_t iValue)
352{
353 if (iValue < 0)
354 {
355 iValue = -iValue;
356 pState->fFlags |= STR_F_NEGATIVE;
357 }
358 return bs3StrFormatU16(pState, iValue);
359}
360#endif
361
362
363BS3_DECL(size_t) Bs3StrFormatV(const char BS3_FAR *pszFormat, va_list va,
364 PFNBS3STRFORMATOUTPUT pfnOutput, void BS3_FAR *pvUser)
365{
366 BS3FMTSTATE State;
367 size_t cchRet = 0;
368 char ch;
369
370 State.pfnOutput = pfnOutput;
371 State.pvUser = pvUser;
372
373 while ((ch = *pszFormat++) != '\0')
374 {
375 char chArgSize;
376
377 /*
378 * Deal with plain chars.
379 */
380 if (ch != '%')
381 {
382 cchRet += State.pfnOutput(ch, State.pvUser);
383 continue;
384 }
385
386 ch = *pszFormat++;
387 if (ch == '%')
388 {
389 cchRet += State.pfnOutput(ch, State.pvUser);
390 continue;
391 }
392
393 /*
394 * Flags.
395 */
396 State.fFlags = 0;
397 for (;;)
398 {
399 unsigned int fThis;
400 switch (ch)
401 {
402 default: fThis = 0; break;
403 case '#': fThis = STR_F_SPECIAL; break;
404 case '-': fThis = STR_F_LEFT; break;
405 case '+': fThis = STR_F_PLUS; break;
406 case ' ': fThis = STR_F_BLANK; break;
407 case '0': fThis = STR_F_ZEROPAD; break;
408 case '\'': fThis = STR_F_THOUSAND_SEP; break;
409 }
410 if (!fThis)
411 break;
412 State.fFlags |= fThis;
413 ch = *pszFormat++;
414 }
415
416 /*
417 * Width.
418 */
419 State.cchWidth = 0;
420 if (RT_C_IS_DIGIT(ch))
421 {
422 do
423 {
424 State.cchWidth *= 10;
425 State.cchWidth = ch - '0';
426 ch = *pszFormat++;
427 } while (RT_C_IS_DIGIT(ch));
428 State.fFlags |= STR_F_WIDTH;
429 }
430 else if (ch == '*')
431 {
432 State.cchWidth = va_arg(va, int);
433 if (State.cchWidth < 0)
434 {
435 State.cchWidth = -State.cchWidth;
436 State.fFlags |= STR_F_LEFT;
437 }
438 State.fFlags |= STR_F_WIDTH;
439 }
440
441 /*
442 * Precision
443 */
444 State.cchPrecision = 0;
445 if (RT_C_IS_DIGIT(ch))
446 {
447 do
448 {
449 State.cchPrecision *= 10;
450 State.cchPrecision = ch - '0';
451 ch = *pszFormat++;
452 } while (RT_C_IS_DIGIT(ch));
453 State.fFlags |= STR_F_PRECISION;
454 }
455 else if (ch == '*')
456 {
457 State.cchPrecision = va_arg(va, int);
458 if (State.cchPrecision < 0)
459 State.cchPrecision = 0;
460 State.fFlags |= STR_F_PRECISION;
461 }
462
463 /*
464 * Argument size.
465 */
466 chArgSize = ch;
467 switch (ch)
468 {
469 default:
470 chArgSize = 0;
471 break;
472
473 case 'z':
474 case 'L':
475 case 'j':
476 case 't':
477 ch = *pszFormat++;
478 break;
479
480 case 'l':
481 ch = *pszFormat++;
482 if (ch == 'l')
483 {
484 chArgSize = 'L';
485 ch = *pszFormat++;
486 }
487 break;
488
489 case 'h':
490 ch = *pszFormat++;
491 if (ch == 'h')
492 {
493 chArgSize = 'H';
494 ch = *pszFormat++;
495 }
496 break;
497 }
498
499 /*
500 * The type.
501 */
502 switch (ch)
503 {
504 /*
505 * Char
506 */
507 case 'c':
508 {
509 char ch = va_arg(va, int /*char*/);
510 cchRet += State.pfnOutput(ch, State.pvUser);
511 break;
512 }
513
514 /*
515 * String.
516 */
517 case 's':
518 {
519 const char BS3_FAR *psz = va_arg(va, const char BS3_FAR *);
520 size_t cch;
521 if (psz != NULL)
522 cch = Bs3StrNLen(psz, State.fFlags & STR_F_PRECISION ? RT_ABS(State.cchPrecision) : ~(size_t)0);
523 else
524 {
525 psz = "<NULL>";
526 cch = 6;
527 }
528
529 if ((State.fFlags & (STR_F_LEFT | STR_F_WIDTH)) == STR_F_WIDTH)
530 while (--State.cchWidth >= cch)
531 cchRet += State.pfnOutput(' ', State.pvUser);
532
533 cchRet += cch;
534 while (cch-- > 0)
535 cchRet += State.pfnOutput(*psz++, State.pvUser);
536
537 if ((State.fFlags & (STR_F_LEFT | STR_F_WIDTH)) == (STR_F_LEFT | STR_F_WIDTH))
538 while (--State.cchWidth >= cch)
539 cchRet += State.pfnOutput(' ', State.pvUser);
540 break;
541 }
542
543 /*
544 * Signed integers.
545 */
546 case 'i':
547 case 'd':
548 State.fFlags &= ~STR_F_SPECIAL;
549 State.fFlags |= STR_F_VALSIGNED;
550 State.uBase = 10;
551 switch (chArgSize)
552 {
553 case 0:
554 case 'h': /* signed short should be promoted to int or be the same as int */
555 case 'H': /* signed char should be promoted to int. */
556 {
557 signed int iValue = va_arg(va, signed int);
558#if ARCH_BITS == 16
559 cchRet += bs3StrFormatS16(&State, iValue);
560#else
561 cchRet += bs3StrFormatS32(&State, iValue);
562#endif
563 break;
564 }
565 case 'l':
566 {
567 signed long lValue = va_arg(va, signed long);
568 if (sizeof(lValue) == 4)
569 cchRet += bs3StrFormatS32(&State, lValue);
570 else
571 cchRet += bs3StrFormatS64(&State, lValue);
572 break;
573 }
574 case 'L':
575 {
576 unsigned long long ullValue = va_arg(va, unsigned long long);
577 cchRet += bs3StrFormatS64(&State, ullValue);
578 break;
579 }
580 }
581 break;
582
583 /*
584 * Unsigned integers.
585 */
586 case 'X':
587 State.fFlags |= STR_F_CAPITAL;
588 case 'x':
589 case 'u':
590 {
591 if (ch == 'u')
592 {
593 State.uBase = 10;
594 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK | STR_F_SPECIAL);
595 }
596 else
597 {
598 State.uBase = 16;
599 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK);
600 }
601 switch (chArgSize)
602 {
603 case 0:
604 case 'h': /* unsigned short should be promoted to int or be the same as int */
605 case 'H': /* unsigned char should be promoted to int. */
606 {
607 unsigned int uValue = va_arg(va, unsigned int);
608#if ARCH_BITS == 16
609 cchRet += bs3StrFormatU16(&State, uValue);
610#else
611 cchRet += bs3StrFormatU32(&State, uValue);
612#endif
613 break;
614 }
615 case 'l':
616 {
617 unsigned long ulValue = va_arg(va, unsigned long);
618 if (sizeof(ulValue) == 4)
619 cchRet += bs3StrFormatU32(&State, ulValue);
620 else
621 cchRet += bs3StrFormatU64(&State, ulValue);
622 break;
623 }
624 case 'L':
625 {
626 unsigned long long ullValue = va_arg(va, unsigned long long);
627 cchRet += bs3StrFormatU64(&State, ullValue);
628 break;
629 }
630 }
631 break;
632 }
633
634 /*
635 * Our stuff.
636 */
637 case 'R':
638 {
639 ch = *pszFormat++;
640 switch (ch)
641 {
642 case 'I':
643 State.fFlags |= STR_F_VALSIGNED;
644 State.uBase &= ~STR_F_SPECIAL;
645 State.uBase = 10;
646 break;
647 case 'U':
648 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK | STR_F_SPECIAL);
649 State.uBase = 10;
650 break;
651 case 'X':
652 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK);
653 State.uBase = 16;
654 break;
655 default:
656 State.uBase = 0;
657 break;
658 }
659 if (State.uBase)
660 {
661 ch = *pszFormat++;
662 switch (ch)
663 {
664#if ARCH_BITS != 16
665 case '3':
666 case '1': /* Will an unsigned 16-bit value always be promoted
667 to a 16-bit unsigned int. It certainly will be promoted to a 32-bit int. */
668 pszFormat++; /* Assumes (1)'6' or (3)'2' */
669#else
670 case '1':
671 pszFormat++; /* Assumes (1)'6' */
672#endif
673 case '8': /* An unsigned 8-bit value should be promoted to int, which is at least 16-bit. */
674 {
675 unsigned int uValue = va_arg(va, unsigned int);
676#if ARCH_BITS == 16
677 cchRet += bs3StrFormatU16(&State, uValue);
678#else
679 cchRet += bs3StrFormatU32(&State, uValue);
680#endif
681 break;
682 }
683#if ARCH_BITS == 16
684 case '3':
685 {
686 uint32_t uValue = va_arg(va, uint32_t);
687 pszFormat++;
688 cchRet += bs3StrFormatU32(&State, uValue);
689 break;
690 }
691#endif
692 case '6':
693 {
694 uint64_t uValue = va_arg(va, uint64_t);
695 pszFormat++;
696 cchRet += bs3StrFormatU64(&State, uValue);
697 break;
698 }
699 }
700 }
701 break;
702 }
703
704 /*
705 * Pointers.
706 */
707 case 'P':
708 State.fFlags |= STR_F_CAPITAL;
709 /* fall thru */
710 case 'p':
711 {
712 void BS3_FAR *pv = va_arg(va, void BS3_FAR *);
713 State.uBase = 16;
714 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK);
715#if ARCH_BITS == 16
716 State.fFlags |= STR_F_ZEROPAD;
717 State.cchWidth = State.fFlags & STR_F_SPECIAL ? 6: 4;
718 cchRet += bs3StrFormatU16(&State, BS3_FP_SEG(pv));
719 cchRet += State.pfnOutput(':', State.pvUser);
720 cchRet += bs3StrFormatU16(&State, BS3_FP_OFF(pv));
721#elif ARCH_BITS == 32
722 State.fFlags |= STR_F_SPECIAL | STR_F_ZEROPAD;
723 State.cchWidth = 10;
724 cchRet += bs3StrFormatU32(&State, (uintptr_t)pv);
725#elif ARCH_BITS == 64
726 State.fFlags |= STR_F_SPECIAL | STR_F_ZEROPAD | STR_F_THOUSAND_SEP;
727 State.cchWidth = 19;
728 cchRet += bs3StrFormatU64(&State, (uintptr_t)pv);
729#else
730# error "Undefined or invalid ARCH_BITS."
731#endif
732 break;
733 }
734
735 }
736 }
737
738 /*
739 * Termination call.
740 */
741 cchRet += State.pfnOutput(0, State.pvUser);
742
743 return cchRet;
744}
745
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