[e8d59ca] | 1 | /* |
---|
| 2 | * COPYRIGHT (c) 2009. |
---|
| 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
| 8 | * |
---|
| 9 | * $Id$ |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <errno.h> |
---|
| 13 | #include <stdlib.h> |
---|
| 14 | #include <limits.h> |
---|
| 15 | #include <stdbool.h> |
---|
| 16 | |
---|
| 17 | #include <math.h> |
---|
| 18 | |
---|
| 19 | /* |
---|
| 20 | * This file is designed to be included multiple times to instantiate |
---|
| 21 | * it and should NOT be protected against multiple inclusions. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #if !defined(STRING_TO_FLOAT) && !defined(STRING_TO_INTEGER) |
---|
| 25 | #error "Neither STRING_TO_FLOAT nor STRING_TO_INTEGER defined" |
---|
| 26 | #endif |
---|
| 27 | |
---|
| 28 | #if defined(STRING_TO_FLOAT) && defined(STRING_TO_INTEGER) |
---|
| 29 | #error "Both STRING_TO_FLOAT nor STRING_TO_INTEGER defined" |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | #ifndef STRING_TO_TYPE |
---|
| 33 | #error "STRING_TO_TYPE not defined" |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | #ifndef STRING_TO_NAME |
---|
| 37 | #error "STRING_TO_NAME not defined" |
---|
| 38 | #endif |
---|
| 39 | |
---|
| 40 | #ifndef STRING_TO_METHOD |
---|
| 41 | #error "STRING_TO_METHOD not defined" |
---|
| 42 | #endif |
---|
| 43 | |
---|
| 44 | #ifndef STRING_TO_MAX |
---|
| 45 | #error "STRING_TO_MAX not defined" |
---|
| 46 | #endif |
---|
| 47 | |
---|
| 48 | #undef ZERO |
---|
| 49 | #ifdef STRING_TO_FLOAT |
---|
| 50 | #define ZERO 0.0 |
---|
| 51 | #elif defined(STRING_TO_INTEGER) |
---|
| 52 | #define ZERO 0 |
---|
| 53 | #endif |
---|
| 54 | |
---|
| 55 | bool STRING_TO_NAME ( |
---|
| 56 | const char *s, |
---|
| 57 | STRING_TO_TYPE *n, |
---|
| 58 | char **endptr |
---|
| 59 | #if defined(STRING_TO_INTEGER) |
---|
| 60 | , |
---|
| 61 | int base |
---|
| 62 | #endif |
---|
| 63 | ) |
---|
| 64 | { |
---|
| 65 | STRING_TO_TYPE result; |
---|
| 66 | char *end; |
---|
| 67 | |
---|
| 68 | if ( !n ) |
---|
| 69 | return false; |
---|
| 70 | |
---|
| 71 | errno = 0; |
---|
| 72 | *n = 0; |
---|
| 73 | |
---|
| 74 | #ifdef STRING_TO_FLOAT |
---|
| 75 | result = STRING_TO_METHOD( s, &end ); |
---|
| 76 | #elif defined(STRING_TO_INTEGER) |
---|
| 77 | result = STRING_TO_METHOD( s, &end, base ); |
---|
| 78 | #endif |
---|
| 79 | |
---|
| 80 | /* If the user wants the end pointer back, then return it. */ |
---|
| 81 | if ( endptr ) |
---|
| 82 | *endptr = end; |
---|
| 83 | |
---|
| 84 | /* nothing was converted */ |
---|
| 85 | if ( end == s ) |
---|
| 86 | return false; |
---|
| 87 | |
---|
| 88 | /* there was a conversion error */ |
---|
| 89 | if ( (result == ZERO) && errno ) |
---|
| 90 | return false; |
---|
| 91 | |
---|
| 92 | #ifdef STRING_TO_MAX |
---|
| 93 | /* there was an overflow */ |
---|
| 94 | if ( (result == STRING_TO_MAX) && (errno == ERANGE)) |
---|
| 95 | return false; |
---|
| 96 | #endif |
---|
| 97 | |
---|
| 98 | #ifdef STRING_TO_MIN |
---|
| 99 | /* there was an underflow */ |
---|
| 100 | if ( (result == STRING_TO_MIN) && (errno == ERANGE)) |
---|
| 101 | return false; |
---|
| 102 | #endif |
---|
| 103 | |
---|
[35d09ba] | 104 | *n = (STRING_TO_TYPE) result; |
---|
[e8d59ca] | 105 | return true; |
---|
| 106 | } |
---|
| 107 | |
---|