1 | /* $Id: strformat.cpp 29783 2010-05-25 13:13:35Z 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
|
---|
59 | typedef struct
|
---|
60 | {
|
---|
61 | uint32_t ulLo;
|
---|
62 | uint32_t ulHi;
|
---|
63 | } KSIZE64;
|
---|
64 |
|
---|
65 |
|
---|
66 | /*******************************************************************************
|
---|
67 | * Internal Functions *
|
---|
68 | *******************************************************************************/
|
---|
69 | static unsigned _strnlen(const char *psz, unsigned cchMax);
|
---|
70 | static unsigned _strnlenUtf16(PCRTUTF16 pwsz, unsigned cchMax);
|
---|
71 | static 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 | */
|
---|
80 | static 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 | */
|
---|
97 | static 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 | */
|
---|
128 | static 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 | */
|
---|
150 | RTDECL(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 | }
|
---|
154 | RT_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 | */
|
---|
169 | static 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 | * Determin 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 | */
|
---|
354 | RTDECL(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 | #ifndef IN_RING3
|
---|
486 | case 'S': /* Unicode string as current code page -> Unicode as UTF-8 in GC/R0. */
|
---|
487 | chArgSize = 'l';
|
---|
488 | /* fall thru */
|
---|
489 | #endif
|
---|
490 | case 's': /* Unicode string as utf8 */
|
---|
491 | {
|
---|
492 | if (chArgSize == 'l')
|
---|
493 | {
|
---|
494 | /* utf-16 -> utf-8 */
|
---|
495 | int cchStr;
|
---|
496 | PCRTUTF16 pwszStr = va_arg(args, PRTUTF16);
|
---|
497 |
|
---|
498 | if (!VALID_PTR(pwszStr))
|
---|
499 | {
|
---|
500 | static RTUTF16 s_wszNull[] = {'<', 'N', 'U', 'L', 'L', '>', '\0' };
|
---|
501 | pwszStr = s_wszNull;
|
---|
502 | }
|
---|
503 | cchStr = _strnlenUtf16(pwszStr, (unsigned)cchPrecision);
|
---|
504 | if (!(fFlags & RTSTR_F_LEFT))
|
---|
505 | while (--cchWidth >= cchStr)
|
---|
506 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
507 | cchWidth -= cchStr;
|
---|
508 | while (cchStr-- > 0)
|
---|
509 | {
|
---|
510 | #ifdef IN_RING3
|
---|
511 | RTUNICP Cp;
|
---|
512 | RTUtf16GetCpEx(&pwszStr, &Cp);
|
---|
513 | char szUtf8[8]; /* Cp=0x7fffffff -> 6 bytes. */
|
---|
514 | char *pszEnd = RTStrPutCp(szUtf8, Cp);
|
---|
515 | cch += pfnOutput(pvArgOutput, szUtf8, pszEnd - szUtf8);
|
---|
516 | #else
|
---|
517 | char ch = (char)*pwszStr++;
|
---|
518 | cch += pfnOutput(pvArgOutput, &ch, 1);
|
---|
519 | #endif
|
---|
520 | }
|
---|
521 | while (--cchWidth >= 0)
|
---|
522 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
523 | }
|
---|
524 | else if (chArgSize == 'L')
|
---|
525 | {
|
---|
526 | /* unicp -> utf8 */
|
---|
527 | int cchStr;
|
---|
528 | PCRTUNICP puszStr = va_arg(args, PCRTUNICP);
|
---|
529 |
|
---|
530 | if (!VALID_PTR(puszStr))
|
---|
531 | {
|
---|
532 | static RTUNICP s_uszNull[] = {'<', 'N', 'U', 'L', 'L', '>', '\0' };
|
---|
533 | puszStr = s_uszNull;
|
---|
534 | }
|
---|
535 | cchStr = _strnlenUni(puszStr, (unsigned)cchPrecision);
|
---|
536 | if (!(fFlags & RTSTR_F_LEFT))
|
---|
537 | while (--cchWidth >= cchStr)
|
---|
538 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
539 |
|
---|
540 | cchWidth -= cchStr;
|
---|
541 | while (cchStr-- > 0)
|
---|
542 | {
|
---|
543 | #ifdef IN_RING3
|
---|
544 | char szUtf8[8]; /* Cp=0x7fffffff -> 6 bytes. */
|
---|
545 | char *pszEnd = RTStrPutCp(szUtf8, *puszStr++);
|
---|
546 | cch += pfnOutput(pvArgOutput, szUtf8, pszEnd - szUtf8);
|
---|
547 | #else
|
---|
548 | char ch = (char)*puszStr++;
|
---|
549 | cch += pfnOutput(pvArgOutput, &ch, 1);
|
---|
550 | #endif
|
---|
551 | }
|
---|
552 | while (--cchWidth >= 0)
|
---|
553 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
554 | }
|
---|
555 | else
|
---|
556 | {
|
---|
557 | int cchStr;
|
---|
558 | const char *pszStr = va_arg(args, char*);
|
---|
559 |
|
---|
560 | if (!VALID_PTR(pszStr))
|
---|
561 | pszStr = "<NULL>";
|
---|
562 | cchStr = _strnlen(pszStr, (unsigned)cchPrecision);
|
---|
563 | if (!(fFlags & RTSTR_F_LEFT))
|
---|
564 | while (--cchWidth >= cchStr)
|
---|
565 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
566 |
|
---|
567 | cch += pfnOutput(pvArgOutput, pszStr, cchStr);
|
---|
568 |
|
---|
569 | while (--cchWidth >= cchStr)
|
---|
570 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
571 | }
|
---|
572 | break;
|
---|
573 | }
|
---|
574 |
|
---|
575 | #ifdef IN_RING3
|
---|
576 | case 'S': /* Unicode string as current code page. */
|
---|
577 | {
|
---|
578 | if (chArgSize == 'l')
|
---|
579 | {
|
---|
580 | /* UTF-16 */
|
---|
581 | int cchStr;
|
---|
582 | PCRTUTF16 pwsz2Str = va_arg(args, PRTUTF16);
|
---|
583 | if (!VALID_PTR(pwsz2Str))
|
---|
584 | {
|
---|
585 | static RTUTF16 s_wsz2Null[] = {'<', 'N', 'U', 'L', 'L', '>', '\0' };
|
---|
586 | pwsz2Str = s_wsz2Null;
|
---|
587 | }
|
---|
588 |
|
---|
589 | cchStr = _strnlenUtf16(pwsz2Str, (unsigned)cchPrecision);
|
---|
590 | if (!(fFlags & RTSTR_F_LEFT))
|
---|
591 | while (--cchWidth >= cchStr)
|
---|
592 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
593 |
|
---|
594 | if (cchStr)
|
---|
595 | {
|
---|
596 | /* allocate temporary buffer. */
|
---|
597 | PRTUTF16 pwsz2Tmp = (PRTUTF16)RTMemTmpAlloc((cchStr + 1) * sizeof(RTUTF16));
|
---|
598 | memcpy(pwsz2Tmp, pwsz2Str, cchStr * sizeof(RTUTF16));
|
---|
599 | pwsz2Tmp[cchStr] = '\0';
|
---|
600 |
|
---|
601 | char *pszUtf8;
|
---|
602 | int rc = RTUtf16ToUtf8(pwsz2Tmp, &pszUtf8);
|
---|
603 | if (RT_SUCCESS(rc))
|
---|
604 | {
|
---|
605 | char *pszCurCp;
|
---|
606 | rc = RTStrUtf8ToCurrentCP(&pszCurCp, pszUtf8);
|
---|
607 | if (RT_SUCCESS(rc))
|
---|
608 | {
|
---|
609 | cch += pfnOutput(pvArgOutput, pszCurCp, strlen(pszCurCp));
|
---|
610 | RTStrFree(pszCurCp);
|
---|
611 | }
|
---|
612 | RTStrFree(pszUtf8);
|
---|
613 | }
|
---|
614 | if (RT_FAILURE(rc))
|
---|
615 | while (cchStr-- > 0)
|
---|
616 | cch += pfnOutput(pvArgOutput, "\x7f", 1);
|
---|
617 | RTMemTmpFree(pwsz2Tmp);
|
---|
618 | }
|
---|
619 |
|
---|
620 | while (--cchWidth >= cchStr)
|
---|
621 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
622 | }
|
---|
623 | else if (chArgSize == 'L')
|
---|
624 | {
|
---|
625 | /* UCS-32 */
|
---|
626 | AssertMsgFailed(("Not implemented yet\n"));
|
---|
627 | }
|
---|
628 | else
|
---|
629 | {
|
---|
630 | /* UTF-8 */
|
---|
631 | int cchStr;
|
---|
632 | const char *pszStr = va_arg(args, char *);
|
---|
633 |
|
---|
634 | if (!VALID_PTR(pszStr))
|
---|
635 | pszStr = "<NULL>";
|
---|
636 | cchStr = _strnlen(pszStr, (unsigned)cchPrecision);
|
---|
637 | if (!(fFlags & RTSTR_F_LEFT))
|
---|
638 | while (--cchWidth >= cchStr)
|
---|
639 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
640 |
|
---|
641 | if (cchStr)
|
---|
642 | {
|
---|
643 | /* allocate temporary buffer. */
|
---|
644 | char *pszTmp = (char *)RTMemTmpAlloc(cchStr + 1);
|
---|
645 | memcpy(pszTmp, pszStr, cchStr);
|
---|
646 | pszTmp[cchStr] = '\0';
|
---|
647 |
|
---|
648 | char *pszCurCp;
|
---|
649 | int rc = RTStrUtf8ToCurrentCP(&pszCurCp, pszTmp);
|
---|
650 | if (RT_SUCCESS(rc))
|
---|
651 | {
|
---|
652 | cch += pfnOutput(pvArgOutput, pszCurCp, strlen(pszCurCp));
|
---|
653 | RTStrFree(pszCurCp);
|
---|
654 | }
|
---|
655 | else
|
---|
656 | while (cchStr-- > 0)
|
---|
657 | cch += pfnOutput(pvArgOutput, "\x7f", 1);
|
---|
658 | RTMemTmpFree(pszTmp);
|
---|
659 | }
|
---|
660 |
|
---|
661 | while (--cchWidth >= cchStr)
|
---|
662 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
663 | }
|
---|
664 | break;
|
---|
665 | }
|
---|
666 | #endif
|
---|
667 |
|
---|
668 |
|
---|
669 | /*-----------------*/
|
---|
670 | /* integer/pointer */
|
---|
671 | /*-----------------*/
|
---|
672 | case 'd':
|
---|
673 | case 'i':
|
---|
674 | case 'o':
|
---|
675 | case 'p':
|
---|
676 | case 'u':
|
---|
677 | case 'x':
|
---|
678 | case 'X':
|
---|
679 | {
|
---|
680 | char achNum[64]; /* FIXME */
|
---|
681 | int cchNum;
|
---|
682 | uint64_t u64Value;
|
---|
683 |
|
---|
684 | switch (pszFormat[-1])
|
---|
685 | {
|
---|
686 | case 'd': /* signed decimal integer */
|
---|
687 | case 'i':
|
---|
688 | fFlags |= RTSTR_F_VALSIGNED;
|
---|
689 | break;
|
---|
690 |
|
---|
691 | case 'o':
|
---|
692 | uBase = 8;
|
---|
693 | break;
|
---|
694 |
|
---|
695 | case 'p':
|
---|
696 | fFlags |= RTSTR_F_ZEROPAD; /* Note not standard behaviour (but I like it this way!) */
|
---|
697 | uBase = 16;
|
---|
698 | if (cchWidth < 0)
|
---|
699 | cchWidth = sizeof(char *) * 2;
|
---|
700 | break;
|
---|
701 |
|
---|
702 | case 'u':
|
---|
703 | uBase = 10;
|
---|
704 | break;
|
---|
705 |
|
---|
706 | case 'X':
|
---|
707 | fFlags |= RTSTR_F_CAPITAL;
|
---|
708 | case 'x':
|
---|
709 | uBase = 16;
|
---|
710 | break;
|
---|
711 | }
|
---|
712 |
|
---|
713 | if (pszFormat[-1] == 'p')
|
---|
714 | u64Value = va_arg(args, uintptr_t);
|
---|
715 | else if (fFlags & RTSTR_F_VALSIGNED)
|
---|
716 | {
|
---|
717 | if (chArgSize == 'L')
|
---|
718 | {
|
---|
719 | u64Value = va_arg(args, int64_t);
|
---|
720 | fFlags |= RTSTR_F_64BIT;
|
---|
721 | }
|
---|
722 | else if (chArgSize == 'l')
|
---|
723 | {
|
---|
724 | u64Value = va_arg(args, signed long);
|
---|
725 | fFlags |= RTSTR_GET_BIT_FLAG(unsigned long);
|
---|
726 | }
|
---|
727 | else if (chArgSize == 'h')
|
---|
728 | {
|
---|
729 | u64Value = va_arg(args, /* signed short */ int);
|
---|
730 | fFlags |= RTSTR_GET_BIT_FLAG(signed short);
|
---|
731 | }
|
---|
732 | else if (chArgSize == 'H')
|
---|
733 | {
|
---|
734 | u64Value = va_arg(args, /* int8_t */ int);
|
---|
735 | fFlags |= RTSTR_GET_BIT_FLAG(int8_t);
|
---|
736 | }
|
---|
737 | else if (chArgSize == 'j')
|
---|
738 | {
|
---|
739 | u64Value = va_arg(args, /*intmax_t*/ int64_t);
|
---|
740 | fFlags |= RTSTR_F_64BIT;
|
---|
741 | }
|
---|
742 | else if (chArgSize == 'z')
|
---|
743 | {
|
---|
744 | u64Value = va_arg(args, size_t);
|
---|
745 | fFlags |= RTSTR_GET_BIT_FLAG(size_t);
|
---|
746 | }
|
---|
747 | else if (chArgSize == 't')
|
---|
748 | {
|
---|
749 | u64Value = va_arg(args, ptrdiff_t);
|
---|
750 | fFlags |= RTSTR_GET_BIT_FLAG(ptrdiff_t);
|
---|
751 | }
|
---|
752 | else
|
---|
753 | {
|
---|
754 | u64Value = va_arg(args, signed int);
|
---|
755 | fFlags |= RTSTR_GET_BIT_FLAG(signed int);
|
---|
756 | }
|
---|
757 | }
|
---|
758 | else
|
---|
759 | {
|
---|
760 | if (chArgSize == 'L')
|
---|
761 | {
|
---|
762 | u64Value = va_arg(args, uint64_t);
|
---|
763 | fFlags |= RTSTR_F_64BIT;
|
---|
764 | }
|
---|
765 | else if (chArgSize == 'l')
|
---|
766 | {
|
---|
767 | u64Value = va_arg(args, unsigned long);
|
---|
768 | fFlags |= RTSTR_GET_BIT_FLAG(unsigned long);
|
---|
769 | }
|
---|
770 | else if (chArgSize == 'h')
|
---|
771 | {
|
---|
772 | u64Value = va_arg(args, /* unsigned short */ int);
|
---|
773 | fFlags |= RTSTR_GET_BIT_FLAG(unsigned short);
|
---|
774 | }
|
---|
775 | else if (chArgSize == 'H')
|
---|
776 | {
|
---|
777 | u64Value = va_arg(args, /* uint8_t */ int);
|
---|
778 | fFlags |= RTSTR_GET_BIT_FLAG(uint8_t);
|
---|
779 | }
|
---|
780 | else if (chArgSize == 'j')
|
---|
781 | {
|
---|
782 | u64Value = va_arg(args, /*uintmax_t*/ int64_t);
|
---|
783 | fFlags |= RTSTR_F_64BIT;
|
---|
784 | }
|
---|
785 | else if (chArgSize == 'z')
|
---|
786 | {
|
---|
787 | u64Value = va_arg(args, size_t);
|
---|
788 | fFlags |= RTSTR_GET_BIT_FLAG(size_t);
|
---|
789 | }
|
---|
790 | else if (chArgSize == 't')
|
---|
791 | {
|
---|
792 | u64Value = va_arg(args, ptrdiff_t);
|
---|
793 | fFlags |= RTSTR_GET_BIT_FLAG(ptrdiff_t);
|
---|
794 | }
|
---|
795 | else
|
---|
796 | {
|
---|
797 | u64Value = va_arg(args, unsigned int);
|
---|
798 | fFlags |= RTSTR_GET_BIT_FLAG(unsigned int);
|
---|
799 | }
|
---|
800 | }
|
---|
801 | cchNum = RTStrFormatNumber((char *)SSToDS(&achNum), u64Value, uBase, cchWidth, cchPrecision, fFlags);
|
---|
802 | cch += pfnOutput(pvArgOutput, (char *)SSToDS(&achNum), cchNum);
|
---|
803 | break;
|
---|
804 | }
|
---|
805 |
|
---|
806 | /*
|
---|
807 | * Nested extensions.
|
---|
808 | */
|
---|
809 | case 'M': /* replace the format string (not stacked yet). */
|
---|
810 | {
|
---|
811 | pszStartOutput = pszFormat = va_arg(args, const char *);
|
---|
812 | AssertPtr(pszStartOutput);
|
---|
813 | break;
|
---|
814 | }
|
---|
815 |
|
---|
816 | case 'N': /* real nesting. */
|
---|
817 | {
|
---|
818 | const char *pszFormatNested = va_arg(args, const char *);
|
---|
819 | va_list *pArgsNested = va_arg(args, va_list *);
|
---|
820 | va_list ArgsNested;
|
---|
821 | va_copy(ArgsNested, *pArgsNested);
|
---|
822 | Assert(pszFormatNested);
|
---|
823 | cch += RTStrFormatV(pfnOutput, pvArgOutput, pfnFormat, pvArgFormat, pszFormatNested, ArgsNested);
|
---|
824 | va_end(ArgsNested);
|
---|
825 | break;
|
---|
826 | }
|
---|
827 |
|
---|
828 | /*
|
---|
829 | * IPRT Extensions.
|
---|
830 | */
|
---|
831 | case 'R':
|
---|
832 | {
|
---|
833 | if (*pszFormat != '[')
|
---|
834 | {
|
---|
835 | pszFormat--;
|
---|
836 | cch += rtstrFormatRt(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
|
---|
837 | }
|
---|
838 | else
|
---|
839 | {
|
---|
840 | pszFormat--;
|
---|
841 | cch += rtstrFormatType(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
|
---|
842 | }
|
---|
843 | break;
|
---|
844 | }
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * Custom format.
|
---|
848 | */
|
---|
849 | default:
|
---|
850 | {
|
---|
851 | if (pfnFormat)
|
---|
852 | {
|
---|
853 | pszFormat--;
|
---|
854 | cch += pfnFormat(pvArgFormat, pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize);
|
---|
855 | }
|
---|
856 | break;
|
---|
857 | }
|
---|
858 | }
|
---|
859 | pszStartOutput = pszFormat;
|
---|
860 | }
|
---|
861 | }
|
---|
862 | else
|
---|
863 | pszFormat++;
|
---|
864 | }
|
---|
865 |
|
---|
866 | /* output pending string. */
|
---|
867 | if (pszStartOutput != pszFormat)
|
---|
868 | cch += pfnOutput(pvArgOutput, pszStartOutput, pszFormat - pszStartOutput);
|
---|
869 |
|
---|
870 | /* terminate the output */
|
---|
871 | pfnOutput(pvArgOutput, NULL, 0);
|
---|
872 |
|
---|
873 | return cch;
|
---|
874 | }
|
---|
875 | RT_EXPORT_SYMBOL(RTStrFormatV);
|
---|
876 |
|
---|
877 |
|
---|
878 | /**
|
---|
879 | * Partial implementation of a printf like formatter.
|
---|
880 | * It doesn't do everything correct, and there is no floating point support.
|
---|
881 | * However, it supports custom formats by the means of a format callback.
|
---|
882 | *
|
---|
883 | * @returns number of bytes formatted.
|
---|
884 | * @param pfnOutput Output worker.
|
---|
885 | * Called in two ways. Normally with a string an it's length.
|
---|
886 | * For termination, it's called with NULL for string, 0 for length.
|
---|
887 | * @param pvArgOutput Argument to the output worker.
|
---|
888 | * @param pfnFormat Custom format worker.
|
---|
889 | * @param pvArgFormat Argument to the format worker.
|
---|
890 | * @param pszFormat Format string.
|
---|
891 | * @param ... Argument list.
|
---|
892 | */
|
---|
893 | RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...)
|
---|
894 | {
|
---|
895 | size_t cch;
|
---|
896 | va_list args;
|
---|
897 | va_start(args, pszFormat);
|
---|
898 | cch = RTStrFormatV(pfnOutput, pvArgOutput, pfnFormat, pvArgFormat, pszFormat, args);
|
---|
899 | va_end(args);
|
---|
900 | return cch;
|
---|
901 | }
|
---|
902 | RT_EXPORT_SYMBOL(RTStrFormat);
|
---|
903 |
|
---|