atoi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <stdlib.h>
  28. #include <ctype.h>
  29. #define LONG_IS_INT 1
  30. static int hexval(char c)
  31. {
  32. if (c >= '0' && c <= '9')
  33. return c - '0';
  34. else if (c >= 'a' && c <= 'f')
  35. return c - 'a' + 10;
  36. else if (c >= 'A' && c <= 'F')
  37. return c - 'A' + 10;
  38. return 0;
  39. }
  40. int atoi(const char *num)
  41. {
  42. #if !LONG_IS_INT
  43. // XXX fail
  44. #else
  45. return atol(num);
  46. #endif
  47. }
  48. unsigned int atoui(const char *num)
  49. {
  50. #if !LONG_IS_INT
  51. // XXX fail
  52. #else
  53. return atoul(num);
  54. #endif
  55. }
  56. long atol(const char *num)
  57. {
  58. long value = 0;
  59. int neg = 0;
  60. if (num[0] == '0' && num[1] == 'x') {
  61. // hex
  62. num += 2;
  63. while (*num && isxdigit(*num))
  64. value = value * 16 + hexval(*num++);
  65. } else {
  66. // decimal
  67. if (num[0] == '-') {
  68. neg = 1;
  69. num++;
  70. }
  71. while (*num && isdigit(*num))
  72. value = value * 10 + *num++ - '0';
  73. }
  74. if (neg)
  75. value = -value;
  76. return value;
  77. }
  78. unsigned long atoul(const char *num)
  79. {
  80. unsigned long value = 0;
  81. if (num[0] == '0' && num[1] == 'x') {
  82. // hex
  83. num += 2;
  84. while (*num && isxdigit(*num))
  85. value = value * 16 + hexval(*num++);
  86. } else {
  87. // decimal
  88. while (*num && isdigit(*num))
  89. value = value * 10 + *num++ - '0';
  90. }
  91. return value;
  92. }
  93. long long atoll(const char *num)
  94. {
  95. long long value = 0;
  96. int neg = 0;
  97. if (num[0] == '0' && num[1] == 'x') {
  98. // hex
  99. num += 2;
  100. while (*num && isxdigit(*num))
  101. value = value * 16 + hexval(*num++);
  102. } else {
  103. // decimal
  104. if (num[0] == '-') {
  105. neg = 1;
  106. num++;
  107. }
  108. while (*num && isdigit(*num))
  109. value = value * 10 + *num++ - '0';
  110. }
  111. if (neg)
  112. value = -value;
  113. return value;
  114. }