basespec.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*====================================================================*
  2. *
  3. * uint64_t basespec (char const * string, unsigned base, unsigned size);
  4. *
  5. * number.h
  6. *
  7. * convert a character string to an equivalent unsigned integer and
  8. * return the result; terminate the program on failure;
  9. *
  10. * the base argument is the number base to be used for conversion;
  11. * base 0 permits the number base to be determined by the string
  12. * string prefix; 0b, 0d or 0x for binary, decimal or hex;
  13. *
  14. * this implementation accepts a minus sign in order to negate any
  15. * number in any base;
  16. *
  17. * the size argument is the maximum number of bytes permitted in the
  18. * result;
  19. *
  20. * Motley Tools by Charles Maier;
  21. * Copyright (c) 2001-2006 by Charles Maier Associates;
  22. * Licensed under the Internet Software Consortium License;
  23. *
  24. *--------------------------------------------------------------------*/
  25. #ifndef BASESPEC_SOURCE
  26. #define BASESPEC_SOURCE
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include "../tools/number.h"
  30. #include "../tools/error.h"
  31. uint64_t basespec (char const * string, unsigned base, unsigned size)
  32. {
  33. char const * number = string;
  34. unsigned radix = RADIX_DEC;
  35. signed scale = 1;
  36. uint64_t limit = 0;
  37. uint64_t value = 0;
  38. unsigned digit = 0;
  39. limit = ~limit;
  40. if (size < sizeof (limit))
  41. {
  42. limit <<= size << 3;
  43. limit = ~limit;
  44. }
  45. if (base)
  46. {
  47. radix = base;
  48. }
  49. if (* number == '=')
  50. {
  51. number++;
  52. }
  53. else if (* number == '+')
  54. {
  55. number++;
  56. }
  57. else if (* number == '-')
  58. {
  59. number++;
  60. scale = -1;
  61. }
  62. if (*number == '0')
  63. {
  64. number++;
  65. if ((*number == 'b') || (*number == 'B'))
  66. {
  67. radix = RADIX_BIN;
  68. number++;
  69. }
  70. else if ((*number == 'd') || (*number == 'D'))
  71. {
  72. radix = RADIX_DEC;
  73. number++;
  74. }
  75. else if ((*number == 'x') || (*number == 'X'))
  76. {
  77. radix = RADIX_HEX;
  78. number++;
  79. }
  80. }
  81. if ((base) && (base != radix))
  82. {
  83. error (1, EINVAL, "%s is not base %d notation", string, base);
  84. }
  85. while ((digit = todigit (*number)) < radix)
  86. {
  87. value *= radix;
  88. value += digit;
  89. if (value > limit)
  90. {
  91. error (1, ERANGE, "%s exceeds %d bits", string, (size << 3));
  92. }
  93. number++;
  94. }
  95. #ifdef WIN32
  96. while (isspace (*number))
  97. {
  98. number++;
  99. }
  100. #endif
  101. if (*number)
  102. {
  103. error (1, EINVAL, "%s is not base %d notation", string, radix);
  104. }
  105. return (scale * value);
  106. }
  107. #endif