VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strtonum.cpp@ 25240

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

Moved RTStrVersionToUInt32 out of strtonum.cpp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.6 KB
Line 
1/* $Id: strtonum.cpp 24662 2009-11-14 23:41:50Z vboxsync $ */
2/** @file
3 * IPRT - String To Number Convertion.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/string.h>
36#include "internal/iprt.h"
37
38#include <iprt/assert.h>
39#include <iprt/ctype.h> /* needed for RT_C_IS_DIGIT */
40#include <iprt/err.h>
41
42
43/*******************************************************************************
44* Global Variables *
45*******************************************************************************/
46/** 8-bit char -> digit. */
47static const unsigned char g_auchDigits[256] =
48{
49 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
50 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255,255,255,255,255,
51 255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,255,255,255,255,255,
52 255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,255,255,255,255,255,
53 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
54 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
55 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
56 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
57};
58/** Approximated overflow shift checks. */
59static const char g_auchShift[36] =
60{
61 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 */
62 64, 64, 63, 63, 62, 62, 62, 62, 61, 61, 61, 61, 61, 61, 61, 61, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 59, 59, 59, 59
63};
64
65/*
66#include <stdio.h>
67int main()
68{
69 int i;
70 printf("static const unsigned char g_auchDigits[256] =\n"
71 "{");
72 for (i = 0; i < 256; i++)
73 {
74 int ch = 255;
75 if (i >= '0' && i <= '9')
76 ch = i - '0';
77 else if (i >= 'a' && i <= 'z')
78 ch = i - 'a' + 10;
79 else if (i >= 'A' && i <= 'Z')
80 ch = i - 'A' + 10;
81 if (i == 0)
82 printf("\n %3d", ch);
83 else if ((i % 32) == 0)
84 printf(",\n %3d", ch);
85 else
86 printf(",%3d", ch);
87 }
88 printf("\n"
89 "};\n");
90 return 0;
91}
92*/
93
94
95/**
96 * Converts a string representation of a number to a 64-bit unsigned number.
97 *
98 * @returns iprt status code.
99 * Warnings are used to indicate convertion problems.
100 * @retval VWRN_NUMBER_TOO_BIG
101 * @retval VWRN_NEGATIVE_UNSIGNED
102 * @retval VWRN_TRAILING_CHARS
103 * @retval VWRN_TRAILING_SPACES
104 * @retval VINF_SUCCESS
105 * @retval VERR_NO_DIGITS
106 *
107 * @param pszValue Pointer to the string value.
108 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
109 * @param uBase The base of the representation used.
110 * If the function will look for known prefixes before defaulting to 10.
111 * @param pu64 Where to store the converted number. (optional)
112 */
113RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64)
114{
115 const char *psz = pszValue;
116 int iShift;
117 int rc;
118 uint64_t u64;
119 unsigned char uch;
120
121 /*
122 * Positive/Negative stuff.
123 */
124 bool fPositive = true;
125 for (;; psz++)
126 {
127 if (*psz == '+')
128 fPositive = true;
129 else if (*psz == '-')
130 fPositive = !fPositive;
131 else
132 break;
133 }
134
135 /*
136 * Check for hex prefix.
137 */
138 if (!uBase)
139 {
140 if ( psz[0] == '0'
141 && (psz[1] == 'x' || psz[1] == 'X')
142 && g_auchDigits[(unsigned char)psz[2]] < 16)
143 {
144 uBase = 16;
145 psz += 2;
146 }
147 else if ( psz[0] == '0'
148 && g_auchDigits[(unsigned char)psz[1]] < 8)
149 {
150 uBase = 8;
151 psz++;
152 }
153 else
154 uBase = 10;
155 }
156 else if ( uBase == 16
157 && psz[0] == '0'
158 && (psz[1] == 'x' || psz[1] == 'X')
159 && g_auchDigits[(unsigned char)psz[2]] < 16)
160 psz += 2;
161
162 /*
163 * Interpret the value.
164 * Note: We only support ascii digits at this time... :-)
165 */
166 iShift = g_auchShift[uBase];
167 pszValue = psz; /* (Prefix and sign doesn't count in the digit counting.) */
168 rc = VINF_SUCCESS;
169 u64 = 0;
170 while ((uch = (unsigned char)*psz) != 0)
171 {
172 unsigned char chDigit = g_auchDigits[uch];
173 uint64_t u64Prev;
174
175 if (chDigit >= uBase)
176 break;
177
178 u64Prev = u64;
179 u64 *= uBase;
180 u64 += chDigit;
181 if (u64Prev > u64 || (u64Prev >> iShift))
182 rc = VWRN_NUMBER_TOO_BIG;
183 psz++;
184 }
185
186 if (!fPositive)
187 {
188 if (rc == VINF_SUCCESS)
189 rc = VWRN_NEGATIVE_UNSIGNED;
190 u64 = -(int64_t)u64;
191 }
192
193 if (pu64)
194 *pu64 = u64;
195
196 if (psz == pszValue)
197 rc = VERR_NO_DIGITS;
198
199 if (ppszNext)
200 *ppszNext = (char *)psz;
201
202 /*
203 * Warn about trailing chars/spaces.
204 */
205 if ( rc == VINF_SUCCESS
206 && *psz)
207 {
208 while (*psz == ' ' || *psz == '\t')
209 psz++;
210 rc = *psz ? VWRN_TRAILING_CHARS : VWRN_TRAILING_SPACES;
211 }
212
213 return rc;
214}
215RT_EXPORT_SYMBOL(RTStrToUInt64Ex);
216
217
218/**
219 * Converts a string representation of a number to a 64-bit unsigned number,
220 * making sure the full string is converted.
221 *
222 * @returns iprt status code.
223 * Warnings are used to indicate convertion problems.
224 * @retval VWRN_NUMBER_TOO_BIG
225 * @retval VWRN_NEGATIVE_UNSIGNED
226 * @retval VINF_SUCCESS
227 * @retval VERR_NO_DIGITS
228 * @retval VERR_TRAILING_SPACES
229 * @retval VERR_TRAILING_CHARS
230 *
231 * @param pszValue Pointer to the string value.
232 * @param uBase The base of the representation used.
233 * If the function will look for known prefixes before defaulting to 10.
234 * @param pu64 Where to store the converted number. (optional)
235 */
236RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64)
237{
238 char *psz;
239 int rc = RTStrToUInt64Ex(pszValue, &psz, uBase, pu64);
240 if (RT_SUCCESS(rc) && *psz)
241 {
242 if (rc == VWRN_TRAILING_CHARS || rc == VWRN_TRAILING_SPACES)
243 rc = -rc;
244 else
245 {
246 while (*psz == ' ' || *psz == '\t')
247 psz++;
248 rc = *psz ? VERR_TRAILING_CHARS : VERR_TRAILING_SPACES;
249 }
250 }
251 return rc;
252}
253RT_EXPORT_SYMBOL(RTStrToUInt64Full);
254
255
256/**
257 * Converts a string representation of a number to a 64-bit unsigned number.
258 * The base is guessed.
259 *
260 * @returns 64-bit unsigned number on success.
261 * @returns 0 on failure.
262 * @param pszValue Pointer to the string value.
263 */
264RTDECL(uint64_t) RTStrToUInt64(const char *pszValue)
265{
266 uint64_t u64;
267 int rc = RTStrToUInt64Ex(pszValue, NULL, 0, &u64);
268 if (RT_SUCCESS(rc))
269 return u64;
270 return 0;
271}
272RT_EXPORT_SYMBOL(RTStrToUInt64);
273
274
275/**
276 * Converts a string representation of a number to a 32-bit unsigned number.
277 *
278 * @returns iprt status code.
279 * Warnings are used to indicate convertion problems.
280 * @retval VWRN_NUMBER_TOO_BIG
281 * @retval VWRN_NEGATIVE_UNSIGNED
282 * @retval VWRN_TRAILING_CHARS
283 * @retval VWRN_TRAILING_SPACES
284 * @retval VINF_SUCCESS
285 * @retval VERR_NO_DIGITS
286 *
287 * @param pszValue Pointer to the string value.
288 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
289 * @param uBase The base of the representation used.
290 * If the function will look for known prefixes before defaulting to 10.
291 * @param pu32 Where to store the converted number. (optional)
292 */
293RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32)
294{
295 uint64_t u64;
296 int rc = RTStrToUInt64Ex(pszValue, ppszNext, uBase, &u64);
297 if (RT_SUCCESS(rc))
298 {
299 if (u64 & ~0xffffffffULL)
300 rc = VWRN_NUMBER_TOO_BIG;
301 }
302 if (pu32)
303 *pu32 = (uint32_t)u64;
304 return rc;
305}
306RT_EXPORT_SYMBOL(RTStrToUInt32Ex);
307
308
309/**
310 * Converts a string representation of a number to a 32-bit unsigned number,
311 * making sure the full string is converted.
312 *
313 * @returns iprt status code.
314 * Warnings are used to indicate convertion problems.
315 * @retval VWRN_NUMBER_TOO_BIG
316 * @retval VWRN_NEGATIVE_UNSIGNED
317 * @retval VINF_SUCCESS
318 * @retval VERR_NO_DIGITS
319 * @retval VERR_TRAILING_SPACES
320 * @retval VERR_TRAILING_CHARS
321 *
322 * @param pszValue Pointer to the string value.
323 * @param uBase The base of the representation used.
324 * If the function will look for known prefixes before defaulting to 10.
325 * @param pu32 Where to store the converted number. (optional)
326 */
327RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32)
328{
329 uint64_t u64;
330 int rc = RTStrToUInt64Full(pszValue, uBase, &u64);
331 if (RT_SUCCESS(rc))
332 {
333 if (u64 & ~0xffffffffULL)
334 rc = VWRN_NUMBER_TOO_BIG;
335 }
336 if (pu32)
337 *pu32 = (uint32_t)u64;
338 return rc;
339}
340RT_EXPORT_SYMBOL(RTStrToUInt32Full);
341
342
343/**
344 * Converts a string representation of a number to a 64-bit unsigned number.
345 * The base is guessed.
346 *
347 * @returns 32-bit unsigned number on success.
348 * @returns 0 on failure.
349 * @param pszValue Pointer to the string value.
350 */
351RTDECL(uint32_t) RTStrToUInt32(const char *pszValue)
352{
353 uint32_t u32;
354 int rc = RTStrToUInt32Ex(pszValue, NULL, 0, &u32);
355 if (RT_SUCCESS(rc))
356 return u32;
357 return 0;
358}
359RT_EXPORT_SYMBOL(RTStrToUInt32);
360
361
362/**
363 * Converts a string representation of a number to a 16-bit unsigned number.
364 *
365 * @returns iprt status code.
366 * Warnings are used to indicate convertion problems.
367 * @retval VWRN_NUMBER_TOO_BIG
368 * @retval VWRN_NEGATIVE_UNSIGNED
369 * @retval VWRN_TRAILING_CHARS
370 * @retval VWRN_TRAILING_SPACES
371 * @retval VINF_SUCCESS
372 * @retval VERR_NO_DIGITS
373 *
374 * @param pszValue Pointer to the string value.
375 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
376 * @param uBase The base of the representation used.
377 * If the function will look for known prefixes before defaulting to 10.
378 * @param pu16 Where to store the converted number. (optional)
379 */
380RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16)
381{
382 uint64_t u64;
383 int rc = RTStrToUInt64Ex(pszValue, ppszNext, uBase, &u64);
384 if (RT_SUCCESS(rc))
385 {
386 if (u64 & ~0xffffULL)
387 rc = VWRN_NUMBER_TOO_BIG;
388 }
389 if (pu16)
390 *pu16 = (uint16_t)u64;
391 return rc;
392}
393RT_EXPORT_SYMBOL(RTStrToUInt16Ex);
394
395
396/**
397 * Converts a string representation of a number to a 16-bit unsigned number,
398 * making sure the full string is converted.
399 *
400 * @returns iprt status code.
401 * Warnings are used to indicate convertion problems.
402 * @retval VWRN_NUMBER_TOO_BIG
403 * @retval VWRN_NEGATIVE_UNSIGNED
404 * @retval VINF_SUCCESS
405 * @retval VERR_NO_DIGITS
406 * @retval VERR_TRAILING_SPACES
407 * @retval VERR_TRAILING_CHARS
408 *
409 * @param pszValue Pointer to the string value.
410 * @param uBase The base of the representation used.
411 * If the function will look for known prefixes before defaulting to 10.
412 * @param pu16 Where to store the converted number. (optional)
413 */
414RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16)
415{
416 uint64_t u64;
417 int rc = RTStrToUInt64Full(pszValue, uBase, &u64);
418 if (RT_SUCCESS(rc))
419 {
420 if (u64 & ~0xffffULL)
421 rc = VWRN_NUMBER_TOO_BIG;
422 }
423 if (pu16)
424 *pu16 = (uint16_t)u64;
425 return rc;
426}
427RT_EXPORT_SYMBOL(RTStrToUInt16Full);
428
429
430/**
431 * Converts a string representation of a number to a 16-bit unsigned number.
432 * The base is guessed.
433 *
434 * @returns 16-bit unsigned number on success.
435 * @returns 0 on failure.
436 * @param pszValue Pointer to the string value.
437 */
438RTDECL(uint16_t) RTStrToUInt16(const char *pszValue)
439{
440 uint16_t u16;
441 int rc = RTStrToUInt16Ex(pszValue, NULL, 0, &u16);
442 if (RT_SUCCESS(rc))
443 return u16;
444 return 0;
445}
446RT_EXPORT_SYMBOL(RTStrToUInt16);
447
448
449/**
450 * Converts a string representation of a number to a 8-bit unsigned number.
451 *
452 * @returns iprt status code.
453 * Warnings are used to indicate convertion problems.
454 * @retval VWRN_NUMBER_TOO_BIG
455 * @retval VWRN_NEGATIVE_UNSIGNED
456 * @retval VWRN_TRAILING_CHARS
457 * @retval VWRN_TRAILING_SPACES
458 * @retval VINF_SUCCESS
459 * @retval VERR_NO_DIGITS
460 *
461 * @param pszValue Pointer to the string value.
462 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
463 * @param uBase The base of the representation used.
464 * If the function will look for known prefixes before defaulting to 10.
465 * @param pu8 Where to store the converted number. (optional)
466 */
467RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8)
468{
469 uint64_t u64;
470 int rc = RTStrToUInt64Ex(pszValue, ppszNext, uBase, &u64);
471 if (RT_SUCCESS(rc))
472 {
473 if (u64 & ~0xffULL)
474 rc = VWRN_NUMBER_TOO_BIG;
475 }
476 if (pu8)
477 *pu8 = (uint8_t)u64;
478 return rc;
479}
480RT_EXPORT_SYMBOL(RTStrToUInt8Ex);
481
482
483/**
484 * Converts a string representation of a number to a 8-bit unsigned number,
485 * making sure the full string is converted.
486 *
487 * @returns iprt status code.
488 * Warnings are used to indicate convertion problems.
489 * @retval VWRN_NUMBER_TOO_BIG
490 * @retval VWRN_NEGATIVE_UNSIGNED
491 * @retval VINF_SUCCESS
492 * @retval VERR_NO_DIGITS
493 * @retval VERR_TRAILING_SPACES
494 * @retval VERR_TRAILING_CHARS
495 *
496 * @param pszValue Pointer to the string value.
497 * @param uBase The base of the representation used.
498 * If the function will look for known prefixes before defaulting to 10.
499 * @param pu8 Where to store the converted number. (optional)
500 */
501RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8)
502{
503 uint64_t u64;
504 int rc = RTStrToUInt64Full(pszValue, uBase, &u64);
505 if (RT_SUCCESS(rc))
506 {
507 if (u64 & ~0xffULL)
508 rc = VWRN_NUMBER_TOO_BIG;
509 }
510 if (pu8)
511 *pu8 = (uint8_t)u64;
512 return rc;
513}
514RT_EXPORT_SYMBOL(RTStrToUInt8Full);
515
516
517/**
518 * Converts a string representation of a number to a 8-bit unsigned number.
519 * The base is guessed.
520 *
521 * @returns 8-bit unsigned number on success.
522 * @returns 0 on failure.
523 * @param pszValue Pointer to the string value.
524 */
525RTDECL(uint8_t) RTStrToUInt8(const char *pszValue)
526{
527 uint8_t u8;
528 int rc = RTStrToUInt8Ex(pszValue, NULL, 0, &u8);
529 if (RT_SUCCESS(rc))
530 return u8;
531 return 0;
532}
533RT_EXPORT_SYMBOL(RTStrToUInt8);
534
535
536
537
538
539
540
541/**
542 * Converts a string representation of a number to a 64-bit signed number.
543 *
544 * @returns iprt status code.
545 * Warnings are used to indicate convertion problems.
546 * @retval VWRN_NUMBER_TOO_BIG
547 * @retval VWRN_TRAILING_CHARS
548 * @retval VWRN_TRAILING_SPACES
549 * @retval VINF_SUCCESS
550 * @retval VERR_NO_DIGITS
551 *
552 * @param pszValue Pointer to the string value.
553 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
554 * @param uBase The base of the representation used.
555 * If the function will look for known prefixes before defaulting to 10.
556 * @param pi64 Where to store the converted number. (optional)
557 */
558RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64)
559{
560 const char *psz = pszValue;
561 int iShift;
562 int rc;
563 int64_t i64;
564 unsigned char uch;
565
566 /*
567 * Positive/Negative stuff.
568 */
569 bool fPositive = true;
570 for (;; psz++)
571 {
572 if (*psz == '+')
573 fPositive = true;
574 else if (*psz == '-')
575 fPositive = !fPositive;
576 else
577 break;
578 }
579
580 /*
581 * Check for hex prefix.
582 */
583 if (!uBase)
584 {
585 if ( *psz == '0'
586 && (psz[1] == 'x' || psz[1] == 'X')
587 && g_auchDigits[(unsigned char)psz[2]] < 16)
588 {
589 uBase = 16;
590 psz += 2;
591 }
592 else if ( *psz == '0'
593 && g_auchDigits[(unsigned char)psz[1]] < 8)
594 {
595 uBase = 8;
596 psz++;
597 }
598 else
599 uBase = 10;
600 }
601 else if ( uBase == 16
602 && *psz == '0'
603 && (psz[1] == 'x' || psz[1] == 'X')
604 && g_auchDigits[(unsigned char)psz[2]] < 16)
605 psz += 2;
606
607 /*
608 * Interpret the value.
609 * Note: We only support ascii digits at this time... :-)
610 */
611 iShift = g_auchShift[uBase]; /** @todo test this, it's probably not 100% right yet. */
612 pszValue = psz; /* (Prefix and sign doesn't count in the digit counting.) */
613 rc = VINF_SUCCESS;
614 i64 = 0;
615 while ((uch = (unsigned char)*psz) != 0)
616 {
617 unsigned char chDigit = g_auchDigits[uch];
618 int64_t i64Prev;
619
620 if (chDigit >= uBase)
621 break;
622
623 i64Prev = i64;
624 i64 *= uBase;
625 i64 += chDigit;
626 if (i64Prev > i64 || (i64Prev >> iShift))
627 rc = VWRN_NUMBER_TOO_BIG;
628 psz++;
629 }
630
631 if (!fPositive)
632 i64 = -i64;
633
634 if (pi64)
635 *pi64 = i64;
636
637 if (psz == pszValue)
638 rc = VERR_NO_DIGITS;
639
640 if (ppszNext)
641 *ppszNext = (char *)psz;
642
643 /*
644 * Warn about trailing chars/spaces.
645 */
646 if ( rc == VINF_SUCCESS
647 && *psz)
648 {
649 while (*psz == ' ' || *psz == '\t')
650 psz++;
651 rc = *psz ? VWRN_TRAILING_CHARS : VWRN_TRAILING_SPACES;
652 }
653
654 return rc;
655}
656RT_EXPORT_SYMBOL(RTStrToInt64Ex);
657
658
659/**
660 * Converts a string representation of a number to a 64-bit signed number,
661 * making sure the full string is converted.
662 *
663 * @returns iprt status code.
664 * Warnings are used to indicate convertion problems.
665 * @retval VWRN_NUMBER_TOO_BIG
666 * @retval VINF_SUCCESS
667 * @retval VERR_TRAILING_CHARS
668 * @retval VERR_TRAILING_SPACES
669 * @retval VERR_NO_DIGITS
670 *
671 * @param pszValue Pointer to the string value.
672 * @param uBase The base of the representation used.
673 * If the function will look for known prefixes before defaulting to 10.
674 * @param pi64 Where to store the converted number. (optional)
675 */
676RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64)
677{
678 char *psz;
679 int rc = RTStrToInt64Ex(pszValue, &psz, uBase, pi64);
680 if (RT_SUCCESS(rc) && *psz)
681 {
682 if (rc == VWRN_TRAILING_CHARS || rc == VWRN_TRAILING_SPACES)
683 rc = -rc;
684 else
685 {
686 while (*psz == ' ' || *psz == '\t')
687 psz++;
688 rc = *psz ? VERR_TRAILING_CHARS : VERR_TRAILING_SPACES;
689 }
690 }
691 return rc;
692}
693RT_EXPORT_SYMBOL(RTStrToInt64Full);
694
695
696/**
697 * Converts a string representation of a number to a 64-bit signed number.
698 * The base is guessed.
699 *
700 * @returns 64-bit signed number on success.
701 * @returns 0 on failure.
702 * @param pszValue Pointer to the string value.
703 */
704RTDECL(int64_t) RTStrToInt64(const char *pszValue)
705{
706 int64_t i64;
707 int rc = RTStrToInt64Ex(pszValue, NULL, 0, &i64);
708 if (RT_SUCCESS(rc))
709 return i64;
710 return 0;
711}
712RT_EXPORT_SYMBOL(RTStrToInt64);
713
714
715/**
716 * Converts a string representation of a number to a 32-bit signed number.
717 *
718 * @returns iprt status code.
719 * Warnings are used to indicate convertion problems.
720 * @retval VWRN_NUMBER_TOO_BIG
721 * @retval VWRN_TRAILING_CHARS
722 * @retval VWRN_TRAILING_SPACES
723 * @retval VINF_SUCCESS
724 * @retval VERR_NO_DIGITS
725 *
726 * @param pszValue Pointer to the string value.
727 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
728 * @param uBase The base of the representation used.
729 * If the function will look for known prefixes before defaulting to 10.
730 * @param pi32 Where to store the converted number. (optional)
731 */
732RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32)
733{
734 int64_t i64;
735 int rc = RTStrToInt64Ex(pszValue, ppszNext, uBase, &i64);
736 if (RT_SUCCESS(rc))
737 {
738 int32_t i32 = (int32_t)i64;
739 if (i64 != (int64_t)i32)
740 rc = VWRN_NUMBER_TOO_BIG;
741 }
742 if (pi32)
743 *pi32 = (int32_t)i64;
744 return rc;
745}
746RT_EXPORT_SYMBOL(RTStrToInt32Ex);
747
748
749/**
750 * Converts a string representation of a number to a 32-bit signed number,
751 * making sure the full string is converted.
752 *
753 * @returns iprt status code.
754 * Warnings are used to indicate convertion problems.
755 * @retval VWRN_NUMBER_TOO_BIG
756 * @retval VINF_SUCCESS
757 * @retval VERR_TRAILING_CHARS
758 * @retval VERR_TRAILING_SPACES
759 * @retval VERR_NO_DIGITS
760 *
761 * @param pszValue Pointer to the string value.
762 * @param uBase The base of the representation used.
763 * If the function will look for known prefixes before defaulting to 10.
764 * @param pi32 Where to store the converted number. (optional)
765 */
766RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32)
767{
768 int64_t i64;
769 int rc = RTStrToInt64Full(pszValue, uBase, &i64);
770 if (RT_SUCCESS(rc))
771 {
772 int32_t i32 = (int32_t)i64;
773 if (i64 != (int64_t)i32)
774 rc = VWRN_NUMBER_TOO_BIG;
775 }
776 if (pi32)
777 *pi32 = (int32_t)i64;
778 return rc;
779}
780RT_EXPORT_SYMBOL(RTStrToInt32Full);
781
782
783/**
784 * Converts a string representation of a number to a 32-bit signed number.
785 * The base is guessed.
786 *
787 * @returns 32-bit signed number on success.
788 * @returns 0 on failure.
789 * @param pszValue Pointer to the string value.
790 */
791RTDECL(int32_t) RTStrToInt32(const char *pszValue)
792{
793 int32_t i32;
794 int rc = RTStrToInt32Ex(pszValue, NULL, 0, &i32);
795 if (RT_SUCCESS(rc))
796 return i32;
797 return 0;
798}
799RT_EXPORT_SYMBOL(RTStrToInt32);
800
801
802/**
803 * Converts a string representation of a number to a 16-bit signed number.
804 *
805 * @returns iprt status code.
806 * Warnings are used to indicate convertion problems.
807 * @retval VWRN_NUMBER_TOO_BIG
808 * @retval VWRN_TRAILING_CHARS
809 * @retval VWRN_TRAILING_SPACES
810 * @retval VINF_SUCCESS
811 * @retval VERR_NO_DIGITS
812 *
813 * @param pszValue Pointer to the string value.
814 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
815 * @param uBase The base of the representation used.
816 * If the function will look for known prefixes before defaulting to 10.
817 * @param pi16 Where to store the converted number. (optional)
818 */
819RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16)
820{
821 int64_t i64;
822 int rc = RTStrToInt64Ex(pszValue, ppszNext, uBase, &i64);
823 if (RT_SUCCESS(rc))
824 {
825 int16_t i16 = (int16_t)i64;
826 if (i64 != (int64_t)i16)
827 rc = VWRN_NUMBER_TOO_BIG;
828 }
829 if (pi16)
830 *pi16 = (int16_t)i64;
831 return rc;
832}
833RT_EXPORT_SYMBOL(RTStrToInt16Ex);
834
835
836/**
837 * Converts a string representation of a number to a 16-bit signed number,
838 * making sure the full string is converted.
839 *
840 * @returns iprt status code.
841 * Warnings are used to indicate convertion problems.
842 * @retval VWRN_NUMBER_TOO_BIG
843 * @retval VINF_SUCCESS
844 * @retval VERR_TRAILING_CHARS
845 * @retval VERR_TRAILING_SPACES
846 * @retval VERR_NO_DIGITS
847 *
848 * @param pszValue Pointer to the string value.
849 * @param uBase The base of the representation used.
850 * If the function will look for known prefixes before defaulting to 10.
851 * @param pi16 Where to store the converted number. (optional)
852 */
853RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16)
854{
855 int64_t i64;
856 int rc = RTStrToInt64Full(pszValue, uBase, &i64);
857 if (RT_SUCCESS(rc))
858 {
859 int16_t i16 = (int16_t)i64;
860 if (i64 != (int64_t)i16)
861 rc = VWRN_NUMBER_TOO_BIG;
862 }
863 if (pi16)
864 *pi16 = (int16_t)i64;
865 return rc;
866}
867RT_EXPORT_SYMBOL(RTStrToInt16Full);
868
869
870/**
871 * Converts a string representation of a number to a 16-bit signed number.
872 * The base is guessed.
873 *
874 * @returns 16-bit signed number on success.
875 * @returns 0 on failure.
876 * @param pszValue Pointer to the string value.
877 */
878RTDECL(int16_t) RTStrToInt16(const char *pszValue)
879{
880 int16_t i16;
881 int rc = RTStrToInt16Ex(pszValue, NULL, 0, &i16);
882 if (RT_SUCCESS(rc))
883 return i16;
884 return 0;
885}
886RT_EXPORT_SYMBOL(RTStrToInt16);
887
888
889/**
890 * Converts a string representation of a number to a 8-bit signed number.
891 *
892 * @returns iprt status code.
893 * Warnings are used to indicate convertion problems.
894 * @retval VWRN_NUMBER_TOO_BIG
895 * @retval VWRN_TRAILING_CHARS
896 * @retval VWRN_TRAILING_SPACES
897 * @retval VINF_SUCCESS
898 * @retval VERR_NO_DIGITS
899 *
900 * @param pszValue Pointer to the string value.
901 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
902 * @param uBase The base of the representation used.
903 * If the function will look for known prefixes before defaulting to 10.
904 * @param pi8 Where to store the converted number. (optional)
905 */
906RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8)
907{
908 int64_t i64;
909 int rc = RTStrToInt64Ex(pszValue, ppszNext, uBase, &i64);
910 if (RT_SUCCESS(rc))
911 {
912 int8_t i8 = (int8_t)i64;
913 if (i64 != (int64_t)i8)
914 rc = VWRN_NUMBER_TOO_BIG;
915 }
916 if (pi8)
917 *pi8 = (int8_t)i64;
918 return rc;
919}
920RT_EXPORT_SYMBOL(RTStrToInt8Ex);
921
922
923/**
924 * Converts a string representation of a number to a 8-bit signed number,
925 * making sure the full string is converted.
926 *
927 * @returns iprt status code.
928 * Warnings are used to indicate convertion problems.
929 * @retval VWRN_NUMBER_TOO_BIG
930 * @retval VINF_SUCCESS
931 * @retval VERR_TRAILING_CHARS
932 * @retval VERR_TRAILING_SPACES
933 * @retval VERR_NO_DIGITS
934 *
935 * @param pszValue Pointer to the string value.
936 * @param uBase The base of the representation used.
937 * If the function will look for known prefixes before defaulting to 10.
938 * @param pi8 Where to store the converted number. (optional)
939 */
940RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8)
941{
942 int64_t i64;
943 int rc = RTStrToInt64Full(pszValue, uBase, &i64);
944 if (RT_SUCCESS(rc))
945 {
946 int8_t i8 = (int8_t)i64;
947 if (i64 != (int64_t)i8)
948 rc = VWRN_NUMBER_TOO_BIG;
949 }
950 if (pi8)
951 *pi8 = (int8_t)i64;
952 return rc;
953}
954RT_EXPORT_SYMBOL(RTStrToInt8Full);
955
956
957/**
958 * Converts a string representation of a number to a 8-bit signed number.
959 * The base is guessed.
960 *
961 * @returns 8-bit signed number on success.
962 * @returns 0 on failure.
963 * @param pszValue Pointer to the string value.
964 */
965RTDECL(int8_t) RTStrToInt8(const char *pszValue)
966{
967 int8_t i8;
968 int rc = RTStrToInt8Ex(pszValue, NULL, 0, &i8);
969 if (RT_SUCCESS(rc))
970 return i8;
971 return 0;
972}
973RT_EXPORT_SYMBOL(RTStrToInt8);
974
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