Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2001, Travis Geiselbrecht. All rights reserved. |
| 3 | ** Distributed under the terms of the NewOS License. |
| 4 | */ |
| 5 | /* |
| 6 | * Copyright (c) 2008 Travis Geiselbrecht |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining |
| 9 | * a copy of this software and associated documentation files |
| 10 | * (the "Software"), to deal in the Software without restriction, |
| 11 | * including without limitation the rights to use, copy, modify, merge, |
| 12 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 | * and to permit persons to whom the Software is furnished to do so, |
| 14 | * subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be |
| 17 | * included in all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <ctype.h> |
| 30 | |
| 31 | #define LONG_IS_INT 1 |
| 32 | |
| 33 | static int hexval(char c) |
| 34 | { |
| 35 | if (c >= '0' && c <= '9') |
| 36 | return c - '0'; |
| 37 | else if (c >= 'a' && c <= 'f') |
| 38 | return c - 'a' + 10; |
| 39 | else if (c >= 'A' && c <= 'F') |
| 40 | return c - 'A' + 10; |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int atoi(const char *num) |
| 46 | { |
| 47 | #if !LONG_IS_INT |
| 48 | // XXX fail |
| 49 | #else |
| 50 | return atol(num); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | unsigned int atoui(const char *num) |
| 55 | { |
| 56 | #if !LONG_IS_INT |
| 57 | // XXX fail |
| 58 | #else |
| 59 | return atoul(num); |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | long atol(const char *num) |
| 64 | { |
| 65 | long value = 0; |
| 66 | int neg = 0; |
| 67 | |
| 68 | if (num[0] == '0' && num[1] == 'x') { |
| 69 | // hex |
| 70 | num += 2; |
| 71 | while (*num && isxdigit(*num)) |
| 72 | value = value * 16 + hexval(*num++); |
| 73 | } else { |
| 74 | // decimal |
| 75 | if (num[0] == '-') { |
| 76 | neg = 1; |
| 77 | num++; |
| 78 | } |
| 79 | while (*num && isdigit(*num)) |
| 80 | value = value * 10 + *num++ - '0'; |
| 81 | } |
| 82 | |
| 83 | if (neg) |
| 84 | value = -value; |
| 85 | |
| 86 | return value; |
| 87 | } |
| 88 | |
| 89 | unsigned long atoul(const char *num) |
| 90 | { |
| 91 | unsigned long value = 0; |
| 92 | if (num[0] == '0' && num[1] == 'x') { |
| 93 | // hex |
| 94 | num += 2; |
| 95 | while (*num && isxdigit(*num)) |
| 96 | value = value * 16 + hexval(*num++); |
| 97 | } else { |
| 98 | // decimal |
| 99 | while (*num && isdigit(*num)) |
| 100 | value = value * 10 + *num++ - '0'; |
| 101 | } |
| 102 | |
| 103 | return value; |
| 104 | } |
| 105 | |